seb-win-refactoring/SafeExamBrowser.Runtime.UnitTests/Operations/ClientTerminationOperationTests.cs

144 lines
4.4 KiB
C#

/*
* 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 AppConfig appConfig;
private Mock<IConfigurationRepository> configuration;
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<ISessionData> session;
private ClientTerminationOperation sut;
[TestInitialize]
public void Initialize()
{
appConfig = new AppConfig();
configuration = new Mock<IConfigurationRepository>();
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<ISessionData>();
configuration.SetupGet(c => c.CurrentSession).Returns(session.Object);
configuration.SetupGet(c => c.AppConfig).Returns(appConfig);
proxyFactory.Setup(f => f.CreateClientProxy(It.IsAny<string>())).Returns(proxy.Object);
session.SetupGet(s => s.ClientProxy).Returns(proxy.Object);
sut = new ClientTerminationOperation(configuration.Object, logger.Object, processFactory.Object, proxyFactory.Object, runtimeHost.Object, 0);
}
[TestMethod]
public void MustDoNothingOnPerform()
{
sut.Perform();
process.VerifyNoOtherCalls();
processFactory.VerifyNoOtherCalls();
proxy.VerifyNoOtherCalls();
proxyFactory.VerifyNoOtherCalls();
runtimeHost.VerifyNoOtherCalls();
}
[TestMethod]
public void MustDoNothingOnRevert()
{
sut.Revert();
process.VerifyNoOtherCalls();
processFactory.VerifyNoOtherCalls();
proxy.VerifyNoOtherCalls();
proxyFactory.VerifyNoOtherCalls();
runtimeHost.VerifyNoOtherCalls();
}
[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);
}
}
}