Skip to content

Commit

Permalink
Fix 3780 - NumericUpDown/DecimalUpDown display does not update with v…
Browse files Browse the repository at this point in the history
…alues ​​outside the range (MaterialDesignInXAML#3781)

* possibily fixed 3780, adjusted the demo app to play around and reproduce the bug

* cleanup demo

* Adding UI tests

Removed the direct Coerce calls as it will be handled by the DependencyProperty

---------

Co-authored-by: Kevin Bost <[email protected]>
  • Loading branch information
corvinsz and Keboo authored Jan 31, 2025
1 parent d82fb55 commit ee04080
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/MaterialDesignThemes.Wpf/UpDownBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,8 @@ private void OnTextBoxFocusLost(object sender, EventArgs e)
{
SetCurrentValue(ValueProperty, value);
}
else
{
textBoxField.Text = Value?.ToString();
}
//NB: Because setting ValueProperty will coerce the value, we re-assign back to the textbox here.
textBoxField.Text = Value?.ToString();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ await Wait.For(async () =>
Assert.Equal("1", await textBox.GetText());
Assert.Equal(1, await numericUpDown.GetValue());
});

recorder.Success();
}

[Fact]
Expand Down Expand Up @@ -63,6 +65,8 @@ await Wait.For(async () =>
});

Assert.True(await plusButton.GetIsEnabled());

recorder.Success();
}

[Fact]
Expand Down Expand Up @@ -94,6 +98,8 @@ await Wait.For(async () =>
});

Assert.True(await minusButton.GetIsEnabled());

recorder.Success();
}

[Fact]
Expand All @@ -116,6 +122,8 @@ public async Task MaxAndMinAssignments_CoerceValueToBeInRange()
Assert.Equal(3, await numericUpDown.GetValue());
Assert.Equal(3, await numericUpDown.GetMinimum());
Assert.Equal(3, await numericUpDown.GetMaximum());

recorder.Success();
}

[Fact]
Expand Down Expand Up @@ -147,4 +155,63 @@ public async Task InternalTextBoxIsFocused_WhenGettingKeyboardFocus()

recorder.Success();
}

[Fact]
[Description("Issue 3781")]
public async Task IncreaseButtonClickWhenTextIsAboveMaximum_DoesNotIncreaseValue()
{
await using var recorder = new TestRecorder(App);

var stackPanel = await LoadXaml<StackPanel>("""
<StackPanel>
<materialDesign:DecimalUpDown Minimum="-2.5" Maximum="2.5" Value="2.5" />
<Button Content="AlternateFocusElement" />
</StackPanel>
""");
var decimalUpDown = await stackPanel.GetElement<DecimalUpDown>();
var textBox = await decimalUpDown.GetElement<TextBox>("PART_TextBox");
var plusButton = await decimalUpDown.GetElement<RepeatButton>("PART_IncreaseButton");

var button = await stackPanel.GetElement<Button>();

await textBox.MoveKeyboardFocus();
await textBox.SendKeyboardInput($"{ModifierKeys.Control}{Key.A}{ModifierKeys.None}30");
await plusButton.LeftClick();

//NB: Because the focus has not left the up down control, we don't expect the text to change
Assert.Equal("30", await textBox.GetText());
Assert.Equal(2.5m, await decimalUpDown.GetValue());

recorder.Success();
}

[Theory]
[Description("Issue 3781")]
[InlineData("30")]
[InlineData("abc")]
[InlineData("2a")]
public async Task LostFocusWhenTextIsInvalid_RevertsToOriginalValue(string inputText)
{
await using var recorder = new TestRecorder(App);

var stackPanel = await LoadXaml<StackPanel>("""
<StackPanel>
<materialDesign:DecimalUpDown Minimum="-2.5" Maximum="2.5" Value="2.5" />
<Button Content="AlternateFocusElement" />
</StackPanel>
""");
var decimalUpDown = await stackPanel.GetElement<DecimalUpDown>();
var textBox = await decimalUpDown.GetElement<TextBox>("PART_TextBox");

var button = await stackPanel.GetElement<Button>();

await textBox.MoveKeyboardFocus();
await textBox.SendKeyboardInput($"{ModifierKeys.Control}{Key.A}{ModifierKeys.None}{inputText}");
await button.MoveKeyboardFocus();

Assert.Equal("2.5", await textBox.GetText());
Assert.Equal(2.5m, await decimalUpDown.GetValue());

recorder.Success();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ await Wait.For(async () =>
Assert.Equal("1", await textBox.GetText());
Assert.Equal(1, await numericUpDown.GetValue());
});

recorder.Success();
}

[Fact]
Expand Down Expand Up @@ -64,6 +66,8 @@ await Wait.For(async () =>
});

Assert.True(await plusButton.GetIsEnabled());

recorder.Success();
}

[Fact]
Expand Down Expand Up @@ -95,6 +99,8 @@ await Wait.For(async () =>
});

Assert.True(await minusButton.GetIsEnabled());

recorder.Success();
}

[Fact]
Expand All @@ -117,6 +123,8 @@ public async Task MaxAndMinAssignments_CoerceValueToBeInRange()
Assert.Equal(3, await numericUpDown.GetValue());
Assert.Equal(3, await numericUpDown.GetMinimum());
Assert.Equal(3, await numericUpDown.GetMaximum());

recorder.Success();
}

[Fact]
Expand Down

0 comments on commit ee04080

Please sign in to comment.