Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ColorPicker: Fix app crash when clearing input fields #3224

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions dev/ColorPicker/APITests/ColorPickerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
12 changes: 6 additions & 6 deletions dev/ColorPicker/ColorPicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<wstring>(alphaTextBox.Text()) + wstring(L"%"));
alphaTextBox.Text(static_cast<wstring>(text) + wstring(L"%"));
alphaTextBox.SelectionStart(cursorPosition);
}

Expand Down Expand Up @@ -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<wstring>(hexTextBox.Text()));
hexTextBox.Text(wstring(L"#") + static_cast<wstring>(text));
hexTextBox.SelectionStart(hexTextBox.Text().size());
}

Expand Down