diff --git a/SafeExamBrowser.Core.UnitTests/Behaviour/Operations/CommunicationOperationTests.cs b/SafeExamBrowser.Core.UnitTests/Behaviour/OperationModel/CommunicationOperationTests.cs similarity index 72% rename from SafeExamBrowser.Core.UnitTests/Behaviour/Operations/CommunicationOperationTests.cs rename to SafeExamBrowser.Core.UnitTests/Behaviour/OperationModel/CommunicationOperationTests.cs index 89977446..1decf907 100644 --- a/SafeExamBrowser.Core.UnitTests/Behaviour/Operations/CommunicationOperationTests.cs +++ b/SafeExamBrowser.Core.UnitTests/Behaviour/OperationModel/CommunicationOperationTests.cs @@ -8,11 +8,12 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; +using SafeExamBrowser.Contracts.Behaviour.OperationModel; using SafeExamBrowser.Contracts.Communication; using SafeExamBrowser.Contracts.Logging; using SafeExamBrowser.Core.Behaviour.OperationModel; -namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations +namespace SafeExamBrowser.Core.UnitTests.Behaviour.OperationModel { [TestClass] public class CommunicationOperationTests @@ -40,22 +41,38 @@ namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations hostMock.Setup(h => h.Stop()).Callback(() => stop = ++order); hostMock.Setup(h => h.Start()).Callback(() => start = ++order); - sut.Repeat(); + var result = sut.Repeat(); hostMock.Verify(h => h.Stop(), Times.Once); hostMock.Verify(h => h.Start(), Times.Once); Assert.AreEqual(stop, 1); Assert.AreEqual(start, 2); + Assert.AreEqual(OperationResult.Success, result); + } + + [TestMethod] + public void MustOnlyRestartHostOnRepeatIfNotRunning() + { + hostMock.SetupGet(h => h.IsRunning).Returns(true); + + var result = sut.Repeat(); + + hostMock.Verify(h => h.Start(), Times.Never); + hostMock.Verify(h => h.Stop(), Times.Never); + + Assert.AreEqual(OperationResult.Success, result); } [TestMethod] public void MustStartHostOnPerform() { - sut.Perform(); + var result = sut.Perform(); hostMock.Verify(h => h.Start(), Times.Once); hostMock.Verify(h => h.Stop(), Times.Never); + + Assert.AreEqual(OperationResult.Success, result); } [TestMethod] diff --git a/SafeExamBrowser.Core.UnitTests/Behaviour/OperationModel/DelayedInitializationOperationTests.cs b/SafeExamBrowser.Core.UnitTests/Behaviour/OperationModel/DelayedInitializationOperationTests.cs new file mode 100644 index 00000000..d7734a79 --- /dev/null +++ b/SafeExamBrowser.Core.UnitTests/Behaviour/OperationModel/DelayedInitializationOperationTests.cs @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using SafeExamBrowser.Contracts.Behaviour.OperationModel; +using SafeExamBrowser.Contracts.UserInterface; +using SafeExamBrowser.Core.Behaviour.OperationModel; + +namespace SafeExamBrowser.Core.UnitTests.Behaviour.OperationModel +{ + [TestClass] + public class DelayedInitializationOperationTests + { + private Mock operationMock; + + [TestInitialize] + public void Initialize() + { + operationMock = new Mock(); + } + + [TestMethod] + public void MustInstantiateOperationOnPerform() + { + var initialized = false; + IOperation initialize() + { + initialized = true; + + return operationMock.Object; + }; + + var sut = new DelayedInitializationOperation(initialize); + + sut.Perform(); + + Assert.IsTrue(initialized); + } + + [TestMethod] + [ExpectedException(typeof(NullReferenceException))] + public void MustNotInstantiateOperationOnRepeat() + { + IOperation initialize() + { + return operationMock.Object; + }; + + var sut = new DelayedInitializationOperation(initialize); + + sut.Repeat(); + } + + [TestMethod] + [ExpectedException(typeof(NullReferenceException))] + public void MustNotInstantiateOperationOnRevert() + { + IOperation initialize() + { + return operationMock.Object; + }; + + var sut = new DelayedInitializationOperation(initialize); + + sut.Revert(); + } + + [TestMethod] + public void MustReturnCorrectOperationResult() + { + IOperation initialize() + { + return operationMock.Object; + }; + + operationMock.Setup(o => o.Perform()).Returns(OperationResult.Success); + operationMock.Setup(o => o.Repeat()).Returns(OperationResult.Failed); + + var sut = new DelayedInitializationOperation(initialize); + var perform = sut.Perform(); + var repeat = sut.Repeat(); + + sut.Revert(); + + Assert.AreEqual(OperationResult.Success, perform); + Assert.AreEqual(OperationResult.Failed, repeat); + } + + [TestMethod] + public void MustUpdateProgressIndicator() + { + IOperation initialize() + { + return operationMock.Object; + }; + + var sut = new DelayedInitializationOperation(initialize) + { + ProgressIndicator = new Mock().Object + }; + + sut.Perform(); + sut.Repeat(); + sut.Revert(); + + operationMock.VerifySet(o => o.ProgressIndicator = It.IsAny(), Times.Exactly(3)); + } + + [TestMethod] + public void MustUseSameInstanceForAllOperations() + { + var first = true; + var operation = operationMock.Object; + IOperation initialize() + { + if (first) + { + return operation; + } + + return new Mock().Object; + }; + + var sut = new DelayedInitializationOperation(initialize); + + sut.Perform(); + sut.Repeat(); + sut.Revert(); + + operationMock.Verify(o => o.Perform(), Times.Once); + operationMock.Verify(o => o.Repeat(), Times.Once); + operationMock.Verify(o => o.Revert(), Times.Once); + } + } +} diff --git a/SafeExamBrowser.Core.UnitTests/Behaviour/OperationModel/DelegateOperationTests.cs b/SafeExamBrowser.Core.UnitTests/Behaviour/OperationModel/DelegateOperationTests.cs new file mode 100644 index 00000000..817227b7 --- /dev/null +++ b/SafeExamBrowser.Core.UnitTests/Behaviour/OperationModel/DelegateOperationTests.cs @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SafeExamBrowser.Core.Behaviour.OperationModel; + +namespace SafeExamBrowser.Core.UnitTests.Behaviour.OperationModel +{ + [TestClass] + public class DelegateOperationTests + { + [TestMethod] + public void MustExecutePerformAction() + { + var performed = false; + void perform() => performed = true; + var sut = new DelegateOperation(perform); + + sut.Perform(); + + Assert.IsTrue(performed); + } + + [TestMethod] + public void MustExecuteRepeatAction() + { + var repeated = false; + void repeat() => repeated = true; + var sut = new DelegateOperation(() => { }, repeat); + + sut.Repeat(); + + Assert.IsTrue(repeated); + } + + [TestMethod] + public void MustExecuteRevertAction() + { + var reverted = false; + void revert() => reverted = true; + var sut = new DelegateOperation(() => { }, revert: revert); + + sut.Revert(); + + Assert.IsTrue(reverted); + } + } +} diff --git a/SafeExamBrowser.Core.UnitTests/Behaviour/Operations/I18nOperationTests.cs b/SafeExamBrowser.Core.UnitTests/Behaviour/OperationModel/I18nOperationTests.cs similarity index 73% rename from SafeExamBrowser.Core.UnitTests/Behaviour/Operations/I18nOperationTests.cs rename to SafeExamBrowser.Core.UnitTests/Behaviour/OperationModel/I18nOperationTests.cs index 8d6e5a16..862907ad 100644 --- a/SafeExamBrowser.Core.UnitTests/Behaviour/Operations/I18nOperationTests.cs +++ b/SafeExamBrowser.Core.UnitTests/Behaviour/OperationModel/I18nOperationTests.cs @@ -8,11 +8,12 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; +using SafeExamBrowser.Contracts.Behaviour.OperationModel; using SafeExamBrowser.Contracts.I18n; using SafeExamBrowser.Contracts.Logging; using SafeExamBrowser.Core.Behaviour.OperationModel; -namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations +namespace SafeExamBrowser.Core.UnitTests.Behaviour.OperationModel { [TestClass] public class I18nOperationTests @@ -34,9 +35,19 @@ namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations [TestMethod] public void MustPerformCorrectly() { - sut.Perform(); + var result = sut.Perform(); textMock.Verify(t => t.Initialize(It.IsAny()), Times.Once); + + Assert.AreEqual(OperationResult.Success, result); + } + + [TestMethod] + public void MustRepeatCorrectly() + { + var result = sut.Repeat(); + + Assert.AreEqual(OperationResult.Success, result); } } } diff --git a/SafeExamBrowser.Core.UnitTests/Behaviour/Operations/OperationSequenceTests.cs b/SafeExamBrowser.Core.UnitTests/Behaviour/OperationModel/OperationSequenceTests.cs similarity index 99% rename from SafeExamBrowser.Core.UnitTests/Behaviour/Operations/OperationSequenceTests.cs rename to SafeExamBrowser.Core.UnitTests/Behaviour/OperationModel/OperationSequenceTests.cs index 229706e9..5866ddcd 100644 --- a/SafeExamBrowser.Core.UnitTests/Behaviour/Operations/OperationSequenceTests.cs +++ b/SafeExamBrowser.Core.UnitTests/Behaviour/OperationModel/OperationSequenceTests.cs @@ -15,7 +15,7 @@ using SafeExamBrowser.Contracts.Logging; using SafeExamBrowser.Contracts.UserInterface; using SafeExamBrowser.Core.Behaviour.OperationModel; -namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations +namespace SafeExamBrowser.Core.UnitTests.Behaviour.OperationModel { [TestClass] public class OperationSequenceTests diff --git a/SafeExamBrowser.Core.UnitTests/Behaviour/Operations/DelayedInitializationOperationTests.cs b/SafeExamBrowser.Core.UnitTests/Behaviour/Operations/DelayedInitializationOperationTests.cs deleted file mode 100644 index 1b31c9ed..00000000 --- a/SafeExamBrowser.Core.UnitTests/Behaviour/Operations/DelayedInitializationOperationTests.cs +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET) - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations -{ - [TestClass] - public class DelayedInitializationOperationTests - { - [TestMethod] - public void TODO() - { - Assert.Fail(); - } - } -} diff --git a/SafeExamBrowser.Core.UnitTests/Behaviour/Operations/DelegateOperationTests.cs b/SafeExamBrowser.Core.UnitTests/Behaviour/Operations/DelegateOperationTests.cs deleted file mode 100644 index fa52d024..00000000 --- a/SafeExamBrowser.Core.UnitTests/Behaviour/Operations/DelegateOperationTests.cs +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET) - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations -{ - [TestClass] - public class DelegateOperationTests - { - [TestMethod] - public void TODO() - { - Assert.Fail(); - } - } -} diff --git a/SafeExamBrowser.Core.UnitTests/Logging/LoggerTests.cs b/SafeExamBrowser.Core.UnitTests/Logging/LoggerTests.cs index e0bc98d2..97b5f0c0 100644 --- a/SafeExamBrowser.Core.UnitTests/Logging/LoggerTests.cs +++ b/SafeExamBrowser.Core.UnitTests/Logging/LoggerTests.cs @@ -64,6 +64,26 @@ namespace SafeExamBrowser.Core.UnitTests.Logging Assert.IsTrue(content.Text.Equals((log[7] as ILogText).Text)); } + [TestMethod] + public void MustAddInnerExceptionsToLog() + { + var sut = new Logger(); + var outerMessage = "Some message for the outer exception"; + var innerMessage = "BAAAAM! Inner one here."; + var innerInnerMessage = "Yikes, a null reference..."; + var exception = new Exception(outerMessage, new ArgumentException(innerMessage, new NullReferenceException(innerInnerMessage))); + + sut.Error("blubb", exception); + + var log = sut.GetLog(); + var logText = log[1] as ILogText; + + Assert.AreEqual(2, log.Count); + Assert.IsTrue(logText.Text.Contains(outerMessage)); + Assert.IsTrue(logText.Text.Contains(innerMessage)); + Assert.IsTrue(logText.Text.Contains(innerInnerMessage)); + } + [TestMethod] public void MustReturnCopyOfLog() { diff --git a/SafeExamBrowser.Core.UnitTests/Logging/ModuleLoggerTests.cs b/SafeExamBrowser.Core.UnitTests/Logging/ModuleLoggerTests.cs index af450592..bfdbaca2 100644 --- a/SafeExamBrowser.Core.UnitTests/Logging/ModuleLoggerTests.cs +++ b/SafeExamBrowser.Core.UnitTests/Logging/ModuleLoggerTests.cs @@ -26,6 +26,7 @@ namespace SafeExamBrowser.Core.UnitTests.Logging var logText = new LogText("Log text"); var sut = new ModuleLogger(loggerMock.Object, typeof(ModuleLoggerTests)); + sut.Debug("Debug"); sut.Info("Info"); sut.Warn("Warning"); sut.Error("Error"); @@ -36,6 +37,7 @@ namespace SafeExamBrowser.Core.UnitTests.Logging sut.Unsubscribe(logObserverMock.Object); sut.GetLog(); + loggerMock.Verify(l => l.Debug($"[{nameof(ModuleLoggerTests)}] Debug"), Times.Once); loggerMock.Verify(l => l.Info($"[{nameof(ModuleLoggerTests)}] Info"), Times.Once); loggerMock.Verify(l => l.Warn($"[{nameof(ModuleLoggerTests)}] Warning"), Times.Once); loggerMock.Verify(l => l.Error($"[{nameof(ModuleLoggerTests)}] Error"), Times.Once); diff --git a/SafeExamBrowser.Core.UnitTests/SafeExamBrowser.Core.UnitTests.csproj b/SafeExamBrowser.Core.UnitTests/SafeExamBrowser.Core.UnitTests.csproj index 2c716cfb..a6444549 100644 --- a/SafeExamBrowser.Core.UnitTests/SafeExamBrowser.Core.UnitTests.csproj +++ b/SafeExamBrowser.Core.UnitTests/SafeExamBrowser.Core.UnitTests.csproj @@ -78,11 +78,11 @@ - - - - - + + + + +