Repository navigation
[tests] Cover interface-valued Java collections #12614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
simonrozsival
merged 14 commits into
main
from
simonrozsival-interface-collection-parity
Sep 8, 2026
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f389881
[tests] Cover interface-valued Java collections
simonrozsival c181c0a
[tests] Avoid rooting interface collection wrappers
simonrozsival 28ee46a
[tests] Isolate interface collection rooting
simonrozsival 2326716
[tests] Enforce interface wrapper root isolation
simonrozsival efdb449
[tests] Fix dictionary rooting graph selector
simonrozsival cabe4e9
[tests] Improve interface collection diagnostics
simonrozsival 9dce449
[tests] Harden NativeAOT rooting graph checks
simonrozsival 6833c82
[tests] Reject ambiguous NativeAOT graph input
simonrozsival 73dc253
[tests] Use resilient device result timeout
simonrozsival bf2bfde
[tests] Stream interface collection device results
simonrozsival acc3c7b
[tests] Match NativeAOT dictionary factory root
simonrozsival a899d01
[tests] Restore individual NUnit interface collection cases
simonrozsival d987cb9
Merge main into interface collection tests
simonrozsival 3ae66a2
[tests] Handle conditional NativeAOT collection factory roots
simonrozsival File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
268 changes: 268 additions & 0 deletions
268
...Mono.Android-Tests/Mono.Android-Tests/Java.Interop/InterfaceCollectionMarshallingTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,268 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| using Android.Runtime; | ||
|
|
||
| using Java.Interop; | ||
|
|
||
| using NUnit.Framework; | ||
|
|
||
| namespace Java.InteropTests | ||
| { | ||
| [TestFixture] | ||
| [Category ("InterfaceCollections")] | ||
| public class InterfaceCollectionMarshallingTests | ||
| { | ||
| [Test] | ||
| public void JavaList_InterfaceElementsPreserveIdentityAndRoundTrip () | ||
| { | ||
| using var holder = new global::Net.Dot.Android.Test.InterfaceCollectionHolder (); | ||
| var list = holder.CreateList (); | ||
| try { | ||
| Assert.AreEqual (typeof (JavaList<global::Net.Dot.Android.Test.IValueProvider>), list.GetType ()); | ||
| Assert.AreEqual (4, list.Count); | ||
|
|
||
| var first = list [0]; | ||
| var duplicate = list [1]; | ||
| var second = list [2]; | ||
|
|
||
| AssertInterfacePeer (first, 11); | ||
| AssertInterfacePeer (second, 22); | ||
| Assert.AreSame (first, duplicate); | ||
| Assert.AreSame (first, list [0]); | ||
| AssertSameJavaObject (first, duplicate); | ||
| AssertDistinctJavaObjects (first, second); | ||
| Assert.IsNull (list [3]); | ||
| Assert.IsTrue (list.Contains (first)); | ||
| Assert.IsTrue (list.Contains (null)); | ||
|
|
||
| list.Add (second); | ||
| Assert.AreEqual (5, list.Count); | ||
| Assert.AreSame (second, list [4]); | ||
| CollectionAssert.AreEqual (new [] { 11, 11, 22, 22 }, list.Where (value => value != null).Select (value => value.Value)); | ||
|
|
||
| var roundTrip = holder.RoundTripList (list); | ||
| try { | ||
| Assert.AreEqual (typeof (JavaList<global::Net.Dot.Android.Test.IValueProvider>), roundTrip.GetType ()); | ||
| AssertSameJavaObject (list, roundTrip); | ||
| Assert.AreSame (first, roundTrip [0]); | ||
| } finally { | ||
| DisposeIfDistinct (list, roundTrip); | ||
| } | ||
|
|
||
| Assert.IsTrue (list.Remove (first)); | ||
| Assert.IsTrue (list.Contains (first)); | ||
| Assert.IsTrue (list.Remove (first)); | ||
| Assert.IsFalse (list.Contains (first)); | ||
| } finally { | ||
| DisposeJavaObject (list); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void JavaList_InheritedInterfaceUsesExplicitInvoker () | ||
| { | ||
| using var holder = new global::Net.Dot.Android.Test.InterfaceCollectionHolder (); | ||
| var list = holder.CreateInheritedList (); | ||
| try { | ||
| Assert.AreEqual (typeof (JavaList<global::Net.Dot.Android.Test.IExtendedValueProvider>), list.GetType ()); | ||
| Assert.AreEqual (2, list.Count); | ||
| AssertInterfacePeer (list [0], 11); | ||
| AssertInterfacePeer (list [1], 22); | ||
| Assert.AreEqual (111, list [0].OtherValue); | ||
| Assert.AreEqual (222, list [1].OtherValue); | ||
| StringAssert.EndsWith ("IExtendedValueProviderInvoker", list [0].GetType ().FullName); | ||
| } finally { | ||
| DisposeJavaObject (list); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void JavaCollection_InterfaceElementsSupportOperationsAndRoundTrip () | ||
| { | ||
| using var holder = new global::Net.Dot.Android.Test.InterfaceCollectionHolder (); | ||
| var collection = holder.CreateCollection (); | ||
| try { | ||
| Assert.AreEqual (typeof (JavaCollection<global::Net.Dot.Android.Test.IValueProvider>), collection.GetType ()); | ||
| Assert.AreEqual (3, collection.Count); | ||
|
|
||
| var values = collection.ToArray (); | ||
| var first = values [0]; | ||
| var second = values [1]; | ||
| AssertInterfacePeer (first, 11); | ||
| AssertInterfacePeer (second, 22); | ||
| AssertDistinctJavaObjects (first, second); | ||
| Assert.IsNull (values [2]); | ||
|
|
||
| collection.Add (first); | ||
| Assert.AreEqual (4, collection.Count); | ||
| Assert.IsTrue (collection.Contains (first)); | ||
| Assert.IsTrue (collection.Contains (null)); | ||
| Assert.AreSame (first, collection.ToArray () [3]); | ||
|
|
||
| var roundTrip = holder.RoundTripCollection (collection); | ||
| try { | ||
| Assert.AreEqual (typeof (JavaCollection<global::Net.Dot.Android.Test.IValueProvider>), roundTrip.GetType ()); | ||
| AssertSameJavaObject (collection, roundTrip); | ||
| Assert.AreSame (first, roundTrip.First ()); | ||
| } finally { | ||
| DisposeIfDistinct (collection, roundTrip); | ||
| } | ||
|
|
||
| collection.Clear (); | ||
| Assert.AreEqual (0, collection.Count); | ||
| } finally { | ||
| DisposeJavaObject (collection); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void JavaDictionary_InterfaceKeysSupportOperationsAndRoundTrip () | ||
| { | ||
| using var holder = new global::Net.Dot.Android.Test.InterfaceCollectionHolder (); | ||
| var dictionary = holder.CreateKeyDictionary (); | ||
| try { | ||
| Assert.AreEqual ( | ||
| typeof (JavaDictionary<global::Net.Dot.Android.Test.IValueProvider, string>), | ||
| dictionary.GetType ()); | ||
| Assert.AreEqual (3, dictionary.Count); | ||
|
|
||
| var first = dictionary.Keys.Single (key => key != null && key.Value == 11); | ||
| var second = dictionary.Keys.Single (key => key != null && key.Value == 22); | ||
| AssertInterfacePeer (first, 11); | ||
| AssertInterfacePeer (second, 22); | ||
| AssertDistinctJavaObjects (first, second); | ||
| Assert.IsTrue (dictionary.ContainsKey (first)); | ||
| Assert.IsTrue (dictionary.ContainsKey (null)); | ||
| Assert.AreEqual ("first", dictionary [first]); | ||
| Assert.AreEqual ("null", dictionary [null]); | ||
| Assert.AreSame (first, dictionary.Keys.Single (key => key != null && key.Value == 11)); | ||
|
|
||
| var roundTrip = holder.RoundTripKeyDictionary (dictionary); | ||
| try { | ||
| AssertSameJavaObject (dictionary, roundTrip); | ||
| Assert.AreEqual ("second", roundTrip [second]); | ||
| } finally { | ||
| DisposeIfDistinct (dictionary, roundTrip); | ||
| } | ||
|
|
||
| Assert.IsTrue (dictionary.Remove (first)); | ||
| Assert.IsFalse (dictionary.ContainsKey (first)); | ||
| } finally { | ||
| DisposeJavaObject (dictionary); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void JavaDictionary_InterfaceValuesPreserveDuplicatesAndRoundTrip () | ||
| { | ||
| using var holder = new global::Net.Dot.Android.Test.InterfaceCollectionHolder (); | ||
| var dictionary = holder.CreateValueDictionary (); | ||
| try { | ||
| Assert.AreEqual ( | ||
| typeof (JavaDictionary<string, global::Net.Dot.Android.Test.IValueProvider>), | ||
| dictionary.GetType ()); | ||
| Assert.AreEqual (4, dictionary.Count); | ||
|
|
||
| var first = dictionary ["first"]; | ||
| var duplicate = dictionary ["duplicate"]; | ||
| var second = dictionary ["second"]; | ||
| AssertInterfacePeer (first, 11); | ||
| AssertInterfacePeer (second, 22); | ||
| Assert.AreSame (first, duplicate); | ||
| Assert.AreSame (first, dictionary ["first"]); | ||
| AssertDistinctJavaObjects (first, second); | ||
| Assert.IsNull (dictionary ["null"]); | ||
| CollectionAssert.AreEquivalent (new [] { 11, 11, 22 }, dictionary.Values.Where (value => value != null).Select (value => value.Value)); | ||
|
|
||
| dictionary.Add ("added", second); | ||
| Assert.AreSame (second, dictionary ["added"]); | ||
|
|
||
| var roundTrip = holder.RoundTripValueDictionary (dictionary); | ||
| try { | ||
| AssertSameJavaObject (dictionary, roundTrip); | ||
| Assert.AreSame (first, roundTrip ["duplicate"]); | ||
| } finally { | ||
| DisposeIfDistinct (dictionary, roundTrip); | ||
| } | ||
|
|
||
| Assert.IsTrue (dictionary.Remove ("first")); | ||
| Assert.IsFalse (dictionary.ContainsKey ("first")); | ||
| } finally { | ||
| DisposeJavaObject (dictionary); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void JavaDictionary_InterfaceKeysAndValuesPreserveIdentityAndRoundTrip () | ||
| { | ||
| using var holder = new global::Net.Dot.Android.Test.InterfaceCollectionHolder (); | ||
| var dictionary = holder.CreateInterfaceDictionary (); | ||
| try { | ||
| Assert.AreEqual ( | ||
| typeof (JavaDictionary< | ||
| global::Net.Dot.Android.Test.IValueProvider, | ||
| global::Net.Dot.Android.Test.IValueProvider>), | ||
| dictionary.GetType ()); | ||
| Assert.AreEqual (3, dictionary.Count); | ||
|
|
||
| var first = dictionary.Keys.Single (key => key != null && key.Value == 11); | ||
| var second = dictionary.Keys.Single (key => key != null && key.Value == 22); | ||
| Assert.AreSame (second, dictionary [first]); | ||
| Assert.AreSame (first, dictionary [second]); | ||
| Assert.IsNull (dictionary [null]); | ||
| Assert.IsTrue (dictionary.ContainsKey (first)); | ||
| Assert.IsTrue (dictionary.Any (pair => ReferenceEquals (pair.Key, first) && ReferenceEquals (pair.Value, second))); | ||
|
|
||
| var roundTrip = holder.RoundTripInterfaceDictionary (dictionary); | ||
| try { | ||
| AssertSameJavaObject (dictionary, roundTrip); | ||
| Assert.AreSame (second, roundTrip [first]); | ||
| } finally { | ||
| DisposeIfDistinct (dictionary, roundTrip); | ||
| } | ||
|
|
||
| Assert.IsTrue (dictionary.Remove (second)); | ||
| Assert.IsFalse (dictionary.ContainsKey (second)); | ||
| } finally { | ||
| DisposeJavaObject (dictionary); | ||
| } | ||
| } | ||
|
|
||
| static void AssertInterfacePeer (global::Net.Dot.Android.Test.IValueProvider peer, int expectedValue) | ||
| { | ||
| Assert.IsNotNull (peer); | ||
| Assert.AreEqual (expectedValue, peer.Value); | ||
| StringAssert.EndsWith ("Invoker", peer.GetType ().Name); | ||
| } | ||
|
|
||
| static void AssertSameJavaObject (object expected, object actual) | ||
| { | ||
| var expectedPeer = (IJavaObject) expected; | ||
| var actualPeer = (IJavaObject) actual; | ||
| Assert.IsTrue (JNIEnv.IsSameObject (expectedPeer.Handle, actualPeer.Handle)); | ||
| } | ||
|
|
||
| static void AssertDistinctJavaObjects (object first, object second) | ||
| { | ||
| var firstPeer = (IJavaObject) first; | ||
| var secondPeer = (IJavaObject) second; | ||
| Assert.IsFalse (JNIEnv.IsSameObject (firstPeer.Handle, secondPeer.Handle)); | ||
| } | ||
|
|
||
| static void DisposeIfDistinct (object owner, object value) | ||
| { | ||
| if (!ReferenceEquals (owner, value)) { | ||
| DisposeJavaObject (value); | ||
| } | ||
| } | ||
|
|
||
| static void DisposeJavaObject (object value) | ||
| { | ||
| if (value is IDisposable disposable) { | ||
| disposable.Dispose (); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...Android-Tests/Mono.Android-Tests/java/net/dot/android/test/InterfaceCollectionHolder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package net.dot.android.test; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public final class InterfaceCollectionHolder { | ||
| private final InterfaceCollectionPeer first; | ||
| private final InterfaceCollectionPeer second; | ||
|
|
||
| public InterfaceCollectionHolder() { | ||
| first = new InterfaceCollectionPeer(11, 111); | ||
| second = new InterfaceCollectionPeer(22, 222); | ||
| } | ||
|
|
||
| public List<ValueProvider> createList() { | ||
| List<ValueProvider> result = new ArrayList<>(); | ||
| result.add(first); | ||
| result.add(first); | ||
| result.add(second); | ||
| result.add(null); | ||
| return result; | ||
| } | ||
|
|
||
| public List<ExtendedValueProvider> createInheritedList() { | ||
| List<ExtendedValueProvider> result = new ArrayList<>(); | ||
| result.add(first); | ||
| result.add(second); | ||
| return result; | ||
| } | ||
|
|
||
| public Collection<ValueProvider> createCollection() { | ||
| Collection<ValueProvider> result = new ArrayList<>(); | ||
| result.add(first); | ||
| result.add(second); | ||
| result.add(null); | ||
| return result; | ||
| } | ||
|
|
||
| public Map<ValueProvider, String> createKeyDictionary() { | ||
| Map<ValueProvider, String> result = new LinkedHashMap<>(); | ||
| result.put(first, "first"); | ||
| result.put(second, "second"); | ||
| result.put(null, "null"); | ||
| return result; | ||
| } | ||
|
|
||
| public Map<String, ValueProvider> createValueDictionary() { | ||
| Map<String, ValueProvider> result = new LinkedHashMap<>(); | ||
| result.put("first", first); | ||
| result.put("duplicate", first); | ||
| result.put("second", second); | ||
| result.put("null", null); | ||
| return result; | ||
| } | ||
|
|
||
| public Map<ValueProvider, ValueProvider> createInterfaceDictionary() { | ||
| Map<ValueProvider, ValueProvider> result = new LinkedHashMap<>(); | ||
| result.put(first, second); | ||
| result.put(second, first); | ||
| result.put(null, null); | ||
| return result; | ||
| } | ||
|
|
||
| public List<ValueProvider> roundTripList(List<ValueProvider> value) { | ||
| return value; | ||
| } | ||
|
|
||
| public Collection<ValueProvider> roundTripCollection(Collection<ValueProvider> value) { | ||
| return value; | ||
| } | ||
|
|
||
| public Map<ValueProvider, String> roundTripKeyDictionary(Map<ValueProvider, String> value) { | ||
| return value; | ||
| } | ||
|
|
||
| public Map<String, ValueProvider> roundTripValueDictionary(Map<String, ValueProvider> value) { | ||
| return value; | ||
| } | ||
|
|
||
| public Map<ValueProvider, ValueProvider> roundTripInterfaceDictionary(Map<ValueProvider, ValueProvider> value) { | ||
| return value; | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
...o.Android-Tests/Mono.Android-Tests/java/net/dot/android/test/InterfaceCollectionPeer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package net.dot.android.test; | ||
|
|
||
| final class InterfaceCollectionPeer implements ExtendedValueProvider { | ||
| private final int value; | ||
| private final int otherValue; | ||
|
|
||
| public InterfaceCollectionPeer(int value, int otherValue) { | ||
| this.value = value; | ||
| this.otherValue = otherValue; | ||
| } | ||
|
|
||
| @Override | ||
| public int getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public int getOtherValue() { | ||
| return otherValue; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.