Skip to content

Commit

Permalink
[Mono.Android-Tests] Run Java.Interop tests as well.
Browse files Browse the repository at this point in the history
Context: dotnet/java-interop#454
Context: xamarin/monodroid@e318861e
Context: 7d32ef3e6
Context: 1a2eb95

When use of `Java.Interop.dll` was originally added in
xamarin/monodroid@e318861e, it didn't implement the "full"
`JniRuntime` abstraction, which meant that until
1a2eb953, trying to instantiate a
`JavaInt32Array` instance would result in a `NotImplementedException`,
because `Android.Runtime.AndroidValueManager.AddPeer()` threw a
`NotImplementedException`.

Commit 1a2eb953 removed the exception but
didn't *implement* `JniRuntime.JniValueManager.AddPeer()`, meaning
even though a `JavaInt32Array` could be *instantiated*, it wouldn't
*work* "properly", e.g. `JniRuntime.JniValueManager.PeekPeer()`
wouldn't *find* the `JavaInt32Array` when given the same handle.

This in turn "happened" because `Mono.Android-Tests.dll` never
*executed* the unit tests for `Java.Interop.dll`, which it *couldn't*
do, because it didn't fully support the semantics.

Add the `Java.Interop.dll` unit tests to `Mono.Android-Tests.dll`, and
fix `Mono.Android.dll` and company so that they can actually *pass*:

  * Add a new `Java.Interop-Tests.csproj` project to "host" the
    Java.Interop unit tests in a MonoAndroid-profile project.

  * Add `java-interop.jar` to the `javac` command line generated by
    `Jar.targets`, so that e.g. `com.xamarin.java_interop.ManagedPeer`
    can be used.

  * Update `Mono.Android.dll` so that the Java.Interop unit tests
    work.  This includes "migrating" the (jobject => instance) mapping
    previously accessible via `Java.Lang.Object.GetObject<T>()` into
    `Android.Runtime.AndroidValueManager`.

  * `AndroidRuntime.GetExceptionForThrowable()` will now "unwrap" a
    `Java.Interop.JavaProxyThrowable` instance.

  * `AndroidTypeManager.GetSimpleReferences()` can't use
    `JNIEnv.GetJniName()` because that always returns
    `java/lang/Object`, even if a type can't be found, which breaks
    several Java.Interop unit tests.  Use
    `JNIEnv.monodroid_typemap_managed_to_java()` instead.

  * `AndroidTypeManager.RegisterNativeMembers()` will now invoke
    `JniRuntime.JniTypeManager.RegisterNativeMembers()` when `methods`
    is empty, so that classes can handle their own registration.  This
    was needed so that
    `Java.InteropTests.CallVirtualFromConstructorDerived` could
    actually be properly instantiated and participate in tests.

  * `Java.Interop.TypeManager.CreateProxy()` now supports
    Java.Interop-style
    `(ref JniObjectReference, JniObjectReferenceOptions)`
    constructors.

  * `Java.Interop.JavaObject` and `Java.Interop.JavaException` are now
    bridgeable types, and can participate in GC's.

    This means that e.g. `JavaInt32Array` instances won't be
    prematurely collected.

TODO:

  * `Java.Lang.Object` should be updated to inherit from
    `Java.Interop.JavaObject`

  * `Java.Lang.Throwable` should be updated to inherit from
    `Java.Interop.JavaException`

  * `generator` needs to be updated to begin *avoiding* the `JNIEnv`
    methods, so that non-`Java.Lang.*`-inheriting types can be used.
  • Loading branch information
jonpryor committed Sep 6, 2019
1 parent 2d400aa commit 8c8cc97
Show file tree
Hide file tree
Showing 32 changed files with 699 additions and 308 deletions.
24 changes: 24 additions & 0 deletions Xamarin.Android-Tests.sln
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mono.Android-Test.Library",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mono.Android-TestsMultiDex", "tests\Runtime-MultiDex\Mono.Android-TestsMultiDex.csproj", "{9ECBEA14-B79F-4F92-9266-495C03A32571}"
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Interop-Tests", "external\Java.Interop\src\Java.Interop\Tests\Interop-Tests.shproj", "{0ADB8D72-7479-49AF-8809-E03AE4A4EAE2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mono.Linq.Expressions", "external\Java.Interop\lib\mono.linq.expressions\Mono.Linq.Expressions.csproj", "{0C001D50-4176-45AE-BDC8-BA626508B0CC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Java.Interop.GenericMarshaler", "external\Java.Interop\src\Java.Interop.GenericMarshaler\Java.Interop.GenericMarshaler.csproj", "{D1243BAB-23CA-4566-A2A3-3ADA2C2DC3AF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Java.Interop-Tests", "src\Mono.Android\Test\Java.Interop-Tests\Java.Interop-Tests.csproj", "{6CB00820-A66B-43E5-8785-ED456C6E9F39}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
src\Mono.Android\Test\Mono.Android-Test.Shared.projitems*{0ab4956e-6fb9-4da0-9d49-ab65a3ff403a}*SharedItemsImports = 13
Expand Down Expand Up @@ -249,6 +257,18 @@ Global
{9ECBEA14-B79F-4F92-9266-495C03A32571}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9ECBEA14-B79F-4F92-9266-495C03A32571}.Release|Any CPU.Build.0 = Release|Any CPU
{9ECBEA14-B79F-4F92-9266-495C03A32571}.Release|Any CPU.Deploy.0 = Release|Any CPU
{0C001D50-4176-45AE-BDC8-BA626508B0CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0C001D50-4176-45AE-BDC8-BA626508B0CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C001D50-4176-45AE-BDC8-BA626508B0CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C001D50-4176-45AE-BDC8-BA626508B0CC}.Release|Any CPU.Build.0 = Release|Any CPU
{D1243BAB-23CA-4566-A2A3-3ADA2C2DC3AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D1243BAB-23CA-4566-A2A3-3ADA2C2DC3AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1243BAB-23CA-4566-A2A3-3ADA2C2DC3AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1243BAB-23CA-4566-A2A3-3ADA2C2DC3AF}.Release|Any CPU.Build.0 = Release|Any CPU
{6CB00820-A66B-43E5-8785-ED456C6E9F39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6CB00820-A66B-43E5-8785-ED456C6E9F39}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6CB00820-A66B-43E5-8785-ED456C6E9F39}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6CB00820-A66B-43E5-8785-ED456C6E9F39}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -284,6 +304,10 @@ Global
{0AB4956E-6FB9-4DA0-9D49-AB65A3FF403A} = {EFBC4DC0-DBFF-4DAA-B0B8-6D0CB02A25F5}
{8CB5FF58-FF95-43B9-9064-9ACE9525866F} = {EFBC4DC0-DBFF-4DAA-B0B8-6D0CB02A25F5}
{9ECBEA14-B79F-4F92-9266-495C03A32571} = {EFBC4DC0-DBFF-4DAA-B0B8-6D0CB02A25F5}
{0ADB8D72-7479-49AF-8809-E03AE4A4EAE2} = {EFBC4DC0-DBFF-4DAA-B0B8-6D0CB02A25F5}
{0C001D50-4176-45AE-BDC8-BA626508B0CC} = {EFBC4DC0-DBFF-4DAA-B0B8-6D0CB02A25F5}
{D1243BAB-23CA-4566-A2A3-3ADA2C2DC3AF} = {EFBC4DC0-DBFF-4DAA-B0B8-6D0CB02A25F5}
{6CB00820-A66B-43E5-8785-ED456C6E9F39} = {EFBC4DC0-DBFF-4DAA-B0B8-6D0CB02A25F5}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8643CD20-B195-4919-8135-27549488237E}
Expand Down
6 changes: 4 additions & 2 deletions build-tools/scripts/Jar.targets
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
<_Jar>"$(JarPath)"</_Jar>
<_Targets>-source $(_JavacSourceVersion) -target $(_JavacTargetVersion)</_Targets>
<_DestDir>$(IntermediateOutputPath)__CreateTestJarFile-bin</_DestDir>
<_AndroidJar>-cp "$(AndroidSdkDirectory)\platforms\android-$(_AndroidApiLevelName)\android.jar"</_AndroidJar>
<_AndroidJar>-bootclasspath "$(AndroidSdkDirectory)\platforms\android-$(_AndroidApiLevelName)\android.jar"</_AndroidJar>
<_JIJar>$([System.IO.Path]::GetFullPath ('$(XAInstallPrefix)'))\xbuild\Xamarin\Android\java-interop.jar</_JIJar>
<_CP>-cp "$(_JIJar)"</_CP>
</PropertyGroup>
<MakeDir Directories="$(_DestDir)" />
<Exec Command="$(_Javac) $(_Targets) -d &quot;$(_DestDir)&quot; $(_AndroidJar) @(_JavacSource->'&quot;%(Identity)&quot;', ' ')" />
<Exec Command="$(_Javac) $(_Targets) -d &quot;$(_DestDir)&quot; $(_AndroidJar) $(_CP) @(_JavacSource->'&quot;%(Identity)&quot;', ' ')" />
<Exec
Command="$(_Jar) cf &quot;classes.jar&quot; ."
WorkingDirectory="$(_DestDir)"
Expand Down
Loading

0 comments on commit 8c8cc97

Please sign in to comment.