Skip to content

Commit 9799606

Browse files
authored
Merge branch 'main' into user/mhawker/gridsplitter-tests
2 parents 90ae117 + ee5a7b8 commit 9799606

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

Microsoft.Toolkit.Uwp.UI/Extensions/TextBox/TextBoxExtensions.Mask.Internals.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,18 @@ private static void Textbox_TextChanging(TextBox textbox, TextBoxTextChangingEve
297297
// case adding data at the end of the textbox
298298
if (oldSelectionStart >= oldText.Length && !isDeleteOrBackspace)
299299
{
300-
textbox.Text = textbox.Text.Substring(0, oldText.Length);
301-
if (oldText.Length >= 0)
300+
// ignore change(s) if oldtext is a substring of new text value
301+
if (textbox.Text.Contains(oldText))
302302
{
303-
textbox.SelectionStart = oldText.Length;
304-
}
303+
textbox.Text = oldText;
305304

306-
return;
305+
if (oldText.Length >= 0)
306+
{
307+
textbox.SelectionStart = oldText.Length;
308+
}
309+
310+
return;
311+
}
307312
}
308313

309314
var textArray = oldText.ToCharArray();
@@ -323,6 +328,9 @@ private static void Textbox_TextChanging(TextBox textbox, TextBoxTextChangingEve
323328
var displayText = textbox.GetValue(DefaultDisplayTextProperty) as string ?? string.Empty;
324329
if (string.IsNullOrEmpty(textbox.Text))
325330
{
331+
textbox.SetValue(OldTextProperty, displayText);
332+
textbox.SetValue(OldSelectionStartProperty, 0);
333+
textbox.SetValue(OldSelectionLengthProperty, 0);
326334
textbox.Text = displayText;
327335
return;
328336
}
@@ -427,4 +435,4 @@ private static int GetSelectionStart(string mask, List<int> escapedChars, int se
427435
return selectionIndex;
428436
}
429437
}
430-
}
438+
}

UITests/UITests.Tests.Shared/Controls/TextBoxMaskTest.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,31 @@ public void TestTextBoxMaskBinding_Property()
4646

4747
Verify.AreEqual(newValue, textBox.GetText());
4848
}
49+
50+
[TestMethod]
51+
[TestPage("TextBoxMaskTestPage")]
52+
public void TestTextBoxMaskBinding_TextChanging_RegressionCheck()
53+
{
54+
var initialValue = FindElement.ById<TextBlock>("InitialValueTextBlock").GetText();
55+
var textBox = FindElement.ById<Edit>("TextBox");
56+
57+
Verify.AreEqual(initialValue, textBox.GetText());
58+
59+
var setEmptyButton = FindElement.ById<Button>("SetEmptyButton");
60+
61+
setEmptyButton.Click();
62+
Wait.ForIdle();
63+
64+
// Previously, if the bound value of TextBox with a mask was set to an empty
65+
// string. It would display the old values after text was entered by the user.
66+
// PR #4048 should fix this issue.
67+
textBox.SendKeys("0");
68+
69+
// The user entering "0" key at the start position of the TextBox would result
70+
// in the following value prior to PR #4048.
71+
const string incorrectValue = "02:50:59";
72+
73+
Verify.AreNotEqual(incorrectValue, textBox.GetText());
74+
}
4975
}
5076
}

UITests/UITests.Tests.Shared/Controls/TextBoxMaskTestPage.xaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<RowDefinition Height="Auto" />
1313
<RowDefinition Height="10" />
1414
<RowDefinition Height="Auto" />
15+
<RowDefinition Height="10" />
16+
<RowDefinition Height="Auto" />
1517
</Grid.RowDefinitions>
1618
<Grid.ColumnDefinitions>
1719
<ColumnDefinition Width="Auto" />
@@ -43,5 +45,10 @@
4345
Grid.Column="2"
4446
HorizontalAlignment="Center"
4547
Text="{x:Bind NewValue}" />
48+
<Button x:Name="SetEmptyButton"
49+
Grid.Row="4"
50+
Grid.Column="0"
51+
Click="SetEmptyButton_Click"
52+
Content="Set Target Value Empty" />
4653
</Grid>
4754
</Page>

UITests/UITests.Tests.Shared/Controls/TextBoxMaskTestPage.xaml.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,11 @@ private void ChangeButton_Click(object sender, RoutedEventArgs e)
3838
Value = NEW_VALUE;
3939
Log.Comment("Value Changed to {0}", Value);
4040
}
41+
42+
private void SetEmptyButton_Click(object sender, RoutedEventArgs e)
43+
{
44+
Value = string.Empty;
45+
Log.Comment("Value Changed to {0}", Value);
46+
}
4147
}
4248
}

0 commit comments

Comments
 (0)