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 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);
        }
Ejemplo n.º 4
0
        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);
        }