diff --git a/SafeExamBrowser.Applications.UnitTests/ApplicationFactoryTests.cs b/SafeExamBrowser.Applications.UnitTests/ApplicationFactoryTests.cs index 08e5effd..f65f0352 100644 --- a/SafeExamBrowser.Applications.UnitTests/ApplicationFactoryTests.cs +++ b/SafeExamBrowser.Applications.UnitTests/ApplicationFactoryTests.cs @@ -60,13 +60,16 @@ namespace SafeExamBrowser.Applications.UnitTests [TestMethod] public void MustCorrectlyReadPathFromRegistry() { - var o = default(object); + object o = @"C:\Some\Registry\Path"; var settings = new WhitelistApplication { DisplayName = "Windows Command Prompt", ExecutableName = "cmd.exe", + ExecutablePath = @"C:\Some\Path" }; + registry.Setup(r => r.TryRead(It.Is(s => s.Contains(RegistryValue.MachineHive.AppPaths_Key)), It.Is(s => s == "Path"), out o)).Returns(true); + var result = sut.TryCreate(settings, out var application); registry.Verify(r => r.TryRead(It.Is(s => s.Contains(RegistryValue.MachineHive.AppPaths_Key)), It.Is(s => s == "Path"), out o), Times.Once); @@ -91,6 +94,22 @@ namespace SafeExamBrowser.Applications.UnitTests Assert.IsNull(application); } + [TestMethod] + public void MustFailGracefullyWhenPathIsInvalid() + { + var settings = new WhitelistApplication + { + ExecutableName = "asdfg(/ç)&=%\"fsdg..exe..", + ExecutablePath = "[]#°§¬#°¢@tu03450'w89tz!$£äöüèé:" + }; + + var result = sut.TryCreate(settings, out _); + + logger.Verify(l => l.Error(It.IsAny(), It.IsAny()), Times.AtLeastOnce); + + Assert.AreEqual(FactoryResult.NotFound, result); + } + [TestMethod] public void MustFailGracefullyAndIndicateThatErrorOccurred() { diff --git a/SafeExamBrowser.Applications.UnitTests/ExternalApplicationTests.cs b/SafeExamBrowser.Applications.UnitTests/ExternalApplicationTests.cs index 51e85bc8..25655679 100644 --- a/SafeExamBrowser.Applications.UnitTests/ExternalApplicationTests.cs +++ b/SafeExamBrowser.Applications.UnitTests/ExternalApplicationTests.cs @@ -51,10 +51,10 @@ namespace SafeExamBrowser.Applications.UnitTests [TestMethod] public void GetWindows_MustCorrectlyReturnOpenWindows() { - var sync = new AutoResetEvent(false); + var openWindows = new List { new IntPtr(123), new IntPtr(234), new IntPtr(456), new IntPtr(345), new IntPtr(567), new IntPtr(789), }; var process1 = new Mock(); var process2 = new Mock(); - var openWindows = new List { new IntPtr(123), new IntPtr(234), new IntPtr(456), new IntPtr(345), new IntPtr(567), new IntPtr(789), }; + var sync = new AutoResetEvent(false); nativeMethods.Setup(n => n.GetOpenWindows()).Returns(openWindows); nativeMethods.Setup(n => n.GetProcessIdFor(It.Is(p => p == new IntPtr(234)))).Returns(1234); @@ -88,6 +88,7 @@ namespace SafeExamBrowser.Applications.UnitTests public void Initialize_MustInitializeCorrectly() { settings.AutoStart = new Random().Next(2) == 1; + settings.Description = "Some Description"; sut.Initialize(); @@ -123,6 +124,40 @@ namespace SafeExamBrowser.Applications.UnitTests processFactory.Verify(f => f.StartNew(It.IsAny(), It.IsAny()), Times.Once); } + [TestMethod] + public void Start_MustRemoveInstanceCorrectlyWhenTerminated() + { + var eventRaised = false; + var openWindows = new List { new IntPtr(123), new IntPtr(234), new IntPtr(456), new IntPtr(345), new IntPtr(567), new IntPtr(789), }; + var process = new Mock(); + var sync = new AutoResetEvent(false); + + nativeMethods.Setup(n => n.GetOpenWindows()).Returns(openWindows); + nativeMethods.Setup(n => n.GetProcessIdFor(It.Is(p => p == new IntPtr(234)))).Returns(1234); + process.Setup(p => p.TryClose(It.IsAny())).Returns(false); + process.Setup(p => p.TryKill(It.IsAny())).Returns(true); + process.SetupGet(p => p.Id).Returns(1234); + processFactory.Setup(f => f.StartNew(It.IsAny(), It.IsAny())).Returns(process.Object); + + sut.WindowsChanged += () => + { + eventRaised = true; + sync.Set(); + }; + + sut.Initialize(); + sut.Start(); + + sync.WaitOne(); + + Assert.AreEqual(1, sut.GetWindows().Count()); + + process.Raise(p => p.Terminated += null, default(int)); + + Assert.IsTrue(eventRaised); + Assert.AreEqual(0, sut.GetWindows().Count()); + } + [TestMethod] public void Terminate_MustStopAllInstancesCorrectly() { diff --git a/SafeExamBrowser.Applications.UnitTests/ExternalApplicationWindowTests.cs b/SafeExamBrowser.Applications.UnitTests/ExternalApplicationWindowTests.cs new file mode 100644 index 00000000..5f903364 --- /dev/null +++ b/SafeExamBrowser.Applications.UnitTests/ExternalApplicationWindowTests.cs @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2023 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.Core.Contracts.Resources.Icons; +using SafeExamBrowser.WindowsApi.Contracts; + +namespace SafeExamBrowser.Applications.UnitTests +{ + [TestClass] + public class ExternalApplicationWindowTests + { + private IntPtr handle; + private NativeIconResource icon; + private Mock nativeMethods; + + private ExternalApplicationWindow sut; + + [TestInitialize] + public void Initialize() + { + handle = new IntPtr(123); + icon = new NativeIconResource(); + nativeMethods = new Mock(); + + sut = new ExternalApplicationWindow(icon, nativeMethods.Object, handle); + } + + [TestMethod] + public void Activate_MustCorrectlyActivateWindow() + { + sut.Activate(); + nativeMethods.Verify(n => n.ActivateWindow(It.Is(h => h == handle))); + } + + [TestMethod] + public void Update_MustCorrectlyUpdateWindow() + { + var iconChanged = false; + var titleChanged = false; + + nativeMethods.Setup(m => m.GetWindowIcon(It.IsAny())).Returns(new IntPtr(456)); + + sut.IconChanged += (_) => iconChanged = true; + sut.TitleChanged += (_) => titleChanged = true; + + sut.Update(); + + Assert.IsTrue(iconChanged); + Assert.IsTrue(titleChanged); + } + } +} diff --git a/SafeExamBrowser.Applications.UnitTests/SafeExamBrowser.Applications.UnitTests.csproj b/SafeExamBrowser.Applications.UnitTests/SafeExamBrowser.Applications.UnitTests.csproj index ad9cf941..9b91d040 100644 --- a/SafeExamBrowser.Applications.UnitTests/SafeExamBrowser.Applications.UnitTests.csproj +++ b/SafeExamBrowser.Applications.UnitTests/SafeExamBrowser.Applications.UnitTests.csproj @@ -84,6 +84,7 @@ +