SetUpFixture.cs (5287B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="SetUpFixture.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace AppInstallerCLIE2ETests 8 { 9 using AppInstallerCLIE2ETests.Helpers; 10 using Microsoft.Win32; 11 using NUnit.Framework; 12 13 /// <summary> 14 /// Set up fixture. 15 /// </summary> 16 [SetUpFixture] 17 public class SetUpFixture 18 { 19 private static bool shouldDisableDevModeOnExit = true; 20 private static bool shouldRevertDefaultFileTypeRiskOnExit = true; 21 private static bool shouldDoAnyTeardown = true; 22 private static string defaultFileTypes = string.Empty; 23 24 /// <summary> 25 /// Set up. 26 /// </summary> 27 [OneTimeSetUp] 28 public void Setup() 29 { 30 var testParams = TestSetup.Parameters; 31 32 if (testParams.IsDefault) 33 { 34 // If no parameters are provided, use defaults that work locally. 35 // This allows the user to assume responsibility for setup. 36 shouldDoAnyTeardown = false; 37 } 38 else 39 { 40 shouldDisableDevModeOnExit = this.EnableDevMode(true); 41 42 shouldRevertDefaultFileTypeRiskOnExit = this.DecreaseFileTypeRisk(".exe;.msi", false); 43 44 if (testParams.PackagedContext) 45 { 46 if (testParams.LooseFileRegistration) 47 { 48 Assert.True(TestCommon.InstallMsixRegister(testParams.AICLIPackagePath), $"InstallMsixRegister : {testParams.AICLIPackagePath}"); 49 } 50 else 51 { 52 Assert.True(TestCommon.InstallMsix(testParams.AICLIPackagePath), $"InstallMsix : {testParams.AICLIPackagePath}"); 53 } 54 } 55 } 56 57 if (!testParams.SkipTestSource) 58 { 59 TestIndex.GenerateE2ESource(); 60 } 61 62 WinGetSettingsHelper.ForcedExperimentalFeatures = testParams.ForcedExperimentalFeatures; 63 WinGetSettingsHelper.InitializeWingetSettings(); 64 } 65 66 /// <summary> 67 /// Tear down. 68 /// </summary> 69 [OneTimeTearDown] 70 public void TearDown() 71 { 72 if (shouldDoAnyTeardown) 73 { 74 if (shouldDisableDevModeOnExit) 75 { 76 this.EnableDevMode(false); 77 } 78 79 if (shouldRevertDefaultFileTypeRiskOnExit) 80 { 81 this.DecreaseFileTypeRisk(defaultFileTypes, true); 82 } 83 84 TestCommon.PublishE2ETestLogs(); 85 86 if (TestSetup.Parameters.PackagedContext) 87 { 88 TestCommon.RemoveMsix(Constants.AICLIPackageName); 89 } 90 } 91 } 92 93 // Returns whether there's a change to the dev mode state after execution 94 private bool EnableDevMode(bool enable) 95 { 96 var appModelUnlockKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock"); 97 98 if (enable) 99 { 100 var value = appModelUnlockKey.GetValue("AllowDevelopmentWithoutDevLicense"); 101 if (value == null || (int)value == 0) 102 { 103 appModelUnlockKey.SetValue("AllowDevelopmentWithoutDevLicense", 1, RegistryValueKind.DWord); 104 return true; 105 } 106 } 107 else 108 { 109 var value = appModelUnlockKey.GetValue("AllowDevelopmentWithoutDevLicense"); 110 if (value != null && ((int)value) != 0) 111 { 112 appModelUnlockKey.SetValue("AllowDevelopmentWithoutDevLicense", 0, RegistryValueKind.DWord); 113 return true; 114 } 115 } 116 117 return false; 118 } 119 120 private bool DecreaseFileTypeRisk(string fileTypes, bool revert) 121 { 122 var defaultFileTypeRiskKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Associations"); 123 string value = (string)defaultFileTypeRiskKey.GetValue("DefaultFileTypeRisk"); 124 125 if (revert) 126 { 127 defaultFileTypeRiskKey.SetValue("LowRiskFileTypes", fileTypes); 128 return false; 129 } 130 else 131 { 132 if (string.IsNullOrEmpty(value)) 133 { 134 defaultFileTypes = string.Empty; 135 defaultFileTypeRiskKey.SetValue("LowRiskFileTypes", fileTypes); 136 } 137 else 138 { 139 defaultFileTypes = value; 140 defaultFileTypeRiskKey.SetValue("LowRiskFileTypes", string.Concat(value, fileTypes)); 141 } 142 143 return true; 144 } 145 } 146 } 147 }