Browse Source

Fix attached PerspexProperties.

Make sure notifications and validation are shared.
pull/298/head
Steven Kirk 11 years ago
parent
commit
6834f12170
  1. 39
      src/Perspex.Base/PerspexProperty.cs
  2. 20
      tests/Perspex.Base.UnitTests/PerspexPropertyTests.cs

39
src/Perspex.Base/PerspexProperty.cs

@ -36,23 +36,22 @@ namespace Perspex
/// <summary> /// <summary>
/// The overridden default values for the property, by type. /// The overridden default values for the property, by type.
/// </summary> /// </summary>
private readonly Dictionary<Type, object> _defaultValues = new Dictionary<Type, object>(); private readonly Dictionary<Type, object> _defaultValues;
/// <summary> /// <summary>
/// Observable fired when this property changes on any <see cref="PerspexObject"/>. /// Observable fired when this property changes on any <see cref="PerspexObject"/>.
/// </summary> /// </summary>
private readonly Subject<PerspexPropertyChangedEventArgs> _initialized = new Subject<PerspexPropertyChangedEventArgs>(); private readonly Subject<PerspexPropertyChangedEventArgs> _initialized;
/// <summary> /// <summary>
/// Observable fired when this property changes on any <see cref="PerspexObject"/>. /// Observable fired when this property changes on any <see cref="PerspexObject"/>.
/// </summary> /// </summary>
private readonly Subject<PerspexPropertyChangedEventArgs> _changed = new Subject<PerspexPropertyChangedEventArgs>(); private readonly Subject<PerspexPropertyChangedEventArgs> _changed;
/// <summary> /// <summary>
/// The validation functions for the property, by type. /// The validation functions for the property, by type.
/// </summary> /// </summary>
private readonly Dictionary<Type, Func<PerspexObject, object, object>> _validation = private readonly Dictionary<Type, Func<PerspexObject, object, object>> _validation;
new Dictionary<Type, Func<PerspexObject, object, object>>();
/// <summary> /// <summary>
/// Gets the ID of the property. /// Gets the ID of the property.
@ -95,6 +94,11 @@ namespace Perspex
throw new ArgumentException("'name' may not contain periods."); throw new ArgumentException("'name' may not contain periods.");
} }
_defaultValues = new Dictionary<Type, object>();
_initialized = new Subject<PerspexPropertyChangedEventArgs>();
_changed = new Subject<PerspexPropertyChangedEventArgs>();
_validation = new Dictionary<Type, Func<PerspexObject, object, object>>();
Name = name; Name = name;
PropertyType = valueType; PropertyType = valueType;
OwnerType = ownerType; OwnerType = ownerType;
@ -136,6 +140,11 @@ namespace Perspex
throw new ArgumentException("'name' may not contain periods."); throw new ArgumentException("'name' may not contain periods.");
} }
_defaultValues = new Dictionary<Type, object>();
_initialized = new Subject<PerspexPropertyChangedEventArgs>();
_changed = new Subject<PerspexPropertyChangedEventArgs>();
_validation = new Dictionary<Type, Func<PerspexObject, object, object>>();
Name = name; Name = name;
PropertyType = valueType; PropertyType = valueType;
OwnerType = ownerType; OwnerType = ownerType;
@ -161,22 +170,15 @@ namespace Perspex
"This method cannot be called on direct PerspexProperties."); "This method cannot be called on direct PerspexProperties.");
} }
//Name = name; _defaultValues = source._defaultValues;
//PropertyType = valueType; _initialized = source._initialized;
//OwnerType = ownerType; _changed = source._changed;
//_defaultValues.Add(ownerType, defaultValue); _validation = source._validation;
//Inherits = inherits;
//DefaultBindingMode = defaultBindingMode;
//IsAttached = isAttached;
//Notifying = notifying;
//_id = s_nextId++;
Name = source.Name; Name = source.Name;
PropertyType = source.PropertyType; PropertyType = source.PropertyType;
OwnerType = ownerType; OwnerType = ownerType;
_defaultValue = source._defaultValue; _defaultValue = source._defaultValue;
_defaultValues = source._defaultValues;
Inherits = source.Inherits; Inherits = source.Inherits;
DefaultBindingMode = source.DefaultBindingMode; DefaultBindingMode = source.DefaultBindingMode;
IsAttached = false; IsAttached = false;
@ -208,6 +210,11 @@ namespace Perspex
"This method can only be called on direct PerspexProperties."); "This method can only be called on direct PerspexProperties.");
} }
_defaultValues = source._defaultValues;
_initialized = source._initialized;
_changed = source._changed;
_validation = source._validation;
Name = source.Name; Name = source.Name;
PropertyType = source.PropertyType; PropertyType = source.PropertyType;
OwnerType = ownerType; OwnerType = ownerType;

20
tests/Perspex.Base.UnitTests/PerspexPropertyTests.cs

@ -170,6 +170,16 @@ namespace Perspex.Base.UnitTests
Assert.Equal(typeof(Class3), p2.OwnerType); Assert.Equal(typeof(Class3), p2.OwnerType);
} }
[Fact]
public void AddOwnered_Properties_Should_Share_Observables()
{
var p1 = new PerspexProperty<string>("p1", typeof(Class1));
var p2 = p1.AddOwner<Class3>();
Assert.Same(p1.Changed, p2.Changed);
Assert.Same(p1.Initialized, p2.Initialized);
}
[Fact] [Fact]
public void AddOwnered_Direct_Property_Should_Equal_Original() public void AddOwnered_Direct_Property_Should_Equal_Original()
{ {
@ -190,6 +200,16 @@ namespace Perspex.Base.UnitTests
Assert.Equal(typeof(Class3), p2.OwnerType); Assert.Equal(typeof(Class3), p2.OwnerType);
} }
[Fact]
public void AddOwnered_Direct_Properties_Should_Share_Observables()
{
var p1 = new PerspexProperty<string>("d1", typeof(Class1), o => null, (o, v) => { });
var p2 = p1.AddOwner<Class3>(o => null, (o, v) => { });
Assert.Same(p1.Changed, p2.Changed);
Assert.Same(p1.Initialized, p2.Initialized);
}
[Fact] [Fact]
public void AddOwner_With_Getter_And_Setter_On_Standard_Property_Should_Throw() public void AddOwner_With_Getter_And_Setter_On_Standard_Property_Should_Throw()
{ {

Loading…
Cancel
Save