-
Notifications
You must be signed in to change notification settings - Fork 533
/
DebuggingTest.cs
executable file
·404 lines (376 loc) · 15.4 KB
/
DebuggingTest.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Net;
using Mono.Debugging.Client;
using Mono.Debugging.Soft;
using NUnit.Framework;
using Xamarin.ProjectTools;
using System.Collections.Generic;
namespace Xamarin.Android.Build.Tests
{
[Category ("UsesDevices")]
public class DebuggingTest : DeviceTest {
[TearDown]
public void ClearDebugProperties ()
{
ClearDebugProperty ();
}
void SetTargetFrameworkAndManifest(XamarinAndroidApplicationProject proj, Builder builder)
{
string apiLevel;
proj.TargetFrameworkVersion = builder.LatestTargetFrameworkVersion (out apiLevel);
// TODO: We aren't sure how to support preview bindings in .NET6 yet.
if (Builder.UseDotNet && apiLevel == "31") {
apiLevel = "30";
proj.TargetFrameworkVersion = "v11.0";
}
proj.AndroidManifest = $@"<?xml version=""1.0"" encoding=""utf-8""?>
<manifest xmlns:android=""http://schemas.android.com/apk/res/android"" android:versionCode=""1"" android:versionName=""1.0"" package=""{proj.PackageName}"">
<uses-sdk android:minSdkVersion=""24"" android:targetSdkVersion=""{apiLevel}"" />
<application android:label=""${{PROJECT_NAME}}"">
</application >
</manifest>";
}
[Test]
public void ApplicationRunsWithoutDebugger ([Values (false, true)] bool isRelease, [Values (false, true)] bool extractNativeLibs)
{
AssertHasDevices ();
var proj = new XamarinFormsAndroidApplicationProject () {
IsRelease = isRelease,
};
if (isRelease || !CommercialBuildAvailable) {
proj.SetAndroidSupportedAbis ("armeabi-v7a", "x86");
}
proj.SetDefaultTargetDevice ();
using (var b = CreateApkBuilder (Path.Combine ("temp", TestName))) {
SetTargetFrameworkAndManifest (proj, b);
proj.AndroidManifest = proj.AndroidManifest.Replace ("<application ", $"<application android:extractNativeLibs=\"{extractNativeLibs.ToString ().ToLowerInvariant ()}\" ");
Assert.True (b.Install (proj), "Project should have installed.");
var manifest = Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath, "android", "AndroidManifest.xml");
AssertExtractNativeLibs (manifest, extractNativeLibs);
ClearAdbLogcat ();
b.BuildLogFile = "run.log";
if (CommercialBuildAvailable)
Assert.True (b.RunTarget (proj, "_Run"), "Project should have run.");
else
AdbStartActivity ($"{proj.PackageName}/{proj.JavaPackageName}.MainActivity");
Assert.True (WaitForActivityToStart (proj.PackageName, "MainActivity",
Path.Combine (Root, b.ProjectDirectory, "logcat.log"), 30), "Activity should have started.");
b.BuildLogFile = "uninstall.log";
Assert.True (b.Uninstall (proj), "Project should have uninstalled.");
}
}
[Test]
public void ClassLibraryMainLauncherRuns ()
{
AssertHasDevices ();
var path = Path.Combine ("temp", TestName);
var app = new XamarinAndroidApplicationProject {
ProjectName = "MyApp",
};
if (!CommercialBuildAvailable) {
app.SetAndroidSupportedAbis ("armeabi-v7a", "x86");
}
app.SetDefaultTargetDevice ();
var lib = new XamarinAndroidLibraryProject {
ProjectName = "MyLibrary"
};
lib.Sources.Add (new BuildItem.Source ("MainActivity.cs") {
TextContent = () => lib.ProcessSourceTemplate (app.DefaultMainActivity).Replace ("${JAVA_PACKAGENAME}", app.JavaPackageName),
});
lib.AndroidResources.Clear ();
foreach (var resource in app.AndroidResources) {
lib.AndroidResources.Add (resource);
}
var reference = $"..\\{lib.ProjectName}\\{lib.ProjectName}.csproj";
app.References.Add (new BuildItem.ProjectReference (reference, lib.ProjectName, lib.ProjectGuid));
// Remove the default MainActivity.cs & AndroidResources
app.AndroidResources.Clear ();
app.AndroidResources.Add (new AndroidItem.AndroidResource ("Resources\\layout\\foo.xml") {
TextContent = () => "<?xml version=\"1.0\" encoding=\"utf-8\" ?><LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\" />"
});
app.Sources.Remove (app.GetItem ("MainActivity.cs"));
using (var libBuilder = CreateDllBuilder (Path.Combine (path, lib.ProjectName)))
using (var appBuilder = CreateApkBuilder (Path.Combine (path, app.ProjectName))) {
SetTargetFrameworkAndManifest (app, appBuilder);
Assert.IsTrue (libBuilder.Build (lib), "library build should have succeeded.");
Assert.True (appBuilder.Install (app), "app should have installed.");
ClearAdbLogcat ();
appBuilder.BuildLogFile = "run.log";
if (CommercialBuildAvailable)
Assert.True (appBuilder.RunTarget (app, "_Run"), "Project should have run.");
else
AdbStartActivity ($"{app.PackageName}/{app.JavaPackageName}.MainActivity");
Assert.True (WaitForActivityToStart (app.PackageName, "MainActivity",
Path.Combine (Root, appBuilder.ProjectDirectory, "logcat.log"), 30), "Activity should have started.");
}
}
#pragma warning disable 414
static object [] DebuggerCustomAppTestCases = new object [] {
new object[] {
/* embedAssemblies */ true,
/* fastDevType */ "Assemblies",
/* activityStarts */ true,
},
new object[] {
/* embedAssemblies */ false,
/* fastDevType */ "Assemblies",
/* activityStarts */ true,
},
new object[] {
/* embedAssemblies */ true,
/* fastDevType */ "Assemblies:Dexes",
/* activityStarts */ true,
},
new object[] {
/* embedAssemblies */ false,
/* fastDevType */ "Assemblies:Dexes",
/* activityStarts */ false,
},
};
#pragma warning restore 414
[Test, Category ("Debugger")]
[TestCaseSource (nameof (DebuggerCustomAppTestCases))]
public void CustomApplicationRunsWithDebuggerAndBreaks (bool embedAssemblies, string fastDevType, bool activityStarts)
{
AssertCommercialBuild ();
AssertHasDevices ();
var proj = new XamarinAndroidApplicationProject () {
IsRelease = false,
AndroidFastDeploymentType = fastDevType,
};
proj.SetAndroidSupportedAbis ("armeabi-v7a", "x86");
proj.SetProperty ("EmbedAssembliesIntoApk", embedAssemblies.ToString ());
proj.SetDefaultTargetDevice ();
proj.Sources.Add (new BuildItem.Source ("MyApplication.cs") {
TextContent = () => proj.ProcessSourceTemplate (@"using System;
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Widget;
namespace ${ROOT_NAMESPACE} {
[Application]
public class MyApplication : Application {
public MyApplication (IntPtr handle, JniHandleOwnership jniHandle)
: base (handle, jniHandle)
{
}
public override void OnCreate ()
{
base.OnCreate ();
}
}
}
"),
});
using (var b = CreateApkBuilder (Path.Combine ("temp", TestName))) {
SetTargetFrameworkAndManifest (proj, b);
Assert.True (b.Install (proj), "Project should have installed.");
int breakcountHitCount = 0;
ManualResetEvent resetEvent = new ManualResetEvent (false);
var sw = new Stopwatch ();
// setup the debugger
var session = new SoftDebuggerSession ();
session.Breakpoints = new BreakpointStore {
{ Path.Combine (Root, b.ProjectDirectory, "MainActivity.cs"), 19 },
{ Path.Combine (Root, b.ProjectDirectory, "MyApplication.cs"), 17 },
};
session.TargetHitBreakpoint += (sender, e) => {
TestContext.WriteLine ($"BREAK {e.Type}, {e.Backtrace.GetFrame (0)}");
breakcountHitCount++;
session.Continue ();
};
var rnd = new Random ();
int port = rnd.Next (10000, 20000);
TestContext.Out.WriteLine ($"{port}");
var args = new SoftDebuggerConnectArgs ("", IPAddress.Loopback, port) {
MaxConnectionAttempts = 10,
};
var startInfo = new SoftDebuggerStartInfo (args) {
WorkingDirectory = Path.Combine (b.ProjectDirectory, proj.IntermediateOutputPath, "android", "assets"),
};
var options = new DebuggerSessionOptions () {
EvaluationOptions = EvaluationOptions.DefaultOptions,
};
options.EvaluationOptions.UseExternalTypeResolver = true;
ClearAdbLogcat ();
b.BuildLogFile = "run.log";
Assert.True (b.RunTarget (proj, "_Run", doNotCleanupOnUpdate: true, parameters: new string [] {
$"AndroidSdbTargetPort={port}",
$"AndroidSdbHostPort={port}",
"AndroidAttachDebugger=True",
}), "Project should have run.");
// do we expect the app to start?
Assert.AreEqual (activityStarts, WaitForDebuggerToStart (Path.Combine (Root, b.ProjectDirectory, "logcat.log")), "Activity should have started");
if (!activityStarts)
return;
// we need to give a bit of time for the debug server to start up.
WaitFor (2000);
session.LogWriter += (isStderr, text) => { Console.WriteLine (text); };
session.OutputWriter += (isStderr, text) => { Console.WriteLine (text); };
session.DebugWriter += (level, category, message) => { Console.WriteLine (message); };
session.Run (startInfo, options);
var expectedTime = TimeSpan.FromSeconds (1);
var actualTime = ProfileFor (() => session.IsConnected);
TestContext.Out.WriteLine ($"Debugger connected in {actualTime}");
Assert.LessOrEqual (actualTime, expectedTime, $"Debugger should have connected within {expectedTime} but it took {actualTime}.");
// we need to wait here for a while to allow the breakpoints to hit
// but we need to timeout
TimeSpan timeout = TimeSpan.FromSeconds (60);
while (session.IsConnected && breakcountHitCount < 2 && timeout >= TimeSpan.Zero) {
Thread.Sleep (10);
timeout = timeout.Subtract (TimeSpan.FromMilliseconds (10));
}
WaitFor (2000);
int expected = 2;
Assert.AreEqual (expected, breakcountHitCount, $"Should have hit {expected} breakpoints. Only hit {breakcountHitCount}");
b.BuildLogFile = "uninstall.log";
Assert.True (b.Uninstall (proj), "Project should have uninstalled.");
session.Exit ();
}
}
#pragma warning disable 414
static object [] DebuggerTestCases = new object [] {
new object[] {
/* embedAssemblies */ true,
/* fastDevType */ "Assemblies",
/* allowDeltaInstall */ false,
/* user */ null,
},
new object[] {
/* embedAssemblies */ false,
/* fastDevType */ "Assemblies",
/* allowDeltaInstall */ false,
/* user */ null,
},
new object[] {
/* embedAssemblies */ false,
/* fastDevType */ "Assemblies",
/* allowDeltaInstall */ true,
/* user */ null,
},
new object[] {
/* embedAssemblies */ false,
/* fastDevType */ "Assemblies:Dexes",
/* allowDeltaInstall */ false,
/* user */ null,
},
new object[] {
/* embedAssemblies */ false,
/* fastDevType */ "Assemblies:Dexes",
/* allowDeltaInstall */ true,
/* user */ null,
},
new object[] {
/* embedAssemblies */ true,
/* fastDevType */ "Assemblies",
/* allowDeltaInstall */ false,
/* user */ DeviceTest.GuestUserName,
},
new object[] {
/* embedAssemblies */ false,
/* fastDevType */ "Assemblies",
/* allowDeltaInstall */ false,
/* user */ DeviceTest.GuestUserName,
},
};
#pragma warning restore 414
[Test, Category ("SmokeTests"), Category ("Debugger")]
[TestCaseSource (nameof(DebuggerTestCases))]
public void ApplicationRunsWithDebuggerAndBreaks (bool embedAssemblies, string fastDevType, bool allowDeltaInstall, string username)
{
AssertCommercialBuild ();
AssertHasDevices ();
int userId = GetUserId (username);
List<string> parameters = new List<string> ();
if (userId >= 0)
parameters.Add ($"AndroidDeviceUserId={userId}");
if (SwitchUser (username)) {
WaitFor (5);
ClickButton ("", "android:id/button1", "Yes continue");
}
var proj = new XamarinFormsAndroidApplicationProject () {
IsRelease = false,
EmbedAssembliesIntoApk = embedAssemblies,
AndroidFastDeploymentType = fastDevType
};
proj.SetAndroidSupportedAbis ("armeabi-v7a", "x86");
proj.SetProperty (KnownProperties._AndroidAllowDeltaInstall, allowDeltaInstall.ToString ());
proj.SetDefaultTargetDevice ();
using (var b = CreateApkBuilder (Path.Combine ("temp", TestName))) {
SetTargetFrameworkAndManifest (proj, b);
Assert.True (b.Install (proj, parameters: parameters.ToArray ()), "Project should have installed.");
int breakcountHitCount = 0;
ManualResetEvent resetEvent = new ManualResetEvent (false);
var sw = new Stopwatch ();
// setup the debugger
var session = new SoftDebuggerSession ();
session.Breakpoints = new BreakpointStore {
{ Path.Combine (Root, b.ProjectDirectory, "MainActivity.cs"), 20 },
{ Path.Combine (Root, b.ProjectDirectory, "MainPage.xaml.cs"), 14 },
{ Path.Combine (Root, b.ProjectDirectory, "MainPage.xaml.cs"), 19 },
{ Path.Combine (Root, b.ProjectDirectory, "App.xaml.cs"), 12 },
};
session.TargetHitBreakpoint += (sender, e) => {
TestContext.WriteLine ($"BREAK {e.Type}, {e.Backtrace.GetFrame (0)}");
breakcountHitCount++;
session.Continue ();
};
var rnd = new Random ();
int port = rnd.Next (10000, 20000);
TestContext.Out.WriteLine ($"{port}");
var args = new SoftDebuggerConnectArgs ("", IPAddress.Loopback, port) {
MaxConnectionAttempts = 10,
};
var startInfo = new SoftDebuggerStartInfo (args) {
WorkingDirectory = Path.Combine (b.ProjectDirectory, proj.IntermediateOutputPath, "android", "assets"),
};
var options = new DebuggerSessionOptions () {
EvaluationOptions = EvaluationOptions.DefaultOptions,
};
options.EvaluationOptions.UseExternalTypeResolver = true;
ClearAdbLogcat ();
b.BuildLogFile = "run.log";
parameters.Add ($"AndroidSdbTargetPort={port}");
parameters.Add ($"AndroidSdbHostPort={port}");
parameters.Add ("AndroidAttachDebugger=True");
Assert.True (b.RunTarget (proj, "_Run", doNotCleanupOnUpdate: true,
parameters: parameters.ToArray ()), "Project should have run.");
Assert.IsTrue (WaitForDebuggerToStart (Path.Combine (Root, b.ProjectDirectory, "logcat.log")), "Activity should have started");
// we need to give a bit of time for the debug server to start up.
WaitFor (2000);
session.LogWriter += (isStderr, text) => { Console.WriteLine (text); };
session.OutputWriter += (isStderr, text) => { Console.WriteLine (text); };
session.DebugWriter += (level, category, message) => { Console.WriteLine (message); };
session.Run (startInfo, options);
WaitFor (TimeSpan.FromSeconds (30), () => session.IsConnected);
Assert.True (session.IsConnected, "Debugger should have connected but it did not.");
// we need to wait here for a while to allow the breakpoints to hit
// but we need to timeout
TimeSpan timeout = TimeSpan.FromSeconds (60);
int expected = 3;
while (session.IsConnected && breakcountHitCount < 3 && timeout >= TimeSpan.Zero) {
Thread.Sleep (10);
timeout = timeout.Subtract (TimeSpan.FromMilliseconds (10));
}
WaitFor (2000);
Assert.AreEqual (expected, breakcountHitCount, $"Should have hit {expected} breakpoints. Only hit {breakcountHitCount}");
breakcountHitCount = 0;
ClearAdbLogcat ();
ClickButton (proj.PackageName, "myXFButton", "CLICK ME");
while (session.IsConnected && breakcountHitCount < 1 && timeout >= TimeSpan.Zero) {
Thread.Sleep (10);
timeout = timeout.Subtract (TimeSpan.FromMilliseconds (10));
}
expected = 1;
Assert.AreEqual (expected, breakcountHitCount, $"Should have hit {expected} breakpoints. Only hit {breakcountHitCount}");
b.BuildLogFile = "uninstall.log";
Assert.True (b.Uninstall (proj), "Project should have uninstalled.");
session.Exit ();
}
}
}
}