Skip to content

Commit be58159

Browse files
authored
[Java.Interop-Tests] Allow consumption by Xamarin.Android (dotnet#454)
Context: dotnet/android#3393 Allow the Java.Interop unit tests to be consumed by Xamarin.Android. In particular, this means: * "Proper" exceptions such as `Java.Lang.ClassNotFoundException` are thrown, not "based" `JavaException` types. * `Xamarin.Android.NUnitLite.dll` doesn't provide `[OneTimeSetUpAttribute]`/etc., so use `[TestFixtureSetUpAttribute]` instead. * Xamarin.Android doesn't currently support `Java.Interop.MarshalMemberBuilder`, so any tests which require it will not work. "Hide" those behind `#if !NO_MARSHAL_MEMBER_BUILDER_SUPPORT`.
1 parent 30f1a06 commit be58159

File tree

9 files changed

+76
-12
lines changed

9 files changed

+76
-12
lines changed

src/Java.Interop/Tests/Java.Interop/JavaArrayContract.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,21 @@ public abstract class JavaArrayContract<T> : ListContract<T>
1212
{
1313
int lrefStartCount;
1414

15+
#if __ANDROID__
16+
[TestFixtureSetUp]
17+
#else // __ANDROID__
1518
[OneTimeSetUp]
19+
#endif // __ANDROID__
1620
public void StartArrayTests ()
1721
{
1822
lrefStartCount = JniEnvironment.LocalReferenceCount;
1923
}
2024

25+
#if __ANDROID__
26+
[TestFixtureTearDown]
27+
#else // __ANDROID__
2128
[OneTimeTearDown]
29+
#endif // __ANDROID__
2230
public void EndArrayTests ()
2331
{
2432
int lref = JniEnvironment.LocalReferenceCount;

src/Java.Interop/Tests/Java.Interop/JavaExceptionTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ public void StackTrace ()
2424
// Dalvik, JVM
2525
e.JavaStackTrace.StartsWith ("java.lang.NoClassDefFoundError: this/type/had/better/not/exist", StringComparison.Ordinal));
2626
e.Dispose ();
27+
#if __ANDROID__
28+
} catch (Java.Lang.Throwable e) {
29+
Assert.IsTrue (
30+
string.Equals ("this/type/had/better/not/exist", e.Message, StringComparison.OrdinalIgnoreCase) ||
31+
e.Message.StartsWith ("Didn't find class \"this.type.had.better.not.exist\" on path: DexPathList"));
32+
Assert.IsTrue (
33+
// ART
34+
e.StackTrace.Contains ("java.lang.ClassNotFoundException: ", StringComparison.Ordinal) ||
35+
// Dalvik, JVM
36+
e.StackTrace.Contains ("java.lang.NoClassDefFoundError: this/type/had/better/not/exist", StringComparison.Ordinal));
37+
e.Dispose ();
38+
#endif // __ANDROID__
2739
}
2840
}
2941

src/Java.Interop/Tests/Java.Interop/JavaObjectArrayTest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ public class JavaObjectArray_object_ContractTest : JavaObjectArrayContractTest<o
130130

131131
int grefStartCount;
132132

133+
#if __ANDROID__
134+
[TestFixtureSetUp]
135+
#else // __ANDROID__
133136
[OneTimeSetUp]
137+
#endif // __ANDROID__
134138
public void BeginCheckGlobalRefCount ()
135139
{
136140
// So that the JavaProxyObject.TypeRef GREF isn't counted.
@@ -139,7 +143,11 @@ public void BeginCheckGlobalRefCount ()
139143
grefStartCount = JniEnvironment.Runtime.GlobalReferenceCount;
140144
}
141145

146+
#if __ANDROID__
147+
[TestFixtureTearDown]
148+
#else // __ANDROID__
142149
[OneTimeTearDown]
150+
#endif // __ANDROID__
143151
public void EndCheckGlobalRefCount ()
144152
{
145153
int gref = JniEnvironment.Runtime.GlobalReferenceCount;

src/Java.Interop/Tests/Java.Interop/JavaObjectTest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,11 @@ public void Ctor_Exceptions ()
166166

167167
// Note: This may break if/when JavaVM provides "default"
168168
Assert.Throws<NotSupportedException> (() => new JavaObjectWithNoJavaPeer ());
169+
#if __ANDROID__
170+
Assert.Throws<Java.Lang.ClassNotFoundException> (() => new JavaObjectWithMissingJavaPeer ()).Dispose ();
171+
#else // !__ANDROID__
169172
Assert.Throws<JavaException> (() => new JavaObjectWithMissingJavaPeer ()).Dispose ();
173+
#endif // !__ANDROID__
170174
}
171175

172176
[Test]

src/Java.Interop/Tests/Java.Interop/JniRuntime.JniValueManagerTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public void GetValue_ReturnsNullWithInvalidSafeHandle ()
126126
Assert.IsNull (JniRuntime.CurrentRuntime.ValueManager.GetValue (ref invalid, JniObjectReferenceOptions.CopyAndDispose));
127127
}
128128

129+
#if !NO_MARSHAL_MEMBER_BUILDER_SUPPORT
129130
[Test]
130131
public unsafe void GetValue_FindBestMatchType ()
131132
{
@@ -138,6 +139,7 @@ public unsafe void GetValue_FindBestMatchType ()
138139
}
139140
}
140141
}
142+
#endif // !NO_MARSHAL_MEMBER_BUILDER_SUPPORT
141143
}
142144
}
143145

src/Java.Interop/Tests/Java.Interop/JniTypeManagerTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ public void GetTypeSignature_Type ()
7575
AssertGetJniTypeInfoForType (typeof (JavaArray<int[]>), "[[I", true, 2);
7676
AssertGetJniTypeInfoForType (typeof (JavaArray<int[]>[]), "[[[I", true, 3);
7777

78+
#if !__ANDROID__
79+
// Re-enable once typemap files contain `JavaObject` subclasses, not just Java.Lang.Object subclasses
7880
AssertGetJniTypeInfoForType (typeof (GenericHolder<int>), GenericHolder<int>.JniTypeName, false, 0);
81+
#endif // !__ANDROID__
7982
}
8083

8184
static void AssertGetJniTypeInfoForType (Type type, string jniType, bool isKeyword, int arrayRank)

src/Java.Interop/Tests/Java.Interop/JniTypeTest.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ public unsafe void Sanity ()
3333
[Test]
3434
public void Ctor_ThrowsIfTypeNotFound ()
3535
{
36+
#if __ANDROID__
37+
Assert.Throws<Java.Lang.ClassNotFoundException> (() => new JniType ("__this__/__type__/__had__/__better__/__not__/__Exist__")).Dispose ();
38+
#else // __ANDROID__
3639
Assert.Throws<JavaException> (() => new JniType ("__this__/__type__/__had__/__better__/__not__/__Exist__")).Dispose ();
40+
#endif // __ANDROID__
3741
}
3842

3943
[Test]
@@ -92,27 +96,29 @@ public void IsAssignableFrom ()
9296
}
9397

9498
[Test]
95-
public void IsInstanceOfType ()
99+
public unsafe void IsInstanceOfType ()
96100
{
97-
using (var t = new JniType ("java/lang/Object"))
98-
using (var b = new TestType ()) {
99-
Assert.IsTrue (t.IsInstanceOfType (b.PeerReference));
100-
}
101-
}
102-
103-
[Test]
104-
public void ObjectBinding ()
105-
{
106-
using (var b = new TestType ()) {
107-
Console.WriteLine ("# ObjectBinding: {0}", b.ToString ());
101+
using (var Object_class = new JniType ("java/lang/Object"))
102+
using (var String_class = new JniType ("java/lang/String")) {
103+
var String_ctor = String_class.GetConstructor ("()V");
104+
var s = String_class.NewObject (String_ctor, null);
105+
try {
106+
Assert.IsTrue (Object_class.IsInstanceOfType (s), "java.lang.String IS-NOT-A java.lang.Object?!");
107+
} finally {
108+
JniObjectReference.Dispose (ref s);
109+
}
108110
}
109111
}
110112

111113
[Test]
112114
public void InvalidSignatureThrowsJniException ()
113115
{
114116
using (var Integer_class = new JniType ("java/lang/Integer")) {
117+
#if __ANDROID__
118+
Assert.Throws<Java.Lang.NoSuchMethodError> (() => Integer_class.GetConstructor ("(C)V")).Dispose ();
119+
#else // __ANDROID__
115120
Assert.Throws<JavaException> (() => Integer_class.GetConstructor ("(C)V")).Dispose ();
121+
#endif // __ANDROID__
116122
}
117123
}
118124

@@ -122,6 +128,7 @@ public void GetStaticFieldID ()
122128
using (var System_class = new JniType ("java/lang/System")) {
123129
var System_in = System_class.GetStaticField ("in", "Ljava/io/InputStream;");
124130
Assert.IsNotNull (System_in);
131+
Assert.IsTrue (System_in.ID != IntPtr.Zero);
125132
}
126133
}
127134

src/Java.Interop/Tests/Java.Interop/TestType.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Java.InteropTests
1010
{
11+
#if !NO_MARSHAL_MEMBER_BUILDER_SUPPORT
1112
[JniTypeSignature (TestType.JniTypeName)]
1213
public partial class TestType : JavaObject
1314
{
@@ -204,5 +205,6 @@ public string GetStringValue (int value)
204205
return value.ToString ();
205206
}
206207
}
208+
#endif // !NO_MARSHAL_MEMBER_BUILDER_SUPPORT
207209
}
208210

src/Java.Interop/Tests/Java.Interop/TestTypeTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,27 @@
77

88
namespace Java.InteropTests
99
{
10+
#if !NO_MARSHAL_MEMBER_BUILDER_SUPPORT
1011
[TestFixture]
1112
public class TestTypeTests : JavaVMFixture
1213
{
1314
int lrefStartCount;
1415

16+
#if __ANDROID__
17+
[TestFixtureSetUp]
18+
#else // __ANDROID__
1519
[OneTimeSetUp]
20+
#endif // __ANDROID__
1621
public void StartArrayTests ()
1722
{
1823
lrefStartCount = JniEnvironment.LocalReferenceCount;
1924
}
2025

26+
#if __ANDROID__
27+
[TestFixtureTearDown]
28+
#else // __ANDROID__
2129
[OneTimeTearDown]
30+
#endif // __ANDROID__
2231
public void EndArrayTests ()
2332
{
2433
int lref = JniEnvironment.LocalReferenceCount;
@@ -52,6 +61,14 @@ public void TestCase ()
5261
}
5362
}
5463

64+
[Test]
65+
public void ObjectBinding ()
66+
{
67+
using (var b = new TestType ()) {
68+
Console.WriteLine ("# ObjectBinding: {0}", b.ToString ());
69+
}
70+
}
71+
5572
[Test]
5673
public void UpdateInt32Array ()
5774
{
@@ -143,5 +160,6 @@ public void PropogateException ()
143160
}
144161
}
145162
}
163+
#endif // !NO_MARSHAL_MEMBER_BUILDER_SUPPORT
146164
}
147165

0 commit comments

Comments
 (0)