Extended unit tests.

This commit is contained in:
dbuechel 2019-04-04 16:06:05 +02:00
parent 9e246ba4a6
commit 88442891c1
10 changed files with 344 additions and 39 deletions

View file

@ -528,6 +528,28 @@ namespace SafeExamBrowser.Client.UnitTests
Assert.IsFalse(args.Cancel);
}
[TestMethod]
public void Shutdown_MustAbortAskingUserForQuitPassword()
{
var args = new System.ComponentModel.CancelEventArgs();
var dialog = new Mock<IPasswordDialog>();
var dialogResult = new Mock<IPasswordDialogResult>();
settings.QuitPasswordHash = "1234";
dialog.Setup(d => d.Show(It.IsAny<IWindow>())).Returns(dialogResult.Object);
dialogResult.SetupGet(r => r.Success).Returns(false);
runtimeProxy.Setup(r => r.RequestShutdown()).Returns(new CommunicationResult(true));
uiFactory.Setup(u => u.CreatePasswordDialog(It.IsAny<TextKey>(), It.IsAny<TextKey>())).Returns(dialog.Object);
sut.TryStart();
taskbar.Raise(t => t.QuitButtonClicked += null, args as object);
uiFactory.Verify(u => u.CreatePasswordDialog(It.IsAny<TextKey>(), It.IsAny<TextKey>()), Times.Once);
runtimeProxy.Verify(p => p.RequestShutdown(), Times.Never);
Assert.IsTrue(args.Cancel);
}
[TestMethod]
public void Shutdown_MustNotInitiateIfQuitPasswordIncorrect()
{
@ -640,6 +662,33 @@ namespace SafeExamBrowser.Client.UnitTests
taskbar.Verify(t => t.Show(), Times.Never);
}
[TestMethod]
public void TerminationActivator_MustCorrectlyInitiateShutdown()
{
var order = 0;
var pause = 0;
var resume = 0;
messageBox.Setup(m => m.Show(
It.IsAny<TextKey>(),
It.IsAny<TextKey>(),
It.IsAny<MessageBoxAction>(),
It.IsAny<MessageBoxIcon>(),
It.IsAny<IWindow>())).Returns(MessageBoxResult.Yes);
runtimeProxy.Setup(r => r.RequestShutdown()).Returns(new CommunicationResult(true));
terminationActivator.Setup(t => t.Pause()).Callback(() => pause = ++order);
terminationActivator.Setup(t => t.Resume()).Callback(() => resume = ++order);
sut.TryStart();
terminationActivator.Raise(t => t.Activated += null);
Assert.AreEqual(1, pause);
Assert.AreEqual(2, resume);
terminationActivator.Verify(t => t.Pause(), Times.Once);
terminationActivator.Verify(t => t.Resume(), Times.Once);
runtimeProxy.Verify(p => p.RequestShutdown(), Times.Once);
}
[TestMethod]
public void WindowMonitor_MustHandleAllowedWindowChangeCorrectly()
{

View file

@ -51,19 +51,28 @@ namespace SafeExamBrowser.Client.UnitTests.Communication
sut.StartupToken = token;
var response = sut.Connect(Guid.Empty);
Assert.IsNotNull(response);
Assert.IsFalse(response.ConnectionEstablished);
Assert.IsFalse(sut.IsConnected);
response = sut.Connect(token);
var response = sut.Connect(token);
Assert.IsNotNull(response);
Assert.IsTrue(response.ConnectionEstablished);
Assert.IsTrue(sut.IsConnected);
}
[TestMethod]
public void MustRejectConnectionIfTokenInvalid()
{
var token = Guid.NewGuid();
sut.StartupToken = token;
var response = sut.Connect(Guid.NewGuid());
Assert.IsNotNull(response);
Assert.IsFalse(response.ConnectionEstablished);
Assert.IsFalse(sut.IsConnected);
}
[TestMethod]
public void MustOnlyAllowOneConcurrentConnection()
{

View file

@ -73,5 +73,13 @@ namespace SafeExamBrowser.Client.UnitTests.Notifications
Assert.IsTrue(button.HasSubscribed);
}
[TestMethod]
public void MustNotFailToTerminateIfNotStarted()
{
var sut = new AboutNotificationController(appConfig.Object, uiFactory.Object);
sut.Terminate();
}
}
}

View file

@ -18,14 +18,14 @@ namespace SafeExamBrowser.Client.UnitTests.Notifications
[TestClass]
public class LogNotificationControllerTests
{
private Mock<ILogger> loggerMock;
private Mock<IUserInterfaceFactory> uiFactoryMock;
private Mock<ILogger> logger;
private Mock<IUserInterfaceFactory> uiFactory;
[TestInitialize]
public void Initialize()
{
loggerMock = new Mock<ILogger>();
uiFactoryMock = new Mock<IUserInterfaceFactory>();
logger = new Mock<ILogger>();
uiFactory = new Mock<IUserInterfaceFactory>();
}
[TestMethod]
@ -33,9 +33,9 @@ namespace SafeExamBrowser.Client.UnitTests.Notifications
{
var button = new NotificationButtonMock();
var window = new Mock<IWindow>();
var sut = new LogNotificationController(loggerMock.Object, uiFactoryMock.Object);
var sut = new LogNotificationController(logger.Object, uiFactory.Object);
uiFactoryMock.Setup(u => u.CreateLogWindow(It.IsAny<ILogger>())).Returns(window.Object);
uiFactory.Setup(u => u.CreateLogWindow(It.IsAny<ILogger>())).Returns(window.Object);
sut.RegisterNotification(button);
button.Click();
sut.Terminate();
@ -48,9 +48,9 @@ namespace SafeExamBrowser.Client.UnitTests.Notifications
{
var button = new NotificationButtonMock();
var window = new Mock<IWindow>();
var sut = new LogNotificationController(loggerMock.Object, uiFactoryMock.Object);
var sut = new LogNotificationController(logger.Object, uiFactory.Object);
uiFactoryMock.Setup(u => u.CreateLogWindow(It.IsAny<ILogger>())).Returns(window.Object);
uiFactory.Setup(u => u.CreateLogWindow(It.IsAny<ILogger>())).Returns(window.Object);
sut.RegisterNotification(button);
button.Click();
button.Click();
@ -58,7 +58,7 @@ namespace SafeExamBrowser.Client.UnitTests.Notifications
button.Click();
button.Click();
uiFactoryMock.Verify(u => u.CreateLogWindow(It.IsAny<ILogger>()), Times.Once);
uiFactory.Verify(u => u.CreateLogWindow(It.IsAny<ILogger>()), Times.Once);
window.Verify(u => u.Show(), Times.Once);
window.Verify(u => u.BringToForeground(), Times.Exactly(4));
}
@ -67,11 +67,19 @@ namespace SafeExamBrowser.Client.UnitTests.Notifications
public void MustSubscribeToClickEvent()
{
var button = new NotificationButtonMock();
var sut = new LogNotificationController(loggerMock.Object, uiFactoryMock.Object);
var sut = new LogNotificationController(logger.Object, uiFactory.Object);
sut.RegisterNotification(button);
Assert.IsTrue(button.HasSubscribed);
}
[TestMethod]
public void MustNotFailToTerminateIfNotStarted()
{
var sut = new LogNotificationController(logger.Object, uiFactory.Object);
sut.Terminate();
}
}
}

View file

@ -25,7 +25,7 @@ namespace SafeExamBrowser.Client.UnitTests.Operations
public class ShellOperationTests
{
private Mock<IActionCenter> actionCenter;
private Mock<IEnumerable<IActionCenterActivator>> activators;
private List<IActionCenterActivator> activators;
private ActionCenterSettings actionCenterSettings;
private Mock<ILogger> logger;
private TaskbarSettings taskbarSettings;
@ -48,7 +48,7 @@ namespace SafeExamBrowser.Client.UnitTests.Operations
public void Initialize()
{
actionCenter = new Mock<IActionCenter>();
activators = new Mock<IEnumerable<IActionCenterActivator>>();
activators = new List<IActionCenterActivator>();
actionCenterSettings = new ActionCenterSettings();
logger = new Mock<ILogger>();
aboutInfo = new Mock<INotificationInfo>();
@ -65,16 +65,11 @@ namespace SafeExamBrowser.Client.UnitTests.Operations
text = new Mock<IText>();
uiFactory = new Mock<IUserInterfaceFactory>();
taskbarSettings.ShowApplicationLog = true;
taskbarSettings.ShowKeyboardLayout = true;
taskbarSettings.ShowWirelessNetwork = true;
taskbarSettings.EnableTaskbar = true;
systemInfo.SetupGet(s => s.HasBattery).Returns(true);
uiFactory.Setup(u => u.CreateNotificationControl(It.IsAny<INotificationInfo>(), It.IsAny<Location>())).Returns(new Mock<INotificationControl>().Object);
sut = new ShellOperation(
actionCenter.Object,
activators.Object,
activators,
actionCenterSettings,
logger.Object,
aboutInfo.Object,
@ -93,23 +88,198 @@ namespace SafeExamBrowser.Client.UnitTests.Operations
}
[TestMethod]
public void MustPerformCorrectly()
public void Perform_MustInitializeActivators()
{
var activatorMocks = new List<Mock<IActionCenterActivator>>
{
new Mock<IActionCenterActivator>(),
new Mock<IActionCenterActivator>(),
new Mock<IActionCenterActivator>()
};
actionCenterSettings.EnableActionCenter = true;
foreach (var activator in activatorMocks)
{
activators.Add(activator.Object);
}
sut.Perform();
terminationActivator.Verify(t => t.Start(), Times.Once);
foreach (var activator in activatorMocks)
{
activator.Verify(a => a.Start(), Times.Once);
}
}
[TestMethod]
public void Perform_MustInitializeClock()
{
actionCenterSettings.EnableActionCenter = true;
actionCenterSettings.ShowClock = true;
taskbarSettings.EnableTaskbar = true;
taskbarSettings.ShowClock = true;
sut.Perform();
actionCenter.VerifySet(a => a.ShowClock = true, Times.Once);
taskbar.VerifySet(t => t.ShowClock = true, Times.Once);
}
[TestMethod]
public void Perform_MustNotInitializeClock()
{
actionCenterSettings.EnableActionCenter = true;
actionCenterSettings.ShowClock = false;
taskbarSettings.EnableTaskbar = true;
taskbarSettings.ShowClock = false;
sut.Perform();
actionCenter.VerifySet(a => a.ShowClock = false, Times.Once);
taskbar.VerifySet(t => t.ShowClock = false, Times.Once);
}
[TestMethod]
public void Perform_MustInitializeNotifications()
{
actionCenterSettings.EnableActionCenter = true;
actionCenterSettings.ShowApplicationLog = true;
taskbarSettings.EnableTaskbar = true;
taskbarSettings.ShowApplicationLog = true;
sut.Perform();
actionCenter.Verify(a => a.AddNotificationControl(It.IsAny<INotificationControl>()), Times.AtLeast(2));
taskbar.Verify(t => t.AddNotificationControl(It.IsAny<INotificationControl>()), Times.AtLeast(2));
}
[TestMethod]
public void Perform_MustNotInitializeNotifications()
{
var logControl = new Mock<INotificationControl>();
actionCenterSettings.EnableActionCenter = true;
actionCenterSettings.ShowApplicationLog = false;
taskbarSettings.EnableTaskbar = true;
taskbarSettings.ShowApplicationLog = false;
uiFactory.Setup(f => f.CreateNotificationControl(It.Is<INotificationInfo>(i => i == logInfo.Object), It.IsAny<Location>())).Returns(logControl.Object);
sut.Perform();
actionCenter.Verify(a => a.AddNotificationControl(It.Is<INotificationControl>(i => i == logControl.Object)), Times.Never);
taskbar.Verify(t => t.AddNotificationControl(It.Is<INotificationControl>(i => i == logControl.Object)), Times.Never);
}
[TestMethod]
public void Perform_MustInitializeSystemComponents()
{
actionCenterSettings.EnableActionCenter = true;
actionCenterSettings.ShowKeyboardLayout = true;
actionCenterSettings.ShowWirelessNetwork = true;
taskbarSettings.EnableTaskbar = true;
taskbarSettings.ShowKeyboardLayout = true;
taskbarSettings.ShowWirelessNetwork = true;
systemInfo.SetupGet(s => s.HasBattery).Returns(true);
uiFactory.Setup(f => f.CreateKeyboardLayoutControl(It.IsAny<Location>())).Returns(new Mock<ISystemKeyboardLayoutControl>().Object);
uiFactory.Setup(f => f.CreatePowerSupplyControl(It.IsAny<Location>())).Returns(new Mock<ISystemPowerSupplyControl>().Object);
uiFactory.Setup(f => f.CreateWirelessNetworkControl(It.IsAny<Location>())).Returns(new Mock<ISystemWirelessNetworkControl>().Object);
sut.Perform();
keyboardLayout.Verify(k => k.Initialize(), Times.Once);
powerSupply.Verify(p => p.Initialize(), Times.Once);
wirelessNetwork.Verify(w => w.Initialize(), Times.Once);
taskbar.Verify(t => t.AddSystemControl(It.IsAny<ISystemControl>()), Times.Exactly(3));
taskbar.Verify(t => t.AddNotificationControl(It.IsAny<INotificationControl>()), Times.Exactly(2));
actionCenter.Verify(a => a.AddSystemControl(It.IsAny<ISystemKeyboardLayoutControl>()), Times.Once);
actionCenter.Verify(a => a.AddSystemControl(It.IsAny<ISystemPowerSupplyControl>()), Times.Once);
actionCenter.Verify(a => a.AddSystemControl(It.IsAny<ISystemWirelessNetworkControl>()), Times.Once);
taskbar.Verify(t => t.AddSystemControl(It.IsAny<ISystemKeyboardLayoutControl>()), Times.Once);
taskbar.Verify(t => t.AddSystemControl(It.IsAny<ISystemPowerSupplyControl>()), Times.Once);
taskbar.Verify(t => t.AddSystemControl(It.IsAny<ISystemWirelessNetworkControl>()), Times.Once);
}
[TestMethod]
public void MustRevertCorrectly()
public void Perform_MustNotInitializeSystemComponents()
{
actionCenterSettings.EnableActionCenter = true;
actionCenterSettings.ShowKeyboardLayout = false;
actionCenterSettings.ShowWirelessNetwork = false;
taskbarSettings.EnableTaskbar = true;
taskbarSettings.ShowKeyboardLayout = false;
taskbarSettings.ShowWirelessNetwork = false;
systemInfo.SetupGet(s => s.HasBattery).Returns(false);
uiFactory.Setup(f => f.CreateKeyboardLayoutControl(It.IsAny<Location>())).Returns(new Mock<ISystemKeyboardLayoutControl>().Object);
uiFactory.Setup(f => f.CreatePowerSupplyControl(It.IsAny<Location>())).Returns(new Mock<ISystemPowerSupplyControl>().Object);
uiFactory.Setup(f => f.CreateWirelessNetworkControl(It.IsAny<Location>())).Returns(new Mock<ISystemWirelessNetworkControl>().Object);
sut.Perform();
keyboardLayout.Verify(k => k.Initialize(), Times.Once);
powerSupply.Verify(p => p.Initialize(), Times.Once);
wirelessNetwork.Verify(w => w.Initialize(), Times.Once);
actionCenter.Verify(a => a.AddSystemControl(It.IsAny<ISystemKeyboardLayoutControl>()), Times.Never);
actionCenter.Verify(a => a.AddSystemControl(It.IsAny<ISystemPowerSupplyControl>()), Times.Never);
actionCenter.Verify(a => a.AddSystemControl(It.IsAny<ISystemWirelessNetworkControl>()), Times.Never);
taskbar.Verify(t => t.AddSystemControl(It.IsAny<ISystemKeyboardLayoutControl>()), Times.Never);
taskbar.Verify(t => t.AddSystemControl(It.IsAny<ISystemPowerSupplyControl>()), Times.Never);
taskbar.Verify(t => t.AddSystemControl(It.IsAny<ISystemWirelessNetworkControl>()), Times.Never);
}
[TestMethod]
public void Perform_MustNotInitializeActionCenterIfNotEnabled()
{
actionCenterSettings.EnableActionCenter = false;
sut.Perform();
actionCenter.VerifyNoOtherCalls();
}
[TestMethod]
public void Perform_MustNotInitializeTaskbarIfNotEnabled()
{
taskbarSettings.EnableTaskbar = false;
sut.Perform();
taskbar.VerifyNoOtherCalls();
}
[TestMethod]
public void Revert_MustTerminateActivators()
{
var activatorMocks = new List<Mock<IActionCenterActivator>>
{
new Mock<IActionCenterActivator>(),
new Mock<IActionCenterActivator>(),
new Mock<IActionCenterActivator>()
};
actionCenterSettings.EnableActionCenter = true;
foreach (var activator in activatorMocks)
{
activators.Add(activator.Object);
}
sut.Revert();
terminationActivator.Verify(t => t.Stop(), Times.Once);
foreach (var activator in activatorMocks)
{
activator.Verify(a => a.Stop(), Times.Once);
}
}
[TestMethod]
public void Revert_MustTerminateControllers()
{
sut.Revert();
aboutController.Verify(c => c.Terminate(), Times.Once);
logController.Verify(c => c.Terminate(), Times.Once);
keyboardLayout.Verify(k => k.Terminate(), Times.Once);
powerSupply.Verify(p => p.Terminate(), Times.Once);
wirelessNetwork.Verify(w => w.Terminate(), Times.Once);

View file

@ -109,6 +109,15 @@ namespace SafeExamBrowser.Client.Operations
private void InitializeActivators()
{
terminationActivator.Start();
if (actionCenterSettings.EnableActionCenter)
{
foreach (var activator in activators)
{
actionCenter.Register(activator);
activator.Start();
}
}
}
private void InitializeActionCenter()
@ -124,12 +133,6 @@ namespace SafeExamBrowser.Client.Operations
InitializeKeyboardLayoutForActionCenter();
InitializeWirelessNetworkForActionCenter();
InitializePowerSupplyForActionCenter();
foreach (var activator in activators)
{
actionCenter.Register(activator);
activator.Start();
}
}
else
{

View file

@ -95,15 +95,16 @@ namespace SafeExamBrowser.Configuration.UnitTests
{
var sut = new SubStream(stream.Object, 100, 200);
stream.SetupGet(s => s.Position).Returns(-100);
sut.Position = -100;
Assert.AreEqual(-1, sut.ReadByte());
stream.SetupGet(s => s.Position).Returns(200);
sut.Position = 200;
Assert.AreEqual(-1, sut.ReadByte());
stream.SetupGet(s => s.Position).Returns(25);
sut.Position = 25;
sut.ReadByte();
stream.Verify(s => s.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()), Times.AtLeastOnce);
stream.Verify(s => s.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()), Times.Once);
}
[TestMethod]

View file

@ -178,6 +178,43 @@ namespace SafeExamBrowser.Logging.UnitTests
Assert.IsTrue(message.Equals((messages[1] as ILogMessage).Message));
}
[TestMethod]
public void MustRespectLogLevel()
{
var sut = new Logger();
sut.LogLevel = LogLevel.Error;
sut.Debug("debug");
sut.Info("info");
sut.Warn("warn");
Assert.AreEqual(0, sut.GetLog().Count);
sut = new Logger();
sut.LogLevel = LogLevel.Warning;
sut.Debug("debug");
sut.Info("info");
sut.Warn("warn");
Assert.AreEqual(1, sut.GetLog().Count);
sut = new Logger();
sut.LogLevel = LogLevel.Info;
sut.Debug("debug");
sut.Info("info");
sut.Warn("warn");
Assert.AreEqual(2, sut.GetLog().Count);
sut = new Logger();
sut.LogLevel = LogLevel.Debug;
sut.Debug("debug");
sut.Info("info");
sut.Warn("warn");
Assert.AreEqual(3, sut.GetLog().Count);
}
[TestMethod]
public void MustUnsubscribeObserver()
{

View file

@ -39,6 +39,9 @@ namespace SafeExamBrowser.Logging.UnitTests
var logText = new LogText("Log text");
var sut = new ModuleLogger(loggerMock.Object, nameof(ModuleLoggerTests));
loggerMock.SetupGet(l => l.LogLevel).Returns(LogLevel.Error);
sut.LogLevel = LogLevel.Debug;
sut.Debug("Debug");
sut.Info("Info");
sut.Warn("Warning");
@ -49,6 +52,7 @@ namespace SafeExamBrowser.Logging.UnitTests
sut.Unsubscribe(logObserverMock.Object);
sut.GetLog();
loggerMock.VerifySet(l => l.LogLevel = LogLevel.Debug, Times.Once);
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);
@ -58,6 +62,8 @@ namespace SafeExamBrowser.Logging.UnitTests
loggerMock.Verify(l => l.Subscribe(logObserverMock.Object), Times.Once);
loggerMock.Verify(l => l.Unsubscribe(logObserverMock.Object), Times.Once);
loggerMock.Verify(l => l.GetLog(), Times.Once);
Assert.AreEqual(LogLevel.Error, sut.LogLevel);
}
}
}

View file

@ -56,6 +56,20 @@ namespace SafeExamBrowser.Runtime.UnitTests.Communication
Assert.IsTrue(response.ConnectionEstablished);
}
[TestMethod]
public void MustRejectConnectionIfTokenInvalid()
{
var token = Guid.NewGuid();
sut.AllowConnection = true;
sut.StartupToken = token;
var response = sut.Connect(Guid.NewGuid());
Assert.IsNotNull(response);
Assert.IsFalse(response.ConnectionEstablished);
}
[TestMethod]
public void MustOnlyAllowOneConcurrentConnection()
{