-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
AndroidTemplateTests.cs
90 lines (74 loc) · 3.14 KB
/
AndroidTemplateTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Maui.IntegrationTests.Android;
namespace Microsoft.Maui.IntegrationTests
{
public class AndroidTemplateTests : BaseBuildTest
{
Emulator TestAvd = new Emulator();
string testPackage = "";
[OneTimeSetUp]
public void AndroidTemplateFxtSetUp()
{
if (TestEnvironment.IsMacOS && RuntimeInformation.OSArchitecture == Architecture.Arm64)
TestAvd.Abi = "arm64-v8a";
TestAvd.InstallAvd();
}
[SetUp]
public void AndroidTemplateSetUp()
{
var emulatorLog = Path.Combine(TestDirectory, $"emulator-launch-{DateTime.UtcNow.ToFileTimeUtc()}.log");
Assert.IsTrue(TestAvd.LaunchAndWaitForAvd(720, emulatorLog), "Failed to launch Test AVD.");
}
[OneTimeTearDown]
public void AndroidTemplateFxtTearDown()
{
Adb.KillEmulator(TestAvd.Id);
// adb.exe can lock certain files on windows, kill it after tests complete
if (TestEnvironment.IsWindows)
{
Adb.Run("kill-server", deviceId: TestAvd.Id);
foreach (var p in Process.GetProcessesByName("adb.exe"))
p.Kill();
}
}
[TearDown]
public void AndroidTemplateTearDown()
{
Adb.UninstallPackage(testPackage);
}
[Test]
[TestCase("maui", DotNetPrevious, "Debug")]
[TestCase("maui", DotNetPrevious, "Release")]
[TestCase("maui", DotNetCurrent, "Debug")]
[TestCase("maui", DotNetCurrent, "Release")]
[TestCase("maui-blazor", DotNetPrevious, "Debug")]
[TestCase("maui-blazor", DotNetPrevious, "Release")]
[TestCase("maui-blazor", DotNetCurrent, "Debug")]
[TestCase("maui-blazor", DotNetCurrent, "Release")]
public void RunOnAndroid(string id, string framework, string config)
{
var projectDir = TestDirectory;
var projectFile = Path.Combine(projectDir, $"{Path.GetFileName(projectDir)}.csproj");
Assert.IsTrue(DotnetInternal.New(id, projectDir, framework),
$"Unable to create template {id}. Check test output for errors.");
AddInstrumentation(projectDir);
Assert.IsTrue(DotnetInternal.Build(projectFile, config, target: "Install", framework: $"{framework}-android", properties: BuildProps),
$"Project {Path.GetFileName(projectFile)} failed to install. Check test output/attachments for errors.");
testPackage = $"com.companyname.{Path.GetFileName(projectDir).ToLowerInvariant()}";
Assert.IsTrue(XHarness.RunAndroid(testPackage, Path.Combine(projectDir, "xh-results"), -1),
$"Project {Path.GetFileName(projectFile)} failed to run. Check test output/attachments for errors.");
}
void AddInstrumentation(string projectDir)
{
var androidDir = Path.Combine(projectDir, "Platforms", "Android");
var instDestination = Path.Combine(androidDir, "Instrumentation.cs");
FileUtilities.CreateFileFromResource("TemplateLaunchInstrumentation.cs", instDestination);
Assert.True(File.Exists(instDestination), "Failed to create Instrumentation.cs");
FileUtilities.ReplaceInFile(instDestination, "namespace mauitemplate", $"namespace {Path.GetFileName(projectDir)}");
FileUtilities.ReplaceInFile(Path.Combine(androidDir, "MainActivity.cs"),
"MainLauncher = true",
"MainLauncher = true, Name = \"com.microsoft.mauitemplate.MainActivity\"");
}
}
}