-
Notifications
You must be signed in to change notification settings - Fork 566
Expand file tree
/
Copy pathAndroidUpdateResourcesTest.cs
More file actions
1593 lines (1476 loc) · 71.3 KB
/
AndroidUpdateResourcesTest.cs
File metadata and controls
1593 lines (1476 loc) · 71.3 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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Microsoft.Build.Framework;
using Mono.Cecil;
using NUnit.Framework;
using Xamarin.Android.Tasks;
using Xamarin.Android.Tools;
using Xamarin.ProjectTools;
namespace Xamarin.Android.Build.Tests
{
[TestFixture]
[Parallelizable (ParallelScope.Children)]
public class AndroidUpdateResourcesTest : BaseTest
{
[Test]
public void CheckMultipleLibraryProjectReferenceAlias ([Values] bool withGlobal, [Values] bool useDesignerAssembly, [Values] AndroidRuntime runtime)
{
bool isRelease = runtime == AndroidRuntime.NativeAOT;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var path = Path.Combine (Root, "temp", TestName);
var library1 = new XamarinAndroidLibraryProject () {
IsRelease = isRelease,
ProjectName = "Library1",
};
library1.SetRuntime (runtime);
var library2 = new XamarinAndroidLibraryProject () {
IsRelease = isRelease,
ProjectName = "Library2",
RootNamespace = "Library1"
};
library2.SetRuntime (runtime);
var proj = new XamarinAndroidApplicationProject () {
IsRelease = isRelease,
References = {
new BuildItem.ProjectReference (Path.Combine("..", library1.ProjectName, Path.GetFileName (library1.ProjectFilePath)), "Library1") {
Metadata = { { "Aliases", withGlobal ? "global,Lib1A,Lib1B" : "Lib1A,Lib1B" } },
},
new BuildItem.ProjectReference (Path.Combine("..", library2.ProjectName, Path.GetFileName (library2.ProjectFilePath)), "Library2") {
Metadata = { { "Aliases", withGlobal ? "global,Lib2A,Lib2B" : "Lib2A,Lib2B" } },
},
},
};
proj.SetRuntime (runtime);
library1.SetProperty ("AndroidUseDesignerAssembly", "false");
library2.SetProperty ("AndroidUseDesignerAssembly", useDesignerAssembly.ToString ());
proj.SetProperty ("AndroidUseDesignerAssembly", useDesignerAssembly.ToString ());
using var builder1 = CreateDllBuilder (Path.Combine (path, library1.ProjectName), cleanupAfterSuccessfulBuild: false, cleanupOnDispose: false);
builder1.ThrowOnBuildFailure = false;
Assert.IsTrue (builder1.Build (library1), "Library should have built.");
using var builder2 = CreateDllBuilder (Path.Combine (path, library2.ProjectName), cleanupAfterSuccessfulBuild: false, cleanupOnDispose: false);
builder2.ThrowOnBuildFailure = false;
Assert.IsTrue (builder2.Build (library2), "Library should have built.");
using var b = CreateApkBuilder (Path.Combine (path, proj.ProjectName), cleanupAfterSuccessfulBuild: false, cleanupOnDispose: false);
b.ThrowOnBuildFailure = false;
Assert.IsTrue (b.Build (proj), "Project should have built.");
if (!useDesignerAssembly) {
string resource_designer_cs = GetResourceDesignerPath (b, proj);
string [] text = GetResourceDesignerLines (proj, resource_designer_cs);
Assert.IsTrue (text.Count (x => x.Contains ("Library1.Resource.String.library_name")) == 2, "library_name resource should be present exactly once for each library");
Assert.IsTrue (text.Count (x => x == "extern alias Lib1A;" || x == "extern alias Lib1B;") <= 1, "No more than one extern alias should be present for each library.");
}
}
[Test]
public void BuildAppWithSystemNamespace ([Values] AndroidRuntime runtime)
{
bool isRelease = runtime == AndroidRuntime.NativeAOT;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var path = Path.Combine (Root, "temp", TestName);
var library = new XamarinAndroidLibraryProject () {
IsRelease = isRelease,
ProjectName = "Library1.System",
};
library.SetRuntime (runtime);
var proj = new XamarinAndroidApplicationProject () {
IsRelease = isRelease,
References = {
new BuildItem.ProjectReference (Path.Combine("..", library.ProjectName, Path.GetFileName (library.ProjectFilePath)), "Library1.System") {
},
},
};
proj.SetRuntime (runtime);
using (var builder = CreateDllBuilder (Path.Combine (path, library.ProjectName), cleanupAfterSuccessfulBuild: false, cleanupOnDispose: false)) {
builder.ThrowOnBuildFailure = false;
Assert.IsTrue (builder.Build (library), "Library should have built.");
using (var b = CreateApkBuilder (Path.Combine (path, proj.ProjectName), cleanupAfterSuccessfulBuild: false, cleanupOnDispose: false)) {
b.ThrowOnBuildFailure = false;
Assert.IsTrue (b.Build (proj), "Project should have built.");
}
}
}
[Test]
public void DesignTimeBuild ([Values] bool isRelease, [Values] bool useManagedParser, [Values] AndroidRuntime runtime)
{
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var regEx = new Regex (@"(?<type>([a-zA-Z_0-9])+)\slibrary_name=(?<value>([0-9A-Za-z])+);", RegexOptions.Compiled | RegexOptions.Multiline );
var path = Path.Combine (Root, "temp", TestName);
var lib = new XamarinAndroidLibraryProject () {
ProjectName = "Lib1",
IsRelease = isRelease,
};
lib.SetRuntime (runtime);
lib.SetProperty ("AndroidUseManagedDesignTimeResourceGenerator", useManagedParser.ToString ());
lib.SetProperty ("AndroidUseDesignerAssembly", "false");
var proj = new XamarinAndroidApplicationProject () {
IsRelease = isRelease,
References = {
new BuildItem.ProjectReference (@"..\Lib1\Lib1.csproj", lib.ProjectName, lib.ProjectGuid),
},
};
proj.SetRuntime (runtime);
var intermediateOutputPath = Path.Combine (path, proj.ProjectName, proj.IntermediateOutputPath);
proj.SetProperty ("AndroidUseManagedDesignTimeResourceGenerator", useManagedParser.ToString ());
proj.SetProperty ("AndroidUseDesignerAssembly", "false");
using (var l = CreateDllBuilder (Path.Combine (path, lib.ProjectName), false, false)) {
using (var b = CreateApkBuilder (Path.Combine (path, proj.ProjectName), false, false)) {
l.Target = "Build";
Assert.IsTrue(l.Clean(lib), "Lib1 should have cleaned successfully");
Assert.IsTrue (l.Build (lib), "Lib1 should have built successfully");
b.ThrowOnBuildFailure = false;
b.Target = "Compile";
Assert.IsTrue (b.Build (proj, doNotCleanupOnUpdate: true, parameters: new string [] { "DesignTimeBuild=true" }),
"first build failed");
var designTimeDesigner = Path.Combine (intermediateOutputPath, "designtime", "Resource.designer.cs");
FileAssert.Exists (designTimeDesigner, $"{designTimeDesigner} should have been created.");
WaitFor (1000);
b.Target = "Build";
Assert.IsTrue (b.Build (proj, doNotCleanupOnUpdate: true, parameters: new string [] { "DesignTimeBuild=false" }), "second build failed");
FileAssert.Exists (Path.Combine (intermediateOutputPath, "R.txt"), "R.txt should exist after IncrementalClean!");
FileAssert.Exists (Path.Combine (intermediateOutputPath, "res.flag"), "res.flag should exist after IncrementalClean!");
if (useManagedParser) {
FileAssert.Exists (designTimeDesigner, $"{designTimeDesigner} should not have been deleted.");
}
var items = new List<string> ();
if (!useManagedParser) {
foreach (var file in Directory.EnumerateFiles (Path.Combine (intermediateOutputPath, "android", "src"), "R.java", SearchOption.AllDirectories)) {
var matches = regEx.Matches (File.ReadAllText (file));
items.AddRange (matches.Cast<System.Text.RegularExpressions.Match> ().Select (x => x.Groups ["value"].Value));
}
var first = items.First ();
Assert.IsTrue (items.All (x => x == first), "All Items should have matching values");
}
}
}
}
[Test]
public void CheckEmbeddedAndroidXResources ([Values] AndroidRuntime runtime)
{
const bool isRelease = true;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var proj = new XamarinAndroidApplicationProject () {
IsRelease = isRelease,
PackageReferences = {
KnownPackages.AndroidXAppCompat,
},
};
proj.SetRuntime (runtime);
using (var b = CreateApkBuilder ()) {
Assert.IsTrue (b.Build (proj), "First build should have succeeded.");
var Rdrawable = b.Output.GetIntermediaryPath (Path.Combine ("android", "bin", "classes", "androidx", "appcompat", "R$drawable.class"));
Assert.IsTrue (File.Exists (Rdrawable), $"{Rdrawable} should exist");
}
}
[Test]
public void MoveResource ([Values] AndroidRuntime runtime)
{
bool isRelease = runtime == AndroidRuntime.NativeAOT;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var proj = new XamarinAndroidApplicationProject {
IsRelease = isRelease,
};
proj.SetRuntime (runtime);
BuildItem image = null;
var image_data = XamarinAndroidCommonProject.GetResourceContents ("Xamarin.ProjectTools.Resources.Base.Icon.png");
image = new AndroidItem.AndroidResource ("Resources\\drawable\\Image.png") { BinaryContent = () => image_data };
proj.AndroidResources.Add (image);
using (var b = CreateApkBuilder ()) {
Assert.IsTrue (b.Build (proj), "First build should have succeeded.");
var oldpath = image.Include ().Replace ('\\', Path.DirectorySeparatorChar);
image.Include = () => "Resources/drawable/NewImage.png";
image.Timestamp = DateTimeOffset.UtcNow.AddMinutes (1);
Assert.IsTrue (b.Build (proj), "Second build should have succeeded.");
Assert.IsFalse (File.Exists (Path.Combine (b.ProjectDirectory, oldpath)), "XamarinProject.UpdateProjectFiles() failed to delete file");
Assert.IsFalse (b.Output.IsTargetSkipped ("_Sign"), "incorrectly skipped some build");
}
}
[Test]
public void ReportAaptErrorsInOriginalFileName ([Values] AndroidRuntime runtime)
{
bool isRelease = runtime == AndroidRuntime.NativeAOT;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var proj = new XamarinAndroidApplicationProject {
IsRelease = isRelease,
};
proj.SetRuntime (runtime);
proj.LayoutMain = @"<root/>\n" + proj.LayoutMain;
using (var b = CreateApkBuilder ()) {
b.ThrowOnBuildFailure = false;
// The AndroidGenerateLayoutBindings=false property is necessary because otherwise build
// will fail in code-behind generator instead of in aapt
Assert.IsFalse (b.Build (proj, parameters: new[] { "AndroidGenerateLayoutBindings=false" }), "Build should have failed.");
Assert.IsTrue (b.LastBuildOutput.Any (s => s.Contains (string.Format ("Resources{0}layout{0}Main.axml", Path.DirectorySeparatorChar)) && s.Contains (": error ")), "error with expected file name is not found");
Assert.IsTrue (b.Clean (proj), "Clean should have succeeded.");
}
}
[Test]
public void ReportAaptWarningsForBlankLevel ([Values] AndroidRuntime runtime)
{
bool isRelease = runtime == AndroidRuntime.NativeAOT;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
//This test should get the warning `Invalid file name: must contain only [a-z0-9_.]`
// However, <Aapt /> still fails due to aapt failing, Resource.designer.cs is not generated
var proj = new XamarinAndroidApplicationProject {
IsRelease = isRelease,
};
proj.SetRuntime (runtime);
proj.AndroidResources.Add (new AndroidItem.AndroidResource ("Resources\\drawable\\Image (1).png") {
BinaryContent = () => XamarinAndroidCommonProject.icon_binary_mdpi
});
using (var b = CreateApkBuilder ()) {
b.ThrowOnBuildFailure = false;
Assert.IsFalse (b.Build (proj), "Build should have failed.");
StringAssertEx.Contains ("APT0003", b.LastBuildOutput, "An error message with a blank \"level\", should be reported as an error!");
Assert.IsTrue (b.Clean (proj), "Clean should have succeeded.");
}
}
[Test]
public void RepetiviteBuildUpdateSingleResource ([Values] AndroidRuntime runtime)
{
bool isRelease = runtime == AndroidRuntime.NativeAOT;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var proj = new XamarinAndroidApplicationProject {
IsRelease = isRelease,
};
proj.SetRuntime (runtime);
using (var b = CreateApkBuilder ()) {
BuildItem image1, image2;
var image_data = XamarinAndroidCommonProject.GetResourceContents ("Xamarin.ProjectTools.Resources.Base.Icon.png");
image1 = new AndroidItem.AndroidResource ("Resources\\drawable\\Image1.png") { BinaryContent = () => image_data };
proj.AndroidResources.Add (image1);
image2 = new AndroidItem.AndroidResource ("Resources\\drawable\\Image2.png") { BinaryContent = () => image_data };
proj.AndroidResources.Add (image2);
b.ThrowOnBuildFailure = false;
Assert.IsTrue (b.Build (proj), "First build was supposed to build without errors");
var firstBuildTime = b.LastBuildTime;
Assert.IsTrue (b.Build (proj, doNotCleanupOnUpdate: true), "Second build was supposed to build without errors");
if (runtime != AndroidRuntime.NativeAOT) {
Assert.IsTrue (firstBuildTime > b.LastBuildTime, "Second build was supposed to be quicker than the first");
}
b.Output.AssertTargetIsSkipped ("_UpdateAndroidResgen");
b.Output.AssertTargetIsSkipped ("_GenerateAndroidResourceDir");
b.Output.AssertTargetIsSkipped ("_CompileJava");
if (runtime != AndroidRuntime.NativeAOT) {
b.Output.AssertTargetIsSkipped (KnownTargets.LinkAssembliesNoShrink);
}
b.Output.AssertTargetIsSkipped ("_CompileResources");
image1.Timestamp = DateTimeOffset.UtcNow;
var layout = proj.AndroidResources.First (x => x.Include() == "Resources\\layout\\Main.axml");
layout.Timestamp = DateTimeOffset.UtcNow;
Assert.IsTrue (b.Build (proj, doNotCleanupOnUpdate:true, saveProject: false), "Third build was supposed to build without errors");
b.Output.AssertTargetIsNotSkipped ("_UpdateAndroidResgen", occurrence: 2);
b.Output.AssertTargetIsNotSkipped ("_GenerateAndroidResourceDir", occurrence: 2);
b.Output.AssertTargetIsSkipped ("_CompileJava", occurrence: 2);
if (runtime != AndroidRuntime.NativeAOT) {
b.Output.AssertTargetIsSkipped (KnownTargets.LinkAssembliesNoShrink, occurrence: 2);
}
b.Output.AssertTargetIsNotSkipped ("_CreateBaseApk", occurrence: 2);
b.Output.AssertTargetIsPartiallyBuilt ("_CompileResources");
}
}
[Test]
[Category ("XamarinBuildDownload")]
[NonParallelizable]
public void Check9PatchFilesAreProcessed ([Values] AndroidRuntime runtime)
{
bool isRelease = runtime == AndroidRuntime.NativeAOT;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var projectPath = Path.Combine ("temp", TestName);
var libproj = new XamarinAndroidLibraryProject () {
IsRelease = isRelease,
ProjectName = "Library1"
};
libproj.SetRuntime (runtime);
var image_data = XamarinAndroidCommonProject.GetResourceContents ("Xamarin.ProjectTools.Resources.Base.Image.9.png");
var image2 = new AndroidItem.AndroidResource ("Resources\\drawable\\Image2.9.png") { BinaryContent = () => image_data };
libproj.AndroidResources.Add (image2);
using (var libb = CreateDllBuilder (Path.Combine (projectPath, "Library1"))) {
libb.Build (libproj);
var proj = new XamarinFormsMapsApplicationProject {
IsRelease = isRelease,
};
proj.SetRuntime (runtime);
var image1 = new AndroidItem.AndroidResource ("Resources\\drawable\\Image1.9.png") { BinaryContent = () => image_data };
proj.AndroidResources.Add (image1);
proj.References.Add (new BuildItem ("ProjectReference", "..\\Library1\\Library1.csproj"));
using (var b = CreateApkBuilder (Path.Combine (projectPath, "Application1"), false, false)) {
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
var path = Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath, "android/bin/packaged_resources");
var data = ZipHelper.ReadFileFromZip (path, "res/drawable/image1.9.png");
Assert.IsNotNull (data, "image1.9.png should be in {0}android/bin/packaged_resources",
proj.IntermediateOutputPath);
var png = PNGChecker.LoadFromBytes (data);
Assert.IsTrue (png.Is9Patch, "image1.9.png should have been processed into a 9 patch image.");
data = ZipHelper.ReadFileFromZip (path, "res/drawable/image2.9.png");
Assert.IsNotNull (data, "image2.9.png should be in {0}android/bin/packaged_resources",
proj.IntermediateOutputPath);
png = PNGChecker.LoadFromBytes (data);
Assert.IsTrue (png.Is9Patch, "image2.9.png should have been processed into a 9 patch image.");
data = ZipHelper.ReadFileFromZip (path, "res/drawable-hdpi-v4/common_google_signin_btn_icon_dark_normal_background.9.png");
Assert.IsNotNull (data, "common_google_signin_btn_icon_dark_normal_background.9.png.png should be in {0}android/bin/packaged_resources",
proj.IntermediateOutputPath);
png = PNGChecker.LoadFromBytes (data);
Assert.IsTrue (png.Is9Patch, "common_google_signin_btn_icon_dark_normal_background.9.png should have been processed into a 9 patch image.");
Directory.Delete (Path.Combine (Root,projectPath), recursive: true);
}
}
}
[Test]
/// <summary>
/// Based on https://bugzilla.xamarin.com/show_bug.cgi?id=29263
/// </summary>
public void CheckXmlResourcesFilesAreProcessed ([Values] AndroidRuntime runtime)
{
bool isRelease = runtime == AndroidRuntime.NativeAOT;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
// TODO: NativeAOT fails with: 'classlibrary1.CustomTextView should have been replaced with an $(Hash).CustomTextView'
if (runtime == AndroidRuntime.NativeAOT) {
Assert.Ignore ("NativeAOT fails here atm");
}
var projectPath = Path.Combine ("temp", TestName);
var layout = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<LinearLayout xmlns:android=""http://schemas.android.com/apk/res/android""
android:orientation = ""vertical""
android:layout_width = ""fill_parent""
android:layout_height = ""fill_parent"">
<classlibrary1.CustomTextView
android:id = ""@+id/myText1""
android:layout_width = ""fill_parent""
android:layout_height = ""wrap_content""
android:text = ""namespace_lower"" />
<ClassLibrary1.CustomTextView
android:id = ""@+id/myText2""
android:layout_width = ""fill_parent""
android:layout_height = ""wrap_content""
android:text = ""namespace_proper"" />
</LinearLayout>";
var lib = new XamarinAndroidLibraryProject () {
IsRelease = isRelease,
ProjectName = "Classlibrary1",
};
lib.SetRuntime (runtime);
lib.AndroidResources.Add (new AndroidItem.AndroidResource ("Resources\\layout\\custom_text_lib.xml") {
TextContent = () => layout,
});
lib.Sources.Add (new BuildItem.Source ("CustomTextView.cs") {
TextContent = () => @"using Android.Widget;
using Android.Content;
using Android.Util;
namespace ClassLibrary1
{
public class CustomTextView : TextView
{
public CustomTextView(Context context, IAttributeSet attributes) : base(context, attributes)
{
}
}
}"
});
var proj = new XamarinAndroidApplicationProject () {
IsRelease = isRelease,
OtherBuildItems = {
new BuildItem.ProjectReference (@"..\Classlibrary1\Classlibrary1.csproj", "Classlibrary1", lib.ProjectGuid) {
},
}
};
proj.SetRuntime (runtime);
proj.AndroidResources.Add (new AndroidItem.AndroidResource ("Resources\\layout\\custom_text_app.xml") {
TextContent = () => layout,
});
proj.AndroidResources.Add (new AndroidItem.AndroidResource ("Resources\\drawable\\UPPER_image.png") {
BinaryContent = () => XamarinAndroidCommonProject.icon_binary_mdpi
});
proj.AndroidResources.Add (new AndroidItem.AndroidResource ("Resources\\xml\\Preferences.xml") {
TextContent = () => @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<PreferenceScreen xmlns:android=""http://schemas.android.com/apk/res/android"">
<EditTextPreference
android:key=""pref_a""
android:title=""EditText Preference""
android:singleLine=""true""
android:inputType=""textUri|textNoSuggestions""/>
<UnnamedProject.CustomPreference
android:key=""pref_b""
/>
</PreferenceScreen>"
});
proj.AndroidResources.Add (new AndroidItem.AndroidResource ("Resources\\values\\Strings1.xml") {
TextContent = () => @"<?xml version=""1.0"" encoding=""utf-8""?>
<resources>
<string name=""title_custompreference"">Custom Preference</string>
</resources>"
});
proj.AndroidResources.Add (new AndroidItem.AndroidResource ("Resources\\values\\Styles.xml") {
TextContent = () => @"<?xml version=""1.0"" encoding=""utf-8""?>
<resources>
<color name=""deep_purple_A200"">#e040fb</color>
<style name=""stylename"">
<item name=""android:background"">@drawable/UPPER_image</item>
<item name=""android:textColorPrimary"">@android:color/white</item>
</style>
<style name=""MyTheme.Base"" parent=""Theme.AppCompat.Light.DarkActionBar"">
<item name=""colorAccent"">@color/deep_purple_A200</item>
</style>
</resources>"
});
proj.Sources.Add (new BuildItem.Source ("CustomPreference.cs") {
TextContent = () => @"using System;
using Android.Preferences;
using Android.Content;
using Android.Util;
namespace UnnamedProject
{
public class CustomPreference : Preference
{
public CustomPreference(Context context, IAttributeSet attrs) : base(context, attrs)
{
SetTitle(Resource.String.title_custompreference);
}
protected override void OnClick()
{
}
}
}"
});
proj.PackageReferences.Add (KnownPackages.AndroidXAppCompat);
using (var libb = CreateDllBuilder (Path.Combine (projectPath, lib.ProjectName), cleanupOnDispose: false))
using (var b = CreateApkBuilder (Path.Combine (projectPath, proj.ProjectName), cleanupOnDispose: false)) {
Assert.IsTrue (libb.Build (lib), "Library Build should have succeeded.");
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
var intermediate = Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath);
var packaged_resources = Path.Combine (intermediate, "android", "bin", "packaged_resources");
FileAssert.Exists (packaged_resources);
var assemblyIdentityMap = b.Output.GetAssemblyMapCache ();
var assemblyIndex = assemblyIdentityMap.IndexOf ($"{lib.ProjectName}.aar").ToString ();
using (var zip = ZipHelper.OpenZip (packaged_resources)) {
CheckCustomView (zip, intermediate, "lp", assemblyIndex, "jl", "res", "layout", "custom_text_lib.xml");
CheckCustomView (zip, intermediate, "res", "layout", "custom_text_app.xml");
}
var preferencesPath = Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath, "res","xml","preferences.xml");
Assert.IsTrue (File.Exists (preferencesPath), "Preferences.xml should have been renamed to preferences.xml");
var doc = XDocument.Load (preferencesPath);
Assert.IsNotNull (doc.Element ("PreferenceScreen"), "PreferenceScreen should be present in preferences.xml");
Assert.IsNull (doc.Element ("PreferenceScreen").Element ("UnnamedProject.CustomPreference"),
"UnamedProject.CustomPreference should have been replaced with an $(Hash).CustomPreference");
var style = Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath, "res", "values", "styles.xml");
Assert.IsTrue (File.Exists (style));
doc = XDocument.Load (style);
var item = doc.Element ("resources").Elements ("style")
.Where(x => x.Attribute ("name").Value == "stylename")
.Elements ("item")
.FirstOrDefault (x => x.Attribute("name").Value == "android:background");
Assert.IsNotNull (item, "The Style should contain an Item");
Assert.AreEqual ("@drawable/upper_image", item.Value, "item value should be @drawable/upper_image");
item = doc.Element ("resources").Elements ("style")
.Where(x => x.Attribute ("name").Value == "MyTheme.Base")
.Elements ("item")
.FirstOrDefault (x => x.Attribute("name").Value == "colorAccent");
Assert.IsNotNull (item, "The Style should contain an Item");
Assert.AreEqual ("@color/deep_purple_A200", item.Value, "item value should be @color/deep_purple_A200");
Assert.IsFalse (StringAssertEx.ContainsText (b.LastBuildOutput, "AndroidResgen: Warning while updating Resource XML"),
"Warning while processing resources should not have been raised.");
Assert.IsTrue (b.Build (proj, doNotCleanupOnUpdate: true), "Build should have succeeded.");
Assert.IsTrue (b.Output.IsTargetSkipped ("_GenerateJavaStubsCore"), "Target _GenerateJavaStubsCore should have been skipped");
lib.Touch ("CustomTextView.cs");
Assert.IsTrue (libb.Build (lib, doNotCleanupOnUpdate: true, saveProject: false), "second library build should have succeeded.");
Assert.IsTrue (b.Build (proj, doNotCleanupOnUpdate: true, saveProject: false), "second app build should have succeeded.");
using (var zip = ZipHelper.OpenZip (packaged_resources)) {
CheckCustomView (zip, intermediate, "lp", assemblyIndex, "jl", "res", "layout", "custom_text_lib.xml");
CheckCustomView (zip, intermediate, "res", "layout", "custom_text_app.xml");
}
Assert.IsTrue (b.Clean (proj), "Clean should have succeeded.");
}
}
void CheckCustomView (Xamarin.Tools.Zip.ZipArchive zip, params string [] paths)
{
var customViewPath = Path.Combine (paths);
FileAssert.Exists (customViewPath, $"custom_text.xml should exist at {customViewPath}");
var doc = XDocument.Load (customViewPath);
Assert.IsNotNull (doc.Element ("LinearLayout"), "PreferenceScreen should be present in preferences.xml");
Assert.IsNull (doc.Element ("LinearLayout").Element ("Classlibrary1.CustomTextView"),
$"Classlibrary1.CustomTextView should have been replaced with an $(Hash).CustomTextView in {customViewPath}");
Assert.IsNull (doc.Element ("LinearLayout").Element ("classlibrary1.CustomTextView"),
$"classlibrary1.CustomTextView should have been replaced with an $(Hash).CustomTextView in {customViewPath}");
//Now check the zip
var customViewInZip = "res/layout/" + Path.GetFileName (customViewPath);
var entry = zip.ReadEntry (customViewInZip);
Assert.IsNotNull (entry, $"`{customViewInZip}` should exist in packaged_resources!");
using (var stream = new MemoryStream ()) {
entry.Extract (stream);
stream.Position = 0;
using (var reader = new StreamReader (stream)) {
//NOTE: This is a binary format, but we can still look for text within.
// Don't use `StringAssert` because `contents` make the failure message unreadable.
var contents = reader.ReadToEnd ();
Assert.IsFalse (contents.Contains ("Classlibrary1.CustomTextView"),
$"Classlibrary1.CustomTextView should have been replaced with an $(Hash).CustomTextView in {customViewInZip} in package");
Assert.IsFalse (contents.Contains ("classlibrary1.CustomTextView"),
$"classlibrary1.CustomTextView should have been replaced with an $(Hash).CustomTextView in {customViewInZip} in package");
}
}
}
static IEnumerable<object[]> Get_ReleaseLanguageData ()
{
var ret = new List<object[]> ();
foreach (AndroidRuntime runtime in Enum.GetValues (typeof (AndroidRuntime))) {
AddTestData (isRelease: false, language: XamarinAndroidProjectLanguage.CSharp, runtime: runtime);
AddTestData (isRelease: true, language: XamarinAndroidProjectLanguage.CSharp, runtime: runtime);
AddTestData (isRelease: false, language: XamarinAndroidProjectLanguage.FSharp, runtime: runtime);
AddTestData (isRelease: true, language: XamarinAndroidProjectLanguage.FSharp, runtime: runtime);
}
return ret;
void AddTestData (bool isRelease, ProjectLanguage language, AndroidRuntime runtime)
{
ret.Add (new object[] {
isRelease,
language,
runtime,
});
}
}
[Test]
[Parallelizable (ParallelScope.Self)]
[TestCaseSource (nameof (Get_ReleaseLanguageData))]
public void CheckResourceDesignerIsCreated (bool isRelease, ProjectLanguage language, AndroidRuntime runtime)
{
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
bool isFSharp = language == XamarinAndroidProjectLanguage.FSharp;
var proj = new XamarinAndroidApplicationProject () {
Language = language,
IsRelease = isRelease,
};
proj.SetRuntime (runtime);
proj.SetProperty ("AndroidUseIntermediateDesignerFile", "True");
using (var b = CreateApkBuilder ()) {
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
// Intermediate designer file support is not compatible with F# projects using Xamarin.Android.FSharp.ResourceProvider.
string outputFile = Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath, "__Microsoft.Android.Resource.Designer" + proj.Language.DefaultDesignerExtension);
Assert.IsTrue (File.Exists (outputFile), $"{outputFile} should have been created in {proj.IntermediateOutputPath}");
Assert.IsTrue (b.Clean (proj), "Clean should have succeeded.");
Assert.IsFalse (File.Exists (outputFile), "Resource.designer{1} should have been cleaned in {0}",
proj.IntermediateOutputPath, proj.Language.DefaultDesignerExtension);
}
}
[Test]
[TestCaseSource(nameof (Get_ReleaseLanguageData))]
public void CheckResourceDesignerIsUpdatedWhenReadOnly (bool isRelease, ProjectLanguage language, AndroidRuntime runtime)
{
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
bool isFSharp = language == XamarinAndroidProjectLanguage.FSharp;
var proj = new XamarinAndroidApplicationProject () {
Language = language,
IsRelease = isRelease,
};
proj.SetRuntime (runtime);
using (var b = CreateApkBuilder ()) {
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
var designerPath = GetResourceDesignerPath (b, proj);
var attr = File.GetAttributes (designerPath);
File.SetAttributes (designerPath, FileAttributes.ReadOnly);
Assert.IsTrue ((File.GetAttributes (designerPath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly,
"{0} should be read only", designerPath);
var main = proj.AndroidResources.First (x => x.Include () == "Resources\\layout\\Main.axml");
main.Timestamp = DateTimeOffset.UtcNow;
main.TextContent = () => @"<?xml version=""1.0"" encoding=""utf-8""?>
<LinearLayout xmlns:android=""http://schemas.android.com/apk/res/android""
android:orientation=""vertical""
android:layout_width=""fill_parent""
android:layout_height=""fill_parent""
>
<Button
android:id=""@+id/myButton""
android:layout_width=""fill_parent""
android:layout_height=""wrap_content""
android:text=""Hello""
/>
<TextView
android:id=""@+id/myText""
/>
</LinearLayout>";
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
Assert.IsTrue ((File.GetAttributes (designerPath) & FileAttributes.ReadOnly) != FileAttributes.ReadOnly,
"{0} should be writable", designerPath);
}
}
[Test]
public void CheckOldResourceDesignerIsNotUsed ([Values] bool isRelease, [Values] AndroidRuntime runtime)
{
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var proj = new XamarinAndroidApplicationProject () {
IsRelease = isRelease,
};
proj.SetRuntime (runtime);
proj.SetProperty ("AndroidUseIntermediateDesignerFile", "True");
proj.SetProperty ("AndroidUseManagedDesignTimeResourceGenerator", "False");
using (var b = CreateApkBuilder ()) {
var designer = Path.Combine ("Resources", "Resource.designer" + proj.Language.DefaultDesignerExtension);
if (File.Exists (designer))
File.Delete (Path.Combine (Root, b.ProjectDirectory, designer));
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
var fi = new FileInfo (Path.Combine (Root, b.ProjectDirectory, designer));
Assert.IsFalse (fi.Length > new [] { 0xef, 0xbb, 0xbf, 0x0d, 0x0a }.Length,
"{0} should not contain anything.", designer);
var designerFile = "__Microsoft.Android.Resource.Designer";
var outputFile = Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath,
designerFile + proj.Language.DefaultDesignerExtension);
Assert.IsTrue (File.Exists (outputFile), $"{designerFile}{proj.Language.DefaultDesignerExtension} should have been created in {proj.IntermediateOutputPath}");
Assert.IsTrue (b.Clean (proj), "Clean should have succeeded.");
Assert.IsFalse (File.Exists (outputFile), $"{designerFile}{proj.Language.DefaultDesignerExtension} should have been cleaned in {proj.IntermediateOutputPath}");
}
}
// ref https://bugzilla.xamarin.com/show_bug.cgi?id=30089
[Test]
public void CheckOldResourceDesignerWithWrongCasingIsRemoved ([Values] bool isRelease, [Values] AndroidRuntime runtime)
{
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var proj = new XamarinAndroidApplicationProject () {
IsRelease = isRelease,
};
proj.SetRuntime (runtime);
proj.SetProperty ("AndroidUseIntermediateDesignerFile", "True");
proj.SetProperty ("AndroidResgenFile", "Resources\\Resource.designer" + proj.Language.DefaultDesignerExtension);
using (var b = CreateApkBuilder ()) {
var designer = proj.Sources.FirstOrDefault (x => x.Include() == "Resources\\Resource.designer" + proj.Language.DefaultDesignerExtension);
designer = designer ?? proj.OtherBuildItems.FirstOrDefault (x => x.Include () == "Resources\\Resource.designer" + proj.Language.DefaultDesignerExtension);
Assert.IsNotNull (designer, $"Failed to retrieve the Resource.designer.{proj.Language.DefaultDesignerExtension}");
designer.Deleted = true;
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
Assert.IsFalse (File.Exists (Path.Combine (Root, b.ProjectDirectory, "Resources",
"Resource.designer" + proj.Language.DefaultDesignerExtension)),
"{0} should not exists", designer.Include ());
var designerFile = "__Microsoft.Android.Resource.Designer";
var outputFile = Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath,
designerFile + proj.Language.DefaultDesignerExtension);
Assert.IsTrue (File.Exists (outputFile), $"{designerFile}{proj.Language.DefaultDesignerExtension} should have been created in {proj.IntermediateOutputPath}");
Assert.IsTrue (b.Clean (proj), "Clean should have succeeded.");
Assert.IsFalse (File.Exists (outputFile), $"{designerFile}{proj.Language.DefaultDesignerExtension} should have been cleaned in {proj.IntermediateOutputPath}");
}
}
[Test]
public void GenerateResourceDesigner_false ([Values] bool useDesignerAssembly, [Values] AndroidRuntime runtime)
{
bool isRelease = runtime == AndroidRuntime.NativeAOT;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var proj = new XamarinAndroidApplicationProject {
IsRelease = isRelease,
EnableDefaultItems = true,
Sources = {
new AndroidItem.AndroidResource (() => "Resources\\drawable\\foo.png") {
BinaryContent = () => XamarinAndroidCommonProject.icon_binary_mdpi,
},
}
};
proj.SetRuntime (runtime);
proj.SetProperty (KnownProperties.OutputType, "Library");
// Turn off Resource.designer.cs and remove usage of it
proj.SetProperty ("AndroidGenerateResourceDesigner", "false");
if (!useDesignerAssembly)
proj.SetProperty ("AndroidUseDesignerAssembly", "false");
proj.MainActivity = proj.DefaultMainActivity
.Replace ("Resource.Layout.Main", "0")
.Replace ("Resource.Id.myButton", "0");
var builder = CreateDllBuilder ();
Assert.IsTrue (builder.RunTarget(proj, "CoreCompile", parameters: new string[] { "BuildingInsideVisualStudio=true" }), "Designtime build should succeed.");
var intermediate = Path.Combine (Root, builder.ProjectDirectory, proj.IntermediateOutputPath);
var resource_designer_cs = Path.Combine (intermediate, "designtime", "Resource.designer.cs");
if (useDesignerAssembly)
resource_designer_cs = Path.Combine (intermediate, "__Microsoft.Android.Resource.Designer.cs");
FileAssert.DoesNotExist (resource_designer_cs);
Assert.IsTrue (builder.Build (proj), "build should succeed");
resource_designer_cs = Path.Combine (intermediate, "Resource.designer.cs");
if (useDesignerAssembly)
resource_designer_cs = Path.Combine (intermediate, "__Microsoft.Android.Resource.Designer.cs");
FileAssert.DoesNotExist (resource_designer_cs);
var assemblyPath = Path.Combine (Root, builder.ProjectDirectory, proj.OutputPath, $"{proj.ProjectName}.dll");
FileAssert.Exists (assemblyPath);
using var assembly = AssemblyDefinition.ReadAssembly (assemblyPath);
var typeName = $"{proj.ProjectName}.Resource";
var type = assembly.MainModule.GetType (typeName);
Assert.IsNull (type, $"{assemblyPath} should *not* contain {typeName}");
}
[Test]
public void CheckThatXA1034IsRaisedForInvalidConfiguration ([Values] bool isRelease, [Values] AndroidRuntime runtime)
{
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
string path = Path.Combine (Root, "temp", TestName);
var foo = new BuildItem.Source ("Foo.cs") {
TextContent = () => @"using System;
namespace Lib1 {
public class Foo {
public static string GetFoo () {
return ""Foo"";
}
}
}"
};
var library = new XamarinAndroidLibraryProject () {
IsRelease = isRelease,
ProjectName = "Lib1",
Sources = { foo },
};
library.SetRuntime (runtime);
library.SetProperty ("AndroidUseDesignerAssembly", "True");
var proj = new XamarinAndroidApplicationProject () {
IsRelease = isRelease,
ProjectName = "App1",
References = {
new BuildItem.ProjectReference ($"..\\{library.ProjectName}\\{library.ProjectName}.csproj", library.ProjectName, library.ProjectGuid),
},
};
proj.SetRuntime (runtime);
proj.SetProperty ("AndroidUseDesignerAssembly", "False");
proj.MainActivity = proj.DefaultMainActivity.Replace ("//${AFTER_ONCREATE}", "Console.WriteLine (Lib1.Foo.GetFoo ());");
using (var lb = CreateDllBuilder (Path.Combine (path, library.ProjectName))) {
lb.ThrowOnBuildFailure = false;
Assert.IsTrue (lb.Build (library), "Library project should have built.");
using (var pb = CreateApkBuilder (Path.Combine (path, proj.ProjectName))) {
pb.ThrowOnBuildFailure = false;
Assert.IsFalse (pb.Build (proj), "Application project build should have failed.");
StringAssertEx.ContainsText (pb.LastBuildOutput, "XA1034: ");
StringAssertEx.ContainsText (pb.LastBuildOutput, "1 Error(s)");
}
}
}
[Test]
public void CheckAaptErrorRaisedForMissingResource ([Values] AndroidRuntime runtime)
{
bool isRelease = runtime == AndroidRuntime.NativeAOT;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var proj = new XamarinAndroidApplicationProject {
IsRelease = isRelease,
};
proj.SetRuntime (runtime);
var main = proj.AndroidResources.First (x => x.Include () == "Resources\\layout\\Main.axml");
main.TextContent = () => @"<?xml version=""1.0"" encoding=""utf-8""?>
<LinearLayout xmlns:android=""http://schemas.android.com/apk/res/android""
android:orientation=""vertical""
android:layout_width=""fill_parent""
android:layout_height=""fill_parent""
>
<Button
android:id=""@id/myButton""
android:layout_width=""fill_parent""
android:layout_height=""wrap_content""
android:text=""@string/foo""
/>
</LinearLayout>";
using (var b = CreateApkBuilder ()) {
b.ThrowOnBuildFailure = false;
Assert.IsFalse (b.Build (proj), "Build should have failed");
StringAssertEx.Contains ("APT2260: ", b.LastBuildOutput);
StringAssertEx.Contains ("3 Error(s)", b.LastBuildOutput);
}
}
[Test]
public void CheckAaptErrorRaisedForInvalidDirectoryName ([Values] AndroidRuntime runtime)
{
bool isRelease = runtime == AndroidRuntime.NativeAOT;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var proj = new XamarinAndroidApplicationProject {
IsRelease = isRelease,
};
proj.SetRuntime (runtime);
proj.AndroidResources.Add (new AndroidItem.AndroidResource("Resources\\booboo\\stuff.xml") {
TextContent = () => @"<?xml version=""1.0"" encoding=""utf-8""?>
<resources>
</resources>"
});
using (var b = CreateApkBuilder ()) {
b.ThrowOnBuildFailure = false;
Assert.IsFalse (b.Build (proj), "Build should have failed");
StringAssertEx.Contains ("APT2144: ", b.LastBuildOutput);
StringAssertEx.Contains ("1 Error(s)", b.LastBuildOutput);
}
}
[Test]
public void CheckAaptErrorRaisedForInvalidFileName ([Values] AndroidRuntime runtime)
{
bool isRelease = runtime == AndroidRuntime.NativeAOT;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var proj = new XamarinAndroidApplicationProject {
IsRelease = isRelease,
};
proj.SetRuntime (runtime);
proj.AndroidResources.Add (new AndroidItem.AndroidResource ("Resources\\drawable\\icon-2.png") {
BinaryContent = () => XamarinAndroidCommonProject.icon_binary_hdpi,
});
proj.AndroidResources.Add (new AndroidItem.AndroidResource ("Resources\\values\\strings-2.xml") {
TextContent = () => @"<?xml version=""1.0"" encoding=""utf-8""?>
<resources>
<string name=""hello"">Hello World, Click Me!</string>
</resources>",
});
using (var b = CreateApkBuilder ()) {
b.ThrowOnBuildFailure = false;
Assert.IsFalse (b.Build (proj), "Build should have failed");
StringAssertEx.Contains ("Invalid file name:", b.LastBuildOutput);
StringAssertEx.Contains ($"1 Error(s)", b.LastBuildOutput);
}
}
[Test]
public void CheckAaptErrorNotRaisedForInvalidFileNameWithValidLogicalName ([Values] AndroidRuntime runtime)
{
bool isRelease = runtime == AndroidRuntime.NativeAOT;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var proj = new XamarinAndroidApplicationProject {
IsRelease = isRelease,
};
proj.SetRuntime (runtime);
proj.AndroidResources.Add (new AndroidItem.AndroidResource ("Resources\\drawable\\icon-2.png") {
Metadata = { { "LogicalName", "Resources\\drawable\\icon2.png" } },
BinaryContent = () => XamarinAndroidCommonProject.icon_binary_hdpi,
});
proj.AndroidResources.Add (new AndroidItem.AndroidResource ("Resources\\values\\strings-2.xml") {
Metadata = { { "LogicalName", "Resources\\values\\strings2.xml" } },
TextContent = () => @"<?xml version=""1.0"" encoding=""utf-8""?>
<resources>
<string name=""hellome"">Hello World, Click Me!</string>
</resources>",
});
using (var b = CreateApkBuilder ()) {
b.ThrowOnBuildFailure = false;
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
StringAssertEx.DoesNotContain ("Invalid file name:", b.LastBuildOutput);
StringAssertEx.DoesNotContain ("1 Error(s)", b.LastBuildOutput);
}
}
[Test]
public void CheckAaptErrorRaisedForDuplicateResourceinApp ([Values] AndroidRuntime runtime)
{
bool isRelease = runtime == AndroidRuntime.NativeAOT;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var proj = new XamarinAndroidApplicationProject {
IsRelease = isRelease,
};
proj.SetRuntime (runtime);
var stringsXml = proj.AndroidResources.First (x => x.Include () == "Resources\\values\\Strings.xml");
stringsXml.TextContent = () => @"<?xml version=""1.0"" encoding=""utf-8""?>
<resources>
<string name=""hello"">Hello World, Click Me!</string>
<string name=""app_name"">Application one</string>
<string name=""some_string_value"">Hello Me From the App</string>
<string name=""some_string_value"">Hello Me From the App 2</string>
</resources>";
using (var b = CreateApkBuilder ()) {
b.ThrowOnBuildFailure = false;
Assert.IsFalse (b.Build (proj), "Build should have failed");
StringAssertEx.Contains ("APT2057: ", b.LastBuildOutput);
StringAssertEx.Contains ("APT2222: ", b.LastBuildOutput);
StringAssertEx.Contains ("APT2261: ", b.LastBuildOutput);
StringAssertEx.Contains ("3 Error(s)", b.LastBuildOutput);
}
}
[Test]
public void CheckFilesAreRemoved ([Values] AndroidRuntime runtime)
{
bool isRelease = runtime == AndroidRuntime.NativeAOT;
if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) {
return;
}
var proj = new XamarinAndroidApplicationProject () {
IsRelease = isRelease,
AndroidResources = { new AndroidItem.AndroidResource ("Resources\\values\\Theme.xml") {
TextContent = () => @"<?xml version=""1.0"" encoding=""utf-8""?>