Skip to content

Commit c80472e

Browse files
dellis1972jonpryor
authored andcommitted
[tests] .NET Standard Support (#846)
Add .NET Standard support to our MSBuild unit test system. You can now create a new `DotNetStandard` project and add that as a reference. The builder also has a new property `$(RequiresMSBuild)` which is used to force the system to use `msbuild` for the underlying build process.
1 parent 52fa433 commit c80472e

File tree

12 files changed

+595
-158
lines changed

12 files changed

+595
-158
lines changed

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/PackagingTest.cs

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,5 +223,230 @@ public void CheckSignApk ([Values(true, false)] bool useApkSigner)
223223
Assert.IsTrue (b.Build (proj), "build failed");
224224
}
225225
}
226+
227+
[Test]
228+
public void NetStandardReferenceTest ()
229+
{
230+
var netStandardProject = new DotNetStandard () {
231+
Language = XamarinAndroidProjectLanguage.CSharp,
232+
ProjectName = "XamFormsSample",
233+
ProjectGuid = Guid.NewGuid ().ToString (),
234+
Sdk = "Microsoft.NET.Sdk",
235+
TargetFramework = "netstandard1.4",
236+
IsRelease = true,
237+
PackageTargetFallback = "portable-net45+win8+wpa81+wp8",
238+
PackageReferences = {
239+
KnownPackages.XamarinFormsPCL_2_3_4_231,
240+
new Package () {
241+
Id = "System.IO.Packaging",
242+
Version = "4.4.0",
243+
},
244+
new Package () {
245+
Id = "Newtonsoft.Json",
246+
Version = "10.0.3"
247+
},
248+
},
249+
OtherBuildItems = {
250+
new BuildItem ("None") {
251+
Remove = () => "**\\*.xaml",
252+
},
253+
new BuildItem ("Compile") {
254+
Update = () => "**\\*.xaml.cs",
255+
DependentUpon = () => "%(Filename)"
256+
},
257+
new BuildItem ("EmbeddedResource") {
258+
Include = () => "**\\*.xaml",
259+
SubType = () => "Designer",
260+
Generator = () => "MSBuild:UpdateDesignTimeXaml",
261+
},
262+
},
263+
Sources = {
264+
new BuildItem.Source ("App.xaml.cs") {
265+
TextContent = () => @"using System;
266+
using System.Collections.Generic;
267+
using System.Linq;
268+
using System.Text;
269+
using Newtonsoft.Json;
270+
271+
using Xamarin.Forms;
272+
273+
namespace XamFormsSample
274+
{
275+
public partial class App : Application
276+
{
277+
public App()
278+
{
279+
JsonConvert.DeserializeObject<string>(""test"");
280+
InitializeComponent();
281+
}
282+
283+
protected override void OnStart()
284+
{
285+
// Handle when your app starts
286+
}
287+
288+
protected override void OnSleep()
289+
{
290+
// Handle when your app sleeps
291+
}
292+
293+
protected override void OnResume()
294+
{
295+
// Handle when your app resumes
296+
}
297+
}
298+
}",
299+
},
300+
new BuildItem.Source ("App.xaml") {
301+
TextContent = () => @"<?xml version=""1.0"" encoding=""utf-8"" ?>
302+
<Application xmlns=""http://xamarin.com/schemas/2014/forms""
303+
xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""
304+
x:Class=""XamFormsSample.App"">
305+
<Application.Resources>
306+
<!-- Application resource dictionary -->
307+
</Application.Resources>
308+
</Application>",
309+
},
310+
},
311+
};
312+
313+
var app = new XamarinAndroidApplicationProject () {
314+
ProjectName = "App1",
315+
IsRelease = true,
316+
UseLatestPlatformSdk = true,
317+
References = {
318+
new BuildItem.Reference ("Mono.Android.Export"),
319+
new BuildItem.ProjectReference ($"..\\{netStandardProject.ProjectName}\\{netStandardProject.ProjectName}.csproj",
320+
netStandardProject.ProjectName, netStandardProject.ProjectGuid),
321+
},
322+
PackageReferences = {
323+
KnownPackages.SupportDesign_25_4_0_1,
324+
KnownPackages.SupportV7CardView_24_2_1,
325+
KnownPackages.AndroidSupportV4_25_4_0_1,
326+
KnownPackages.SupportCoreUtils_25_4_0_1,
327+
KnownPackages.SupportMediaCompat_25_4_0_1,
328+
KnownPackages.SupportFragment_25_4_0_1,
329+
KnownPackages.SupportCoreUI_25_4_0_1,
330+
KnownPackages.SupportCompat_25_4_0_1,
331+
KnownPackages.SupportV7AppCompat_25_4_0_1,
332+
KnownPackages.XamarinForms_2_3_4_231,
333+
}
334+
};
335+
app.SetProperty (KnownProperties.AndroidSupportedAbis, "x86;armeabi-v7a");
336+
var expectedFiles = new string [] {
337+
"Java.Interop.dll",
338+
"Mono.Android.dll",
339+
"mscorlib.dll",
340+
"mscorlib.dll.mdb",
341+
"System.Collections.Concurrent.dll",
342+
"System.Collections.dll",
343+
"System.Core.dll",
344+
"System.Diagnostics.Debug.dll",
345+
"System.dll",
346+
"System.Linq.dll",
347+
"System.Reflection.dll",
348+
"System.Reflection.Extensions.dll",
349+
"System.Runtime.dll",
350+
"System.Runtime.Extensions.dll",
351+
"System.Runtime.InteropServices.dll",
352+
"System.Runtime.Serialization.dll",
353+
"System.Threading.dll",
354+
"System.IO.Packaging.dll",
355+
"System.IO.Compression.dll",
356+
"System.IO.Compression.pdb",
357+
"Mono.Android.Export.dll",
358+
"Mono.Android.Export.pdb",
359+
"App1.dll",
360+
"App1.pdb",
361+
"FormsViewGroup.dll",
362+
"FormsViewGroup.dll.mdb",
363+
"Xamarin.Android.Support.Compat.dll",
364+
"Xamarin.Android.Support.Core.UI.dll",
365+
"Xamarin.Android.Support.Core.Utils.dll",
366+
"Xamarin.Android.Support.Design.dll",
367+
"Xamarin.Android.Support.Fragment.dll",
368+
"Xamarin.Android.Support.Media.Compat.dll",
369+
"Xamarin.Android.Support.v4.dll",
370+
"Xamarin.Android.Support.v7.AppCompat.dll",
371+
"Xamarin.Android.Support.Animated.Vector.Drawable.dll",
372+
"Xamarin.Android.Support.Vector.Drawable.dll",
373+
"Xamarin.Android.Support.Transition.dll",
374+
"Xamarin.Android.Support.v7.MediaRouter.dll",
375+
"Xamarin.Android.Support.v7.RecyclerView.dll",
376+
"Xamarin.Android.Support.Annotations.dll",
377+
"Xamarin.Android.Support.v7.CardView.dll",
378+
"Xamarin.Forms.Core.dll",
379+
"Xamarin.Forms.Core.dll.mdb",
380+
"Xamarin.Forms.Platform.Android.dll",
381+
"Xamarin.Forms.Platform.Android.dll.mdb",
382+
"Xamarin.Forms.Platform.dll",
383+
"Xamarin.Forms.Xaml.dll",
384+
"Xamarin.Forms.Xaml.dll.mdb",
385+
"XamFormsSample.dll",
386+
"XamFormsSample.pdb",
387+
"Mono.Android.pdb",
388+
"System.Core.pdb",
389+
"System.pdb",
390+
"Mono.Security.dll",
391+
"Mono.Security.pdb",
392+
"System.Xml.dll",
393+
"System.Xml.pdb",
394+
"System.ComponentModel.Composition.dll",
395+
"System.ComponentModel.Composition.pdb",
396+
"System.Net.Http.dll",
397+
"System.Net.Http.pdb",
398+
"System.Runtime.Serialization.pdb",
399+
"System.ServiceModel.Internals.dll",
400+
"System.ServiceModel.Internals.pdb",
401+
"System.Threading.Tasks.dll",
402+
"System.ObjectModel.dll",
403+
"System.Globalization.dll",
404+
"System.ComponentModel.dll",
405+
"System.Xml.ReaderWriter.dll",
406+
"System.Linq.Expressions.dll",
407+
"System.IO.dll",
408+
"System.Dynamic.Runtime.dll",
409+
"System.Text.RegularExpressions.dll",
410+
"System.Diagnostics.Tools.dll",
411+
"Newtonsoft.Json.dll",
412+
"Microsoft.CSharp.dll",
413+
"System.Numerics.dll",
414+
"System.Xml.Linq.dll",
415+
};
416+
var path = Path.Combine ("temp", TestContext.CurrentContext.Test.Name);
417+
using (var builder = CreateDllBuilder (Path.Combine (path, netStandardProject.ProjectName), cleanupOnDispose: false)) {
418+
if (!Directory.Exists (builder.MicrosoftNetSdkDirectory))
419+
Assert.Ignore ("Microsoft.NET.Sdk not found.");
420+
builder.RequiresMSBuild = true;
421+
builder.Target = "Restore";
422+
Assert.IsTrue (builder.Build (netStandardProject), "XamFormsSample Nuget packages should have been restored.");
423+
builder.Target = "Build";
424+
Assert.IsTrue (builder.Build (netStandardProject), "XamFormsSample should have built.");
425+
using (var ab = CreateApkBuilder (Path.Combine (path, app.ProjectName), cleanupOnDispose: false)) {
426+
ab.RequiresMSBuild = true;
427+
ab.Target = "Restore";
428+
Assert.IsTrue (ab.Build (app), "App should have built.");
429+
ab.Target = "SignAndroidPackage";
430+
Assert.IsTrue (ab.Build (app), "App should have built.");
431+
var apk = Path.Combine (Root, ab.ProjectDirectory,
432+
app.IntermediateOutputPath, "android", "bin", "UnnamedProject.UnnamedProject.apk");
433+
using (var zip = ZipHelper.OpenZip (apk)) {
434+
var existingFiles = zip.Where (a => a.FullName.StartsWith ("assemblies/", StringComparison.InvariantCultureIgnoreCase));
435+
var missingFiles = expectedFiles.Where (x => !zip.ContainsEntry ("assmelbies/" + Path.GetFileName (x)));
436+
Assert.IsTrue (missingFiles.Any (),
437+
string.Format ("The following Expected files are missing. {0}",
438+
string.Join (Environment.NewLine, missingFiles)));
439+
var additionalFiles = existingFiles.Where (x => !expectedFiles.Contains (Path.GetFileName (x.FullName)));
440+
Assert.IsTrue (!additionalFiles.Any (),
441+
string.Format ("Unexpected Files found! {0}",
442+
string.Join (Environment.NewLine, additionalFiles.Select (x => x.FullName))));
443+
}
444+
if (!HasDevices)
445+
Assert.Ignore ("Skipping Installation. No devices available.");
446+
ab.Target = "Install";
447+
Assert.IsTrue (ab.Build (app), "App should have installed.");
448+
}
449+
}
450+
}
226451
}
227452
}

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/KnownPackages.cs

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static class KnownPackages
6666
new BuildItem.Reference ("Xamarin.Android.Support.v13") {
6767
MetadataValues = "HintPath=..\\packages\\Xamarin.Android.Support.v13.20.0.0.4\\lib\\MonoAndroid32\\Xamarin.Android.Support.v13.dll" }
6868
}
69-
};
69+
};
7070
public static Package AndroidSupportV13Beta = new Package () {
7171
Id = "Xamarin.Android.Support.v13",
7272
Version = "21.0.0.0-beta1",
@@ -93,20 +93,20 @@ public static class KnownPackages
9393
new BuildItem.Reference ("Xamarin.Android.Wearable") {
9494
MetadataValues = "HintPath=..\\packages\\Xamarin.Android.Wear.1.0.0-preview7\\lib\\MonoAndroid10\\Xamarin.Android.Wearable.dll" }
9595
}
96-
};
96+
};
9797
public static Package SupportV7RecyclerView = new Package {
9898
Id = "Xamarin.Android.Support.v7.RecyclerView",
99-
Version="21.0.0.0-beta1",
100-
TargetFramework="MonoAndroid523",
99+
Version = "21.0.0.0-beta1",
100+
TargetFramework = "MonoAndroid523",
101101
References = {
102102
new BuildItem.Reference ("Xamarin.Android.Support.V7.RecyclerView") {
103103
MetadataValues = "HintPath=..\\packages\\Xamarin.Android.Support.v7.RecyclerView.21.0.0.0-beta1\\lib\\MonoAndroid\\Xamarin.Android.Support.v7.RecyclerView.dll" }
104104
}
105-
};
105+
};
106106
public static Package SupportV7CardView = new Package {
107107
Id = "Xamarin.Android.Support.v7.Cardview",
108-
Version="21.0.3.0",
109-
TargetFramework="MonoAndroid523",
108+
Version = "21.0.3.0",
109+
TargetFramework = "MonoAndroid523",
110110
References = {
111111
new BuildItem.Reference ("Xamarin.Android.Support.v7.CardView") {
112112
MetadataValues = "HintPath=..\\packages\\Xamarin.Android.Support.v7.CardView.21.0.3.0\\lib\\MonoAndroid403\\Xamarin.Android.Support.v7.CardView.dll" }
@@ -121,6 +121,15 @@ public static class KnownPackages
121121
MetadataValues = "HintPath=..\\packages\\Xamarin.Android.Support.v7.CardView.24.2.1\\lib\\MonoAndroid70\\Xamarin.Android.Support.v7.CardView.dll" }
122122
}
123123
};
124+
public static Package SupportV7CardView_25_4_0_1 = new Package {
125+
Id = "Xamarin.Android.Support.v7.Cardview",
126+
Version = "25.4.0.1",
127+
TargetFramework = "MonoAndroid70",
128+
References = {
129+
new BuildItem.Reference ("Xamarin.Android.Support.v7.CardView") {
130+
MetadataValues = "HintPath=..\\packages\\Xamarin.Android.Support.v7.CardView.25.4.0.1\\lib\\MonoAndroid70\\Xamarin.Android.Support.v7.CardView.dll" }
131+
}
132+
};
124133
public static Package SupportV7AppCompat_21_0_3_0 = new Package {
125134
Id = "Xamarin.Android.Support.v7.AppCompat",
126135
Version = "21.0.3.0",
@@ -220,6 +229,15 @@ public static class KnownPackages
220229
MetadataValues = "HintPath=..\\packages\\Xamarin.Android.Support.v7.Palette.22.1.1.1\\lib\\MonoAndroid403\\Xamarin.Android.Support.v7.Palette.dll" }
221230
}
222231
};
232+
public static Package SupportDesign_25_4_0_1 = new Package {
233+
Id = "Xamarin.Android.Support.Design",
234+
Version = "25.4.0.1",
235+
TargetFramework = "MonoAndroid70",
236+
References = {
237+
new BuildItem.Reference ("Xamarin.Android.Support.Design") {
238+
MetadataValues = "HintPath=..\\packages\\Xamarin.Android.Support.Design.25.4.0.1\\lib\\MonoAndroid70\\Xamarin.Android.Support.Design.dll" }
239+
}
240+
};
223241
public static Package GooglePlayServices_22_0_0_2 = new Package {
224242
Id = "Xamarin.GooglePlayServices",
225243
Version = "22.0.0.2",
@@ -265,6 +283,41 @@ public static class KnownPackages
265283
},
266284
}
267285
};
286+
public static Package XamarinFormsPCL_2_3_4_231 = new Package {
287+
Id = "Xamarin.Forms",
288+
Version = "2.3.4.231",
289+
TargetFramework = "portable-net45+win+wp80+MonoAndroid10+xamarinios10+MonoTouch10",
290+
References = {
291+
new BuildItem.Reference ("Xamarin.Forms.Core") {
292+
MetadataValues = "HintPath=..\\packages\\Xamarin.Forms.2.3.4.231\\lib\\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10\\Xamarin.Forms.Core.dll"
293+
},
294+
new BuildItem.Reference ("Xamarin.Forms.Xaml") {
295+
MetadataValues = "HintPath=..\\packages\\Xamarin.Forms.2.3.4.231\\lib\\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10\\Xamarin.Forms.Xaml.dll"
296+
},
297+
}
298+
};
299+
public static Package XamarinForms_2_3_4_231 = new Package {
300+
Id = "Xamarin.Forms",
301+
Version = "2.3.4.231",
302+
TargetFramework = "MonoAndroid44",
303+
References = {
304+
new BuildItem.Reference ("Xamarin.Forms.Platform.Android") {
305+
MetadataValues = "HintPath=..\\packages\\Xamarin.Forms.2.3.4.231\\lib\\MonoAndroid10\\Xamarin.Forms.Platform.Android.dll"
306+
},
307+
new BuildItem.Reference ("FormsViewGroup") {
308+
MetadataValues = "HintPath=..\\packages\\Xamarin.Forms.2.3.4.231\\lib\\MonoAndroid10\\FormsViewGroup.dll"
309+
},
310+
new BuildItem.Reference ("Xamarin.Forms.Core") {
311+
MetadataValues = "HintPath=..\\packages\\Xamarin.Forms.2.3.4.231\\lib\\MonoAndroid10\\Xamarin.Forms.Core.dll"
312+
},
313+
new BuildItem.Reference ("Xamarin.Forms.Xaml") {
314+
MetadataValues = "HintPath=..\\packages\\Xamarin.Forms.2.3.4.231\\lib\\MonoAndroid10\\Xamarin.Forms.Xaml.dll"
315+
},
316+
new BuildItem.Reference ("Xamarin.Forms.Platform") {
317+
MetadataValues = "HintPath=..\\packages\\Xamarin.Forms.2.3.4.231\\lib\\MonoAndroid10\\Xamarin.Forms.Platform.dll"
318+
},
319+
}
320+
};
268321
public static Package CocosSharp_PCL_Shared_1_5_0_0 = new Package {
269322
Id = "CocosSharp.PCL.Shared",
270323
Version = "1.5.0.0",

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XamarinAndroidProject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Xamarin.ProjectTools
99
{
10-
public abstract class XamarinAndroidProject : XamarinProject
10+
public abstract class XamarinAndroidProject : DotNetXamarinProject
1111
{
1212
protected XamarinAndroidProject (string debugConfigurationName = "Debug", string releaseConfigurationName = "Release")
1313
: base (debugConfigurationName, releaseConfigurationName)

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/BuildActions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public static class BuildActions
1111
{
1212
public const string None = "None";
1313
public const string ProjectReference = "ProjectReference";
14+
public const string PackageReference = "PackageReference";
1415
public const string Reference = "Reference";
1516
public const string Compile = "Compile";
1617
public const string EmbeddedResource = "EmbeddedResource";

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/BuildItem.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,31 @@ public BuildItem (string buildAction, string include)
9090
{
9191
}
9292

93-
public BuildItem (string buildAction, Func<string> include)
93+
public BuildItem (string buildAction, Func<string> include = null)
9494
{
9595
BuildAction = buildAction;
9696
Include = include;
9797
Metadata = new Dictionary<string, string> ();
9898
Timestamp = DateTimeOffset.UtcNow;
9999
Encoding = Encoding.UTF8;
100100
Attributes = FileAttributes.Normal;
101+
Generator = null;
102+
Remove = null;
103+
SubType = null;
104+
Update = null;
105+
DependentUpon = null;
106+
Version = null;
101107
}
102108

103109
public DateTimeOffset? Timestamp { get; set; }
104110
public string BuildAction { get; set; }
105111
public Func<string> Include { get; set; }
112+
public Func<string> Remove { get; set; }
113+
public Func<string> Update { get; set; }
114+
public Func<string> SubType { get; set; }
115+
public Func<string> Generator { get; set; }
116+
public Func<string> DependentUpon { get; set; }
117+
public Func<string> Version { get; set; }
106118
public IDictionary<string,string> Metadata { get; private set; }
107119
public Func<string> TextContent { get; set; }
108120
public Func<byte[]> BinaryContent { get; set; }

0 commit comments

Comments
 (0)