Skip to content

Commit

Permalink
Add Android unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
premun committed May 7, 2021
1 parent f93e9e5 commit ad16487
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Arcade.Common;
using Microsoft.Arcade.Test.Common;
using Microsoft.Build.Framework;
using Microsoft.DotNet.Internal.DependencyInjection.Testing;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Xunit;

#nullable enable
namespace Microsoft.DotNet.Helix.Sdk.Tests
{
public class CreateXHarnessAndroidWorkItemsTests
{
private readonly MockFileSystem _fileSystem;
private readonly Mock<IZipArchiveManager> _zipArchiveManager;
private readonly CreateXHarnessAndroidWorkItems _task;

public CreateXHarnessAndroidWorkItemsTests()
{
_fileSystem = new MockFileSystem();
_zipArchiveManager = new();
_zipArchiveManager.SetReturnsDefault(Task.CompletedTask);
_zipArchiveManager
.Setup(x => x.ArchiveFile(It.IsAny<string>(), It.IsAny<string>()))
.Callback<string, string>((folder, zipPath) =>
{
_fileSystem.Files.Add(zipPath, "zip of " + folder);
});

_task = new CreateXHarnessAndroidWorkItems()
{
BuildEngine = new MockBuildEngine(),
};
}

[Fact]
public void MissingApkNamePropertyIsCaught()
{
var collection = CreateMockServiceCollection();
_task.ConfigureServices(collection);
_task.Apks = new[]
{
CreateApk("/apks/System.Foo.app", null!)
};

// Act
using var provider = collection.BuildServiceProvider();
_task.InvokeExecute(provider).Should().BeFalse();

// Verify
_task.WorkItems.Length.Should().Be(0);
}

[Fact]
public void AndroidXHarnessWorkItemIsCreated()
{
var collection = CreateMockServiceCollection();
_task.ConfigureServices(collection);
_task.Apks = new[]
{
CreateApk("/apks/System.Foo.apk", "System.Foo", "00:15:42", "00:08:55")
};

// Act
using var provider = collection.BuildServiceProvider();
_task.InvokeExecute(provider).Should().BeTrue();

// Verify
_task.WorkItems.Length.Should().Be(1);

var workItem = _task.WorkItems.First();
workItem.GetMetadata("Identity").Should().Be("System.Foo");
workItem.GetMetadata("Timeout").Should().Be("00:15:42");

var payloadArchive = workItem.GetMetadata("PayloadArchive");
payloadArchive.Should().NotBeNullOrEmpty();
_fileSystem.FileExists(payloadArchive).Should().BeTrue();

var command = workItem.GetMetadata("Command");
command.Should().Contain("--timeout \"00:08:55\"");

_zipArchiveManager
.Verify(x => x.AddResourceFileToArchive<CreateXHarnessAndroidWorkItems>(payloadArchive, It.Is<string>(s => s.Contains("xharness-helix-job.android.sh")), It.IsAny<string>()), Times.AtLeastOnce);
_zipArchiveManager
.Verify(x => x.AddResourceFileToArchive<CreateXHarnessAndroidWorkItems>(payloadArchive, It.Is<string>(s => s.Contains("xharness-helix-job.android.bat")), It.IsAny<string>()), Times.AtLeastOnce);
}

[Fact]
public void ArchivePayloadIsOverwritten()
{
var collection = CreateMockServiceCollection();
_task.ConfigureServices(collection);
_task.Apks = new[]
{
CreateApk("apks/System.Foo.apk", "System.Foo"),
CreateApk("apks/System.Bar.apk", "System.Bar"),
};

_fileSystem.Files.Add("apks/xharness-apk-payload-system.foo.zip", "archive");

// Act
using var provider = collection.BuildServiceProvider();
_task.InvokeExecute(provider).Should().BeTrue();

// Verify
_task.WorkItems.Length.Should().Be(2);

var workItem = _task.WorkItems.Last();
workItem.GetMetadata("Identity").Should().Be("System.Bar");

workItem = _task.WorkItems.First();
workItem.GetMetadata("Identity").Should().Be("System.Foo");

var payloadArchive = workItem.GetMetadata("PayloadArchive");
payloadArchive.Should().NotBeNullOrEmpty();
_fileSystem.FileExists(payloadArchive).Should().BeTrue();
_fileSystem.RemovedFiles.Should().Contain(payloadArchive);
}

[Fact]
public void AreDependenciesRegistered()
{
var task = new CreateXHarnessAndroidWorkItems();

var collection = new ServiceCollection();
task.ConfigureServices(collection);
var provider = collection.BuildServiceProvider();

DependencyInjectionValidation.IsDependencyResolutionCoherent(
s => task.ConfigureServices(s),
out string message,
additionalSingletonTypes: task.GetExecuteParameterTypes()
)
.Should()
.BeTrue(message);
}

private ITaskItem CreateApk(
string path,
string apkName,
string? workItemTimeout = null,
string? testTimeout = null,
int expectedExitCode = 0)
{
var mockBundle = new Mock<ITaskItem>();
mockBundle.SetupGet(x => x.ItemSpec).Returns(path);
mockBundle.Setup(x => x.GetMetadata("AndroidPackageName")).Returns(apkName);

if (workItemTimeout != null)
{
mockBundle.Setup(x => x.GetMetadata("WorkItemTimeout")).Returns(workItemTimeout);
}

if (testTimeout != null)
{
mockBundle.Setup(x => x.GetMetadata("TestTimeout")).Returns(testTimeout);
}

if (expectedExitCode != 0)
{
mockBundle.Setup(x => x.GetMetadata("ExpectedExitCode")).Returns(expectedExitCode.ToString());
}

_fileSystem.WriteToFile(path, "apk");

return mockBundle.Object;
}

private IServiceCollection CreateMockServiceCollection()
{
var collection = new ServiceCollection();
collection.AddSingleton<IFileSystem>(_fileSystem);
collection.AddSingleton(_zipArchiveManager.Object);
return collection;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Arcade.Common;
using Microsoft.Arcade.Test.Common;
using Microsoft.Build.Framework;
using Microsoft.DotNet.Internal.DependencyInjection.Testing;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Xunit;
using Microsoft.DotNet.Arcade.Test.Common;
using Microsoft.DotNet.Internal.DependencyInjection.Testing;
using Microsoft.Build.Framework;

#nullable enable
namespace Microsoft.DotNet.Helix.Sdk.Tests
Expand Down Expand Up @@ -144,15 +137,12 @@ public void AreDependenciesRegistered()
var provider = collection.BuildServiceProvider();

DependencyInjectionValidation.IsDependencyResolutionCoherent(
s =>
{
task.ConfigureServices(s);
},
out string message,
additionalSingletonTypes: task.GetExecuteParameterTypes()
)
.Should()
.BeTrue(message);
s => task.ConfigureServices(s),
out string message,
additionalSingletonTypes: task.GetExecuteParameterTypes()
)
.Should()
.BeTrue(message);
}

private ITaskItem CreateAppBundle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class CreateXHarnessAndroidWorkItems : XHarnessTaskBase
private const string ArgumentsPropName = "Arguments";
private const string AndroidInstrumentationNamePropName = "AndroidInstrumentationName";
private const string DeviceOutputPathPropName = "DeviceOutputPath";
private const string AndroidPackageNamePropName = "AndroidPackageName";

private const string PosixAndroidWrapperScript = "tools.xharness_runner.xharness-helix-job.android.sh";
private const string NonPosixAndroidWrapperScript = "tools.xharness_runner.xharness-helix-job.android.bat";
Expand Down Expand Up @@ -47,7 +48,7 @@ public override void ConfigureServices(IServiceCollection collection)
/// collates logged errors in order to determine the success of HelixWorkItems
/// </summary>
/// <returns>A boolean value indicating the success of HelixWorkItem creation</returns>
public bool Execute(IZipArchiveManager zipArchiveManager, IFileSystem fileSystem)
public bool ExecuteTask(IZipArchiveManager zipArchiveManager, IFileSystem fileSystem)
{
var tasks = Apks.Select(apk => PrepareWorkItem(zipArchiveManager, fileSystem, apk));

Expand Down Expand Up @@ -114,9 +115,9 @@ private async Task<string> CreateZipArchiveOfPackageAsync(IZipArchiveManager zip
private string ValidateMetadataAndGetXHarnessAndroidCommand(ITaskItem appPackage, TimeSpan xHarnessTimeout, int expectedExitCode)
{
// Validation of any metadata specific to Android stuff goes here
if (!appPackage.GetRequiredMetadata(Log, "AndroidPackageName", out string androidPackageName))
if (!appPackage.GetRequiredMetadata(Log, AndroidPackageNamePropName, out string androidPackageName))
{
Log.LogError("AndroidPackageName metadata must be specified; this may match, but can vary from file name");
Log.LogError($"{AndroidPackageNamePropName} metadata must be specified; this may match, but can vary from file name");
return null;
}

Expand Down

0 comments on commit ad16487

Please sign in to comment.