-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathAppShutdownTests.cs
More file actions
133 lines (115 loc) · 5.43 KB
/
Copy pathAppShutdownTests.cs
File metadata and controls
133 lines (115 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// -----------------------------------------------------------------------------
// <copyright file="AppShutdownTests.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------------
namespace AppInstallerCLIE2ETests
{
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using AppInstallerCLIE2ETests.Helpers;
using NUnit.Framework;
/// <summary>
/// `test appshutdown` command tests.
/// </summary>
public class AppShutdownTests
{
/// <summary>
/// Runs winget test appshutdown and register the application to force a WM_QUERYENDSESSION message.
/// </summary>
[Test]
[Ignore("This test relied on a signal to terminate that was determined to be problematic. We may need OS fixes to test it when elevated.")]
public void RegisterApplicationTest()
{
if (!TestSetup.Parameters.PackagedContext)
{
Assert.Ignore("Not packaged context.");
}
if (!TestCommon.ExecutingAsAdministrator && TestCommon.IsCIEnvironment)
{
Assert.Ignore("This test won't work on Window Server as non-admin");
}
if (!Environment.Is64BitProcess)
{
// My guess is that HAM terminates us faster after the CTRL-C on x86...
Assert.Ignore("This test is flaky when run as x86.");
}
if (string.IsNullOrEmpty(TestSetup.Parameters.AICLIPackagePath))
{
throw new NullReferenceException("AICLIPackagePath");
}
var appxManifest = Path.Combine(TestSetup.Parameters.AICLIPackagePath, "AppxManifest.xml");
if (!File.Exists(appxManifest))
{
throw new FileNotFoundException(appxManifest);
}
// In order to registering the application we need a higher version number and pass the force app shutdown flag.
// Doing it the long way.
var xmlDoc = new XmlDocument();
XmlNamespaceManager namespaces = new XmlNamespaceManager(xmlDoc.NameTable);
namespaces.AddNamespace("n", "http://schemas.microsoft.com/appx/manifest/foundation/windows10");
xmlDoc.Load(appxManifest);
var identityNode = xmlDoc.SelectSingleNode("/n:Package/n:Identity", namespaces);
if (identityNode == null)
{
throw new NullReferenceException("Identity node");
}
var versionAttribute = identityNode.Attributes["Version"];
if (versionAttribute == null)
{
throw new NullReferenceException("Version attribute");
}
var ogVersion = new Version(versionAttribute.Value);
var newVersion = new Version(ogVersion.Major, ogVersion.Minor, ogVersion.Build, ogVersion.Revision + 1);
versionAttribute.Value = newVersion.ToString();
xmlDoc.Save(appxManifest);
// This just waits for the app termination event.
var testCmdTask = new Task<TestCommon.RunCommandResult>(() =>
{
return TestCommon.RunAICLICommand("test", "appshutdown --verbose", timeOut: 300000, throwOnTimeout: false);
});
// Register the app with the updated version.
var registerTask = new Task<bool>(() =>
{
return TestCommon.InstallMsixRegister(TestSetup.Parameters.AICLIPackagePath, true, false);
});
// Give it a little time.
testCmdTask.Start();
Thread.Sleep(30000);
registerTask.Start();
Task.WaitAll(new Task[] { testCmdTask, registerTask }, 360000);
// The ctrl-c command terminates the batch file before the exit code file gets created.
// Look for the output.
Assert.That(testCmdTask.Result.StdOut.Contains("Succeeded waiting for app shutdown event"), Is.True);
}
/// <summary>
/// Runs winget test can-unload-now expecting that it cannot be unloaded.
/// </summary>
[Test]
public void CanUnloadNowTest()
{
var result = TestCommon.RunAICLICommand("test", "can-unload-now --verbose");
var lines = result.StdOut.Split('\n', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
Assert.That(lines.Length, Is.EqualTo(5));
Assert.That(lines[0].Contains("Internal objects:"), Is.True);
Assert.That(lines[0].Contains("Internal objects: 0"), Is.False);
Assert.That(lines[1].Contains("External objects: 0"), Is.True);
Assert.That(lines[2].Contains("DllCanUnloadNow"), Is.True);
Assert.That(lines[3].Contains("Internal objects: 0"), Is.True);
Assert.That(lines[4].Contains("External objects: 0"), Is.True);
}
/// <summary>
/// Runs winget test term-signal-handler to check for proper thread termination.
/// </summary>
[Test]
public void TermSignalHandler()
{
var result = TestCommon.RunAICLICommand("test", "term-signal-handler --verbose");
Assert.That(result.StdOut.Contains("Got a window handle"), Is.True);
}
}
}