Esempio n. 1
0
        public void Encode()
        {
            IntPtr nscoding = Runtime.GetProtocol("NSCoding");

            Assert.That(nscoding, Is.Not.EqualTo(IntPtr.Zero), "NSCoding");

#if !XAMCORE_2_0
            // NSObject does not conform to NSCoding
            using (var o = new NSObject()) {
                Assert.False(o.ConformsToProtocol(nscoding), "NSObject/NSCoding");
                using (var c = new NSCoder()) {
                    Assert.Throws <InvalidOperationException> (delegate {
                        o.EncodeTo(c);
                    }, "NSObject/!NSCoding");
                }
            }
#endif

            // NSNumber conforms to NSCoding
            using (var n = new NSNumber(-1)) {
                Assert.True(n.ConformsToProtocol(nscoding), "NSNumber/NSCoding");
                using (var d = new NSMutableData())
                    using (var a = new NSKeyedArchiver(d)) {
                        n.EncodeTo(a);
                        a.FinishEncoding();
                    }
            }
        }
Esempio n. 2
0
        protected virtual void CheckNSObjectProtocol(NSObject obj)
        {
            // not documented to allow null, but commonly used this way. OTOH it's not clear what code answer this
            // (it might be different implementations) but we can make sure that Apple allows null with this test
            // ref: https://bugzilla.xamarin.com/show_bug.cgi?id=35924
            var kind_of_null = obj.IsKindOfClass(null);

            if (kind_of_null)
            {
                ReportError("{0} : IsKindOfClass(null) failed", instance_type_name);
            }
            var is_member_of_null = obj.IsMemberOfClass(null);

            if (is_member_of_null)
            {
                ReportError("{0} : IsMemberOfClass(null) failed", instance_type_name);
            }
            var respond_to_null = obj.RespondsToSelector(null);

            if (respond_to_null)
            {
                ReportError("{0} : RespondToSelector(null) failed", instance_type_name);
            }
            var conforms_to_null = obj.ConformsToProtocol(IntPtr.Zero);

            if (conforms_to_null)
            {
                ReportError("{0} : ConformsToProtocol(null) failed", instance_type_name);
            }
        }
Esempio n. 3
0
        public void Copy()
        {
            IntPtr nscopying = Runtime.GetProtocol("NSCopying");

            Assert.That(nscopying, Is.Not.EqualTo(IntPtr.Zero), "NSCopying");

            IntPtr nsmutablecopying = Runtime.GetProtocol("NSMutableCopying");

            Assert.That(nsmutablecopying, Is.Not.EqualTo(IntPtr.Zero), "NSMutableCopying");

            // NSObject does not conform to NSCopying
            using (var o = new NSObject()) {
                Assert.False(o.ConformsToProtocol(nscopying), "NSObject/NSCopying");
                Assert.False(o.ConformsToProtocol(nsmutablecopying), "NSObject/NSMutableCopying");
            }

            // NSNumber conforms to NSCopying - but not NSMutableCopying
            using (var n = new NSNumber(-1)) {
                Assert.True(n.ConformsToProtocol(nscopying), "NSNumber/NSCopying");
                using (var xn = n.Copy()) {
                    Assert.NotNull(xn, "NSNumber/Copy/NotNull");
                    Assert.AreSame(n, xn, "NSNumber/Copy/NotSame");
                }
                Assert.False(n.ConformsToProtocol(nsmutablecopying), "NSNumber/NSMutableCopying");
            }

            // NSMutableString conforms to NSCopying - but not NSMutableCopying
            using (var s = new NSMutableString(1)) {
                Assert.True(s.ConformsToProtocol(nscopying), "NSMutableString/NSCopying");
                using (var xs = s.Copy()) {
                    Assert.NotNull(xs, "NSMutableString/Copy/NotNull");
                    Assert.AreNotSame(s, xs, "NSMutableString/Copy/NotSame");
                }
                Assert.True(s.ConformsToProtocol(nsmutablecopying), "NSMutableString/NSMutableCopying");
                using (var xs = s.MutableCopy()) {
                    Assert.NotNull(xs, "NSMutableString/MutableCopy/NotNull");
                    Assert.AreNotSame(s, xs, "NSMutableString/MutableCopy/NotSame");
                }
            }
        }