|
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 () |
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.AreNotSame (t, acIntermediate, |
| 43 | + "Expected t and registered to be the same instance; " + |
| 44 | + $"t={RuntimeHelpers.GetHashCode (t).ToString ("x")}, " + |
| 45 | + $"registered={RuntimeHelpers.GetHashCode (registered).ToString ("x")}"); |
| 46 | + Assert.AreSame (t, cfIntermediate, |
| 47 | + "Expected t and cfIntermediate to be the same instance; " + |
| 48 | + $"t={RuntimeHelpers.GetHashCode (t).ToString ("x")}, " + |
| 49 | + $"cfIntermediate={RuntimeHelpers.GetHashCode (cfIntermediate).ToString ("x")}"); |
| 50 | + |
| 51 | + CallVirtualFromConstructorDerived.Intermediate_FromActivationConstructor = null; |
| 52 | + CallVirtualFromConstructorDerived.Intermediate_FromCalledFromConstructor = null; |
19 | 53 | }
|
20 | 54 |
|
21 | 55 | [Test]
|
22 |
| - public void ActivationConstructor () |
| 56 | + public void CreateJavaInstanceFirst () |
23 | 57 | {
|
24 |
| - var t = CallVirtualFromConstructorDerived.NewInstance (42); |
25 |
| - using (t) { |
26 |
| - Assert.IsTrue (t.InvokedActivationConstructor); |
27 |
| - Assert.IsTrue (t.InvokedConstructor); |
28 |
| - } |
| 58 | + CallVirtualFromConstructorDerived.Intermediate_FromActivationConstructor = null; |
| 59 | + CallVirtualFromConstructorDerived.Intermediate_FromCalledFromConstructor = null; |
| 60 | + |
| 61 | + using var t = CallVirtualFromConstructorDerived.NewInstance (42); |
| 62 | + |
| 63 | + Assert.IsTrue ( |
| 64 | + t.Called, |
| 65 | + "CalledFromConstructor method override should have been called."); |
| 66 | + Assert.IsTrue ( |
| 67 | + t.InvokedActivationConstructor, |
| 68 | + "Activation Constructor should have been called, as calledFromConstructor() is invoked before ManagedPeer.construct()."); |
| 69 | + Assert.IsTrue ( |
| 70 | + t.InvokedConstructor, |
| 71 | + "(int) constructor should have been called, via ManagedPeer.construct()."); |
| 72 | + |
| 73 | + var registered = JniRuntime.CurrentRuntime.ValueManager.PeekValue (t.PeerReference); |
| 74 | + var acIntermediate = CallVirtualFromConstructorDerived.Intermediate_FromActivationConstructor; |
| 75 | + var cfIntermediate = CallVirtualFromConstructorDerived.Intermediate_FromCalledFromConstructor; |
| 76 | + |
| 77 | + Assert.AreSame (t, registered, |
| 78 | + "Expected t and registered to be the same instance; " + |
| 79 | + $"t={RuntimeHelpers.GetHashCode (t).ToString ("x")}, " + |
| 80 | + $"registered={RuntimeHelpers.GetHashCode (registered).ToString ("x")}"); |
| 81 | + Assert.AreSame (t, acIntermediate, |
| 82 | + "Expected t and registered to be the same instance; " + |
| 83 | + $"t={RuntimeHelpers.GetHashCode (t).ToString ("x")}, " + |
| 84 | + $"registered={RuntimeHelpers.GetHashCode (registered).ToString ("x")}"); |
| 85 | + Assert.AreSame (t, cfIntermediate, |
| 86 | + "Expected t and cfIntermediate to be the same instance; " + |
| 87 | + $"t={RuntimeHelpers.GetHashCode (t).ToString ("x")}, " + |
| 88 | + $"cfIntermediate={RuntimeHelpers.GetHashCode (cfIntermediate).ToString ("x")}"); |
| 89 | + |
| 90 | + CallVirtualFromConstructorDerived.Intermediate_FromActivationConstructor = null; |
| 91 | + CallVirtualFromConstructorDerived.Intermediate_FromCalledFromConstructor = null; |
29 | 92 | }
|
30 | 93 | }
|
31 | 94 | }
|
|
0 commit comments