From d4295b3753cd0cb672ffca585cf612a1425a0c9d Mon Sep 17 00:00:00 2001 From: dbuechel Date: Wed, 19 Sep 2018 14:54:19 +0200 Subject: [PATCH] SEBWIN-220: Implemented unit tests for KioskModeOperation. --- .../Operations/KioskModeOperationTests.cs | 207 +++++++++++++++++- 1 file changed, 204 insertions(+), 3 deletions(-) diff --git a/SafeExamBrowser.Runtime.UnitTests/Operations/KioskModeOperationTests.cs b/SafeExamBrowser.Runtime.UnitTests/Operations/KioskModeOperationTests.cs index 47f40dcf..19834db0 100644 --- a/SafeExamBrowser.Runtime.UnitTests/Operations/KioskModeOperationTests.cs +++ b/SafeExamBrowser.Runtime.UnitTests/Operations/KioskModeOperationTests.cs @@ -7,16 +7,217 @@ */ using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using SafeExamBrowser.Contracts.Configuration; +using SafeExamBrowser.Contracts.Configuration.Settings; +using SafeExamBrowser.Contracts.Logging; +using SafeExamBrowser.Contracts.WindowsApi; +using SafeExamBrowser.Runtime.Operations; namespace SafeExamBrowser.Runtime.UnitTests.Operations { [TestClass] public class KioskModeOperationTests { - [TestMethod] - public void TODO() + private Mock configuration; + private Mock desktopFactory; + private Mock explorerShell; + private Mock logger; + private Mock processFactory; + private Settings settings; + private KioskModeOperation sut; + + [TestInitialize] + public void Initialize() { - Assert.Fail(); + configuration = new Mock(); + desktopFactory = new Mock(); + explorerShell = new Mock(); + logger = new Mock(); + processFactory = new Mock(); + settings = new Settings(); + + configuration.SetupGet(c => c.CurrentSettings).Returns(settings); + + sut = new KioskModeOperation(configuration.Object, desktopFactory.Object, explorerShell.Object, logger.Object, processFactory.Object); + } + + [TestMethod] + public void MustCorrectlyInitializeCreateNewDesktop() + { + var originalDesktop = new Mock(); + var newDesktop = new Mock(); + var order = 0; + var getCurrrent = 0; + var createNew = 0; + var activate = 0; + var setStartup = 0; + + settings.KioskMode = KioskMode.CreateNewDesktop; + + desktopFactory.Setup(f => f.GetCurrent()).Callback(() => getCurrrent = ++order).Returns(originalDesktop.Object); + desktopFactory.Setup(f => f.CreateNew(It.IsAny())).Callback(() => createNew = ++order).Returns(newDesktop.Object); + newDesktop.Setup(d => d.Activate()).Callback(() => activate = ++order); + processFactory.SetupSet(f => f.StartupDesktop = It.IsAny()).Callback(() => setStartup = ++order); + + sut.Perform(); + + desktopFactory.Verify(f => f.GetCurrent(), Times.Once); + desktopFactory.Verify(f => f.CreateNew(It.IsAny()), Times.Once); + newDesktop.Verify(d => d.Activate(), Times.Once); + processFactory.VerifySet(f => f.StartupDesktop = newDesktop.Object, Times.Once); + + Assert.AreEqual(1, getCurrrent); + Assert.AreEqual(2, createNew); + Assert.AreEqual(3, activate); + Assert.AreEqual(4, setStartup); + } + + [TestMethod] + public void MustCorrectlyInitializeDisableExplorerShell() + { + settings.KioskMode = KioskMode.DisableExplorerShell; + + sut.Perform(); + + explorerShell.Verify(s => s.Terminate(), Times.Once); + } + + [TestMethod] + public void MustCorrectlyRevertCreateNewDesktop() + { + var originalDesktop = new Mock(); + var newDesktop = new Mock(); + var order = 0; + var activate = 0; + var setStartup = 0; + var close = 0; + + settings.KioskMode = KioskMode.CreateNewDesktop; + + desktopFactory.Setup(f => f.GetCurrent()).Returns(originalDesktop.Object); + desktopFactory.Setup(f => f.CreateNew(It.IsAny())).Returns(newDesktop.Object); + originalDesktop.Setup(d => d.Activate()).Callback(() => activate = ++order); + processFactory.SetupSet(f => f.StartupDesktop = It.Is(d => d == originalDesktop.Object)).Callback(() => setStartup = ++order); + newDesktop.Setup(d => d.Close()).Callback(() => close = ++order); + + sut.Perform(); + sut.Revert(); + + originalDesktop.Verify(d => d.Activate(), Times.Once); + processFactory.VerifySet(f => f.StartupDesktop = originalDesktop.Object, Times.Once); + newDesktop.Verify(d => d.Close(), Times.Once); + + Assert.AreEqual(1, activate); + Assert.AreEqual(2, setStartup); + Assert.AreEqual(3, close); + } + + [TestMethod] + public void MustCorrectlyRevertDisableExplorerShell() + { + settings.KioskMode = KioskMode.DisableExplorerShell; + + sut.Perform(); + sut.Revert(); + + explorerShell.Verify(s => s.Start(), Times.Once); + } + + [TestMethod] + public void MustCorrectlySwitchToOtherKioskModeWhenRepeating() + { + var originalDesktop = new Mock(); + var newDesktop = new Mock(); + + desktopFactory.Setup(f => f.GetCurrent()).Returns(originalDesktop.Object); + desktopFactory.Setup(f => f.CreateNew(It.IsAny())).Returns(newDesktop.Object); + + settings.KioskMode = KioskMode.CreateNewDesktop; + sut.Perform(); + + explorerShell.Verify(s => s.Terminate(), Times.Never); + explorerShell.Verify(s => s.Start(), Times.Never); + newDesktop.Verify(d => d.Activate(), Times.Once); + newDesktop.Verify(d => d.Close(), Times.Never); + originalDesktop.Verify(d => d.Activate(), Times.Never); + + settings.KioskMode = KioskMode.DisableExplorerShell; + sut.Repeat(); + + explorerShell.Verify(s => s.Terminate(), Times.Once); + explorerShell.Verify(s => s.Start(), Times.Never); + newDesktop.Verify(d => d.Activate(), Times.Once); + newDesktop.Verify(d => d.Close(), Times.Once); + originalDesktop.Verify(d => d.Activate(), Times.Once); + + settings.KioskMode = KioskMode.CreateNewDesktop; + sut.Repeat(); + + explorerShell.Verify(s => s.Terminate(), Times.Once); + explorerShell.Verify(s => s.Start(), Times.Once); + newDesktop.Verify(d => d.Activate(), Times.Exactly(2)); + newDesktop.Verify(d => d.Close(), Times.Once); + originalDesktop.Verify(d => d.Activate(), Times.Once); + } + + [TestMethod] + public void MustDoNothingWithoutKioskMode() + { + settings.KioskMode = KioskMode.None; + + sut.Perform(); + sut.Repeat(); + sut.Revert(); + + desktopFactory.VerifyNoOtherCalls(); + explorerShell.VerifyNoOtherCalls(); + processFactory.VerifyNoOtherCalls(); + } + + [TestMethod] + public void MustNotReinitializeCreateNewDesktopWhenRepeating() + { + var originalDesktop = new Mock(); + var newDesktop = new Mock(); + + settings.KioskMode = KioskMode.CreateNewDesktop; + + desktopFactory.Setup(f => f.GetCurrent()).Returns(originalDesktop.Object); + desktopFactory.Setup(f => f.CreateNew(It.IsAny())).Returns(newDesktop.Object); + + sut.Perform(); + sut.Repeat(); + sut.Repeat(); + sut.Repeat(); + sut.Repeat(); + sut.Revert(); + + desktopFactory.Verify(f => f.GetCurrent(), Times.Once); + desktopFactory.Verify(f => f.CreateNew(It.IsAny()), Times.Once); + newDesktop.Verify(d => d.Activate(), Times.Once); + processFactory.VerifySet(f => f.StartupDesktop = newDesktop.Object, Times.Once); + } + + [TestMethod] + public void MustNotReinitializeDisableExplorerShellWhenRepeating() + { + settings.KioskMode = KioskMode.DisableExplorerShell; + + sut.Perform(); + sut.Repeat(); + sut.Repeat(); + sut.Repeat(); + sut.Repeat(); + sut.Revert(); + + explorerShell.Verify(s => s.Terminate(), Times.Once); + } + + [TestMethod] + public void MustRestoreOriginalDesktopInCaseOfFailure() + { + } } }