From 817f598d8a2aee6fea11f63f4c7fc9c0a48ff3e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20B=C3=BCchel?= Date: Tue, 4 Jul 2023 17:19:42 +0200 Subject: [PATCH] SEBWIN-679: Extended unit tests for core library and attempted to fix open windows test for external application. --- .../ExternalApplicationTests.cs | 6 ++++-- .../CommunicationHostOperationTests.cs | 16 +++++++++++++++- .../Operations/DelegateOperationTests.cs | 11 +++++++++++ .../Operations/I18nOperationTests.cs | 9 +++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/SafeExamBrowser.Applications.UnitTests/ExternalApplicationTests.cs b/SafeExamBrowser.Applications.UnitTests/ExternalApplicationTests.cs index 2b7276f6..2f7b5129 100644 --- a/SafeExamBrowser.Applications.UnitTests/ExternalApplicationTests.cs +++ b/SafeExamBrowser.Applications.UnitTests/ExternalApplicationTests.cs @@ -10,6 +10,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading; +using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using SafeExamBrowser.Core.Contracts.Resources.Icons; @@ -51,7 +52,7 @@ namespace SafeExamBrowser.Applications.UnitTests [TestMethod] public void GetWindows_MustCorrectlyReturnOpenWindows() { - var openWindows = new List { new IntPtr(123), new IntPtr(234), new IntPtr(456), new IntPtr(345), new IntPtr(567), new IntPtr(789), }; + var openWindows = new List { new IntPtr(123), new IntPtr(234), new IntPtr(456), new IntPtr(345), new IntPtr(567), new IntPtr(789) }; var process1 = new Mock(); var process2 = new Mock(); var sync = new AutoResetEvent(false); @@ -84,8 +85,9 @@ namespace SafeExamBrowser.Applications.UnitTests Assert.IsTrue(windows.Any(w => w.Handle == new IntPtr(567))); nativeMethods.Setup(n => n.GetOpenWindows()).Returns(openWindows.Skip(2)); - process2.Raise(p => p.Terminated += null, default(int)); + Task.Run(() => process2.Raise(p => p.Terminated += null, default(int))); + sync.WaitOne(); sync.WaitOne(); windows = sut.GetWindows(); diff --git a/SafeExamBrowser.Core.UnitTests/Operations/CommunicationHostOperationTests.cs b/SafeExamBrowser.Core.UnitTests/Operations/CommunicationHostOperationTests.cs index e28e238f..3f636b19 100644 --- a/SafeExamBrowser.Core.UnitTests/Operations/CommunicationHostOperationTests.cs +++ b/SafeExamBrowser.Core.UnitTests/Operations/CommunicationHostOperationTests.cs @@ -10,8 +10,8 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using SafeExamBrowser.Communication.Contracts; using SafeExamBrowser.Core.Contracts.OperationModel; -using SafeExamBrowser.Logging.Contracts; using SafeExamBrowser.Core.Operations; +using SafeExamBrowser.Logging.Contracts; namespace SafeExamBrowser.Core.UnitTests.Operations { @@ -83,5 +83,19 @@ namespace SafeExamBrowser.Core.UnitTests.Operations hostMock.Verify(h => h.Stop(), Times.Once); hostMock.Verify(h => h.Start(), Times.Never); } + + [TestMethod] + public void MustFireStatusChangedEvent() + { + var fired = 0; + + sut.StatusChanged += (_) => fired++; + + sut.Perform(); + sut.Repeat(); + sut.Revert(); + + Assert.AreEqual(3, fired); + } } } diff --git a/SafeExamBrowser.Core.UnitTests/Operations/DelegateOperationTests.cs b/SafeExamBrowser.Core.UnitTests/Operations/DelegateOperationTests.cs index c6396647..fffb1e20 100644 --- a/SafeExamBrowser.Core.UnitTests/Operations/DelegateOperationTests.cs +++ b/SafeExamBrowser.Core.UnitTests/Operations/DelegateOperationTests.cs @@ -64,5 +64,16 @@ namespace SafeExamBrowser.Core.UnitTests.Operations Assert.AreEqual(OperationResult.Success, repeat); Assert.AreEqual(OperationResult.Success, revert); } + + [TestMethod] + public void MustNotFireEvents() + { + var sut = new DelegateOperation(default, default, default); + + sut.ActionRequired += (_) => Assert.Fail(); + sut.StatusChanged += (_) => Assert.Fail(); + + sut.Perform(); + } } } diff --git a/SafeExamBrowser.Core.UnitTests/Operations/I18nOperationTests.cs b/SafeExamBrowser.Core.UnitTests/Operations/I18nOperationTests.cs index 3b3cf2f9..1db3289a 100644 --- a/SafeExamBrowser.Core.UnitTests/Operations/I18nOperationTests.cs +++ b/SafeExamBrowser.Core.UnitTests/Operations/I18nOperationTests.cs @@ -48,5 +48,14 @@ namespace SafeExamBrowser.Core.UnitTests.Operations sut.Revert(); text.VerifyNoOtherCalls(); } + + [TestMethod] + public void MustNotFireEvents() + { + sut.ActionRequired += (_) => Assert.Fail(); + sut.StatusChanged += (_) => Assert.Fail(); + + sut.Perform(); + } } }