Skip to content

Commit 377a884

Browse files
michael-hawkerRosuavio
authored andcommitted
Rewrite test to be deterministic
1 parent d21f12d commit 377a884

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

UnitTests/UnitTests.UWP/UI/Controls/Test_ListDetailsView_UI.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Threading.Tasks;
1212
using Windows.UI.Xaml;
1313
using Windows.UI.Xaml.Controls;
14+
using Windows.UI.Xaml.Input;
1415
using Windows.UI.Xaml.Markup;
1516

1617
namespace UnitTests.UWP.UI.Controls
@@ -52,23 +53,22 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
5253

5354
await SetTestContentAsync(listDetailsView);
5455

55-
await Task.Delay(1000);
56-
5756
var firsttb = listDetailsView.FindDescendant<TextBox>();
5857

59-
firsttb.Focus(FocusState.Programmatic);
58+
await App.DispatcherQueue.EnqueueAsync(() => firsttb.Focus(FocusState.Programmatic));
6059

61-
await Task.Delay(1000);
60+
Assert.AreEqual(firsttb, FocusManager.GetFocusedElement(), "TextBox didn't get focus");
6261

63-
var firstLostFocus = false;
62+
var tcs = new TaskCompletionSource<bool>();
6463

65-
firsttb.LostFocus += (s, e) => firstLostFocus = true;
64+
firsttb.LostFocus += (s, e) => tcs.SetResult(true);
6665

6766
listDetailsView.SelectedIndex = -1;
6867

69-
await Task.Delay(1000);
68+
await Task.WhenAny(tcs.Task, Task.Delay(2000));
7069

71-
Assert.IsTrue(firstLostFocus, "TextBox in the first item should have lost focus.");
70+
Assert.IsTrue(tcs.Task.IsCompleted);
71+
Assert.IsTrue(tcs.Task.Result, "TextBox in the first item should have lost focus.");
7272
});
7373
}
7474

@@ -90,23 +90,22 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
9090

9191
await SetTestContentAsync(listDetailsView);
9292

93-
await Task.Delay(1000);
94-
9593
var firsttb = listDetailsView.FindDescendant<TextBox>();
9694

97-
firsttb.Focus(FocusState.Programmatic);
95+
await App.DispatcherQueue.EnqueueAsync(() => firsttb.Focus(FocusState.Programmatic));
9896

99-
await Task.Delay(1000);
97+
Assert.AreEqual(firsttb, FocusManager.GetFocusedElement(), "TextBox didn't get focus");
10098

101-
var firstLostFocus = false;
99+
var tcs = new TaskCompletionSource<bool>();
102100

103-
firsttb.LostFocus += (s, e) => firstLostFocus = true;
101+
firsttb.LostFocus += (s, e) => tcs.SetResult(true);
104102

105103
listDetailsView.SelectedIndex = 1;
106104

107-
await Task.Delay(1000);
105+
await Task.WhenAny(tcs.Task, Task.Delay(2000));
108106

109-
Assert.IsTrue(firstLostFocus, "TextBox in the first item should have lost focus.");
107+
Assert.IsTrue(tcs.Task.IsCompleted);
108+
Assert.IsTrue(tcs.Task.Result, "TextBox in the first item should have lost focus.");
110109
});
111110
}
112111
}

0 commit comments

Comments
 (0)