-
Notifications
You must be signed in to change notification settings - Fork 566
Expand file tree
/
Copy pathEnvironmentContentTests.cs
More file actions
262 lines (231 loc) · 12.5 KB
/
EnvironmentContentTests.cs
File metadata and controls
262 lines (231 loc) · 12.5 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Microsoft.Build.Framework;
using Mono.Cecil;
using NUnit.Framework;
using Xamarin.ProjectTools;
using Xamarin.Android.Tasks;
namespace Xamarin.Android.Build.Tests
{
[Parallelizable (ParallelScope.Children)]
public class EnvironmentContentTests : BaseTest
{
[Test]
[NonParallelizable]
public void BuildApplicationWithMonoEnvironment ([Values ("", "Normal", "Offline")] string sequencePointsMode, [Values] AndroidRuntime runtime)
{
const bool isRelease = true;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
// TODO: NativeAOT fails all the tests, `MONO_DEBUG` is not found in the environment. Question is - should we fix it for backward compatibility,
// even though NativeAOT won't use it (and CoreCLR passes the tests), or should we just ignore this test for NativeAOT?
if (runtime == AndroidRuntime.NativeAOT) {
Assert.Ignore ("NativeAOT doesn't currently export the MONO_DEBUG environment variable");
}
string supportedAbis = runtime switch {
AndroidRuntime.MonoVM => "armeabi-v7a;x86",
AndroidRuntime.CoreCLR => "arm64-v8a;x86_64",
AndroidRuntime.NativeAOT => "arm64-v8a;x86_64",
_ => throw new NotSupportedException ($"Unsupported runtime '{runtime}'")
};
var lib = new XamarinAndroidLibraryProject {
ProjectName = "Library1",
IsRelease = isRelease,
OtherBuildItems = { new AndroidItem.AndroidEnvironment ("Mono.env") {
TextContent = () => "MONO_DEBUG=soft-breakpoints"
},
},
};
lib.SetRuntime (runtime);
var app = new XamarinFormsAndroidApplicationProject () {
IsRelease = isRelease,
AndroidLinkModeRelease = AndroidLinkMode.Full,
References = {
new BuildItem ("ProjectReference","..\\Library1\\Library1.csproj"),
},
};
app.SetRuntime (runtime);
//LinkSkip one assembly that contains __AndroidLibraryProjects__.zip
string linkSkip = "FormsViewGroup";
app.SetProperty ("AndroidLinkSkip", linkSkip);
app.SetProperty ("_AndroidSequencePointsMode", sequencePointsMode);
app.SetAndroidSupportedAbis (supportedAbis);
using (var libb = CreateDllBuilder (Path.Combine ("temp", TestName, lib.ProjectName)))
using (var appb = CreateApkBuilder (Path.Combine ("temp", TestName, app.ProjectName))) {
Assert.IsTrue (libb.Build (lib), "Library build should have succeeded.");
Assert.IsTrue (appb.Build (app), "App should have succeeded.");
string intermediateOutputDir = Path.Combine (Root, appb.ProjectDirectory, app.IntermediateOutputPath);
List<EnvironmentHelper.EnvironmentFile> envFiles = EnvironmentHelper.GatherEnvironmentFiles (intermediateOutputDir, supportedAbis, true, runtime);
Dictionary<string, string> envvars = EnvironmentHelper.ReadEnvironmentVariables (envFiles, runtime);
Assert.IsTrue (envvars.Count > 0, $"No environment variables defined");
string monoDebugVar;
Assert.IsTrue (envvars.TryGetValue ("MONO_DEBUG", out monoDebugVar), "Environment should contain MONO_DEBUG");
Assert.IsFalse (String.IsNullOrEmpty (monoDebugVar), "Environment must contain MONO_DEBUG with a value");
Assert.IsTrue (monoDebugVar.IndexOf ("soft-breakpoints", StringComparison.Ordinal) >= 0, "Environment must contain MONO_DEBUG with 'soft-breakpoints' in its value");
if (!String.IsNullOrEmpty (sequencePointsMode))
Assert.IsTrue (monoDebugVar.IndexOf ("gen-compact-seq-points", StringComparison.Ordinal) >= 0, "The values from Mono.env should have been merged into environment");
EnvironmentHelper.AssertValidEnvironmentSharedLibrary (intermediateOutputDir, AndroidSdkPath, AndroidNdkPath, supportedAbis, runtime);
var assemblyDir = Path.Combine (Root, appb.ProjectDirectory, app.IntermediateOutputPath, "android", "assets");
var rp = new ReaderParameters { ReadSymbols = false };
foreach (var assemblyFile in Directory.EnumerateFiles (assemblyDir, "*.dll")) {
using (var assembly = AssemblyDefinition.ReadAssembly (assemblyFile)) {
foreach (var module in assembly.Modules) {
var resources = module.Resources.Select (r => r.Name).ToArray ();
Assert.IsFalse (StringAssertEx.ContainsText (resources, "__AndroidEnvironment__"), "AndroidEnvironment EmbeddedResource should be stripped!");
Assert.IsFalse (StringAssertEx.ContainsText (resources, "__AndroidLibraryProjects__.zip"), "__AndroidLibraryProjects__.zip should be stripped!");
Assert.IsFalse (StringAssertEx.ContainsText (resources, "__AndroidNativeLibraries__.zip"), "__AndroidNativeLibraries__.zip should be stripped!");
}
}
}
}
}
[Test]
public void CheckMonoDebugIsAddedToEnvironment ([Values ("", "Normal", "Offline")] string sequencePointsMode)
{
const string supportedAbis = "armeabi-v7a;x86";
var proj = new XamarinAndroidApplicationProject () {
IsRelease = true,
};
// Mono-only test
proj.SetRuntime (AndroidRuntime.MonoVM);
proj.SetProperty ("_AndroidSequencePointsMode", sequencePointsMode);
proj.SetAndroidSupportedAbis (supportedAbis);
using (var b = CreateApkBuilder ()) {
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
string intermediateOutputDir = Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath);
List<EnvironmentHelper.EnvironmentFile> envFiles = EnvironmentHelper.GatherEnvironmentFiles (intermediateOutputDir, supportedAbis, true, AndroidRuntime.MonoVM);
Dictionary<string, string> envvars = EnvironmentHelper.ReadEnvironmentVariables (envFiles, AndroidRuntime.MonoVM);
Assert.IsTrue (envvars.Count > 0, $"No environment variables defined");
string monoDebugVar;
bool monoDebugVarFound = envvars.TryGetValue ("MONO_DEBUG", out monoDebugVar);
if (String.IsNullOrEmpty (sequencePointsMode))
Assert.IsFalse (monoDebugVarFound, $"environment should not contain MONO_DEBUG={monoDebugVar}");
else {
Assert.IsTrue (monoDebugVarFound, "environment should contain MONO_DEBUG");
Assert.AreEqual ("gen-compact-seq-points", monoDebugVar, "environment should contain MONO_DEBUG=gen-compact-seq-points");
}
EnvironmentHelper.AssertValidEnvironmentSharedLibrary (intermediateOutputDir, AndroidSdkPath, AndroidNdkPath, supportedAbis, AndroidRuntime.MonoVM);
}
}
[Test]
public void CheckConcurrentGC ()
{
var proj = new XamarinAndroidApplicationProject () {
IsRelease = true,
};
var gcVarName = "MONO_GC_PARAMS";
var expectedDefaultValue = "major=marksweep";
var expectedUpdatedValue = "major=marksweep-conc";
var supportedAbis = "armeabi-v7a;arm64-v8a";
// MonoVM-only test
proj.SetRuntime (Android.Tasks.AndroidRuntime.MonoVM);
proj.SetAndroidSupportedAbis (supportedAbis);
using (var b = CreateApkBuilder ()) {
proj.SetProperty ("AndroidEnableSGenConcurrent", "False");
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
var intermediateOutputDir = Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath);
// AndroidEnableSGenConcurrent=False by default
List<EnvironmentHelper.EnvironmentFile> envFiles = EnvironmentHelper.GatherEnvironmentFiles (intermediateOutputDir, supportedAbis, true, AndroidRuntime.MonoVM);
Dictionary<string, string> envvars = EnvironmentHelper.ReadEnvironmentVariables (envFiles, AndroidRuntime.MonoVM);
Assert.IsTrue (envvars.ContainsKey (gcVarName), $"Environment should contain '{gcVarName}'.");
Assert.AreEqual (expectedDefaultValue, envvars[gcVarName], $"'{gcVarName}' should have been '{expectedDefaultValue}' when concurrent GC is disabled.");
proj.SetProperty ("AndroidEnableSGenConcurrent", "True");
Assert.IsTrue (b.Build (proj), "Second build should have succeeded.");
envFiles = EnvironmentHelper.GatherEnvironmentFiles (intermediateOutputDir, supportedAbis, true, AndroidRuntime.MonoVM);
envvars = EnvironmentHelper.ReadEnvironmentVariables (envFiles, AndroidRuntime.MonoVM);
Assert.IsTrue (envvars.ContainsKey (gcVarName), $"Environment should contain '{gcVarName}'.");
Assert.AreEqual (expectedUpdatedValue, envvars[gcVarName], $"'{gcVarName}' should have been '{expectedUpdatedValue}' when concurrent GC is enabled.");
}
}
[Test]
public void CheckForInvalidHttpClientHandlerType ()
{
// Test with MonoVM only since NativeAOT will fail with XA1042 before reaching XA1031
const bool isRelease = true;
var runtime = AndroidRuntime.MonoVM;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var proj = new XamarinAndroidApplicationProject () {
IsRelease = isRelease,
};
proj.SetRuntime (runtime);
using (var b = CreateApkBuilder ()) {
b.ThrowOnBuildFailure = false;
proj.SetProperty ("AndroidHttpClientHandlerType", "Android.App.Application");
Assert.IsFalse (b.Build (proj), "Build should not have succeeded.");
Assert.IsTrue (StringAssertEx.ContainsText (b.LastBuildOutput, "XA1031"), "Output should contain XA1031");
// Also expect deprecation warning
Assert.IsTrue (StringAssertEx.ContainsText (b.LastBuildOutput, "XA1043"), "Output should contain XA1043 deprecation warning");
}
}
[Test]
public void CheckHttpClientHandlerType_NativeAOT_Error ()
{
// NativeAOT should fail with an error when AndroidHttpClientHandlerType is set
const bool isRelease = true;
var runtime = AndroidRuntime.NativeAOT;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var proj = new XamarinAndroidApplicationProject () {
IsRelease = isRelease,
};
proj.SetRuntime (runtime);
using (var b = CreateApkBuilder ()) {
b.ThrowOnBuildFailure = false;
proj.SetProperty ("AndroidHttpClientHandlerType", "Xamarin.Android.Net.AndroidMessageHandler");
Assert.IsFalse (b.Build (proj), "Build should not have succeeded for NativeAOT with AndroidHttpClientHandlerType set.");
Assert.IsTrue (StringAssertEx.ContainsText (b.LastBuildOutput, "XA1042"), "Output should contain XA1042");
}
}
[Test]
[TestCase (AndroidRuntime.MonoVM)]
[TestCase (AndroidRuntime.CoreCLR)]
public void CheckHttpClientHandlerType_DeprecationWarning (AndroidRuntime runtime)
{
bool isRelease = false;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var proj = new XamarinAndroidApplicationProject () {
IsRelease = isRelease,
};
var httpClientHandlerVarName = "XA_HTTP_CLIENT_HANDLER_TYPE";
var expectedDefaultValue = "System.Net.Http.SocketsHttpHandler, System.Net.Http";
var expectedUpdatedValue = "Xamarin.Android.Net.AndroidMessageHandler";
string supportedAbis = runtime switch {
AndroidRuntime.MonoVM => "armeabi-v7a;arm64-v8a",
AndroidRuntime.CoreCLR => "arm64-v8a;x86_64",
_ => throw new NotSupportedException ($"Unsupported runtime '{runtime}'")
};
proj.SetRuntime (runtime);
proj.SetAndroidSupportedAbis (supportedAbis);
proj.PackageReferences.Add (new Package() { Id = "System.Net.Http", Version = "*" });
proj.MainActivity = proj.DefaultMainActivity.Replace ("//${AFTER_ONCREATE}", "var _ = new System.Net.Http.HttpClient ();");
using (var b = CreateApkBuilder ()) {
proj.SetProperty ("AndroidHttpClientHandlerType", expectedDefaultValue);
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
// Expect deprecation warning XA1043
Assert.IsTrue (StringAssertEx.ContainsText (b.LastBuildOutput, "XA1043"), "Output should contain XA1043 deprecation warning");
var intermediateOutputDir = Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath);
List<EnvironmentHelper.EnvironmentFile>? envFiles = null;
Dictionary<string, string> envvars;
envFiles = EnvironmentHelper.GatherEnvironmentFiles (intermediateOutputDir, supportedAbis, true, runtime);
envvars = EnvironmentHelper.ReadEnvironmentVariables (envFiles, runtime);
Assert.IsTrue (envvars.ContainsKey (httpClientHandlerVarName), $"Environment should contain '{httpClientHandlerVarName}'.");
Assert.AreEqual (expectedDefaultValue, envvars[httpClientHandlerVarName]);
proj.SetProperty ("AndroidHttpClientHandlerType", expectedUpdatedValue);
Assert.IsTrue (b.Build (proj), "Second build should have succeeded.");
envFiles = EnvironmentHelper.GatherEnvironmentFiles (intermediateOutputDir, supportedAbis, true, runtime);
envvars = EnvironmentHelper.ReadEnvironmentVariables (envFiles, runtime);
Assert.IsTrue (envvars.ContainsKey (httpClientHandlerVarName), $"Environment should contain '{httpClientHandlerVarName}'.");
Assert.AreEqual (expectedUpdatedValue, envvars[httpClientHandlerVarName]);
}
}
}
}