From bb92a6ff0c61b3a33a25f6c571f8cc5a8835715c Mon Sep 17 00:00:00 2001 From: Felix Date: Tue, 1 Sep 2020 11:12:55 +0200 Subject: [PATCH 1/2] Fix crash issues. --- dev/ColorPicker/ColorPicker.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/dev/ColorPicker/ColorPicker.cpp b/dev/ColorPicker/ColorPicker.cpp index 4183522c7f..c6f33b6f34 100644 --- a/dev/ColorPicker/ColorPicker.cpp +++ b/dev/ColorPicker/ColorPicker.cpp @@ -923,11 +923,10 @@ void ColorPicker::OnAlphaTextChanging(winrt::TextBox const& /*sender*/, winrt::T // where it was before. const int cursorPosition = alphaTextBox.SelectionStart() + alphaTextBox.SelectionLength(); - const wchar_t* s = alphaTextBox.Text().begin() + alphaTextBox.Text().size() - 1; - - if (*s != '%') + const auto text = alphaTextBox.Text(); + if (text.empty() || text.begin()[text.size() - 1] != '%') { - alphaTextBox.Text(static_cast(alphaTextBox.Text()) + wstring(L"%")); + alphaTextBox.Text(static_cast(text) + wstring(L"%")); alphaTextBox.SelectionStart(cursorPosition); } @@ -961,9 +960,10 @@ void ColorPicker::OnHexTextChanging(winrt::TextBox const& /*sender*/, winrt::Tex // If the user hasn't entered a #, we'll do that for them, keeping the cursor // where it was before. - if (hexTextBox.Text().begin()[0] != '#') + const auto text = hexTextBox.Text(); + if (text.empty() || text.begin()[0] != '#') { - hexTextBox.Text(wstring(L"#") + static_cast(hexTextBox.Text())); + hexTextBox.Text(wstring(L"#") + static_cast(text)); hexTextBox.SelectionStart(hexTextBox.Text().size()); } From 1687ae8419112925571e84e158fe208810162e41 Mon Sep 17 00:00:00 2001 From: Felix Date: Tue, 1 Sep 2020 11:13:58 +0200 Subject: [PATCH 2/2] Added API tests. --- dev/ColorPicker/APITests/ColorPickerTests.cs | 52 ++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/dev/ColorPicker/APITests/ColorPickerTests.cs b/dev/ColorPicker/APITests/ColorPickerTests.cs index d4a9e93cb0..6ba36fff3c 100644 --- a/dev/ColorPicker/APITests/ColorPickerTests.cs +++ b/dev/ColorPicker/APITests/ColorPickerTests.cs @@ -253,6 +253,58 @@ public void ValidateFractionalWidthDoesNotCrash() SetAsRootAndWaitForColorSpectrumFill(colorSpectrum); } + [TestMethod] + public void VerifyClearingHexInputFieldDoesNotCrash() + { + ColorPicker colorPicker = null; + + RunOnUIThread.Execute(() => + { + colorPicker = new ColorPicker(); + colorPicker.IsHexInputVisible = true; + + Content = colorPicker; + }); + + IdleSynchronizer.Wait(); + + RunOnUIThread.Execute(() => + { + var hexTextBox = VisualTreeUtils.FindVisualChildByName(colorPicker, "HexTextBox") as TextBox; + + Verify.IsGreaterThan(hexTextBox.Text.Length, 0, "Hex TextBox should have not been empty."); + + // Clearing the hex input field should not crash the app. + hexTextBox.Text = ""; + }); + } + + [TestMethod] + public void VerifyClearingAlphaChannelInputFieldDoesNotCrash() + { + ColorPicker colorPicker = null; + + RunOnUIThread.Execute(() => + { + colorPicker = new ColorPicker(); + colorPicker.IsAlphaEnabled = true; + + Content = colorPicker; + }); + + IdleSynchronizer.Wait(); + + RunOnUIThread.Execute(() => + { + var alphaChannelTextBox = VisualTreeUtils.FindVisualChildByName(colorPicker, "AlphaTextBox") as TextBox; + + Verify.IsGreaterThan(alphaChannelTextBox.Text.Length, 0, "Alpha channel TextBox should have not been empty."); + + // Clearing the alpha channel input field should not crash the app. + alphaChannelTextBox.Text = ""; + }); + } + // XamlControlsXamlMetaDataProvider does not exist in the OS repo, // so we can't execute this test as authored there. [TestMethod]