public void PropertyObserver_Getter_Observes_instance_StringProperty_AutoActivateTrue()
        {
            var instance = new NotifyPropertyChangedClass1()
            {
                StringProperty = null
            };
            var callCount = 0;

            using var observes = PropertyReferenceObserver.ObservesAndGet(() => instance.StringProperty, true, () => callCount++);
            Assert.AreEqual(0, callCount);
            Assert.AreEqual(null, observes.Value);

            instance.StringProperty = "1";
            Assert.AreEqual(1, callCount);
            Assert.AreEqual("1", observes.Value);

            instance.StringProperty = "2";
            Assert.AreEqual(2, callCount);
            Assert.AreEqual("2", observes.Value);

            observes.Deactivate();
            Assert.AreEqual(2, callCount);
            Assert.AreEqual("2", observes.Value);

            instance.StringProperty = "3";
            Assert.AreEqual(2, callCount);
            Assert.AreEqual("3", observes.Value);

            instance.StringProperty = null;
            Assert.AreEqual(2, callCount);
            Assert.AreEqual(null, observes.Value);
        }
コード例 #2
0
        public void PropertyObserver_Getter_Observes_instance1_StringProperty_AutoActivateFalse()
        {
            var instance1 = new NotifyPropertyChangedClass1 {
                StringProperty = null
            };
            var instance2 = new NotifyPropertyChangedClass1();
            var callCount = 0;

            using var observes = PropertyReferenceObserver.ObservesAndGet(
                      instance1,
                      instance2,
                      (i1, i2) => i1.StringProperty,
                      false,
                      () => callCount++);
            Assert.AreEqual(0, callCount);
            Assert.AreEqual(null, observes.Value);

            instance1.StringProperty = "1";
            Assert.AreEqual(0, callCount);
            Assert.AreEqual("1", observes.Value);

            observes.Activate();
            Assert.AreEqual(1, callCount);
            Assert.AreEqual("1", observes.Value);

            instance1.StringProperty = "2";
            Assert.AreEqual(2, callCount);
            Assert.AreEqual("2", observes.Value);

            observes.Deactivate();
            Assert.AreEqual(2, callCount);
            Assert.AreEqual("2", observes.Value);

            instance1.StringProperty = "3";
            Assert.AreEqual(2, callCount);
            Assert.AreEqual("3", observes.Value);

            instance1.StringProperty = null;
            Assert.AreEqual(2, callCount);
            Assert.AreEqual(null, observes.Value);
        }