From 47582b162368e8edf7a3b11c13b8e9dabc5f0a26 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Thu, 31 Mar 2022 11:56:14 -0700 Subject: Provide managed CA and Embedded UI DTF libraries via NuGet Lots of refactoring to bring the SFX tooling back into the 'dtf' layer since they are (in the end) tightly coupled to some DTF assemblies. Also refactored the DTF tests into their own folder and added a couple integration tests to build using the new CA/UI NuGet package. Closes wixtoolset/issues#6080 --- src/test/dtf/Directory.Build.props | 11 ++ src/test/dtf/Directory.Build.targets | 6 + src/test/dtf/DtfE2ETests.sln | 28 ++++ src/test/dtf/EmbeddedUI/AssemblyInfo.cs | 3 + src/test/dtf/EmbeddedUI/EmbeddedUI.csproj | 49 ++++++ src/test/dtf/EmbeddedUI/InstallProgressCounter.cs | 174 ++++++++++++++++++++++ src/test/dtf/EmbeddedUI/SampleEmbeddedUI.cs | 130 ++++++++++++++++ src/test/dtf/EmbeddedUI/SetupWizard.xaml | 17 +++ src/test/dtf/EmbeddedUI/SetupWizard.xaml.cs | 109 ++++++++++++++ src/test/dtf/SampleCA/SampleCA.cs | 125 ++++++++++++++++ src/test/dtf/SampleCA/SampleCA.csproj | 10 ++ src/test/test.cmd | 2 + 12 files changed, 664 insertions(+) create mode 100644 src/test/dtf/Directory.Build.props create mode 100644 src/test/dtf/Directory.Build.targets create mode 100644 src/test/dtf/DtfE2ETests.sln create mode 100644 src/test/dtf/EmbeddedUI/AssemblyInfo.cs create mode 100644 src/test/dtf/EmbeddedUI/EmbeddedUI.csproj create mode 100644 src/test/dtf/EmbeddedUI/InstallProgressCounter.cs create mode 100644 src/test/dtf/EmbeddedUI/SampleEmbeddedUI.cs create mode 100644 src/test/dtf/EmbeddedUI/SetupWizard.xaml create mode 100644 src/test/dtf/EmbeddedUI/SetupWizard.xaml.cs create mode 100644 src/test/dtf/SampleCA/SampleCA.cs create mode 100644 src/test/dtf/SampleCA/SampleCA.csproj (limited to 'src/test') diff --git a/src/test/dtf/Directory.Build.props b/src/test/dtf/Directory.Build.props new file mode 100644 index 00000000..0035a9e6 --- /dev/null +++ b/src/test/dtf/Directory.Build.props @@ -0,0 +1,11 @@ + + + + + IntegrationDtf + false + + + + + diff --git a/src/test/dtf/Directory.Build.targets b/src/test/dtf/Directory.Build.targets new file mode 100644 index 00000000..4e97b6ca --- /dev/null +++ b/src/test/dtf/Directory.Build.targets @@ -0,0 +1,6 @@ + + + + + + diff --git a/src/test/dtf/DtfE2ETests.sln b/src/test/dtf/DtfE2ETests.sln new file mode 100644 index 00000000..39d8cf08 --- /dev/null +++ b/src/test/dtf/DtfE2ETests.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30114.105 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EmbeddedUI", "EmbeddedUI\EmbeddedUI.csproj", "{864B8C50-7895-4485-AC89-900D86FD8C0D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleCA", "SampleCA\SampleCA.csproj", "{8F53B9CC-6FBE-493D-9C9A-09B2AD578CE7}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {864B8C50-7895-4485-AC89-900D86FD8C0D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {864B8C50-7895-4485-AC89-900D86FD8C0D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {864B8C50-7895-4485-AC89-900D86FD8C0D}.Release|Any CPU.ActiveCfg = Debug|Any CPU + {864B8C50-7895-4485-AC89-900D86FD8C0D}.Release|Any CPU.Build.0 = Debug|Any CPU + {8F53B9CC-6FBE-493D-9C9A-09B2AD578CE7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8F53B9CC-6FBE-493D-9C9A-09B2AD578CE7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8F53B9CC-6FBE-493D-9C9A-09B2AD578CE7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8F53B9CC-6FBE-493D-9C9A-09B2AD578CE7}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/src/test/dtf/EmbeddedUI/AssemblyInfo.cs b/src/test/dtf/EmbeddedUI/AssemblyInfo.cs new file mode 100644 index 00000000..27aeb535 --- /dev/null +++ b/src/test/dtf/EmbeddedUI/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System.Reflection; + +[assembly: AssemblyDescription("Sample managed embedded external UI")] diff --git a/src/test/dtf/EmbeddedUI/EmbeddedUI.csproj b/src/test/dtf/EmbeddedUI/EmbeddedUI.csproj new file mode 100644 index 00000000..9f745a19 --- /dev/null +++ b/src/test/dtf/EmbeddedUI/EmbeddedUI.csproj @@ -0,0 +1,49 @@ + + + {864B8C50-7895-4485-AC89-900D86FD8C0D} + Library + WixToolset.Samples.EmbeddedUI + WixToolset.Samples.EmbeddedUI + v3.5 + 512 + + + + + + + + SetupWizard.xaml + + + + + + MSBuild:Compile + Designer + + + + + + 3.0 + + + 3.0 + + + + 3.5 + + + + 3.0 + + + + + + + + + diff --git a/src/test/dtf/EmbeddedUI/InstallProgressCounter.cs b/src/test/dtf/EmbeddedUI/InstallProgressCounter.cs new file mode 100644 index 00000000..3d75081c --- /dev/null +++ b/src/test/dtf/EmbeddedUI/InstallProgressCounter.cs @@ -0,0 +1,174 @@ +namespace WixToolset.Samples.EmbeddedUI +{ + using System; + using WixToolset.Dtf.WindowsInstaller; + + /// + /// Tracks MSI progress messages and converts them to usable progress. + /// + public class InstallProgressCounter + { + private int total; + private int completed; + private int step; + private bool moveForward; + private bool enableActionData; + private int progressPhase; + private double scriptPhaseWeight; + + public InstallProgressCounter() : this(0.3) + { + } + + public InstallProgressCounter(double scriptPhaseWeight) + { + if (!(0 <= scriptPhaseWeight && scriptPhaseWeight <= 1)) + { + throw new ArgumentOutOfRangeException("scriptPhaseWeight"); + } + + this.scriptPhaseWeight = scriptPhaseWeight; + } + + /// + /// Gets a number between 0 and 1 that indicates the overall installation progress. + /// + public double Progress { get; private set; } + + public void ProcessMessage(InstallMessage messageType, Record messageRecord) + { + // This MSI progress-handling code was mostly borrowed from burn and translated from C++ to C#. + + switch (messageType) + { + case InstallMessage.ActionStart: + if (this.enableActionData) + { + this.enableActionData = false; + } + break; + + case InstallMessage.ActionData: + if (this.enableActionData) + { + if (this.moveForward) + { + this.completed += this.step; + } + else + { + this.completed -= this.step; + } + + this.UpdateProgress(); + } + break; + + case InstallMessage.Progress: + this.ProcessProgressMessage(messageRecord); + break; + } + } + + private void ProcessProgressMessage(Record progressRecord) + { + // This MSI progress-handling code was mostly borrowed from burn and translated from C++ to C#. + + if (progressRecord == null || progressRecord.FieldCount == 0) + { + return; + } + + int fieldCount = progressRecord.FieldCount; + int progressType = progressRecord.GetInteger(1); + string progressTypeString = String.Empty; + switch (progressType) + { + case 0: // Master progress reset + if (fieldCount < 4) + { + return; + } + + this.progressPhase++; + + this.total = progressRecord.GetInteger(2); + if (this.progressPhase == 1) + { + // HACK!!! this is a hack courtesy of the Windows Installer team. It seems the script planning phase + // is always off by "about 50". So we'll toss an extra 50 ticks on so that the standard progress + // doesn't go over 100%. If there are any custom actions, they may blow the total so we'll call this + // "close" and deal with the rest. + this.total += 50; + } + + this.moveForward = (progressRecord.GetInteger(3) == 0); + this.completed = (this.moveForward ? 0 : this.total); // if forward start at 0, if backwards start at max + this.enableActionData = false; + + this.UpdateProgress(); + break; + + case 1: // Action info + if (fieldCount < 3) + { + return; + } + + if (progressRecord.GetInteger(3) == 0) + { + this.enableActionData = false; + } + else + { + this.enableActionData = true; + this.step = progressRecord.GetInteger(2); + } + break; + + case 2: // Progress report + if (fieldCount < 2 || this.total == 0 || this.progressPhase == 0) + { + return; + } + + if (this.moveForward) + { + this.completed += progressRecord.GetInteger(2); + } + else + { + this.completed -= progressRecord.GetInteger(2); + } + + this.UpdateProgress(); + break; + + case 3: // Progress total addition + this.total += progressRecord.GetInteger(2); + break; + } + } + + private void UpdateProgress() + { + if (this.progressPhase < 1 || this.total == 0) + { + this.Progress = 0; + } + else if (this.progressPhase == 1) + { + this.Progress = this.scriptPhaseWeight * Math.Min(this.completed, this.total) / this.total; + } + else if (this.progressPhase == 2) + { + this.Progress = this.scriptPhaseWeight + + (1 - this.scriptPhaseWeight) * Math.Min(this.completed, this.total) / this.total; + } + else + { + this.Progress = 1; + } + } + } +} diff --git a/src/test/dtf/EmbeddedUI/SampleEmbeddedUI.cs b/src/test/dtf/EmbeddedUI/SampleEmbeddedUI.cs new file mode 100644 index 00000000..b9cd213a --- /dev/null +++ b/src/test/dtf/EmbeddedUI/SampleEmbeddedUI.cs @@ -0,0 +1,130 @@ +namespace WixToolset.Samples.EmbeddedUI +{ + using System; + using System.Collections.Generic; + using System.Configuration; + using System.Threading; + using System.Windows; + using System.Windows.Threading; + using WixToolset.Dtf.WindowsInstaller; + using Application = System.Windows.Application; + + public class SampleEmbeddedUI : IEmbeddedUI + { + private Thread appThread; + private Application app; + private SetupWizard setupWizard; + private ManualResetEvent installStartEvent; + private ManualResetEvent installExitEvent; + + /// + /// Initializes the embedded UI. + /// + /// Handle to the installer which can be used to get and set properties. + /// The handle is only valid for the duration of this method call. + /// Path to the directory that contains all the files from the MsiEmbeddedUI table. + /// On entry, contains the current UI level for the installation. After this + /// method returns, the installer resets the UI level to the returned value of this parameter. + /// True if the embedded UI was successfully initialized; false if the installation + /// should continue without the embedded UI. + /// The installation was canceled by the user. + /// The embedded UI failed to initialize and + /// causes the installation to fail. + public bool Initialize(Session session, string resourcePath, ref InstallUIOptions internalUILevel) + { + if (session != null) + { + if ((internalUILevel & InstallUIOptions.Full) != InstallUIOptions.Full) + { + // Don't show custom UI when the UI level is set to basic. + return false; + + // An embedded UI could display an alternate dialog sequence for reduced or + // basic modes, but it's not implemented here. We'll just fall back to the + // built-in MSI basic UI. + } + + if (String.Equals(session["REMOVE"], "All", StringComparison.OrdinalIgnoreCase)) + { + // Don't show custom UI when uninstalling. + return false; + + // An embedded UI could display an uninstall wizard, it's just not imlemented here. + } + } + + // Start the setup wizard on a separate thread. + this.installStartEvent = new ManualResetEvent(false); + this.installExitEvent = new ManualResetEvent(false); + this.appThread = new Thread(this.Run); + this.appThread.SetApartmentState(ApartmentState.STA); + this.appThread.Start(); + + // Wait for the setup wizard to either kickoff the install or prematurely exit. + int waitResult = WaitHandle.WaitAny(new WaitHandle[] { this.installStartEvent, this.installExitEvent }); + if (waitResult == 1) + { + // The setup wizard set the exit event instead of the start event. Cancel the installation. + throw new InstallCanceledException(); + } + else + { + // Start the installation with a silenced internal UI. + // This "embedded external UI" will handle message types except for source resolution. + internalUILevel = InstallUIOptions.NoChange | InstallUIOptions.SourceResolutionOnly; + return true; + } + } + + /// + /// Processes information and progress messages sent to the user interface. + /// + /// Message type. + /// Record that contains message data. + /// Message box buttons. + /// Message box icon. + /// Message box default button. + /// Result of processing the message. + public MessageResult ProcessMessage(InstallMessage messageType, Record messageRecord, + MessageButtons buttons, MessageIcon icon, MessageDefaultButton defaultButton) + { + // Synchronously send the message to the setup wizard window on its thread. + object result = this.setupWizard.Dispatcher.Invoke(DispatcherPriority.Send, + new Func(delegate() + { + return this.setupWizard.ProcessMessage(messageType, messageRecord, buttons, icon, defaultButton); + })); + return (MessageResult) result; + } + + /// + /// Shuts down the embedded UI at the end of the installation. + /// + /// + /// If the installation was canceled during initialization, this method will not be called. + /// If the installation was canceled or failed at any later point, this method will be called at the end. + /// + public void Shutdown() + { + // Wait for the user to exit the setup wizard. + this.setupWizard.Dispatcher.BeginInvoke(DispatcherPriority.Normal, + new Action(delegate() + { + this.setupWizard.EnableExit(); + })); + this.appThread.Join(); + } + + /// + /// Creates the setup wizard and runs the application thread. + /// + private void Run() + { + this.app = new Application(); + this.setupWizard = new SetupWizard(this.installStartEvent); + this.setupWizard.InitializeComponent(); + this.app.Run(this.setupWizard); + this.installExitEvent.Set(); + } + } +} diff --git a/src/test/dtf/EmbeddedUI/SetupWizard.xaml b/src/test/dtf/EmbeddedUI/SetupWizard.xaml new file mode 100644 index 00000000..9fd493a7 --- /dev/null +++ b/src/test/dtf/EmbeddedUI/SetupWizard.xaml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + diff --git a/src/test/dtf/EmbeddedUI/SetupWizard.xaml.cs b/src/test/dtf/EmbeddedUI/SetupWizard.xaml.cs new file mode 100644 index 00000000..b846d61f --- /dev/null +++ b/src/test/dtf/EmbeddedUI/SetupWizard.xaml.cs @@ -0,0 +1,109 @@ +namespace WixToolset.Samples.EmbeddedUI +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Threading; + using System.Windows; + using System.Windows.Controls; + using System.Windows.Data; + using System.Windows.Documents; + using System.Windows.Input; + using System.Windows.Media; + using System.Windows.Media.Imaging; + using System.Windows.Navigation; + using System.Windows.Shapes; + using WixToolset.Dtf.WindowsInstaller; + + /// + /// Interaction logic for SetupWizard.xaml + /// + public partial class SetupWizard : Window + { + private ManualResetEvent installStartEvent; + private InstallProgressCounter progressCounter; + private bool canceled; + + public SetupWizard(ManualResetEvent installStartEvent) + { + this.installStartEvent = installStartEvent; + this.progressCounter = new InstallProgressCounter(0.5); + } + + public MessageResult ProcessMessage(InstallMessage messageType, Record messageRecord, + MessageButtons buttons, MessageIcon icon, MessageDefaultButton defaultButton) + { + try + { + this.progressCounter.ProcessMessage(messageType, messageRecord); + this.progressBar.Value = this.progressBar.Minimum + + this.progressCounter.Progress * (this.progressBar.Maximum - this.progressBar.Minimum); + this.progressLabel.Content = "" + (int) Math.Round(100 * this.progressCounter.Progress) + "%"; + + switch (messageType) + { + case InstallMessage.Error: + case InstallMessage.Warning: + case InstallMessage.Info: + string message = String.Format("{0}: {1}", messageType, messageRecord); + this.LogMessage(message); + break; + } + + if (this.canceled) + { + this.canceled = false; + return MessageResult.Cancel; + } + } + catch (Exception ex) + { + this.LogMessage(ex.ToString()); + this.LogMessage(ex.StackTrace); + } + + return MessageResult.OK; + } + + private void LogMessage(string message) + { + this.messagesTextBox.Text += Environment.NewLine + message; + this.messagesTextBox.ScrollToEnd(); + } + + internal void EnableExit() + { + this.progressBar.Visibility = Visibility.Hidden; + this.progressLabel.Visibility = Visibility.Hidden; + this.cancelButton.Visibility = Visibility.Hidden; + this.exitButton.Visibility = Visibility.Visible; + } + + private void installButton_Click(object sender, RoutedEventArgs e) + { + this.installButton.Visibility = Visibility.Hidden; + this.progressBar.Visibility = Visibility.Visible; + this.progressLabel.Visibility = Visibility.Visible; + this.installStartEvent.Set(); + } + + private void exitButton_Click(object sender, RoutedEventArgs e) + { + this.Close(); + } + + private void cancelButton_Click(object sender, RoutedEventArgs e) + { + if (this.installButton.Visibility == Visibility.Visible) + { + this.Close(); + } + else + { + this.canceled = true; + this.cancelButton.IsEnabled = false; + } + } + } +} diff --git a/src/test/dtf/SampleCA/SampleCA.cs b/src/test/dtf/SampleCA/SampleCA.cs new file mode 100644 index 00000000..fc9f30fe --- /dev/null +++ b/src/test/dtf/SampleCA/SampleCA.cs @@ -0,0 +1,125 @@ +namespace WixToolset.Samples +{ + using System; + using System.Collections.Generic; + using System.IO; + using WixToolset.Dtf.WindowsInstaller; + + public class SampleCA + { + [CustomAction] + public static ActionResult SampleCA1(Session session) + { + using (Record msgRec = new Record(0)) + { + msgRec[0] = "Hello from SampleCA1!" + + "\r\nCLR version is v" + Environment.Version; + session.Message(InstallMessage.Info, msgRec); + session.Message(InstallMessage.User, msgRec); + } + + session.Log("Testing summary info..."); + SummaryInfo summInfo = session.Database.SummaryInfo; + session.Log("MSI PackageCode = {0}", summInfo.RevisionNumber); + session.Log("MSI ModifyDate = {0}", summInfo.LastSaveTime); + + string testProp = session["SampleCATest"]; + session.Log("Simple property test: [SampleCATest]={0}.", testProp); + + session.Log("Testing subdirectory extraction..."); + string testFilePath = "testsub\\SampleCAs.cs"; + if (!File.Exists(testFilePath)) + { + session.Log("Subdirectory extraction failed. File not found: " + testFilePath); + return ActionResult.Failure; + } + else + { + session.Log("Found file extracted in subdirectory."); + } + + session.Log("Testing record stream extraction..."); + string tempFile = null; + try + { + tempFile = Path.GetTempFileName(); + using (View binView = session.Database.OpenView( + "SELECT `Binary`.`Data` FROM `Binary`, `CustomAction` " + + "WHERE `CustomAction`.`Target` = 'SampleCA1' AND " + + "`CustomAction`.`Source` = `Binary`.`Name`")) + { + binView.Execute(); + using (Record binRec = binView.Fetch()) + { + binRec.GetStream(1, tempFile); + } + } + + session.Log("CA binary file size: {0}", new FileInfo(tempFile).Length); + string binFileVersion = Installer.GetFileVersion(tempFile); + session.Log("CA binary file version: {0}", binFileVersion); + } + finally + { + if (tempFile != null && File.Exists(tempFile)) + { + File.Delete(tempFile); + } + } + + session.Log("Testing record stream reading..."); + using (View binView2 = session.Database.OpenView("SELECT `Data` FROM `Binary` WHERE `Name` = 'TestData'")) + { + binView2.Execute(); + using (Record binRec2 = binView2.Fetch()) + { + Stream stream = binRec2.GetStream("Data"); + string testData = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd(); + session.Log("Test data: " + testData); + } + } + + session.Log("Listing components"); + using (View compView = session.Database.OpenView( + "SELECT `Component` FROM `Component`")) + { + compView.Execute(); + foreach (Record compRec in compView) + { + using (compRec) + { + session.Log("\t{0}", compRec["Component"]); + } + } + } + + session.Log("Testing the ability to access an external MSI database..."); + string tempDbFile = Path.GetTempFileName(); + using (Database tempDb = new Database(tempDbFile, DatabaseOpenMode.CreateDirect)) + { + // Just create an empty database. + } + using (Database tempDb2 = new Database(tempDbFile)) + { + // See if we can open and query the database. + IList tables = tempDb2.ExecuteStringQuery("SELECT `Name` FROM `_Tables`"); + session.Log("Found " + tables.Count + " tables in the newly created database."); + } + File.Delete(tempDbFile); + + return ActionResult.Success; + } + + [CustomAction("SampleCA2")] + public static ActionResult SampleCustomAction2(Session session) + { + using (Record msgRec = new Record(0)) + { + msgRec[0] = "Hello from SampleCA2!"; + session.Message(InstallMessage.Info, msgRec); + session.Message(InstallMessage.User, msgRec); + } + return ActionResult.UserExit; + } + } +} diff --git a/src/test/dtf/SampleCA/SampleCA.csproj b/src/test/dtf/SampleCA/SampleCA.csproj new file mode 100644 index 00000000..fb6d8dca --- /dev/null +++ b/src/test/dtf/SampleCA/SampleCA.csproj @@ -0,0 +1,10 @@ + + + net472 + Sample managed custom actions + + + + + + diff --git a/src/test/test.cmd b/src/test/test.cmd index 3158b2c2..4c80ba7d 100644 --- a/src/test/test.cmd +++ b/src/test/test.cmd @@ -13,6 +13,8 @@ @call burn\test_burn.cmd %_C% %_T% || exit /b +msbuild -t:Restore dtf\DtfE2ETests.sln -p:Configuration=%_C% -nologo -m -warnaserror -bl:%_L%\dtfe2etests.binlog || exit /b + dotnet test wix -c %_C% --nologo -l "trx;LogFileName=%_L%\TestResults\WixToolsetTest.WixE2ETests.trx" || exit /b @popd -- cgit v1.2.3-55-g6feb