From 0b3b53d24b786c4d00e28456d62bde99c229229f Mon Sep 17 00:00:00 2001 From: xiaoy312 Date: Wed, 24 Apr 2024 16:08:30 -0400 Subject: [PATCH] chore: fix failing tests - When_Changes_In_TextChanged // racing condition, needed a WaitForIdle - When_Text_Changed_Sequence // undo KeyboardHelper.InputText change, that doesnt work --- .../Given_AutoSuggestBox.cs | 65 +++++++++++-------- .../Windows_UI_Xaml_Controls/Given_TextBox.cs | 9 ++- 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_AutoSuggestBox.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_AutoSuggestBox.cs index a952ea31d6df..20d8c6d64a1c 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_AutoSuggestBox.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_AutoSuggestBox.cs @@ -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 { @@ -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() { "ab", "abc", "abcde" }; + var SUT = new AutoSuggestBox() + { + ItemsSource = new List() { "ab", "abc", "abcde" } + }; WindowHelper.WindowContent = SUT; await WindowHelper.WaitForIdle(); + + var textBox = (TextBox)SUT.GetTemplateChild("TextBox"); + + var expectations = new List(); var reasons = new List(); 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) diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBox.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBox.cs index 3d01e867b66a..0e3d3333d104 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBox.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBox.cs @@ -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 { @@ -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)