From 0ee636003b6d1083ea4caeed85ef3efbc815ed06 Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Thu, 6 Apr 2017 23:13:49 +0200 Subject: [PATCH] Fix 54334 (#855) * [C] unset the FromStyle flag on manual setting * [C] Do not reset overriden values to default * complete the fix --- Xamarin.Forms.Core.UnitTests/StyleTests.cs | 58 ++++++++++++- Xamarin.Forms.Core/BindableObject.cs | 5 +- Xamarin.Forms.Core/Setter.cs | 4 +- .../Issues/Bz28719.xaml.cs | 8 +- Xamarin.Forms.Xaml.UnitTests/Issues/Bz41048.xaml | 26 ++++++ .../Issues/Bz41048.xaml.cs | 49 +++++++++++ Xamarin.Forms.Xaml.UnitTests/Issues/Bz54334.xaml | 8 ++ .../Issues/Bz54334.xaml.cs | 96 ++++++++++++++++++++++ .../Xamarin.Forms.Xaml.UnitTests.csproj | 12 +++ 9 files changed, 257 insertions(+), 9 deletions(-) create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Bz41048.xaml create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Bz41048.xaml.cs create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Bz54334.xaml create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Bz54334.xaml.cs diff --git a/Xamarin.Forms.Core.UnitTests/StyleTests.cs b/Xamarin.Forms.Core.UnitTests/StyleTests.cs index d6927e5..8caac97 100644 --- a/Xamarin.Forms.Core.UnitTests/StyleTests.cs +++ b/Xamarin.Forms.Core.UnitTests/StyleTests.cs @@ -8,12 +8,19 @@ namespace Xamarin.Forms.Core.UnitTests public class StyleTests : BaseTestFixture { [SetUp] - public void Setup () + public override void Setup () { base.Setup (); Device.PlatformServices = new MockPlatformServices (); } + [TearDown] + public override void TearDown() + { + base.TearDown(); + Application.Current = null; + } + [Test] public void ApplyUnapplyStyle () { @@ -703,5 +710,54 @@ namespace Xamarin.Forms.Core.UnitTests Assert.AreEqual (Color.Pink, button.TextColor); Assert.AreEqual (20d, button.FontSize); } + + [Test] + public void ReplacingResourcesDoesNotOverrideManuallySetProperties() + { + var label0 = new Label { + TextColor = Color.Pink + }; + var label1 = new Label(); + + Assume.That(label0.TextColor, Is.EqualTo(Color.Pink)); + Assume.That(label1.TextColor, Is.EqualTo(Color.Default)); + + var rd0 = new ResourceDictionary { + new Style (typeof(Label)) { + Setters = { + new Setter {Property = Label.TextColorProperty, Value = Color.Olive} + } + } + }; + var rd1 = new ResourceDictionary { + new Style (typeof(Label)) { + Setters = { + new Setter {Property = Label.TextColorProperty, Value = Color.Lavender} + } + } + }; + + var mockApp = new MockApplication(); + Application.Current = mockApp; + mockApp.Resources = rd0; + + var layout = new StackLayout { + Children = { + label0, + label1, + } + }; + + mockApp.MainPage = new ContentPage { Content = layout}; + //Assert.That(label0.TextColor, Is.EqualTo(Color.Pink)); + //Assert.That(label1.TextColor, Is.EqualTo(Color.Default)); + + Assert.That(label0.TextColor, Is.EqualTo(Color.Pink)); + Assert.That(label1.TextColor, Is.EqualTo(Color.Olive)); + + mockApp.Resources = rd1; + Assert.That(label0.TextColor, Is.EqualTo(Color.Pink)); + Assert.That(label1.TextColor, Is.EqualTo(Color.Lavender)); + } } } \ No newline at end of file diff --git a/Xamarin.Forms.Core/BindableObject.cs b/Xamarin.Forms.Core/BindableObject.cs index e87282d..35e3648 100644 --- a/Xamarin.Forms.Core/BindableObject.cs +++ b/Xamarin.Forms.Core/BindableObject.cs @@ -363,9 +363,10 @@ namespace Xamarin.Forms value = property.CoerceValue(this, value); BindablePropertyContext context = GetOrCreateContext(property); - if (manuallySet) + if (manuallySet) { context.Attributes |= BindableContextAttributes.IsManuallySet; - else + context.Attributes &= ~BindableContextAttributes.IsSetFromStyle; + } else context.Attributes &= ~BindableContextAttributes.IsManuallySet; if (fromStyle) diff --git a/Xamarin.Forms.Core/Setter.cs b/Xamarin.Forms.Core/Setter.cs index ea58d4f..07bef63 100644 --- a/Xamarin.Forms.Core/Setter.cs +++ b/Xamarin.Forms.Core/Setter.cs @@ -63,12 +63,12 @@ namespace Xamarin.Forms internal void UnApply(BindableObject target, bool fromStyle = false) { if (target == null) - throw new ArgumentNullException("target"); + throw new ArgumentNullException(nameof(target)); if (Property == null) return; object actual = target.GetValue(Property); - if (!fromStyle && !Equals(actual, Value)) + if (!Equals(actual, Value) && !(Value is Binding) && !(Value is DynamicResource)) { //Do not reset default value if the value has been changed _originalValues.Remove(target); diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz28719.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz28719.xaml.cs index bd0fe48..6353b53 100644 --- a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz28719.xaml.cs +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz28719.xaml.cs @@ -44,10 +44,10 @@ namespace Xamarin.Forms.Xaml.UnitTests Assert.NotNull (image0); cell0.BindingContext = new {IsSelected = true}; - Assert.AreEqual ("Remove.png", (image0.Source as FileImageSource).File); + Assert.AreEqual ("Remove.png", (image0.Source as FileImageSource)?.File); cell0.BindingContext = new {IsSelected = false}; - Assert.AreEqual ("Add.png", (image0.Source as FileImageSource).File); + Assert.AreEqual ("Add.png", (image0.Source as FileImageSource)?.File); var cell1 = template.CreateContent () as ViewCell; Assert.NotNull (cell1); @@ -55,10 +55,10 @@ namespace Xamarin.Forms.Xaml.UnitTests Assert.NotNull (image1); cell1.BindingContext = new {IsSelected = true}; - Assert.AreEqual ("Remove.png", (image1.Source as FileImageSource).File); + Assert.AreEqual ("Remove.png", (image1.Source as FileImageSource)?.File); cell1.BindingContext = new {IsSelected = false}; - Assert.AreEqual ("Add.png", (image1.Source as FileImageSource).File); + Assert.AreEqual ("Add.png", (image1.Source as FileImageSource)?.File); } } } diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz41048.xaml b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz41048.xaml new file mode 100644 index 0000000..81cf4ae --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz41048.xaml @@ -0,0 +1,26 @@ + + + + + + + + + + + +