/* * Copyright (c) 2021 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.Applications.Contracts; using SafeExamBrowser.Client.Operations; using SafeExamBrowser.Core.Contracts.Notifications; using SafeExamBrowser.I18n.Contracts; using SafeExamBrowser.Logging.Contracts; using SafeExamBrowser.Settings; using SafeExamBrowser.Settings.Applications; using SafeExamBrowser.SystemComponents.Contracts; using SafeExamBrowser.SystemComponents.Contracts.Audio; using SafeExamBrowser.SystemComponents.Contracts.Keyboard; using SafeExamBrowser.SystemComponents.Contracts.PowerSupply; using SafeExamBrowser.SystemComponents.Contracts.WirelessNetwork; using SafeExamBrowser.UserInterface.Contracts; using SafeExamBrowser.UserInterface.Contracts.Shell; namespace SafeExamBrowser.Client.UnitTests.Operations { [TestClass] public class ShellOperationTests { private Mock actionCenter; private Mock audio; private ClientContext context; private Mock logger; private Mock aboutNotification; private Mock keyboard; private Mock logNotification; private Mock powerSupply; private Mock systemInfo; private Mock taskbar; private Mock taskview; private Mock text; private Mock uiFactory; private Mock wirelessAdapter; private ShellOperation sut; [TestInitialize] public void Initialize() { actionCenter = new Mock(); audio = new Mock(); context = new ClientContext(); logger = new Mock(); aboutNotification = new Mock(); keyboard = new Mock(); logNotification = new Mock(); powerSupply = new Mock(); systemInfo = new Mock(); taskbar = new Mock(); taskview = new Mock(); text = new Mock(); uiFactory = new Mock(); wirelessAdapter = new Mock(); context.Settings = new AppSettings(); uiFactory .Setup(u => u.CreateNotificationControl(It.IsAny(), It.IsAny())) .Returns(new Mock().Object); sut = new ShellOperation( actionCenter.Object, audio.Object, aboutNotification.Object, context, keyboard.Object, logger.Object, logNotification.Object, powerSupply.Object, systemInfo.Object, taskbar.Object, taskview.Object, text.Object, uiFactory.Object, wirelessAdapter.Object); } [TestMethod] public void Perform_MustInitializeActivators() { var actionCenterActivator = new Mock(); var taskviewActivator = new Mock(); var terminationActivator = new Mock(); context.Activators.Add(actionCenterActivator.Object); context.Activators.Add(taskviewActivator.Object); context.Activators.Add(terminationActivator.Object); context.Settings.ActionCenter.EnableActionCenter = true; context.Settings.Keyboard.AllowAltTab = true; context.Settings.Security.AllowTermination = true; sut.Perform(); actionCenter.Verify(a => a.Register(It.Is(a2 => a2 == actionCenterActivator.Object)), Times.Once); actionCenterActivator.Verify(a => a.Start(), Times.Once); taskview.Verify(t => t.Register(It.Is(a => a == taskviewActivator.Object)), Times.Once); taskviewActivator.Verify(a => a.Start(), Times.Once); terminationActivator.Verify(a => a.Start(), Times.Once); } [TestMethod] public void Perform_MustInitializeApplications() { var application1 = new Mock(); var application1Settings = new WhitelistApplication { ShowInShell = true }; var application2 = new Mock(); var application2Settings = new WhitelistApplication { ShowInShell = false }; var application3 = new Mock(); var application3Settings = new WhitelistApplication { ShowInShell = true }; application1.SetupGet(a => a.Id).Returns(application1Settings.Id); application2.SetupGet(a => a.Id).Returns(application2Settings.Id); application3.SetupGet(a => a.Id).Returns(application3Settings.Id); context.Applications.Add(application1.Object); context.Applications.Add(application2.Object); context.Applications.Add(application3.Object); context.Settings.ActionCenter.EnableActionCenter = true; context.Settings.Taskbar.EnableTaskbar = true; context.Settings.Applications.Whitelist.Add(application1Settings); context.Settings.Applications.Whitelist.Add(application2Settings); context.Settings.Applications.Whitelist.Add(application3Settings); sut.Perform(); actionCenter.Verify(a => a.AddApplicationControl(It.IsAny(), false), Times.Exactly(2)); taskbar.Verify(t => t.AddApplicationControl(It.IsAny(), false), Times.Exactly(2)); taskview.Verify(t => t.Add(It.Is(a => a == application1.Object)), Times.Once); taskview.Verify(t => t.Add(It.Is(a => a == application2.Object)), Times.Once); taskview.Verify(t => t.Add(It.Is(a => a == application3.Object)), Times.Once); uiFactory.Verify(f => f.CreateApplicationControl(It.Is(a => a == application1.Object), Location.ActionCenter), Times.Once); uiFactory.Verify(f => f.CreateApplicationControl(It.Is(a => a == application1.Object), Location.Taskbar), Times.Once); uiFactory.Verify(f => f.CreateApplicationControl(It.Is(a => a == application2.Object), Location.ActionCenter), Times.Never); uiFactory.Verify(f => f.CreateApplicationControl(It.Is(a => a == application2.Object), Location.Taskbar), Times.Never); uiFactory.Verify(f => f.CreateApplicationControl(It.Is(a => a == application3.Object), Location.ActionCenter), Times.Once); uiFactory.Verify(f => f.CreateApplicationControl(It.Is(a => a == application3.Object), Location.Taskbar), Times.Once); } [TestMethod] public void Perform_MustNotAddApplicationsToShellIfNotEnabled() { var application1 = new Mock(); var application1Settings = new WhitelistApplication { ShowInShell = true }; var application2 = new Mock(); var application2Settings = new WhitelistApplication { ShowInShell = true }; var application3 = new Mock(); var application3Settings = new WhitelistApplication { ShowInShell = true }; application1.SetupGet(a => a.Id).Returns(application1Settings.Id); application2.SetupGet(a => a.Id).Returns(application2Settings.Id); application3.SetupGet(a => a.Id).Returns(application3Settings.Id); context.Applications.Add(application1.Object); context.Applications.Add(application2.Object); context.Applications.Add(application3.Object); context.Settings.ActionCenter.EnableActionCenter = false; context.Settings.Taskbar.EnableTaskbar = false; context.Settings.Applications.Whitelist.Add(application1Settings); context.Settings.Applications.Whitelist.Add(application2Settings); context.Settings.Applications.Whitelist.Add(application3Settings); sut.Perform(); actionCenter.Verify(a => a.AddApplicationControl(It.IsAny(), false), Times.Never); taskbar.Verify(t => t.AddApplicationControl(It.IsAny(), false), Times.Never); taskview.Verify(t => t.Add(It.Is(a => a == application1.Object)), Times.Once); taskview.Verify(t => t.Add(It.Is(a => a == application2.Object)), Times.Once); taskview.Verify(t => t.Add(It.Is(a => a == application3.Object)), Times.Once); uiFactory.Verify(f => f.CreateApplicationControl(It.Is(a => a == application1.Object), Location.ActionCenter), Times.Never); uiFactory.Verify(f => f.CreateApplicationControl(It.Is(a => a == application1.Object), Location.Taskbar), Times.Never); uiFactory.Verify(f => f.CreateApplicationControl(It.Is(a => a == application2.Object), Location.ActionCenter), Times.Never); uiFactory.Verify(f => f.CreateApplicationControl(It.Is(a => a == application2.Object), Location.Taskbar), Times.Never); uiFactory.Verify(f => f.CreateApplicationControl(It.Is(a => a == application3.Object), Location.ActionCenter), Times.Never); uiFactory.Verify(f => f.CreateApplicationControl(It.Is(a => a == application3.Object), Location.Taskbar), Times.Never); } [TestMethod] public void Perform_MustInitializeClock() { context.Settings.ActionCenter.EnableActionCenter = true; context.Settings.ActionCenter.ShowClock = true; context.Settings.Taskbar.EnableTaskbar = true; context.Settings.Taskbar.ShowClock = true; sut.Perform(); actionCenter.VerifySet(a => a.ShowClock = true, Times.Once); taskbar.VerifySet(t => t.ShowClock = true, Times.Once); } [TestMethod] public void Perform_MustNotInitializeClock() { context.Settings.ActionCenter.EnableActionCenter = true; context.Settings.ActionCenter.ShowClock = false; context.Settings.Taskbar.EnableTaskbar = true; context.Settings.Taskbar.ShowClock = false; sut.Perform(); actionCenter.VerifySet(a => a.ShowClock = false, Times.Once); taskbar.VerifySet(t => t.ShowClock = false, Times.Once); } [TestMethod] public void Perform_MustInitializeQuitButton() { context.Settings.ActionCenter.EnableActionCenter = true; context.Settings.Taskbar.EnableTaskbar = true; context.Settings.Security.AllowTermination = false; sut.Perform(); actionCenter.VerifySet(a => a.ShowQuitButton = false, Times.Once); taskbar.VerifySet(t => t.ShowQuitButton = false, Times.Once); actionCenter.VerifySet(a => a.ShowQuitButton = true, Times.Never); taskbar.VerifySet(t => t.ShowQuitButton = true, Times.Never); actionCenter.Reset(); taskbar.Reset(); context.Settings.Security.AllowTermination = true; sut.Perform(); actionCenter.VerifySet(a => a.ShowQuitButton = false, Times.Never); taskbar.VerifySet(t => t.ShowQuitButton = false, Times.Never); actionCenter.VerifySet(a => a.ShowQuitButton = true, Times.Once); taskbar.VerifySet(t => t.ShowQuitButton = true, Times.Once); } [TestMethod] public void Perform_MustInitializeNotifications() { context.Settings.ActionCenter.EnableActionCenter = true; context.Settings.ActionCenter.ShowApplicationInfo = true; context.Settings.ActionCenter.ShowApplicationLog = true; context.Settings.Taskbar.EnableTaskbar = true; context.Settings.Taskbar.ShowApplicationInfo = true; context.Settings.Taskbar.ShowApplicationLog = true; sut.Perform(); actionCenter.Verify(a => a.AddNotificationControl(It.IsAny()), Times.AtLeast(2)); taskbar.Verify(t => t.AddNotificationControl(It.IsAny()), Times.AtLeast(2)); } [TestMethod] public void Perform_MustNotInitializeNotifications() { var logControl = new Mock(); context.Settings.ActionCenter.EnableActionCenter = true; context.Settings.ActionCenter.ShowApplicationLog = false; context.Settings.Taskbar.EnableTaskbar = true; context.Settings.Taskbar.ShowApplicationLog = false; uiFactory .Setup(f => f.CreateNotificationControl(It.IsAny(), It.IsAny())) .Returns(logControl.Object); sut.Perform(); actionCenter.Verify(a => a.AddNotificationControl(It.Is(i => i == logControl.Object)), Times.Never); taskbar.Verify(t => t.AddNotificationControl(It.Is(i => i == logControl.Object)), Times.Never); } [TestMethod] public void Perform_MustInitializeSystemComponents() { context.Settings.ActionCenter.EnableActionCenter = true; context.Settings.ActionCenter.ShowAudio = true; context.Settings.ActionCenter.ShowKeyboardLayout = true; context.Settings.ActionCenter.ShowWirelessNetwork = true; context.Settings.Taskbar.EnableTaskbar = true; context.Settings.Taskbar.ShowAudio = true; context.Settings.Taskbar.ShowKeyboardLayout = true; context.Settings.Taskbar.ShowWirelessNetwork = true; systemInfo.SetupGet(s => s.HasBattery).Returns(true); uiFactory.Setup(f => f.CreateAudioControl(It.IsAny(), It.IsAny())).Returns(new Mock().Object); uiFactory.Setup(f => f.CreateKeyboardLayoutControl(It.IsAny(), It.IsAny())).Returns(new Mock().Object); uiFactory.Setup(f => f.CreatePowerSupplyControl(It.IsAny(), It.IsAny())).Returns(new Mock().Object); uiFactory.Setup(f => f.CreateWirelessNetworkControl(It.IsAny(), It.IsAny())).Returns(new Mock().Object); sut.Perform(); audio.Verify(a => a.Initialize(), Times.Once); powerSupply.Verify(p => p.Initialize(), Times.Once); wirelessAdapter.Verify(w => w.Initialize(), Times.Once); keyboard.Verify(k => k.Initialize(), Times.Once); actionCenter.Verify(a => a.AddSystemControl(It.IsAny()), Times.Exactly(4)); taskbar.Verify(t => t.AddSystemControl(It.IsAny()), Times.Exactly(4)); } [TestMethod] public void Perform_MustNotInitializeSystemComponents() { context.Settings.ActionCenter.EnableActionCenter = true; context.Settings.ActionCenter.ShowAudio = false; context.Settings.ActionCenter.ShowKeyboardLayout = false; context.Settings.ActionCenter.ShowWirelessNetwork = false; context.Settings.Taskbar.EnableTaskbar = true; context.Settings.Taskbar.ShowAudio = false; context.Settings.Taskbar.ShowKeyboardLayout = false; context.Settings.Taskbar.ShowWirelessNetwork = false; systemInfo.SetupGet(s => s.HasBattery).Returns(false); uiFactory.Setup(f => f.CreateAudioControl(It.IsAny(), It.IsAny())).Returns(new Mock().Object); uiFactory.Setup(f => f.CreateKeyboardLayoutControl(It.IsAny(), It.IsAny())).Returns(new Mock().Object); uiFactory.Setup(f => f.CreatePowerSupplyControl(It.IsAny(), It.IsAny())).Returns(new Mock().Object); uiFactory.Setup(f => f.CreateWirelessNetworkControl(It.IsAny(), It.IsAny())).Returns(new Mock().Object); sut.Perform(); audio.Verify(a => a.Initialize(), Times.Once); powerSupply.Verify(p => p.Initialize(), Times.Once); wirelessAdapter.Verify(w => w.Initialize(), Times.Once); keyboard.Verify(k => k.Initialize(), Times.Once); actionCenter.Verify(a => a.AddSystemControl(It.IsAny()), Times.Never); taskbar.Verify(t => t.AddSystemControl(It.IsAny()), Times.Never); } [TestMethod] public void Perform_MustNotInitializeActionCenterIfNotEnabled() { var actionCenterActivator = new Mock(); context.Activators.Add(actionCenterActivator.Object); context.Settings.ActionCenter.EnableActionCenter = false; sut.Perform(); actionCenter.VerifyNoOtherCalls(); actionCenterActivator.VerifyNoOtherCalls(); } [TestMethod] public void Perform_MustNotInitializeTaskviewActivatorIfNotEnabled() { var taskviewActivator = new Mock(); context.Activators.Add(taskviewActivator.Object); context.Settings.Keyboard.AllowAltTab = false; sut.Perform(); taskview.Verify(t => t.Register(It.IsAny()), Times.Never); taskviewActivator.VerifyNoOtherCalls(); } [TestMethod] public void Perform_MustNotInitializeTaskbarIfNotEnabled() { context.Settings.Taskbar.EnableTaskbar = false; sut.Perform(); taskbar.VerifyNoOtherCalls(); } [TestMethod] public void Perform_MustNotInitializeTerminationActivatorIfNotEnabled() { var terminationActivator = new Mock(); context.Activators.Add(terminationActivator.Object); context.Settings.Security.AllowTermination = false; sut.Perform(); terminationActivator.Verify(a => a.Start(), Times.Never); } [TestMethod] public void Revert_MustTerminateActivators() { var actionCenterActivator = new Mock(); var taskviewActivator = new Mock(); var terminationActivator = new Mock(); context.Activators.Add(actionCenterActivator.Object); context.Activators.Add(taskviewActivator.Object); context.Activators.Add(terminationActivator.Object); sut.Revert(); actionCenterActivator.Verify(a => a.Stop(), Times.Once); taskviewActivator.Verify(a => a.Stop(), Times.Once); terminationActivator.Verify(a => a.Stop(), Times.Once); } [TestMethod] public void Revert_MustTerminateControllers() { sut.Revert(); aboutNotification.Verify(c => c.Terminate(), Times.Once); audio.Verify(a => a.Terminate(), Times.Once); logNotification.Verify(c => c.Terminate(), Times.Once); powerSupply.Verify(p => p.Terminate(), Times.Once); keyboard.Verify(k => k.Terminate(), Times.Once); wirelessAdapter.Verify(w => w.Terminate(), Times.Once); } } }