public void PropertyObserver_Observes_instance_IntProperty_AutoActivateFalse() { var instance = new NotifyPropertyChangedClass1() { Class2 = null }; var callCount = 0; using var observes = PropertyObserver.Observes(() => instance.Class2.IntProperty, false, () => callCount++); Assert.AreEqual(0, callCount); instance.Class2 = new NotifyPropertyChangedClass2 { IntProperty = 1 }; Assert.AreEqual(0, callCount); observes.Activate(); Assert.AreEqual(1, callCount); instance.Class2.IntProperty = 2; Assert.AreEqual(2, callCount); observes.Deactivate(); Assert.AreEqual(2, callCount); instance.Class2.IntProperty = 3; Assert.AreEqual(2, callCount); }
public void PropertyObserver_Observes_instance1_StringProperty_AutoActivateTrue() { var instance1 = new NotifyPropertyChangedClass1(); var instance2 = new NotifyPropertyChangedClass1(); var callCount = 0; using var observes = PropertyObserver.Observes( instance1, instance2, (i1, i2) => i1.StringProperty, true, () => callCount++); Assert.AreEqual(0, callCount); instance1.StringProperty = "1"; Assert.AreEqual(1, callCount); instance1.StringProperty = "2"; Assert.AreEqual(2, callCount); observes.Deactivate(); Assert.AreEqual(2, callCount); instance1.StringProperty = "3"; Assert.AreEqual(2, callCount); }
public void NotifyPropertyChanged_Integer_Subscribe() { var actionRaisedCount = 0; var notifyPropertyChangedTestObject = new NotifyPropertyChangedTestObject { IntProperty = 1 }; using var observer = PropertyObserver.Observes( notifyPropertyChangedTestObject.IntPropertyExpression, () => actionRaisedCount++, false); Assert.AreEqual(0, actionRaisedCount); notifyPropertyChangedTestObject.IntProperty = 2; Assert.AreEqual(0, actionRaisedCount); observer.Subscribe(); notifyPropertyChangedTestObject.IntProperty = 3; Assert.AreEqual(1, actionRaisedCount); notifyPropertyChangedTestObject.IntProperty = 4; Assert.AreEqual(2, actionRaisedCount); observer.Unsubscribe(); notifyPropertyChangedTestObject.IntProperty = 5; Assert.AreEqual(2, actionRaisedCount); }
public void PropertyObserver_Getter_Fallback_Observes_instance_StringProperty_AutoActivateTrue() { var instance = new NotifyPropertyChangedClass1() { StringProperty = null }; var callCount = 0; using var observes = PropertyObserver.Observes(() => instance.StringProperty, true, () => callCount++, "Fallback"); Assert.AreEqual(0, callCount); Assert.AreEqual("Fallback", 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("Fallback", observes.Value); }
public void NotifyPropertyChanged_OwnerAndExpression_ObservesIntegerAndBoolean_Test() { var actionIntegerRaised = false; var actionBooleanRaised = false; var notifyPropertyChangedTestObject = new NotifyPropertyChangedTestObject { IntProperty = 1, BoolProperty = false }; using var integerObserver = PropertyObserver.Observes( notifyPropertyChangedTestObject, o => o.IntProperty, () => actionIntegerRaised = true); using var booleanObserver = PropertyObserver.Observes( notifyPropertyChangedTestObject, o => o.BoolProperty, () => actionBooleanRaised = true); Assert.False(actionIntegerRaised); Assert.False(actionBooleanRaised); notifyPropertyChangedTestObject.BoolProperty = true; Assert.True(actionBooleanRaised); Assert.False(actionIntegerRaised); notifyPropertyChangedTestObject.IntProperty = 2; Assert.True(actionBooleanRaised); Assert.True(actionIntegerRaised); }
public void PropertyObserver_Getter_Fallback_Observes_instance_IntProperty_AutoActivateTrue() { var instance = new NotifyPropertyChangedClass1() { Class2 = null }; var callCount = 0; using var observes = PropertyObserver.Observes(() => instance.Class2.IntProperty, true, () => callCount++, 99); Assert.AreEqual(0, callCount); Assert.AreEqual(99, observes.Value); instance.Class2 = new NotifyPropertyChangedClass2 { IntProperty = 1 }; Assert.AreEqual(1, callCount); Assert.AreEqual(1, observes.Value); instance.Class2.IntProperty = 2; Assert.AreEqual(2, callCount); Assert.AreEqual(2, observes.Value); observes.Deactivate(); Assert.AreEqual(2, callCount); Assert.AreEqual(2, observes.Value); instance.Class2.IntProperty = 3; Assert.AreEqual(2, callCount); Assert.AreEqual(3, observes.Value); instance.Class2 = null; Assert.AreEqual(2, callCount); Assert.AreEqual(99, observes.Value); }
public void PropertyObserver_Observes_instance1_IntProperty_AutoActivateTrue() { var instance1 = new NotifyPropertyChangedClass1 { Class2 = null }; var instance2 = new NotifyPropertyChangedClass1 { Class2 = null }; var callCount = 0; using var observes = PropertyObserver.Observes( instance1, instance2, (i1, i2) => i1.Class2.IntProperty, true, () => callCount++); Assert.AreEqual(0, callCount); instance1.Class2 = new NotifyPropertyChangedClass2 { IntProperty = 1 }; Assert.AreEqual(1, callCount); instance1.Class2.IntProperty = 2; Assert.AreEqual(2, callCount); observes.Deactivate(); Assert.AreEqual(2, callCount); instance1.Class2.IntProperty = 3; Assert.AreEqual(2, callCount); }
public void PropertyObserver_ValueGetter_Fallback_Observes_instance1_IntProperty_AutoActivateFalse() { var instance1 = new NotifyPropertyChangedClass1 { Class2 = null }; var instance2 = new NotifyPropertyChangedClass1 { Class2 = null }; var callCount = 0; var value = -1; using var observes = PropertyObserver.Observes( instance1, instance2, (i1, i2) => i1.Class2.IntProperty, false, v => { value = v; callCount++; }, 99); Assert.AreEqual(0, callCount); Assert.AreEqual(-1, value); Assert.AreEqual(99, observes.Value); instance1.Class2 = new NotifyPropertyChangedClass2 { IntProperty = 1 }; Assert.AreEqual(0, callCount); Assert.AreEqual(-1, value); Assert.AreEqual(1, observes.Value); observes.Activate(); Assert.AreEqual(1, callCount); Assert.AreEqual(1, value); Assert.AreEqual(1, observes.Value); instance1.Class2.IntProperty = 2; Assert.AreEqual(2, callCount); Assert.AreEqual(2, value); Assert.AreEqual(2, observes.Value); observes.Deactivate(); Assert.AreEqual(2, callCount); Assert.AreEqual(2, value); Assert.AreEqual(2, observes.Value); instance1.Class2.IntProperty = 3; Assert.AreEqual(2, callCount); Assert.AreEqual(2, value); Assert.AreEqual(3, observes.Value); instance1.Class2 = null; Assert.AreEqual(2, callCount); Assert.AreEqual(2, value); Assert.AreEqual(99, observes.Value); }
public void PropertyObserver_ValueGetter_Fallback_Observes_instance1_StringProperty_AutoActivateFalse() { var instance1 = new NotifyPropertyChangedClass1 { StringProperty = null }; var instance2 = new NotifyPropertyChangedClass1(); var callCount = 0; var value = "Nil"; using var observes = PropertyObserver.Observes( instance1, instance2, (i1, i2) => i1.StringProperty, false, v => { value = v; callCount++; }, "Fallback"); Assert.AreEqual(0, callCount); Assert.AreEqual("Nil", value); Assert.AreEqual("Fallback", observes.Value); instance1.StringProperty = "1"; Assert.AreEqual(0, callCount); Assert.AreEqual("Nil", value); Assert.AreEqual("1", observes.Value); observes.Activate(); Assert.AreEqual(1, callCount); Assert.AreEqual("1", value); Assert.AreEqual("1", observes.Value); instance1.StringProperty = "2"; Assert.AreEqual(2, callCount); Assert.AreEqual("2", value); Assert.AreEqual("2", observes.Value); observes.Deactivate(); Assert.AreEqual(2, callCount); Assert.AreEqual("2", value); Assert.AreEqual("2", observes.Value); instance1.StringProperty = "3"; Assert.AreEqual(2, callCount); Assert.AreEqual("2", value); Assert.AreEqual("3", observes.Value); instance1.StringProperty = null; Assert.AreEqual(2, callCount); Assert.AreEqual("2", value); Assert.AreEqual("Fallback", observes.Value); }
public void NotifyPropertyChanged_SameAre_NotEqualOperator_Test() { var notifyPropertyChangedTestObject = new NotifyPropertyChangedTestObject(); using var observer1 = PropertyObserver.Observes( notifyPropertyChangedTestObject.IntPropertyExpression, () => { }); using var observer2 = PropertyObserver.Observes( notifyPropertyChangedTestObject.IntPropertyExpression, () => { }); Assert.False(observer1 != observer2); }
public void NotifyPropertyChanged_Equal_1_Test() { var notifyPropertyChangedTestObject = new NotifyPropertyChangedTestObject(); using var observer1 = PropertyObserver.Observes( notifyPropertyChangedTestObject.IntPropertyExpression, () => { }); using var observer2 = PropertyObserver.Observes( notifyPropertyChangedTestObject.IntPropertyExpression, () => { }); Assert.True(observer1.Equals(observer2)); }
public void NotifyPropertyChanged_Integer_AutoActivate_False() { var actionRaised = false; var notifyPropertyChangedTestObject = new NotifyPropertyChangedTestObject(); using var observer = PropertyObserver.Observes( notifyPropertyChangedTestObject.IntPropertyExpression, () => actionRaised = true, false); Assert.False(actionRaised); notifyPropertyChangedTestObject.IntProperty = 2; Assert.False(actionRaised); }
public void NotifyPropertyChanged_Different_NotEqualOperator_Test() { var notifyPropertyChangedTestObject = new NotifyPropertyChangedTestObject(); using var observer1 = PropertyObserver.Observes( notifyPropertyChangedTestObject.BoolPropertyExpression, () => { }); using var observer2 = PropertyObserver.Observes( notifyPropertyChangedTestObject.IntPropertyExpression, () => { }); Assert.True((object)observer1 != observer2); }
public void NotifyPropertyChanged_Equal_2_Test() { var notifyPropertyChangedTestObject = new NotifyPropertyChangedTestObject(); using var observer1 = PropertyObserver.Observes( notifyPropertyChangedTestObject.ComplexPropertyIntPropertyExpression, () => { }); using var observer2 = PropertyObserver.Observes( notifyPropertyChangedTestObject, o => o.ComplexProperty.IntProperty, () => { }); Assert.True(observer1 == observer2); }
public void PropertyObserver_ValueGetter_Fallback_Observes_instance_IntProperty() { var instance = new NotifyPropertyChangedClass1() { Class2 = null }; var callCount = 0; var value = -1; using var observes = PropertyObserver.Observes(() => instance.Class2.IntProperty, (v) => { value = v; callCount++; }, 99); Assert.AreEqual(0, callCount); Assert.AreEqual(-1, value); Assert.AreEqual(99, observes.Value); instance.Class2 = new NotifyPropertyChangedClass2 { IntProperty = 1 }; Assert.AreEqual(0, callCount); Assert.AreEqual(-1, value); Assert.AreEqual(1, observes.Value); observes.Activate(); Assert.AreEqual(1, callCount); Assert.AreEqual(1, value); Assert.AreEqual(1, observes.Value); instance.Class2.IntProperty = 2; Assert.AreEqual(2, callCount); Assert.AreEqual(2, value); Assert.AreEqual(2, observes.Value); observes.Deactivate(); Assert.AreEqual(2, callCount); Assert.AreEqual(2, value); Assert.AreEqual(2, observes.Value); instance.Class2.IntProperty = 3; Assert.AreEqual(2, callCount); Assert.AreEqual(2, value); Assert.AreEqual(3, observes.Value); instance.Class2 = null; Assert.AreEqual(2, callCount); Assert.AreEqual(2, value); Assert.AreEqual(99, observes.Value); }
/// <summary> /// Adds the expression. /// </summary> /// <param name="propertyExpression">The property expression.</param> /// <exception cref="System.ArgumentException">propertyExpression</exception> public void AddExpression(Expression <Func <T> > propertyExpression) { if (this.observedPropertiesExpressions.TryGetValue(propertyExpression.ToString(), out _)) { throw new ArgumentException( $"{propertyExpression} is already being observed.", nameof(propertyExpression)); } var observer = PropertyObserver.Observes(propertyExpression, () => this.Update.Raise()); this.observedPropertiesExpressions.Add(propertyExpression.ToString(), observer); observer.Subscribe(); }
public void PropertyObserver_ValueGetter_Fallback_Observes_instance_StringProperty() { var instance = new NotifyPropertyChangedClass1() { StringProperty = null }; var callCount = 0; var value = "Nil"; using var observes = PropertyObserver.Observes(() => instance.StringProperty, (v) => { value = v; callCount++; }, "Fallback"); Assert.AreEqual(0, callCount); Assert.AreEqual("Nil", value); Assert.AreEqual("Fallback", observes.Value); instance.StringProperty = "1"; Assert.AreEqual(0, callCount); Assert.AreEqual("Nil", value); Assert.AreEqual("1", observes.Value); observes.Activate(); Assert.AreEqual(1, callCount); Assert.AreEqual("1", value); Assert.AreEqual("1", observes.Value); instance.StringProperty = "2"; Assert.AreEqual(2, callCount); Assert.AreEqual("2", value); Assert.AreEqual("2", observes.Value); observes.Deactivate(); Assert.AreEqual(2, callCount); Assert.AreEqual("2", value); Assert.AreEqual("2", observes.Value); instance.StringProperty = "3"; Assert.AreEqual(2, callCount); Assert.AreEqual("2", value); Assert.AreEqual("3", observes.Value); instance.StringProperty = null; Assert.AreEqual(2, callCount); Assert.AreEqual("2", value); Assert.AreEqual("Fallback", observes.Value); }
public void PropertyObserver_Getter_Fallback_Observes_instance1_IntProperty() { var instance1 = new NotifyPropertyChangedClass1 { Class2 = null }; var instance2 = new NotifyPropertyChangedClass1() { Class2 = null }; var callCount = 0; using var observes = PropertyObserver.Observes( instance1, instance2, (i1, i2) => i1.Class2.IntProperty, () => callCount++, 99); Assert.AreEqual(0, callCount); Assert.AreEqual(99, observes.Value); instance1.Class2 = new NotifyPropertyChangedClass2 { IntProperty = 1 }; Assert.AreEqual(0, callCount); Assert.AreEqual(1, observes.Value); observes.Activate(); Assert.AreEqual(1, callCount); Assert.AreEqual(1, observes.Value); instance1.Class2.IntProperty = 2; Assert.AreEqual(2, callCount); Assert.AreEqual(2, observes.Value); observes.Deactivate(); Assert.AreEqual(2, callCount); Assert.AreEqual(2, observes.Value); instance1.Class2.IntProperty = 3; Assert.AreEqual(2, callCount); Assert.AreEqual(3, observes.Value); instance1.Class2 = null; Assert.AreEqual(2, callCount); Assert.AreEqual(99, observes.Value); }
/// <summary> /// Adds the expression. /// </summary> /// <typeparam name="TOwner">The type of the owner.</typeparam> /// <param name="owner">The owner.</param> /// <param name="propertyExpression">The property expression.</param> /// <exception cref="ArgumentException">propertyExpression</exception> public void AddExpression <TOwner>(TOwner owner, Expression <Func <TOwner, T> > propertyExpression) where TOwner : INotifyPropertyChanged { if (this.observedPropertiesExpressions.TryGetValue( owner.GetHashCode() + propertyExpression.ToString(), out _)) { throw new ArgumentException( $"{propertyExpression} is already being observed.", nameof(propertyExpression)); } var observer = PropertyObserver.Observes(owner, propertyExpression, () => this.Update.Raise()); this.observedPropertiesExpressions.Add(owner.GetHashCode() + propertyExpression.ToString(), observer); observer.Subscribe(); }
public void PropertyObserver_Getter_Fallback_Observes_instance1_StringProperty() { var instance1 = new NotifyPropertyChangedClass1 { StringProperty = null }; var instance2 = new NotifyPropertyChangedClass1(); var callCount = 0; using var observes = PropertyObserver.Observes( instance1, instance2, (i1, i2) => i1.StringProperty, () => callCount++, "Fallback"); Assert.AreEqual(0, callCount); Assert.AreEqual("Fallback", 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("Fallback", observes.Value); }
public void PropertyObserver_Observes_instance_StringProperty_AutoActivateFalse() { var instance = new NotifyPropertyChangedClass1(); var callCount = 0; using var observes = PropertyObserver.Observes(() => instance.StringProperty, false, () => callCount++); Assert.AreEqual(0, callCount); instance.StringProperty = "1"; Assert.AreEqual(0, callCount); observes.Activate(); Assert.AreEqual(1, callCount); instance.StringProperty = "2"; Assert.AreEqual(2, callCount); observes.Deactivate(); Assert.AreEqual(2, callCount); instance.StringProperty = "3"; Assert.AreEqual(2, callCount); }
public void NotifyPropertyChanged_OwnerAndExpressions_ComplexType_Test() { var canExecuteChangedRaiseCount = 0; var notifyPropertyChangedTestObject = new NotifyPropertyChangedTestObject { ComplexProperty = new ComplexType { IntProperty = 1, InnerComplexProperty = new ComplexType { IntProperty = 1, InnerComplexProperty = new ComplexType { IntProperty = 1 } } } }; using var observer1 = PropertyObserver.Observes( notifyPropertyChangedTestObject, o => o.ComplexProperty.IntProperty, () => canExecuteChangedRaiseCount++); using var observer2 = PropertyObserver.Observes( notifyPropertyChangedTestObject, o => o.ComplexProperty.InnerComplexProperty.IntProperty, () => canExecuteChangedRaiseCount++); using var observer3 = PropertyObserver.Observes( notifyPropertyChangedTestObject, o => o.ComplexProperty.InnerComplexProperty.InnerComplexProperty.IntProperty, () => canExecuteChangedRaiseCount++); Assert.AreEqual(0, canExecuteChangedRaiseCount); notifyPropertyChangedTestObject.ComplexProperty.IntProperty = 2; Assert.AreEqual(1, canExecuteChangedRaiseCount); notifyPropertyChangedTestObject.ComplexProperty.InnerComplexProperty.IntProperty = 2; Assert.AreEqual(2, canExecuteChangedRaiseCount); notifyPropertyChangedTestObject.ComplexProperty.InnerComplexProperty.InnerComplexProperty.IntProperty = 2; Assert.AreEqual(3, canExecuteChangedRaiseCount); var innerInnerComplexProp = notifyPropertyChangedTestObject.ComplexProperty.InnerComplexProperty .InnerComplexProperty; var innerComplexProp = notifyPropertyChangedTestObject.ComplexProperty.InnerComplexProperty; var complexProp = notifyPropertyChangedTestObject.ComplexProperty; Assert.AreEqual(1, innerInnerComplexProp.GetPropertyChangedSubscribedLength()); Assert.AreEqual(2, innerComplexProp.GetPropertyChangedSubscribedLength()); Assert.AreEqual(3, complexProp.GetPropertyChangedSubscribedLength()); notifyPropertyChangedTestObject.ComplexProperty = new ComplexType { InnerComplexProperty = new ComplexType { InnerComplexProperty = new ComplexType() } }; Assert.AreEqual(6, canExecuteChangedRaiseCount); Assert.AreEqual(0, innerInnerComplexProp.GetPropertyChangedSubscribedLength()); Assert.AreEqual(0, innerComplexProp.GetPropertyChangedSubscribedLength()); Assert.AreEqual(0, complexProp.GetPropertyChangedSubscribedLength()); innerInnerComplexProp = notifyPropertyChangedTestObject.ComplexProperty.InnerComplexProperty .InnerComplexProperty; innerComplexProp = notifyPropertyChangedTestObject.ComplexProperty.InnerComplexProperty; complexProp = notifyPropertyChangedTestObject.ComplexProperty; Assert.AreEqual(1, innerInnerComplexProp.GetPropertyChangedSubscribedLength()); Assert.AreEqual(2, innerComplexProp.GetPropertyChangedSubscribedLength()); Assert.AreEqual(3, complexProp.GetPropertyChangedSubscribedLength()); notifyPropertyChangedTestObject.ComplexProperty = null; Assert.AreEqual(9, canExecuteChangedRaiseCount); Assert.AreEqual(0, innerInnerComplexProp.GetPropertyChangedSubscribedLength()); Assert.AreEqual(0, innerComplexProp.GetPropertyChangedSubscribedLength()); Assert.AreEqual(0, complexProp.GetPropertyChangedSubscribedLength()); }
/// <summary> /// Observes the given Property and Raises CanExecuteChanged when it changes /// </summary> public static RelayCommand Observes <T>(this RelayCommand command, Expression <Func <T> > propertyExpression) { PropertyObserver.Observes(propertyExpression, command.RaiseCanExecuteChanged); return(command); }
/// <summary> /// Observes the given Property and refilteres to collection when it changes /// </summary> public static FilteredObservableCollection <TCommand> Observes <TCommand, TProp>(this FilteredObservableCollection <TCommand> command, Expression <Func <TProp> > propertyExpression) { PropertyObserver.Observes(propertyExpression, command.FilterChanged); return(command); }