winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

AppShutdownTests.cs (5194B)


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