Skip to content

Commit

Permalink
Merge pull request #17971 from Youssef1313/getdefaultvalue
Browse files Browse the repository at this point in the history
refactor: DP system simplifications
  • Loading branch information
Youssef1313 authored Aug 19, 2024
2 parents 9735ac1 + e2b8083 commit 3295c52
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 459 deletions.
18 changes: 0 additions & 18 deletions src/Uno.UI.Tests/BinderTests/Given_Binder.LocalCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,6 @@ public void When_Coerce_And_Coerce_False()

Assert.AreEqual(true, SUT.IsEnabled);
}

[TestMethod]
public void When_Coerce_And_MetadataOverride()
{
var SUT = new BinderLocalCache_Data_IsEnabled_Inherited();
Assert.AreEqual(false, SUT.IsEnabled);

SUT.SetValue(BinderLocalCache_Data_IsEnabled.IsEnabledProperty, true);
Assert.AreEqual(true, SUT.IsEnabled);
}
}

public partial class BinderLocalCache_Data : DependencyObject
Expand Down Expand Up @@ -158,12 +148,4 @@ public void SuppressIsEnabled(bool suppress)
this.CoerceValue(IsEnabledProperty);
}
}

public partial class BinderLocalCache_Data_IsEnabled_Inherited : BinderLocalCache_Data_IsEnabled
{
static BinderLocalCache_Data_IsEnabled_Inherited()
{
IsEnabledProperty.OverrideMetadata(typeof(BinderLocalCache_Data_IsEnabled_Inherited), new FrameworkPropertyMetadata(defaultValue: false));
}
}
}
187 changes: 0 additions & 187 deletions src/Uno.UI.Tests/DependencyProperty/Given_DependencyProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -980,41 +980,6 @@ private object Custom(object dependencyObject, object baseValue, DependencyPrope

#endregion

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void When_OverrideMetadata_With_Metadata_Is_Not_Derived_From_BaseMetadata_Then_Fail()
{
var testProperty = DependencyProperty.Register(
nameof(When_OverrideMetadata_With_Metadata_Is_Not_Derived_From_BaseMetadata_Then_Fail),
typeof(string),
typeof(MockDependencyObject),
new FrameworkPropertyMetadata(null)
);

testProperty.OverrideMetadata(typeof(MockDependencyObject2), new PropertyMetadata(null));
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void When_OverrideMetadata_With_ForType_Is_OwnerType_Then_Fail()
{
var testProperty = DependencyProperty.Register(
nameof(When_OverrideMetadata_With_ForType_Is_OwnerType_Then_Fail),
typeof(string),
typeof(MockDependencyObject),
null
);

testProperty.OverrideMetadata(typeof(MockDependencyObject), new PropertyMetadata("test"));
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void When_OverrideMetadata_With_Same_ForType_Twice_Then_Fail()
{
MyDependencyObject1.MyPropertyProperty.OverrideMetadata(typeof(MyDependencyObject2), new PropertyMetadata("test"));
}

[TestMethod]
public void When_PropertyMetadata_Is_Null()
{
Expand Down Expand Up @@ -1049,54 +1014,6 @@ public void When_SetValue_With_Coercion_Precedence_Then_Fail()
SUT.SetValue(testProperty, "test", DependencyPropertyValuePrecedences.Coercion);
}

[TestMethod]
public void When_OverrideMetadata_DefaultValue()
{
var SUT1 = new MyDependencyObject1();
var SUT2 = new MyDependencyObject2();
var SUT3 = new MyDependencyObject3();

Assert.AreEqual("default1", MyDependencyObject1.MyPropertyProperty.GetMetadata(typeof(MyDependencyObject1)).DefaultValue);
Assert.AreEqual("default2", MyDependencyObject1.MyPropertyProperty.GetMetadata(typeof(MyDependencyObject2)).DefaultValue);
Assert.AreEqual("default3", MyDependencyObject1.MyPropertyProperty.GetMetadata(typeof(MyDependencyObject3)).DefaultValue);

Assert.AreEqual("default1", SUT1.GetValue(MyDependencyObject1.MyPropertyProperty));
Assert.AreEqual("default2", SUT2.GetValue(MyDependencyObject1.MyPropertyProperty));
Assert.AreEqual("default3", SUT3.GetValue(MyDependencyObject1.MyPropertyProperty));
}

[TestMethod]
public void When_OverrideMetadata_CoerceValueCallback()
{
var SUT1 = new MyDependencyObject1();
var SUT2 = new MyDependencyObject2();
var SUT3 = new MyDependencyObject3();

// +1 CoerceValueCallback (1)
SUT1.SetValue(MyDependencyObject1.MyPropertyProperty, "A");
SUT2.SetValue(MyDependencyObject1.MyPropertyProperty, "A");
SUT3.SetValue(MyDependencyObject1.MyPropertyProperty, "A");

// +1 CoerceValueCallback (2)
SUT1.SetValue(MyDependencyObject1.MyPropertyProperty, "A");
SUT2.SetValue(MyDependencyObject1.MyPropertyProperty, "A");
SUT3.SetValue(MyDependencyObject1.MyPropertyProperty, "A");

// +1 CoerceValueCallback (3)
SUT1.SetValue(MyDependencyObject1.MyPropertyProperty, "B");
SUT2.SetValue(MyDependencyObject1.MyPropertyProperty, "B");
SUT3.SetValue(MyDependencyObject1.MyPropertyProperty, "B");

// +1 CoerceValueCallback (4)
SUT1.CoerceValue(MyDependencyObject1.MyPropertyProperty);
SUT2.CoerceValue(MyDependencyObject1.MyPropertyProperty);
SUT3.CoerceValue(MyDependencyObject1.MyPropertyProperty);

Assert.AreEqual(4, SUT1.CoerceValueCallbackCount);
Assert.AreEqual(4, SUT2.CoerceValueCallbackCount);
Assert.AreEqual(4, SUT3.CoerceValueCallbackCount);
}

[TestMethod]
public void When_SetValue_Inheritance_And_CoerceValue_Then_GetValue_Local_Is_UnsetValue()
{
Expand All @@ -1108,77 +1025,6 @@ public void When_SetValue_Inheritance_And_CoerceValue_Then_GetValue_Local_Is_Uns
Assert.AreEqual(DependencyProperty.UnsetValue, SUT.GetValue(MyDependencyObject1.MyPropertyProperty, DependencyPropertyValuePrecedences.Local));
}

[TestMethod]
public void When_OverrideMetadata_PropertyChangedCallback()
{
var SUT1 = new MyDependencyObject1();
var SUT2 = new MyDependencyObject2();
var SUT3 = new MyDependencyObject3();

SUT1.SetValue(MyDependencyObject1.MyPropertyProperty, "A"); // +1 PropertyChangedCallback (1)
SUT2.SetValue(MyDependencyObject1.MyPropertyProperty, "A"); // +2 PropertyChangedCallback (2)
SUT3.SetValue(MyDependencyObject1.MyPropertyProperty, "A"); // +3 PropertyChangedCallback (3)

SUT1.SetValue(MyDependencyObject1.MyPropertyProperty, "A"); // +0 PropertyChangedCallback (1)
SUT2.SetValue(MyDependencyObject1.MyPropertyProperty, "A"); // +0 PropertyChangedCallback (2)
SUT3.SetValue(MyDependencyObject1.MyPropertyProperty, "A"); // +0 PropertyChangedCallback (3)

SUT1.SetValue(MyDependencyObject1.MyPropertyProperty, "B"); // +1 PropertyChangedCallback (2)
SUT2.SetValue(MyDependencyObject1.MyPropertyProperty, "B"); // +2 PropertyChangedCallback (4)
SUT3.SetValue(MyDependencyObject1.MyPropertyProperty, "B"); // +3 PropertyChangedCallback (6)

SUT1.CoerceValue(MyDependencyObject1.MyPropertyProperty); // +0 PropertyChangedCallback (2)
SUT1.CoerceValue(MyDependencyObject1.MyPropertyProperty); // +0 PropertyChangedCallback (4)
SUT1.CoerceValue(MyDependencyObject1.MyPropertyProperty); // +0 PropertyChangedCallback (6)

var propertyChangedCallbacks1 = new[]
{
"changed1: coercion1: A",
"changed1: coercion1: B",
};

var propertyChangedCallbacks2 = new[]
{
"changed1: coercion2: A",
"changed2: coercion2: A",
"changed1: coercion2: B",
"changed2: coercion2: B",
};

var propertyChangedCallbacks3 = new[]
{
"changed1: coercion3: A",
"changed2: coercion3: A",
"changed3: coercion3: A",
"changed1: coercion3: B",
"changed2: coercion3: B",
"changed3: coercion3: B",
};

Assert.IsTrue(SUT1.PropertyChangedCallbacks.SequenceEqual(propertyChangedCallbacks1));
Assert.IsTrue(SUT2.PropertyChangedCallbacks.SequenceEqual(propertyChangedCallbacks2));
Assert.IsTrue(SUT3.PropertyChangedCallbacks.SequenceEqual(propertyChangedCallbacks3));
}

[TestMethod]
public void When_OverrideMetadata_FrameworkPropertyMetadata_Options()
{
var SUT1 = new MyDependencyObject1();
var SUT2 = new MyDependencyObject2();
var SUT3 = new MyDependencyObject3();

var metadata1 = MyDependencyObject1.MyPropertyProperty.GetMetadata(typeof(MyDependencyObject1));
var metadata2 = MyDependencyObject1.MyPropertyProperty.GetMetadata(typeof(MyDependencyObject2));
var metadata3 = MyDependencyObject1.MyPropertyProperty.GetMetadata(typeof(MyDependencyObject3));

Assert.IsNotInstanceOfType(metadata1, typeof(FrameworkPropertyMetadata));
Assert.IsInstanceOfType(metadata2, typeof(FrameworkPropertyMetadata));
Assert.IsInstanceOfType(metadata3, typeof(FrameworkPropertyMetadata));

Assert.AreEqual(FrameworkPropertyMetadataOptions.Default | FrameworkPropertyMetadataOptions.Inherits, (metadata2 as FrameworkPropertyMetadata).Options);
Assert.AreEqual(FrameworkPropertyMetadataOptions.Default | FrameworkPropertyMetadataOptions.Inherits, (metadata3 as FrameworkPropertyMetadata).Options);
}

[TestMethod]
public void When_SetValue_TypeA_On_PropertyType_Of_TypeB_Then_GetValue_Of_TypeA()
{
Expand Down Expand Up @@ -2037,39 +1883,6 @@ public MyDependencyObject1() { }
public int CoerceValueCallbackCount { get; set; } = 0;
}

partial class MyDependencyObject2 : MyDependencyObject1
{
static MyDependencyObject2()
{
var metadata = new FrameworkPropertyMetadata(
"default2",
FrameworkPropertyMetadataOptions.Inherits,
(s, e) => { (s as MyDependencyObject1).PropertyChangedCallbacks.Add("changed2: " + e.NewValue); },
(s, baseValue, _) => { (s as MyDependencyObject1).CoerceValueCallbackCount++; return "coercion2: " + baseValue; }
);

MyPropertyProperty.OverrideMetadata(typeof(MyDependencyObject2), metadata);
}

public MyDependencyObject2() { }
}

partial class MyDependencyObject3 : MyDependencyObject2
{
static MyDependencyObject3()
{
var metadata = new FrameworkPropertyMetadata(
"default3",
(s, e) => { (s as MyDependencyObject1).PropertyChangedCallbacks.Add("changed3: " + e.NewValue); },
(s, baseValue, _) => { (s as MyDependencyObject1).CoerceValueCallbackCount++; return "coercion3: " + baseValue; }
);

MyPropertyProperty.OverrideMetadata(typeof(MyDependencyObject3), metadata);
}

public MyDependencyObject3() { }
}

partial class NullablePropertyOwner : FrameworkElement
{

Expand Down
3 changes: 2 additions & 1 deletion src/Uno.UI/DataBinding/BindingExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ private void ApplyFallbackValue(bool useTypeDefaultValue = true)
}
else if (useTypeDefaultValue && TargetPropertyDetails != null)
{
SetTargetValue(TargetPropertyDetails.Property.GetMetadata(_view.Target?.GetType()).DefaultValue);
var viewTarget = _view.Target;
SetTargetValue(TargetPropertyDetails.Property.GetDefaultValue(viewTarget as UIElement, viewTarget?.GetType()));
}
}

Expand Down
13 changes: 0 additions & 13 deletions src/Uno.UI/UI/Xaml/Controls/Button/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,6 @@ namespace Microsoft.UI.Xaml.Controls
{
public partial class Button : ButtonBase
{
static Button()
{
HorizontalContentAlignmentProperty.OverrideMetadata(
typeof(Button),
new FrameworkPropertyMetadata(HorizontalAlignment.Center)
);

VerticalContentAlignmentProperty.OverrideMetadata(
typeof(Button),
new FrameworkPropertyMetadata(VerticalAlignment.Center)
);
}

/// <summary>
/// Initializes a new instance of the Button class.
/// </summary>
Expand Down
5 changes: 0 additions & 5 deletions src/Uno.UI/UI/Xaml/Controls/ProgressBar/ProgressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ public partial class ProgressBar : RangeBase
private FrameworkElement _determinateRoot;
private FrameworkElement _progressBarIndicator;

static ProgressBar()
{
MaximumProperty.OverrideMetadata(typeof(ProgressBar), new FrameworkPropertyMetadata(100d));
}

public ProgressBar()
{
TemplateSettings = new ProgressBarTemplateSettings();
Expand Down
15 changes: 0 additions & 15 deletions src/Uno.UI/UI/Xaml/Controls/ScrollViewer/ScrollViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,6 @@ public static class ScrollBarsSeparator

internal event SizeChangedEventHandler? ExtentSizeChanged;

static ScrollViewer()
{
#if !IS_UNIT_TESTS
HorizontalContentAlignmentProperty.OverrideMetadata(
typeof(ScrollViewer),
new FrameworkPropertyMetadata(HorizontalAlignment.Stretch)
);

VerticalContentAlignmentProperty.OverrideMetadata(
typeof(ScrollViewer),
new FrameworkPropertyMetadata(VerticalAlignment.Top)
);
#endif
}

public ScrollViewer()
{
DefaultStyleKey = typeof(ScrollViewer);
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ private void InitializeProperties()
OnIsSpellCheckEnabledChanged(IsSpellCheckEnabled);
OnTextAlignmentChanged(TextAlignment);
OnTextWrappingChanged();
OnFocusStateChanged((FocusState)FocusStateProperty.GetMetadata(GetType()).DefaultValue, FocusState, initial: true);
OnFocusStateChanged(FocusState.Unfocused, FocusState, initial: true);
OnTextCharacterCasingChanged(CharacterCasing);
UpdateDescriptionVisibility(true);
var buttonRef = _deleteButton?.GetTarget();
Expand Down
8 changes: 4 additions & 4 deletions src/Uno.UI/UI/Xaml/DependencyObjectStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -648,14 +648,14 @@ private void ApplyCoercion(DependencyObject actualInstanceAlias, DependencyPrope
return;
}

var coerceValueCallback = propertyDetails.Metadata.CoerceValueCallback;
var coerceValueCallback = propertyDetails.Property.Metadata.CoerceValueCallback;
if (coerceValueCallback == null)
{
// No coercion to remove or to apply.
return;
}

var options = (propertyDetails.Metadata as FrameworkPropertyMetadata)?.Options ?? FrameworkPropertyMetadataOptions.Default;
var options = (propertyDetails.Property.Metadata as FrameworkPropertyMetadata)?.Options ?? FrameworkPropertyMetadataOptions.Default;

if (Equals(previousValue, baseValue) && ((options & FrameworkPropertyMetadataOptions.CoerceOnlyWhenChanged) != 0))
{
Expand Down Expand Up @@ -1869,7 +1869,7 @@ private void InvokeCallbacks(
)
{
//var propertyChangedParams = new PropertyChangedParams(property, previousValue, newValue);
var propertyMetadata = propertyDetails.Metadata;
var propertyMetadata = property.Metadata;

// We can reuse the weak reference, otherwise capture the weak reference to this instance.
var instanceRef = _originalObjectRef ?? ThisWeakReference;
Expand Down Expand Up @@ -2038,7 +2038,7 @@ DependencyPropertyDetails propertyDetails
{
if (value != null
&& value != DependencyProperty.UnsetValue
&& ((propertyDetails.Metadata as FrameworkPropertyMetadata)?.Options.HasAutoConvert() ?? false))
&& ((propertyDetails.Property.Metadata as FrameworkPropertyMetadata)?.Options.HasAutoConvert() ?? false))
{
if (value?.GetType() != propertyDetails.Property.Type)
{
Expand Down
Loading

0 comments on commit 3295c52

Please sign in to comment.