Exemple #1
0
        public void DisposeWithJavaObjectDisposesObject([Values(true, false)] bool register)
        {
            var native = new JavaObject();

            if (!register)
            {
                native.UnregisterFromRuntime();
            }

            var instance = new DynamicJavaInstance(native);

            Assert.AreEqual(1, JavaClassInfo.GetClassInfoCount("java/lang/Object"));

            Assert.AreSame(native, instance.Value);
            instance.Dispose();
            Assert.AreEqual(null, instance.Value);
            Assert.AreEqual(-1, JavaClassInfo.GetClassInfoCount("java/lang/Object"));

            if (register)
            {
                Assert.IsTrue(native.PeerReference.IsValid);
            }
            else
            {
                Assert.IsFalse(native.PeerReference.IsValid);
            }
        }
        public void ObjectDisposed()
        {
            var o = new JavaObject();

            o.Dispose();

            // These should not throw
            var h = o.PeerReference;
            var p = o.JniPeerMembers;

            // These should throw
            Assert.Throws <ObjectDisposedException> (() => o.GetHashCode());
            Assert.Throws <ObjectDisposedException> (() => o.UnregisterFromRuntime());
            Assert.Throws <ObjectDisposedException> (() => o.ToString());
            Assert.Throws <ObjectDisposedException> (() => o.Equals(o));
        }
Exemple #3
0
        public void GetValue_ReturnsAlias()
        {
            var local = new JavaObject();

            local.UnregisterFromRuntime();
            Assert.IsNull(JniRuntime.CurrentRuntime.ValueManager.PeekValue(local.PeerReference));
            // GetObject must always return a value (unless handle is null, etc.).
            // However, since we called local.UnregisterFromRuntime(),
            // JniRuntime.PeekObject() is null (asserted above), but GetObject() must
            // **still** return _something_.
            // In this case, it returns an _alias_.
            // TODO: "most derived type" alias generation. (Not relevant here, but...)
            var p     = local.PeerReference;
            var alias = JniRuntime.CurrentRuntime.ValueManager.GetValue <IJavaPeerable> (ref p, JniObjectReferenceOptions.Copy);

            Assert.AreNotSame(local, alias);
            alias.Dispose();
            local.Dispose();
        }
        public void UnregisterFromRuntime()
        {
            int registeredCount = JniRuntime.CurrentRuntime.ValueManager.GetSurfacedPeers().Count;
            JniObjectReference l;
            JavaObject         o;

            using (o = new JavaObject()) {
                l = o.PeerReference.NewLocalRef();
                Assert.AreEqual(JniObjectReferenceType.Global, o.PeerReference.Type);
                Assert.AreEqual(registeredCount + 1, JniRuntime.CurrentRuntime.ValueManager.GetSurfacedPeers().Count);
                Assert.IsNotNull(JniRuntime.CurrentRuntime.ValueManager.PeekValue(l));
                Assert.AreNotSame(l, o.PeerReference);
                Assert.AreSame(o, JniRuntime.CurrentRuntime.ValueManager.PeekValue(l));
            }
            Assert.AreEqual(registeredCount, JniRuntime.CurrentRuntime.ValueManager.GetSurfacedPeers().Count);
            Assert.IsNull(JniRuntime.CurrentRuntime.ValueManager.PeekValue(l));
            JniObjectReference.Dispose(ref l);
            Assert.Throws <ObjectDisposedException> (() => o.UnregisterFromRuntime());
        }