From eaaf41e6e912b249d5df013d1314216199fa6a50 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Thu, 14 May 2026 09:24:22 +0200 Subject: [PATCH 1/2] Use Java identity in constructor activation tests Compare forwarded Java object constructor arguments with JNI object identity instead of raw JNI reference handles, since equivalent Java objects can be represented by distinct local/global refs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../Java.Interop/ConstructorActivationTests.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/Mono.Android-Tests/Mono.Android-Tests/Java.Interop/ConstructorActivationTests.cs b/tests/Mono.Android-Tests/Mono.Android-Tests/Java.Interop/ConstructorActivationTests.cs index 1fd51eff2e1..10ce2e05bac 100644 --- a/tests/Mono.Android-Tests/Mono.Android-Tests/Java.Interop/ConstructorActivationTests.cs +++ b/tests/Mono.Android-Tests/Mono.Android-Tests/Java.Interop/ConstructorActivationTests.cs @@ -95,7 +95,7 @@ public void JavaSideContextConstructorForwardsArgument () new JValue (Application.Context))) { Assert.AreEqual (1, ConstructorActivationContextView.ConstructorInvocations); Assert.IsNotNull (instance.ContextValue); - Assert.AreEqual (Application.Context.Handle, instance.ContextValue.Handle); + AssertSameJavaObject (Application.Context, instance.ContextValue); } } @@ -110,7 +110,7 @@ public void JavaSideContextAttributeSetConstructorForwardsArguments () JValue.Zero)) { Assert.AreEqual (1, ConstructorActivationAttributeSetView.ConstructorInvocations); Assert.IsNotNull (instance.ContextValue); - Assert.AreEqual (Application.Context.Handle, instance.ContextValue.Handle); + AssertSameJavaObject (Application.Context, instance.ContextValue); Assert.IsNull (instance.AttributeSetValue); } } @@ -127,7 +127,7 @@ public void JavaSideContextAttributeSetStyleConstructorForwardsArguments () new JValue (42))) { Assert.AreEqual (1, ConstructorActivationStyledView.ConstructorInvocations); Assert.IsNotNull (instance.ContextValue); - Assert.AreEqual (Application.Context.Handle, instance.ContextValue.Handle); + AssertSameJavaObject (Application.Context, instance.ContextValue); Assert.IsNull (instance.AttributeSetValue); Assert.AreEqual (42, instance.DefStyleAttrValue); } @@ -474,7 +474,7 @@ public void JavaSideObjectArrayConstructorForwardsValues () Assert.IsNotNull (instance.ObjectArrayValue); Assert.AreEqual (2, instance.ObjectArrayValue.Length); Assert.AreEqual ("object-array-value", instance.ObjectArrayValue [0].ToString ()); - Assert.AreEqual (Application.Context.Handle, instance.ObjectArrayValue [1].Handle); + AssertSameJavaObject (Application.Context, instance.ObjectArrayValue [1]); } } @@ -564,6 +564,13 @@ static void AssertRegisteredSame (T instance) registered.Dispose (); } } + + static void AssertSameJavaObject (Java.Lang.Object expected, Java.Lang.Object actual) + { + Assert.IsTrue ( + JNIEnv.IsSameObject (expected.Handle, actual.Handle), + $"Expected Java object identity to match. Expected handle: {expected.Handle}, actual handle: {actual.Handle}."); + } } [Register ("net/dot/android/test/ConstructorActivationThrowing")] From 0973a6781dffbcbc283d0a6c75f681ebf356d8a8 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Thu, 14 May 2026 12:12:38 +0200 Subject: [PATCH 2/2] Use JniEnvironment for object identity assertion Update the constructor activation helper to compare PeerReference values with JniEnvironment.Types.IsSameObject and assert inputs are non-null before dereferencing. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../Java.Interop/ConstructorActivationTests.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Mono.Android-Tests/Mono.Android-Tests/Java.Interop/ConstructorActivationTests.cs b/tests/Mono.Android-Tests/Mono.Android-Tests/Java.Interop/ConstructorActivationTests.cs index 10ce2e05bac..9b2bf94ee10 100644 --- a/tests/Mono.Android-Tests/Mono.Android-Tests/Java.Interop/ConstructorActivationTests.cs +++ b/tests/Mono.Android-Tests/Mono.Android-Tests/Java.Interop/ConstructorActivationTests.cs @@ -567,8 +567,10 @@ static void AssertRegisteredSame (T instance) static void AssertSameJavaObject (Java.Lang.Object expected, Java.Lang.Object actual) { + Assert.IsNotNull (expected); + Assert.IsNotNull (actual); Assert.IsTrue ( - JNIEnv.IsSameObject (expected.Handle, actual.Handle), + JniEnvironment.Types.IsSameObject (expected.PeerReference, actual.PeerReference), $"Expected Java object identity to match. Expected handle: {expected.Handle}, actual handle: {actual.Handle}."); } }