Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using AndroidMSTestProof.TestLibrary;
using Java.Interop;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[assembly: DoNotParallelize]

namespace AndroidMSTestProof.JavaInterop;

[TestClass]
public class InteropTests : LifecycleBase
{
[TestMethod]
[TestCategory ("proof")]
[TestCategory ("jni")]
public void ReferencedJNI ()
{
AssertInitialized ();
using var value = new Java.Lang.String ("referenced JNI");
Assert.AreEqual ("referenced JNI", value.ToString ());
Assert.AreNotEqual (IntPtr.Zero, JniEnvironment.EnvironmentPointer);
Context.WriteLine ("[D ASSEMBLY] " + typeof (InteropTests).Assembly.GetName ().Name);
}

#pragma warning disable MSTEST0087 // Matching display names deliberately test distinct JNI row identities.
[TestMethod]
[TestCategory ("proof")]
[TestCategory ("jni")]
[DataRow ("first", DisplayName = "same JNI display")]
[DataRow ("second", DisplayName = "same JNI display")]
public void StringRoundtrip (string text)
{
AssertInitialized ();
var reference = JniEnvironment.Strings.NewString (text);
try {
Assert.IsTrue (reference.IsValid);
Assert.AreEqual (text, JniEnvironment.Strings.ToString (reference));
} finally {
JniObjectReference.Dispose (ref reference);
}
}
#pragma warning restore MSTEST0087

[TestMethod]
[TestCategory ("diagnostic")]
public void GeneratedInteropFailure ()
{
AssertInitialized ();
Assert.Fail ("intentional referenced-JavaInterop generated invocation failure");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\Shared.props" />
<ItemGroup>
<ProjectReference Include="..\TestLibrary\MSTest.Android.TestLibrary.Tests.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

[assembly: DoNotParallelize]

namespace AndroidMSTestProof.TestLibrary;

public abstract class LifecycleBase
{
int initialized;
int cleaned;

public TestContext? TestContext { get; set; }

protected TestContext Context => TestContext ?? throw new InvalidOperationException ("MSTest did not assign the inherited context.");

[TestInitialize]
public void Initialize ()
{
Assert.AreEqual (1, ++initialized);
Assert.AreEqual (0, cleaned);
using var value = new Java.Lang.String ("initialize");
Assert.AreEqual ("initialize", value.ToString ());
Context.WriteLine ($"[D INITIALIZE] {GetType ().FullName} {Context.TestName} calls={initialized}");
}

[TestCleanup]
public void Cleanup ()
{
Assert.AreEqual (1, initialized);
Assert.AreEqual (1, ++cleaned);
using var value = new Java.Lang.String ("cleanup");
Assert.AreEqual ("cleanup", value.ToString ());
Context.WriteLine ($"[D CLEANUP] {GetType ().FullName} {Context.TestName} calls={cleaned} outcome={Context.CurrentTestOutcome}");
}

protected void AssertInitialized ()
{
Assert.AreEqual (1, initialized);
Assert.AreEqual (0, cleaned);
}
}

[TestClass]
public class LibraryTests : LifecycleBase
{
[TestMethod]
[TestCategory ("proof")]
[TestCategory ("library")]
public void ReferencedLibrary ()
{
AssertInitialized ();
Assert.AreEqual (typeof (LibraryTests).Assembly, GetType ().Assembly);
Context.WriteLine ("[D ASSEMBLY] " + typeof (LibraryTests).Assembly.GetName ().Name);
}

[TestMethod]
[TestCategory ("proof")]
[TestCategory ("library")]
public async Task AsyncLibrary ()
{
AssertInitialized ();
await Task.Delay (1);
AssertInitialized ();
}

[TestMethod]
[TestCategory ("proof")]
[TestCategory ("library")]
public void InconclusiveLibrary ()
{
AssertInitialized ();
Assert.Inconclusive ("intentional D inconclusive; inherited cleanup must execute");
}

[TestMethod]
[TestCategory ("diagnostic")]
public void GeneratedLibraryFailure ()
{
AssertInitialized ();
Assert.Fail ("intentional referenced-library generated invocation failure");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\Shared.props" />
</Project>
19 changes: 19 additions & 0 deletions tests/MSTest.Android.Tests/MSTest.Android.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="Shared.props" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<ApplicationId>net.dot.android.mstestproof</ApplicationId>
<AndroidInstrumentation>net.dot.android.mstestproof.Instrumentation</AndroidInstrumentation>
<IsTestingPlatformApplication>true</IsTestingPlatformApplication>
<EnableMSTestRunner>true</EnableMSTestRunner>
<AndroidUseInstrumentation>true</AndroidUseInstrumentation>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Libraries\**\*.cs" />
<Compile Include="..\..\src\Microsoft.Android.Run\InstrumentationProtocol.cs" Link="InstrumentationProtocol.cs" />
<ProjectReference Include="Libraries\JavaInterop\MSTest.Android.JavaInterop.Tests.csproj" />
<ProjectReference Include="Libraries\TestLibrary\MSTest.Android.TestLibrary.Tests.csproj" />
<PackageReference Include="Microsoft.Testing.Extensions.TrxReport" Version="$(MicrosoftTestingPlatformVersion)" PrivateAssets="all" Publish="true" />
<RuntimeHostConfigurationOption Include="IncludeCategories" Value="$(IncludeCategories)" Condition=" '$(IncludeCategories)' != '' " />
</ItemGroup>
</Project>
106 changes: 106 additions & 0 deletions tests/MSTest.Android.Tests/ProofTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System.Reflection;
using AndroidMSTestProof.TestLibrary;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[assembly: DoNotParallelize]

namespace AndroidMSTestProof;

[TestClass]
public class ProofTests : LifecycleBase
{
[TestMethod]
[TestCategory ("proof")]
public void AndroidBinding ()
{
AssertInitialized ();
Assert.IsTrue (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.N);
Assert.AreEqual ("Mono.Android", typeof (Java.Lang.String).Assembly.GetName ().Name);
}

[TestMethod]
[TestCategory ("proof")]
public async Task AsyncTask ()
{
AssertInitialized ();
await Task.Delay (1);
AssertInitialized ();
using var value = new Java.Lang.String ("async JNI");
Assert.AreEqual ("async JNI", value.ToString ());
}

[TestMethod]
[TestCategory ("proof")]
public async ValueTask AsyncValueTask ()
{
AssertInitialized ();
await Task.Delay (1);
AssertInitialized ();
}

#pragma warning disable MSTEST0042, MSTEST0087 // Identical arguments and display names deliberately test row identity.
[TestMethod]
[TestCategory ("proof")]
[DataRow (0, DisplayName = "same data display")]
[DataRow (0, DisplayName = "same data display")]
[DataRow (1, DisplayName = "same data display")]
public void DataRows (int row)
{
AssertInitialized ();
Assert.IsTrue (row is 0 or 1);
Context.WriteLine ($"[D ROW] DataRows value={row}");
}
#pragma warning restore MSTEST0042, MSTEST0087

public static IEnumerable<object []> DynamicCases {
get {
yield return [0];
yield return [0];
yield return [1];
}
}

public static string DynamicDisplay (MethodInfo method, object [] data) => "same dynamic display";

[TestMethod]
[TestCategory ("proof")]
[DynamicData (nameof (DynamicCases), DynamicDataDisplayName = nameof (DynamicDisplay))]
public async Task DynamicRows (int row)
{
AssertInitialized ();
await Task.Delay (1);
Assert.IsTrue (row is 0 or 1);
Context.WriteLine ($"[D ROW] DynamicRows value={row}");
}

[TestMethod]
[TestCategory ("proof")]
public void Attachment ()
{
AssertInitialized ();
var directory = Android.App.Application.Context.GetExternalFilesDir (null)?.AbsolutePath
?? throw new InvalidOperationException ("No Android external results directory.");
var path = Path.Combine (directory, "TestResults", "d-proof-attachment.txt");
File.WriteAllText (path, "D attachment\nline two");
Context.AddResultFile (path);
Context.WriteLine ("[D ATTACHMENT] " + path);
}

[TestMethod]
[TestCategory ("proof")]
[Ignore ("intentional D ignored test; no initialization or cleanup expected")]
public void Ignored ()
{
Assert.Fail ("an ignored test must never execute");
}

[TestMethod]
[TestCategory ("diagnostic")]
public void GeneratedFailure ()
{
AssertInitialized ();
#pragma warning disable MSTEST0025 // Preserve an actual comparison failure and its generated invocation stack.
Assert.AreEqual ("expected D value", "actual D value", "intentional diagnostic failure\nsecond line");
#pragma warning restore MSTEST0025
}
}
Loading