SEBWIN-296: Fixed unit tests for client controller.

This commit is contained in:
dbuechel 2019-02-12 13:57:22 +01:00
parent 1013d1e6bc
commit 0ccb10ca88

View file

@ -6,171 +6,200 @@
* 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 Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
//using Moq; using Moq;
//using SafeExamBrowser.Client.Behaviour; using SafeExamBrowser.Contracts.Browser;
//using SafeExamBrowser.Contracts.Core.OperationModel; using SafeExamBrowser.Contracts.Communication.Hosts;
//using SafeExamBrowser.Contracts.Communication.Proxies; using SafeExamBrowser.Contracts.Communication.Proxies;
//using SafeExamBrowser.Contracts.Logging; using SafeExamBrowser.Contracts.Configuration;
//using SafeExamBrowser.Contracts.Monitoring; using SafeExamBrowser.Contracts.Configuration.Cryptography;
//using SafeExamBrowser.Contracts.UserInterface; using SafeExamBrowser.Contracts.Configuration.Settings;
//using SafeExamBrowser.Contracts.UserInterface.Taskbar; using SafeExamBrowser.Contracts.Core.OperationModel;
using SafeExamBrowser.Contracts.I18n;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.Monitoring;
using SafeExamBrowser.Contracts.UserInterface;
using SafeExamBrowser.Contracts.UserInterface.MessageBox;
using SafeExamBrowser.Contracts.UserInterface.Taskbar;
using SafeExamBrowser.Contracts.UserInterface.Windows;
using SafeExamBrowser.Contracts.WindowsApi;
namespace SafeExamBrowser.Client.UnitTests namespace SafeExamBrowser.Client.UnitTests
{ {
[TestClass] [TestClass]
public class ClientControllerTests public class ClientControllerTests
{ {
//private Mock<IDisplayMonitor> displayMonitorMock; private AppConfig appConfig;
//private Mock<ILogger> loggerMock; private Mock<IBrowserApplicationController> browserController;
//private Mock<IProcessMonitor> processMonitorMock; private Mock<IClientHost> clientHost;
//private Mock<IOperationSequence> operationSequenceMock; private Mock<IDisplayMonitor> displayMonitor;
//private Mock<IRuntimeProxy> runtimeProxyMock; private Mock<IExplorerShell> explorerShell;
//private Mock<ITaskbar> taskbarMock; private Mock<IHashAlgorithm> hashAlgorithm;
//private Mock<IUserInterfaceFactory> uiFactoryMock; private Mock<ILogger> logger;
//private Mock<IWindowMonitor> windowMonitorMock; private Mock<IMessageBox> messageBox;
private Mock<IProcessMonitor> processMonitor;
private Mock<IOperationSequence> operationSequence;
private Mock<IRuntimeProxy> runtimeProxy;
private Guid sessionId;
private Settings settings;
private Mock<Action> shutdown;
private Mock<ITaskbar> taskbar;
private Mock<IText> text;
private Mock<IUserInterfaceFactory> uiFactory;
private Mock<IWindowMonitor> windowMonitor;
//private ClientController sut; private ClientController sut;
[TestMethod] [TestInitialize]
public void TODO() public void Initialize()
{ {
Assert.Fail(); appConfig = new AppConfig();
browserController = new Mock<IBrowserApplicationController>();
clientHost = new Mock<IClientHost>();
displayMonitor = new Mock<IDisplayMonitor>();
explorerShell = new Mock<IExplorerShell>();
hashAlgorithm = new Mock<IHashAlgorithm>();
logger = new Mock<ILogger>();
messageBox = new Mock<IMessageBox>();
processMonitor = new Mock<IProcessMonitor>();
operationSequence = new Mock<IOperationSequence>();
runtimeProxy = new Mock<IRuntimeProxy>();
sessionId = Guid.NewGuid();
settings = new Settings();
shutdown = new Mock<Action>();
taskbar = new Mock<ITaskbar>();
text = new Mock<IText>();
uiFactory = new Mock<IUserInterfaceFactory>();
windowMonitor = new Mock<IWindowMonitor>();
operationSequence.Setup(o => o.TryPerform()).Returns(OperationResult.Success);
runtimeProxy.Setup(r => r.InformClientReady()).Returns(new CommunicationResult(true));
uiFactory.Setup(u => u.CreateSplashScreen(It.IsAny<AppConfig>())).Returns(new Mock<ISplashScreen>().Object);
sut = new ClientController(
displayMonitor.Object,
explorerShell.Object,
hashAlgorithm.Object,
logger.Object,
messageBox.Object,
operationSequence.Object,
processMonitor.Object,
runtimeProxy.Object,
shutdown.Object,
taskbar.Object,
text.Object,
uiFactory.Object,
windowMonitor.Object);
sut.AppConfig = appConfig;
sut.Browser = browserController.Object;
sut.ClientHost = clientHost.Object;
sut.SessionId = sessionId;
sut.Settings = settings;
sut.TryStart();
} }
//[TestInitialize] [TestMethod]
//public void Initialize() public void MustHandleDisplayChangeCorrectly()
//{ {
// displayMonitorMock = new Mock<IDisplayMonitor>(); var order = 0;
// loggerMock = new Mock<ILogger>(); var workingArea = 0;
// processMonitorMock = new Mock<IProcessMonitor>(); var taskbar = 0;
// operationSequenceMock = new Mock<IOperationSequence>();
// runtimeProxyMock = new Mock<IRuntimeProxy>();
// taskbarMock = new Mock<ITaskbar>();
// uiFactoryMock = new Mock<IUserInterfaceFactory>();
// windowMonitorMock= new Mock<IWindowMonitor>();
// operationSequenceMock.Setup(o => o.TryPerform()).Returns(true); displayMonitor.Setup(w => w.InitializePrimaryDisplay(this.taskbar.Object.GetAbsoluteHeight())).Callback(() => workingArea = ++order);
this.taskbar.Setup(t => t.InitializeBounds()).Callback(() => taskbar = ++order);
// sut = new ClientController( displayMonitor.Raise(d => d.DisplayChanged += null);
// displayMonitorMock.Object,
// loggerMock.Object,
// operationSequenceMock.Object,
// processMonitorMock.Object,
// runtimeProxyMock.Object,
// new Action(() => { }),
// taskbarMock.Object,
// uiFactoryMock.Object,
// windowMonitorMock.Object);
// sut.TryStart(); displayMonitor.Verify(w => w.InitializePrimaryDisplay(this.taskbar.Object.GetAbsoluteHeight()), Times.Once);
//} this.taskbar.Verify(t => t.InitializeBounds(), Times.Once);
//[TestMethod] Assert.IsTrue(workingArea == 1);
//public void MustHandleDisplayChangeCorrectly() Assert.IsTrue(taskbar == 2);
//{ }
// var order = 0;
// var workingArea = 0;
// var taskbar = 0;
// displayMonitorMock.Setup(w => w.InitializePrimaryDisplay(taskbarMock.Object.GetAbsoluteHeight())).Callback(() => workingArea = ++order); [TestMethod]
// taskbarMock.Setup(t => t.InitializeBounds()).Callback(() => taskbar = ++order); public void MustHandleExplorerStartCorrectly()
{
var order = 0;
var shell = 0;
var workingArea = 0;
var bounds = 0;
// displayMonitorMock.Raise(d => d.DisplayChanged += null); explorerShell.Setup(e => e.Terminate()).Callback(() => shell = ++order);
displayMonitor.Setup(w => w.InitializePrimaryDisplay(taskbar.Object.GetAbsoluteHeight())).Callback(() => workingArea = ++order);
taskbar.Setup(t => t.InitializeBounds()).Callback(() => bounds = ++order);
// displayMonitorMock.Verify(w => w.InitializePrimaryDisplay(taskbarMock.Object.GetAbsoluteHeight()), Times.Once); processMonitor.Raise(p => p.ExplorerStarted += null);
// taskbarMock.Verify(t => t.InitializeBounds(), Times.Once);
// Assert.IsTrue(workingArea == 1); explorerShell.Verify(p => p.Terminate(), Times.Once);
// Assert.IsTrue(taskbar == 2); displayMonitor.Verify(w => w.InitializePrimaryDisplay(taskbar.Object.GetAbsoluteHeight()), Times.Once);
//} taskbar.Verify(t => t.InitializeBounds(), Times.Once);
//[TestMethod] Assert.IsTrue(shell == 1);
//public void MustHandleExplorerStartCorrectly() Assert.IsTrue(workingArea == 2);
//{ Assert.IsTrue(bounds == 3);
// var order = 0; }
// var processManager = 0;
// var workingArea = 0;
// var taskbar = 0;
// processMonitorMock.Setup(p => p.CloseExplorerShell()).Callback(() => processManager = ++order); [TestMethod]
// displayMonitorMock.Setup(w => w.InitializePrimaryDisplay(taskbarMock.Object.GetAbsoluteHeight())).Callback(() => workingArea = ++order); public void MustHandleAllowedWindowChangeCorrectly()
// taskbarMock.Setup(t => t.InitializeBounds()).Callback(() => taskbar = ++order); {
var window = new IntPtr(12345);
// processMonitorMock.Raise(p => p.ExplorerStarted += null); processMonitor.Setup(p => p.BelongsToAllowedProcess(window)).Returns(true);
// processMonitorMock.Verify(p => p.CloseExplorerShell(), Times.Once); windowMonitor.Raise(w => w.WindowChanged += null, window);
// displayMonitorMock.Verify(w => w.InitializePrimaryDisplay(taskbarMock.Object.GetAbsoluteHeight()), Times.Once);
// taskbarMock.Verify(t => t.InitializeBounds(), Times.Once);
// Assert.IsTrue(processManager == 1); processMonitor.Verify(p => p.BelongsToAllowedProcess(window), Times.Once);
// Assert.IsTrue(workingArea == 2); windowMonitor.Verify(w => w.Hide(window), Times.Never);
// Assert.IsTrue(taskbar == 3); windowMonitor.Verify(w => w.Close(window), Times.Never);
//} }
//[TestMethod] [TestMethod]
//public void MustHandleAllowedWindowChangeCorrectly() public void MustHandleUnallowedWindowHideCorrectly()
//{ {
// var window = new IntPtr(12345); var order = 0;
var belongs = 0;
var hide = 0;
var window = new IntPtr(12345);
// processMonitorMock.Setup(p => p.BelongsToAllowedProcess(window)).Returns(true); processMonitor.Setup(p => p.BelongsToAllowedProcess(window)).Returns(false).Callback(() => belongs = ++order);
windowMonitor.Setup(w => w.Hide(window)).Returns(true).Callback(() => hide = ++order);
// windowMonitorMock.Raise(w => w.WindowChanged += null, window); windowMonitor.Raise(w => w.WindowChanged += null, window);
// processMonitorMock.Verify(p => p.BelongsToAllowedProcess(window), Times.Once); processMonitor.Verify(p => p.BelongsToAllowedProcess(window), Times.Once);
// windowMonitorMock.Verify(w => w.Hide(window), Times.Never); windowMonitor.Verify(w => w.Hide(window), Times.Once);
// windowMonitorMock.Verify(w => w.Close(window), Times.Never); windowMonitor.Verify(w => w.Close(window), Times.Never);
//}
//[TestMethod] Assert.IsTrue(belongs == 1);
//public void MustHandleUnallowedWindowHideCorrectly() Assert.IsTrue(hide == 2);
//{ }
// var order = 0;
// var belongs = 0;
// var hide = 0;
// var window = new IntPtr(12345);
// processMonitorMock.Setup(p => p.BelongsToAllowedProcess(window)).Returns(false).Callback(() => belongs = ++order); [TestMethod]
// windowMonitorMock.Setup(w => w.Hide(window)).Returns(true).Callback(() => hide = ++order); public void MustHandleUnallowedWindowCloseCorrectly()
{
var order = 0;
var belongs = 0;
var hide = 0;
var close = 0;
var window = new IntPtr(12345);
// windowMonitorMock.Raise(w => w.WindowChanged += null, window); processMonitor.Setup(p => p.BelongsToAllowedProcess(window)).Returns(false).Callback(() => belongs = ++order);
windowMonitor.Setup(w => w.Hide(window)).Returns(false).Callback(() => hide = ++order);
windowMonitor.Setup(w => w.Close(window)).Callback(() => close = ++order);
// processMonitorMock.Verify(p => p.BelongsToAllowedProcess(window), Times.Once); windowMonitor.Raise(w => w.WindowChanged += null, window);
// windowMonitorMock.Verify(w => w.Hide(window), Times.Once);
// windowMonitorMock.Verify(w => w.Close(window), Times.Never);
// Assert.IsTrue(belongs == 1); processMonitor.Verify(p => p.BelongsToAllowedProcess(window), Times.Once);
// Assert.IsTrue(hide == 2); windowMonitor.Verify(w => w.Hide(window), Times.Once);
//} windowMonitor.Verify(w => w.Close(window), Times.Once);
//[TestMethod] Assert.IsTrue(belongs == 1);
//public void MustHandleUnallowedWindowCloseCorrectly() Assert.IsTrue(hide == 2);
//{ Assert.IsTrue(close == 3);
// var order = 0; }
// var belongs = 0;
// var hide = 0;
// var close = 0;
// var window = new IntPtr(12345);
// processMonitorMock.Setup(p => p.BelongsToAllowedProcess(window)).Returns(false).Callback(() => belongs = ++order);
// windowMonitorMock.Setup(w => w.Hide(window)).Returns(false).Callback(() => hide = ++order);
// windowMonitorMock.Setup(w => w.Close(window)).Callback(() => close = ++order);
// windowMonitorMock.Raise(w => w.WindowChanged += null, window);
// processMonitorMock.Verify(p => p.BelongsToAllowedProcess(window), Times.Once);
// windowMonitorMock.Verify(w => w.Hide(window), Times.Once);
// windowMonitorMock.Verify(w => w.Close(window), Times.Once);
// Assert.IsTrue(belongs == 1);
// Assert.IsTrue(hide == 2);
// Assert.IsTrue(close == 3);
//}
//[TestCleanup]
//public void Cleanup()
//{
// // sut.Stop();
//}
} }
} }