Skip to content

Commit 4b46cce

Browse files
authored
Enable more tests to run on all 3 runtimes, part 10 (#10621)
Thisupdates several test suites to support multiple Android runtimes by parameterizing tests and handling runtime-specific behaviors. ### Test parameterization and runtime support * Converted static test data arrays in `SingleProjectTest.cs` and `XASdkTests.cs` to methods that generate test cases for all values of the `AndroidRuntime` enum, allowing each test to run for every runtime. Test methods now accept an `AndroidRuntime` parameter and set up projects accordingly. * Updated `WearTests.cs` test methods to accept `AndroidRuntime` as a parameter and configure projects to use the specified runtime. ### Runtime-specific handling in tests * Added logic to skip unsupported configurations and adjust project properties based on the runtime, such as setting `IsRelease` for NativeAOT and using `SetRuntime`. * Modified assertions and file path expectations to account for differences between NativeAOT and other runtimes, such as using `.aab` instead of `.apk` and adjusting assembly paths. ### Improved warning assertions * Added `AssertHasSomeWarnings` extension method to allow tests to check for a specific number of MSBuild warnings, and used it to accommodate the single warning currently emitted by NativeAOT builds.
1 parent 1c27ade commit 4b46cce

4 files changed

Lines changed: 259 additions & 100 deletions

File tree

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/SingleProjectTest.cs

Lines changed: 82 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;
45
using System.Xml.Linq;
56
using Mono.Cecil;
67
using NUnit.Framework;
8+
using Xamarin.Android.Tasks;
79
using Xamarin.Android.Tools;
810
using Xamarin.ProjectTools;
911

@@ -12,36 +14,71 @@ namespace Xamarin.Android.Build.Tests
1214
[Parallelizable (ParallelScope.Children)]
1315
public partial class SingleProjectTest : BaseTest
1416
{
15-
static readonly object [] AndroidManifestPropertiesSource = new object [] {
16-
new object [] {
17-
/* versionName */ "2.1",
18-
/* versionCode */ "42",
19-
/* errorMessage */ "",
20-
},
21-
new object [] {
22-
/* versionName */ "1.0.0",
23-
/* versionCode */ "1.0.0",
24-
/* errorMessage */ "XA0003",
25-
},
26-
new object [] {
27-
/* versionName */ "3.1.3a1",
28-
/* versionCode */ "42",
29-
/* errorMessage */ "",
30-
},
31-
new object [] {
32-
/* versionName */ "6.0-preview.7",
33-
/* versionCode */ "42",
34-
/* errorMessage */ "",
35-
},
36-
};
17+
static IEnumerable<object[]> Get_AndroidManifestProperties_Data ()
18+
{
19+
var ret = new List<object[]> ();
20+
21+
foreach (AndroidRuntime runtime in Enum.GetValues (typeof (AndroidRuntime))) {
22+
AddTestData (
23+
// TODO: this has changed across all the runtimes from the previous "2.1" to its current value.
24+
// Check if it's a valid change.
25+
versionName: "2.1+29f8376059d032c8eb436e757b146148a71d069e",
26+
versionCode: "42",
27+
errorMessage: "",
28+
runtime: runtime
29+
);
30+
31+
AddTestData (
32+
versionName: "1.0.0",
33+
versionCode: "1.0.0",
34+
errorMessage: "XA0003",
35+
runtime: runtime
36+
);
37+
38+
AddTestData (
39+
versionName: "3.1.3a1",
40+
versionCode: "42",
41+
errorMessage: "",
42+
runtime: runtime
43+
);
44+
45+
AddTestData (
46+
versionName: "6.0-preview.7",
47+
versionCode: "42",
48+
errorMessage: "",
49+
runtime: runtime
50+
);
51+
}
52+
53+
return ret;
54+
55+
void AddTestData (string versionName, string versionCode, string errorMessage, AndroidRuntime runtime)
56+
{
57+
ret.Add (new object[] {
58+
versionName,
59+
versionCode,
60+
errorMessage,
61+
runtime,
62+
});
63+
}
64+
}
3765

3866
[Test]
39-
[TestCaseSource (nameof (AndroidManifestPropertiesSource))]
40-
public void AndroidManifestProperties (string versionName, string versionCode, string errorMessage)
67+
[TestCaseSource (nameof (Get_AndroidManifestProperties_Data))]
68+
public void AndroidManifestProperties (string versionName, string versionCode, string errorMessage, AndroidRuntime runtime)
4169
{
70+
bool isRelease = runtime == AndroidRuntime.NativeAOT;
71+
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
72+
return;
73+
}
74+
4275
var packageName = "com.xamarin.singleproject";
4376
var applicationLabel = "My Sweet App";
44-
var proj = new XamarinAndroidApplicationProject ();
77+
var proj = new XamarinAndroidApplicationProject {
78+
IsRelease = isRelease,
79+
};
80+
proj.SetRuntime (runtime);
81+
4582
proj.AndroidManifest = proj.AndroidManifest
4683
.Replace ("package=\"${PACKAGENAME}\"", "")
4784
.Replace ("android:label=\"${PROJECT_NAME}\"", "")
@@ -73,7 +110,11 @@ public void AndroidManifestProperties (string versionName, string versionCode, s
73110
Assert.AreEqual (applicationLabel, doc.Root.Element("application").Attribute (AndroidAppManifest.AndroidXNamespace + "label")?.Value);
74111
}
75112

76-
var apk = b.Output.GetIntermediaryPath ($"android/bin/{packageName}.apk");
113+
string packageExtension = runtime switch {
114+
AndroidRuntime.NativeAOT => "aab",
115+
_ => "apk"
116+
};
117+
var apk = b.Output.GetIntermediaryPath ($"android/bin/{packageName}.{packageExtension}");
77118
FileAssert.Exists (apk);
78119

79120
// If not valid version, skip
@@ -86,7 +127,10 @@ public void AndroidManifestProperties (string versionName, string versionCode, s
86127
$"{versionName.Substring (0, index)}.0.0";
87128

88129
foreach (string abi in proj.GetRuntimeIdentifiersAsAbis ()) {
89-
var assemblyPath = b.Output.GetIntermediaryPath ($"android/assets/{abi}/{proj.ProjectName}.dll");
130+
string assemblyPath = runtime switch {
131+
AndroidRuntime.NativeAOT => b.Output.GetIntermediaryPath ($"{MonoAndroidHelper.AbiToRid (abi)}/linked/{proj.ProjectName}.dll"),
132+
_ => b.Output.GetIntermediaryPath ($"android/assets/{abi}/{proj.ProjectName}.dll")
133+
};
90134
FileAssert.Exists (assemblyPath);
91135
using var assembly = AssemblyDefinition.ReadAssembly (assemblyPath);
92136

@@ -107,13 +151,22 @@ public void AndroidManifestProperties (string versionName, string versionCode, s
107151
}
108152

109153
[Test]
110-
public void AndroidManifestValuesWin ()
154+
public void AndroidManifestValuesWin ([Values] AndroidRuntime runtime)
111155
{
156+
bool isRelease = runtime == AndroidRuntime.NativeAOT;
157+
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
158+
return;
159+
}
160+
112161
var packageName = "com.xamarin.singleproject";
113162
var applicationLabel = "My Sweet App";
114163
var versionName = "99.0";
115164
var versionCode = "99";
116-
var proj = new XamarinAndroidApplicationProject ();
165+
var proj = new XamarinAndroidApplicationProject {
166+
IsRelease = isRelease,
167+
};
168+
proj.SetRuntime (runtime);
169+
117170
proj.AndroidManifest = proj.AndroidManifest
118171
.Replace ("package=\"${PACKAGENAME}\"", $"package=\"{packageName}\"")
119172
.Replace ("android:label=\"${PROJECT_NAME}\"", $"android:label=\"{applicationLabel}\"")

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/AssertionExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,11 @@ public static void AssertHasNoWarnings (this DotNetCLI dotnet)
138138
{
139139
Assert.IsTrue (StringAssertEx.ContainsText (dotnet.LastBuildOutput, " 0 Warning(s)"), $"{dotnet.BuildLogFile} should have no MSBuild warnings.");
140140
}
141+
142+
[DebuggerHidden]
143+
public static void AssertHasSomeWarnings (this DotNetCLI dotnet, uint numOfExpectedWarnings)
144+
{
145+
Assert.IsTrue (StringAssertEx.ContainsText (dotnet.LastBuildOutput, $" {numOfExpectedWarnings} Warning(s)"), $"{dotnet.BuildLogFile} should have {numOfExpectedWarnings} MSBuild warnings.");
146+
}
141147
}
142148
}

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/WearTests.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.IO;
22
using NUnit.Framework;
3+
using Xamarin.Android.Tasks;
34
using Xamarin.ProjectTools;
45

56
namespace Xamarin.Android.Build.Tests
@@ -8,24 +9,36 @@ namespace Xamarin.Android.Build.Tests
89
public class WearTests : BaseTest
910
{
1011
[Test]
11-
public void BasicProject ([Values (true, false)] bool isRelease)
12+
public void BasicProject ([Values] bool isRelease, [Values] AndroidRuntime runtime)
1213
{
14+
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
15+
return;
16+
}
1317
var proj = new XamarinAndroidWearApplicationProject {
1418
IsRelease = isRelease,
1519
};
20+
proj.SetRuntime (runtime);
1621
using (var b = CreateApkBuilder ()) {
1722
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
1823
}
1924
}
2025

2126
[Test]
22-
public void BundledWearApp ()
27+
public void BundledWearApp ([Values] AndroidRuntime runtime)
2328
{
29+
bool isRelease = runtime == AndroidRuntime.NativeAOT;
30+
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
31+
return;
32+
}
33+
2434
var path = Path.Combine ("temp", TestName);
2535
var app = new XamarinAndroidApplicationProject {
36+
IsRelease = isRelease,
2637
ProjectName = "MyApp",
2738
EmbedAssembliesIntoApk = true,
2839
};
40+
app.SetRuntime (runtime);
41+
2942
var wear = new XamarinAndroidWearApplicationProject {
3043
EmbedAssembliesIntoApk = true,
3144
};
@@ -43,10 +56,15 @@ public void BundledWearApp ()
4356
}
4457

4558
[Test]
46-
public void WearProjectJavaBuildFailure ()
59+
public void WearProjectJavaBuildFailure ([Values] AndroidRuntime runtime)
4760
{
61+
const bool isRelease = true;
62+
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
63+
return;
64+
}
65+
4866
var proj = new XamarinAndroidApplicationProject {
49-
IsRelease = true,
67+
IsRelease = isRelease,
5068
EnableDefaultItems = true,
5169
PackageReferences = {
5270
KnownPackages.XamarinAndroidXWear,
@@ -56,6 +74,7 @@ public void WearProjectJavaBuildFailure ()
5674
},
5775
SupportedOSPlatformVersion = "23",
5876
};
77+
proj.SetRuntime (runtime);
5978
var builder = CreateApkBuilder ();
6079
builder.ThrowOnBuildFailure = false;
6180
Assert.IsFalse (builder.Build (proj), $"{proj.ProjectName} should fail.");

0 commit comments

Comments
 (0)