-
Notifications
You must be signed in to change notification settings - Fork 533
/
AndroidDependenciesTests.cs
228 lines (209 loc) · 10.8 KB
/
AndroidDependenciesTests.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using NUnit.Framework;
using Xamarin.Android.Tools;
using Xamarin.ProjectTools;
using Microsoft.Build.Framework;
namespace Xamarin.Android.Build.Tests
{
[TestFixture]
[Parallelizable (ParallelScope.Children)]
public class AndroidDependenciesTests : BaseTest
{
[Test]
[NonParallelizable] // Do not run environment modifying tests in parallel.
public void InstallAndroidDependenciesTest ([Values ("GoogleV2", "Xamarin")] string manifestType)
{
// Set to true when we are marking a new Android API level as stable, but it has not
// been added to the Xamarin manifest yet.
var xamarin_manifest_needs_updating = false;
AssertCommercialBuild ();
var oldSdkPath = Environment.GetEnvironmentVariable ("TEST_ANDROID_SDK_PATH");
var oldJdkPath = Environment.GetEnvironmentVariable ("TEST_ANDROID_JDK_PATH");
try {
string sdkPath = Path.Combine (Root, "temp", TestName, "android-sdk");
string jdkPath = Path.Combine (Root, "temp", TestName, "android-jdk");
Environment.SetEnvironmentVariable ("TEST_ANDROID_SDK_PATH", sdkPath);
Environment.SetEnvironmentVariable ("TEST_ANDROID_JDK_PATH", jdkPath);
foreach (var path in new [] { sdkPath, jdkPath }) {
if (Directory.Exists (path))
Directory.Delete (path, recursive: true);
Directory.CreateDirectory (path);
}
var proj = new XamarinAndroidApplicationProject ();
var buildArgs = new List<string> {
"AcceptAndroidSDKLicenses=true",
$"AndroidManifestType={manifestType}",
};
// When using the default Xamarin manifest, this test should fail if we can't install any of the defaults in Xamarin.Android.Tools.Versions.props
// When using the Google manifest, override the platform tools version to the one in their manifest as it only ever contains one version
if (manifestType == "GoogleV2") {
buildArgs.Add ($"AndroidSdkPlatformToolsVersion={GetCurrentPlatformToolsVersion ()}");
}
using (var b = CreateApkBuilder ()) {
b.Verbosity = LoggerVerbosity.Detailed;
b.CleanupAfterSuccessfulBuild = false;
string defaultTarget = b.Target;
b.Target = "InstallAndroidDependencies";
b.BuildLogFile = "install-deps.log";
Assert.IsTrue (b.Build (proj, parameters: buildArgs.ToArray ()), "InstallAndroidDependencies should have succeeded.");
// When dependencies can not be resolved/installed a warning will be present in build output:
// Dependency `platform-tools` should have been installed but could not be resolved.
var depFailedMessage = "should have been installed but could not be resolved";
bool failedToInstall = b.LastBuildOutput.ContainsText (depFailedMessage);
// If we don't think the Xamarin manifest has been updated to contain the new API level:
// - Don't error if we got the expected failure
// - Error if didn't get a failure, because we need to update this test
if (manifestType == "Xamarin" && xamarin_manifest_needs_updating) {
if (!failedToInstall)
Assert.Fail ("We didn't expect the Xamarin manifest to have the requested component. If the manifest has been updated, change 'InstallAndroidDependenciesTest.xamarin_manifest_needs_updating' to be 'false'. ");
return;
}
if (failedToInstall) {
var sb = new StringBuilder ();
foreach (var line in b.LastBuildOutput) {
if (line.Contains (depFailedMessage)) {
sb.AppendLine (line);
}
}
Assert.Fail ($"A required dependency was not installed, warnings are listed below. Please check the task output in 'install-deps.log'.\n{sb.ToString ()}");
}
b.Target = defaultTarget;
b.BuildLogFile = "build.log";
Assert.IsTrue (b.Build (proj, true), "build should have succeeded.");
Assert.IsTrue ( b.LastBuildOutput.ContainsText ($"Output Property: _AndroidSdkDirectory={sdkPath}"),
$"_AndroidSdkDirectory was not set to new SDK path `{sdkPath}`. Please check the task output in 'install-deps.log'");
Assert.IsTrue (b.LastBuildOutput.ContainsText ($"Output Property: _JavaSdkDirectory={jdkPath}"),
$"_JavaSdkDirectory was not set to new JDK path `{jdkPath}`. Please check the task output in 'install-deps.log'");
Assert.IsTrue (b.LastBuildOutput.ContainsText ($"JavaPlatformJarPath={sdkPath}"),
$"JavaPlatformJarPath did not contain new SDK path `{sdkPath}`. Please check the task output in 'install-deps.log'");
}
} finally {
Environment.SetEnvironmentVariable ("TEST_ANDROID_SDK_PATH", oldSdkPath);
Environment.SetEnvironmentVariable ("TEST_ANDROID_JDK_PATH", oldJdkPath);
}
}
static string GetCurrentPlatformToolsVersion ()
{
var s = new XmlReaderSettings {
XmlResolver = null,
};
var r = XmlReader.Create ("https://dl-ssl.google.com/android/repository/repository2-3.xml", s);
var d = XDocument.Load (r);
var platformToolsPackage = d.Root.Elements ("remotePackage")
.Where (e => "platform-tools" == (string) e.Attribute("path"))
.FirstOrDefault ();
var revision = platformToolsPackage.Element ("revision");
return $"{revision.Element ("major")?.Value}.{revision.Element ("minor")?.Value}.{revision.Element ("micro")?.Value}";
}
[Test]
[TestCase ("AotAssemblies", false)]
[TestCase ("AndroidEnableProfiledAot", false)]
[TestCase ("EnableLLVM", true)]
public void GetDependencyNdkRequiredConditions (string property, bool ndkRequired)
{
var proj = new XamarinAndroidApplicationProject ();
proj.AotAssemblies = true;
proj.SetProperty (property, "true");
using (var builder = CreateApkBuilder ()) {
builder.Verbosity = LoggerVerbosity.Detailed;
builder.Target = "GetAndroidDependencies";
Assert.IsTrue (builder.Build (proj), "Build should have succeeded.");
IEnumerable<string> taskOutput = builder.LastBuildOutput
.Select (x => x.Trim ())
.SkipWhile (x => !x.StartsWith ("Task \"CalculateProjectDependencies\"", StringComparison.Ordinal))
.SkipWhile (x => !x.StartsWith ("Output Item(s):", StringComparison.Ordinal))
.TakeWhile (x => !x.StartsWith ("Done executing task \"CalculateProjectDependencies\"", StringComparison.Ordinal));
if (ndkRequired)
StringAssertEx.Contains ("ndk-bundle", taskOutput, "ndk-bundle should be a dependency.");
else
StringAssertEx.DoesNotContain ("ndk-bundle", taskOutput, "ndk-bundle should not be a dependency.");
}
}
[Test]
public void GetDependencyWhenBuildToolsAreMissingTest ()
{
var apis = new ApiInfo [] {
};
var path = Path.Combine ("temp", TestName);
var androidSdkPath = CreateFauxAndroidSdkDirectory (Path.Combine (path, "android-sdk"),
null, apis);
var referencesPath = CreateFauxReferencesDirectory (Path.Combine (path, "xbuild-frameworks"), apis);
var proj = new XamarinAndroidApplicationProject () {
IsRelease = true,
TargetSdkVersion = "26",
};
var parameters = new string [] {
$"TargetFrameworkRootPath={referencesPath}",
$"AndroidSdkDirectory={androidSdkPath}",
};
string buildToolsVersion = GetExpectedBuildToolsVersion ();
using (var builder = CreateApkBuilder (Path.Combine (path, proj.ProjectName), cleanupAfterSuccessfulBuild: false, cleanupOnDispose: false)) {
builder.Verbosity = LoggerVerbosity.Detailed;
builder.ThrowOnBuildFailure = false;
builder.Target = "GetAndroidDependencies";
Assert.True (builder.Build (proj, parameters: parameters),
string.Format ("First Build should have succeeded"));
int apiLevel = XABuildConfig.AndroidDefaultTargetDotnetApiLevel;
StringAssertEx.Contains ($"platforms/android-{apiLevel}", builder.LastBuildOutput, $"platforms/android-{apiLevel} should be a dependency.");
StringAssertEx.Contains ($"build-tools/{buildToolsVersion}", builder.LastBuildOutput, $"build-tools/{buildToolsVersion} should be a dependency.");
StringAssertEx.Contains ("platform-tools", builder.LastBuildOutput, "platform-tools should be a dependency.");
}
}
[Test]
public void GetDependencyWhenSDKIsMissingTest ([Values (true, false)] bool createSdkDirectory, [Values (true, false)] bool installJavaDeps)
{
var apis = new ApiInfo [] {
};
var path = Path.Combine ("temp", TestName);
var androidSdkPath = Path.Combine (path, "android-sdk");
if (createSdkDirectory)
Directory.CreateDirectory (androidSdkPath);
else if (Directory.Exists (androidSdkPath))
Directory.Delete (androidSdkPath, recursive: true);
var referencesPath = CreateFauxReferencesDirectory (Path.Combine (path, "xbuild-frameworks"), apis);
var proj = new XamarinAndroidApplicationProject () {
IsRelease = true,
TargetSdkVersion = "26",
};
var requestedJdkVersion = "17.0.8.1";
var parameters = new string [] {
$"TargetFrameworkRootPath={referencesPath}",
$"AndroidSdkDirectory={androidSdkPath}",
$"JavaSdkVersion={requestedJdkVersion}",
$"AndroidInstallJavaDependencies={installJavaDeps}",
};
string buildToolsVersion = GetExpectedBuildToolsVersion ();
using (var builder = CreateApkBuilder (Path.Combine (path, proj.ProjectName), cleanupAfterSuccessfulBuild: false, cleanupOnDispose: false)) {
builder.Verbosity = LoggerVerbosity.Detailed;
builder.ThrowOnBuildFailure = false;
builder.Target = "GetAndroidDependencies";
Assert.True (builder.Build (proj, parameters: parameters),
string.Format ("First Build should have succeeded"));
int apiLevel = XABuildConfig.AndroidDefaultTargetDotnetApiLevel;
StringAssertEx.Contains ($"platforms/android-{apiLevel}", builder.LastBuildOutput, $"platforms/android-{apiLevel} should be a dependency.");
StringAssertEx.Contains ($"build-tools/{buildToolsVersion}", builder.LastBuildOutput, $"build-tools/{buildToolsVersion} should be a dependency.");
StringAssertEx.Contains ("platform-tools", builder.LastBuildOutput, "platform-tools should be a dependency.");
if (installJavaDeps)
StringAssertEx.ContainsRegex ($@"JavaDependency=\s*jdk\s*Version={requestedJdkVersion}", builder.LastBuildOutput, $"jdk {requestedJdkVersion} should be a dependency.");
else
StringAssertEx.DoesNotContainRegex ($@"JavaDependency=\s*jdk\s*Version={requestedJdkVersion}", builder.LastBuildOutput, $"jdk {requestedJdkVersion} should not be a dependency.");
}
}
static readonly XNamespace MSBuildXmlns = "http://schemas.microsoft.com/developer/msbuild/2003";
static string GetExpectedBuildToolsVersion ()
{
var propsPath = Path.Combine (XABuildPaths.TopDirectory, "src", "Xamarin.Android.Build.Tasks", "Xamarin.Android.Common.props.in");
var props = XElement.Load (propsPath);
var AndroidSdkBuildToolsVersion = props.Elements (MSBuildXmlns + "PropertyGroup")
.Elements (MSBuildXmlns + "AndroidSdkBuildToolsVersion")
.FirstOrDefault ();
return AndroidSdkBuildToolsVersion?.Value?.Trim ();
}
}
}