SEBWIN-183: Implemented unit tests for startup-/shutdown-operations.

This commit is contained in:
dbuechel 2017-10-11 11:46:39 +02:00
parent fdb32ff5f2
commit 91a8e4d6e4
11 changed files with 658 additions and 0 deletions

View file

@ -0,0 +1,72 @@
/*
* Copyright (c) 2017 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 Moq;
using SafeExamBrowser.Contracts.Behaviour;
using SafeExamBrowser.Contracts.Configuration;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.UserInterface;
using SafeExamBrowser.Contracts.UserInterface.Taskbar;
using SafeExamBrowser.Core.Behaviour.Operations;
namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations
{
[TestClass]
public class BrowserOperationTests
{
private Mock<IApplicationController> controllerMock;
private Mock<IApplicationInfo> appInfoMock;
private Mock<ILogger> loggerMock;
private Mock<ISplashScreen> splashScreenMock;
private Mock<ITaskbar> taskbarMock;
private Mock<IUserInterfaceFactory> uiFactoryMock;
private BrowserOperation sut;
[TestInitialize]
public void Initialize()
{
controllerMock = new Mock<IApplicationController>();
appInfoMock = new Mock<IApplicationInfo>();
loggerMock = new Mock<ILogger>();
splashScreenMock = new Mock<ISplashScreen>();
taskbarMock = new Mock<ITaskbar>();
uiFactoryMock = new Mock<IUserInterfaceFactory>();
sut = new BrowserOperation(controllerMock.Object, appInfoMock.Object, loggerMock.Object, taskbarMock.Object, uiFactoryMock.Object)
{
SplashScreen = splashScreenMock.Object
};
}
[TestMethod]
public void MustPeformCorrectly()
{
var order = 0;
controllerMock.Setup(c => c.Initialize()).Callback(() => Assert.AreEqual(++order, 1));
controllerMock.Setup(c => c.RegisterApplicationButton(It.IsAny<IApplicationButton>())).Callback(() => Assert.AreEqual(++order, 2));
taskbarMock.Setup(t => t.AddApplication(It.IsAny<IApplicationButton>())).Callback(() => Assert.AreEqual(++order, 3));
sut.Perform();
controllerMock.Verify(c => c.Initialize(), Times.Once);
controllerMock.Verify(c => c.RegisterApplicationButton(It.IsAny<IApplicationButton>()), Times.Once);
taskbarMock.Verify(t => t.AddApplication(It.IsAny<IApplicationButton>()), Times.Once);
}
[TestMethod]
public void MustRevertCorrectly()
{
sut.Revert();
controllerMock.Verify(c => c.Terminate(), Times.Once);
}
}
}

View file

@ -0,0 +1,56 @@
/*
* Copyright (c) 2017 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 Moq;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.UserInterface;
using SafeExamBrowser.Contracts.WindowsApi;
using SafeExamBrowser.Core.Behaviour.Operations;
namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations
{
[TestClass]
public class ClipboardOperationTests
{
private Mock<ILogger> loggerMock;
private Mock<INativeMethods> nativeMethodsMock;
private Mock<ISplashScreen> splashScreenMock;
private ClipboardOperation sut;
[TestInitialize]
public void Initialize()
{
loggerMock = new Mock<ILogger>();
nativeMethodsMock = new Mock<INativeMethods>();
splashScreenMock = new Mock<ISplashScreen>();
sut = new ClipboardOperation(loggerMock.Object, nativeMethodsMock.Object)
{
SplashScreen = splashScreenMock.Object
};
}
[TestMethod]
public void MustPerformCorrectly()
{
sut.Perform();
nativeMethodsMock.Verify(n => n.EmptyClipboard(), Times.Once);
}
[TestMethod]
public void MustRevertCorrectly()
{
sut.Revert();
nativeMethodsMock.Verify(n => n.EmptyClipboard(), Times.Once);
}
}
}

View file

@ -0,0 +1,73 @@
/*
* Copyright (c) 2017 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 Moq;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.Monitoring;
using SafeExamBrowser.Contracts.UserInterface;
using SafeExamBrowser.Contracts.UserInterface.Taskbar;
using SafeExamBrowser.Core.Behaviour.Operations;
namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations
{
[TestClass]
public class DisplayMonitorOperationTests
{
private Mock<IDisplayMonitor> displayMonitorMock;
private Mock<ILogger> loggerMock;
private Mock<ISplashScreen> splashScreenMock;
private Mock<ITaskbar> taskbarMock;
private DisplayMonitorOperation sut;
[TestInitialize]
public void Initialize()
{
loggerMock = new Mock<ILogger>();
displayMonitorMock = new Mock<IDisplayMonitor>();
splashScreenMock = new Mock<ISplashScreen>();
taskbarMock = new Mock<ITaskbar>();
sut = new DisplayMonitorOperation(displayMonitorMock.Object, loggerMock.Object, taskbarMock.Object)
{
SplashScreen = splashScreenMock.Object
};
}
[TestMethod]
public void MustPerformCorrectly()
{
var order = 0;
displayMonitorMock.Setup(d => d.PreventSleepMode()).Callback(() => Assert.AreEqual(++order, 1));
displayMonitorMock.Setup(d => d.InitializePrimaryDisplay(It.IsAny<int>())).Callback(() => Assert.AreEqual(++order, 2));
displayMonitorMock.Setup(d => d.StartMonitoringDisplayChanges()).Callback(() => Assert.AreEqual(++order, 3));
sut.Perform();
displayMonitorMock.Verify(d => d.PreventSleepMode(), Times.Once);
displayMonitorMock.Verify(d => d.InitializePrimaryDisplay(It.IsAny<int>()), Times.Once);
displayMonitorMock.Verify(d => d.StartMonitoringDisplayChanges(), Times.Once);
}
[TestMethod]
public void MustRevertCorrectly()
{
var order = 0;
displayMonitorMock.Setup(d => d.StopMonitoringDisplayChanges()).Callback(() => Assert.AreEqual(++order, 1));
displayMonitorMock.Setup(d => d.ResetPrimaryDisplay()).Callback(() => Assert.AreEqual(++order, 2));
sut.Revert();
displayMonitorMock.Verify(d => d.StopMonitoringDisplayChanges(), Times.Once);
displayMonitorMock.Verify(d => d.ResetPrimaryDisplay(), Times.Once);
}
}
}

View file

@ -0,0 +1,48 @@
/*
* Copyright (c) 2017 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 Moq;
using SafeExamBrowser.Contracts.I18n;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.UserInterface;
using SafeExamBrowser.Core.Behaviour.Operations;
namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations
{
[TestClass]
public class I18nOperationTests
{
private Mock<ILogger> loggerMock;
private Mock<ISplashScreen> splashScreenMock;
private Mock<IText> textMock;
private I18nOperation sut;
[TestInitialize]
public void Initialize()
{
loggerMock = new Mock<ILogger>();
splashScreenMock = new Mock<ISplashScreen>();
textMock = new Mock<IText>();
sut = new I18nOperation(loggerMock.Object, textMock.Object)
{
SplashScreen = splashScreenMock.Object
};
}
[TestMethod]
public void MustPerformCorrectly()
{
sut.Perform();
textMock.Verify(t => t.Initialize(It.IsAny<ITextResource>()), Times.Once);
}
}
}

View file

@ -0,0 +1,59 @@
/*
* Copyright (c) 2017 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 Moq;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.Monitoring;
using SafeExamBrowser.Contracts.UserInterface;
using SafeExamBrowser.Contracts.WindowsApi;
using SafeExamBrowser.Core.Behaviour.Operations;
namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations
{
[TestClass]
public class KeyboardInterceptorOperationTests
{
private Mock<IKeyboardInterceptor> keyboardInterceptorMock;
private Mock<ILogger> loggerMock;
private Mock<INativeMethods> nativeMethodsMock;
private Mock<ISplashScreen> splashScreenMock;
private KeyboardInterceptorOperation sut;
[TestInitialize]
public void Initialize()
{
keyboardInterceptorMock = new Mock<IKeyboardInterceptor>();
loggerMock = new Mock<ILogger>();
nativeMethodsMock = new Mock<INativeMethods>();
splashScreenMock = new Mock<ISplashScreen>();
sut = new KeyboardInterceptorOperation(keyboardInterceptorMock.Object, loggerMock.Object, nativeMethodsMock.Object)
{
SplashScreen = splashScreenMock.Object
};
}
[TestMethod]
public void MustPerformCorrectly()
{
sut.Perform();
nativeMethodsMock.Verify(n => n.RegisterKeyboardHook(It.IsAny<IKeyboardInterceptor>()), Times.Once);
}
[TestMethod]
public void MustRevertCorrectly()
{
sut.Revert();
nativeMethodsMock.Verify(n => n.DeregisterKeyboardHook(It.IsAny<IKeyboardInterceptor>()), Times.Once);
}
}
}

View file

@ -0,0 +1,59 @@
/*
* Copyright (c) 2017 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 Moq;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.Monitoring;
using SafeExamBrowser.Contracts.UserInterface;
using SafeExamBrowser.Contracts.WindowsApi;
using SafeExamBrowser.Core.Behaviour.Operations;
namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations
{
[TestClass]
public class MouseInterceptorOperationTests
{
private Mock<IMouseInterceptor> mouseInterceptorMock;
private Mock<ILogger> loggerMock;
private Mock<INativeMethods> nativeMethodsMock;
private Mock<ISplashScreen> splashScreenMock;
private MouseInterceptorOperation sut;
[TestInitialize]
public void Initialize()
{
mouseInterceptorMock = new Mock<IMouseInterceptor>();
loggerMock = new Mock<ILogger>();
nativeMethodsMock = new Mock<INativeMethods>();
splashScreenMock = new Mock<ISplashScreen>();
sut = new MouseInterceptorOperation(loggerMock.Object, mouseInterceptorMock.Object, nativeMethodsMock.Object)
{
SplashScreen = splashScreenMock.Object
};
}
[TestMethod]
public void MustPerformCorrectly()
{
sut.Perform();
nativeMethodsMock.Verify(n => n.RegisterMouseHook(It.IsAny<IMouseInterceptor>()), Times.Once);
}
[TestMethod]
public void MustRevertCorrectly()
{
sut.Revert();
nativeMethodsMock.Verify(n => n.DeregisterMouseHook(It.IsAny<IMouseInterceptor>()), Times.Once);
}
}
}

View file

@ -0,0 +1,68 @@
/*
* Copyright (c) 2017 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 Moq;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.Monitoring;
using SafeExamBrowser.Contracts.UserInterface;
using SafeExamBrowser.Core.Behaviour.Operations;
namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations
{
[TestClass]
public class ProcessMonitorOperationTests
{
private Mock<ILogger> loggerMock;
private Mock<IProcessMonitor> processMonitorMock;
private Mock<ISplashScreen> splashScreenMock;
private ProcessMonitorOperation sut;
[TestInitialize]
public void Initialize()
{
loggerMock = new Mock<ILogger>();
processMonitorMock = new Mock<IProcessMonitor>();
splashScreenMock = new Mock<ISplashScreen>();
sut = new ProcessMonitorOperation(loggerMock.Object, processMonitorMock.Object)
{
SplashScreen = splashScreenMock.Object
};
}
[TestMethod]
public void MustPerformCorrectly()
{
var order = 0;
processMonitorMock.Setup(p => p.CloseExplorerShell()).Callback(() => Assert.AreEqual(++order, 1));
processMonitorMock.Setup(p => p.StartMonitoringExplorer()).Callback(() => Assert.AreEqual(++order, 2));
sut.Perform();
processMonitorMock.Verify(p => p.CloseExplorerShell(), Times.Once);
processMonitorMock.Verify(p => p.StartMonitoringExplorer(), Times.Once);
}
[TestMethod]
public void MustRevertCorrectly()
{
var order = 0;
processMonitorMock.Setup(p => p.StopMonitoringExplorer()).Callback(() => Assert.AreEqual(++order, 1));
processMonitorMock.Setup(p => p.StartExplorerShell()).Callback(() => Assert.AreEqual(++order, 2));
sut.Revert();
processMonitorMock.Verify(p => p.StopMonitoringExplorer(), Times.Once);
processMonitorMock.Verify(p => p.StartExplorerShell(), Times.Once);
}
}
}

View file

@ -0,0 +1,56 @@
/*
* Copyright (c) 2017 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 Moq;
using SafeExamBrowser.Contracts.Behaviour;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.UserInterface;
using SafeExamBrowser.Core.Behaviour.Operations;
namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations
{
[TestClass]
public class RuntimeControllerOperationTests
{
private Mock<ILogger> loggerMock;
private Mock<IRuntimeController> runtimeControllerMock;
private Mock<ISplashScreen> splashScreenMock;
private RuntimeControllerOperation sut;
[TestInitialize]
public void Initialize()
{
loggerMock = new Mock<ILogger>();
runtimeControllerMock = new Mock<IRuntimeController>();
splashScreenMock = new Mock<ISplashScreen>();
sut = new RuntimeControllerOperation(runtimeControllerMock.Object, loggerMock.Object)
{
SplashScreen = splashScreenMock.Object
};
}
[TestMethod]
public void MustPerformCorrectly()
{
sut.Perform();
runtimeControllerMock.Verify(r => r.Start(), Times.Once);
}
[TestMethod]
public void MustRevertCorrectly()
{
sut.Revert();
runtimeControllerMock.Verify(r => r.Stop(), Times.Once);
}
}
}

View file

@ -0,0 +1,89 @@
/*
* Copyright (c) 2017 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 Moq;
using SafeExamBrowser.Contracts.Configuration;
using SafeExamBrowser.Contracts.Configuration.Settings;
using SafeExamBrowser.Contracts.I18n;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.SystemComponents;
using SafeExamBrowser.Contracts.UserInterface;
using SafeExamBrowser.Contracts.UserInterface.Taskbar;
using SafeExamBrowser.Core.Behaviour.Operations;
namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations
{
[TestClass]
public class TaskbarOperationTests
{
private Mock<ILogger> loggerMock;
private Mock<ISettings> settingsMock;
private Mock<ISplashScreen> splashScreenMock;
private Mock<ISystemComponent<ISystemKeyboardLayoutControl>> keyboardLayoutMock;
private Mock<ISystemComponent<ISystemPowerSupplyControl>> powerSupplyMock;
private Mock<ISystemInfo> systemInfoMock;
private Mock<ITaskbar> taskbarMock;
private Mock<IText> textMock;
private Mock<IUserInterfaceFactory> uiFactoryMock;
private TaskbarOperation sut;
[TestInitialize]
public void Initialize()
{
loggerMock = new Mock<ILogger>();
settingsMock = new Mock<ISettings>();
splashScreenMock = new Mock<ISplashScreen>();
keyboardLayoutMock = new Mock<ISystemComponent<ISystemKeyboardLayoutControl>>();
powerSupplyMock = new Mock<ISystemComponent<ISystemPowerSupplyControl>>();
systemInfoMock = new Mock<ISystemInfo>();
taskbarMock = new Mock<ITaskbar>();
textMock = new Mock<IText>();
uiFactoryMock = new Mock<IUserInterfaceFactory>();
settingsMock.SetupGet(s => s.AllowApplicationLog).Returns(true);
settingsMock.SetupGet(s => s.AllowKeyboardLayout).Returns(true);
systemInfoMock.SetupGet(s => s.HasBattery).Returns(true);
uiFactoryMock.Setup(u => u.CreateNotification(It.IsAny<INotificationInfo>())).Returns(new Mock<INotificationButton>().Object);
sut = new TaskbarOperation(
loggerMock.Object,
settingsMock.Object,
keyboardLayoutMock.Object,
powerSupplyMock.Object,
systemInfoMock.Object,
taskbarMock.Object,
textMock.Object,
uiFactoryMock.Object)
{
SplashScreen = splashScreenMock.Object
};
}
[TestMethod]
public void MustPerformCorrectly()
{
sut.Perform();
keyboardLayoutMock.Verify(k => k.Initialize(It.IsAny<ISystemKeyboardLayoutControl>()), Times.Once);
powerSupplyMock.Verify(p => p.Initialize(It.IsAny<ISystemPowerSupplyControl>()), Times.Once);
taskbarMock.Verify(t => t.AddSystemControl(It.IsAny<ISystemControl>()), Times.Exactly(2));
taskbarMock.Verify(t => t.AddNotification(It.IsAny<INotificationButton>()), Times.Once);
}
[TestMethod]
public void MustRevertCorrectly()
{
sut.Revert();
keyboardLayoutMock.Verify(k => k.Terminate(), Times.Once);
powerSupplyMock.Verify(p => p.Terminate(), Times.Once);
}
}
}

View file

@ -0,0 +1,68 @@
/*
* Copyright (c) 2017 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 Moq;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.Monitoring;
using SafeExamBrowser.Contracts.UserInterface;
using SafeExamBrowser.Core.Behaviour.Operations;
namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations
{
[TestClass]
public class WindowMonitorOperationTests
{
private Mock<ILogger> loggerMock;
private Mock<ISplashScreen> splashScreenMock;
private Mock<IWindowMonitor> windowMonitorMock;
private WindowMonitorOperation sut;
[TestInitialize]
public void Initialize()
{
loggerMock = new Mock<ILogger>();
splashScreenMock = new Mock<ISplashScreen>();
windowMonitorMock = new Mock<IWindowMonitor>();
sut = new WindowMonitorOperation(loggerMock.Object, windowMonitorMock.Object)
{
SplashScreen = splashScreenMock.Object
};
}
[TestMethod]
public void MustPerformCorrectly()
{
var order = 0;
windowMonitorMock.Setup(w => w.HideAllWindows()).Callback(() => Assert.AreEqual(++order, 1));
windowMonitorMock.Setup(w => w.StartMonitoringWindows()).Callback(() => Assert.AreEqual(++order, 2));
sut.Perform();
windowMonitorMock.Verify(w => w.HideAllWindows(), Times.Once);
windowMonitorMock.Verify(w => w.StartMonitoringWindows(), Times.Once);
}
[TestMethod]
public void MustRevertCorrectly()
{
var order = 0;
windowMonitorMock.Setup(w => w.StopMonitoringWindows()).Callback(() => Assert.AreEqual(++order, 1));
windowMonitorMock.Setup(w => w.RestoreHiddenWindows()).Callback(() => Assert.AreEqual(++order, 2));
sut.Revert();
windowMonitorMock.Verify(w => w.StopMonitoringWindows(), Times.Once);
windowMonitorMock.Verify(w => w.RestoreHiddenWindows(), Times.Once);
}
}
}

View file

@ -72,6 +72,16 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Behaviour\Operations\BrowserOperationTests.cs" />
<Compile Include="Behaviour\Operations\ClipboardOperationTests.cs" />
<Compile Include="Behaviour\Operations\DisplayMonitorOperationTests.cs" />
<Compile Include="Behaviour\Operations\I18nOperationTests.cs" />
<Compile Include="Behaviour\Operations\KeyboardInterceptorOperationTests.cs" />
<Compile Include="Behaviour\Operations\MouseInterceptorOperationTests.cs" />
<Compile Include="Behaviour\Operations\ProcessMonitorOperationTests.cs" />
<Compile Include="Behaviour\Operations\RuntimeControllerOperationTests.cs" />
<Compile Include="Behaviour\Operations\TaskbarOperationTests.cs" />
<Compile Include="Behaviour\Operations\WindowMonitorOperationTests.cs" />
<Compile Include="Behaviour\RuntimeControllerTests.cs" />
<Compile Include="Behaviour\StartupControllerTests.cs" />
<Compile Include="Behaviour\ShutdownControllerTests.cs" />