|
1 | 1 | using System;
|
| 2 | +using System.Runtime.CompilerServices; |
2 | 3 |
|
3 | 4 | using Java.Interop;
|
4 | 5 |
|
|
7 | 8 | namespace Java.InteropTests {
|
8 | 9 |
|
9 | 10 | [TestFixture]
|
| 11 | +#if !__ANDROID__ |
| 12 | + // We want stability around the CallVirtualFromConstructorDerived static fields |
| 13 | + [NonParallelizable] |
| 14 | +#endif // !__ANDROID__ |
10 | 15 | public class InvokeVirtualFromConstructorTests : JavaVMFixture
|
11 | 16 | {
|
12 | 17 | [Test]
|
13 |
| - public void InvokeVirtualFromConstructor () |
| 18 | + public void CreateManagedInstanceFirst_WithAllocObject () |
14 | 19 | {
|
15 |
| - using (var t = new CallVirtualFromConstructorDerived (42)) { |
16 |
| - Assert.IsTrue (t.Called); |
17 |
| - Assert.IsNotNull (JniRuntime.CurrentRuntime.ValueManager.PeekValue (t.PeerReference)); |
18 |
| - } |
| 20 | + CallVirtualFromConstructorDerived.Intermediate_FromActivationConstructor = null; |
| 21 | + CallVirtualFromConstructorDerived.Intermediate_FromCalledFromConstructor = null; |
| 22 | + |
| 23 | + using var t = new CallVirtualFromConstructorDerived (42); |
| 24 | + Assert.IsTrue ( |
| 25 | + t.Called, |
| 26 | + "CalledFromConstructor method override should have been called."); |
| 27 | + Assert.IsFalse ( |
| 28 | + t.InvokedActivationConstructor, |
| 29 | + "Activation Constructor should have been called, as calledFromConstructor() is invoked before ManagedPeer.construct()."); |
| 30 | + Assert.IsTrue ( |
| 31 | + t.InvokedConstructor, |
| 32 | + "(int) constructor should have been called, via ManagedPeer.construct()."); |
| 33 | + |
| 34 | + var registered = JniRuntime.CurrentRuntime.ValueManager.PeekValue (t.PeerReference); |
| 35 | + var acIntermediate = CallVirtualFromConstructorDerived.Intermediate_FromActivationConstructor; |
| 36 | + var cfIntermediate = CallVirtualFromConstructorDerived.Intermediate_FromCalledFromConstructor; |
| 37 | + |
| 38 | + Assert.AreSame (t, registered, |
| 39 | + "Expected t and registered to be the same instance; " + |
| 40 | + $"t={RuntimeHelpers.GetHashCode (t).ToString ("x")}, " + |
| 41 | + $"registered={RuntimeHelpers.GetHashCode (registered).ToString ("x")}"); |
| 42 | + Assert.IsNull (acIntermediate, |
| 43 | + "Activation Constructor should not have been called, because of AllocObject semantics"); |
| 44 | + Assert.AreSame (t, cfIntermediate, |
| 45 | + "Expected t and cfIntermediate to be the same instance; " + |
| 46 | + $"t={RuntimeHelpers.GetHashCode (t).ToString ("x")}, " + |
| 47 | + $"cfIntermediate={RuntimeHelpers.GetHashCode (cfIntermediate).ToString ("x")}"); |
| 48 | + |
| 49 | + Dispose (ref CallVirtualFromConstructorDerived.Intermediate_FromActivationConstructor); |
| 50 | + Dispose (ref CallVirtualFromConstructorDerived.Intermediate_FromCalledFromConstructor); |
| 51 | + } |
| 52 | + |
| 53 | + static void Dispose<T> (ref T peer) |
| 54 | + where T : class, IJavaPeerable |
| 55 | + { |
| 56 | + if (peer == null) |
| 57 | + return; |
| 58 | + |
| 59 | + peer.Dispose (); |
| 60 | + peer = null; |
19 | 61 | }
|
20 | 62 |
|
21 | 63 | [Test]
|
22 |
| - public void ActivationConstructor () |
| 64 | + public void CreateManagedInstanceFirst_WithNewObject () |
23 | 65 | {
|
24 |
| - var t = CallVirtualFromConstructorDerived.NewInstance (42); |
25 |
| - using (t) { |
26 |
| - Assert.IsTrue (t.InvokedActivationConstructor); |
27 |
| - Assert.IsTrue (t.InvokedConstructor); |
28 |
| - } |
| 66 | + CallVirtualFromConstructorDerived.Intermediate_FromActivationConstructor = null; |
| 67 | + CallVirtualFromConstructorDerived.Intermediate_FromCalledFromConstructor = null; |
| 68 | + |
| 69 | + using var t = new CallVirtualFromConstructorDerived (42, useNewObject: true); |
| 70 | + Assert.IsFalse ( |
| 71 | + t.Called, |
| 72 | + "CalledFromConstructor method override was called on a different instance."); |
| 73 | + Assert.IsFalse ( |
| 74 | + t.InvokedActivationConstructor, |
| 75 | + "Activation Constructor should not have been called, as calledFromConstructor() is invoked before ManagedPeer.construct()."); |
| 76 | + Assert.IsTrue ( |
| 77 | + t.InvokedConstructor, |
| 78 | + "(int) constructor should have been called, via ManagedPeer.construct()."); |
| 79 | + |
| 80 | + var registered = JniRuntime.CurrentRuntime.ValueManager.PeekValue (t.PeerReference); |
| 81 | + var acIntermediate = CallVirtualFromConstructorDerived.Intermediate_FromActivationConstructor; |
| 82 | + var cfIntermediate = CallVirtualFromConstructorDerived.Intermediate_FromCalledFromConstructor; |
| 83 | + |
| 84 | + Assert.AreSame (t, registered, |
| 85 | + "Expected t and registered to be the same instance; " + |
| 86 | + $"t={RuntimeHelpers.GetHashCode (t).ToString ("x")}, " + |
| 87 | + $"registered={RuntimeHelpers.GetHashCode (registered).ToString ("x")}"); |
| 88 | + Assert.IsNotNull (acIntermediate, |
| 89 | + "Activation Constructor should have been called, because of NewObject"); |
| 90 | + Assert.IsTrue ( |
| 91 | + acIntermediate.Called, |
| 92 | + "CalledFromConstructor method override should have been called on acIntermediate."); |
| 93 | + Assert.IsTrue ( |
| 94 | + acIntermediate.InvokedActivationConstructor, |
| 95 | + "Activation Constructor should have been called on intermediate instance, as calledFromConstructor() is invoked before ManagedPeer.construct()."); |
| 96 | + Assert.AreNotSame (t, acIntermediate, |
| 97 | + "Expected t and registered to be different instances; " + |
| 98 | + $"t={RuntimeHelpers.GetHashCode (t).ToString ("x")}, " + |
| 99 | + $"acIntermediate={RuntimeHelpers.GetHashCode (acIntermediate).ToString ("x")}"); |
| 100 | + Assert.AreNotSame (t, cfIntermediate, |
| 101 | + "Expected t and cfIntermediate to be different instances; " + |
| 102 | + $"t={RuntimeHelpers.GetHashCode (t).ToString ("x")}, " + |
| 103 | + $"cfIntermediate={RuntimeHelpers.GetHashCode (cfIntermediate).ToString ("x")}"); |
| 104 | + Assert.AreSame (acIntermediate, cfIntermediate, |
| 105 | + "Expected acIntermediate and cfIntermediate to be the same instance; " + |
| 106 | + $"acIntermediate={RuntimeHelpers.GetHashCode (acIntermediate).ToString ("x")}, " + |
| 107 | + $"cfIntermediate={RuntimeHelpers.GetHashCode (cfIntermediate).ToString ("x")}"); |
| 108 | + |
| 109 | + Dispose (ref CallVirtualFromConstructorDerived.Intermediate_FromActivationConstructor); |
| 110 | + Dispose (ref CallVirtualFromConstructorDerived.Intermediate_FromCalledFromConstructor); |
| 111 | + } |
| 112 | + |
| 113 | + [Test] |
| 114 | + public void CreateJavaInstanceFirst () |
| 115 | + { |
| 116 | + CallVirtualFromConstructorDerived.Intermediate_FromActivationConstructor = null; |
| 117 | + CallVirtualFromConstructorDerived.Intermediate_FromCalledFromConstructor = null; |
| 118 | + |
| 119 | + using var t = CallVirtualFromConstructorDerived.NewInstance (42); |
| 120 | + |
| 121 | + Assert.IsTrue ( |
| 122 | + t.Called, |
| 123 | + "CalledFromConstructor method override should have been called."); |
| 124 | + Assert.IsTrue ( |
| 125 | + t.InvokedActivationConstructor, |
| 126 | + "Activation Constructor should have been called, as calledFromConstructor() is invoked before ManagedPeer.construct()."); |
| 127 | + Assert.IsTrue ( |
| 128 | + t.InvokedConstructor, |
| 129 | + "(int) constructor should have been called, via ManagedPeer.construct()."); |
| 130 | + |
| 131 | + var registered = JniRuntime.CurrentRuntime.ValueManager.PeekValue (t.PeerReference); |
| 132 | + var acIntermediate = CallVirtualFromConstructorDerived.Intermediate_FromActivationConstructor; |
| 133 | + var cfIntermediate = CallVirtualFromConstructorDerived.Intermediate_FromCalledFromConstructor; |
| 134 | + |
| 135 | + Assert.AreSame (t, registered, |
| 136 | + "Expected t and registered to be the same instance; " + |
| 137 | + $"t={RuntimeHelpers.GetHashCode (t).ToString ("x")}, " + |
| 138 | + $"registered={RuntimeHelpers.GetHashCode (registered).ToString ("x")}"); |
| 139 | + Assert.AreSame (t, acIntermediate, |
| 140 | + "Expected t and registered to be the same instance; " + |
| 141 | + $"t={RuntimeHelpers.GetHashCode (t).ToString ("x")}, " + |
| 142 | + $"acIntermediate={RuntimeHelpers.GetHashCode (acIntermediate).ToString ("x")}"); |
| 143 | + Assert.AreSame (t, cfIntermediate, |
| 144 | + "Expected t and cfIntermediate to be the same instance; " + |
| 145 | + $"t={RuntimeHelpers.GetHashCode (t).ToString ("x")}, " + |
| 146 | + $"cfIntermediate={RuntimeHelpers.GetHashCode (cfIntermediate).ToString ("x")}"); |
| 147 | + |
| 148 | + Dispose (ref CallVirtualFromConstructorDerived.Intermediate_FromActivationConstructor); |
| 149 | + Dispose (ref CallVirtualFromConstructorDerived.Intermediate_FromCalledFromConstructor); |
29 | 150 | }
|
30 | 151 | }
|
31 | 152 | }
|
|
0 commit comments