summaryrefslogtreecommitdiff
path: root/src/test/burn/WixToolsetTest.BurnE2E/TestBAController.cs
blob: 8e6611a27258142909bd9574fc047313c8eade92 (plain)
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
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolsetTest.BurnE2E
{
    using System;
    using Microsoft.Win32;
    using WixTestTools;
    using WixToolset.Mba.Core;

    public class TestBAController : IDisposable
    {
        public TestBAController(WixTestContext testContext, bool x64 = false)
        {
            this.TestGroupName = testContext.TestGroupName;
            this.BaseRegKeyPath = x64 ? @"Software\WiX\Tests" : @"Software\WOW6432Node\WiX\Tests";
            this.TestBaseRegKeyPath = String.Format(@"{0}\TestBAControl\{1}", this.BaseRegKeyPath, this.TestGroupName);
        }

        private string BaseRegKeyPath { get; }

        private string TestBaseRegKeyPath { get; }

        public string TestGroupName { get; }

        /// <summary>
        /// Sets a test value in the registry to communicate with the TestBA.
        /// </summary>
        /// <param name="name">Name of the value to set.</param>
        /// <param name="value">Value to set. If this is null, the value is removed.</param>
        public void SetBurnTestValue(string name, string value)
        {
            using (var testKey = Registry.LocalMachine.CreateSubKey(this.TestBaseRegKeyPath))
            {
                if (String.IsNullOrEmpty(value))
                {
                    testKey.DeleteValue(name, false);
                }
                else
                {
                    testKey.SetValue(name, value);
                }
            }
        }

        public void SetExplicitlyElevateAndPlanFromOnElevateBegin(string value = "true")
        {
            this.SetBurnTestValue("ExplicitlyElevateAndPlanFromOnElevateBegin", value);
        }

        public void SetForceKeepRegistration(string value = "true")
        {
            this.SetBurnTestValue("ForceKeepRegistration", value);
        }

        public void SetImmediatelyQuit(string value = "true")
        {
            this.SetBurnTestValue("ImmediatelyQuit", value);
        }

        public void SetQuitAfterDetect(string value = "true")
        {
            this.SetBurnTestValue("QuitAfterDetect", value);
        }

        /// <summary>
        /// Slows the cache progress of a package.
        /// </summary>
        /// <param name="packageId">Package identity.</param>
        /// <param name="delay">Sets or removes the delay on a package being cached.</param>
        public void SetPackageSlowCache(string packageId, int? delay)
        {
            this.SetPackageState(packageId, "SlowCache", delay.HasValue ? delay.ToString() : null);
        }

        /// <summary>
        /// Cancels the cache of a package at a particular progress point.
        /// </summary>
        /// <param name="packageId">Package identity.</param>
        /// <param name="cancelPoint">Sets or removes the cancel progress on a package being cached.</param>
        public void SetPackageCancelCacheAtProgress(string packageId, int? cancelPoint)
        {
            this.SetPackageState(packageId, "CancelCacheAtProgress", cancelPoint.HasValue ? cancelPoint.ToString() : null);
        }

        /// <summary>
        /// Slows the execute progress of a package.
        /// </summary>
        /// <param name="packageId">Package identity.</param>
        /// <param name="delay">Sets or removes the delay on a package being executed.</param>
        public void SetPackageSlowExecute(string packageId, int? delay)
        {
            this.SetPackageState(packageId, "SlowExecute", delay.HasValue ? delay.ToString() : null);
        }

        /// <summary>
        /// Cancels the execute of a package at a particular progress point.
        /// </summary>
        /// <param name="packageId">Package identity.</param>
        /// <param name="cancelPoint">Sets or removes the cancel progress on a package being executed.</param>
        public void SetPackageCancelExecuteAtProgress(string packageId, int? cancelPoint)
        {
            this.SetPackageState(packageId, "CancelExecuteAtProgress", cancelPoint.HasValue ? cancelPoint.ToString() : null);
        }

        /// <summary>
        /// Cancels the execute of a package at the next progess after the specified MSI action start.
        /// </summary>
        /// <param name="packageId">Package identity.</param>
        /// <param name="actionName">Sets or removes the cancel progress on a package being executed.</param>
        public void SetPackageCancelExecuteAtActionStart(string packageId, string actionName)
        {
            this.SetPackageState(packageId, "CancelExecuteAtActionStart", actionName);
        }

        /// <summary>
        /// Cancels the execute of a package at a particular OnProgress point.
        /// </summary>
        /// <param name="packageId">Package identity.</param>
        /// <param name="cancelPoint">Sets or removes the cancel OnProgress point on a package being executed.</param>
        public void SetPackageCancelOnProgressAtProgress(string packageId, int? cancelPoint)
        {
            this.SetPackageState(packageId, "CancelOnProgressAtProgress", cancelPoint.HasValue ? cancelPoint.ToString() : null);
        }

        /// <summary>
        /// Retries the files in use one or more times before canceling.
        /// </summary>
        /// <param name="packageId">Package identity.</param>
        /// <param name="cancelPoint">Sets or removes the retry count on a package's file in use message.</param>
        public void SetPackageRetryExecuteFilesInUse(string packageId, int? retryCount)
        {
            this.SetPackageState(packageId, "RetryExecuteFilesInUse", retryCount.HasValue ? retryCount.ToString() : null);
        }

        /// <summary>
        /// Sets the requested state for a package that the TestBA will return to the engine during plan.
        /// </summary>
        /// <param name="packageId">Package identity.</param>
        /// <param name="state">State to request.</param>
        public void SetPackageRequestedState(string packageId, RequestState state)
        {
            this.SetPackageState(packageId, "Requested", state.ToString());
        }

        /// <summary>
        /// Sets the requested state for a package that the TestBA will return to the engine during plan.
        /// </summary>
        /// <param name="packageId">Package identity.</param>
        /// <param name="state">State to request.</param>
        public void SetPackageFeatureState(string packageId, string featureId, FeatureState state)
        {
            this.SetPackageState(packageId, String.Concat(featureId, "Requested"), state.ToString());
        }

        /// <summary>
        /// Requests the BA to log the test registry value for the specified package.
        /// </summary>
        /// <param name="packageId"></param>
        /// <param name="value"></param>
        public void SetPackageRecordTestRegistryValue(string packageId, string value = "true")
        {
            this.SetPackageState(packageId, "RecordTestRegistryValue", value);
        }

        public void SetPackageProcessCancelAction(string packageId, BOOTSTRAPPER_EXECUTEPROCESSCANCEL_ACTION action)
        {
            this.SetPackageState(packageId, "ProcessCancelAction", action.ToString());
        }

        /// <summary>
        /// Sets the number of times to re-run the Detect phase.
        /// </summary>
        /// <param name="state">Number of times to run Detect (after the first, normal, Detect).</param>
        public void SetRedetectCount(int redetectCount)
        {
            this.SetPackageState(null, "RedetectCount", redetectCount.ToString());
        }

        /// <summary>
        /// Resets the state for a package that the TestBA will return to the engine during plan.
        /// </summary>
        /// <param name="packageId">Package identity.</param>
        public void ResetPackageStates(string packageId)
        {
            var key = String.Format(@"{0}\{1}", this.TestBaseRegKeyPath, packageId ?? String.Empty);
            Registry.LocalMachine.DeleteSubKey(key);
        }

        public void SetVerifyArguments(string verifyArguments)
        {
            this.SetBurnTestValue("VerifyArguments", verifyArguments);

        }

        private void SetPackageState(string packageId, string name, string value)
        {
            var key = String.Format(@"{0}\{1}", this.TestBaseRegKeyPath, packageId ?? String.Empty);
            using (var packageKey = Registry.LocalMachine.CreateSubKey(key))
            {
                if (String.IsNullOrEmpty(value))
                {
                    packageKey.DeleteValue(name, false);
                }
                else
                {
                    packageKey.SetValue(name, value);
                }
            }
        }

        public void Dispose()
        {
            Registry.LocalMachine.DeleteSubKeyTree($@"{this.BaseRegKeyPath}\{this.TestGroupName}", false);
            Registry.LocalMachine.DeleteSubKeyTree($@"{this.BaseRegKeyPath}\TestBAControl", false);
        }
    }
}