diff --git a/SafeExamBrowser.Runtime.UnitTests/Operations/ServiceOperationTests.cs b/SafeExamBrowser.Runtime.UnitTests/Operations/ServiceOperationTests.cs index 644d4fd4..4e7641dc 100644 --- a/SafeExamBrowser.Runtime.UnitTests/Operations/ServiceOperationTests.cs +++ b/SafeExamBrowser.Runtime.UnitTests/Operations/ServiceOperationTests.cs @@ -15,7 +15,9 @@ using SafeExamBrowser.Contracts.Configuration; using SafeExamBrowser.Contracts.Configuration.Settings; using SafeExamBrowser.Contracts.Core.OperationModel; using SafeExamBrowser.Contracts.Logging; +using SafeExamBrowser.Contracts.UserInterface.MessageBox; using SafeExamBrowser.Runtime.Operations; +using SafeExamBrowser.Runtime.Operations.Events; namespace SafeExamBrowser.Runtime.UnitTests.Operations { @@ -133,16 +135,34 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations service.Verify(s => s.StartSession(It.IsAny()), Times.Never); } + [TestMethod] + public void Perform_MustHandleCommunicationFailureWhenStartingSession() + { + service.SetupGet(s => s.IsConnected).Returns(true); + service.Setup(s => s.Connect(null, true)).Returns(true); + service.Setup(s => s.StartSession(It.IsAny())).Returns(new CommunicationResult(false)); + + var result = sut.Perform(); + + service.Verify(s => s.StartSession(It.IsAny()), Times.Once); + + Assert.AreEqual(OperationResult.Failed, result); + } + [TestMethod] public void Perform_MustFailIfServiceMandatoryAndNotAvailable() { + var errorShown = false; + service.SetupGet(s => s.IsConnected).Returns(false); service.Setup(s => s.Connect(null, true)).Returns(false); settings.ServicePolicy = ServicePolicy.Mandatory; + sut.ActionRequired += (args) => errorShown = args is MessageEventArgs m && m.Icon == MessageBoxIcon.Error; var result = sut.Perform(); Assert.AreEqual(OperationResult.Failed, result); + Assert.IsTrue(errorShown); } [TestMethod] @@ -158,6 +178,22 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations Assert.AreEqual(OperationResult.Success, result); } + [TestMethod] + public void Perform_MustShowWarningIfServiceNotAvailableAndPolicyWarn() + { + var warningShown = false; + + service.SetupGet(s => s.IsConnected).Returns(false); + service.Setup(s => s.Connect(null, true)).Returns(false); + settings.ServicePolicy = ServicePolicy.Warn; + sut.ActionRequired += (args) => warningShown = args is MessageEventArgs m && m.Icon == MessageBoxIcon.Warning; + + var result = sut.Perform(); + + Assert.AreEqual(OperationResult.Success, result); + Assert.IsTrue(warningShown); + } + [TestMethod] public void Repeat_MustStopCurrentAndStartNewSession() { @@ -178,6 +214,26 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations Assert.AreEqual(OperationResult.Success, result); } + [TestMethod] + public void Repeat_MustEstablishConnectionIfNotConnected() + { + PerformNormally(); + + service.Reset(); + service.SetupGet(s => s.IsConnected).Returns(false); + service.Setup(s => s.Connect(null, true)).Returns(true); + service + .Setup(s => s.StopSession(It.IsAny())) + .Returns(new CommunicationResult(true)) + .Callback(() => runtimeHost.Raise(h => h.ServiceSessionStopped += null)); + + var result = sut.Repeat(); + + service.Verify(s => s.Connect(It.IsAny(), It.IsAny()), Times.Once); + + Assert.AreEqual(OperationResult.Success, result); + } + [TestMethod] public void Repeat_MustFailIfCurrentSessionWasNotStoppedSuccessfully() { @@ -273,6 +329,21 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations Assert.AreEqual(OperationResult.Failed, result); } + [TestMethod] + public void Revert_MustFailIfSessionStopUnsuccessful() + { + service + .Setup(s => s.StopSession(It.IsAny())) + .Returns(new CommunicationResult(true)) + .Callback(() => runtimeHost.Raise(h => h.ServiceFailed += null)); + + PerformNormally(); + + var result = sut.Revert(); + + Assert.AreEqual(OperationResult.Failed, result); + } + [TestMethod] public void Revert_MustFailIfSessionNotStoppedWithinTimeout() {