1111using System . Threading . Tasks ;
1212using Windows . UI . Xaml ;
1313using Windows . UI . Xaml . Controls ;
14+ using Windows . UI . Xaml . Input ;
1415using Windows . UI . Xaml . Markup ;
1516
1617namespace 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