Skip to content

Commit e41c808

Browse files
committed
Added improvement to automation peer to throw ElementNotEnabledException if attempting to update value when in a readonly state
1 parent 9e1c078 commit e41c808

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Microsoft.Toolkit.Uwp.UI.Controls.Input/TokenizingTextBox/TokenizingTextBoxAutomationPeer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,14 @@ private TokenizingTextBox OwningTokenizingTextBox
4545

4646
/// <summary>Sets the value of a control.</summary>
4747
/// <param name="value">The value to set. The provider is responsible for converting the value to the appropriate data type.</param>
48+
/// <exception cref="T:Windows.UI.Xaml.Automation.ElementNotEnabledException">Thrown if the control is in a read-only state.</exception>
4849
public void SetValue(string value)
4950
{
51+
if (IsReadOnly)
52+
{
53+
throw new ElementNotEnabledException($"Could not set the value of the {nameof(TokenizingTextBox)} ");
54+
}
55+
5056
this.OwningTokenizingTextBox.Text = value;
5157
}
5258

UnitTests/UnitTests.UWP/UI/Controls/Test_TokenizingTextBox_AutomationPeer.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,32 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
4545
tokenizingTextBox.Name = expectedName;
4646
Assert.IsTrue(tokenizingTextBoxAutomationPeer.GetName().Contains(expectedName), "Verify that the UIA name contains the given Name of the TokenizingTextBox.");
4747

48-
tokenizingTextBox.Text = expectedValue;
48+
tokenizingTextBoxAutomationPeer.SetValue(expectedValue);
4949
Assert.IsTrue(tokenizingTextBoxAutomationPeer.Value.Equals(expectedValue), "Verify that the Value contains the given Text of the TokenizingTextBox.");
5050
});
5151
}
5252

53+
[TestMethod]
54+
public async Task ShouldThrowElementNotEnabledExceptionIfValueSetWhenDisabled()
55+
{
56+
await App.DispatcherQueue.EnqueueAsync(async () =>
57+
{
58+
const string expectedValue = "Wor";
59+
60+
var tokenizingTextBox = new TokenizingTextBox { IsEnabled = false };
61+
62+
await SetTestContentAsync(tokenizingTextBox);
63+
64+
var tokenizingTextBoxAutomationPeer =
65+
FrameworkElementAutomationPeer.CreatePeerForElement(tokenizingTextBox) as TokenizingTextBoxAutomationPeer;
66+
67+
Assert.ThrowsException<ElementNotEnabledException>(() =>
68+
{
69+
tokenizingTextBoxAutomationPeer.SetValue(expectedValue);
70+
});
71+
});
72+
}
73+
5374
public class TokenizingTextBoxTestItem
5475
{
5576
public string Title { get; set; }

0 commit comments

Comments
 (0)