Skip to content

Commit 66d0165

Browse files
dellis1972jonpryor
authored andcommitted
[Xamarin.Android.Build.Tests] Migrate Tests from commercial repo (#2980)
As part of a move to unify our unit tests into the main repo this PR brings in all the InstantRun tests. They are protected behind a `CommercialBuildAvailable` check which will only be `true` if the commercial parts are available. The commercial tests are also flagged with a nunit `[Category("Commercial")]` just in case we decide to filter in the future. This PR also makes some changes to the folder structure. ALL `Task` based tests (i.e ones that test just a `Task` using the `MockBbuildEngine`) have been moved into the `Tasks` folder. We already have a few tests in that folder, this make sure they are all in one place. The `*.OSS.cs` partial class files have been renamed to `*.TestCaseSource.cs`. They also handle the tests for BOTH open source and commercial builds. As a result they will not longer be needed in the commercial repo. They were also moved into the shared project. This change will probably cause commercial build failures until PR #966 is merged on the commercial side.
1 parent 9b99cce commit 66d0165

17 files changed

+740
-19
lines changed

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.OSS.cs renamed to src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.TestCaseSource.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ public partial class BuildTest : BaseTest
7272
/* embedassebmlies */ true ,
7373
/* expectedResult */ "release",
7474
},
75+
new object[] {
76+
/* supportedAbi */ new string[] { "armeabi-v7a"},
77+
/* debugSymbols */ true ,
78+
/* debugType */ "Full",
79+
/* optimize */ true ,
80+
/* embedassebmlies */ false ,
81+
/* expectedResult */ CommercialBuildAvailable ? "debug" : "release",
82+
},
7583
new object[] {
7684
/* supportedAbi */ new string[] { "armeabi-v7a"},
7785
/* debugSymbols */ true ,
@@ -102,7 +110,7 @@ public partial class BuildTest : BaseTest
102110
/* debugType */ "",
103111
/* optimize */ true ,
104112
/* embedassebmlies */ false ,
105-
/* expectedResult */ "release",
113+
/* expectedResult */ CommercialBuildAvailable ? "debug" : "release",
106114
},
107115
new object[] {
108116
/* supportedAbi */ new string[] { "armeabi-v7a"},
@@ -126,7 +134,7 @@ public partial class BuildTest : BaseTest
126134
/* debugType */ "",
127135
/* optimize */ null ,
128136
/* embedassebmlies */ null ,
129-
/* expectedResult */ "release",
137+
/* expectedResult */ CommercialBuildAvailable ? "debug" : "release",
130138
},
131139
};
132140

@@ -137,7 +145,7 @@ public partial class BuildTest : BaseTest
137145
/* aotAssemblies */ false,
138146
/* debugSymbols */ true,
139147
/* debugType */ "Full",
140-
/* embedMdb */ true, // because we don't use FastDev in the OSS repo
148+
/* embedMdb */ !CommercialBuildAvailable, // because we don't use FastDev in the OSS repo
141149
/* expectedRuntime */ "debug",
142150
},
143151
new object[] {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3693,6 +3693,20 @@ public void AssemblyWithMissingTargetFramework ()
36933693
FileAssert.Exists (javaStub);
36943694
}
36953695
}
3696+
3697+
[Test]
3698+
[Category ("Commercial")]
3699+
public void LibraryProjectsShouldSkipGetPrimaryCpuAbi ()
3700+
{
3701+
if (!CommercialBuildAvailable)
3702+
Assert.Ignore ("Not required on Open Source Builds");
3703+
const string target = "_GetPrimaryCpuAbi";
3704+
var proj = new XamarinAndroidLibraryProject ();
3705+
using (var b = CreateDllBuilder (Path.Combine ("temp", TestName))) {
3706+
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
3707+
Assert.IsTrue (b.Output.IsTargetSkipped (target), $"`{target}` should be skipped!");
3708+
}
3709+
}
36963710
}
36973711
}
36983712

Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
using System;
2+
using NUnit.Framework;
3+
using Xamarin.ProjectTools;
4+
using System.IO;
5+
using System.Linq;
6+
using Microsoft.Build.Framework;
7+
using System.Text;
8+
using System.Xml.Linq;
9+
10+
namespace Xamarin.Android.Build.Tests
11+
{
12+
[TestFixture]
13+
[NonParallelizable] //These tests deploy to devices
14+
[Category ("Commercial")]
15+
public class InstallTests : BaseTest
16+
{
17+
[Test]
18+
public void ReInstallIfUserUninstalled ([Values (false, true)] bool isRelease)
19+
{
20+
if (!CommercialBuildAvailable)
21+
Assert.Ignore ("Not required on Open Source Builds");
22+
23+
if (!HasDevices) {
24+
Assert.Ignore ("Test Skipped no devices or emulators found.");
25+
}
26+
27+
var proj = new XamarinAndroidApplicationProject () {
28+
IsRelease = isRelease,
29+
};
30+
if (isRelease) {
31+
var abis = new string [] { "armeabi-v7a", "x86" };
32+
proj.SetProperty (KnownProperties.AndroidSupportedAbis, string.Join (";", abis));
33+
}
34+
using (var builder = CreateApkBuilder (Path.Combine ("temp", TestContext.CurrentContext.Test.Name))) {
35+
builder.Verbosity = LoggerVerbosity.Diagnostic;
36+
Assert.IsTrue (builder.Build (proj));
37+
Assert.IsTrue (builder.Install (proj));
38+
Assert.AreEqual ($"package:{proj.PackageName}", RunAdbCommand ($"shell pm list packages {proj.PackageName}").Trim (),
39+
$"{proj.PackageName} is not installed on the device.");
40+
Assert.AreEqual ("Success", RunAdbCommand ($"uninstall {proj.PackageName}").Trim (), $"{proj.PackageName} was not uninstalled.");
41+
Assert.IsTrue (builder.Install (proj));
42+
Assert.AreEqual ($"package:{proj.PackageName}", RunAdbCommand ($"shell pm list packages {proj.PackageName}").Trim (),
43+
$"{proj.PackageName} is not installed on the device.");
44+
}
45+
}
46+
47+
[Test]
48+
public void InstallAndUnInstall ([Values (false, true)] bool isRelease)
49+
{
50+
if (!CommercialBuildAvailable)
51+
Assert.Ignore ("Not required on Open Source Builds");
52+
53+
if (!HasDevices) {
54+
Assert.Ignore ("Test Skipped no devices or emulators found.");
55+
}
56+
57+
var proj = new XamarinAndroidApplicationProject () {
58+
IsRelease = isRelease,
59+
};
60+
if (isRelease) {
61+
var abis = new string [] { "armeabi-v7a", "x86" };
62+
proj.SetProperty (KnownProperties.AndroidSupportedAbis, string.Join (";", abis));
63+
}
64+
using (var builder = CreateApkBuilder (Path.Combine ("temp", TestContext.CurrentContext.Test.Name))) {
65+
builder.Verbosity = LoggerVerbosity.Diagnostic;
66+
Assert.IsTrue (builder.Build (proj));
67+
Assert.IsTrue (builder.Install (proj));
68+
Assert.AreEqual ($"package:{proj.PackageName}", RunAdbCommand ($"shell pm list packages {proj.PackageName}").Trim (),
69+
$"{proj.PackageName} is not installed on the device.");
70+
71+
var overrideDirs = new string [] {
72+
$"/storage/emulated/0/Android/data/{proj.PackageName}/files/.__override__",
73+
$"/mnt/shell/emulated/0/Android/data/{proj.PackageName}/files/.__override__",
74+
$"/storage/sdcard/Android/data/{proj.PackageName}/files/.__override__",
75+
};
76+
var directorylist = string.Empty;
77+
foreach (var dir in overrideDirs) {
78+
var listing = RunAdbCommand ($"shell ls {dir}");
79+
if (!listing.Contains ("No such file or directory"))
80+
directorylist += listing;
81+
}
82+
if (!isRelease) {
83+
StringAssert.Contains ($"{proj.AssemblyName}", directorylist, $"{proj.AssemblyName} not found in fastdev directory.");
84+
}
85+
else {
86+
StringAssert.IsMatch ("", directorylist.Trim (), "fastdev directory should NOT exist for Release builds.");
87+
}
88+
89+
Assert.IsTrue (builder.Uninstall (proj));
90+
Assert.AreNotEqual ($"package:{proj.PackageName}", RunAdbCommand ($"shell pm list packages {proj.PackageName}").Trim (),
91+
$"{proj.PackageName} is installed on the device.");
92+
}
93+
}
94+
95+
[Test]
96+
public void SwitchConfigurationsShouldRedeploy ()
97+
{
98+
if (!CommercialBuildAvailable)
99+
Assert.Ignore ("Not required on Open Source Builds");
100+
101+
if (!HasDevices) {
102+
Assert.Ignore ("Test Skipped no devices or emulators found.");
103+
}
104+
105+
var proj = new XamarinAndroidApplicationProject () {
106+
IsRelease = false,
107+
};
108+
var abis = new string [] { "armeabi-v7a", "x86" };
109+
proj.SetProperty (KnownProperties.AndroidSupportedAbis, string.Join (";", abis));
110+
using (var builder = CreateApkBuilder (Path.Combine ("temp", TestContext.CurrentContext.Test.Name))) {
111+
builder.Verbosity = LoggerVerbosity.Diagnostic;
112+
Assert.IsTrue (builder.Build (proj));
113+
Assert.IsTrue (builder.Install (proj));
114+
Assert.AreEqual ($"package:{proj.PackageName}", RunAdbCommand ($"shell pm list packages {proj.PackageName}").Trim (),
115+
$"{proj.PackageName} is not installed on the device.");
116+
117+
var overrideDirs = new string [] {
118+
$"/storage/emulated/0/Android/data/{proj.PackageName}/files/.__override__",
119+
$"/mnt/shell/emulated/0/Android/data/{proj.PackageName}/files/.__override__",
120+
$"/storage/sdcard/Android/data/{proj.PackageName}/files/.__override__",
121+
};
122+
var directorylist = string.Empty;
123+
foreach (var dir in overrideDirs) {
124+
var listing = RunAdbCommand ($"shell ls {dir}");
125+
if (!listing.Contains ("No such file or directory"))
126+
directorylist += listing;
127+
}
128+
StringAssert.Contains ($"{proj.AssemblyName}", directorylist, $"{proj.AssemblyName} not found in fastdev directory.");
129+
130+
proj.IsRelease = true;
131+
Assert.IsTrue (builder.Build (proj));
132+
Assert.IsTrue (builder.Install (proj));
133+
Assert.AreEqual ($"package:{proj.PackageName}", RunAdbCommand ($"shell pm list packages {proj.PackageName}").Trim (),
134+
$"{proj.PackageName} is not installed on the device.");
135+
136+
directorylist = string.Empty;
137+
foreach (var dir in overrideDirs) {
138+
var listing = RunAdbCommand ($"shell ls {dir}");
139+
if (!listing.Contains ("No such file or directory"))
140+
directorylist += listing;
141+
}
142+
StringAssert.IsMatch ("", directorylist.Trim (), "fastdev directory should NOT exist for Release builds.");
143+
144+
proj.IsRelease = false;
145+
Assert.IsTrue (builder.Build (proj));
146+
Assert.IsTrue (builder.Install (proj));
147+
Assert.AreEqual ($"package:{proj.PackageName}", RunAdbCommand ($"shell pm list packages {proj.PackageName}").Trim (),
148+
$"{proj.PackageName} is not installed on the device.");
149+
directorylist = string.Empty;
150+
foreach (var dir in overrideDirs) {
151+
var listing = RunAdbCommand ($"shell ls {dir}");
152+
if (!listing.Contains ("No such file or directory"))
153+
directorylist += listing;
154+
}
155+
StringAssert.Contains ($"{proj.AssemblyName}", directorylist, $"{proj.AssemblyName} not found in fastdev directory.");
156+
157+
Assert.IsTrue (builder.Uninstall (proj));
158+
Assert.AreNotEqual ($"package:{proj.PackageName}", RunAdbCommand ($"shell pm list packages {proj.PackageName}").Trim (),
159+
$"{proj.PackageName} is installed on the device.");
160+
}
161+
}
162+
163+
[Test]
164+
public void InstallWithoutSharedRuntime ()
165+
{
166+
if (!CommercialBuildAvailable)
167+
Assert.Ignore ("Not required on Open Source Builds");
168+
169+
if (!HasDevices) {
170+
Assert.Ignore ("Test Skipped no devices or emulators found.");
171+
}
172+
173+
var proj = new XamarinAndroidApplicationProject () {
174+
IsRelease = true,
175+
};
176+
proj.SetProperty (proj.ReleaseProperties, "Optimize", false);
177+
proj.SetProperty (proj.ReleaseProperties, "DebugType", "none");
178+
proj.SetProperty (proj.ReleaseProperties, "AndroidUseSharedRuntime", false);
179+
proj.RemoveProperty (proj.ReleaseProperties, "EmbedAssembliesIntoApk");
180+
var abis = new string [] { "armeabi-v7a", "x86" };
181+
proj.SetProperty (KnownProperties.AndroidSupportedAbis, string.Join (";", abis));
182+
using (var builder = CreateApkBuilder (Path.Combine ("temp", TestContext.CurrentContext.Test.Name), false, false)) {
183+
builder.Verbosity = LoggerVerbosity.Diagnostic;
184+
if (RunAdbCommand ("shell pm list packages Mono.Android.DebugRuntime").Trim ().Length != 0)
185+
RunAdbCommand ("uninstall Mono.Android.DebugRuntime");
186+
Assert.IsTrue (builder.Install (proj));
187+
var runtimeInfo = builder.GetSupportedRuntimes ();
188+
var apkPath = Path.Combine (Root, builder.ProjectDirectory,
189+
proj.IntermediateOutputPath, "android", "bin", "UnnamedProject.UnnamedProject.apk");
190+
using (var apk = ZipHelper.OpenZip (apkPath)) {
191+
foreach (var abi in abis) {
192+
var runtime = runtimeInfo.FirstOrDefault (x => x.Abi == abi && x.Runtime == "debug");
193+
Assert.IsNotNull (runtime, "Could not find the expected runtime.");
194+
var inApk = ZipHelper.ReadFileFromZip (apk, String.Format ("lib/{0}/{1}", abi, runtime.Name));
195+
var inApkRuntime = runtimeInfo.FirstOrDefault (x => x.Abi == abi && x.Size == inApk.Length);
196+
Assert.IsNotNull (inApkRuntime, "Could not find the actual runtime used.");
197+
Assert.AreEqual (runtime.Size, inApkRuntime.Size, "expected {0} got {1}", "debug", inApkRuntime.Runtime);
198+
}
199+
}
200+
//FIXME: https://github.com/xamarin/androidtools/issues/141
201+
//Assert.AreEqual (0, RunAdbCommand ("shell pm list packages Mono.Android.DebugRuntime").Trim ().Length,
202+
// "The Shared Runtime should not have been installed.");
203+
var overrideDirs = new string [] {
204+
$"/data/data/{proj.PackageName}/files/.__override__",
205+
$"/storage/emulated/0/Android/data/{proj.PackageName}/files/.__override__",
206+
$"/mnt/shell/emulated/0/Android/data/{proj.PackageName}/files/.__override__",
207+
$"/storage/sdcard/Android/data/{proj.PackageName}/files/.__override__",
208+
};
209+
var directorylist = string.Empty;
210+
foreach (var dir in overrideDirs) {
211+
var listing = RunAdbCommand ($"shell ls {dir}");
212+
if (!listing.Contains ("No such file or directory"))
213+
directorylist += listing;
214+
}
215+
StringAssert.Contains ($"{proj.ProjectName}.dll", directorylist, $"{proj.ProjectName}.dll should exist in the .__override__ directory.");
216+
StringAssert.Contains ($"System.dll", directorylist, $"System.dll should exist in the .__override__ directory.");
217+
StringAssert.Contains ($"Mono.Android.dll", directorylist, $"Mono.Android.dll should exist in the .__override__ directory.");
218+
219+
}
220+
}
221+
222+
[Test]
223+
public void InstallErrorCode ()
224+
{
225+
if (!CommercialBuildAvailable)
226+
Assert.Ignore ("Not required on Open Source Builds");
227+
228+
if (!HasDevices) {
229+
Assert.Ignore ("Test Skipped no devices or emulators found.");
230+
}
231+
232+
//Setup a situation where we get INSTALL_FAILED_NO_MATCHING_ABIS
233+
var abi = "armeabi-v7a";
234+
var proj = new XamarinAndroidApplicationProject ();
235+
proj.SetProperty (proj.DebugProperties, "AndroidUseSharedRuntime", false);
236+
proj.SetProperty (proj.DebugProperties, "EmbedAssembliesIntoApk", true);
237+
proj.SetProperty (proj.DebugProperties, KnownProperties.AndroidSupportedAbis, abi);
238+
239+
using (var builder = CreateApkBuilder (Path.Combine ("temp", TestContext.CurrentContext.Test.Name))) {
240+
builder.ThrowOnBuildFailure = false;
241+
if (!builder.Install (proj)) {
242+
Assert.IsTrue (StringAssertEx.ContainsText (builder.LastBuildOutput, "ADB0020"), "Should receive ADB0020 error code.");
243+
} else {
244+
Assert.Ignore ($"Install should have failed, but we might have an {abi} emulator attached.");
245+
}
246+
}
247+
}
248+
249+
[Test]
250+
public void ToggleFastDev ()
251+
{
252+
if (!CommercialBuildAvailable)
253+
Assert.Ignore ("Not required on Open Source Builds");
254+
255+
if (!HasDevices) {
256+
Assert.Ignore ("Test Skipped no devices or emulators found.");
257+
}
258+
259+
var proj = new XamarinAndroidApplicationProject ();
260+
proj.SetProperty (proj.DebugProperties, "AndroidUseSharedRuntime", true);
261+
proj.SetProperty (proj.DebugProperties, "EmbedAssembliesIntoApk", false);
262+
263+
using (var builder = CreateApkBuilder (Path.Combine ("temp", TestContext.CurrentContext.Test.Name))) {
264+
Assert.IsTrue (builder.Install (proj), "Install should have succeeded.");
265+
266+
var overrideDirs = new string [] {
267+
$"/data/data/{proj.PackageName}/files/.__override__",
268+
$"/storage/emulated/0/Android/data/{proj.PackageName}/files/.__override__",
269+
$"/mnt/shell/emulated/0/Android/data/{proj.PackageName}/files/.__override__",
270+
$"/storage/sdcard/Android/data/{proj.PackageName}/files/.__override__",
271+
};
272+
var directorylist = string.Empty;
273+
foreach (var dir in overrideDirs) {
274+
var listing = RunAdbCommand ($"shell ls {dir}");
275+
if (!listing.Contains ("No such file or directory"))
276+
directorylist += listing;
277+
}
278+
StringAssert.Contains ($"{proj.ProjectName}.dll", directorylist, $"{proj.ProjectName}.dll should exist in the .__override__ directory.");
279+
280+
//Now toggle FastDev to OFF
281+
proj.SetProperty (proj.DebugProperties, "AndroidUseSharedRuntime", false);
282+
proj.SetProperty (proj.DebugProperties, "EmbedAssembliesIntoApk", true);
283+
var abis = new string [] { "armeabi-v7a", "x86" };
284+
proj.SetProperty (KnownProperties.AndroidSupportedAbis, string.Join (";", abis));
285+
286+
Assert.IsTrue (builder.Install (proj), "Second install should have succeeded.");
287+
288+
directorylist = string.Empty;
289+
foreach (var dir in overrideDirs) {
290+
var listing = RunAdbCommand ($"shell ls {dir}");
291+
if (!listing.Contains ("No such file or directory"))
292+
directorylist += listing;
293+
}
294+
295+
Assert.AreEqual ("", directorylist, "There should be no files in Fast Dev directories! Instead found: " + directorylist);
296+
297+
//Deploy one last time to verify install still works without the .__override__ directory existing
298+
Assert.IsTrue (builder.Install (proj), "Third install should have succeeded.");
299+
}
300+
}
301+
}
302+
}

0 commit comments

Comments
 (0)