SEBWIN-221: Fixed unit tests for changes in session handling (introduction of SessionContext).

This commit is contained in:
dbuechel 2018-10-12 15:23:43 +02:00
parent 95ad176047
commit bae7ed8a25
19 changed files with 655 additions and 407 deletions

View file

@ -6,8 +6,9 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/ */
using System;
using System.Diagnostics; using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq; using Moq;
using SafeExamBrowser.Client.Operations; using SafeExamBrowser.Client.Operations;
@ -42,7 +43,11 @@ namespace SafeExamBrowser.Client.UnitTests.Operations
sut = new ClientHostDisconnectionOperation(clientHost.Object, logger.Object, timeout_ms); sut = new ClientHostDisconnectionOperation(clientHost.Object, logger.Object, timeout_ms);
clientHost.SetupGet(h => h.IsConnected).Returns(true).Callback(() => clientHost.Raise(h => h.RuntimeDisconnected += null)); clientHost.SetupGet(h => h.IsConnected).Returns(true).Callback(() => Task.Run(() =>
{
Thread.Sleep(10);
clientHost.Raise(h => h.RuntimeDisconnected += null);
}));
stopWatch.Start(); stopWatch.Start();
sut.Revert(); sut.Revert();

View file

@ -31,71 +31,45 @@ namespace SafeExamBrowser.Client.UnitTests.Operations
[TestMethod] [TestMethod]
public void MustPerformCorrectlyForCreateNewDesktop() public void MustPerformCorrectlyForCreateNewDesktop()
{ {
var order = 0;
var hideAll = 0;
var startMonitoring = 0;
var sut = new WindowMonitorOperation(KioskMode.CreateNewDesktop, loggerMock.Object, windowMonitorMock.Object); var sut = new WindowMonitorOperation(KioskMode.CreateNewDesktop, loggerMock.Object, windowMonitorMock.Object);
windowMonitorMock.Setup(w => w.HideAllWindows()).Callback(() => hideAll = ++order);
windowMonitorMock.Setup(w => w.StartMonitoringWindows()).Callback(() => startMonitoring = ++order);
sut.Perform(); sut.Perform();
windowMonitorMock.Verify(w => w.HideAllWindows(), Times.Never); windowMonitorMock.Verify(w => w.HideAllWindows(), Times.Never);
windowMonitorMock.Verify(w => w.StartMonitoringWindows(), Times.Once); windowMonitorMock.Verify(w => w.StartMonitoringWindows(), Times.Once);
Assert.AreEqual(0, hideAll);
Assert.AreEqual(1, startMonitoring);
} }
[TestMethod] [TestMethod]
public void MustRevertCorrectlyForCreateNewDesktop() public void MustRevertCorrectlyForCreateNewDesktop()
{ {
var order = 0;
var stop = 0;
var restore = 0;
var sut = new WindowMonitorOperation(KioskMode.CreateNewDesktop, loggerMock.Object, windowMonitorMock.Object); var sut = new WindowMonitorOperation(KioskMode.CreateNewDesktop, loggerMock.Object, windowMonitorMock.Object);
windowMonitorMock.Setup(w => w.StopMonitoringWindows()).Callback(() => stop = ++order);
windowMonitorMock.Setup(w => w.RestoreHiddenWindows()).Callback(() => restore = ++order);
sut.Revert(); sut.Revert();
windowMonitorMock.Verify(w => w.StopMonitoringWindows(), Times.Once); windowMonitorMock.Verify(w => w.StopMonitoringWindows(), Times.Once);
windowMonitorMock.Verify(w => w.RestoreHiddenWindows(), Times.Never); windowMonitorMock.Verify(w => w.RestoreHiddenWindows(), Times.Never);
Assert.AreEqual(0, restore);
Assert.AreEqual(1, stop);
} }
[TestMethod] [TestMethod]
public void MustPerformCorrectlyForDisableExplorerShell() public void MustPerformCorrectlyForDisableExplorerShell()
{ {
var order = 0;
var sut = new WindowMonitorOperation(KioskMode.DisableExplorerShell, loggerMock.Object, windowMonitorMock.Object); var sut = new WindowMonitorOperation(KioskMode.DisableExplorerShell, loggerMock.Object, windowMonitorMock.Object);
windowMonitorMock.Setup(w => w.HideAllWindows()).Callback(() => Assert.AreEqual(++order, 1));
windowMonitorMock.Setup(w => w.StartMonitoringWindows()).Callback(() => Assert.AreEqual(++order, 2));
sut.Perform(); sut.Perform();
windowMonitorMock.Verify(w => w.HideAllWindows(), Times.Once); windowMonitorMock.Verify(w => w.HideAllWindows(), Times.Never);
windowMonitorMock.Verify(w => w.StartMonitoringWindows(), Times.Once); windowMonitorMock.Verify(w => w.StartMonitoringWindows(), Times.Once);
} }
[TestMethod] [TestMethod]
public void MustRevertCorrectlyForDisableExplorerShell() public void MustRevertCorrectlyForDisableExplorerShell()
{ {
var order = 0;
var sut = new WindowMonitorOperation(KioskMode.DisableExplorerShell, loggerMock.Object, windowMonitorMock.Object); var sut = new WindowMonitorOperation(KioskMode.DisableExplorerShell, loggerMock.Object, windowMonitorMock.Object);
windowMonitorMock.Setup(w => w.StopMonitoringWindows()).Callback(() => Assert.AreEqual(++order, 1));
windowMonitorMock.Setup(w => w.RestoreHiddenWindows()).Callback(() => Assert.AreEqual(++order, 2));
sut.Revert(); sut.Revert();
windowMonitorMock.Verify(w => w.StopMonitoringWindows(), Times.Once); windowMonitorMock.Verify(w => w.StopMonitoringWindows(), Times.Once);
windowMonitorMock.Verify(w => w.RestoreHiddenWindows(), Times.Once); windowMonitorMock.Verify(w => w.RestoreHiddenWindows(), Times.Never);
} }
[TestMethod] [TestMethod]
@ -103,6 +77,9 @@ namespace SafeExamBrowser.Client.UnitTests.Operations
{ {
var sut = new WindowMonitorOperation(KioskMode.None, loggerMock.Object, windowMonitorMock.Object); var sut = new WindowMonitorOperation(KioskMode.None, loggerMock.Object, windowMonitorMock.Object);
sut.Perform();
sut.Revert();
windowMonitorMock.VerifyNoOtherCalls(); windowMonitorMock.VerifyNoOtherCalls();
} }
} }

View file

@ -36,11 +36,6 @@ namespace SafeExamBrowser.Client.Operations
logger.Info("Initializing window monitoring..."); logger.Info("Initializing window monitoring...");
StatusChanged?.Invoke(TextKey.OperationStatus_InitializeWindowMonitoring); StatusChanged?.Invoke(TextKey.OperationStatus_InitializeWindowMonitoring);
if (kioskMode == KioskMode.DisableExplorerShell)
{
windowMonitor.HideAllWindows();
}
if (kioskMode != KioskMode.None) if (kioskMode != KioskMode.None)
{ {
windowMonitor.StartMonitoringWindows(); windowMonitor.StartMonitoringWindows();
@ -59,11 +54,6 @@ namespace SafeExamBrowser.Client.Operations
windowMonitor.StopMonitoringWindows(); windowMonitor.StopMonitoringWindows();
} }
if (kioskMode == KioskMode.DisableExplorerShell)
{
windowMonitor.RestoreHiddenWindows();
}
return OperationResult.Success; return OperationResult.Success;
} }
} }

View file

@ -9,9 +9,7 @@
using System; using System;
using System.Reflection; using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq; using SafeExamBrowser.Contracts.Configuration;
using SafeExamBrowser.Contracts.Communication.Proxies;
using SafeExamBrowser.Contracts.WindowsApi;
namespace SafeExamBrowser.Configuration.UnitTests namespace SafeExamBrowser.Configuration.UnitTests
{ {
@ -28,130 +26,50 @@ namespace SafeExamBrowser.Configuration.UnitTests
sut = new ConfigurationRepository(executablePath, string.Empty, string.Empty, string.Empty); sut = new ConfigurationRepository(executablePath, string.Empty, string.Empty, string.Empty);
} }
[TestMethod]
public void AppConfigMustNeverBeNull()
{
Assert.IsNotNull(sut.AppConfig);
}
[TestMethod]
public void CurrentSessionIsInitiallyNull()
{
Assert.IsNull(sut.CurrentSession);
}
[TestMethod]
public void CurrentSettingsAreInitiallyNull()
{
Assert.IsNull(sut.CurrentSettings);
}
[TestMethod]
public void MustCorrectlyBuildClientConfiguration()
{
sut.LoadDefaultSettings();
sut.InitializeSessionConfiguration();
var appConfig = sut.AppConfig;
var clientConfig = sut.BuildClientConfiguration();
var session = sut.CurrentSession;
var settings = sut.CurrentSettings;
Assert.AreEqual(session.Id, clientConfig.SessionId);
Assert.AreSame(appConfig, clientConfig.AppConfig);
Assert.AreSame(settings, clientConfig.Settings);
}
[TestMethod] [TestMethod]
public void MustCorrectlyInitializeSessionConfiguration() public void MustCorrectlyInitializeSessionConfiguration()
{ {
sut.InitializeSessionConfiguration(); var appConfig = sut.InitializeAppConfig();
var configuration = sut.InitializeSessionConfiguration();
Assert.IsNull(sut.CurrentSession.ClientProcess); Assert.IsInstanceOfType(configuration.AppConfig, typeof(AppConfig));
Assert.IsNull(sut.CurrentSession.ClientProxy); Assert.IsInstanceOfType(configuration.Id, typeof(Guid));
Assert.IsInstanceOfType(sut.CurrentSession.Id, typeof(Guid)); Assert.IsNull(configuration.Settings);
Assert.IsInstanceOfType(sut.CurrentSession.StartupToken, typeof(Guid)); Assert.IsInstanceOfType(configuration.StartupToken, typeof(Guid));
} }
[TestMethod] [TestMethod]
public void MustCorrectlyUpdateAppConfig() public void MustCorrectlyUpdateAppConfig()
{ {
var clientAddress = sut.AppConfig.ClientAddress; var appConfig = sut.InitializeAppConfig();
var clientId = sut.AppConfig.ClientId; var clientAddress = appConfig.ClientAddress;
var clientLogFile = sut.AppConfig.ClientLogFile; var clientId = appConfig.ClientId;
var runtimeAddress = sut.AppConfig.RuntimeAddress; var clientLogFile = appConfig.ClientLogFile;
var runtimeId = sut.AppConfig.RuntimeId; var runtimeAddress = appConfig.RuntimeAddress;
var runtimeLogFile = sut.AppConfig.RuntimeLogFile; var runtimeId = appConfig.RuntimeId;
var runtimeLogFile = appConfig.RuntimeLogFile;
var configuration = sut.InitializeSessionConfiguration();
sut.InitializeSessionConfiguration(); Assert.AreNotEqual(configuration.AppConfig.ClientAddress, clientAddress);
Assert.AreNotEqual(configuration.AppConfig.ClientId, clientId);
Assert.AreNotEqual(sut.AppConfig.ClientAddress, clientAddress); Assert.AreEqual(configuration.AppConfig.ClientLogFile, clientLogFile);
Assert.AreNotEqual(sut.AppConfig.ClientId, clientId); Assert.AreEqual(configuration.AppConfig.RuntimeAddress, runtimeAddress);
Assert.AreEqual(sut.AppConfig.ClientLogFile, clientLogFile); Assert.AreEqual(configuration.AppConfig.RuntimeId, runtimeId);
Assert.AreEqual(sut.AppConfig.RuntimeAddress, runtimeAddress); Assert.AreEqual(configuration.AppConfig.RuntimeLogFile, runtimeLogFile);
Assert.AreEqual(sut.AppConfig.RuntimeId, runtimeId);
Assert.AreEqual(sut.AppConfig.RuntimeLogFile, runtimeLogFile);
} }
[TestMethod] [TestMethod]
public void MustCorrectlyUpdateSessionConfiguration() public void MustCorrectlyUpdateSessionConfiguration()
{ {
var process = new Mock<IProcess>(); var appConfig = sut.InitializeAppConfig();
var proxy = new Mock<IClientProxy>(); var firstSession = sut.InitializeSessionConfiguration();
var secondSession = sut.InitializeSessionConfiguration();
var thirdSession = sut.InitializeSessionConfiguration();
sut.InitializeSessionConfiguration();
var firstSession = sut.CurrentSession;
sut.CurrentSession.ClientProcess = process.Object;
sut.CurrentSession.ClientProxy = proxy.Object;
sut.InitializeSessionConfiguration();
var secondSession = sut.CurrentSession;
Assert.AreSame(firstSession.ClientProcess, secondSession.ClientProcess);
Assert.AreSame(firstSession.ClientProxy, secondSession.ClientProxy);
Assert.AreNotEqual(firstSession.Id, secondSession.Id); Assert.AreNotEqual(firstSession.Id, secondSession.Id);
Assert.AreNotEqual(firstSession.StartupToken, secondSession.StartupToken); Assert.AreNotEqual(firstSession.StartupToken, secondSession.StartupToken);
sut.CurrentSession.ClientProcess = null;
sut.CurrentSession.ClientProxy = null;
sut.InitializeSessionConfiguration();
var thirdSession = sut.CurrentSession;
Assert.IsNull(thirdSession.ClientProcess);
Assert.IsNull(thirdSession.ClientProxy);
Assert.AreNotEqual(secondSession.Id, thirdSession.Id); Assert.AreNotEqual(secondSession.Id, thirdSession.Id);
Assert.AreNotEqual(secondSession.StartupToken, thirdSession.StartupToken); Assert.AreNotEqual(secondSession.StartupToken, thirdSession.StartupToken);
} }
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void MustNotAllowNullForExecutablePath()
{
new ConfigurationRepository(null, null, null, null);
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void MustNotAllowNullForProgramCopyright()
{
new ConfigurationRepository(string.Empty, null, null, null);
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void MustNotAllowNullForProgramTitle()
{
new ConfigurationRepository(string.Empty, string.Empty, null, null);
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void MustNotAllowNullForProgramVersion()
{
new ConfigurationRepository(string.Empty, string.Empty, string.Empty, null);
}
} }
} }

View file

@ -350,9 +350,9 @@ namespace SafeExamBrowser.Core.UnitTests.OperationModel
operationB.Setup(o => o.Perform()).Returns(OperationResult.Success); operationB.Setup(o => o.Perform()).Returns(OperationResult.Success);
operationC.Setup(o => o.Perform()).Returns(OperationResult.Success); operationC.Setup(o => o.Perform()).Returns(OperationResult.Success);
operationA.Setup(o => o.Revert()).Callback(() => a = ++current); operationA.Setup(o => o.Revert()).Returns(OperationResult.Success).Callback(() => a = ++current);
operationB.Setup(o => o.Revert()).Callback(() => b = ++current); operationB.Setup(o => o.Revert()).Returns(OperationResult.Success).Callback(() => b = ++current);
operationC.Setup(o => o.Revert()).Callback(() => c = ++current); operationC.Setup(o => o.Revert()).Returns(OperationResult.Success).Callback(() => c = ++current);
operations.Enqueue(operationA.Object); operations.Enqueue(operationA.Object);
operations.Enqueue(operationB.Object); operations.Enqueue(operationB.Object);

View file

@ -12,7 +12,6 @@ using Moq;
using SafeExamBrowser.Contracts.Communication; using SafeExamBrowser.Contracts.Communication;
using SafeExamBrowser.Contracts.Communication.Data; using SafeExamBrowser.Contracts.Communication.Data;
using SafeExamBrowser.Contracts.Communication.Hosts; using SafeExamBrowser.Contracts.Communication.Hosts;
using SafeExamBrowser.Contracts.Configuration;
using SafeExamBrowser.Contracts.Logging; using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Runtime.Communication; using SafeExamBrowser.Runtime.Communication;
@ -21,7 +20,6 @@ namespace SafeExamBrowser.Runtime.UnitTests.Communication
[TestClass] [TestClass]
public class RuntimeHostTests public class RuntimeHostTests
{ {
private Mock<IConfigurationRepository> configuration;
private Mock<IHostObject> hostObject; private Mock<IHostObject> hostObject;
private Mock<IHostObjectFactory> hostObjectFactory; private Mock<IHostObjectFactory> hostObjectFactory;
private Mock<ILogger> logger; private Mock<ILogger> logger;
@ -30,14 +28,13 @@ namespace SafeExamBrowser.Runtime.UnitTests.Communication
[TestInitialize] [TestInitialize]
public void Initialize() public void Initialize()
{ {
configuration = new Mock<IConfigurationRepository>();
hostObject = new Mock<IHostObject>(); hostObject = new Mock<IHostObject>();
hostObjectFactory = new Mock<IHostObjectFactory>(); hostObjectFactory = new Mock<IHostObjectFactory>();
logger = new Mock<ILogger>(); logger = new Mock<ILogger>();
hostObjectFactory.Setup(f => f.CreateObject(It.IsAny<string>(), It.IsAny<ICommunication>())).Returns(hostObject.Object); hostObjectFactory.Setup(f => f.CreateObject(It.IsAny<string>(), It.IsAny<ICommunication>())).Returns(hostObject.Object);
sut = new RuntimeHost("net:pipe://some/address", configuration.Object, hostObjectFactory.Object, logger.Object, 0); sut = new RuntimeHost("net:pipe://some/address", hostObjectFactory.Object, logger.Object, 0);
} }
[TestMethod] [TestMethod]

View file

@ -26,21 +26,21 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
private Action clientReady; private Action clientReady;
private Action terminated; private Action terminated;
private AppConfig appConfig; private AppConfig appConfig;
private Mock<IConfigurationRepository> configuration;
private Mock<IClientProxy> proxy; private Mock<IClientProxy> proxy;
private Mock<ILogger> logger; private Mock<ILogger> logger;
private Mock<IProcess> process; private Mock<IProcess> process;
private Mock<IProcessFactory> processFactory; private Mock<IProcessFactory> processFactory;
private Mock<IProxyFactory> proxyFactory; private Mock<IProxyFactory> proxyFactory;
private Mock<IRuntimeHost> runtimeHost; private Mock<IRuntimeHost> runtimeHost;
private Mock<ISessionData> session; private Mock<ISessionConfiguration> session;
private SessionContext sessionContext;
private ClientOperation sut; private ClientOperation sut;
[TestInitialize] [TestInitialize]
public void Initialize() public void Initialize()
{ {
appConfig = new AppConfig(); appConfig = new AppConfig();
configuration = new Mock<IConfigurationRepository>();
clientReady = new Action(() => runtimeHost.Raise(h => h.ClientReady += null)); clientReady = new Action(() => runtimeHost.Raise(h => h.ClientReady += null));
logger = new Mock<ILogger>(); logger = new Mock<ILogger>();
process = new Mock<IProcess>(); process = new Mock<IProcess>();
@ -48,20 +48,20 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
proxy = new Mock<IClientProxy>(); proxy = new Mock<IClientProxy>();
proxyFactory = new Mock<IProxyFactory>(); proxyFactory = new Mock<IProxyFactory>();
runtimeHost = new Mock<IRuntimeHost>(); runtimeHost = new Mock<IRuntimeHost>();
session = new Mock<ISessionData>(); session = new Mock<ISessionConfiguration>();
sessionContext = new SessionContext();
terminated = new Action(() => terminated = new Action(() =>
{ {
runtimeHost.Raise(h => h.ClientDisconnected += null); runtimeHost.Raise(h => h.ClientDisconnected += null);
process.Raise(p => p.Terminated += null, 0); process.Raise(p => p.Terminated += null, 0);
}); });
configuration.SetupGet(c => c.CurrentSession).Returns(session.Object); session.SetupGet(s => s.AppConfig).Returns(appConfig);
configuration.SetupGet(c => c.AppConfig).Returns(appConfig); sessionContext.Current = session.Object;
sessionContext.Next = session.Object;
proxyFactory.Setup(f => f.CreateClientProxy(It.IsAny<string>())).Returns(proxy.Object); proxyFactory.Setup(f => f.CreateClientProxy(It.IsAny<string>())).Returns(proxy.Object);
session.SetupGet(s => s.ClientProcess).Returns(process.Object);
session.SetupGet(s => s.ClientProxy).Returns(proxy.Object);
sut = new ClientOperation(configuration.Object, logger.Object, processFactory.Object, proxyFactory.Object, runtimeHost.Object, 0); sut = new ClientOperation(logger.Object, processFactory.Object, proxyFactory.Object, runtimeHost.Object, sessionContext, 0);
} }
[TestMethod] [TestMethod]
@ -78,9 +78,8 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
result = sut.Perform(); result = sut.Perform();
session.VerifySet(s => s.ClientProcess = process.Object, Times.Once); Assert.AreEqual(process.Object, sessionContext.ClientProcess);
session.VerifySet(s => s.ClientProxy = proxy.Object, Times.Once); Assert.AreEqual(proxy.Object, sessionContext.ClientProxy);
Assert.AreEqual(OperationResult.Success, result); Assert.AreEqual(OperationResult.Success, result);
} }
@ -91,9 +90,8 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
result = sut.Perform(); result = sut.Perform();
session.VerifySet(s => s.ClientProcess = process.Object, Times.Never); Assert.IsNull(sessionContext.ClientProcess);
session.VerifySet(s => s.ClientProxy = proxy.Object, Times.Never); Assert.IsNull(sessionContext.ClientProxy);
Assert.AreEqual(OperationResult.Failed, result); Assert.AreEqual(OperationResult.Failed, result);
} }
@ -107,9 +105,8 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
result = sut.Perform(); result = sut.Perform();
session.VerifySet(s => s.ClientProcess = process.Object, Times.Once); Assert.IsNotNull(sessionContext.ClientProcess);
session.VerifySet(s => s.ClientProxy = proxy.Object, Times.Once); Assert.IsNotNull(sessionContext.ClientProxy);
Assert.AreEqual(OperationResult.Failed, result); Assert.AreEqual(OperationResult.Failed, result);
} }
@ -127,9 +124,8 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
result = sut.Perform(); result = sut.Perform();
session.VerifySet(s => s.ClientProcess = process.Object, Times.Once); Assert.IsNotNull(sessionContext.ClientProcess);
session.VerifySet(s => s.ClientProxy = proxy.Object, Times.Once); Assert.IsNotNull(sessionContext.ClientProxy);
Assert.AreEqual(OperationResult.Failed, result); Assert.AreEqual(OperationResult.Failed, result);
} }
@ -147,70 +143,8 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
result = sut.Repeat(); result = sut.Repeat();
session.VerifySet(s => s.ClientProcess = process.Object, Times.Once); Assert.AreEqual(process.Object, sessionContext.ClientProcess);
session.VerifySet(s => s.ClientProxy = proxy.Object, Times.Once); Assert.AreEqual(proxy.Object, sessionContext.ClientProxy);
Assert.AreEqual(OperationResult.Success, result);
}
[TestMethod]
public void MustTerminateClientOnRepeat()
{
var terminated = new Action(() =>
{
runtimeHost.Raise(h => h.ClientDisconnected += null);
process.Raise(p => p.Terminated += null, 0);
});
proxy.Setup(p => p.Disconnect()).Callback(terminated);
session.SetupGet(s => s.ClientProcess).Returns(process.Object);
var result = sut.Repeat();
proxy.Verify(p => p.InitiateShutdown(), Times.Once);
proxy.Verify(p => p.Disconnect(), Times.Once);
process.Verify(p => p.Kill(), Times.Never);
session.VerifySet(s => s.ClientProcess = null, Times.Once);
session.VerifySet(s => s.ClientProxy = null, Times.Once);
Assert.AreEqual(OperationResult.Success, result);
}
[TestMethod]
public void MustDoNothingIfNoClientCreated()
{
session.SetupGet(s => s.ClientProcess).Returns(null as IProcess);
var result = sut.Repeat();
session.VerifyGet(s => s.ClientProcess, Times.Once);
process.VerifyNoOtherCalls();
processFactory.VerifyNoOtherCalls();
proxy.VerifyNoOtherCalls();
proxyFactory.VerifyNoOtherCalls();
runtimeHost.VerifyNoOtherCalls();
Assert.AreEqual(OperationResult.Success, result);
}
[TestMethod]
public void MustDoNothingIfNoClientRunning()
{
process.SetupGet(p => p.HasTerminated).Returns(true);
session.SetupGet(s => s.ClientProcess).Returns(process.Object);
var result = sut.Repeat();
process.VerifyGet(p => p.HasTerminated, Times.Once);
session.VerifyGet(s => s.ClientProcess, Times.Exactly(2));
process.VerifyNoOtherCalls();
processFactory.VerifyNoOtherCalls();
proxy.VerifyNoOtherCalls();
proxyFactory.VerifyNoOtherCalls();
runtimeHost.VerifyNoOtherCalls();
Assert.AreEqual(OperationResult.Success, result); Assert.AreEqual(OperationResult.Success, result);
} }
@ -225,8 +159,9 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
proxy.Verify(p => p.InitiateShutdown(), Times.Once); proxy.Verify(p => p.InitiateShutdown(), Times.Once);
proxy.Verify(p => p.Disconnect(), Times.Once); proxy.Verify(p => p.Disconnect(), Times.Once);
process.Verify(p => p.Kill(), Times.Never); process.Verify(p => p.Kill(), Times.Never);
session.VerifySet(s => s.ClientProcess = null, Times.Once);
session.VerifySet(s => s.ClientProxy = null, Times.Once); Assert.IsNull(sessionContext.ClientProcess);
Assert.IsNull(sessionContext.ClientProxy);
} }
[TestMethod] [TestMethod]
@ -238,8 +173,9 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
sut.Revert(); sut.Revert();
process.Verify(p => p.Kill(), Times.AtLeastOnce); process.Verify(p => p.Kill(), Times.AtLeastOnce);
session.VerifySet(s => s.ClientProcess = null, Times.Once);
session.VerifySet(s => s.ClientProxy = null, Times.Once); Assert.IsNull(sessionContext.ClientProcess);
Assert.IsNull(sessionContext.ClientProxy);
} }
[TestMethod] [TestMethod]
@ -249,12 +185,13 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
sut.Revert(); sut.Revert();
process.Verify(p => p.Kill(), Times.Exactly(5)); process.Verify(p => p.Kill(), Times.Exactly(5));
session.VerifySet(s => s.ClientProcess = null, Times.Never);
session.VerifySet(s => s.ClientProxy = null, Times.Never); Assert.IsNotNull(sessionContext.ClientProcess);
Assert.IsNotNull(sessionContext.ClientProxy);
} }
[TestMethod] [TestMethod]
public void MustNotStopClientIfAlreadyTerminated() public void MustNotStopClientOnRevertIfAlreadyTerminated()
{ {
process.SetupGet(p => p.HasTerminated).Returns(true); process.SetupGet(p => p.HasTerminated).Returns(true);
@ -263,8 +200,9 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
proxy.Verify(p => p.InitiateShutdown(), Times.Never); proxy.Verify(p => p.InitiateShutdown(), Times.Never);
proxy.Verify(p => p.Disconnect(), Times.Never); proxy.Verify(p => p.Disconnect(), Times.Never);
process.Verify(p => p.Kill(), Times.Never); process.Verify(p => p.Kill(), Times.Never);
session.VerifySet(s => s.ClientProcess = null, Times.Never);
session.VerifySet(s => s.ClientProxy = null, Times.Never); Assert.IsNull(sessionContext.ClientProcess);
Assert.IsNull(sessionContext.ClientProxy);
} }
private void PerformNormally() private void PerformNormally()

View file

@ -0,0 +1,109 @@
/*
* 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.Communication.Hosts;
using SafeExamBrowser.Contracts.Communication.Proxies;
using SafeExamBrowser.Contracts.Configuration;
using SafeExamBrowser.Contracts.Core.OperationModel;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.WindowsApi;
using SafeExamBrowser.Runtime.Operations;
namespace SafeExamBrowser.Runtime.UnitTests.Operations
{
[TestClass]
public class ClientTerminationOperationTests
{
private Action clientReady;
private Action terminated;
private AppConfig appConfig;
private Mock<IClientProxy> proxy;
private Mock<ILogger> logger;
private Mock<IProcess> process;
private Mock<IProcessFactory> processFactory;
private Mock<IProxyFactory> proxyFactory;
private Mock<IRuntimeHost> runtimeHost;
private Mock<ISessionConfiguration> session;
private SessionContext sessionContext;
private ClientTerminationOperation sut;
[TestInitialize]
public void Initialize()
{
appConfig = new AppConfig();
clientReady = new Action(() => runtimeHost.Raise(h => h.ClientReady += null));
logger = new Mock<ILogger>();
process = new Mock<IProcess>();
processFactory = new Mock<IProcessFactory>();
proxy = new Mock<IClientProxy>();
proxyFactory = new Mock<IProxyFactory>();
runtimeHost = new Mock<IRuntimeHost>();
session = new Mock<ISessionConfiguration>();
sessionContext = new SessionContext();
terminated = new Action(() =>
{
runtimeHost.Raise(h => h.ClientDisconnected += null);
process.Raise(p => p.Terminated += null, 0);
});
session.SetupGet(s => s.AppConfig).Returns(appConfig);
sessionContext.ClientProcess = process.Object;
sessionContext.ClientProxy = proxy.Object;
sessionContext.Current = session.Object;
sessionContext.Next = session.Object;
proxyFactory.Setup(f => f.CreateClientProxy(It.IsAny<string>())).Returns(proxy.Object);
sut = new ClientTerminationOperation(logger.Object, processFactory.Object, proxyFactory.Object, runtimeHost.Object, sessionContext, 0);
}
[TestMethod]
public void MustTerminateClientOnRepeat()
{
var terminated = new Action(() =>
{
runtimeHost.Raise(h => h.ClientDisconnected += null);
process.Raise(p => p.Terminated += null, 0);
});
proxy.Setup(p => p.Disconnect()).Callback(terminated);
var result = sut.Repeat();
proxy.Verify(p => p.InitiateShutdown(), Times.Once);
proxy.Verify(p => p.Disconnect(), Times.Once);
process.Verify(p => p.Kill(), Times.Never);
Assert.IsNull(sessionContext.ClientProcess);
Assert.IsNull(sessionContext.ClientProxy);
Assert.AreEqual(OperationResult.Success, result);
}
[TestMethod]
public void MustDoNothingOnRepeatIfNoClientRunning()
{
process.SetupGet(p => p.HasTerminated).Returns(true);
sessionContext.ClientProcess = process.Object;
var result = sut.Repeat();
process.VerifyGet(p => p.HasTerminated, Times.Once);
process.VerifyNoOtherCalls();
processFactory.VerifyNoOtherCalls();
proxy.VerifyNoOtherCalls();
proxyFactory.VerifyNoOtherCalls();
runtimeHost.VerifyNoOtherCalls();
Assert.AreEqual(OperationResult.Success, result);
}
}
}

View file

@ -11,7 +11,6 @@ using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq; using Moq;
using SafeExamBrowser.Contracts.Communication.Data; using SafeExamBrowser.Contracts.Communication.Data;
using SafeExamBrowser.Contracts.Communication.Proxies;
using SafeExamBrowser.Contracts.Configuration; using SafeExamBrowser.Contracts.Configuration;
using SafeExamBrowser.Contracts.Configuration.Settings; using SafeExamBrowser.Contracts.Configuration.Settings;
using SafeExamBrowser.Contracts.Core.OperationModel; using SafeExamBrowser.Contracts.Core.OperationModel;
@ -28,7 +27,10 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
private Mock<ILogger> logger; private Mock<ILogger> logger;
private Mock<IConfigurationRepository> repository; private Mock<IConfigurationRepository> repository;
private Mock<IResourceLoader> resourceLoader; private Mock<IResourceLoader> resourceLoader;
private Mock<ISessionConfiguration> session;
private SessionContext sessionContext;
private Settings settings; private Settings settings;
private ConfigurationOperation sut; private ConfigurationOperation sut;
[TestInitialize] [TestInitialize]
@ -38,32 +40,36 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
logger = new Mock<ILogger>(); logger = new Mock<ILogger>();
repository = new Mock<IConfigurationRepository>(); repository = new Mock<IConfigurationRepository>();
resourceLoader = new Mock<IResourceLoader>(); resourceLoader = new Mock<IResourceLoader>();
session = new Mock<ISessionConfiguration>();
sessionContext = new SessionContext();
settings = new Settings(); settings = new Settings();
appConfig.AppDataFolder = @"C:\Not\Really\AppData"; appConfig.AppDataFolder = @"C:\Not\Really\AppData";
appConfig.DefaultSettingsFileName = "SettingsDummy.txt"; appConfig.DefaultSettingsFileName = "SettingsDummy.txt";
appConfig.ProgramDataFolder = @"C:\Not\Really\ProgramData"; appConfig.ProgramDataFolder = @"C:\Not\Really\ProgramData";
session.SetupGet(s => s.AppConfig).Returns(appConfig);
repository.SetupGet(r => r.CurrentSettings).Returns(settings); session.SetupGet(s => s.Settings).Returns(settings);
sessionContext.Next = session.Object;
} }
[TestMethod] [TestMethod]
public void MustUseCommandLineArgumentAs1stPrio() public void MustUseCommandLineArgumentAs1stPrio()
{ {
var settings = default(Settings);
var url = @"http://www.safeexambrowser.org/whatever.seb"; var url = @"http://www.safeexambrowser.org/whatever.seb";
var location = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), nameof(Operations)); var location = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), nameof(Operations));
appConfig.ProgramDataFolder = location; appConfig.ProgramDataFolder = location;
appConfig.AppDataFolder = location; appConfig.AppDataFolder = location;
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), null, null)).Returns(LoadStatus.Success); repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null)).Returns(LoadStatus.Success);
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, new[] { "blubb.exe", url }); sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
sut.Perform(); sut.Perform();
var resource = new Uri(url); var resource = new Uri(url);
repository.Verify(r => r.LoadSettings(It.Is<Uri>(u => u.Equals(resource)), null, null), Times.Once); repository.Verify(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, null, null), Times.Once);
} }
[TestMethod] [TestMethod]
@ -74,14 +80,14 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
appConfig.ProgramDataFolder = location; appConfig.ProgramDataFolder = location;
appConfig.AppDataFolder = $@"{location}\WRONG"; appConfig.AppDataFolder = $@"{location}\WRONG";
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), null, null)).Returns(LoadStatus.Success); repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null)).Returns(LoadStatus.Success);
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, null); sut = new ConfigurationOperation(null, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
sut.Perform(); sut.Perform();
var resource = new Uri(Path.Combine(location, "SettingsDummy.txt")); var resource = new Uri(Path.Combine(location, "SettingsDummy.txt"));
repository.Verify(r => r.LoadSettings(It.Is<Uri>(u => u.Equals(resource)), null, null), Times.Once); repository.Verify(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, null, null), Times.Once);
} }
[TestMethod] [TestMethod]
@ -91,20 +97,20 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
appConfig.AppDataFolder = location; appConfig.AppDataFolder = location;
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), null, null)).Returns(LoadStatus.Success); repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null)).Returns(LoadStatus.Success);
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, null); sut = new ConfigurationOperation(null, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
sut.Perform(); sut.Perform();
var resource = new Uri(Path.Combine(location, "SettingsDummy.txt")); var resource = new Uri(Path.Combine(location, "SettingsDummy.txt"));
repository.Verify(r => r.LoadSettings(It.Is<Uri>(u => u.Equals(resource)), null, null), Times.Once); repository.Verify(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, null, null), Times.Once);
} }
[TestMethod] [TestMethod]
public void MustFallbackToDefaultsAsLastPrio() public void MustFallbackToDefaultsAsLastPrio()
{ {
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, null); sut = new ConfigurationOperation(null, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
sut.Perform(); sut.Perform();
repository.Verify(r => r.LoadDefaultSettings(), Times.Once); repository.Verify(r => r.LoadDefaultSettings(), Times.Once);
@ -114,9 +120,9 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
public void MustAbortIfWishedByUser() public void MustAbortIfWishedByUser()
{ {
appConfig.ProgramDataFolder = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), nameof(Operations)); appConfig.ProgramDataFolder = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), nameof(Operations));
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), null, null)).Returns(LoadStatus.Success); repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null)).Returns(LoadStatus.Success);
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, null); sut = new ConfigurationOperation(null, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
sut.ActionRequired += args => sut.ActionRequired += args =>
{ {
if (args is ConfigurationCompletedEventArgs c) if (args is ConfigurationCompletedEventArgs c)
@ -133,9 +139,9 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
[TestMethod] [TestMethod]
public void MustNotAbortIfNotWishedByUser() public void MustNotAbortIfNotWishedByUser()
{ {
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), null, null)).Returns(LoadStatus.Success); repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null)).Returns(LoadStatus.Success);
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, null); sut = new ConfigurationOperation(null, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
sut.ActionRequired += args => sut.ActionRequired += args =>
{ {
if (args is ConfigurationCompletedEventArgs c) if (args is ConfigurationCompletedEventArgs c)
@ -153,9 +159,9 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
public void MustNotAllowToAbortIfNotInConfigureClientMode() public void MustNotAllowToAbortIfNotInConfigureClientMode()
{ {
settings.ConfigurationMode = ConfigurationMode.Exam; settings.ConfigurationMode = ConfigurationMode.Exam;
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), null, null)).Returns(LoadStatus.Success); repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null)).Returns(LoadStatus.Success);
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, null); sut = new ConfigurationOperation(null, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
sut.ActionRequired += args => sut.ActionRequired += args =>
{ {
if (args is ConfigurationCompletedEventArgs c) if (args is ConfigurationCompletedEventArgs c)
@ -172,10 +178,10 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
{ {
repository.Setup(r => r.LoadDefaultSettings()); repository.Setup(r => r.LoadDefaultSettings());
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, null); sut = new ConfigurationOperation(null, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
sut.Perform(); sut.Perform();
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, new string[] { }); sut = new ConfigurationOperation(new string[] { }, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
sut.Perform(); sut.Perform();
repository.Verify(r => r.LoadDefaultSettings(), Times.Exactly(2)); repository.Verify(r => r.LoadDefaultSettings(), Times.Exactly(2));
@ -186,7 +192,7 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
{ {
var uri = @"an/invalid\uri.'*%yolo/()你好"; var uri = @"an/invalid\uri.'*%yolo/()你好";
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, new[] { "blubb.exe", uri }); sut = new ConfigurationOperation(new[] { "blubb.exe", uri }, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
sut.Perform(); sut.Perform();
} }
@ -195,9 +201,9 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
{ {
var url = @"http://www.safeexambrowser.org/whatever.seb"; var url = @"http://www.safeexambrowser.org/whatever.seb";
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), null, null)).Returns(LoadStatus.AdminPasswordNeeded); repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null)).Returns(LoadStatus.AdminPasswordNeeded);
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, new[] { "blubb.exe", url }); sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
sut.ActionRequired += args => sut.ActionRequired += args =>
{ {
if (args is PasswordRequiredEventArgs p) if (args is PasswordRequiredEventArgs p)
@ -208,7 +214,7 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
sut.Perform(); sut.Perform();
repository.Verify(r => r.LoadSettings(It.IsAny<Uri>(), null, null), Times.Exactly(5)); repository.Verify(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null), Times.Exactly(5));
} }
[TestMethod] [TestMethod]
@ -216,9 +222,9 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
{ {
var url = @"http://www.safeexambrowser.org/whatever.seb"; var url = @"http://www.safeexambrowser.org/whatever.seb";
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), null, null)).Returns(LoadStatus.SettingsPasswordNeeded); repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null)).Returns(LoadStatus.SettingsPasswordNeeded);
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, new[] { "blubb.exe", url }); sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
sut.ActionRequired += args => sut.ActionRequired += args =>
{ {
if (args is PasswordRequiredEventArgs p) if (args is PasswordRequiredEventArgs p)
@ -229,7 +235,7 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
sut.Perform(); sut.Perform();
repository.Verify(r => r.LoadSettings(It.IsAny<Uri>(), null, null), Times.Exactly(5)); repository.Verify(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null), Times.Exactly(5));
} }
[TestMethod] [TestMethod]
@ -238,10 +244,10 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
var password = "test"; var password = "test";
var url = @"http://www.safeexambrowser.org/whatever.seb"; var url = @"http://www.safeexambrowser.org/whatever.seb";
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), null, null)).Returns(LoadStatus.AdminPasswordNeeded); repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null)).Returns(LoadStatus.AdminPasswordNeeded);
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), password, null)).Returns(LoadStatus.Success); repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, password, null)).Returns(LoadStatus.Success);
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, new[] { "blubb.exe", url }); sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
sut.ActionRequired += args => sut.ActionRequired += args =>
{ {
if (args is PasswordRequiredEventArgs p) if (args is PasswordRequiredEventArgs p)
@ -253,8 +259,8 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
sut.Perform(); sut.Perform();
repository.Verify(r => r.LoadSettings(It.IsAny<Uri>(), null, null), Times.Once); repository.Verify(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null), Times.Once);
repository.Verify(r => r.LoadSettings(It.IsAny<Uri>(), password, null), Times.Once); repository.Verify(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, password, null), Times.Once);
} }
[TestMethod] [TestMethod]
@ -263,10 +269,10 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
var password = "test"; var password = "test";
var url = @"http://www.safeexambrowser.org/whatever.seb"; var url = @"http://www.safeexambrowser.org/whatever.seb";
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), null, null)).Returns(LoadStatus.SettingsPasswordNeeded); repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null)).Returns(LoadStatus.SettingsPasswordNeeded);
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), null, password)).Returns(LoadStatus.Success); repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, password)).Returns(LoadStatus.Success);
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, new[] { "blubb.exe", url }); sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
sut.ActionRequired += args => sut.ActionRequired += args =>
{ {
if (args is PasswordRequiredEventArgs p) if (args is PasswordRequiredEventArgs p)
@ -278,8 +284,8 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
sut.Perform(); sut.Perform();
repository.Verify(r => r.LoadSettings(It.IsAny<Uri>(), null, null), Times.Once); repository.Verify(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null), Times.Once);
repository.Verify(r => r.LoadSettings(It.IsAny<Uri>(), null, password), Times.Once); repository.Verify(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, password), Times.Once);
} }
[TestMethod] [TestMethod]
@ -287,9 +293,9 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
{ {
var url = @"http://www.safeexambrowser.org/whatever.seb"; var url = @"http://www.safeexambrowser.org/whatever.seb";
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), null, null)).Returns(LoadStatus.AdminPasswordNeeded); repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null)).Returns(LoadStatus.AdminPasswordNeeded);
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, new[] { "blubb.exe", url }); sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
sut.ActionRequired += args => sut.ActionRequired += args =>
{ {
if (args is PasswordRequiredEventArgs p) if (args is PasswordRequiredEventArgs p)
@ -308,9 +314,9 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
{ {
var url = @"http://www.safeexambrowser.org/whatever.seb"; var url = @"http://www.safeexambrowser.org/whatever.seb";
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), null, null)).Returns(LoadStatus.SettingsPasswordNeeded); repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null)).Returns(LoadStatus.SettingsPasswordNeeded);
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, new[] { "blubb.exe", url }); sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
sut.ActionRequired += args => sut.ActionRequired += args =>
{ {
if (args is PasswordRequiredEventArgs p) if (args is PasswordRequiredEventArgs p)
@ -331,11 +337,11 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
var settingsPassword = "abc"; var settingsPassword = "abc";
var url = @"http://www.safeexambrowser.org/whatever.seb"; var url = @"http://www.safeexambrowser.org/whatever.seb";
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), null, null)).Returns(LoadStatus.SettingsPasswordNeeded); repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null)).Returns(LoadStatus.SettingsPasswordNeeded);
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), null, settingsPassword)).Returns(LoadStatus.AdminPasswordNeeded); repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, settingsPassword)).Returns(LoadStatus.AdminPasswordNeeded);
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), adminPassword, settingsPassword)).Returns(LoadStatus.Success); repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, adminPassword, settingsPassword)).Returns(LoadStatus.Success);
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, new[] { "blubb.exe", url }); sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
sut.ActionRequired += args => sut.ActionRequired += args =>
{ {
if (args is PasswordRequiredEventArgs p) if (args is PasswordRequiredEventArgs p)
@ -347,30 +353,9 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
sut.Perform(); sut.Perform();
repository.Verify(r => r.LoadSettings(It.IsAny<Uri>(), null, null), Times.Once); repository.Verify(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null), Times.Once);
repository.Verify(r => r.LoadSettings(It.IsAny<Uri>(), null, settingsPassword), Times.Once); repository.Verify(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, settingsPassword), Times.Once);
repository.Verify(r => r.LoadSettings(It.IsAny<Uri>(), adminPassword, settingsPassword), Times.Once); repository.Verify(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, adminPassword, settingsPassword), Times.Once);
}
[TestMethod]
public void MustNotWaitForPasswordViaClientIfCommunicationHasFailed()
{
var clientProxy = new Mock<IClientProxy>();
var communication = new CommunicationResult(false);
var session = new Mock<ISessionData>();
var url = @"http://www.safeexambrowser.org/whatever.seb";
clientProxy.Setup(c => c.RequestPassword(It.IsAny<PasswordRequestPurpose>(), It.IsAny<Guid>())).Returns(communication);
repository.SetupGet(r => r.CurrentSession).Returns(session.Object);
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), null, null)).Returns(LoadStatus.SettingsPasswordNeeded);
session.SetupGet(r => r.ClientProxy).Returns(clientProxy.Object);
settings.KioskMode = KioskMode.CreateNewDesktop;
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, new[] { "blubb.exe", url });
var result = sut.Perform();
Assert.AreEqual(OperationResult.Aborted, result);
} }
[TestMethod] [TestMethod]
@ -379,9 +364,9 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
var url = @"http://www.safeexambrowser.org/whatever.seb"; var url = @"http://www.safeexambrowser.org/whatever.seb";
resourceLoader.Setup(r => r.IsHtmlResource(It.IsAny<Uri>())).Returns(false); resourceLoader.Setup(r => r.IsHtmlResource(It.IsAny<Uri>())).Returns(false);
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), null, null)).Returns(LoadStatus.InvalidData); repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null)).Returns(LoadStatus.InvalidData);
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, new[] { "blubb.exe", url }); sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
var result = sut.Perform(); var result = sut.Perform();
@ -394,9 +379,9 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
var url = "http://www.blubb.org/some/resource.html"; var url = "http://www.blubb.org/some/resource.html";
resourceLoader.Setup(r => r.IsHtmlResource(It.IsAny<Uri>())).Returns(true); resourceLoader.Setup(r => r.IsHtmlResource(It.IsAny<Uri>())).Returns(true);
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), null, null)).Returns(LoadStatus.InvalidData); repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null)).Returns(LoadStatus.InvalidData);
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, new[] { "blubb.exe", url }); sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
var result = sut.Perform(); var result = sut.Perform();
@ -410,14 +395,14 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
var location = Path.GetDirectoryName(GetType().Assembly.Location); var location = Path.GetDirectoryName(GetType().Assembly.Location);
var resource = new Uri(Path.Combine(location, nameof(Operations), "SettingsDummy.txt")); var resource = new Uri(Path.Combine(location, nameof(Operations), "SettingsDummy.txt"));
repository.SetupGet(r => r.ReconfigurationFilePath).Returns(resource.AbsolutePath); sessionContext.ReconfigurationFilePath = resource.AbsolutePath;
repository.Setup(r => r.LoadSettings(It.Is<Uri>(u => u.Equals(resource)), null, null)).Returns(LoadStatus.Success); repository.Setup(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, null, null)).Returns(LoadStatus.Success);
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, null); sut = new ConfigurationOperation(null, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
var result = sut.Repeat(); var result = sut.Repeat();
repository.Verify(r => r.LoadSettings(It.Is<Uri>(u => u.Equals(resource)), null, null), Times.Once); repository.Verify(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, null, null), Times.Once);
Assert.AreEqual(OperationResult.Success, result); Assert.AreEqual(OperationResult.Success, result);
} }
@ -427,20 +412,20 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
{ {
var resource = new Uri("file:///C:/does/not/exist.txt"); var resource = new Uri("file:///C:/does/not/exist.txt");
repository.SetupGet(r => r.ReconfigurationFilePath).Returns(null as string); sessionContext.ReconfigurationFilePath = null;
repository.Setup(r => r.LoadSettings(It.IsAny<Uri>(), null, null)).Returns(LoadStatus.Success); repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, null, null)).Returns(LoadStatus.Success);
sut = new ConfigurationOperation(appConfig, repository.Object, logger.Object, resourceLoader.Object, null); sut = new ConfigurationOperation(null, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
var result = sut.Repeat(); var result = sut.Repeat();
repository.Verify(r => r.LoadSettings(It.Is<Uri>(u => u.Equals(resource)), null, null), Times.Never); repository.Verify(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, null, null), Times.Never);
Assert.AreEqual(OperationResult.Failed, result); Assert.AreEqual(OperationResult.Failed, result);
repository.SetupGet(r => r.ReconfigurationFilePath).Returns(resource.AbsolutePath); sessionContext.ReconfigurationFilePath = resource.AbsolutePath;
result = sut.Repeat(); result = sut.Repeat();
repository.Verify(r => r.LoadSettings(It.Is<Uri>(u => u.Equals(resource)), null, null), Times.Never); repository.Verify(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, null, null), Times.Never);
Assert.AreEqual(OperationResult.Failed, result); Assert.AreEqual(OperationResult.Failed, result);
} }
} }

View file

@ -10,6 +10,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq; using Moq;
using SafeExamBrowser.Contracts.Configuration; using SafeExamBrowser.Contracts.Configuration;
using SafeExamBrowser.Contracts.Configuration.Settings; using SafeExamBrowser.Contracts.Configuration.Settings;
using SafeExamBrowser.Contracts.Core.OperationModel;
using SafeExamBrowser.Contracts.Logging; using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.WindowsApi; using SafeExamBrowser.Contracts.WindowsApi;
using SafeExamBrowser.Runtime.Operations; using SafeExamBrowser.Runtime.Operations;
@ -19,27 +20,37 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
[TestClass] [TestClass]
public class KioskModeOperationTests public class KioskModeOperationTests
{ {
private Mock<IConfigurationRepository> configuration; private Mock<ISessionConfiguration> currentSession;
private Settings currentSettings;
private Mock<IDesktopFactory> desktopFactory; private Mock<IDesktopFactory> desktopFactory;
private Mock<IExplorerShell> explorerShell; private Mock<IExplorerShell> explorerShell;
private Mock<ILogger> logger; private Mock<ILogger> logger;
private Mock<ISessionConfiguration> nextSession;
private Settings nextSettings;
private Mock<IProcessFactory> processFactory; private Mock<IProcessFactory> processFactory;
private Settings settings; private SessionContext sessionContext;
private KioskModeOperation sut; private KioskModeOperation sut;
[TestInitialize] [TestInitialize]
public void Initialize() public void Initialize()
{ {
configuration = new Mock<IConfigurationRepository>(); currentSession = new Mock<ISessionConfiguration>();
currentSettings = new Settings();
desktopFactory = new Mock<IDesktopFactory>(); desktopFactory = new Mock<IDesktopFactory>();
explorerShell = new Mock<IExplorerShell>(); explorerShell = new Mock<IExplorerShell>();
logger = new Mock<ILogger>(); logger = new Mock<ILogger>();
nextSession = new Mock<ISessionConfiguration>();
nextSettings = new Settings();
processFactory = new Mock<IProcessFactory>(); processFactory = new Mock<IProcessFactory>();
settings = new Settings(); sessionContext = new SessionContext();
configuration.SetupGet(c => c.CurrentSettings).Returns(settings); currentSession.SetupGet(s => s.Settings).Returns(currentSettings);
nextSession.SetupGet(s => s.Settings).Returns(nextSettings);
sessionContext.Current = currentSession.Object;
sessionContext.Next = nextSession.Object;
sut = new KioskModeOperation(configuration.Object, desktopFactory.Object, explorerShell.Object, logger.Object, processFactory.Object); sut = new KioskModeOperation(desktopFactory.Object, explorerShell.Object, logger.Object, processFactory.Object, sessionContext);
} }
[TestMethod] [TestMethod]
@ -54,7 +65,7 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
var setStartup = 0; var setStartup = 0;
var suspend = 0; var suspend = 0;
settings.KioskMode = KioskMode.CreateNewDesktop; nextSettings.KioskMode = KioskMode.CreateNewDesktop;
desktopFactory.Setup(f => f.GetCurrent()).Callback(() => getCurrrent = ++order).Returns(originalDesktop.Object); desktopFactory.Setup(f => f.GetCurrent()).Callback(() => getCurrrent = ++order).Returns(originalDesktop.Object);
desktopFactory.Setup(f => f.CreateNew(It.IsAny<string>())).Callback(() => createNew = ++order).Returns(newDesktop.Object); desktopFactory.Setup(f => f.CreateNew(It.IsAny<string>())).Callback(() => createNew = ++order).Returns(newDesktop.Object);
@ -70,6 +81,8 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
processFactory.VerifySet(f => f.StartupDesktop = newDesktop.Object, Times.Once); processFactory.VerifySet(f => f.StartupDesktop = newDesktop.Object, Times.Once);
explorerShell.Verify(s => s.Suspend(), Times.Once); explorerShell.Verify(s => s.Suspend(), Times.Once);
Assert.AreSame(sessionContext.NewDesktop, newDesktop.Object);
Assert.AreSame(sessionContext.OriginalDesktop, originalDesktop.Object);
Assert.AreEqual(1, getCurrrent); Assert.AreEqual(1, getCurrrent);
Assert.AreEqual(2, createNew); Assert.AreEqual(2, createNew);
Assert.AreEqual(3, activate); Assert.AreEqual(3, activate);
@ -80,7 +93,7 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
[TestMethod] [TestMethod]
public void MustCorrectlyInitializeDisableExplorerShell() public void MustCorrectlyInitializeDisableExplorerShell()
{ {
settings.KioskMode = KioskMode.DisableExplorerShell; nextSettings.KioskMode = KioskMode.DisableExplorerShell;
sut.Perform(); sut.Perform();
@ -98,7 +111,8 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
var close = 0; var close = 0;
var resume = 0; var resume = 0;
settings.KioskMode = KioskMode.CreateNewDesktop; currentSettings.KioskMode = KioskMode.CreateNewDesktop;
nextSettings.KioskMode = KioskMode.CreateNewDesktop;
desktopFactory.Setup(f => f.GetCurrent()).Returns(originalDesktop.Object); desktopFactory.Setup(f => f.GetCurrent()).Returns(originalDesktop.Object);
desktopFactory.Setup(f => f.CreateNew(It.IsAny<string>())).Returns(newDesktop.Object); desktopFactory.Setup(f => f.CreateNew(It.IsAny<string>())).Returns(newDesktop.Object);
@ -107,14 +121,16 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
newDesktop.Setup(d => d.Close()).Callback(() => close = ++order); newDesktop.Setup(d => d.Close()).Callback(() => close = ++order);
explorerShell.Setup(s => s.Resume()).Callback(() => resume = ++order); explorerShell.Setup(s => s.Resume()).Callback(() => resume = ++order);
sut.Perform(); var performResult = sut.Perform();
sut.Revert(); var revertResult = sut.Revert();
originalDesktop.Verify(d => d.Activate(), Times.Once); originalDesktop.Verify(d => d.Activate(), Times.Once);
processFactory.VerifySet(f => f.StartupDesktop = originalDesktop.Object, Times.Once); processFactory.VerifySet(f => f.StartupDesktop = originalDesktop.Object, Times.Once);
newDesktop.Verify(d => d.Close(), Times.Once); newDesktop.Verify(d => d.Close(), Times.Once);
explorerShell.Verify(s => s.Resume(), Times.Once); explorerShell.Verify(s => s.Resume(), Times.Once);
Assert.AreEqual(OperationResult.Success, performResult);
Assert.AreEqual(OperationResult.Success, revertResult);
Assert.AreEqual(1, activate); Assert.AreEqual(1, activate);
Assert.AreEqual(2, setStartup); Assert.AreEqual(2, setStartup);
Assert.AreEqual(3, close); Assert.AreEqual(3, close);
@ -124,25 +140,32 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
[TestMethod] [TestMethod]
public void MustCorrectlyRevertDisableExplorerShell() public void MustCorrectlyRevertDisableExplorerShell()
{ {
settings.KioskMode = KioskMode.DisableExplorerShell; currentSettings.KioskMode = KioskMode.DisableExplorerShell;
nextSettings.KioskMode = KioskMode.DisableExplorerShell;
sut.Perform(); var performResult = sut.Perform();
sut.Revert(); var revertResult = sut.Revert();
explorerShell.Verify(s => s.Start(), Times.Once); explorerShell.Verify(s => s.Start(), Times.Once);
Assert.AreEqual(OperationResult.Success, performResult);
Assert.AreEqual(OperationResult.Success, revertResult);
} }
[TestMethod] [TestMethod]
public void MustCorrectlySwitchToOtherKioskModeWhenRepeating() public void MustCorrectlyStartNewKioskModeWhenRepeating()
{ {
var newDesktop = new Mock<IDesktop>(); var newDesktop = new Mock<IDesktop>();
var originalDesktop = new Mock<IDesktop>(); var originalDesktop = new Mock<IDesktop>();
var result = default(OperationResult);
desktopFactory.Setup(f => f.GetCurrent()).Returns(originalDesktop.Object); desktopFactory.Setup(f => f.GetCurrent()).Returns(originalDesktop.Object);
desktopFactory.Setup(f => f.CreateNew(It.IsAny<string>())).Returns(newDesktop.Object); desktopFactory.Setup(f => f.CreateNew(It.IsAny<string>())).Returns(newDesktop.Object);
nextSettings.KioskMode = KioskMode.CreateNewDesktop;
settings.KioskMode = KioskMode.CreateNewDesktop; result = sut.Perform();
sut.Perform();
Assert.AreEqual(OperationResult.Success, result);
explorerShell.Verify(s => s.Terminate(), Times.Never); explorerShell.Verify(s => s.Terminate(), Times.Never);
explorerShell.Verify(s => s.Start(), Times.Never); explorerShell.Verify(s => s.Start(), Times.Never);
@ -152,33 +175,43 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
newDesktop.Verify(d => d.Close(), Times.Never); newDesktop.Verify(d => d.Close(), Times.Never);
originalDesktop.Verify(d => d.Activate(), Times.Never); originalDesktop.Verify(d => d.Activate(), Times.Never);
settings.KioskMode = KioskMode.DisableExplorerShell; nextSettings.KioskMode = KioskMode.DisableExplorerShell;
sut.Repeat();
explorerShell.Verify(s => s.Resume(), Times.Once); result = sut.Repeat();
Assert.AreEqual(OperationResult.Success, result);
explorerShell.Verify(s => s.Resume(), Times.Never);
explorerShell.Verify(s => s.Terminate(), Times.Once); explorerShell.Verify(s => s.Terminate(), Times.Once);
explorerShell.Verify(s => s.Suspend(), Times.Once); explorerShell.Verify(s => s.Suspend(), Times.Once);
explorerShell.Verify(s => s.Start(), Times.Never); explorerShell.Verify(s => s.Start(), Times.Never);
newDesktop.Verify(d => d.Activate(), Times.Once); newDesktop.Verify(d => d.Activate(), Times.Once);
newDesktop.Verify(d => d.Close(), Times.Once); newDesktop.Verify(d => d.Close(), Times.Never);
originalDesktop.Verify(d => d.Activate(), Times.Once); originalDesktop.Verify(d => d.Activate(), Times.Never);
settings.KioskMode = KioskMode.CreateNewDesktop; currentSettings.KioskMode = nextSettings.KioskMode;
sut.Repeat(); nextSettings.KioskMode = KioskMode.CreateNewDesktop;
explorerShell.Verify(s => s.Resume(), Times.Once); result = sut.Repeat();
Assert.AreEqual(OperationResult.Success, result);
explorerShell.Verify(s => s.Resume(), Times.Never);
explorerShell.Verify(s => s.Terminate(), Times.Once); explorerShell.Verify(s => s.Terminate(), Times.Once);
explorerShell.Verify(s => s.Suspend(), Times.Exactly(2)); explorerShell.Verify(s => s.Suspend(), Times.Exactly(2));
explorerShell.Verify(s => s.Start(), Times.Once); explorerShell.Verify(s => s.Start(), Times.Never);
newDesktop.Verify(d => d.Activate(), Times.Exactly(2)); newDesktop.Verify(d => d.Activate(), Times.Exactly(2));
newDesktop.Verify(d => d.Close(), Times.Once); newDesktop.Verify(d => d.Close(), Times.Never);
originalDesktop.Verify(d => d.Activate(), Times.Once); originalDesktop.Verify(d => d.Activate(), Times.Never);
Assert.AreSame(sessionContext.NewDesktop, newDesktop.Object);
Assert.AreSame(sessionContext.OriginalDesktop, originalDesktop.Object);
} }
[TestMethod] [TestMethod]
public void MustDoNothingWithoutKioskMode() public void MustDoNothingWithoutKioskMode()
{ {
settings.KioskMode = KioskMode.None; nextSettings.KioskMode = KioskMode.None;
sut.Perform(); sut.Perform();
sut.Repeat(); sut.Repeat();
@ -195,7 +228,8 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
var newDesktop = new Mock<IDesktop>(); var newDesktop = new Mock<IDesktop>();
var originalDesktop = new Mock<IDesktop>(); var originalDesktop = new Mock<IDesktop>();
settings.KioskMode = KioskMode.CreateNewDesktop; currentSettings.KioskMode = KioskMode.CreateNewDesktop;
nextSettings.KioskMode = KioskMode.CreateNewDesktop;
desktopFactory.Setup(f => f.GetCurrent()).Returns(originalDesktop.Object); desktopFactory.Setup(f => f.GetCurrent()).Returns(originalDesktop.Object);
desktopFactory.Setup(f => f.CreateNew(It.IsAny<string>())).Returns(newDesktop.Object); desktopFactory.Setup(f => f.CreateNew(It.IsAny<string>())).Returns(newDesktop.Object);
@ -212,12 +246,16 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
newDesktop.Verify(d => d.Activate(), Times.Once); newDesktop.Verify(d => d.Activate(), Times.Once);
processFactory.VerifySet(f => f.StartupDesktop = newDesktop.Object, Times.Once); processFactory.VerifySet(f => f.StartupDesktop = newDesktop.Object, Times.Once);
explorerShell.Verify(s => s.Suspend(), Times.Once); explorerShell.Verify(s => s.Suspend(), Times.Once);
Assert.AreSame(sessionContext.NewDesktop, newDesktop.Object);
Assert.AreSame(sessionContext.OriginalDesktop, originalDesktop.Object);
} }
[TestMethod] [TestMethod]
public void MustNotReinitializeDisableExplorerShellWhenRepeating() public void MustNotReinitializeDisableExplorerShellWhenRepeating()
{ {
settings.KioskMode = KioskMode.DisableExplorerShell; currentSettings.KioskMode = KioskMode.DisableExplorerShell;
nextSettings.KioskMode = KioskMode.DisableExplorerShell;
sut.Perform(); sut.Perform();
sut.Repeat(); sut.Repeat();

View file

@ -0,0 +1,147 @@
/*
* 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 Moq;
using SafeExamBrowser.Contracts.Configuration;
using SafeExamBrowser.Contracts.Configuration.Settings;
using SafeExamBrowser.Contracts.Core.OperationModel;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.WindowsApi;
using SafeExamBrowser.Runtime.Operations;
namespace SafeExamBrowser.Runtime.UnitTests.Operations
{
[TestClass]
public class KioskModeTerminationOperationTests
{
private Mock<IDesktopFactory> desktopFactory;
private Mock<IExplorerShell> explorerShell;
private Mock<ILogger> logger;
private Mock<ISessionConfiguration> nextSession;
private Settings nextSettings;
private Mock<IProcessFactory> processFactory;
private SessionContext sessionContext;
private KioskModeTerminationOperation sut;
private Mock<ISessionConfiguration> currentSession;
private Settings currentSettings;
[TestInitialize]
public void Initialize()
{
currentSession = new Mock<ISessionConfiguration>();
currentSettings = new Settings();
desktopFactory = new Mock<IDesktopFactory>();
explorerShell = new Mock<IExplorerShell>();
logger = new Mock<ILogger>();
nextSession = new Mock<ISessionConfiguration>();
nextSettings = new Settings();
processFactory = new Mock<IProcessFactory>();
sessionContext = new SessionContext();
currentSession.SetupGet(s => s.Settings).Returns(currentSettings);
nextSession.SetupGet(s => s.Settings).Returns(nextSettings);
sessionContext.Current = currentSession.Object;
sessionContext.Next = nextSession.Object;
sut = new KioskModeTerminationOperation(desktopFactory.Object, explorerShell.Object, logger.Object, processFactory.Object, sessionContext);
}
[TestMethod]
public void MustDoNothingOnPerform()
{
var result = sut.Perform();
currentSession.VerifyNoOtherCalls();
desktopFactory.VerifyNoOtherCalls();
explorerShell.VerifyNoOtherCalls();
logger.VerifyNoOtherCalls();
nextSession.VerifyNoOtherCalls();
processFactory.VerifyNoOtherCalls();
Assert.AreEqual(OperationResult.Success, result);
}
[TestMethod]
public void MustCorrectlyTerminateOldKioskModeWhenRepeating()
{
var newDesktop = new Mock<IDesktop>();
var originalDesktop = new Mock<IDesktop>();
var result = default(OperationResult);
sessionContext.NewDesktop = newDesktop.Object;
sessionContext.OriginalDesktop = originalDesktop.Object;
sessionContext.ActiveMode = KioskMode.DisableExplorerShell;
nextSettings.KioskMode = KioskMode.CreateNewDesktop;
result = sut.Repeat();
Assert.AreEqual(OperationResult.Success, result);
explorerShell.Verify(s => s.Resume(), Times.Never);
explorerShell.Verify(s => s.Start(), Times.Once);
explorerShell.Verify(s => s.Suspend(), Times.Never);
explorerShell.Verify(s => s.Terminate(), Times.Never);
newDesktop.Verify(d => d.Activate(), Times.Never);
newDesktop.Verify(d => d.Close(), Times.Never);
originalDesktop.Verify(d => d.Activate(), Times.Never);
sessionContext.ActiveMode = nextSettings.KioskMode;
nextSettings.KioskMode = KioskMode.DisableExplorerShell;
result = sut.Repeat();
Assert.AreEqual(OperationResult.Success, result);
explorerShell.Verify(s => s.Resume(), Times.Once);
explorerShell.Verify(s => s.Start(), Times.Once);
explorerShell.Verify(s => s.Suspend(), Times.Never);
explorerShell.Verify(s => s.Terminate(), Times.Never);
newDesktop.Verify(d => d.Activate(), Times.Never);
newDesktop.Verify(d => d.Close(), Times.Once);
originalDesktop.Verify(d => d.Activate(), Times.Once);
sessionContext.ActiveMode = nextSettings.KioskMode;
nextSettings.KioskMode = KioskMode.CreateNewDesktop;
result = sut.Repeat();
Assert.AreEqual(OperationResult.Success, result);
explorerShell.Verify(s => s.Resume(), Times.Once);
explorerShell.Verify(s => s.Start(), Times.Exactly(2));
explorerShell.Verify(s => s.Suspend(), Times.Never);
explorerShell.Verify(s => s.Terminate(), Times.Never);
newDesktop.Verify(d => d.Activate(), Times.Never);
newDesktop.Verify(d => d.Close(), Times.Once);
originalDesktop.Verify(d => d.Activate(), Times.Once);
}
[TestMethod]
public void MustNotTerminateKioskModeIfSameInNextSesssion()
{
}
[TestMethod]
public void MustDoNothingOnRevert()
{
var result = sut.Revert();
currentSession.VerifyNoOtherCalls();
desktopFactory.VerifyNoOtherCalls();
explorerShell.VerifyNoOtherCalls();
logger.VerifyNoOtherCalls();
nextSession.VerifyNoOtherCalls();
processFactory.VerifyNoOtherCalls();
Assert.AreEqual(OperationResult.Success, result);
}
}
}

View file

@ -23,8 +23,8 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
{ {
private Mock<ILogger> logger; private Mock<ILogger> logger;
private Mock<IServiceProxy> service; private Mock<IServiceProxy> service;
private Mock<IConfigurationRepository> configuration; private Mock<ISessionConfiguration> session;
private Mock<ISessionData> session; private SessionContext sessionContext;
private Settings settings; private Settings settings;
private ServiceOperation sut; private ServiceOperation sut;
@ -33,26 +33,27 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
{ {
logger = new Mock<ILogger>(); logger = new Mock<ILogger>();
service = new Mock<IServiceProxy>(); service = new Mock<IServiceProxy>();
configuration = new Mock<IConfigurationRepository>(); session = new Mock<ISessionConfiguration>();
session = new Mock<ISessionData>(); sessionContext = new SessionContext();
settings = new Settings(); settings = new Settings();
configuration.SetupGet(c => c.CurrentSession).Returns(session.Object); sessionContext.Current = session.Object;
configuration.SetupGet(c => c.CurrentSettings).Returns(settings); sessionContext.Next = session.Object;
session.SetupGet(s => s.Settings).Returns(settings);
sut = new ServiceOperation(configuration.Object, logger.Object, service.Object); sut = new ServiceOperation(logger.Object, service.Object, sessionContext);
} }
[TestMethod] [TestMethod]
public void MustConnectToService() public void MustConnectToService()
{ {
service.Setup(s => s.Connect(null, true)).Returns(true); service.Setup(s => s.Connect(null, true)).Returns(true);
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Mandatory }); settings.ServicePolicy = ServicePolicy.Mandatory;
sut.Perform(); sut.Perform();
service.Setup(s => s.Connect(null, true)).Returns(true); service.Setup(s => s.Connect(null, true)).Returns(true);
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Optional }); settings.ServicePolicy = ServicePolicy.Optional;
sut.Perform(); sut.Perform();
@ -83,12 +84,12 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
public void MustNotFailIfServiceNotAvailable() public void MustNotFailIfServiceNotAvailable()
{ {
service.Setup(s => s.Connect(null, true)).Returns(false); service.Setup(s => s.Connect(null, true)).Returns(false);
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Mandatory }); settings.ServicePolicy = ServicePolicy.Mandatory;
sut.Perform(); sut.Perform();
service.Setup(s => s.Connect(null, true)).Returns(false); service.Setup(s => s.Connect(null, true)).Returns(false);
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Optional }); settings.ServicePolicy = ServicePolicy.Optional;
sut.Perform(); sut.Perform();
} }
@ -97,7 +98,7 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
public void MustFailIfServiceMandatoryAndNotAvailable() public void MustFailIfServiceMandatoryAndNotAvailable()
{ {
service.Setup(s => s.Connect(null, true)).Returns(false); service.Setup(s => s.Connect(null, true)).Returns(false);
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Mandatory }); settings.ServicePolicy = ServicePolicy.Mandatory;
var result = sut.Perform(); var result = sut.Perform();
@ -108,7 +109,7 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
public void MustNotFailIfServiceOptionalAndNotAvailable() public void MustNotFailIfServiceOptionalAndNotAvailable()
{ {
service.Setup(s => s.Connect(null, true)).Returns(false); service.Setup(s => s.Connect(null, true)).Returns(false);
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Optional }); settings.ServicePolicy = ServicePolicy.Optional;
var result = sut.Perform(); var result = sut.Perform();
@ -121,13 +122,13 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
public void MustDisconnectWhenReverting() public void MustDisconnectWhenReverting()
{ {
service.Setup(s => s.Connect(null, true)).Returns(true); service.Setup(s => s.Connect(null, true)).Returns(true);
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Mandatory }); settings.ServicePolicy = ServicePolicy.Mandatory;
sut.Perform(); sut.Perform();
sut.Revert(); sut.Revert();
service.Setup(s => s.Connect(null, true)).Returns(true); service.Setup(s => s.Connect(null, true)).Returns(true);
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Optional }); settings.ServicePolicy = ServicePolicy.Optional;
sut.Perform(); sut.Perform();
sut.Revert(); sut.Revert();
@ -162,7 +163,7 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
{ {
service.Setup(s => s.Connect(null, true)).Returns(true); service.Setup(s => s.Connect(null, true)).Returns(true);
service.Setup(s => s.Disconnect()).Returns(false); service.Setup(s => s.Disconnect()).Returns(false);
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Optional }); settings.ServicePolicy = ServicePolicy.Optional;
sut.Perform(); sut.Perform();
sut.Revert(); sut.Revert();
@ -174,13 +175,13 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
public void MustNotDisconnnectIfNotAvailable() public void MustNotDisconnnectIfNotAvailable()
{ {
service.Setup(s => s.Connect(null, true)).Returns(false); service.Setup(s => s.Connect(null, true)).Returns(false);
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Mandatory }); settings.ServicePolicy = ServicePolicy.Mandatory;
sut.Perform(); sut.Perform();
sut.Revert(); sut.Revert();
service.Setup(s => s.Connect(null, true)).Returns(false); service.Setup(s => s.Connect(null, true)).Returns(false);
configuration.SetupGet(s => s.CurrentSettings).Returns(new Settings { ServicePolicy = ServicePolicy.Optional }); settings.ServicePolicy = ServicePolicy.Optional;
sut.Perform(); sut.Perform();
sut.Revert(); sut.Revert();

View file

@ -0,0 +1,81 @@
/*
* 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 Moq;
using SafeExamBrowser.Contracts.Configuration;
using SafeExamBrowser.Contracts.Core.OperationModel;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Runtime.Operations;
namespace SafeExamBrowser.Runtime.UnitTests.Operations
{
[TestClass]
public class SessionActivationOperationTests
{
private Mock<ISessionConfiguration> currentSession;
private Mock<ILogger> logger;
private Mock<ISessionConfiguration> nextSession;
private SessionContext sessionContext;
private SessionActivationOperation sut;
[TestInitialize]
public void Initialize()
{
currentSession = new Mock<ISessionConfiguration>();
logger = new Mock<ILogger>();
nextSession = new Mock<ISessionConfiguration>();
sessionContext = new SessionContext();
sessionContext.Current = currentSession.Object;
sessionContext.Next = nextSession.Object;
sut = new SessionActivationOperation(logger.Object, sessionContext);
}
[TestMethod]
public void MustCorrectlyActivateFirstSession()
{
sessionContext.Current = null;
var result = sut.Perform();
currentSession.VerifyNoOtherCalls();
nextSession.VerifyGet(s => s.Id);
Assert.AreEqual(OperationResult.Success, result);
Assert.AreSame(sessionContext.Current, nextSession.Object);
Assert.IsNull(sessionContext.Next);
}
[TestMethod]
public void MustCorrectlySwitchSession()
{
var result = sut.Repeat();
currentSession.VerifyGet(s => s.Id);
nextSession.VerifyGet(s => s.Id);
Assert.AreEqual(OperationResult.Success, result);
Assert.AreSame(sessionContext.Current, nextSession.Object);
Assert.IsNull(sessionContext.Next);
}
[TestMethod]
public void MustAlwaysRevertSuccessfully()
{
var result = sut.Revert();
currentSession.VerifyNoOtherCalls();
nextSession.VerifyNoOtherCalls();
Assert.AreEqual(OperationResult.Success, result);
}
}
}

View file

@ -23,7 +23,9 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
private Mock<IConfigurationRepository> configuration; private Mock<IConfigurationRepository> configuration;
private Mock<ILogger> logger; private Mock<ILogger> logger;
private Mock<IRuntimeHost> runtimeHost; private Mock<IRuntimeHost> runtimeHost;
private Mock<ISessionData> session; private Mock<ISessionConfiguration> session;
private SessionContext sessionContext;
private SessionInitializationOperation sut; private SessionInitializationOperation sut;
[TestInitialize] [TestInitialize]
@ -33,12 +35,14 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
configuration = new Mock<IConfigurationRepository>(); configuration = new Mock<IConfigurationRepository>();
logger = new Mock<ILogger>(); logger = new Mock<ILogger>();
runtimeHost = new Mock<IRuntimeHost>(); runtimeHost = new Mock<IRuntimeHost>();
session = new Mock<ISessionData>(); session = new Mock<ISessionConfiguration>();
sessionContext = new SessionContext();
configuration.SetupGet(c => c.CurrentSession).Returns(session.Object); configuration.Setup(c => c.InitializeSessionConfiguration()).Returns(session.Object);
configuration.SetupGet(c => c.AppConfig).Returns(appConfig); session.SetupGet(s => s.AppConfig).Returns(appConfig);
sessionContext.Next = session.Object;
sut = new SessionInitializationOperation(configuration.Object, logger.Object, runtimeHost.Object); sut = new SessionInitializationOperation(configuration.Object, logger.Object, runtimeHost.Object, sessionContext);
} }
[TestMethod] [TestMethod]

View file

@ -91,5 +91,26 @@ namespace SafeExamBrowser.Runtime.UnitTests
// Assert.AreEqual(OperationResult.Aborted, result); // Assert.AreEqual(OperationResult.Aborted, result);
//} //}
//[TestMethod]
//public void MustNotWaitForPasswordViaClientIfCommunicationHasFailed()
//{
// var clientProxy = new Mock<IClientProxy>();
// var communication = new CommunicationResult(false);
// var session = new Mock<ISessionData>();
// var url = @"http://www.safeexambrowser.org/whatever.seb";
// clientProxy.Setup(c => c.RequestPassword(It.IsAny<PasswordRequestPurpose>(), It.IsAny<Guid>())).Returns(communication);
// repository.SetupGet(r => r.CurrentSession).Returns(session.Object);
// repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), null, null)).Returns(LoadStatus.SettingsPasswordNeeded);
// session.SetupGet(r => r.ClientProxy).Returns(clientProxy.Object);
// settings.KioskMode = KioskMode.CreateNewDesktop;
// sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, logger.Object, resourceLoader.Object, sessionContext);
// var result = sut.Perform();
// Assert.AreEqual(OperationResult.Aborted, result);
//}
} }
} }

View file

@ -79,10 +79,13 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Operations\ClientTerminationOperationTests.cs" />
<Compile Include="Operations\ConfigurationOperationTests.cs" /> <Compile Include="Operations\ConfigurationOperationTests.cs" />
<Compile Include="Operations\KioskModeOperationTests.cs" /> <Compile Include="Operations\KioskModeOperationTests.cs" />
<Compile Include="Operations\KioskModeTerminationOperationTests.cs" />
<Compile Include="Operations\ServiceOperationTests.cs" /> <Compile Include="Operations\ServiceOperationTests.cs" />
<Compile Include="Operations\ClientOperationTests.cs" /> <Compile Include="Operations\ClientOperationTests.cs" />
<Compile Include="Operations\SessionActivationOperationTests.cs" />
<Compile Include="Operations\SessionInitializationOperationTests.cs" /> <Compile Include="Operations\SessionInitializationOperationTests.cs" />
<Compile Include="RuntimeControllerTests.cs" /> <Compile Include="RuntimeControllerTests.cs" />
<Compile Include="Communication\RuntimeHostTests.cs" /> <Compile Include="Communication\RuntimeHostTests.cs" />

View file

@ -66,11 +66,11 @@ namespace SafeExamBrowser.Runtime.Operations
if (success) if (success)
{ {
logger.Info($"Successfully started new client instance."); logger.Info("Successfully started new client instance.");
} }
else else
{ {
logger.Error($"Failed to start new client instance! Aborting procedure..."); logger.Error("Failed to start new client instance! Aborting procedure...");
} }
return success ? OperationResult.Success : OperationResult.Failed; return success ? OperationResult.Success : OperationResult.Failed;

View file

@ -17,15 +17,33 @@ namespace SafeExamBrowser.Runtime.Operations
{ {
internal class KioskModeOperation : SessionOperation internal class KioskModeOperation : SessionOperation
{ {
protected IDesktopFactory desktopFactory; private IDesktopFactory desktopFactory;
protected IExplorerShell explorerShell; private IExplorerShell explorerShell;
private IProcessFactory processFactory;
protected ILogger logger; protected ILogger logger;
protected IProcessFactory processFactory;
private static IDesktop newDesktop; private IDesktop NewDesktop
private static IDesktop originalDesktop; {
get { return Context.NewDesktop; }
set { Context.NewDesktop = value; }
}
protected static KioskMode? ActiveMode { get; private set; } private IDesktop OriginalDesktop
{
get { return Context.OriginalDesktop; }
set { Context.OriginalDesktop = value; }
}
/// <summary>
/// TODO: This mechanism exposes the internal state of the operation! Find better solution which will keep the
/// state internal but still allow unit testing of both kiosk mode operations independently!
/// </summary>
protected KioskMode? ActiveMode
{
get { return Context.ActiveMode; }
set { Context.ActiveMode = value; }
}
public override event ActionRequiredEventHandler ActionRequired { add { } remove { } } public override event ActionRequiredEventHandler ActionRequired { add { } remove { } }
public override event StatusChangedEventHandler StatusChanged; public override event StatusChangedEventHandler StatusChanged;
@ -97,14 +115,14 @@ namespace SafeExamBrowser.Runtime.Operations
private void CreateNewDesktop() private void CreateNewDesktop()
{ {
originalDesktop = desktopFactory.GetCurrent(); OriginalDesktop = desktopFactory.GetCurrent();
logger.Info($"Current desktop is {originalDesktop}."); logger.Info($"Current desktop is {OriginalDesktop}.");
newDesktop = desktopFactory.CreateNew(nameof(SafeExamBrowser)); NewDesktop = desktopFactory.CreateNew(nameof(SafeExamBrowser));
logger.Info($"Created new desktop {newDesktop}."); logger.Info($"Created new desktop {NewDesktop}.");
newDesktop.Activate(); NewDesktop.Activate();
processFactory.StartupDesktop = newDesktop; processFactory.StartupDesktop = NewDesktop;
logger.Info("Successfully activated new desktop."); logger.Info("Successfully activated new desktop.");
explorerShell.Suspend(); explorerShell.Suspend();
@ -112,21 +130,21 @@ namespace SafeExamBrowser.Runtime.Operations
private void CloseNewDesktop() private void CloseNewDesktop()
{ {
if (originalDesktop != null) if (OriginalDesktop != null)
{ {
originalDesktop.Activate(); OriginalDesktop.Activate();
processFactory.StartupDesktop = originalDesktop; processFactory.StartupDesktop = OriginalDesktop;
logger.Info($"Switched back to original desktop {originalDesktop}."); logger.Info($"Switched back to original desktop {OriginalDesktop}.");
} }
else else
{ {
logger.Warn($"No original desktop found when attempting to close new desktop!"); logger.Warn($"No original desktop found when attempting to close new desktop!");
} }
if (newDesktop != null) if (NewDesktop != null)
{ {
newDesktop.Close(); NewDesktop.Close();
logger.Info($"Closed new desktop {newDesktop}."); logger.Info($"Closed new desktop {NewDesktop}.");
} }
else else
{ {

View file

@ -8,6 +8,7 @@
using SafeExamBrowser.Contracts.Communication.Proxies; using SafeExamBrowser.Contracts.Communication.Proxies;
using SafeExamBrowser.Contracts.Configuration; using SafeExamBrowser.Contracts.Configuration;
using SafeExamBrowser.Contracts.Configuration.Settings;
using SafeExamBrowser.Contracts.WindowsApi; using SafeExamBrowser.Contracts.WindowsApi;
namespace SafeExamBrowser.Runtime namespace SafeExamBrowser.Runtime
@ -17,6 +18,11 @@ namespace SafeExamBrowser.Runtime
/// </summary> /// </summary>
internal class SessionContext internal class SessionContext
{ {
/// <summary>
/// The currently active <see cref="KioskMode"/>.
/// </summary>
public KioskMode? ActiveMode { get; set; }
/// <summary> /// <summary>
/// The currently running client process. /// The currently running client process.
/// </summary> /// </summary>
@ -32,11 +38,21 @@ namespace SafeExamBrowser.Runtime
/// </summary> /// </summary>
public ISessionConfiguration Current { get; set; } public ISessionConfiguration Current { get; set; }
/// <summary>
/// The new desktop, if <see cref="KioskMode.CreateNewDesktop"/> is currently active.
/// </summary>
public IDesktop NewDesktop { get; set; }
/// <summary> /// <summary>
/// The configuration of the next session to be activated. /// The configuration of the next session to be activated.
/// </summary> /// </summary>
public ISessionConfiguration Next { get; set; } public ISessionConfiguration Next { get; set; }
/// <summary>
/// The original desktop, if <see cref="KioskMode.CreateNewDesktop"/> is currently active.
/// </summary>
public IDesktop OriginalDesktop { get; set; }
/// <summary> /// <summary>
/// The path of the configuration file to be used for reconfiguration. /// The path of the configuration file to be used for reconfiguration.
/// </summary> /// </summary>