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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 389 additions and 39 deletions

View File

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" IgnorableNamespaces="uap mp build" xmlns:build="http://schemas.microsoft.com/developer/appx/2015/build">
<Identity Name="3b548ab3-3034-4edf-a625-4691c73b43a9" Publisher="CN=divyan" Version="1.0.0.0" ProcessorArchitecture="x86" />
<mp:PhoneIdentity PhoneProductId="3b548ab3-3034-4edf-a625-4691c73b43a9" PhonePublisherId="00000000-0000-0000-0000-000000000000" />
<Properties>
<DisplayName>DevelopmentApp</DisplayName>
<PublisherDisplayName>divyan</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
</Properties>
<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.18362.0" MaxVersionTested="10.0.18362.0" />
<PackageDependency Name="Microsoft.VCLibs.140.00.Debug" MinVersion="14.0.27810.0" Publisher="CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US" />
<PackageDependency Name="Microsoft.NET.CoreRuntime.2.2" MinVersion="2.2.27902.3" Publisher="CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US" />
<PackageDependency Name="Microsoft.NET.CoreFramework.Debug.2.2" MinVersion="2.2.27909.0" Publisher="CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US" />
</Dependencies>
<Resources>
<Resource Language="EN-US" />
</Resources>
<Applications>
<Application Id="App" Executable="DevelopmentApp.exe" EntryPoint="DevelopmentApp.App">
<uap:VisualElements DisplayName="DevelopmentApp" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="DevelopmentApp" BackgroundColor="transparent">
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png" />
<uap:SplashScreen Image="Assets\SplashScreen.png" />
</uap:VisualElements>
</Application>
</Applications>
<Capabilities>
<Capability Name="internetClient" />
</Capabilities>
<Extensions>
<Extension Category="windows.activatableClass.inProcessServer">
<InProcessServer>
<Path>DevelopmentApp.exe</Path>
<ActivatableClass ActivatableClassId="Microsoft.UI.Xaml.Markup.ReflectionXamlMetadataProvider" ThreadingModel="both" />
</InProcessServer>
</Extension>
</Extensions>
<build:Metadata>
<build:Item Name="TargetFrameworkMoniker" Value=".NETCore,Version=v5.0" />
<build:Item Name="VisualStudio" Version="16.0" />
<build:Item Name="VisualStudioEdition" Value="Microsoft Visual Studio Enterprise 2019" />
<build:Item Name="OperatingSystem" Version="10.0.18362.1 (WinBuild.160101.0800)" />
<build:Item Name="Microsoft.Build.AppxPackage.dll" Version="16.0.30009.100" />
<build:Item Name="ProjectGUID" Value="{A995B1F6-B462-4CC0-8E7F-3C9A9354A700}" />
<build:Item Name="OptimizingToolset" Value="None" />
<build:Item Name="TargetRuntime" Value="Managed" />
<build:Item Name="Microsoft.Windows.UI.Xaml.Build.Tasks.dll" Version="10.0.18362.1" />
<build:Item Name="MakePri.exe" Version="10.0.18362.1 (WinBuild.160101.0800)" />
</build:Metadata>
</Package>

View File

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" IgnorableNamespaces="uap mp build" xmlns:build="http://schemas.microsoft.com/developer/appx/2015/build">
<Identity Name="3b548ab3-3034-4edf-a625-4691c73b43a9" Publisher="CN=divyan" Version="1.0.0.0" ProcessorArchitecture="x86" />
<mp:PhoneIdentity PhoneProductId="3b548ab3-3034-4edf-a625-4691c73b43a9" PhonePublisherId="00000000-0000-0000-0000-000000000000" />
<Properties>
<DisplayName>FrameworkApp</DisplayName>
<PublisherDisplayName>divyan</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
</Properties>
<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.18362.0" MaxVersionTested="10.0.18362.0" />
<PackageDependency Name="Microsoft.VCLibs.140.00.Debug" MinVersion="14.0.27810.0" Publisher="CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US" />
<PackageDependency Name="Microsoft.NET.CoreRuntime.2.2" MinVersion="2.2.27902.3" Publisher="CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US" />
<PackageDependency Name="Microsoft.NET.CoreFramework.Debug.2.2" MinVersion="2.2.27909.0" Publisher="CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US" />
</Dependencies>
<Resources>
<Resource Language="EN-US" />
</Resources>
<Applications>
<Application Id="App" Executable="FrameworkApp.exe" EntryPoint="FrameworkApp.App">
<uap:VisualElements DisplayName="FrameworkApp" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="FrameworkApp" BackgroundColor="transparent">
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png" />
<uap:SplashScreen Image="Assets\SplashScreen.png" />
</uap:VisualElements>
</Application>
</Applications>
<Capabilities>
<Capability Name="internetClient" />
</Capabilities>
<Extensions>
<Extension Category="windows.activatableClass.inProcessServer">
<InProcessServer>
<Path>FrameworkApp.exe</Path>
<ActivatableClass ActivatableClassId="Microsoft.UI.Xaml.Markup.ReflectionXamlMetadataProvider" ThreadingModel="both" />
</InProcessServer>
</Extension>
</Extensions>
<build:Metadata>
<build:Item Name="TargetFrameworkMoniker" Value=".NETCore,Version=v5.0" />
<build:Item Name="VisualStudio" Version="16.0" />
<build:Item Name="VisualStudioEdition" Value="Microsoft Visual Studio Enterprise 2019" />
<build:Item Name="OperatingSystem" Version="10.0.18362.1 (WinBuild.160101.0800)" />
<build:Item Name="Microsoft.Build.AppxPackage.dll" Version="16.0.30009.100" />
<build:Item Name="ProjectGUID" Value="{A995B1F6-B462-4CC0-8E7F-3C9A9354A700}" />
<build:Item Name="OptimizingToolset" Value="None" />
<build:Item Name="TargetRuntime" Value="Managed" />
<build:Item Name="Microsoft.Windows.UI.Xaml.Build.Tasks.dll" Version="10.0.18362.1" />
<build:Item Name="MakePri.exe" Version="10.0.18362.1 (WinBuild.160101.0800)" />
</build:Metadata>
</Package>

View File

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" IgnorableNamespaces="uap mp build" xmlns:build="http://schemas.microsoft.com/developer/appx/2015/build">
<Identity Name="3b548ab3-3034-4edf-a625-4691c73b43a9" Publisher="CN=divyan" Version="1.0.0.0" ProcessorArchitecture="x86" />
<mp:PhoneIdentity PhoneProductId="3b548ab3-3034-4edf-a625-4691c73b43a9" PhonePublisherId="00000000-0000-0000-0000-000000000000" />
<Properties>
<DisplayName>PackagedApp</DisplayName>
<PublisherDisplayName>divyan</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
</Properties>
<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.18362.0" MaxVersionTested="10.0.18362.0" />
<PackageDependency Name="Microsoft.VCLibs.140.00.Debug" MinVersion="14.0.27810.0" Publisher="CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US" />
<PackageDependency Name="Microsoft.NET.CoreRuntime.2.2" MinVersion="2.2.27902.3" Publisher="CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US" />
<PackageDependency Name="Microsoft.NET.CoreFramework.Debug.2.2" MinVersion="2.2.27909.0" Publisher="CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US" />
</Dependencies>
<Resources>
<Resource Language="EN-US" />
</Resources>
<Applications>
<Application Id="App" Executable="PackagedApp.exe" EntryPoint="PackagedApp.App">
<uap:VisualElements DisplayName="PackagedApp" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="PackagedApp" BackgroundColor="transparent">
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png" />
<uap:SplashScreen Image="Assets\SplashScreen.png" />
</uap:VisualElements>
</Application>
</Applications>
<Capabilities>
<Capability Name="internetClient" />
</Capabilities>
<Extensions>
<Extension Category="windows.activatableClass.inProcessServer">
<InProcessServer>
<Path>PackagedApp.exe</Path>
<ActivatableClass ActivatableClassId="Microsoft.UI.Xaml.Markup.ReflectionXamlMetadataProvider" ThreadingModel="both" />
</InProcessServer>
</Extension>
</Extensions>
<build:Metadata>
<build:Item Name="TargetFrameworkMoniker" Value=".NETCore,Version=v5.0" />
<build:Item Name="VisualStudio" Version="16.0" />
<build:Item Name="VisualStudioEdition" Value="Microsoft Visual Studio Enterprise 2019" />
<build:Item Name="OperatingSystem" Version="10.0.18362.1 (WinBuild.160101.0800)" />
<build:Item Name="Microsoft.Build.AppxPackage.dll" Version="16.0.30009.100" />
<build:Item Name="ProjectGUID" Value="{A995B1F6-B462-4CC0-8E7F-3C9A9354A700}" />
<build:Item Name="OptimizingToolset" Value="None" />
<build:Item Name="TargetRuntime" Value="Managed" />
<build:Item Name="Microsoft.Windows.UI.Xaml.Build.Tasks.dll" Version="10.0.18362.1" />
<build:Item Name="MakePri.exe" Version="10.0.18362.1 (WinBuild.160101.0800)" />
</build:Metadata>
</Package>

View File

@ -6,6 +6,8 @@
<IsPackable>false</IsPackable>
<Platforms>x64</Platforms>
<ApplicationManifest>AppxManifests\developmentApp\AppxManifest.xml</ApplicationManifest>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@ -23,4 +25,16 @@
<ProjectReference Include="..\Microsoft.Plugin.Program\Microsoft.Plugin.Program.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="AppxManifests\DevelopmentApp\AppxManifest.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="AppxManifests\FrameworkApp\AppxManifest.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="AppxManifests\PackagedApp\AppxManifest.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -0,0 +1,76 @@
using Castle.Core.Internal;
using Microsoft.Plugin.Program.Programs;
using Moq;
using NUnit.Framework;
using System.Collections.Generic;
namespace Microsoft.Plugin.Program.UnitTests.Programs
{
[TestFixture]
class UWPTests
{
static readonly PackageWrapper developmentModeApp = new PackageWrapper(
"DevelopmentApp",
"DevelopmentApp",
"DevelopmentApp",
false,
true,
"AppxManifests/DevelopmentApp"
);
static readonly PackageWrapper frameworkApp = new PackageWrapper(
"FrameworkApp",
"FrameworkApp",
"FrameworkApp",
true,
false,
"AppxManifests/FrameworkApp"
);
static readonly PackageWrapper packagedApp = new PackageWrapper(
"PackagedApp",
"PackagedApp",
"PackagedApp",
false,
false,
"AppxManifests/PackagedApp"
);
[Test]
public void All_ShouldReturnPackagesWithDevelopmentMode_WhenCalled()
{
// Arrange
Main._settings = new Settings();
List<IPackage> packages = new List<IPackage>() { developmentModeApp, packagedApp };
var mock = new Mock<IPackageManager>();
mock.Setup(x => x.FindPackagesForCurrentUser()).Returns(packages);
UWP.PackageManagerWrapper = mock.Object;
// Act
var applications = UWP.All();
// Assert
Assert.AreEqual(applications.Length, 2);
Assert.IsTrue(applications.FindAll(x => x.Name == "DevelopmentApp").Length > 0);
Assert.IsTrue(applications.FindAll(x => x.Name == "PackagedApp").Length > 0);
}
[Test]
public void All_ShouldNotReturnPackageFrameworks_WhenCalled()
{
// Arrange
Main._settings = new Settings();
List<IPackage> packages = new List<IPackage>() { frameworkApp, packagedApp };
var mock = new Mock<IPackageManager>();
mock.Setup(x => x.FindPackagesForCurrentUser()).Returns(packages);
UWP.PackageManagerWrapper = mock.Object;
// Act
var applications = UWP.All();
// Assert
Assert.AreEqual(applications.Length, 1);
Assert.IsTrue(applications.FindAll(x => x.Name == "PackagedApp").Length > 0);
}
}
}

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)
{