From f32bf19f50a726730d6c3f43cfc3a8cb75d0c319 Mon Sep 17 00:00:00 2001 From: dbuechel Date: Tue, 26 Feb 2019 08:40:00 +0100 Subject: [PATCH] SEBWIN-296: Extended unit tests for runtime host and controller. --- .../Communication/RuntimeHostTests.cs | 16 +++ .../RuntimeControllerTests.cs | 112 +++++++++++++++++- 2 files changed, 126 insertions(+), 2 deletions(-) diff --git a/SafeExamBrowser.Runtime.UnitTests/Communication/RuntimeHostTests.cs b/SafeExamBrowser.Runtime.UnitTests/Communication/RuntimeHostTests.cs index 4700efe9..b498bf01 100644 --- a/SafeExamBrowser.Runtime.UnitTests/Communication/RuntimeHostTests.cs +++ b/SafeExamBrowser.Runtime.UnitTests/Communication/RuntimeHostTests.cs @@ -274,6 +274,22 @@ namespace SafeExamBrowser.Runtime.UnitTests.Communication Assert.AreEqual(SimpleResponsePurport.UnknownMessage, (response as SimpleResponse)?.Purport); } + [TestMethod] + public void MustNotFailIfNoEventHandlersSubscribed() + { + sut.AllowConnection = true; + sut.StartupToken = Guid.Empty; + + var token = sut.Connect(Guid.Empty).CommunicationToken.Value; + + sut.Send(new SimpleMessage(SimpleMessagePurport.ClientIsReady) { CommunicationToken = token }); + sut.Send(new SimpleMessage(SimpleMessagePurport.ConfigurationNeeded) { CommunicationToken = token }); + sut.Send(new SimpleMessage(SimpleMessagePurport.RequestShutdown) { CommunicationToken = token }); + sut.Send(new MessageBoxReplyMessage(Guid.Empty, MessageBoxResult.Cancel) { CommunicationToken = token }); + sut.Send(new PasswordReplyMessage(Guid.Empty, false, "") { CommunicationToken = token }); + sut.Send(new ReconfigurationMessage("") { CommunicationToken = token }); + } + private class TestMessage : Message { }; } } diff --git a/SafeExamBrowser.Runtime.UnitTests/RuntimeControllerTests.cs b/SafeExamBrowser.Runtime.UnitTests/RuntimeControllerTests.cs index 6e2e6f25..c78a5171 100644 --- a/SafeExamBrowser.Runtime.UnitTests/RuntimeControllerTests.cs +++ b/SafeExamBrowser.Runtime.UnitTests/RuntimeControllerTests.cs @@ -16,6 +16,7 @@ using SafeExamBrowser.Contracts.Communication.Proxies; using SafeExamBrowser.Contracts.Configuration; using SafeExamBrowser.Contracts.Configuration.Settings; using SafeExamBrowser.Contracts.Core.OperationModel; +using SafeExamBrowser.Contracts.Core.OperationModel.Events; using SafeExamBrowser.Contracts.I18n; using SafeExamBrowser.Contracts.Logging; using SafeExamBrowser.Contracts.UserInterface; @@ -114,8 +115,7 @@ namespace SafeExamBrowser.Runtime.UnitTests public void ClientProxy_MustShutdownWhenConnectionLost() { StartSession(); - - clientProcess.Raise(c => c.Terminated += null, -1); + clientProxy.Raise(c => c.ConnectionLost += null); messageBox.Verify(m => m.Show( It.IsAny(), @@ -201,6 +201,19 @@ namespace SafeExamBrowser.Runtime.UnitTests shutdown.Verify(s => s(), Times.Once); } + [TestMethod] + public void Operations_MustAllowToAbortStartupForClientConfiguration() + { + var args = new ConfigurationCompletedEventArgs(); + + messageBox.Setup(m => m.Show(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(MessageBoxResult.Yes); + + sut.TryStart(); + sessionSequence.Raise(s => s.ActionRequired += null, args); + + Assert.IsTrue(args.AbortStartup); + } + [TestMethod] public void Operations_MustRequestPasswordViaDialogOnDefaultDesktop() { @@ -337,6 +350,45 @@ namespace SafeExamBrowser.Runtime.UnitTests It.IsAny()), Times.Once); } + [TestMethod] + public void Operations_MustUpdateProgress() + { + var args = new ProgressChangedEventArgs + { + CurrentValue = 23, + IsIndeterminate = true, + MaxValue = 150, + Progress = true, + Regress = true + }; + var runtimeWindow = new Mock(); + + uiFactory.Setup(u => u.CreateRuntimeWindow(It.IsAny())).Returns(runtimeWindow.Object); + + sut.TryStart(); + sessionSequence.Raise(o => o.ProgressChanged += null, args); + + runtimeWindow.Verify(s => s.SetValue(It.Is(i => i == args.CurrentValue)), Times.Once); + runtimeWindow.Verify(s => s.SetIndeterminate(), Times.Once); + runtimeWindow.Verify(s => s.SetMaxValue(It.Is(i => i == args.MaxValue)), Times.Once); + runtimeWindow.Verify(s => s.Progress(), Times.Once); + runtimeWindow.Verify(s => s.Regress(), Times.Once); + } + + [TestMethod] + public void Operations_MustUpdateStatus() + { + var key = TextKey.OperationStatus_EmptyClipboard; + var runtimeWindow = new Mock(); + + uiFactory.Setup(u => u.CreateRuntimeWindow(It.IsAny())).Returns(runtimeWindow.Object); + + sut.TryStart(); + sessionSequence.Raise(o => o.StatusChanged += null, key); + + runtimeWindow.Verify(s => s.UpdateStatus(It.Is(k => k == key), It.IsAny()), Times.Once); + } + [TestMethod] public void Shutdown_MustRevertSessionThenBootstrapSequence() { @@ -392,6 +444,26 @@ namespace SafeExamBrowser.Runtime.UnitTests Assert.AreEqual(1, bootstrap); } + [TestMethod] + public void Shutdown_MustIndicateFailureToUser() + { + var order = 0; + var bootstrap = 0; + var session = 0; + + sut.TryStart(); + + bootstrapSequence.Reset(); + sessionSequence.Reset(); + + bootstrapSequence.Setup(b => b.TryRevert()).Returns(OperationResult.Failed).Callback(() => bootstrap = ++order); + sessionSequence.Setup(b => b.TryRevert()).Returns(OperationResult.Success).Callback(() => session = ++order); + + sut.Terminate(); + + messageBox.Verify(m => m.Show(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.AtLeastOnce); + } + [TestMethod] public void Startup_MustPerformBootstrapThenSessionSequence() { @@ -440,6 +512,42 @@ namespace SafeExamBrowser.Runtime.UnitTests Assert.AreEqual(0, session); } + [TestMethod] + public void Startup_MustTerminateOnSessionStartFailure() + { + bootstrapSequence.Setup(b => b.TryPerform()).Returns(OperationResult.Success); + sessionSequence.Setup(b => b.TryPerform()).Returns(OperationResult.Failed).Callback(() => sessionContext.Current = currentSession.Object); + sessionContext.Current = null; + + var success = sut.TryStart(); + + bootstrapSequence.Verify(b => b.TryPerform(), Times.Once); + bootstrapSequence.Verify(b => b.TryRevert(), Times.Never); + sessionSequence.Verify(b => b.TryPerform(), Times.Once); + sessionSequence.Verify(b => b.TryRepeat(), Times.Never); + sessionSequence.Verify(b => b.TryRevert(), Times.Once); + + shutdown.Verify(s => s(), Times.Once); + } + + [TestMethod] + public void Startup_MustNotTerminateOnSessionStartAbortion() + { + bootstrapSequence.Setup(b => b.TryPerform()).Returns(OperationResult.Success); + sessionSequence.Setup(b => b.TryPerform()).Returns(OperationResult.Aborted).Callback(() => sessionContext.Current = currentSession.Object); + sessionContext.Current = null; + + var success = sut.TryStart(); + + bootstrapSequence.Verify(b => b.TryPerform(), Times.Once); + bootstrapSequence.Verify(b => b.TryRevert(), Times.Never); + sessionSequence.Verify(b => b.TryPerform(), Times.Once); + sessionSequence.Verify(b => b.TryRepeat(), Times.Never); + sessionSequence.Verify(b => b.TryRevert(), Times.Never); + + shutdown.Verify(s => s(), Times.Never); + } + private void StartSession() { bootstrapSequence.Setup(b => b.TryPerform()).Returns(OperationResult.Success);