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