Skip to content

Commit

Permalink
[Xamarin.Android.Tools.Aidl-Tests] Add unit tests (#5756)
Browse files Browse the repository at this point in the history
Context: #5726

We have someone willing to contribute fixes for
`Xamarin.Android.Tools.Aidl.dll` (yay!).  However, we no longer have
any expertise with AIDL available on the team, and there are no unit
tests for this area.  This makes us very reluctant to make changes
which could potentially break the currently working aspects of AIDL.

Add a new `tests/Xamarin.Android.Tools.Aidl-Tests` unit test project
and seed it with a few examples from our repo and a few large examples
from other OSS repos.

Note: this does *not* mean that the behavior tested by these tests is
necessarily *correct*.  It simply reflects what the code does *today*,
in order that we can see any changes made by future PR's.

~~ Note ~~

The format for test data files is delimited by 4 pound signs as:

	<input>

	####

	<output>

Test results can be generated by:

  - Placing the input in a test file
  - Enabling the `GENERATE_OUTPUT` define at the top of
    `AidlCompilerTestBase.cs`
  - Running the test
  - The test file in `bin/TestData` will now contain the test output
    which can be copied back to the original test data file.
  • Loading branch information
jpobst authored Mar 25, 2021
1 parent 4e37293 commit 81d37a1
Show file tree
Hide file tree
Showing 15 changed files with 16,648 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Xamarin.Android-Tests.sln
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Java.Interop-Tests", "tests
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Java.Interop", "external\Java.Interop\src\Java.Interop\Java.Interop.csproj", "{94BD81F7-B06F-4295-9636-F8A3B6BDC762}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xamarin.Android.Tools.Aidl-Tests", "tests\Xamarin.Android.Tools.Aidl-Tests\Xamarin.Android.Tools.Aidl-Tests.csproj", "{883941C8-C4ED-428D-A7D2-26F2EEA23F92}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xamarin.Android.Tools.Aidl", "src\Xamarin.Android.Tools.Aidl\Xamarin.Android.Tools.Aidl.csproj", "{302D9D2E-1F98-4374-9B6B-922F78620C4B}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
tests\Mono.Android-Tests\Mono.Android-Test.Shared.projitems*{0ab4956e-6fb9-4da0-9d49-ab65a3ff403a}*SharedItemsImports = 13
Expand Down Expand Up @@ -241,6 +245,14 @@ Global
{94BD81F7-B06F-4295-9636-F8A3B6BDC762}.Debug|Any CPU.Build.0 = Debug|Any CPU
{94BD81F7-B06F-4295-9636-F8A3B6BDC762}.Release|Any CPU.ActiveCfg = Release|Any CPU
{94BD81F7-B06F-4295-9636-F8A3B6BDC762}.Release|Any CPU.Build.0 = Release|Any CPU
{883941C8-C4ED-428D-A7D2-26F2EEA23F92}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{883941C8-C4ED-428D-A7D2-26F2EEA23F92}.Debug|Any CPU.Build.0 = Debug|Any CPU
{883941C8-C4ED-428D-A7D2-26F2EEA23F92}.Release|Any CPU.ActiveCfg = Release|Any CPU
{883941C8-C4ED-428D-A7D2-26F2EEA23F92}.Release|Any CPU.Build.0 = Release|Any CPU
{302D9D2E-1F98-4374-9B6B-922F78620C4B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{302D9D2E-1F98-4374-9B6B-922F78620C4B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{302D9D2E-1F98-4374-9B6B-922F78620C4B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{302D9D2E-1F98-4374-9B6B-922F78620C4B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -278,6 +290,8 @@ Global
{D1243BAB-23CA-4566-A2A3-3ADA2C2DC3AF} = {EFBC4DC0-DBFF-4DAA-B0B8-6D0CB02A25F5}
{6CB00820-A66B-43E5-8785-ED456C6E9F39} = {EFBC4DC0-DBFF-4DAA-B0B8-6D0CB02A25F5}
{94BD81F7-B06F-4295-9636-F8A3B6BDC762} = {EFBC4DC0-DBFF-4DAA-B0B8-6D0CB02A25F5}
{883941C8-C4ED-428D-A7D2-26F2EEA23F92} = {EFBC4DC0-DBFF-4DAA-B0B8-6D0CB02A25F5}
{302D9D2E-1F98-4374-9B6B-922F78620C4B} = {EFBC4DC0-DBFF-4DAA-B0B8-6D0CB02A25F5}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8643CD20-B195-4919-8135-27549488237E}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ jobs:
dotNetTestExtraArgs: --filter "TestCategory != Node-1 & TestCategory != Node-2 & TestCategory != Node-3"
testResultsFile: TestResult-MSBuildTests-macOS-NoNode-$(XA.Build.Configuration).xml

# Only run these tests on node 2
- ${{ if eq(parameters.run_extra_tests, true) }}:
- template: run-nunit-tests.yaml
parameters:
useDotNet: $(UseDotNet)
testRunTitle: Xamarin.Android.Tools.Aidl-Tests - macOS
testAssembly: $(System.DefaultWorkingDirectory)/bin/Test$(XA.Build.Configuration)/${{ parameters.target_framework }}/Xamarin.Android.Tools.Aidl-Tests.dll
testResultsFile: TestResult-Aidl-Tests-macOS-$(XA.Build.Configuration).xml

- template: upload-results.yaml
parameters:
artifactName: Test Results - MSBuild ${{ parameters.job_suffix }} - macOS-${{ parameters.node_id }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ jobs:
dotNetTestExtraArgs: --filter "TestCategory != Node-1 & TestCategory != Node-2 & TestCategory != Node-3"
testResultsFile: TestResult-MSBuildTests-Windows-NoNode-$(XA.Build.Configuration).xml

# Only run these tests on node 2
- ${{ if eq(parameters.run_extra_tests, true) }}:
- template: run-nunit-tests.yaml
parameters:
useDotNet: $(UseDotNet)
testRunTitle: Xamarin.Android.Tools.Aidl-Tests - Windows
testAssembly: $(System.DefaultWorkingDirectory)\bin\Test$(XA.Build.Configuration)\${{ parameters.target_framework }}\Xamarin.Android.Tools.Aidl-Tests.dll
testResultsFile: TestResult-Aidl-Tests-Windows-$(XA.Build.Configuration).xml

- template: upload-results.yaml
parameters:
artifactName: Test Results - MSBuild ${{ parameters.job_suffix }} - Windows-${{ parameters.node_id }}
Expand Down
1 change: 1 addition & 0 deletions build-tools/scripts/RunTests.targets
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</PropertyGroup>
<ItemGroup>
<_TestAssembly Include="$(_TopDir)\bin\Test$(Configuration)\net472\Xamarin.Android.Build.Tests.dll" />
<_TestAssembly Include="$(_TopDir)\bin\Test$(Configuration)\net472\Xamarin.Android.Tools.Aidl-Tests.dll" />
<_ApkTestProject Include="$(_TopDir)\tests\Mono.Android-Tests\Mono.Android-Tests.csproj" />
<_ApkTestProject Include="$(_TopDir)\tests\CodeGen-Binding\Xamarin.Android.JcwGen-Tests\Xamarin.Android.JcwGen-Tests.csproj" />
<_ApkTestProject Include="$(_TopDir)\tests\CodeGen-MkBundle\Xamarin.Android.MakeBundle-Tests\Xamarin.Android.MakeBundle-Tests.csproj" />
Expand Down
35 changes: 35 additions & 0 deletions src/Xamarin.Android.Tools.Aidl/AidlCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,41 @@ public Result Run (ConverterOptions opts, Func<string,AssemblyDefinition> resolv

return result;
}

// This overload is primarily for unit tests.
public Result Run (string input, out string output, AssemblyDefinition[] references = null, ParcelableHandling parcelableHandling = ParcelableHandling.Ignore)
{
var result = new Result ();
var database = new BindingDatabase (references ?? Array.Empty<AssemblyDefinition> ());
var lang = new LanguageData (new AidlGrammar () { LanguageFlags = LanguageFlags.Default | LanguageFlags.CreateAst });
var parser = new Parser (lang);

var pt = parser.Parse (input);

if (pt.HasErrors ()) {
foreach (var l in pt.ParserMessages)
result.LogMessages.Add ("input.aidl", l);

output = null;
return result;
}

var unit = (CompilationUnit) pt.Root.AstNode;

var parcelables = new List<TypeName> ();

foreach (Parcelable t in unit.Types.Where (t => t is Parcelable))
parcelables.Add (unit.Package == null ? t.Name : new TypeName (unit.Package.Identifiers.Concat (t.Name.Identifiers).ToArray ()));

var sw = new StringWriter ();
var opts = new ConverterOptions { ParcelableHandling = parcelableHandling };

new CSharpCodeGenerator (sw, database).GenerateCode (unit, parcelables, opts);

output = sw.ToString ();

return result;
}
}
}

1 change: 1 addition & 0 deletions src/Xamarin.Android.Tools.Aidl/BindingDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public BindingDatabase (IEnumerable<string> assemblies, Func<string,AssemblyDefi
public BindingDatabase (IEnumerable<AssemblyDefinition> asses)
{
this.asses.AddRange (asses);
Initialize (null, null);
}

public IDictionary<string,string> NamespaceMappings {
Expand Down
63 changes: 63 additions & 0 deletions tests/Xamarin.Android.Tools.Aidl-Tests/AidlCompilerTestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//#define GENERATE_OUTPUT
// This define will overwrite the files in /bin/TestData.
// You will still need to copy them back to the original /TestData.

using System;
using System.IO;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
using Xamarin.Android.Tools.Aidl;

namespace Xamarin.Android.Tools.Aidl_Tests
{
public class AidlCompilerTestBase
{
protected void RunTest (string name)
{
var root = Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location);
var file = Path.Combine (root, "TestData", name + ".txt");
var text = File.ReadAllText (file);

(var input, var expected_output) = SplitTestFile (text);

var compiler = new AidlCompiler ();
var results = compiler.Run (input, out var output);

Assert.False (results.LogMessages.Any ());

#if GENERATE_OUTPUT
using (var sw = new StreamWriter (file)) {
sw.WriteLine (input);
sw.WriteLine ();
sw.WriteLine ("####");
sw.WriteLine ();
sw.WriteLine (output);
}

Assert.Ignore ("Generating output for this test.");
#endif // GENERATE_OUTPUT

Assert.AreEqual (StripLineEndings (expected_output), StripLineEndings (output));
}

(string input, string output) SplitTestFile (string text)
{
var index = text.IndexOf ("####");

if (index < 0) {
#if GENERATE_OUTPUT
return (text, null);
#endif // GENERATE_OUTPUT
throw new Exception ("No expected output found.");
}

var input = text.Substring (0, index).Trim ();
var output = text.Substring (index + 4).Trim ();

return (input, output);
}

string StripLineEndings (string input) => input.Replace ("\n", "").Replace ("\r", "").Trim ();
}
}
31 changes: 31 additions & 0 deletions tests/Xamarin.Android.Tools.Aidl-Tests/AidlCompilerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;

namespace Xamarin.Android.Tools.Aidl_Tests
{
[TestFixture]
public class AidlCompilerTests : AidlCompilerTestBase
{
[Test]
public void ListAndMap () => RunTest (nameof (ListAndMap));

[Test]
public void NamespaceResolution () => RunTest (nameof (NamespaceResolution));

[Test]
public void PrimitiveTypes () => RunTest (nameof (PrimitiveTypes));

[Test]
public void FaceService () => RunTest (nameof (FaceService));

[Test]
public void IUpdateEngine () => RunTest (nameof (IUpdateEngine));

[Test]
public void ITelephony () => RunTest (nameof (ITelephony));
}
}
Loading

0 comments on commit 81d37a1

Please sign in to comment.