| 1 | // 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. |
| 2 | |
| 3 | namespace WixTestTools |
| 4 | { |
| 5 | using System; |
| 6 | using System.IO; |
| 7 | using System.Linq; |
| 8 | using WixToolset.Data.WindowsInstaller; |
| 9 | using WixToolset.Data.WindowsInstaller.Rows; |
| 10 | using Xunit; |
| 11 | |
| 12 | public partial class PackageInstaller |
| 13 | { |
| 14 | public string PackagePdb { get; } |
| 15 | |
| 16 | private bool IsX64 { get; } |
| 17 | |
| 18 | private WindowsInstallerData WiData { get; } |
| 19 | |
| 20 | public string GetInstalledFilePath(string filename) |
| 21 | { |
| 22 | var fileRow = this.WiData.Tables["File"].Rows.Single(r => r.FieldAsString(2).Contains(filename)); |
| 23 | var componentRow = this.WiData.Tables["Component"].Rows.Single(r => r.FieldAsString(0) == fileRow.FieldAsString(1)); |
| 24 | var directoryId = componentRow.FieldAsString(2); |
| 25 | var path = filename; |
| 26 | |
| 27 | while (directoryId != null) |
| 28 | { |
| 29 | string directoryName; |
| 30 | |
| 31 | if (directoryId == "ProgramFiles6432Folder") |
| 32 | { |
| 33 | var baseDirectory = this.IsX64 ? Environment.SpecialFolder.ProgramFiles : Environment.SpecialFolder.ProgramFilesX86; |
| 34 | directoryName = Environment.GetFolderPath(baseDirectory); |
| 35 | |
| 36 | directoryId = null; |
| 37 | } |
| 38 | else if (directoryId == "LocalAppDataFolder") |
| 39 | { |
| 40 | directoryName = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); |
| 41 | |
| 42 | directoryId = null; |
| 43 | } |
| 44 | else |
| 45 | { |
| 46 | var directoryRow = this.WiData.Tables["Directory"].Rows.Single(r => r.FieldAsString(0) == directoryId); |
| 47 | var value = directoryRow.FieldAsString(2); |
| 48 | var longNameIndex = value.IndexOf('|') + 1; |
| 49 | directoryName = longNameIndex > 0 ? value.Substring(longNameIndex) : value; |
| 50 | |
| 51 | directoryId = directoryRow.FieldAsString(1); |
| 52 | } |
| 53 | |
| 54 | path = Path.Combine(directoryName, path); |
| 55 | } |
| 56 | |
| 57 | return path; |
| 58 | } |
| 59 | |
| 60 | public string GetProperty(string name) |
| 61 | { |
| 62 | var row = this.WiData.Tables["Property"].Rows.Cast<PropertyRow>().Single(r => r.Property == name); |
| 63 | return row.Value; |
| 64 | } |
| 65 | |
| 66 | public string GetControlText(string dialog, string control) |
| 67 | { |
| 68 | var row = this.WiData.Tables["Control"].Rows.Cast<ControlRow>().Single(r => r.Dialog == dialog && r.Control == control); |
| 69 | return row.Text; |
| 70 | } |
| 71 | |
| 72 | public bool IsInstalled() |
| 73 | { |
| 74 | var productCode = this.GetProperty("ProductCode"); |
| 75 | return MsiUtilities.IsProductInstalled(productCode); |
| 76 | } |
| 77 | |
| 78 | public bool IsInstalledWithVersion() |
| 79 | { |
| 80 | var productCode = this.GetProperty("ProductCode"); |
| 81 | Version prodVersion = new Version(this.GetProperty("ProductVersion")); |
| 82 | return MsiUtilities.IsProductInstalledWithVersion(productCode, prodVersion); |
| 83 | } |
| 84 | |
| 85 | public void VerifyInstalled(bool installed) |
| 86 | { |
| 87 | Assert.Equal(installed, this.IsInstalled()); |
| 88 | } |
| 89 | |
| 90 | public void VerifyInstalledWithVersion(bool installed) |
| 91 | { |
| 92 | Assert.Equal(installed, this.IsInstalledWithVersion()); |
| 93 | } |
| 94 | |
| 95 | public void DeleteTestRegistryValue(string name) |
| 96 | { |
| 97 | using (var root = this.TestContext.GetTestRegistryRoot(this.IsX64)) |
| 98 | { |
| 99 | Assert.NotNull(root); |
| 100 | root.DeleteValue(name); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | public void VerifyTestRegistryRootDeleted() |
| 105 | { |
| 106 | using var testRegistryRoot = this.TestContext.GetTestRegistryRoot(this.IsX64); |
| 107 | Assert.Null(testRegistryRoot); |
| 108 | } |
| 109 | |
| 110 | public void VerifyTestRegistryValue(string name, string expectedValue) |
| 111 | { |
| 112 | using (var root = this.TestContext.GetTestRegistryRoot(this.IsX64)) |
| 113 | { |
| 114 | Assert.NotNull(root); |
| 115 | var actualValue = root.GetValue(name) as string; |
| 116 | Assert.Equal(expectedValue, actualValue); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |