-
Notifications
You must be signed in to change notification settings - Fork 722
/
Copy pathApplication.cs
644 lines (545 loc) · 24.6 KB
/
Application.cs
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Xaml.Tests.MUXControls.InteractionTests.Common;
using Windows.Management.Deployment;
using Common;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
#if USING_TAEF
using WEX.TestExecution;
using WEX.TestExecution.Markup;
using WEX.Logging.Interop;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.Logging;
#endif
using Microsoft.Windows.Apps.Test.Automation;
using Microsoft.Windows.Apps.Test.Foundation;
using Microsoft.Windows.Apps.Test.Foundation.Controls;
using Microsoft.Windows.Apps.Test.Foundation.Patterns;
using Microsoft.Windows.Apps.Test.Foundation.Waiters;
using System.Runtime.InteropServices;
using Windows.Foundation;
namespace Windows.UI.Xaml.Tests.MUXControls.InteractionTests.Infra
{
using Window = Microsoft.Windows.Apps.Test.Foundation.Controls.Window;
public class Application
{
private readonly string _packageName;
private readonly string _packageFamilyName;
private readonly string _appName;
private readonly string _appWindowTitle;
private readonly string _appProcessName;
private readonly string _appInstallerName;
private readonly bool _isUWPApp;
private readonly string _certSerialNumber;
private readonly string _baseAppxDir;
private readonly UICondition _windowCondition = null;
private readonly UICondition _appFrameWindowCondition = null;
public Application(string packageName, string packageFamilyName, string appName, string testAppMainWindowTitle, string testAppProcessName, string testAppInstallerName, string certSerialNumber, string baseAppxDir, bool isUWPApp = true)
{
_packageName = packageName;
_packageFamilyName = packageFamilyName;
_appName = appName;
_isUWPApp = isUWPApp;
_certSerialNumber = certSerialNumber;
_baseAppxDir = baseAppxDir;
_appWindowTitle = testAppMainWindowTitle;
_appProcessName = testAppProcessName;
_appInstallerName = testAppInstallerName;
if (_isUWPApp)
{
_windowCondition = UICondition.Create("@ClassName='Windows.UI.Core.CoreWindow' AND @Name={0}", _appWindowTitle);
_appFrameWindowCondition = UICondition.Create("@ClassName='ApplicationFrameWindow' AND @Name={0}", _appWindowTitle);
}
else
{
_windowCondition = UICondition.Create("@ClassName='Window' AND @Name={0}", _appWindowTitle);
_appFrameWindowCondition = UICondition.Create("@ClassName='Window' AND @Name={0}", _appWindowTitle);
}
}
#region Properties
public UIObject CoreWindow { get; private set; }
public UIObject ApplicationFrameWindow { get; private set; }
public Process Process { get; private set; }
public IntPtr Hwnd
{
get
{
return CoreWindow.NativeWindowHandle;
}
}
#endregion
#region Methods
internal void Initialize(bool doLaunch = false, string deploymentDir = null)
{
var topWindowCondition = _windowCondition.OrWith(_appFrameWindowCondition);
UIObject topWindowObj = null;
bool didFindWindow = UIObject.Root.Children.TryFind(topWindowCondition, out topWindowObj);
// Only try to launch the app if we couldn't find the window.
if (doLaunch && !didFindWindow)
{
CoreWindow = Launch(deploymentDir);
if (CoreWindow.Parent.Matches(_appFrameWindowCondition))
{
ApplicationFrameWindow = CoreWindow.Parent;
}
}
else if (didFindWindow)
{
// topWindowObj should match either _windowCondition or _appFrameWindowCondition
if (topWindowObj.Matches(_windowCondition))
{
// If the top level window is CoreWindow, then there is no AppFrame window:
CoreWindow = topWindowObj;
ApplicationFrameWindow = null;
}
else // _appFrameWindowCondition
{
if(!topWindowObj.Matches(_appFrameWindowCondition))
{
// This should never happen
Verify.Fail($"Expected topWindowObj ({UIObjectToLoggableString(topWindowObj)}) to match _appFrameWindowCondition ({_appFrameWindowCondition})");
}
// Maxmize window to ensure we can find UIA elements
var appFrameWindow = new Window(topWindowObj);
if (appFrameWindow.CanMaximize)
{
appFrameWindow.SetWindowVisualState(WindowVisualState.Maximized);
}
Verify.IsTrue(topWindowObj.Matches(_appFrameWindowCondition));
ApplicationFrameWindow = topWindowObj;
Log.Comment("Looking for CoreWindow...");
for (int retries = 0; retries < 5; ++retries)
{
if (topWindowObj.Children.TryFind(_windowCondition, out var coreWindowObject))
{
CoreWindow = coreWindowObject;
Log.Comment("Found CoreWindow.");
break;
}
Log.Comment("CoreWindow not found. Sleep for 500 ms and retry");
Thread.Sleep(500);
}
}
}
if (CoreWindow == null)
{
// We expect to have a window by this point.
LogDumpTree();
throw new UIObjectNotFoundException("Could not find application window.");
}
// If this is running on desktop (it has an app frame window) then try to
// maximize the window.
if (ApplicationFrameWindow != null)
{
var appFrameWindow = new Window(ApplicationFrameWindow);
if (appFrameWindow.CanMaximize)
{
appFrameWindow.SetWindowVisualState(WindowVisualState.Maximized);
}
}
Process = Process.GetProcessById(CoreWindow.ProcessId);
Wait.InitializeWaitHelper();
if (TestEnvironment.TestContext.Properties.Contains("WaitForAppDebugger"))
{
Wait.ForAppDebugger();
}
TestCleanupHelper.TestSetupHelperPendingDisposals = 0;
}
private UIObject Launch(string deploymentDir)
{
UIObject coreWindow = null;
// When running from MUXControls repo we want to install the app.
// When running in TestMD we also want to install the app.
#if USING_TAEF
TestAppInstallHelper.InstallTestAppIfNeeded(deploymentDir, _packageName, _packageFamilyName, _appInstallerName);
#else
InstallTestAppIfNeeded();
#endif
Log.Comment("Launching app {0}", _appName);
coreWindow = LaunchApp();
Verify.IsNotNull(coreWindow, "coreWindow");
Log.Comment("Waiting for the close-app invoker to be found to signal that the app has launched successfully...");
for (int retries = 0; retries < 5; ++retries)
{
UIObject obj;
coreWindow.Descendants.TryFind(UICondition.Create("@AutomationId='__CloseAppInvoker'"), out obj);
if (obj != null)
{
Log.Comment("Invoker found!");
break;
}
Log.Comment("Invoker not found. Sleeping for 500 ms before trying again...");
Thread.Sleep(500);
}
var unhandledExceptionReportingTextBox = new Edit(coreWindow.Descendants.Find(UICondition.Create("@AutomationId='__UnhandledExceptionReportingTextBox'")));
var valueChangedSource = new PropertyChangedEventSource(unhandledExceptionReportingTextBox, Scope.Element, UIProperty.Get("Value.Value"));
valueChangedSource.Start(new TestAppCrashDetector());
Log.Comment("15056441 tracing, device family:" + Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily);
return coreWindow;
}
private UIObject LaunchApp()
{
UIObject coreWindow = null;
// Launch sometimes times out but the app is just slow to launch and Launch has what appears to be
// a 5-second timeout built in. 5 seconds isn't always enough in VM scenarios. If we try again,
// Launch will see that the app is already running and move on.
const int MaxLaunchRetries = 5;
for (int retries = 1; retries <= MaxLaunchRetries; ++retries)
{
try
{
Log.Comment("Attempting launch, try #{0}...", retries);
coreWindow = _isUWPApp ? LaunchUWPApp() : LaunchNonUWPApp(_packageName);
Log.Comment("Launch successful!");
break;
}
catch (Exception ex)
{
Log.Comment("Failed to launch app. Exception: " + ex.ToString());
if (retries < MaxLaunchRetries)
{
Log.Comment("UAPApp.Launch might not have waited long enough, trying again {0}", retries);
Thread.Sleep(TimeSpan.FromSeconds(10)); // Give a healthy wait time.
}
else
{
Log.Comment("Dumping UIA tree...");
LogDumpTree();
Log.Error("Could not launch app {0} with top-level window condition '{1}'!", _appName, CreateTopLevelWindowCondition().ToString());
throw;
}
}
}
return coreWindow;
}
private UIObject LaunchUWPApp()
{
Debug.Assert(_isUWPApp);
var nameCondition = UICondition.CreateFromName(_appWindowTitle);
var topLevelWindowCondition = CreateTopLevelWindowCondition().AndWith(nameCondition);
return UAPApp.Launch(_appName, topLevelWindowCondition);
}
private UIObject LaunchNonUWPApp(string packageName)
{
Debug.Assert(!_isUWPApp);
var nameCondition = UICondition.CreateFromName(packageName);
var topLevelWindowCondition = UICondition.CreateFromClassName("Window").AndWith(nameCondition);
UIObject coreWindow = null;
try
{
coreWindow = UAPApp.Launch(_appName, topLevelWindowCondition);
}
catch
{
// UAP.Launch launches the app, but fails trying to find a CoreWindow in the launched app. If the
// App is not UWP (as in the case of WPF app hosting a XAML Island), We can fallback to looking for
// just a window and use it as the root for future queries.
// not a uwp app, see if we can find any window with the condition that matches.
coreWindow = FindCore.ByNameAndClassName(root: UIObject.Root, name: packageName, className: "Window", shouldWait: true);
}
return coreWindow;
}
public void Close()
{
int appWindowsProccessId = GetProcessIdFromAppWindow();
if (!CloseAppWindowWithCloseButton())
{
Log.Comment("Failed to close application window. We will fall back to terminating the app process.");
}
EnsureApplicationProcessHasExited(appWindowsProccessId);
if (Process != null)
{
Process.Dispose();
Process = null;
}
}
private void EnsureApplicationProcessHasExited(int appWindowsProccessId)
{
// Ensure that the application process has exited.
// Terminate the process and wait if necessary.
// To ensure that we don't end up in an unexpected state, we use multiple ways to
// find application processes
// 1. Find processes by name matching the app name
// 2. Use the proc id from the app window UIA object
// 3. Use the Process obj we found when this Application object was initialized.
// This is just a sanity check. Under normal circumstances, there should only be
// one app process.
var appProcesses = Process.GetProcessesByName(_appProcessName).ToList();
if (appWindowsProccessId != -1)
{
try
{
appProcesses.Add(Process.GetProcessById(appWindowsProccessId));
}
catch (Exception)
{
// Ignore. GetProcessById throws if the process has already exited.
}
}
if (Process != null)
{
appProcesses.Add(Process);
}
foreach (var proc in appProcesses)
{
if (!proc.HasExited)
{
if (!KillProcessAndWaitForExit(proc))
{
throw new Exception($"Unable to kill process: {proc}");
}
}
}
}
private int GetProcessIdFromAppWindow()
{
if (UIObject.Root.Children.TryFind(_windowCondition, out UIObject topWindowObj))
{
return topWindowObj.ProcessId;
}
else
{
return -1;
}
}
private bool CloseAppWindowWithCloseButton()
{
var topWindowCondition = _windowCondition.OrWith(_appFrameWindowCondition);
UIObject topWindowObj = null;
bool didFindWindow = UIObject.Root.Children.TryFind(topWindowCondition, out topWindowObj);
if(!didFindWindow)
{
Log.Comment("Application.CloseAppWindowWithCloseButton: Cound not find app window.");
return false;
}
Log.Comment("Closing application: {0}", topWindowObj);
UIObject closeAppInvoker;
if (!topWindowObj.Descendants.TryFind(UICondition.Create("@AutomationId='__CloseAppInvoker'"), out closeAppInvoker))
{
Log.Comment("Application.CloseAppWindowWithCloseButton: Failed to find close app invoker.");
return false;
}
Log.Comment("Invoking CloseAppInvoker {0}", closeAppInvoker);
(new Button(closeAppInvoker)).Invoke();
bool didWindowClose = WaitForWindowToClose(topWindowCondition);
if (!didWindowClose)
{
Log.Comment("Application.CloseAppWindowWithCloseButton: Window did not close");
return false;
}
return true;
}
private static bool KillProcessAndWaitForExit(Process process)
{
Log.Comment($"Killing process {process}");
if (process.HasExited)
{
return true;
}
else
{
process.Kill();
return process.WaitForExit(10000 /*milliseconds*/);
}
}
private static bool WaitForWindowToClose(UICondition topWindowCondition)
{
// We'll wait until the window closes. For some reason, ProcessClosedWaiter
// doesn't seem to actually work, so we'll instead just check for the window
// until the check fails.
int triesLeft = 20;
bool didFindWindow = true;
do
{
if (triesLeft == 0)
{
return false;
}
Wait.ForMilliseconds(100);
didFindWindow = UIObject.Root.Children.TryFind(topWindowCondition, out UIObject topWindowObj);
triesLeft--;
} while (didFindWindow);
return true;
}
public void GoBack()
{
Log.Comment("Going to the previous page...");
// The System Back button sometimes cannot be found at the first attempt,
// or invoking it sometimes fails. Retry a few times.
for (int retries = 0; ; retries++)
{
try
{
// If the process has closed prematurely, then there's no back button to press,
// so we'll just no-op.
if (Process.HasExited)
{
Log.Comment("Process already exited");
return;
}
Log.Comment("Invoking the back button...");
FindElement.ById<Button>("__GoBackInvoker").InvokeAndWait(TimeSpan.FromSeconds(10));
Log.Comment("Invoke successful.");
// We're now exiting the page we were previously on, so everything has changed. As such, we should clear our
// element cache, in order to ensure that we don't accidentally retrieve any stale UI objects.
ElementCache.Clear();
// Successfully found and invoked the Back button. Exit the retry loop.
break;
}
catch (Exception e)
{
string log = "Failed to find and invoke Back button. Error: " + e.ToString();
if (retries == 10)
{
Log.Error(log);
DumpHelper.DumpFullContext();
throw;
}
else
{
Log.Warning(log);
if (retries % 2 == 0)
{
Log.Comment("Clearing element cache which may be stale.");
ElementCache.Clear();
}
Log.Comment("Back button was not found. Trying again ({0}).", retries);
Thread.Sleep(150);
}
}
}
}
private UICondition CreateTopLevelWindowCondition()
{
string deviceFamily = Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily;
if (deviceFamily.Equals("Windows.Desktop", StringComparison.OrdinalIgnoreCase)
|| deviceFamily.Equals("Windows.Server", StringComparison.OrdinalIgnoreCase)
|| deviceFamily.Equals("Windows.Team", StringComparison.OrdinalIgnoreCase))
{
return UICondition.CreateFromClassName("ApplicationFrameWindow");
}
else
{
return UICondition.CreateFromClassName("Windows.UI.Core.CoreWindow");
}
}
#if !USING_TAEF
private void InstallTestAppIfNeeded()
{
string mostRecentlyBuiltAppx = string.Empty;
DateTime timeMostRecentlyBuilt = DateTime.MinValue;
var exclude = new[] { "Microsoft.NET.CoreRuntime", "Microsoft.VCLibs" };
var files = Directory.GetFiles(_baseAppxDir, $"{_packageName}*.appx", SearchOption.AllDirectories).ToList();
files.AddRange(Directory.GetFiles(_baseAppxDir, $"{_packageName}*.msix", SearchOption.AllDirectories));
var filteredFiles = files.Where(f => !exclude.Any(Path.GetFileNameWithoutExtension(f).Contains));
if (filteredFiles.Count() == 0)
{
throw new Exception(string.Format("Failed to find '*.appx' or '*.msix' in {0}'!", _baseAppxDir));
}
foreach (string file in filteredFiles)
{
DateTime fileWriteTime = File.GetLastWriteTime(file);
if (fileWriteTime > timeMostRecentlyBuilt)
{
timeMostRecentlyBuilt = fileWriteTime;
mostRecentlyBuiltAppx = file;
}
}
PackageManager packageManager = new PackageManager();
DeploymentResult result = null;
var installedPackages = packageManager.FindPackagesForUser(string.Empty, _packageFamilyName);
foreach (var installedPackage in installedPackages)
{
Log.Comment("Test AppX package already installed. Removing existing package by name: {0}", installedPackage.Id.FullName);
AutoResetEvent removePackageCompleteEvent = new AutoResetEvent(false);
var removePackageOperation = packageManager.RemovePackageAsync(installedPackage.Id.FullName);
removePackageOperation.Completed = (operation, status) =>
{
if (status != AsyncStatus.Started)
{
result = operation.GetResults();
removePackageCompleteEvent.Set();
}
};
removePackageCompleteEvent.WaitOne();
if (!string.IsNullOrEmpty(result.ErrorText))
{
Log.Error("Removal failed!");
Log.Error("Package removal ActivityId = {0}", result.ActivityId);
Log.Error("Package removal ErrorText = {0}", result.ErrorText);
Log.Error("Package removal ExtendedErrorCode = {0}", result.ExtendedErrorCode);
}
else
{
Log.Comment("Removal successful.");
}
}
Log.Comment("Installing AppX...");
Log.Comment("Checking if the app's certificate is installed...");
// If the certificate for the app is not present, installing it requires elevation.
// We'll run Add-AppDevPackage.ps1 without -Force in that circumstance so the user
// can be prompted to allow elevation. We don't want to run it without -Force all the time,
// as that prompts the user to hit enter at the end of the install, which is an annoying
// and unnecessary step. The parameter is the SHA-1 hash of the certificate.
var certutilProcess = Process.Start(new ProcessStartInfo("certutil.exe",
string.Format("-verifystore TrustedPeople {0}", _certSerialNumber)) {
UseShellExecute = true
});
certutilProcess.WaitForExit();
if(certutilProcess.ExitCode == 0)
{
Log.Comment("Certificate is installed. Installing app...");
}
else
{
Log.Comment("Certificate is not installed. Installing app and certificate...");
}
var powershellProcess = Process.Start(new ProcessStartInfo("powershell",
string.Format("-ExecutionPolicy Unrestricted -File {0}\\Add-AppDevPackage.ps1 {1}",
Path.GetDirectoryName(mostRecentlyBuiltAppx),
certutilProcess.ExitCode == 0 ? "-Force" : "")) {
UseShellExecute = true
});
powershellProcess.WaitForExit();
if (powershellProcess.ExitCode != 0)
{
throw new Exception(string.Format("Failed to install AppX for {0}!", _packageName));
}
}
#endif
#endregion
private void LogDumpTree()
{
try
{
TestEnvironment.LogDumpTree(UIObject.Root);
}
catch(Exception e)
{
Log.Comment(e.Message);
}
}
// UIObjects expose a ToString method to give a human-readable representation of the object.
// But the string includes '{' and '}' which the test logger does not handle well.
private string UIObjectToLoggableString(UIObject obj)
{
if(obj == null)
{
return "Null";
}
else
{
return obj.ToString().Replace('{', '[').Replace('}', ']');
}
}
}
}