@@ -15,7 +15,7 @@ namespace Java.InteropTests {
1515 public class InvokeVirtualFromConstructorTests : JavaVMFixture
1616 {
1717 [ Test ]
18- public void CreateManagedInstanceFirst ( )
18+ public void CreateManagedInstanceFirst_WithAllocObject ( )
1919 {
2020 CallVirtualFromConstructorDerived . Intermediate_FromActivationConstructor = null ;
2121 CallVirtualFromConstructorDerived . Intermediate_FromCalledFromConstructor = null ;
@@ -39,17 +39,75 @@ public void CreateManagedInstanceFirst ()
3939 "Expected t and registered to be the same instance; " +
4040 $ "t={ RuntimeHelpers . GetHashCode ( t ) . ToString ( "x" ) } , " +
4141 $ "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" ) } ") ;
42+ Assert . IsNull ( acIntermediate ,
43+ "Activation Constructor should not have been called, because of AllocObject semantics" ) ;
4644 Assert . AreSame ( t , cfIntermediate ,
4745 "Expected t and cfIntermediate to be the same instance; " +
4846 $ "t={ RuntimeHelpers . GetHashCode ( t ) . ToString ( "x" ) } , " +
4947 $ "cfIntermediate={ RuntimeHelpers . GetHashCode ( cfIntermediate ) . ToString ( "x" ) } ") ;
5048
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 ;
61+ }
62+
63+ [ Test ]
64+ public void CreateManagedInstanceFirst_WithNewObject ( )
65+ {
5166 CallVirtualFromConstructorDerived . Intermediate_FromActivationConstructor = null ;
5267 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 ) ;
53111 }
54112
55113 [ Test ]
@@ -81,14 +139,14 @@ public void CreateJavaInstanceFirst ()
81139 Assert . AreSame ( t , acIntermediate ,
82140 "Expected t and registered to be the same instance; " +
83141 $ "t={ RuntimeHelpers . GetHashCode ( t ) . ToString ( "x" ) } , " +
84- $ "registered ={ RuntimeHelpers . GetHashCode ( registered ) . ToString ( "x" ) } ") ;
142+ $ "acIntermediate ={ RuntimeHelpers . GetHashCode ( acIntermediate ) . ToString ( "x" ) } ") ;
85143 Assert . AreSame ( t , cfIntermediate ,
86144 "Expected t and cfIntermediate to be the same instance; " +
87145 $ "t={ RuntimeHelpers . GetHashCode ( t ) . ToString ( "x" ) } , " +
88146 $ "cfIntermediate={ RuntimeHelpers . GetHashCode ( cfIntermediate ) . ToString ( "x" ) } ") ;
89147
90- CallVirtualFromConstructorDerived . Intermediate_FromActivationConstructor = null ;
91- CallVirtualFromConstructorDerived . Intermediate_FromCalledFromConstructor = null ;
148+ Dispose ( ref CallVirtualFromConstructorDerived . Intermediate_FromActivationConstructor ) ;
149+ Dispose ( ref CallVirtualFromConstructorDerived . Intermediate_FromCalledFromConstructor ) ;
92150 }
93151 }
94152}
0 commit comments