-
Notifications
You must be signed in to change notification settings - Fork 569
Expand file tree
/
Copy pathManagedResourceParserTests.cs
More file actions
744 lines (691 loc) · 33.9 KB
/
ManagedResourceParserTests.cs
File metadata and controls
744 lines (691 loc) · 33.9 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
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xamarin.ProjectTools;
using System.IO;
using System.Linq;
using Microsoft.Build.Framework;
using System.Text;
using Xamarin.Android.Tasks;
using Microsoft.Build.Utilities;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace Xamarin.Android.Build.Tests {
[TestFixture]
[Parallelizable (ParallelScope.Children)]
public class ManagedResourceParserTests : BaseTest {
const string ValuesXml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<resources>
<bool name=""a_bool"">false</bool>
<color name=""a_color"">#FFFFFFFF</color>
<integer name=""an_integer"">0</integer>
<integer-array name=""int_array"">
<item>0</item>
<item>1</item>
</integer-array>
<array name=""array_of_colors"">
<item>#FFFF0000</item>
<item>#FF00FF00</item>
<item>#FF0000FF</item>
</array>
</resources>
";
const string StringsXml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<resources xmlns:ns1=""http://schemas.android.com/tools"" xmlns:ns2=""urn:oasis:names:tc:xliff:document:1.2"">
<string name=""hello"">Hello World, Click Me!</string>
<string name=""app_name"">App1</string>
<plurals name=""num_locations_reported"">
<item quantity=""zero"">No location reported</item>
<item quantity=""one""> One location reported</item>
<item quantity=""other"">%d locations reported</item>
</plurals>
<string name=""translatable_text"" translatable=""false""><ns2:g example=""999"" id=""maximum number"">%1$d</ns2:g><ns2:g example=""+"" id=""suffix"">%2$s</ns2:g></string>
</resources>
";
const string StringsXml2 = @"<?xml version=""1.0"" encoding=""utf-8""?>
<resources>
<string name=""foo"">Hello World, Click Me!</string>
<string-array name=""widths_array"">
<item>Thin</item>
<item>Thinish</item>
<item>Medium</item>
<item>Thickish</item>
<item>Thick</item>
</string-array>
<string name=""menu_settings"">Android Beam settings</string>
<string name=""fixed""></string>
</resources>
";
const string Menu = @"<menu xmlns:android=""http://schemas.android.com/apk/res/android"">
<item android:id=""@+id/menu_settings""
android:icon=""@drawable/ic_menu_preferences""
android:showAsAction=""ifRoom""
android:title=""@string/menu_settings"" />
</menu>";
const string Animator = @"<?xml version=""1.0"" encoding=""utf-8""?>
<objectAnimator xmlns:android=""http://schemas.android.com/apk/res/android""
android:duration=""600""
android:interpolator=""@android:interpolator/fast_out_linear_in""
android:propertyName=""y""
android:valueFrom=""5000""
android:valueTo=""0""
android:valueType=""floatType"" />";
const string Animation = @"<?xml version=""1.0"" encoding=""utf-8""?>
<set xmlns:android=""http://schemas.android.com/apk/res/android"" android:shareInterpolator=""false"">
<alpha android:fromAlpha=""0.0"" android:toAlpha=""1.0"" />
</set>
";
const string Array = @"<?xml version=""1.0"" encoding=""utf-8""?>
<resources>
<array name=""alphabet"">
<item>A</item>
<item>Z</item>
</array>
</resources>
";
const string Dimen = @"<?xml version=""1.0"" encoding=""utf-8""?>
<resources>
<dimen name=""main_text_item_size"">17dp</dimen>
</resources>";
const string Styleable = @"<?xml version=""1.0"" encoding=""utf-8""?>
<resources>
<declare-styleable name=""CustomFonts"">
<attr name=""android:scrollX"" />
<attr name=""customFont"" />
<attr name=""customFont"" />
<attr name=""customFont1"" />
</declare-styleable>
<declare-styleable name=""MultiSelectListPreference"">
<attr name=""entries"">value</attr>
<attr name=""android:entries""/>
<attr name=""entryValues"">value</attr>
<attr name=""android:entryValues""/>
</declare-styleable>
<attr name=""customFont"" format=""enum"">
<enum name=""regular"" value=""0""/>
</attr>
<attr name=""customFont1"" format=""enum"">
<enum name=""regular"" value=""0""/>
</attr>
<attr name=""entries"" format=""enum"">
<enum name=""entry_1"" value=""0""/>
</attr>
<attr name=""entryValues"" format=""enum"">
<enum name=""entry_1_value"" value=""0""/>
</attr>
</resources>";
const string Styleablev21 = @"<?xml version=""1.0"" encoding=""utf-8""?>
<resources>
<declare-styleable name=""MultiSelectListPreference"">
<attr name=""entries"">value</attr>
<attr name=""android:entries""/>
<attr name=""entryValues"">value</attr>
<attr name=""android:entryValues""/>
</declare-styleable>
</resources>";
const string Selector = @"<?xml version=""1.0"" encoding=""utf-8""?>
<selector xmlns:android=""http://schemas.android.com/apk/res/android"">
<item android:state_pressed=""true"" android:color=""#ffff0000""/>
<item android:state_focused=""true"" android:color=""#ff0000ff""/>
<item android:color=""#ff000000""/>
</selector>
";
const string Transition = @"<?xml version=""1.0"" encoding=""utf-8""?>
<changeBounds
xmlns:android=""http://schemas.android.com/apk/res/android""
android:duration=""5000""
android:interpolator=""@android:anim/overshoot_interpolator"" />
";
const string Main = @"<?xml version=""1.0"" encoding=""utf-8""?>
<LinearLayout xmlns:android=""http://schemas.android.com/apk/res/android"">
<TextView android:id=""@+id/seekBar"" />
<TextView android:id=""@+id/seekbar"" />
<TextView android:id=""@+id/textview.withperiod"" />
<TextView android:id=""@+id/Føø-Bar"" />
</LinearLayout>
";
const string CustomId = @"<?xml version=""1.0"" encoding=""utf-8""?>
<LinearLayout xmlns:android=""http://schemas.android.com/apk/res/android"">
<TextView android:id=""@+MyCustomID/HelloWorldTextView""/>
<TextView android:id=""@+ACustomID/foo1""/>
</LinearLayout>
";
const string CustomInterpolator = @"<?xml version=""1.0"" encoding=""utf-8""?>
<accelerateInterpolator
xmlns:android=""http://schemas.android.com/apk/res/android""
android:factor=""2"" />
";
const string Xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<MyXml>
</MyXml>
";
const string AndroidManifest = @"<?xml version='1.0'?>
<manifest xmlns:android=""http://schemas.android.com/apk/res/android"" package=""MonoAndroidApplication4.MonoAndroidApplication4"" />";
/// <summary>
/// When you add new resources to the code blocks above, the following
/// Rtxt string needs to be updated with the correct values.
/// You can do this manually OR use aapt2
///
/// Run the GenerateDesignerFileFromRtxt test and let it fail.
/// You can then go to the appropriate resource directory
/// and run the following commands.
///
/// aapt2 compile -o compile.flata --dir res/
/// aapt2 compile -o lp.flata --dir lp/res/
/// aapt2 link -o foo.apk --manifest AndroidManifest.xml -R lp.flata -R compile.flata --auto-add-overlay --output-text-symbols R.txt -I ~/android-toolchain-xa/sdk/platforms/android-Q/android.jar
///
/// Then copy the values from the R.txt file and place them below.
/// </summary>
/// int ACustomID foo1 0x7f160000
/// int MyCustomID HelloWorldTextView 0x7f150000
const string Rtxt = @"int anim custominterpolator 0x7f010000
int anim some_animation 0x7f010001
int animator slide_in_bottom 0x7f020000
int array alphabet 0x7f030000
int array array_of_colors 0x7f030001
int array int_array 0x7f030002
int array widths_array 0x7f030003
int attr customFont 0x7f040000
int attr customFont1 0x7f040001
int attr entries 0x7f040002
int attr entryValues 0x7f040003
int bool a_bool 0x7f050000
int color a_color 0x7f060000
int color selector1 0x7f060001
int dimen main_text_item_size 0x7f070000
int drawable ic_menu_preferences 0x7f080000
int font arial 0x7f090000
int id Føø_Bar 0x7f0a0000
int id entry_1 0x7f0a0001
int id entry_1_value 0x7f0a0002
int id menu_settings 0x7f0a0003
int id regular 0x7f0a0004
int id seekBar 0x7f0a0005
int id seekbar 0x7f0a0006
int id textview_withperiod 0x7f0a0007
int integer an_integer 0x7f0b0000
int layout main 0x7f0c0000
int menu options 0x7f0d0000
int mipmap icon 0x7f0e0000
int plurals num_locations_reported 0x7f0f0000
int raw afoo 0x7f100000
int raw foo 0x7f100001
int string app_name 0x7f110000
int string fixed 0x7f110001
int string foo 0x7f110002
int string hello 0x7f110003
int string menu_settings 0x7f110004
int string translatable_text 0x7f110005
int[] styleable CustomFonts { 0x010100d2, 0x7f040000, 0x7f040000, 0x7f040001 }
int styleable CustomFonts_android_scrollX 0
int styleable CustomFonts_customFont 1
int styleable CustomFonts_customFont 2
int styleable CustomFonts_customFont1 3
int[] styleable MultiSelectListPreference { 0x010100b2, 0x010101f8, 0x7f040002, 0x7f040003 }
int styleable MultiSelectListPreference_android_entries 0
int styleable MultiSelectListPreference_android_entryValues 1
int styleable MultiSelectListPreference_entries 2
int styleable MultiSelectListPreference_entryValues 3
int transition transition 0x7f130000
int xml myxml 0x7f140000
";
public string AndroidSdkDirectory { get; set; } = AndroidSdkResolver.GetAndroidSdkPath ();
public void CreateResourceDirectory (string path)
{
Directory.CreateDirectory (Path.Combine (Root, path));
Directory.CreateDirectory (Path.Combine (Root, path, "res", "animator"));
Directory.CreateDirectory (Path.Combine (Root, path, "res", "anim"));
Directory.CreateDirectory (Path.Combine (Root, path, "res", "color"));
Directory.CreateDirectory (Path.Combine (Root, path, "res", "drawable"));
Directory.CreateDirectory (Path.Combine (Root, path, "res", "mipmap"));
Directory.CreateDirectory (Path.Combine (Root, path, "res", "menu"));
Directory.CreateDirectory (Path.Combine (Root, path, "res", "font"));
Directory.CreateDirectory (Path.Combine (Root, path, "res", "layout"));
Directory.CreateDirectory (Path.Combine (Root, path, "res", "values"));
Directory.CreateDirectory (Path.Combine (Root, path, "res", "values-v21"));
Directory.CreateDirectory (Path.Combine (Root, path, "res", "transition"));
Directory.CreateDirectory (Path.Combine (Root, path, "res", "raw"));
Directory.CreateDirectory (Path.Combine (Root, path, "res", "xml"));
File.WriteAllText (Path.Combine (Root, path, "AndroidManifest.xml"), AndroidManifest);
File.WriteAllText (Path.Combine (Root, path, "res", "color", "selector1.xml"), Selector);
File.WriteAllText (Path.Combine (Root, path, "res", "anim", "custominterpolator.xml"), CustomInterpolator);
File.WriteAllText (Path.Combine (Root, path, "res", "values", "arrays.xml"), Array);
File.WriteAllText (Path.Combine (Root, path, "res", "values", "values.xml"), ValuesXml);
File.WriteAllText (Path.Combine (Root, path, "res", "values", "strings.xml"), StringsXml);
File.WriteAllText (Path.Combine (Root, path, "res", "values", "attrs.xml"), Styleable);
File.WriteAllText (Path.Combine (Root, path, "res", "values-v21", "attrs.xml"), Styleablev21);
File.WriteAllText (Path.Combine (Root, path, "res", "transition", "transition.xml"), Transition);
File.WriteAllText (Path.Combine (Root, path, "res", "raw", "afoo.txt"), "AFoo");
File.WriteAllText (Path.Combine (Root, path, "res", "raw", "foo.txt"), "Foo");
File.WriteAllText (Path.Combine (Root, path, "res", "layout", "main.xml"), Main);
File.WriteAllText (Path.Combine (Root, path, "res", "xml", "myxml.xml"), Xml);
Directory.CreateDirectory (Path.Combine (Root, path, "lp", "res", "animator"));
Directory.CreateDirectory (Path.Combine (Root, path, "lp", "res", "anim"));
Directory.CreateDirectory (Path.Combine (Root, path, "lp", "res", "font"));
Directory.CreateDirectory (Path.Combine (Root, path, "lp", "res", "values"));
Directory.CreateDirectory (Path.Combine (Root, path, "lp", "res", "drawable"));
Directory.CreateDirectory (Path.Combine (Root, path, "lp", "res", "menu"));
Directory.CreateDirectory (Path.Combine (Root, path, "lp", "res", "mipmap-hdpi"));
File.WriteAllText (Path.Combine (Root, path, "lp", "res", "animator", "slide_in_bottom.xml"), Animator);
File.WriteAllText (Path.Combine (Root, path, "lp", "res", "anim", "some_animation.xml"), Animation);
File.WriteAllText (Path.Combine (Root, path, "lp", "res", "font", "arial.ttf"), "");
File.WriteAllText (Path.Combine (Root, path, "lp", "res", "values", "strings.xml"), StringsXml2);
File.WriteAllText (Path.Combine (Root, path, "lp", "res", "values", "dimen.xml"), Dimen);
var icon_binary_mdpi = XamarinAndroidCommonProject.GetResourceContents ("Xamarin.ProjectTools.Resources.Base.Icon.png");
File.WriteAllBytes (Path.Combine (Root, path, "lp", "res", "drawable", "ic_menu_preferences.png"), icon_binary_mdpi);
File.WriteAllBytes (Path.Combine (Root, path, "lp", "res", "mipmap-hdpi", "icon.png"), icon_binary_mdpi);
File.WriteAllText (Path.Combine (Root, path, "lp", "res", "menu", "options.xml"), Menu);
File.WriteAllText (Path.Combine (Root, path, "lp", "__res_name_case_map.txt"), "menu/Options.xml;menu/options.xml");
}
void BuildLibraryWithResources (string path, AndroidRuntime runtime)
{
bool isRelease = runtime == AndroidRuntime.NativeAOT;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var library = new XamarinAndroidLibraryProject () {
IsRelease = isRelease,
ProjectName = "Library",
};
library.SetRuntime (runtime);
var libraryStrings = library.AndroidResources.FirstOrDefault (r => r.Include () == @"Resources\values\Strings.xml");
library.SetProperty ("AndroidUseDesignerAssembly", "false");
library.AndroidResources.Clear ();
library.AndroidResources.Add (libraryStrings);
library.AndroidResources.Add (new AndroidItem.AndroidResource (Path.Combine ("Resources", "animator", "slide_in_bottom.xml")) { TextContent = () => Animator });
library.AndroidResources.Add (new AndroidItem.AndroidResource (Path.Combine ("Resources", "font", "arial.ttf")) { TextContent = () => "" });
library.AndroidResources.Add (new AndroidItem.AndroidResource (Path.Combine ("Resources", "values", "strings2.xml")) { TextContent = () => StringsXml2 });
library.AndroidResources.Add (new AndroidItem.AndroidResource (Path.Combine ("Resources", "values", "dimen.xml")) { TextContent = () => Dimen });
var icon_binary_mdpi = XamarinAndroidCommonProject.GetResourceContents ("Xamarin.ProjectTools.Resources.Base.Icon.png");
library.AndroidResources.Add (new AndroidItem.AndroidResource (Path.Combine ("Resources", "drawable", "ic_menu_preferences.png")) { BinaryContent = () => icon_binary_mdpi });
library.AndroidResources.Add (new AndroidItem.AndroidResource (Path.Combine ("Resources", "mipmap-hdpi", "icon.png")) { BinaryContent = () => icon_binary_mdpi });
library.AndroidResources.Add (new AndroidItem.AndroidResource (Path.Combine ("Resources", "menu", "options.xml")) { TextContent = () => Menu });
using (ProjectBuilder builder = CreateDllBuilder (Path.Combine (Root, path))) {
Assert.IsTrue (builder.Build (library), "Build should have succeeded");
}
}
void CompareFilesIgnoreRuntimeInfoString (string file1, string file2)
{
FileAssert.Exists (file1);
FileAssert.Exists (file2);
var content1 = File.ReadAllText (file1);
var content2 = File.ReadAllText (file2);
// This string is only generated when running on mono, replace with a new line that will be stripped when comparing.
var runtimeVersionRegex = new Regex (@"//\s*Runtime Version:.*");
content1 = runtimeVersionRegex.Replace (content1, Environment.NewLine);
content2 = runtimeVersionRegex.Replace (content2, Environment.NewLine);
var replaceText = "\t[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"Xamarin.Android.Build.Tasks\", \"1.0.0.0\")]";
var assemblyVersionRegex = new Regex (@"(.*)GeneratedCodeAttribute(.{2})Xamarin.Android.Build.Tasks(.*)");
content1 = assemblyVersionRegex.Replace (content1, replaceText);
content2 = assemblyVersionRegex.Replace (content2, replaceText);
using (var s1 = new MemoryStream (Encoding.UTF8.GetBytes (content1)))
using (var s2 = new MemoryStream (Encoding.UTF8.GetBytes (content2))) {
if (!StreamCompare (s1, s2)) {
TestContext.AddTestAttachment (file1, Path.GetFileName (file1));
TestContext.AddTestAttachment (file2, Path.GetFileName (file2));
Assert.Fail ($"{file1} and {file2} do not match.");
}
}
}
GenerateResourceCaseMap CreateCaseMapTask (string path)
{
var task = new GenerateResourceCaseMap () {
BuildEngine = new MockBuildEngine (TestContext.Out)
};
task.ProjectDir = Path.Combine (Root, path);
task.ResourceDirectory = Path.Combine (Root, path, "res") + Path.DirectorySeparatorChar;
task.Resources = new TaskItem [] {
new TaskItem (Path.Combine (Root, path, "res", "values", "strings.xml"), new Dictionary<string, string> () {
{ "LogicalName", "values\\strings.xml" },
}),
};
task.AdditionalResourceDirectories = new TaskItem [] {
new TaskItem (Path.Combine (Root, path, "lp", "res")),
};
task.OutputFile = new TaskItem (Path.Combine (Root, path, "case_map.txt"));
return task;
}
GenerateResourceDesigner CreateTask (string path)
{
var task = new GenerateResourceDesigner {
BuildEngine = new MockBuildEngine (TestContext.Out)
};
task.UseManagedResourceGenerator = true;
task.DesignTimeBuild = true;
task.Namespace = "Foo.Foo";
task.NetResgenOutputFile = Path.Combine (Root, path, "Resource.designer.cs");
task.DesignTimeOutputFile = Path.Combine (Root, path, "designtime", "Resource.designer.cs");
task.ProjectDir = Path.Combine (Root, path);
task.ResourceDirectory = Path.Combine (Root, path, "res") + Path.DirectorySeparatorChar;
task.Resources = new TaskItem [] {
new TaskItem (Path.Combine (Root, path, "res", "values", "strings.xml"), new Dictionary<string, string> () {
{ "LogicalName", "values\\strings.xml" },
}),
};
task.AdditionalResourceDirectories = new TaskItem [] {
new TaskItem (Path.Combine (Root, path, "lp", "res")),
};
task.CaseMapFile = Path.Combine (Root, path, "case_map.txt");
task.IsApplication = true;
var platform = AndroidSdkResolver.GetMaxInstalledPlatform ();
task.JavaPlatformJarPath = Path.Combine (AndroidSdkDirectory, "platforms", $"android-{platform.Major}", "android.jar");
return task;
}
static string ExpectedOutputDir = Path.Combine (XABuildPaths.TestAssemblyOutputDirectory, "Expected");
void AssertResourceDesigner (GenerateResourceDesigner task, string expectedFile)
{
var expected = Path.Combine (ExpectedOutputDir, expectedFile);
FileAssert.Exists (task.NetResgenOutputFile);
CompareFilesIgnoreRuntimeInfoString (task.NetResgenOutputFile, expected);
if (!string.IsNullOrEmpty (task.DesignTimeOutputFile)) {
FileAssert.Exists (task.DesignTimeOutputFile);
CompareFilesIgnoreRuntimeInfoString (task.DesignTimeOutputFile, expected);
}
}
[Test]
public void GenerateDesignerFileWithÜmläüts ()
{
var path = Path.Combine ("temp", TestName + " Some Space");
CreateResourceDirectory (path);
var mapTask = CreateCaseMapTask (path);
Assert.IsTrue (mapTask.Execute (), "Map Task should have executed successfully.");
var task = CreateTask (path);
Assert.IsTrue (task.Execute (), "Task should have executed successfully.");
AssertResourceDesigner (task, "GenerateDesignerFileExpected.cs");
Directory.Delete (Path.Combine (Root, path), recursive: true);
}
[Test]
public void GenerateDesignerFileFromRtxt ([Values] bool withLibraryReference, [Values] AndroidRuntime runtime)
{
// TODO: fix NativeAOT, it currently fails with:
//
// bin/TestDebug/temp/GenerateDesignerFileFromRtxtTrueNativeAOT Some Space/Resource.designer.cs and bin/TestDebug/Expected/GenerateDesignerFileWithLibraryReferenceExpected.cs do not match.
if (runtime == AndroidRuntime.NativeAOT) {
Assert.Ignore ("NativeAOT currently doesn't work with this test.");
}
var path = Path.Combine ("temp", TestName + " Some Space");
CreateResourceDirectory (path);
var mapTask = CreateCaseMapTask (path);
Assert.IsTrue (mapTask.Execute (), "Map Task should have executed successfully.");
var task = CreateTask (path);
task.RTxtFile = Path.Combine (Root, path, "R.txt");
File.WriteAllText (task.RTxtFile, Rtxt);
if (withLibraryReference) {
var libraryPath = Path.Combine (path, "Library");
BuildLibraryWithResources (libraryPath, runtime);
task.References = new TaskItem [] {
new TaskItem (Path.Combine (Root, libraryPath, "bin", "Debug", "Library.dll"))
};
}
Assert.IsTrue (task.Execute (), "Task should have executed successfully.");
AssertResourceDesigner (task, withLibraryReference ? "GenerateDesignerFileWithLibraryReferenceExpected.cs" : "GenerateDesignerFileExpected.cs");
Directory.Delete (Path.Combine (Root, path), recursive: true);
}
[Test]
public void GenerateDesignerFileFromEmptyRtxt ()
{
var path = Path.Combine ("temp", TestName + " Some Space");
CreateResourceDirectory (path);
var task = CreateTask (path);
task.RTxtFile = Path.Combine (Root, path, "R.txt");
File.WriteAllText (task.RTxtFile, Environment.NewLine);
Assert.IsTrue (task.Execute (), "Task should have executed successfully.");
Assert.IsTrue (File.Exists (task.NetResgenOutputFile), $"{task.NetResgenOutputFile} should have been created.");
Directory.Delete (Path.Combine (Root, path), recursive: true);
}
[Test]
public void UpdateLayoutIdIsIncludedInDesigner ([Values(true, false)] bool useRtxt)
{
var path = Path.Combine ("temp", TestName + " Some Space");
CreateResourceDirectory (path);
var mapTask = CreateCaseMapTask (path);
Assert.IsTrue (mapTask.Execute (), "Map Task should have executed successfully.");
if (useRtxt)
File.WriteAllText (Path.Combine (Root, path, "R.txt"), Rtxt);
IBuildEngine engine = new MockBuildEngine (TestContext.Out);
var task = new GenerateResourceDesigner {
BuildEngine = engine
};
task.UseManagedResourceGenerator = true;
task.DesignTimeBuild = true;
task.Namespace = "Foo.Foo";
task.NetResgenOutputFile = Path.Combine (Root, path, "Resource.designer.cs");
task.ProjectDir = Path.Combine (Root, path);
task.ResourceDirectory = Path.Combine (Root, path, "res") + Path.DirectorySeparatorChar;
task.Resources = new TaskItem [] {
new TaskItem (Path.Combine (Root, path, "res", "values", "strings.xml"), new Dictionary<string, string> () {
{ "LogicalName", "values\\strings.xml" },
}),
};
task.AdditionalResourceDirectories = new TaskItem [] {
new TaskItem (Path.Combine (Root, path, "lp", "res")),
};
task.ResourceFlagFile = Path.Combine (Root, path, "AndroidResgen.flag");
task.CaseMapFile = Path.Combine (Root, path, "case_map.txt");
File.WriteAllText (task.ResourceFlagFile, string.Empty);
task.IsApplication = true;
var platform = AndroidSdkResolver.GetMaxInstalledPlatform ();
task.JavaPlatformJarPath = Path.Combine (AndroidSdkDirectory, "platforms", $"android-{platform.Major}", "android.jar");
Assert.IsTrue (task.Execute (), "Task should have executed successfully.");
Assert.IsTrue (File.Exists (task.NetResgenOutputFile), $"{task.NetResgenOutputFile} should have been created.");
var expected = Path.Combine (ExpectedOutputDir, "GenerateDesignerFileExpected.cs");
CompareFilesIgnoreRuntimeInfoString (task.NetResgenOutputFile, expected);
// Update the id, and force the managed parser to re-parse the output
File.WriteAllText (Path.Combine (Root, path, "res", "layout", "main.xml"), Main.Replace ("@+id/textview.withperiod", "@+id/textview.withperiod2"));
File.SetLastWriteTimeUtc (task.ResourceFlagFile, DateTime.UtcNow);
Assert.IsTrue (task.Execute (), "Task should have executed successfully.");
Assert.IsTrue (File.Exists (task.NetResgenOutputFile), $"{task.NetResgenOutputFile} should have been created.");
var data = File.ReadAllText (expected);
var expectedWithNewId = Path.Combine (Root, path, "GenerateDesignerFileExpectedWithNewId.cs");
File.WriteAllText (expectedWithNewId, data.Replace ("withperiod", "withperiod2"));
CompareFilesIgnoreRuntimeInfoString (task.NetResgenOutputFile, expectedWithNewId);
Directory.Delete (Path.Combine (Root, path), recursive: true);
}
[Test]
public void RtxtGeneratorOutput ()
{
var path = Path.Combine ("temp", TestName);
var platform = AndroidSdkResolver.GetMaxInstalledPlatform ();
string resPath = Path.Combine (Root, path, "res");
string rTxt = Path.Combine (Root, path, "R.txt");
string expectedrTxt = Path.Combine (Root, path, "expectedR.txt");
CreateResourceDirectory (path);
File.WriteAllText (expectedrTxt, Rtxt);
List<BuildErrorEventArgs> errors = new List<BuildErrorEventArgs> ();
List<BuildMessageEventArgs> messages = new List<BuildMessageEventArgs> ();
IBuildEngine engine = new MockBuildEngine (TestContext.Out, errors: errors, messages: messages);
var generateRtxt = new GenerateRtxt () {
BuildEngine = engine,
RTxtFile = rTxt,
ResourceDirectory = resPath,
CaseMapFile = Path.Combine (Root, path, "case_map.txt"),
JavaPlatformJarPath = Path.Combine (AndroidSdkDirectory, "platforms", $"android-{platform.Major}", "android.jar"),
ResourceFlagFile = Path.Combine (Root, path, "res.flag"),
AdditionalResourceDirectories = new string[] {
Path.Combine (Root, path, "lp", "res"),
},
};
Assert.IsTrue (generateRtxt.Execute (), "Task should have succeeded.");
FileAssert.Exists (rTxt, $"{rTxt} should have been created.");
CompareFilesIgnoreRuntimeInfoString (rTxt, expectedrTxt);
Directory.Delete (Path.Combine (Root, path), recursive: true);
}
[Test]
public void CompareAapt2AndManagedParserOutput ()
{
var path = Path.Combine ("temp", TestName);
CreateResourceDirectory (path);
File.WriteAllText (Path.Combine (Root, path, "foo.map"), @"a\nb");
Directory.CreateDirectory (Path.Combine (Root, path, "java"));
List<BuildErrorEventArgs> errors = new List<BuildErrorEventArgs> ();
List<BuildMessageEventArgs> messages = new List<BuildMessageEventArgs> ();
IBuildEngine engine = new MockBuildEngine (TestContext.Out, errors: errors, messages: messages);
var aapt2Compile = new Aapt2Compile {
BuildEngine = engine,
ToolPath = GetPathToAapt2 (),
ResourceDirectories = new ITaskItem [] {
new TaskItem(Path.Combine (Root, path, "lp", "res"), new Dictionary<string, string> {
{ "Hash", "lp" }
}),
new TaskItem(Path.Combine (Root, path, "res"), new Dictionary<string, string> {
{ "Hash", "compiled" }
}),
},
FlatArchivesDirectory = Path.Combine (Root, path),
FlatFilesDirectory = Path.Combine (Root, path),
};
Assert.IsTrue (aapt2Compile.Execute (), $"Aapt2 Compile should have succeeded. {string.Join (" ", errors.Select (x => x.Message))}");
var platform = AndroidSdkResolver.GetMaxInstalledPlatform ();
string resPath = Path.Combine (Root, path, "res");
string rTxt = Path.Combine (Root, path, "R.txt");
var aapt2Link = new Aapt2Link {
BuildEngine = engine,
ToolPath = GetPathToAapt2 (),
ResourceDirectories = new ITaskItem [] { new TaskItem (resPath) },
ManifestFiles = new ITaskItem [] { new TaskItem (Path.Combine (Root, path, "AndroidManifest.xml")) },
AdditionalResourceArchives = new ITaskItem [] { new TaskItem (Path.Combine (Root, path, "lp.flata")) },
CompiledResourceFlatArchive = new TaskItem (Path.Combine (Root, path, "compiled.flata")),
OutputFile = Path.Combine (Root, path, "foo.apk"),
AssemblyIdentityMapFile = Path.Combine (Root, path, "foo.map"),
JavaPlatformJarPath = Path.Combine (AndroidSdkDirectory, "platforms", $"android-{platform.Major}", "android.jar"),
JavaDesignerOutputDirectory = Path.Combine (Root, path, "java"),
ResourceSymbolsTextFile = rTxt,
};
Assert.IsTrue (aapt2Link.Execute (), "Aapt2 Link should have succeeded.");
FileAssert.Exists (rTxt, $"{rTxt} should have been created.");
var task = new GenerateResourceDesigner {
BuildEngine = engine
};
task.UseManagedResourceGenerator = true;
task.DesignTimeBuild = false;
task.Namespace = "MonoAndroidApplication4.MonoAndroidApplication4";
task.NetResgenOutputFile = Path.Combine (Root, path, "Resource.designer.aapt2.cs");
task.ProjectDir = Path.Combine (Root, path);
task.CaseMapFile = Path.Combine (Root, path, "case_map.txt");
task.ResourceDirectory = Path.Combine (Root, path, "res") + Path.DirectorySeparatorChar;
task.Resources = new TaskItem [] {
new TaskItem (Path.Combine (Root, path, "res", "values", "strings.xml"), new Dictionary<string, string> () {
{ "LogicalName", "values\\strings.xml" },
}),
};
task.AdditionalResourceDirectories = new TaskItem [] {
new TaskItem (Path.Combine (Root, path, "lp", "res")),
};
task.ResourceFlagFile = Path.Combine (Root, path, "AndroidResgen.flag");
task.IsApplication = true;
task.JavaPlatformJarPath = aapt2Link.JavaPlatformJarPath;
Assert.IsTrue (task.Execute (), "Task should have executed successfully.");
File.WriteAllText (task.ResourceFlagFile, string.Empty);
File.Delete (Path.Combine (Root, path, "R.txt.bak"));
File.Move (rTxt, Path.Combine (Root, path, "R.txt.bak"));
task.UseManagedResourceGenerator = true;
task.DesignTimeBuild = true;
task.NetResgenOutputFile = Path.Combine (Root, path, "Resource.designer.managed.cs");
Assert.IsTrue (task.Execute (), "Task should have executed successfully.");
string aapt2Designer = Path.Combine (Root, path, "Resource.designer.aapt2.cs");
string managedDesigner = Path.Combine (Root, path, "Resource.designer.managed.cs");
CompareFilesIgnoreRuntimeInfoString (managedDesigner, aapt2Designer);
Directory.Delete (Path.Combine (Root, path), recursive: true);
}
[Test]
[NonParallelizable] // Test measures performance
public void CheckPerformanceOfManagedParser ()
{
var path = Path.Combine ("temp", TestName);
CreateResourceDirectory (path);
IBuildEngine engine = new MockBuildEngine (TestContext.Out);
TaskLoggingHelper loggingHelper = new TaskLoggingHelper (engine, nameof (ManagedResourceParser));
string resPath = Path.Combine (Root, path, "res");
var platform = AndroidSdkResolver.GetMaxInstalledPlatform ();
var flagFile = Path.Combine (Root, path, "AndroidResgen.flag");
var lp = new string [] { Path.Combine (Root, path, "lp", "res") };
Stopwatch sw = new Stopwatch ();
long totalMS = 0;
int i;
for (i = 0; i < 100; i++) {
var parser = new ManagedResourceParser (loggingHelper) {
JavaPlatformDirectory = Path.Combine (AndroidSdkDirectory, "platforms", $"android-{platform.Major}"),
ResourceFlagFile = flagFile,
};
sw.Start ();
var codeDom = parser.Parse (resPath, "R.txt", lp, isApp: true, resourceMap: new Dictionary<string, string> ());
sw.Stop ();
TestContext.Out.WriteLine ($"Pass {i} took {sw.ElapsedMilliseconds} ms");
totalMS += sw.ElapsedMilliseconds;
sw.Reset ();
Assert.AreEqual (20, codeDom.Members.Count, "Expected 20 Classes to be generated");
}
TestContext.Out.WriteLine ($"Average {totalMS / i} ms");
Assert.LessOrEqual (totalMS / i, 160, "Parser should have taken on average less than 160 ms.");
}
[Test]
public void GenerateDesignerFileWithElevenStyleableAttributesFromRtxt ()
{
var styleable = @"<resources>
<declare-styleable name = ""ElevenAttributes"">
<attr name = ""attr00"" format=""string"" />
<attr name = ""attr01"" format=""string"" />
<attr name = ""attr02"" format=""string"" />
<attr name = ""attr03"" format=""string"" />
<attr name = ""attr04"" format=""string"" />
<attr name = ""attr05"" format=""string"" />
<attr name = ""attr06"" format=""string"" />
<attr name = ""attr07"" format=""string"" />
<attr name = ""attr08"" format=""string"" />
<attr name = ""attr09"" format=""string"" />
<attr name = ""attr10"" format=""string"" />
</declare-styleable>
</resources>";
var rtxt = @"int attr attr00 0x7f010000
int attr attr01 0x7f010001
int attr attr02 0x7f010002
int attr attr03 0x7f010003
int attr attr04 0x7f010004
int attr attr05 0x7f010005
int attr attr06 0x7f010006
int attr attr07 0x7f010007
int attr attr08 0x7f010008
int attr attr09 0x7f010009
int attr attr10 0x7f01000a
int[] styleable ElevenAttributes { 0x7f010000, 0x7f010001, 0x7f010002, 0x7f010003, 0x7f010004, 0x7f010005, 0x7f010006, 0x7f010007, 0x7f010008, 0x7f010009, 0x7f01000a }
int styleable ElevenAttributes_attr00 0
int styleable ElevenAttributes_attr01 1
int styleable ElevenAttributes_attr02 2
int styleable ElevenAttributes_attr03 3
int styleable ElevenAttributes_attr04 4
int styleable ElevenAttributes_attr05 5
int styleable ElevenAttributes_attr06 6
int styleable ElevenAttributes_attr07 7
int styleable ElevenAttributes_attr08 8
int styleable ElevenAttributes_attr09 9
int styleable ElevenAttributes_attr10 10";
var path = Path.Combine ("temp", TestName);
Directory.CreateDirectory (Path.Combine (Root, path));
File.WriteAllText (Path.Combine (Root, path, "AndroidManifest.xml"), AndroidManifest);
Directory.CreateDirectory (Path.Combine (Root, path, "res"));
Directory.CreateDirectory (Path.Combine (Root, path, "res", "values"));
File.WriteAllText (Path.Combine (Root, path, "res", "values", "attrs.xml"), styleable);
File.WriteAllText (Path.Combine (Root, path, "R.txt"), rtxt);
IBuildEngine engine = new MockBuildEngine (TestContext.Out);
var task = new GenerateResourceDesigner {
BuildEngine = engine
};
task.UseManagedResourceGenerator = true;
task.DesignTimeBuild = true;
task.Namespace = "Foo.Foo";
task.NetResgenOutputFile = Path.Combine (Root, path, "Resource.designer.cs");
task.ProjectDir = Path.Combine (Root, path);
task.CaseMapFile = Path.Combine (Root, path, "case_map.txt");
task.ResourceDirectory = Path.Combine (Root, path, "res");
task.Resources = new TaskItem [] {};
task.IsApplication = true;
var platform = AndroidSdkResolver.GetMaxInstalledPlatform ();
task.JavaPlatformJarPath = Path.Combine (AndroidSdkDirectory, "platforms", $"android-{platform}", "android.jar");
Assert.IsTrue (task.Execute (), "Task should have executed successfully.");
Assert.IsTrue (File.Exists (task.NetResgenOutputFile), $"{task.NetResgenOutputFile} should have been created.");
var expected = Path.Combine (ExpectedOutputDir, "GenerateDesignerFileWithElevenStyleableAttributesExpected.cs");
CompareFilesIgnoreRuntimeInfoString (task.NetResgenOutputFile, expected);
Directory.Delete (Path.Combine (Root, path), recursive: true);
}
}
}