Esempio n. 1
0
        public void TestWithoutSerializableHandler()
        {
            IDataPortal <SerializationRoot> dataPortal = _testDIContext.CreateDataPortal <SerializationRoot>();

            TestResults.Reinitialise();
            UnitTestContext             context = GetContext();
            SerializationRoot           root    = SerializationRoot.NewSerializationRoot(dataPortal);
            nonSerializableEventHandler handler = new nonSerializableEventHandler();

            handler.Reg(root);
            root.Data = "something";
            context.Assert.AreEqual("1", TestResults.GetResult("PropertyChangedFiredCount"));
            root.Data = "something else";
            context.Assert.AreEqual("2", TestResults.GetResult("PropertyChangedFiredCount"));

            //serialize an object with eventhandling objects that are nonserializable
            root      = root.Clone();
            root.Data = "something new";

            //still at 2 even though we changed the property again
            //when the clone method performs serialization, the nonserializable
            //object containing an event handler for the propertyChanged event
            //is lost
            context.Assert.AreEqual("2", TestResults.GetResult("PropertyChangedFiredCount"));
            context.Assert.Success();
        }
Esempio n. 2
0
        public void TestSerializableEventsActionFails()
        {
            IDataPortal <SerializationRoot> dataPortal = _testDIContext.CreateDataPortal <SerializationRoot>();

            var root        = SerializationRoot.NewSerializationRoot(dataPortal);
            var nonSerClass = new NonSerializedClass();
            Action <object, PropertyChangedEventArgs> h = (sender, eventArgs) => { nonSerClass.Do(); };
            var method  = typeof(Action <object, PropertyChangedEventArgs>).GetMethod("Invoke");
            var delgate = (PropertyChangedEventHandler)(object)method.CreateDelegate(typeof(PropertyChangedEventHandler), h);

            root.PropertyChanged += delgate;
            // TODO: Should this test target another formatter, or just be deleted?
            //var b = new BinaryFormatterWrapper();
            //try
            //{
            //  b.Serialize(new MemoryStream(), root);
            //  Assert.Fail("Serialization should have thrown an exception");
            //}
            //catch (System.Runtime.Serialization.SerializationException)
            //{
            //  // serialization failed as expected
            //}
        }
Esempio n. 3
0
        public void SerializableEvents()
        {
            IDataPortal <SerializationRoot> dataPortal = _testDIContext.CreateDataPortal <SerializationRoot>();

            TestResults.Reinitialise();
            UnitTestContext context = GetContext();

            SerializationRoot root    = SerializationRoot.NewSerializationRoot(dataPortal);
            TestEventSink     handler = new TestEventSink();

            root.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler
                                        (OnIsDirtyChanged);

            root.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler
                                        (StaticOnIsDirtyChanged);

            root.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler
                                        (PublicStaticOnIsDirtyChanged);

            root.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler
                                        (OnIsDirtyChanged); //will call this method twice since it is assigned twice

            handler.Reg(root);

            root.Data = "abc";

            context.Assert.AreEqual("abc", root.Data, "Data value not set");

            context.Assert.AreEqual(
                "OnIsDirtyChanged",
                TestResults.GetResult("OnIsDirtyChanged"),
                "Didn't call local handler");

            context.Assert.AreEqual(
                "StaticOnIsDirtyChanged",
                TestResults.GetResult("StaticOnIsDirtyChanged"),
                "Didn't call static handler");

            Assert.AreEqual(
                "PublicStaticOnIsDirtyChanged",
                TestResults.GetResult("PublicStaticOnIsDirtyChanged"),
                "Didn't call public static handler");

            Assert.AreEqual(
                "Test.OnIsDirtyChanged",
                TestResults.GetResult("Test.OnIsDirtyChanged"),
                "Didn't call serializable handler");

            Assert.AreEqual(
                "Test.PrivateOnIsDirtyChanged",
                TestResults.GetResult("Test.PrivateOnIsDirtyChanged"),
                "Didn't call serializable private handler");

            root = (SerializationRoot)root.Clone();

            TestResults.Reinitialise();

            root.Data = "xyz";

            context.Assert.AreEqual("xyz", root.Data, "Data value not set");

            context.Assert.AreEqual("", TestResults.GetResult("OnIsDirtyChanged"),
                                    "Called local handler after clone");

            context.Assert.AreEqual("", TestResults.GetResult("StaticOnIsDirtyChanged"),
                                    "Called static handler after clone");

            context.Assert.AreEqual(
                "PublicStaticOnIsDirtyChanged",
                TestResults.GetResult("PublicStaticOnIsDirtyChanged"),
                "Didn't call public static handler after clone");

            context.Assert.AreEqual(
                "Test.OnIsDirtyChanged",
                TestResults.GetResult("Test.OnIsDirtyChanged"),
                "Didn't call serializable handler after clone");

            context.Assert.AreEqual("", TestResults.GetResult("Test.PrivateOnIsDirtyChanged"),
                                    "Called serializable private handler after clone");

            context.Assert.Success();
        }