From b4c6fdca99ce1151487506297f6db0e4aa2febb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marvin=20R=C3=BChe?= Date: Tue, 9 Nov 2021 14:42:40 +0100 Subject: [PATCH 1/3] Fix ColorPickerButton SelectedColor bug (fixes #4367) Update internal ColorPicker Color when ColorPickerButton SelectedColor changes --- .../ColorPicker/ColorPickerButton.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.Input/ColorPicker/ColorPickerButton.cs b/Microsoft.Toolkit.Uwp.UI.Controls.Input/ColorPicker/ColorPickerButton.cs index 34c90a5cb90..dc785d1fbba 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.Input/ColorPicker/ColorPickerButton.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.Input/ColorPicker/ColorPickerButton.cs @@ -81,7 +81,7 @@ public Color SelectedColor /// Identifies the dependency property. /// public static readonly DependencyProperty SelectedColorProperty = - DependencyProperty.Register(nameof(SelectedColor), typeof(Color), typeof(ColorPickerButton), new PropertyMetadata(null)); + DependencyProperty.Register(nameof(SelectedColor), typeof(Color), typeof(ColorPickerButton), new PropertyMetadata(null, new PropertyChangedCallback(SelectedColorChanged))); #pragma warning disable SA1306 // Field names should begin with lower-case letter //// Template Parts @@ -142,6 +142,14 @@ protected override void OnApplyTemplate() } } + private static void SelectedColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is ColorPickerButton instance && !(instance.ColorPicker is null)) + { + instance.ColorPicker.Color = instance.SelectedColor; + } + } + private void ColorPicker_ColorChanged(Microsoft.UI.Xaml.Controls.ColorPicker sender, Microsoft.UI.Xaml.Controls.ColorChangedEventArgs args) { SelectedColor = args.NewColor; From 1e6a16af8ec177214415174bd04e47e0c18aa819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marvin=20R=C3=BChe?= Date: Thu, 11 Nov 2021 10:24:25 +0100 Subject: [PATCH 2/3] Add UITest to verify fix --- .../Controls/ColorPickerButtonTest.cs | 85 +++++++++++++++++++ .../Controls/ColorPickerButtonTestPage.xaml | 13 +++ .../ColorPickerButtonTestPage.xaml.cs | 47 ++++++++++ 3 files changed, 145 insertions(+) create mode 100644 UITests/UITests.Tests.Shared/Controls/ColorPickerButtonTest.cs create mode 100644 UITests/UITests.Tests.Shared/Controls/ColorPickerButtonTestPage.xaml create mode 100644 UITests/UITests.Tests.Shared/Controls/ColorPickerButtonTestPage.xaml.cs diff --git a/UITests/UITests.Tests.Shared/Controls/ColorPickerButtonTest.cs b/UITests/UITests.Tests.Shared/Controls/ColorPickerButtonTest.cs new file mode 100644 index 00000000000..c3303edbe74 --- /dev/null +++ b/UITests/UITests.Tests.Shared/Controls/ColorPickerButtonTest.cs @@ -0,0 +1,85 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Windows.Apps.Test.Foundation.Controls; +using Windows.UI.Xaml.Tests.MUXControls.InteractionTests.Common; +using Windows.UI.Xaml.Tests.MUXControls.InteractionTests.Infra; + +#if USING_TAEF +using WEX.Logging.Interop; +using WEX.TestExecution; +using WEX.TestExecution.Markup; +#else +using Microsoft.VisualStudio.TestTools.UnitTesting; +#endif + +namespace UITests.Tests +{ + [TestClass] + public class ColorPickerButtonTest : UITestBase + { + [ClassInitialize] + [TestProperty("RunAs", "User")] + [TestProperty("Classification", "ScenarioTestSuite")] + [TestProperty("Platform", "Any")] + public static void ClassInitialize(TestContext testContext) + { + TestEnvironment.Initialize(testContext, WinUICsUWPSampleApp); + } + + /// + /// This test validates the two way binding of the selected color. It verifies that + /// when the bound property changes this change is properly forwarded to the internal colorpicker. + /// See also issue #4367 + /// + [TestMethod] + [TestPage("ColorPickerButtonTestPage")] + public void TwoWayTestMethod() + { + var colorpicker = new Button(FindElement.ById("TheColorPickerButton")); + + var redButton = new Button(FindElement.ById("SetRedButton")); + + Verify.IsNotNull(colorpicker); + Verify.IsNotNull(colorpicker); + + colorpicker.Click(); + + Wait.ForIdle(); + var colorInput = GetColorPickerInputField(); + + Verify.AreEqual("008000", colorInput.GetText()); + + // close the picker + colorpicker.Click(); + + Wait.ForIdle(); + + redButton.Click(); + + Wait.ForIdle(); + + colorpicker.Click(); + + var colorInput_new = GetColorPickerInputField(); + Verify.AreEqual("FF0000", colorInput_new.GetText()); + } + + private static Edit GetColorPickerInputField() + { + var channelButton = new Button(FindElement.ByName("Channels")); + Verify.IsNotNull(channelButton); + + Wait.ForIdle(); + + channelButton.Click(); + + Wait.ForIdle(); + + var colorInput = new Edit(FindElement.ByName("Hexadecimal Color Input")); + Verify.IsNotNull(colorInput); + return colorInput; + } + } +} \ No newline at end of file diff --git a/UITests/UITests.Tests.Shared/Controls/ColorPickerButtonTestPage.xaml b/UITests/UITests.Tests.Shared/Controls/ColorPickerButtonTestPage.xaml new file mode 100644 index 00000000000..27f563c0bc4 --- /dev/null +++ b/UITests/UITests.Tests.Shared/Controls/ColorPickerButtonTestPage.xaml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/UITests/UITests.Tests.Shared/Controls/ColorPickerButtonTestPage.xaml.cs b/UITests/UITests.Tests.Shared/Controls/ColorPickerButtonTestPage.xaml.cs new file mode 100644 index 00000000000..503105e9603 --- /dev/null +++ b/UITests/UITests.Tests.Shared/Controls/ColorPickerButtonTestPage.xaml.cs @@ -0,0 +1,47 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.ComponentModel; +using Windows.UI; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; + +namespace UITests.App.Pages +{ + /// + /// An empty page that can be used on its own or navigated to within a Frame. + /// + public sealed partial class ColorPickerButtonTestPage : Page, INotifyPropertyChanged + { + private Color _theColor = Colors.Green; + + public Color TheColor + { + get => _theColor; + set + { + if (_theColor != value) + { + _theColor = value; + OnPropertyChanged(nameof(TheColor)); + } + } + } + + public ColorPickerButtonTestPage() + { + DataContext = this; + this.InitializeComponent(); + } + + public event PropertyChangedEventHandler PropertyChanged; + + private void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + + private void Button_Click(object sender, RoutedEventArgs e) + { + TheColor = Colors.Red; + } + } +} \ No newline at end of file From 8fcb64a85c4159bde7a14ab5cd8efb80e548e07b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marvin=20R=C3=BChe?= Date: Fri, 12 Nov 2021 23:12:56 +0100 Subject: [PATCH 3/3] Update UITests/UITests.Tests.Shared/Controls/ColorPickerButtonTest.cs Co-authored-by: XAML-Knight <86266896+XAML-Knight@users.noreply.github.com> --- UITests/UITests.Tests.Shared/Controls/ColorPickerButtonTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UITests/UITests.Tests.Shared/Controls/ColorPickerButtonTest.cs b/UITests/UITests.Tests.Shared/Controls/ColorPickerButtonTest.cs index c3303edbe74..f1672919788 100644 --- a/UITests/UITests.Tests.Shared/Controls/ColorPickerButtonTest.cs +++ b/UITests/UITests.Tests.Shared/Controls/ColorPickerButtonTest.cs @@ -42,7 +42,7 @@ public void TwoWayTestMethod() var redButton = new Button(FindElement.ById("SetRedButton")); Verify.IsNotNull(colorpicker); - Verify.IsNotNull(colorpicker); + Verify.IsNotNull(redButton); colorpicker.Click();