11
11
using System . Threading . Tasks ;
12
12
using Windows . UI . Xaml ;
13
13
using Windows . UI . Xaml . Controls ;
14
+ using Windows . UI . Xaml . Input ;
14
15
using Windows . UI . Xaml . Markup ;
15
16
16
17
namespace UnitTests . UWP . UI . Controls
@@ -52,23 +53,22 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
52
53
53
54
await SetTestContentAsync ( listDetailsView ) ;
54
55
55
- await Task . Delay ( 1000 ) ;
56
-
57
56
var firsttb = listDetailsView . FindDescendant < TextBox > ( ) ;
58
57
59
- firsttb . Focus ( FocusState . Programmatic ) ;
58
+ await App . DispatcherQueue . EnqueueAsync ( ( ) => firsttb . Focus ( FocusState . Programmatic ) ) ;
60
59
61
- await Task . Delay ( 1000 ) ;
60
+ Assert . AreEqual ( firsttb , FocusManager . GetFocusedElement ( ) , "TextBox didn't get focus" ) ;
62
61
63
- var firstLostFocus = false ;
62
+ var tcs = new TaskCompletionSource < bool > ( ) ;
64
63
65
- firsttb . LostFocus += ( s , e ) => firstLostFocus = true ;
64
+ firsttb . LostFocus += ( s , e ) => tcs . SetResult ( true ) ;
66
65
67
66
listDetailsView . SelectedIndex = - 1 ;
68
67
69
- await Task . Delay ( 1000 ) ;
68
+ await Task . WhenAny ( tcs . Task , Task . Delay ( 2000 ) ) ;
70
69
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." ) ;
72
72
} ) ;
73
73
}
74
74
@@ -90,23 +90,22 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
90
90
91
91
await SetTestContentAsync ( listDetailsView ) ;
92
92
93
- await Task . Delay ( 1000 ) ;
94
-
95
93
var firsttb = listDetailsView . FindDescendant < TextBox > ( ) ;
96
94
97
- firsttb . Focus ( FocusState . Programmatic ) ;
95
+ await App . DispatcherQueue . EnqueueAsync ( ( ) => firsttb . Focus ( FocusState . Programmatic ) ) ;
98
96
99
- await Task . Delay ( 1000 ) ;
97
+ Assert . AreEqual ( firsttb , FocusManager . GetFocusedElement ( ) , "TextBox didn't get focus" ) ;
100
98
101
- var firstLostFocus = false ;
99
+ var tcs = new TaskCompletionSource < bool > ( ) ;
102
100
103
- firsttb . LostFocus += ( s , e ) => firstLostFocus = true ;
101
+ firsttb . LostFocus += ( s , e ) => tcs . SetResult ( true ) ;
104
102
105
103
listDetailsView . SelectedIndex = 1 ;
106
104
107
- await Task . Delay ( 1000 ) ;
105
+ await Task . WhenAny ( tcs . Task , Task . Delay ( 2000 ) ) ;
108
106
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." ) ;
110
109
} ) ;
111
110
}
112
111
}
0 commit comments