/* * Copyright (c) 2024 ETH Zürich, IT Services * * 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.Applications.Contracts; using SafeExamBrowser.Browser.Contracts; using SafeExamBrowser.Client.Operations; using SafeExamBrowser.Logging.Contracts; using SafeExamBrowser.Settings; using SafeExamBrowser.UserInterface.Contracts; using SafeExamBrowser.UserInterface.Contracts.Shell; namespace SafeExamBrowser.Client.UnitTests.Operations { [TestClass] public class BrowserOperationTests { private Mock actionCenter; private Mock browser; private ClientContext context; private Mock logger; private AppSettings settings; private Mock taskbar; private Mock taskview; private Mock uiFactory; private BrowserOperation sut; [TestInitialize] public void Initialize() { actionCenter = new Mock(); browser = new Mock(); context = new ClientContext(); logger = new Mock(); settings = new AppSettings(); taskbar = new Mock(); taskview = new Mock(); uiFactory = new Mock(); context.Browser = browser.Object; context.Settings = settings; sut = new BrowserOperation(actionCenter.Object, context, logger.Object, taskbar.Object, taskview.Object, uiFactory.Object); } [TestMethod] public void Perform_MustInitializeBrowserAndTaskview() { settings.Browser.EnableBrowser = true; sut.Perform(); browser.Verify(c => c.Initialize(), Times.Once); taskview.Verify(t => t.Add(It.Is>(a => a == context.Browser)), Times.Once); } [TestMethod] public void Perform_MustNotInitializeBrowserIfNotEnabled() { settings.Browser.EnableBrowser = false; settings.UserInterface.ActionCenter.EnableActionCenter = true; settings.UserInterface.Taskbar.EnableTaskbar = true; sut.Perform(); actionCenter.Verify(a => a.AddApplicationControl(It.IsAny(), true), Times.Never); browser.Verify(c => c.Initialize(), Times.Never); taskbar.Verify(t => t.AddApplicationControl(It.IsAny(), true), Times.Never); taskview.Verify(t => t.Add(It.Is>(a => a == context.Browser)), Times.Never); } [TestMethod] public void Perform_MustCorrectlyInitializeControls() { settings.Browser.EnableBrowser = true; settings.UserInterface.ActionCenter.EnableActionCenter = false; settings.UserInterface.Taskbar.EnableTaskbar = false; sut.Perform(); actionCenter.Verify(a => a.AddApplicationControl(It.IsAny(), true), Times.Never); taskbar.Verify(t => t.AddApplicationControl(It.IsAny(), true), Times.Never); settings.UserInterface.ActionCenter.EnableActionCenter = true; settings.UserInterface.Taskbar.EnableTaskbar = true; sut.Perform(); actionCenter.Verify(a => a.AddApplicationControl(It.IsAny(), true), Times.Once); taskbar.Verify(t => t.AddApplicationControl(It.IsAny(), true), Times.Once); } [TestMethod] public void Revert_MustTerminateBrowser() { settings.Browser.EnableBrowser = true; sut.Revert(); browser.Verify(c => c.Terminate(), Times.Once); } [TestMethod] public void Revert_MustNotTerminateBrowserIfNotEnabled() { settings.Browser.EnableBrowser = false; sut.Revert(); browser.Verify(c => c.Terminate(), Times.Never); } } }