/
BuildAssetsTest.cs
171 lines (164 loc) · 6.56 KB
/
BuildAssetsTest.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
using System;
using NUnit.Framework;
using Xamarin.ProjectTools;
using System.IO;
using System.Linq;
using Microsoft.Build.Framework;
using System.Text;
using System.Xml.Linq;
namespace Xamarin.Android.Build.Tests
{
[Category ("Node-3")]
[Parallelizable (ParallelScope.Children)]
public class BuildAssetsTest : BaseTest
{
[Test]
public void CheckPostCompileAssetsIncludedInAPK ()
{
var path = Path.Combine ("temp", TestName);
var proj = new XamarinAndroidApplicationProject () {
ProjectName = "App1",
IsRelease = true,
OtherBuildItems = {
new AndroidItem.AndroidAsset ("Assets\\asset3.txt") {
TextContent = () => "PlaceHolder",
Encoding = Encoding.ASCII,
},
new AndroidItem.AndroidAsset ("Assets\\subfolder\\asset4.txt") {
TextContent = () => "Asset4",
Encoding = Encoding.ASCII,
},
},
Imports = {
new Import (() => "My.Test.target") {
TextContent = () => @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
<Target Name=""CustomTarget"" AfterTargets=""Compile"" >
<WriteLinesToFile
File=""Assets\\asset3.txt""
Lines=""Asset3""
Overwrite=""true""/>
</Target>
</Project>"
},
},
};
using (var b = CreateApkBuilder (Path.Combine (path, proj.ProjectName))) {
Assert.IsTrue (b.Build (proj), "{0} should have built successfully.", proj.ProjectName);
using (var apk = ZipHelper.OpenZip (Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath, "android", "bin", "packaged_resources"))) {
var item = "assets/asset3.txt";
var data = ZipHelper.ReadFileFromZip (apk, item);
Assert.IsNotNull (data, "{0} should be in the apk.", item);
var text = Encoding.ASCII.GetString (data).Trim ();
Assert.AreEqual ("Asset3", text, $"The Contents of {item} should be \"Asset3\" but was {text}");
}
Directory.Delete (Path.Combine (Root, path), recursive: true);
}
}
[Test]
public void CheckAssetsAreIncludedInAPK ([Values (true, false)] bool useAapt2)
{
AssertAaptSupported (useAapt2);
var projectPath = Path.Combine ("temp", TestName);
var libproj = new XamarinAndroidLibraryProject () {
ProjectName = "Library1",
IsRelease = true,
OtherBuildItems = {
new AndroidItem.AndroidAsset ("Assets\\asset1.txt") {
TextContent = () => "Asset1",
Encoding = Encoding.ASCII,
},
new AndroidItem.AndroidAsset ("Assets\\subfolder\\") {
},
new AndroidItem.AndroidAsset ("Assets\\subfolder\\asset2.txt") {
TextContent = () => "Asset2",
Encoding = Encoding.ASCII,
},
new AndroidItem.AndroidAsset ("Assets\\subfolder\\asset5.txt") {
TextContent = () => "Asset5",
Encoding = Encoding.ASCII,
Metadata = { { "LogicalName", Path.Combine (Path.GetPathRoot (Root), "Assets", "subfolder", "asset5.txt") } },
},
}
};
var proj = new XamarinAndroidApplicationProject () {
ProjectName = "App1",
IsRelease = true,
OtherBuildItems = {
new AndroidItem.AndroidAsset ("Assets\\asset3.txt") {
TextContent = () => "Asset3",
Encoding = Encoding.ASCII,
},
new AndroidItem.AndroidAsset ("Assets\\subfolder\\") {
},
new AndroidItem.AndroidAsset ("Assets\\subfolder\\asset4.txt") {
TextContent = () => "Asset4",
Encoding = Encoding.ASCII,
},
new AndroidItem.AndroidAsset ("Assets\\subfolder\\asset6.txt") {
TextContent = () => "Asset6",
Encoding = Encoding.ASCII,
Metadata = { { "LogicalName", Path.Combine (Path.GetPathRoot (Root), "Assets", "subfolder", "asset6.txt") } },
},
}
};
proj.AndroidUseAapt2 = useAapt2;
proj.References.Add (new BuildItem ("ProjectReference", "..\\Library1\\Library1.csproj"));
using (var libb = CreateDllBuilder (Path.Combine (projectPath, libproj.ProjectName))) {
Assert.IsTrue (libb.Build (libproj), "{0} should have built successfully.", libproj.ProjectName);
using (var b = CreateApkBuilder (Path.Combine (projectPath, proj.ProjectName))) {
Assert.IsTrue (b.Build (proj), "{0} should have built successfully.", proj.ProjectName);
using (var apk = ZipHelper.OpenZip (Path.Combine (Root, b.ProjectDirectory, proj.OutputPath, $"{proj.PackageName}-Signed.apk"))) {
foreach (var a in libproj.OtherBuildItems.Where (x => x is AndroidItem.AndroidAsset)) {
var item = a.Include ().ToLower ().Replace ("\\", "/");
if (item.EndsWith ("/"))
continue;
var data = ZipHelper.ReadFileFromZip (apk, item);
Assert.IsNotNull (data, "{0} should be in the apk.", item);
Assert.AreEqual (a.TextContent (), Encoding.ASCII.GetString (data), "The Contents of {0} should be \"{1}\"", item, a.TextContent ());
}
foreach (var a in proj.OtherBuildItems.Where (x => x is AndroidItem.AndroidAsset)) {
var item = a.Include ().ToLower ().Replace ("\\", "/");
if (item.EndsWith ("/"))
continue;
var data = ZipHelper.ReadFileFromZip (apk, item);
Assert.IsNotNull (data, "{0} should be in the apk.", item);
Assert.AreEqual (a.TextContent (), Encoding.ASCII.GetString (data), "The Contents of {0} should be \"{1}\"", item, a.TextContent ());
}
}
Directory.Delete (Path.Combine (Root, projectPath), recursive: true);
}
}
}
[Test]
public void FullPath ()
{
var assetPath = Path.GetFullPath (Path.Combine (Root, "temp", TestName, "Assets", "foo.txt"));
Directory.CreateDirectory (Path.GetDirectoryName (assetPath));
File.WriteAllText (assetPath, contents: "bar");
var proj = new XamarinAndroidLibraryProject {
OtherBuildItems = {
new AndroidItem.AndroidAsset (assetPath) {
Metadata = { { "LogicalName", Path.Combine ("Assets", "foo.txt") } },
},
},
};
using (var b = CreateDllBuilder (Path.Combine ("temp", TestName, "SubDir"))) {
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
var libraryProjectImports = Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath, "__AndroidLibraryProjects__.zip");
if (Builder.UseDotNet) {
var aarPath = Path.Combine (Root, b.ProjectDirectory, proj.OutputPath, $"{proj.ProjectName}.aar");
FileAssert.Exists (aarPath);
using (var aar = ZipHelper.OpenZip (aarPath)) {
aar.AssertEntryContents (aarPath, "assets/foo.txt", contents: "bar");
}
FileAssert.DoesNotExist (libraryProjectImports);
} else {
FileAssert.Exists (libraryProjectImports);
using (var zip = ZipHelper.OpenZip (libraryProjectImports)) {
zip.AssertEntryContents (libraryProjectImports, "library_project_imports/assets/foo.txt", contents: "bar");
}
}
}
}
}
}