-
Notifications
You must be signed in to change notification settings - Fork 536
/
Copy pathDebuggingTest.cs
executable file
·520 lines (481 loc) · 19.5 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
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
{
[TestFixture]
[Category ("UsesDevice")]
public class DebuggingTest : DeviceTest {
[TearDown]
public void ClearDebugProperties ()
{
ClearDebugProperty ();
}
void SetTargetFrameworkAndManifest(XamarinAndroidApplicationProject proj, Builder builder)
{
builder.LatestTargetFrameworkVersion (out string apiLevel);
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>";
}
int FindTextInFile (string file, string text)
{
int lineNumber = 1;
foreach (var line in File.ReadAllLines (file)) {
if (line.Contains (text)) {
return lineNumber;
}
lineNumber++;
}
Console.WriteLine ($"Could not find '{text}' in '{file}'");
return -1;
}
[Test]
[Category ("Node-3")]
public void ApplicationRunsWithoutDebugger ([Values (false, true)] bool isRelease, [Values (false, true)] bool extractNativeLibs, [Values (false, true)] bool useEmbeddedDex)
{
AssertHasDevices ();
SwitchUser ();
var proj = new XamarinFormsAndroidApplicationProject () {
IsRelease = isRelease,
};
if (isRelease || !CommercialBuildAvailable) {
proj.SetAndroidSupportedAbis ("armeabi-v7a", "x86", "x86_64");
}
proj.SetDefaultTargetDevice ();
if (Builder.UseDotNet && isRelease) {
// bundle tool does NOT support embeddedDex files it seems.
useEmbeddedDex = false;
}
using (var b = CreateApkBuilder (Path.Combine ("temp", TestName))) {
SetTargetFrameworkAndManifest (proj, b);
proj.AndroidManifest = proj.AndroidManifest.Replace ("<application ", $"<application android:extractNativeLibs=\"{extractNativeLibs.ToString ().ToLowerInvariant ()}\" android:useEmbeddedDex=\"{useEmbeddedDex.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]
[Category ("Node-3")]
public void ClassLibraryMainLauncherRuns ([Values (true, false)] bool preloadAssemblies)
{
AssertHasDevices ();
SwitchUser ();
var path = Path.Combine ("temp", TestName);
var app = new XamarinAndroidApplicationProject {
ProjectName = "MyApp",
};
if (!CommercialBuildAvailable) {
app.SetAndroidSupportedAbis ("armeabi-v7a", "x86", "x86_64");
}
app.SetDefaultTargetDevice ();
app.SetProperty ("AndroidEnablePreloadAssemblies", preloadAssemblies.ToString ());
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""
android:layout_width=""fill_parent""
android:layout_height=""wrap_content""
/>"
});
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"), Category ("Node-4")]
[TestCaseSource (nameof (DebuggerCustomAppTestCases))]
[Retry(5)]
public void CustomApplicationRunsWithDebuggerAndBreaks (bool embedAssemblies, string fastDevType, bool activityStarts)
{
AssertCommercialBuild ();
AssertHasDevices ();
SwitchUser ();
var path = Path.Combine (Root, "temp", TestName);
if (Directory.Exists (path)) {
TestContext.Out.WriteLine ($"Deleting previous run at '{path}'");
Directory.Delete (path,recursive:true);
}
var proj = new XamarinAndroidApplicationProject () {
IsRelease = false,
AndroidFastDeploymentType = fastDevType,
};
proj.SetAndroidSupportedAbis ("armeabi-v7a", "x86", "x86_64");
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)) {
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 ();
string file = Path.Combine (Root, b.ProjectDirectory, "MainActivity.cs");
int line = FindTextInFile (file, "base.OnCreate (bundle);");
session.Breakpoints.Add (file, line);
file = Path.Combine (Root, b.ProjectDirectory, "MyApplication.cs");
line = FindTextInFile (file, "base.OnCreate ();");
session.Breakpoints.Add (file, line);
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 = 2000, // we need a long delay here to get a reliable connection
};
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.");
session.LogWriter += (isStderr, text) => { Console.WriteLine (text); };
session.OutputWriter += (isStderr, text) => { Console.WriteLine (text); };
session.DebugWriter += (level, category, message) => { Console.WriteLine (message); };
// do we expect the app to start?
Assert.AreEqual (activityStarts, WaitForDebuggerToStart (Path.Combine (Root, b.ProjectDirectory, "logcat.log")), "Debugger should have started");
if (!activityStarts)
return;
Assert.False (session.HasExited, "Target should not have exited.");
session.Run (startInfo, options);
var expectedTime = TimeSpan.FromSeconds (1);
var actualTime = ProfileFor (() => session.IsConnected);
Assert.True (session.IsConnected, "Debugger should have connected but it did not.");
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,
/* debugType */ "",
},
new object[] {
/* embedAssemblies */ true,
/* fastDevType */ "Assemblies",
/* allowDeltaInstall */ false,
/* user */ null,
/* debugType */ "full",
},
new object[] {
/* embedAssemblies */ false,
/* fastDevType */ "Assemblies",
/* allowDeltaInstall */ false,
/* user */ null,
/* debugType */ "",
},
new object[] {
/* embedAssemblies */ false,
/* fastDevType */ "Assemblies",
/* allowDeltaInstall */ true,
/* user */ null,
/* debugType */ "",
},
new object[] {
/* embedAssemblies */ false,
/* fastDevType */ "Assemblies:Dexes",
/* allowDeltaInstall */ false,
/* user */ null,
/* debugType */ "",
},
new object[] {
/* embedAssemblies */ false,
/* fastDevType */ "Assemblies:Dexes",
/* allowDeltaInstall */ true,
/* user */ null,
/* debugType */ "",
},
new object[] {
/* embedAssemblies */ true,
/* fastDevType */ "Assemblies",
/* allowDeltaInstall */ false,
/* user */ DeviceTest.GuestUserName,
/* debugType */ "",
},
new object[] {
/* embedAssemblies */ false,
/* fastDevType */ "Assemblies",
/* allowDeltaInstall */ false,
/* user */ DeviceTest.GuestUserName,
/* debugType */ "",
},
};
#pragma warning restore 414
[Test, Category ("SmokeTests"), Category ("Debugger"), Category ("Node-4")]
[TestCaseSource (nameof(DebuggerTestCases))]
[Retry (5)]
public void ApplicationRunsWithDebuggerAndBreaks (bool embedAssemblies, string fastDevType, bool allowDeltaInstall, string username, string debugType)
{
AssertCommercialBuild ();
AssertHasDevices ();
SwitchUser ();
WaitFor (5000);
var path = Path.Combine (Root, "temp", TestName);
if (Directory.Exists (path)) {
TestContext.Out.WriteLine ($"Deleting previous run at '{path}'");
Directory.Delete (path,recursive:true);
}
int userId = GetUserId (username);
List<string> parameters = new List<string> ();
if (userId >= 0)
parameters.Add ($"AndroidDeviceUserId={userId}");
if (SwitchUser (username)) {
WaitFor (5000);
ClearBlockingDialogs ();
ClickButton ("", "android:id/button1", "Yes continue");
}
var lib = new XamarinAndroidLibraryProject {
ProjectName = "Library1",
Sources = {
new BuildItem.Source ("Foo.cs") {
TextContent = () =>
@"public class Foo
{
public Foo ()
{
}
}"
},
},
};
var app = new XamarinFormsAndroidApplicationProject {
ProjectName = "App",
IsRelease = false,
EmbedAssembliesIntoApk = embedAssemblies,
AndroidFastDeploymentType = fastDevType
};
app.MainPage = app.MainPage.Replace ("InitializeComponent ();", "InitializeComponent (); new Foo ();");
app.AddReference (lib);
app.SetAndroidSupportedAbis ("armeabi-v7a", "x86", "x86_64");
app.SetProperty (KnownProperties._AndroidAllowDeltaInstall, allowDeltaInstall.ToString ());
if (!string.IsNullOrEmpty (debugType)) {
app.SetProperty ("DebugType", debugType);
}
app.SetDefaultTargetDevice ();
using (var libBuilder = CreateDllBuilder (Path.Combine (path, lib.ProjectName)))
using (var appBuilder = CreateApkBuilder (Path.Combine (path, app.ProjectName))) {
Assert.True (libBuilder.Build (lib), "Library should have built.");
SetTargetFrameworkAndManifest (app, appBuilder);
Assert.True (appBuilder.Install (app, parameters: parameters.ToArray ()), "App should have installed.");
if (!embedAssemblies) {
// Check that we deployed app and framework .pdb files
StringAssertEx.ContainsRegex ($@"NotifySync CopyFile.+{app.ProjectName}\.pdb", appBuilder.LastBuildOutput,
$"{app.ProjectName}.pdb should be deployed!");
StringAssertEx.ContainsRegex ($@"NotifySync CopyFile.+{lib.ProjectName}\.pdb", appBuilder.LastBuildOutput,
$"{lib.ProjectName}.pdb should be deployed!");
StringAssertEx.ContainsRegex ($@"NotifySync CopyFile.+Mono.Android\.pdb", appBuilder.LastBuildOutput,
$"Mono.Android.pdb should be deployed!");
}
int breakcountHitCount = 0;
ManualResetEvent resetEvent = new ManualResetEvent (false);
var sw = new Stopwatch ();
// setup the debugger
var session = new SoftDebuggerSession ();
session.Breakpoints = new BreakpointStore ();
string file = Path.Combine (Root, appBuilder.ProjectDirectory, "MainActivity.cs");
int line = FindTextInFile (file, "base.OnCreate (savedInstanceState);");
session.Breakpoints.Add (file, line);
file = Path.Combine (Root, appBuilder.ProjectDirectory, "MainPage.xaml.cs");
line = FindTextInFile (file, "InitializeComponent ();");
session.Breakpoints.Add (file, line);
file = Path.Combine (Root, appBuilder.ProjectDirectory, "MainPage.xaml.cs");
line = FindTextInFile (file, "Console.WriteLine (");
session.Breakpoints.Add (file, line);
file = Path.Combine (Root, appBuilder.ProjectDirectory, "App.xaml.cs");
line = FindTextInFile (file, "InitializeComponent ();");
session.Breakpoints.Add (file, line);
file = Path.Combine (Root, libBuilder.ProjectDirectory, "Foo.cs");
line = FindTextInFile (file, "public Foo ()");
// Add one to the line so we get the '{' under the constructor
session.Breakpoints.Add (file, line++);
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 = 2000,
};
var startInfo = new SoftDebuggerStartInfo (args) {
WorkingDirectory = Path.Combine (appBuilder.ProjectDirectory, app.IntermediateOutputPath, "android", "assets"),
};
var options = new DebuggerSessionOptions () {
EvaluationOptions = EvaluationOptions.DefaultOptions,
};
options.EvaluationOptions.UseExternalTypeResolver = true;
ClearAdbLogcat ();
appBuilder.BuildLogFile = "run.log";
parameters.Add ($"AndroidSdbTargetPort={port}");
parameters.Add ($"AndroidSdbHostPort={port}");
parameters.Add ("AndroidAttachDebugger=True");
Assert.True (appBuilder.RunTarget (app, "_Run", doNotCleanupOnUpdate: true,
parameters: parameters.ToArray ()), "Project should have run.");
session.LogWriter += (isStderr, text) => {
TestContext.Out.WriteLine (text);
};
session.OutputWriter += (isStderr, text) => {
TestContext.Out.WriteLine (text);
};
session.DebugWriter += (level, category, message) => {
TestContext.Out.WriteLine (message);
};
Assert.IsTrue (WaitForDebuggerToStart (Path.Combine (Root, appBuilder.ProjectDirectory, "logcat.log")), "Debugger should have started");
session.Run (startInfo, options);
TestContext.Out.WriteLine ($"Detected debugger startup in log");
Assert.False (session.HasExited, "Target should not have exited.");
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
TestContext.Out.WriteLine ($"Debugger connected.");
TimeSpan timeout = TimeSpan.FromSeconds (60);
int expected = 4;
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 ();
ClearBlockingDialogs ();
Assert.True (ClickButton (app.PackageName, "myXFButton", "CLICK ME"), "Button should have been clicked!");
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}");
appBuilder.BuildLogFile = "uninstall.log";
Assert.True (appBuilder.Uninstall (app), "Project should have uninstalled.");
session.Exit ();
}
}
}
}