SEBWIN-674: Extended unit tests for third-party application logic.
This commit is contained in:
parent
627c568400
commit
11b10e8e45
5 changed files with 87 additions and 5 deletions
|
@ -0,0 +1,62 @@
|
||||||
|
/*
|
||||||
|
* 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 Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
using Moq;
|
||||||
|
using SafeExamBrowser.Core.Contracts.Resources.Icons;
|
||||||
|
using SafeExamBrowser.Logging.Contracts;
|
||||||
|
using SafeExamBrowser.WindowsApi.Contracts;
|
||||||
|
|
||||||
|
namespace SafeExamBrowser.Applications.UnitTests
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class ExternalApplicationInstanceTests
|
||||||
|
{
|
||||||
|
private NativeIconResource icon;
|
||||||
|
private Mock<ILogger> logger;
|
||||||
|
private Mock<INativeMethods> nativeMethods;
|
||||||
|
private Mock<IProcess> process;
|
||||||
|
private ExternalApplicationInstance sut;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
icon = new NativeIconResource();
|
||||||
|
logger = new Mock<ILogger>();
|
||||||
|
nativeMethods = new Mock<INativeMethods>();
|
||||||
|
process = new Mock<IProcess>();
|
||||||
|
|
||||||
|
sut = new ExternalApplicationInstance(icon, logger.Object, nativeMethods.Object, process.Object, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Terminate_MustDoNothingIfAlreadyTerminated()
|
||||||
|
{
|
||||||
|
process.SetupGet(p => p.HasTerminated).Returns(true);
|
||||||
|
|
||||||
|
sut.Terminate();
|
||||||
|
|
||||||
|
process.Verify(p => p.TryClose(It.IsAny<int>()), Times.Never());
|
||||||
|
process.Verify(p => p.TryKill(It.IsAny<int>()), Times.Never());
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Terminate_MustLogIfTerminationFailed()
|
||||||
|
{
|
||||||
|
process.Setup(p => p.TryClose(It.IsAny<int>())).Returns(false);
|
||||||
|
process.Setup(p => p.TryKill(It.IsAny<int>())).Returns(false);
|
||||||
|
process.SetupGet(p => p.HasTerminated).Returns(false);
|
||||||
|
|
||||||
|
sut.Terminate();
|
||||||
|
|
||||||
|
logger.Verify(l => l.Warn(It.IsAny<string>()), Times.AtLeastOnce);
|
||||||
|
process.Verify(p => p.TryClose(It.IsAny<int>()), Times.AtLeastOnce());
|
||||||
|
process.Verify(p => p.TryKill(It.IsAny<int>()), Times.AtLeastOnce());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -82,6 +82,18 @@ namespace SafeExamBrowser.Applications.UnitTests
|
||||||
Assert.IsTrue(windows.Any(w => w.Handle == new IntPtr(234)));
|
Assert.IsTrue(windows.Any(w => w.Handle == new IntPtr(234)));
|
||||||
Assert.IsTrue(windows.Any(w => w.Handle == new IntPtr(345)));
|
Assert.IsTrue(windows.Any(w => w.Handle == new IntPtr(345)));
|
||||||
Assert.IsTrue(windows.Any(w => w.Handle == new IntPtr(567)));
|
Assert.IsTrue(windows.Any(w => w.Handle == new IntPtr(567)));
|
||||||
|
|
||||||
|
nativeMethods.Setup(n => n.GetOpenWindows()).Returns(openWindows.Take(4));
|
||||||
|
process2.Raise(p => p.Terminated += null, default(int));
|
||||||
|
|
||||||
|
sync.WaitOne();
|
||||||
|
|
||||||
|
windows = sut.GetWindows();
|
||||||
|
|
||||||
|
Assert.AreEqual(2, windows.Count());
|
||||||
|
Assert.IsTrue(windows.Any(w => w.Handle == new IntPtr(234)));
|
||||||
|
Assert.IsTrue(windows.Any(w => w.Handle == new IntPtr(345)));
|
||||||
|
Assert.IsTrue(windows.All(w => w.Handle != new IntPtr(567)));
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
|
@ -127,7 +139,7 @@ namespace SafeExamBrowser.Applications.UnitTests
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void Start_MustRemoveInstanceCorrectlyWhenTerminated()
|
public void Start_MustRemoveInstanceCorrectlyWhenTerminated()
|
||||||
{
|
{
|
||||||
var eventRaised = false;
|
var eventCount = 0;
|
||||||
var openWindows = new List<IntPtr> { new IntPtr(123), new IntPtr(234), new IntPtr(456), new IntPtr(345), new IntPtr(567), new IntPtr(789), };
|
var openWindows = new List<IntPtr> { new IntPtr(123), new IntPtr(234), new IntPtr(456), new IntPtr(345), new IntPtr(567), new IntPtr(789), };
|
||||||
var process = new Mock<IProcess>();
|
var process = new Mock<IProcess>();
|
||||||
var sync = new AutoResetEvent(false);
|
var sync = new AutoResetEvent(false);
|
||||||
|
@ -141,7 +153,7 @@ namespace SafeExamBrowser.Applications.UnitTests
|
||||||
|
|
||||||
sut.WindowsChanged += () =>
|
sut.WindowsChanged += () =>
|
||||||
{
|
{
|
||||||
eventRaised = true;
|
eventCount++;
|
||||||
sync.Set();
|
sync.Set();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -154,7 +166,7 @@ namespace SafeExamBrowser.Applications.UnitTests
|
||||||
|
|
||||||
process.Raise(p => p.Terminated += null, default(int));
|
process.Raise(p => p.Terminated += null, default(int));
|
||||||
|
|
||||||
Assert.IsTrue(eventRaised);
|
Assert.AreEqual(2, eventCount);
|
||||||
Assert.AreEqual(0, sut.GetWindows().Count());
|
Assert.AreEqual(0, sut.GetWindows().Count());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,12 +47,16 @@ namespace SafeExamBrowser.Applications.UnitTests
|
||||||
var titleChanged = false;
|
var titleChanged = false;
|
||||||
|
|
||||||
nativeMethods.Setup(m => m.GetWindowIcon(It.IsAny<IntPtr>())).Returns(new IntPtr(456));
|
nativeMethods.Setup(m => m.GetWindowIcon(It.IsAny<IntPtr>())).Returns(new IntPtr(456));
|
||||||
|
nativeMethods.Setup(m => m.GetWindowTitle((It.IsAny<IntPtr>()))).Returns("Some New Window Title");
|
||||||
|
|
||||||
sut.IconChanged += (_) => iconChanged = true;
|
sut.IconChanged += (_) => iconChanged = true;
|
||||||
sut.TitleChanged += (_) => titleChanged = true;
|
sut.TitleChanged += (_) => titleChanged = true;
|
||||||
|
|
||||||
sut.Update();
|
sut.Update();
|
||||||
|
|
||||||
|
nativeMethods.Verify(m => m.GetWindowIcon(handle), Times.Once);
|
||||||
|
nativeMethods.Verify(m => m.GetWindowTitle(handle), Times.Once);
|
||||||
|
|
||||||
Assert.IsTrue(iconChanged);
|
Assert.IsTrue(iconChanged);
|
||||||
Assert.IsTrue(titleChanged);
|
Assert.IsTrue(titleChanged);
|
||||||
}
|
}
|
||||||
|
|
|
@ -85,6 +85,7 @@
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="ExternalApplicationTests.cs" />
|
<Compile Include="ExternalApplicationTests.cs" />
|
||||||
<Compile Include="ExternalApplicationWindowTests.cs" />
|
<Compile Include="ExternalApplicationWindowTests.cs" />
|
||||||
|
<Compile Include="ExternalApplicationInstanceTests.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="app.config" />
|
<None Include="app.config" />
|
||||||
|
|
|
@ -162,8 +162,11 @@ namespace SafeExamBrowser.Applications
|
||||||
|
|
||||||
private void FinalizeEvents()
|
private void FinalizeEvents()
|
||||||
{
|
{
|
||||||
timer.Elapsed -= Timer_Elapsed;
|
if (timer != default)
|
||||||
timer.Stop();
|
{
|
||||||
|
timer.Elapsed -= Timer_Elapsed;
|
||||||
|
timer.Stop();
|
||||||
|
}
|
||||||
|
|
||||||
process.Terminated -= Process_Terminated;
|
process.Terminated -= Process_Terminated;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue