Packaged apps test (#4921)

* Added wrapper for PackageManager and Package class

* Added tests for package in development and framework mode

* Renamed UWP test file

* Improved readability of UWP tests
This commit is contained in:
Divyansh Srivastava
2020-07-10 13:43:02 -07:00
committed by GitHub
parent ec803d63c8
commit 653ae777d5
12 changed files with 389 additions and 39 deletions

View File

@@ -107,5 +107,11 @@
<PackageReference Include="NLog" Version="4.7.0" />
<PackageReference Include="System.Runtime" Version="4.3.1" />
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Microsoft.Plugin.Program.UnitTests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Microsoft.Plugin.Program.Programs
{
public interface IPackage
{
string Name { get; }
string FullName { get; }
string FamilyName { get; }
bool IsFramework { get; }
bool IsDevelopmentMode { get; }
string InstalledLocation { get; }
}
}

View File

@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Microsoft.Plugin.Program.Programs
{
public interface IPackageManager
{
IEnumerable<IPackage> FindPackagesForCurrentUser();
}
}

View File

@@ -0,0 +1,41 @@
using Package = Windows.ApplicationModel.Package;
namespace Microsoft.Plugin.Program.Programs
{
public class PackageWrapper : IPackage
{
public string Name { get; }
public string FullName { get; }
public string FamilyName { get; }
public bool IsFramework { get; }
public bool IsDevelopmentMode { get; }
public string InstalledLocation { get; }
public PackageWrapper(string Name, string FullName, string FamilyName, bool IsFramework, bool IsDevelopmentMode, string InstalledLocation)
{
this.Name = Name;
this.FullName = FullName;
this.FamilyName = FamilyName;
this.IsFramework = IsFramework;
this.IsDevelopmentMode = IsDevelopmentMode;
this.InstalledLocation = InstalledLocation;
}
public static PackageWrapper GetWrapperFromPackage(Package package)
{
return new PackageWrapper(
package.Id.Name,
package.Id.FullName,
package.Id.FamilyName,
package.IsFramework,
package.IsDevelopmentMode,
package.InstalledLocation.Path
);
}
}
}

View File

@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.IO.Packaging;
using System.Security.Principal;
using Windows.Management.Deployment;
using Windows.ApplicationModel;
using Package = Windows.ApplicationModel.Package;
namespace Microsoft.Plugin.Program.Programs
{
public class PackageManagerWrapper : IPackageManager
{
readonly PackageManager packageManager;
public PackageManagerWrapper()
{
packageManager = new PackageManager();
}
public IEnumerable<IPackage> FindPackagesForCurrentUser()
{
List<PackageWrapper> packages = new List<PackageWrapper>();
var user = WindowsIdentity.GetCurrent().User;
if (user != null)
{
var id = user.Value;
var m = this.packageManager.FindPackagesForUser(id);
foreach (Package p in m)
{
packages.Add(PackageWrapper.GetWrapperFromPackage(p));
}
}
return packages;
}
}
}

View File

@@ -38,12 +38,13 @@ namespace Microsoft.Plugin.Program.Programs
public PackageVersion Version { get; set; }
public UWP(Package package)
{
Name = package.Id.Name;
FullName = package.Id.FullName;
FamilyName = package.Id.FamilyName;
public static IPackageManager PackageManagerWrapper { get; set; } = new PackageManagerWrapper();
public UWP(IPackage package)
{
Name = package.Name;
FullName = package.FullName;
FamilyName = package.FamilyName;
}
public void InitializeAppInfo(string installedLocation)
@@ -155,12 +156,12 @@ namespace Microsoft.Plugin.Program.Programs
try
{
u = new UWP(p);
u.InitializeAppInfo(p.InstalledLocation.Path);
u.InitializeAppInfo(p.InstalledLocation);
}
catch (Exception e)
{
ProgramLogger.LogException($"|UWP|All|{p.InstalledLocation}|An unexpected error occurred and "
+ $"unable to convert Package to UWP for {p.Id.FullName}", e);
+ $"unable to convert Package to UWP for {p.FullName}", e);
return new Application[] { };
}
return u.Apps;
@@ -179,40 +180,30 @@ namespace Microsoft.Plugin.Program.Programs
}
}
private static IEnumerable<Package> CurrentUserPackages()
private static IEnumerable<IPackage> CurrentUserPackages()
{
var u = WindowsIdentity.GetCurrent().User;
if (u != null)
var ps = PackageManagerWrapper.FindPackagesForCurrentUser();
ps = ps.Where(p =>
{
var id = u.Value;
var m = new PackageManager();
var ps = m.FindPackagesForUser(id);
ps = ps.Where(p =>
bool valid;
try
{
bool valid;
try
{
var f = p.IsFramework;
var path = p.InstalledLocation.Path;
valid = !f && !string.IsNullOrEmpty(path);
}
catch (Exception e)
{
ProgramLogger.LogException("UWP" ,"CurrentUserPackages", $"id","An unexpected error occurred and "
+ $"unable to verify if package is valid", e);
return false;
}
var f = p.IsFramework;
var path = p.InstalledLocation;
valid = !f && !string.IsNullOrEmpty(path);
}
catch (Exception e)
{
ProgramLogger.LogException("UWP" ,"CurrentUserPackages", $"id","An unexpected error occurred and "
+ $"unable to verify if package is valid", e);
return false;
}
return valid;
});
return ps;
}
else
{
return new Package[] { };
}
return valid;
});
return ps;
}
public override string ToString()

View File

@@ -34,7 +34,8 @@ namespace Microsoft.Plugin.Program.Storage
try
{
var uwp = new UWP(args.Package);
var packageWrapper = PackageWrapper.GetWrapperFromPackage(args.Package);
var uwp = new UWP(packageWrapper);
uwp.InitializeAppInfo(args.Package.InstalledLocation.Path);
foreach (var app in uwp.Apps)
{
@@ -57,7 +58,8 @@ namespace Microsoft.Plugin.Program.Storage
if (args.Progress == 0)
{
//find apps associated with this package.
var uwp = new UWP(args.Package);
var packageWrapper = PackageWrapper.GetWrapperFromPackage(args.Package);
var uwp = new UWP(packageWrapper);
var apps = Items.Where(a => a.Package.Equals(uwp)).ToArray();
foreach (var app in apps)
{