Skip to content

Commit

Permalink
Added Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dellis1972 committed Feb 12, 2018
1 parent fd62d2f commit d3b1997
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class CalculateProjectDependencies : Task

public string ToolsVersion { get; set; }

public string NdkVersion { get; set; }

[Output]
public ITaskItem [] Dependencies { get; set; }

Expand Down Expand Up @@ -50,6 +52,9 @@ public override bool Execute ()
if (!string.IsNullOrEmpty (ToolsVersion)) {
dependencies.Add (CreateAndroidDependency ("tool", ToolsVersion));
}
if (!string.IsNullOrEmpty (NdkVersion)) {
dependencies.Add (CreateAndroidDependency ("ndk-bundle", NdkVersion));
}
Dependencies = dependencies.ToArray ();
return !Log.HasLoggedErrors;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
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;

namespace Xamarin.Android.Build.Tests {

[TestFixture]
[Parallelizable (ParallelScope.Children)]
public class GetDependenciesTest : BaseTest {

[Test]
public void ManifestFileDoesNotExist ()
{
IBuildEngine engine = new MockBuildEngine (TestContext.Out);
var task = new CalculateProjectDependencies {
BuildEngine = engine
};

task.BuildToolsVersion = "26.0.1";
task.TargetFrameworkVersion = "v8.0";
task.ManifestFile = new TaskItem ("AndroidManifest.xml");
task.Execute ();
Assert.IsNotNull (task.Dependencies);
Assert.AreEqual (4, task.Dependencies.Length);
Assert.IsNotNull (task.Dependencies.FirstOrDefault (x => x.ItemSpec == "build-tool" && x.GetMetadata ("Version") == "26.0.1"),
"Dependencies should contains a build-tool version 26.0.1");
Assert.IsNotNull (task.Dependencies.FirstOrDefault (x => x.ItemSpec == "platform" && x.GetMetadata ("Version") == "26"),
"Dependencies should contains a platform version 26");
}

[Test]
public void ManifestFileExists ()
{
IBuildEngine engine = new MockBuildEngine (TestContext.Out);
var task = new CalculateProjectDependencies {
BuildEngine = engine
};

var path = Path.Combine (Root, "temp", TestName);
Directory.CreateDirectory (path);
var manifestFile = Path.Combine (path, "AndroidManifest.xml");
File.WriteAllText (manifestFile, @"<?xml version='1.0' ?>
<manifest xmlns:android='http://schemas.android.com/apk/res/android' android:versionCode='1' android:versionName='1.0' package='Mono.Android_Tests'>
<uses-sdk android:minSdkVersion='10' />
</manifest>");

task.BuildToolsVersion = "26.0.1";
task.TargetFrameworkVersion = "v8.0";
task.ManifestFile = new TaskItem (manifestFile);
Assert.IsTrue(task.Execute ());
Assert.IsNotNull (task.Dependencies);
Assert.AreEqual (5, task.Dependencies.Length);
Assert.IsNotNull (task.Dependencies.FirstOrDefault (x => x.ItemSpec == "build-tool" && x.GetMetadata ("Version") == "26.0.1"),
"Dependencies should contain a build-tool version 26.0.1");
Assert.IsNotNull (task.Dependencies.FirstOrDefault (x => x.ItemSpec == "platform" && x.GetMetadata ("Version") == "26"),
"Dependencies should contain a platform version 26");
Assert.IsNotNull (task.Dependencies.FirstOrDefault (x => x.ItemSpec == "platform" && x.GetMetadata ("Version") == "10"),
"Dependencies should contain a platform version 10");

Directory.Delete (path, recursive: true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Build.Framework;

namespace Xamarin.Android.Build.Tests {
public class MockBuildEngine : IBuildEngine, IBuildEngine2, IBuildEngine3, IBuildEngine4 {
public MockBuildEngine (TextWriter output)
{
this.Output = output;
}

private TextWriter Output { get; }

int IBuildEngine.ColumnNumberOfTaskNode => -1;

bool IBuildEngine.ContinueOnError => false;

int IBuildEngine.LineNumberOfTaskNode => -1;

string IBuildEngine.ProjectFileOfTaskNode => "this.xml";

bool IBuildEngine2.IsRunningMultipleNodes => false;

bool IBuildEngine.BuildProjectFile (string projectFileName, string [] targetNames, IDictionary globalProperties, IDictionary targetOutputs) => true;

void IBuildEngine.LogCustomEvent (CustomBuildEventArgs e)
{
this.Output.WriteLine ($"Custom: {e.Message}");
}

void IBuildEngine.LogErrorEvent (BuildErrorEventArgs e)
{
this.Output.WriteLine ($"Error: {e.Message}");
}

void IBuildEngine.LogMessageEvent (BuildMessageEventArgs e)
{
this.Output.WriteLine ($"Message: {e.Message}");
}

void IBuildEngine.LogWarningEvent (BuildWarningEventArgs e)
{
this.Output.WriteLine ($"Warning: {e.Message}");
}

private Dictionary<object, object> Tasks = new Dictionary<object, object> ();

void IBuildEngine4.RegisterTaskObject (object key, object obj, RegisteredTaskObjectLifetime lifetime, bool allowEarlyCollection)
{
Tasks.Add (key, obj);
}

object IBuildEngine4.GetRegisteredTaskObject (object key, RegisteredTaskObjectLifetime lifetime)
{
return null;
}

object IBuildEngine4.UnregisterTaskObject (object key, RegisteredTaskObjectLifetime lifetime)
{
var obj = Tasks [key];
Tasks.Remove (key);
return obj;
}

BuildEngineResult IBuildEngine3.BuildProjectFilesInParallel (string [] projectFileNames, string [] targetNames, IDictionary [] globalProperties, IList<string> [] removeGlobalProperties, string [] toolsVersion, bool returnTargetOutputs)
{
throw new NotImplementedException ();
}

void IBuildEngine3.Yield () { }

void IBuildEngine3.Reacquire () { }

bool IBuildEngine2.BuildProjectFile (string projectFileName, string [] targetNames, IDictionary globalProperties, IDictionary targetOutputs, string toolsVersion) => true;

bool IBuildEngine2.BuildProjectFilesInParallel (string [] projectFileNames, string [] targetNames, IDictionary [] globalProperties, IDictionary [] targetOutputsPerProject, string [] toolsVersion, bool useResultsCache, bool unloadProjectsOnCompletion) => true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
<Compile Include="$(MSBuildThisFileDirectory)PackagingTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\BaseTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\BuildHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\MockBuildEngine.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="$(MSBuildThisFileDirectory)Utilities\" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2781,6 +2781,7 @@ because xbuild doesn't support framework reference assemblies.
BuildToolsVersion="$(AndroidSdkBuildToolsVersion)"
PlatformToolsVersion="$(AndroidSdkPlatformToolsVersion)"
ToolsVersion="$(AndroidSdkToolsVersion)"
NdkVersion="$(AndroidNdkVersion)"
>
<Output TaskParameter="Dependencies" ItemName="AndroidDependency" />
</CalculateProjectDependencies>
Expand Down

0 comments on commit d3b1997

Please sign in to comment.