Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Run Device Tests for UWP #149

Merged
merged 6 commits into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Caboodle/Permissions/Permissions.uwp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using Windows.Devices.Geolocation;
Expand All @@ -22,12 +23,14 @@ static void PlatformEnsureDeclared(PermissionType permission)
return;

var doc = XDocument.Load(appManifestFilename, LoadOptions.None);
var xname = XNamespace.Get(appManifestXmlns);

var reader = doc.CreateReader();
var namespaceManager = new XmlNamespaceManager(reader.NameTable);
namespaceManager.AddNamespace("x", appManifestXmlns);
foreach (var cap in uwpCapabilities)
{
// If the manifest doesn't contain a capability we need, throw
if (!doc.Root.XPathSelectElements($"//{xname}Capabilities[@Name='{cap}'")?.Any() ?? false)
if ((!doc.Root.XPathSelectElements($"//x:DeviceCapability[@Name='{cap}']", namespaceManager)?.Any() ?? false) &&
(!doc.Root.XPathSelectElements($"//x:Capability[@Name='{cap}']", namespaceManager)?.Any() ?? false))
throw new PermissionException($"You need to declare the capability `{cap}` in your AppxManifest.xml file");
}
}
Expand Down
2 changes: 1 addition & 1 deletion Caboodle/Preferences/Preferences.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ static T PlatformGet<T>(string key, T defaultValue, string sharedName)
}
else
{
if (!double.TryParse(savedDouble, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out double outDouble))
if (!double.TryParse(savedDouble, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out var outDouble))
{
var maxString = Convert.ToString(double.MaxValue, CultureInfo.InvariantCulture);
outDouble = savedDouble.Equals(maxString) ? double.MaxValue : double.MinValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<PackageReference Include="Xamarin.Android.Support.CustomTabs" Version="26.1.0.1" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.devices" Version="2.3.3" />
<PackageReference Include="UnitTests.HeadlessRunner" Version="1.1.0" />
<PackageReference Include="UnitTests.HeadlessRunner" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Caboodle\Caboodle.csproj">
Expand Down
17 changes: 15 additions & 2 deletions DeviceTests/Caboodle.DeviceTests.Android/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Reflection;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Android.App;
using Android.Content.PM;
using Android.OS;
Expand All @@ -20,7 +22,18 @@ protected override void OnCreate(Bundle bundle)

if (!string.IsNullOrEmpty(hostIp))
{
Tests.RunAsync(hostIp, hostPort, Traits.GetCommonTraits(), typeof(Battery_Tests).Assembly);
// Run the headless test runner for CI
Task.Run(() =>
{
return Tests.RunAsync(new TestOptions
{
Assemblies = new List<Assembly> { typeof(Battery_Tests).Assembly },
NetworkLogHost = hostIp,
NetworkLogPort = hostPort,
Filters = Traits.GetCommonTraits(),
Format = TestResultsFormat.XunitV2
});
});
}

// tests can be inside the main assembly
Expand Down
39 changes: 24 additions & 15 deletions DeviceTests/Caboodle.DeviceTests.UWP/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Reflection;
using System.Threading.Tasks;
using System.Web;
using UnitTests.HeadlessRunner;
using Windows.ApplicationModel.Activation;
Expand All @@ -12,31 +13,39 @@ namespace Caboodle.DeviceTests.UWP
{
public sealed partial class App : RunnerApplication
{
protected override async void OnActivated(IActivatedEventArgs args)
protected override void OnActivated(IActivatedEventArgs args)
{
base.OnActivated(args);

if (args.Kind == ActivationKind.Protocol)
{
var protocolArgs = (ProtocolActivatedEventArgs)args;
if (!string.IsNullOrEmpty(protocolArgs?.Uri?.Query))
if (!string.IsNullOrEmpty(protocolArgs?.Uri?.Host))
{
var q = HttpUtility.ParseQueryString(protocolArgs.Uri.Query);
var ip = q["host_ip"];
int port;
if (!string.IsNullOrEmpty(ip) && int.TryParse(q["host_port"], out port))
var parts = protocolArgs.Uri.Host.Split('_');
if (parts.Length >= 2 && !string.IsNullOrEmpty(parts[0]))
{
#pragma warning disable 4014
try
{
Tests.RunAsync(ip, port, Traits.GetCommonTraits(), typeof(Battery_Tests).Assembly);
}
catch (Exception ex)
var ip = parts[0]?.Replace('-', '.');

if (int.TryParse(parts[1], out var port))
{
var m = new MessageDialog("Ex: " + ex.ToString());
await m.ShowAsync();
Task.Run(() =>
{
var xunitRunner = new UnitTests.HeadlessRunner.Xunit.XUnitTestInstrumentation
{
NetworkLogEnabled = true,
NetworkLogHost = ip,
NetworkLogPort = port,
ResultsFormat = TestResultsFormat.XunitV2,
Filters = Traits.GetCommonTraits()
};

var asm = typeof(App).GetTypeInfo().Assembly;
var asmFilename = asm.GetName().Name + ".exe";

xunitRunner.Run(new TestAssemblyInfo(asm, asmFilename));
});
}
#pragma warning restore 4014
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="UnitTests.HeadlessRunner">
<Version>1.1.0</Version>
<Version>2.0.0</Version>
</PackageReference>
<PackageReference Include="Xamarin.Forms" Version="2.5.0.280555" />
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform" Version="6.0.6" />
Expand Down
20 changes: 16 additions & 4 deletions DeviceTests/Caboodle.DeviceTests.iOS/AppDelegate.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Reflection;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Foundation;
using UIKit;
using UnitTests.HeadlessRunner;
Expand All @@ -15,10 +17,20 @@ public override bool FinishedLaunching(UIApplication app, NSDictionary options)
if (testCfg != null && testCfg.Length > 1)
{
var ip = testCfg[0];
int port;
if (int.TryParse(testCfg[1], out port))
if (int.TryParse(testCfg[1], out var port))
{
Tests.RunAsync(ip, port, Traits.GetCommonTraits(), typeof(Battery_Tests).Assembly);
// Run the headless test runner for CI
Task.Run(() =>
{
return Tests.RunAsync(new TestOptions
{
Assemblies = new List<Assembly> { typeof(Battery_Tests).Assembly },
NetworkLogHost = ip,
NetworkLogPort = port,
Filters = Traits.GetCommonTraits(),
Format = TestResultsFormat.XunitV2
});
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<PackageReference Include="Xamarin.Forms" Version="2.5.0.280555" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.devices" Version="2.3.3" />
<PackageReference Include="UnitTests.HeadlessRunner" Version="1.1.0" />
<PackageReference Include="UnitTests.HeadlessRunner" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Caboodle\Caboodle.csproj">
Expand Down
9 changes: 5 additions & 4 deletions DeviceTests/build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ var IOS_SIM_RUNTIME = EnvironmentVariable("IOS_SIM_RUNTIME") ?? "iOS 11.1";
var IOS_PROJ = "./Caboodle.DeviceTests.iOS/Caboodle.DeviceTests.iOS.csproj";
var IOS_BUNDLE_ID = "com.xamarin.caboodle.devicetests";
var IOS_IPA_PATH = "./Caboodle.DeviceTests.iOS/bin/iPhoneSimulator/Release/CaboodleDeviceTestsiOS.app";
var IOS_TEST_RESULTS_PATH = "./nunit-ios.xml";
var IOS_TEST_RESULTS_PATH = "./xunit-ios.xml";

var ANDROID_PROJ = "./Caboodle.DeviceTests.Android/Caboodle.DeviceTests.Android.csproj";
var ANDROID_APK_PATH = "./Caboodle.DeviceTests.Android/bin/Release/com.xamarin.caboodle.devicetests-Signed.apk";
var ANDROID_TEST_RESULTS_PATH = "./nunit-android.xml";
var ANDROID_TEST_RESULTS_PATH = "./xunit-android.xml";
var ANDROID_AVD = "CABOODLE";
var ANDROID_PKG_NAME = "com.xamarin.caboodle.devicetests";
var ANDROID_EMU_TARGET = EnvironmentVariable("ANDROID_EMU_TARGET") ?? "system-images;android-26;google_apis;x86";
var ANDROID_EMU_DEVICE = EnvironmentVariable("ANDROID_EMU_DEVICE") ?? "Nexus 5X";

var UWP_PROJ = "./Caboodle.DeviceTests.UWP/Caboodle.DeviceTests.UWP.csproj";
var UWP_TEST_RESULTS_PATH = "./nunit-uwp.xml";
var UWP_TEST_RESULTS_PATH = "./xunit-uwp.xml";
var UWP_PACKAGE_ID = "ec0cc741-fd3e-485c-81be-68815c480690";

var TCP_LISTEN_PORT = 10578;
Expand Down Expand Up @@ -279,7 +279,8 @@ Task ("test-uwp-emu")

// Launch the app
Information("Running appx: {0}", appxBundlePath);
System.Diagnostics.Process.Start($"caboodle-device-tests://?host_ip={TCP_LISTEN_HOST}&host_port={TCP_LISTEN_PORT}");
var ip = TCP_LISTEN_HOST.Replace(".", "-");
System.Diagnostics.Process.Start($"caboodle-device-tests://{ip}_{TCP_LISTEN_PORT}");

// Wait for the test results to come back
Information("Waiting for tests...");
Expand Down