public void ObservableExpressionCallsTest()
        {
            CallsTestElement element = new CallsTestElement();

            element.Value = 1;

            Assert.AreEqual(0, element.ValueGetterCallsCount);
            Assert.AreEqual(1, element.ValueSetterCallsCount);

            ObservableExpression observer = new ObservableExpression(element, "Value");

            Assert.AreEqual(1, element.ValueGetterCallsCount);
            Assert.AreEqual(1, element.ValueSetterCallsCount);
            Assert.AreEqual(1, observer.Value);

            Assert.IsTrue(observer.TrySetValue(2));

            Assert.AreEqual(2, element.ValueGetterCallsCount);
            Assert.AreEqual(2, element.ValueSetterCallsCount);
            Assert.AreEqual(2, observer.Value);

            Assert.IsTrue(observer.TrySetValue(2));

            Assert.AreEqual(2, element.ValueGetterCallsCount);
            Assert.AreEqual(2, element.ValueSetterCallsCount);
            Assert.AreEqual(2, observer.Value);

            Assert.IsTrue(observer.TrySetValue(3));

            Assert.AreEqual(3, element.ValueGetterCallsCount);
            Assert.AreEqual(3, element.ValueSetterCallsCount);
            Assert.AreEqual(3, observer.Value);
        }
        public void ObservableExpressionHybridPropertyTest()
        {
            DependencyObjectNode child           = new DependencyObjectNode();
            ObservableNode       observableChild = new ObservableNode {
                Value = 1
            };

            DependencyObjectNode root = new DependencyObjectNode {
                Value_ClrWrapper = 0
            };

            int valueChangedCount = 0;

            ObservableExpression observer = new ObservableExpression(root, "Child.ObservableChild.Value");

            observer.ValueChanged += (sender, e) => valueChangedCount++;

            Assert.AreEqual(ObservableValue.UnsetValue, observer.Value);

            root.Child_ClrWrapper            = child;
            child.ObservableChild_ClrWrapper = observableChild;

            Assert.AreEqual(1, observer.Value);
            Assert.AreEqual(1, valueChangedCount);

            observableChild.Value = 2;

            Assert.AreEqual(2, observer.Value);
            Assert.AreEqual(2, valueChangedCount);

            root.Child_ClrWrapper = null;

            Assert.AreEqual(ObservableValue.UnsetValue, observer.Value);
            Assert.AreEqual(3, valueChangedCount);

            observer.Dispose();

            root.Child_ClrWrapper = child;

            Assert.AreEqual(3, valueChangedCount);
        }
        public void ObservableExpressionCustomIndexerTest()
        {
            ObservableNode root = new ObservableNode {
                ItemValue = 2
            };
            int valueChanged = 0;

            ObservableExpression observableExpression1 = new ObservableExpression(root, "Item[3]");
            ObservableExpression observableExpression2 = new ObservableExpression(root, "[4]");

            observableExpression1.ValueChanged += (sender, e) => valueChanged++;
            observableExpression2.ValueChanged += (sender, e) => valueChanged++;

            Assert.AreEqual(6, observableExpression1.Value);
            Assert.AreEqual(8, observableExpression2.Value);

            root.ItemValue = 3;
            Assert.AreEqual(9, observableExpression1.Value);
            Assert.AreEqual(12, observableExpression2.Value);
            Assert.AreEqual(2, valueChanged);
        }
示例#4
0
 public void TryItOut()
 {
     var sampleClass = new SampleClass(10);
     var observableMethodExpression = new ObservableExpression <int>(() => sampleClass.Method());
 }
        public void ObservableExpressionIndexerTest()
        {
            ObservableNode root = new ObservableNode {
                Children = new ObservableCollection <ObservableNode> {
                    new ObservableNode {
                        Value = 1
                    }
                }
            };

            int valueChanged = 0;

            ObservableExpression observableExpression = new ObservableExpression(root, "Children[0].Value");

            observableExpression.ValueChanged += (sender, e) => valueChanged++;

            Assert.AreEqual(1, observableExpression.Value);
            Assert.AreEqual(0, valueChanged);

            root.Children[0].Value = 2;
            Assert.AreEqual(2, observableExpression.Value);
            Assert.AreEqual(1, valueChanged);

            root.Children[0] = new ObservableNode {
                Value = 3
            };
            Assert.AreEqual(3, observableExpression.Value);
            Assert.AreEqual(2, valueChanged);

            root.Children = new ObservableCollection <ObservableNode> {
                new ObservableNode {
                    Value = 4
                }
            };
            Assert.AreEqual(4, observableExpression.Value);
            Assert.AreEqual(3, valueChanged);

            root.Children[0] = new ObservableNode {
                Value = 4
            };
            Assert.AreEqual(4, observableExpression.Value);
            Assert.AreEqual(3, valueChanged);

            root.Children = new ObservableCollection <ObservableNode> {
                new ObservableNode {
                    Value = 4
                }
            };
            Assert.AreEqual(4, observableExpression.Value);
            Assert.AreEqual(3, valueChanged);

            root.Children.Add(new ObservableNode {
                Value = 5
            });
            Assert.AreEqual(4, observableExpression.Value);
            Assert.AreEqual(3, valueChanged);

            root.Children.RemoveAt(0);
            Assert.AreEqual(5, observableExpression.Value);
            Assert.AreEqual(4, valueChanged);

            observableExpression.Dispose();
            root.Children[0].Value = 6;
            Assert.AreEqual(4, valueChanged);
        }
        public void ObservableExpressionDependencyPropertyTest()
        {
            DependencyObjectNode child1 = new DependencyObjectNode {
                Value_ClrWrapper = 1
            };
            DependencyObjectNode child2 = new DependencyObjectNode {
                Value_ClrWrapper = 2
            };

            DependencyObjectNode root = new DependencyObjectNode {
                Value_ClrWrapper = 0
            };

            int valueChangedCount = 0;

            ObservableExpression observableExpression = new ObservableExpression(root, "Child.Value");

            observableExpression.ValueChanged += (sender, e) => valueChangedCount++;

            Assert.AreEqual(ObservableValue.UnsetValue, observableExpression.Value);

            root.Child_ClrWrapper = child1;

            Assert.AreEqual(1, observableExpression.Value);
            Assert.AreEqual(1, valueChangedCount);

            child1.Value_ClrWrapper = 2;

            Assert.AreEqual(2, observableExpression.Value);
            Assert.AreEqual(2, valueChangedCount);

            root.Child_ClrWrapper = child2;

            Assert.AreEqual(2, observableExpression.Value);
            Assert.AreEqual(2, valueChangedCount);

            child1.Value_ClrWrapper = 3;

            Assert.AreEqual(2, observableExpression.Value);
            Assert.AreEqual(2, valueChangedCount);

            child2.Value_ClrWrapper = 3;

            Assert.AreEqual(3, observableExpression.Value);
            Assert.AreEqual(3, valueChangedCount);

            Assert.AreEqual(true, observableExpression.TrySetValue(4));
            Assert.AreEqual(3, child1.Value_ClrWrapper);
            Assert.AreEqual(4, child2.Value_ClrWrapper);
            Assert.AreEqual(4, valueChangedCount);

            root.Child_ClrWrapper = null;

            Assert.AreEqual(ObservableValue.UnsetValue, observableExpression.Value);
            Assert.AreEqual(5, valueChangedCount);

            observableExpression.Dispose();

            root.Child_ClrWrapper = child1;
            Assert.AreEqual(5, valueChangedCount);
        }
        public void ObservableExpressionClrPropertyTest()
        {
            ObservableNode child1 = new ObservableNode {
                Value = 1
            };
            ObservableNode child2 = new ObservableNode {
                Value = 2
            };

            ObservableNode root = new ObservableNode {
                Value = 0
            };

            int valueChangedCount = 0;

            ObservableExpression observableExpression = new ObservableExpression(root, "Child.Value");

            observableExpression.ValueChanged += (sender, oldValue, newValue) => valueChangedCount++;

            Assert.AreEqual(ObservableValue.UnsetValue, observableExpression.Value);

            root.Child = child1;

            Assert.AreEqual(1, observableExpression.Value);
            Assert.AreEqual(1, valueChangedCount);

            child1.Value = 2;

            Assert.AreEqual(2, observableExpression.Value);
            Assert.AreEqual(2, valueChangedCount);

            root.Child = child2;

            Assert.AreEqual(2, observableExpression.Value);
            Assert.AreEqual(2, valueChangedCount);

            child1.Value = 3;

            Assert.AreEqual(2, observableExpression.Value);
            Assert.AreEqual(2, valueChangedCount);

            child2.Value = 3;

            Assert.AreEqual(3, observableExpression.Value);
            Assert.AreEqual(3, valueChangedCount);

            Assert.AreEqual(true, observableExpression.TrySetValue(4));
            Assert.AreEqual(3, child1.Value);
            Assert.AreEqual(4, child2.Value);
            Assert.AreEqual(4, valueChangedCount);

            root.Child = null;

            Assert.AreEqual(ObservableValue.UnsetValue, observableExpression.Value);
            Assert.AreEqual(5, valueChangedCount);

            observableExpression.Dispose();

            root.Child = child1;
            Assert.AreEqual(5, valueChangedCount);
        }