Implemented unit tests for event controller.

This commit is contained in:
dbuechel 2017-07-28 11:05:42 +02:00
parent af7e1e6a6e
commit f6d4a281a6
2 changed files with 140 additions and 0 deletions

View file

@ -0,0 +1,139 @@
/*
* 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 System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SafeExamBrowser.Contracts.Behaviour;
using SafeExamBrowser.Contracts.Configuration;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.Monitoring;
using SafeExamBrowser.Contracts.UserInterface;
using SafeExamBrowser.Core.Behaviour;
namespace SafeExamBrowser.Core.UnitTests.Behaviour
{
[TestClass]
public class EventControllerTests
{
private Mock<ILogger> loggerMock;
private Mock<IProcessMonitor> processMonitorMock;
private Mock<ITaskbar> taskbarMock;
private Mock<IWindowMonitor> windowMonitorMock;
private Mock<IWorkingArea> workingAreaMock;
private IEventController sut;
[TestInitialize]
public void Initialize()
{
loggerMock = new Mock<ILogger>();
processMonitorMock = new Mock<IProcessMonitor>();
taskbarMock = new Mock<ITaskbar>();
windowMonitorMock= new Mock<IWindowMonitor>();
workingAreaMock = new Mock<IWorkingArea>();
sut = new EventController(
loggerMock.Object,
processMonitorMock.Object,
taskbarMock.Object,
windowMonitorMock.Object,
workingAreaMock.Object);
sut.Start();
}
[TestMethod]
public void MustHandleExplorerStartCorrectly()
{
var order = 0;
var processManager = 0;
var workingArea = 0;
var taskbar = 0;
processMonitorMock.Setup(p => p.CloseExplorerShell()).Callback(() => processManager = ++order);
workingAreaMock.Setup(w => w.InitializeFor(taskbarMock.Object)).Callback(() => workingArea = ++order);
taskbarMock.Setup(t => t.InitializeBounds()).Callback(() => taskbar = ++order);
processMonitorMock.Raise(p => p.ExplorerStarted += null);
processMonitorMock.Verify(p => p.CloseExplorerShell(), Times.Once);
workingAreaMock.Verify(w => w.InitializeFor(taskbarMock.Object), Times.Once);
taskbarMock.Verify(t => t.InitializeBounds(), Times.Once);
Assert.IsTrue(processManager == 1);
Assert.IsTrue(workingArea == 2);
Assert.IsTrue(taskbar == 3);
}
[TestMethod]
public void MustHandleAllowedWindowChangeCorrectly()
{
var window = new IntPtr(12345);
processMonitorMock.Setup(p => p.BelongsToAllowedProcess(window)).Returns(true);
windowMonitorMock.Raise(w => w.WindowChanged += null, window);
processMonitorMock.Verify(p => p.BelongsToAllowedProcess(window), Times.Once);
windowMonitorMock.Verify(w => w.Hide(window), Times.Never);
windowMonitorMock.Verify(w => w.Close(window), Times.Never);
}
[TestMethod]
public void MustHandleUnallowedWindowHideCorrectly()
{
var order = 0;
var belongs = 0;
var hide = 0;
var window = new IntPtr(12345);
processMonitorMock.Setup(p => p.BelongsToAllowedProcess(window)).Returns(false).Callback(() => belongs = ++order);
windowMonitorMock.Setup(w => w.Hide(window)).Returns(true).Callback(() => hide = ++order);
windowMonitorMock.Raise(w => w.WindowChanged += null, window);
processMonitorMock.Verify(p => p.BelongsToAllowedProcess(window), Times.Once);
windowMonitorMock.Verify(w => w.Hide(window), Times.Once);
windowMonitorMock.Verify(w => w.Close(window), Times.Never);
Assert.IsTrue(belongs == 1);
Assert.IsTrue(hide == 2);
}
[TestMethod]
public void MustHandleUnallowedWindowCloseCorrectly()
{
var order = 0;
var belongs = 0;
var hide = 0;
var close = 0;
var window = new IntPtr(12345);
processMonitorMock.Setup(p => p.BelongsToAllowedProcess(window)).Returns(false).Callback(() => belongs = ++order);
windowMonitorMock.Setup(w => w.Hide(window)).Returns(false).Callback(() => hide = ++order);
windowMonitorMock.Setup(w => w.Close(window)).Callback(() => close = ++order);
windowMonitorMock.Raise(w => w.WindowChanged += null, window);
processMonitorMock.Verify(p => p.BelongsToAllowedProcess(window), Times.Once);
windowMonitorMock.Verify(w => w.Hide(window), Times.Once);
windowMonitorMock.Verify(w => w.Close(window), Times.Once);
Assert.IsTrue(belongs == 1);
Assert.IsTrue(hide == 2);
Assert.IsTrue(close == 3);
}
[TestCleanup]
public void Cleanup()
{
sut.Stop();
}
}
}

View file

@ -72,6 +72,7 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="Behaviour\EventControllerTests.cs" />
<Compile Include="Behaviour\StartupControllerTests.cs" />
<Compile Include="Behaviour\ShutdownControllerTests.cs" />
<Compile Include="I18n\TextTests.cs" />