|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Globalization; |
| 4 | +using System.IO; |
| 5 | +using System.Reflection; |
| 6 | +using Android.App; |
| 7 | +using Android.OS; |
| 8 | +using Android.Runtime; |
| 9 | +using Microsoft.DotNet.XHarness.TestRunners.Common; |
| 10 | +using Microsoft.DotNet.XHarness.TestRunners.Xunit; |
| 11 | +using Xamarin.Essentials; |
| 12 | + |
| 13 | +namespace DeviceTests.Droid |
| 14 | +{ |
| 15 | + [Instrumentation(Name = "com.xamarin.essentials.devicetests.TestInstrumentation")] |
| 16 | + public class TestInstrumentation : Instrumentation |
| 17 | + { |
| 18 | + string resultsFileName; |
| 19 | + |
| 20 | + protected TestInstrumentation() |
| 21 | + { |
| 22 | + } |
| 23 | + |
| 24 | + protected TestInstrumentation(IntPtr handle, JniHandleOwnership transfer) |
| 25 | + : base(handle, transfer) |
| 26 | + { |
| 27 | + } |
| 28 | + |
| 29 | + public override void OnCreate(Bundle arguments) |
| 30 | + { |
| 31 | + base.OnCreate(arguments); |
| 32 | + |
| 33 | + resultsFileName = arguments.GetString("results-file-name", "TestResults.xml"); |
| 34 | + |
| 35 | + Start(); |
| 36 | + } |
| 37 | + |
| 38 | + public override async void OnStart() |
| 39 | + { |
| 40 | + base.OnStart(); |
| 41 | + |
| 42 | + var bundle = new Bundle(); |
| 43 | + |
| 44 | + var entryPoint = new TestsEntryPoint(resultsFileName); |
| 45 | + entryPoint.TestsCompleted += (sender, results) => |
| 46 | + { |
| 47 | + var message = |
| 48 | + $"Tests run: {results.ExecutedTests} " + |
| 49 | + $"Passed: {results.PassedTests} " + |
| 50 | + $"Inconclusive: {results.InconclusiveTests} " + |
| 51 | + $"Failed: {results.FailedTests} " + |
| 52 | + $"Ignored: {results.SkippedTests}"; |
| 53 | + bundle.PutString("test-execution-summary", message); |
| 54 | + |
| 55 | + bundle.PutLong("return-code", results.FailedTests == 0 ? 0 : 1); |
| 56 | + }; |
| 57 | + |
| 58 | + await entryPoint.RunAsync(); |
| 59 | + |
| 60 | + if (File.Exists(entryPoint.TestsResultsFinalPath)) |
| 61 | + bundle.PutString("test-results-path", entryPoint.TestsResultsFinalPath); |
| 62 | + |
| 63 | + if (bundle.GetLong("return-code", -1) == -1) |
| 64 | + bundle.PutLong("return-code", 1); |
| 65 | + |
| 66 | + Finish(Result.Ok, bundle); |
| 67 | + } |
| 68 | + |
| 69 | + class TestsEntryPoint : AndroidApplicationEntryPoint |
| 70 | + { |
| 71 | + readonly string resultsPath; |
| 72 | + |
| 73 | + public TestsEntryPoint(string resultsFileName) |
| 74 | + { |
| 75 | +#pragma warning disable CS0618 // Type or member is obsolete |
| 76 | + var root = Platform.HasApiLevel(30) |
| 77 | + ? Android.OS.Environment.ExternalStorageDirectory.AbsolutePath |
| 78 | + : Application.Context.GetExternalFilesDir(null)?.AbsolutePath ?? FileSystem.AppDataDirectory; |
| 79 | +#pragma warning restore CS0618 // Type or member is obsolete |
| 80 | + |
| 81 | + var docsDir = Path.Combine(root, "Documents"); |
| 82 | + |
| 83 | + if (!Directory.Exists(docsDir)) |
| 84 | + Directory.CreateDirectory(docsDir); |
| 85 | + |
| 86 | + resultsPath = Path.Combine(docsDir, resultsFileName); |
| 87 | + } |
| 88 | + |
| 89 | + protected override bool LogExcludedTests => true; |
| 90 | + |
| 91 | + public override TextWriter Logger => null; |
| 92 | + |
| 93 | + public override string TestsResultsFinalPath => resultsPath; |
| 94 | + |
| 95 | + protected override int? MaxParallelThreads => System.Environment.ProcessorCount; |
| 96 | + |
| 97 | + protected override IDevice Device { get; } = new TestDevice(); |
| 98 | + |
| 99 | + protected override IEnumerable<TestAssemblyInfo> GetTestAssemblies() |
| 100 | + { |
| 101 | + yield return new TestAssemblyInfo(Assembly.GetExecutingAssembly(), Assembly.GetExecutingAssembly().Location); |
| 102 | + yield return new TestAssemblyInfo(typeof(Battery_Tests).Assembly, typeof(Battery_Tests).Assembly.Location); |
| 103 | + } |
| 104 | + |
| 105 | + protected override void TerminateWithSuccess() |
| 106 | + { |
| 107 | + } |
| 108 | + |
| 109 | + protected override TestRunner GetTestRunner(LogWriter logWriter) |
| 110 | + { |
| 111 | + var testRunner = base.GetTestRunner(logWriter); |
| 112 | + var additional = new List<string> |
| 113 | + { |
| 114 | + $"{Traits.FileProvider}={Traits.FeatureSupport.ToExclude(Platform.HasApiLevel(24))}", |
| 115 | + }; |
| 116 | + testRunner.SkipCategories(Traits.GetSkipTraits(additional)); |
| 117 | + return testRunner; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + class TestDevice : IDevice |
| 122 | + { |
| 123 | + public string BundleIdentifier => AppInfo.PackageName; |
| 124 | + |
| 125 | + public string UniqueIdentifier => Guid.NewGuid().ToString("N"); |
| 126 | + |
| 127 | + public string Name => DeviceInfo.Name; |
| 128 | + |
| 129 | + public string Model => DeviceInfo.Model; |
| 130 | + |
| 131 | + public string SystemName => DeviceInfo.Platform.ToString(); |
| 132 | + |
| 133 | + public string SystemVersion => DeviceInfo.VersionString; |
| 134 | + |
| 135 | + public string Locale => CultureInfo.CurrentCulture.Name; |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments