Skip to content

Commit

Permalink
chore: fix failing tests
Browse files Browse the repository at this point in the history
- When_Changes_In_TextChanged // racing condition, needed a WaitForIdle
- When_Text_Changed_Sequence // undo KeyboardHelper.InputText change, that doesnt work
  • Loading branch information
Xiaoy312 committed Apr 24, 2024
1 parent 1aede17 commit 0b3b53d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
using Microsoft.UI.Xaml.Media;
using Uno.Disposables;
using Uno.Extensions;

using static Private.Infrastructure.TestServices;
using static Microsoft.UI.Xaml.Controls.AutoSuggestionBoxTextChangeReason;

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls
{
Expand Down Expand Up @@ -282,66 +284,75 @@ public async Task When_Text_Changed_SuggestionChosen()
Assert.AreEqual(AutoSuggestionBoxTextChangeReason.SuggestionChosen, reason);
}


[TestMethod]
[DataRow(true)]
[DataRow(false)]
public async Task When_Text_Changed_Sequence(bool waitBetweenActions)
{
var SUT = new AutoSuggestBox();
SUT.ItemsSource = new List<string>() { "ab", "abc", "abcde" };
var SUT = new AutoSuggestBox()
{
ItemsSource = new List<string>() { "ab", "abc", "abcde" }
};
WindowHelper.WindowContent = SUT;
await WindowHelper.WaitForIdle();

var textBox = (TextBox)SUT.GetTemplateChild("TextBox");

var expectations = new List<AutoSuggestionBoxTextChangeReason>();
var reasons = new List<AutoSuggestionBoxTextChangeReason>();
SUT.TextChanged += (s, e) =>
{
reasons.Add(e.Reason);
};

expectations.Add(SuggestionChosen);
SUT.Focus(FocusState.Programmatic);
SUT.ChoseItem("ab");
await Wait();

expectations.Add(ProgrammaticChange);
SUT.Text = "other";
await Wait();
KeyboardHelper.InputText("manual");

expectations.Add(UserInput);
SUT.Focus(FocusState.Programmatic);
textBox.ProcessTextInput("manual");
await Wait();

expectations.Add(SuggestionChosen);
SUT.ChoseItem("ab");
await Wait();
KeyboardHelper.InputText("manual");

expectations.Add(UserInput);
SUT.Focus(FocusState.Programmatic);
textBox.ProcessTextInput("manual");
await Wait();

expectations.Add(ProgrammaticChange);
SUT.Focus(FocusState.Programmatic);
SUT.Text = "other";
await Wait();

expectations.Add(SuggestionChosen);
SUT.ChoseItem("ab");
await Wait();

await WindowHelper.WaitForIdle();

#if __SKIA__ // skia is closer to what happens on WinUI. On WinUI, if there is no delay between changes, AutoSuggestBox.TextChanged is fired once (but TextBox.TextChanged fires everytime)
if (waitBetweenActions)
#endif
{
CollectionAssert.AreEquivalent(
new[] {
AutoSuggestionBoxTextChangeReason.SuggestionChosen,
AutoSuggestionBoxTextChangeReason.ProgrammaticChange,
AutoSuggestionBoxTextChangeReason.UserInput,
AutoSuggestionBoxTextChangeReason.SuggestionChosen,
AutoSuggestionBoxTextChangeReason.UserInput,
AutoSuggestionBoxTextChangeReason.ProgrammaticChange,
AutoSuggestionBoxTextChangeReason.SuggestionChosen
},
reasons);
}
#if __SKIA__
else
// skia is closer to what happens on WinUI. On WinUI, if there is no delay between changes,
// AutoSuggestBox.TextChanged is fired once (but TextBox.TextChanged fires everytime)
if (!waitBetweenActions)
{
CollectionAssert.AreEquivalent(
new[] {
AutoSuggestionBoxTextChangeReason.SuggestionChosen
},
reasons);
expectations = new() { SuggestionChosen };
}
#endif

CollectionAssert.AreEquivalent(expectations, reasons, string.Join("; ",
$"expectations[{expectations.Count}]: {string.Join(",", expectations)}",
$"actual[{reasons.Count}]: {string.Join(",", reasons)}"
));

async Task Wait()
{
if (waitBetweenActions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,6 @@ public async Task When_Changes_In_TextChanged()
{
int update = 0;
var initialText = "Text";
string GetCurrentText() => initialText + update;
string GetNewText() => initialText + ++update;

var textBox = new TextBox
{
Expand All @@ -539,13 +537,18 @@ public async Task When_Changes_In_TextChanged()
WindowHelper.WindowContent = textBox;
await WindowHelper.WaitForLoaded(textBox);

// make sure the initial (''->'Waiting') TextChanged event had time to be dispatched
await WindowHelper.WaitForIdle();

int textChangedInvokeCount = 0;
int textChangingInvokeCount = 0;

bool failedCheck = false;
bool finished = false;

void OnTextChanged(object sender, TextChangedEventArgs e)
string GetCurrentText() => initialText + update;
string GetNewText() => initialText + ++update;
async void OnTextChanged(object sender, TextChangedEventArgs e)
{
textChangedInvokeCount++;
if (GetCurrentText() != textBox.Text)
Expand Down

0 comments on commit 0b3b53d

Please sign in to comment.