1
+ using NUnit . Framework ;
2
+ using System . Linq ;
3
+
4
+ namespace Xamarin . Forms . Core . UnitTests
5
+ {
6
+ [ TestFixture ]
7
+ public class TabIndexTests : BaseTestFixture
8
+ {
9
+
10
+ [ Test ]
11
+ public void GetTabIndexesOnParentPage_ImplicitZero ( )
12
+ {
13
+ var target = new StackLayout
14
+ {
15
+ Children = {
16
+ new Label { TabIndex = 1 } ,
17
+ new Label { TabIndex = 0 } ,
18
+ new Label { TabIndex = 3 } ,
19
+ new Label { TabIndex = 2 } ,
20
+ }
21
+ } ;
22
+
23
+ var page = new ContentPage { Content = target } ;
24
+
25
+ var tabIndexes = target . GetTabIndexesOnParentPage ( out int _ ) ;
26
+
27
+ //StackLayout is technically the first element with TabIndex 0.
28
+ Assert . AreEqual ( target , tabIndexes [ 0 ] [ 0 ] ) ;
29
+ }
30
+
31
+ [ Test ]
32
+ public void GetTabIndexesOnParentPage_ExplicitZero ( )
33
+ {
34
+ Label target = new Label { TabIndex = 0 } ;
35
+ var stackLayout = new StackLayout
36
+ {
37
+ Children = {
38
+ new Label { TabIndex = 1 } ,
39
+ target ,
40
+ new Label { TabIndex = 3 } ,
41
+ new Label { TabIndex = 2 } ,
42
+ }
43
+ } ;
44
+
45
+ var page = new ContentPage { Content = stackLayout } ;
46
+
47
+ var tabIndexes = stackLayout . GetTabIndexesOnParentPage ( out int _ ) ;
48
+
49
+ Assert . AreEqual ( target , tabIndexes [ 0 ] [ 1 ] ) ;
50
+ }
51
+
52
+ [ Test ]
53
+ public void GetTabIndexesOnParentPage_NegativeTabIndex ( )
54
+ {
55
+ Label target = new Label { TabIndex = - 1 } ;
56
+ var stackLayout = new StackLayout
57
+ {
58
+ Children = {
59
+ new Label { TabIndex = 1 } ,
60
+ target ,
61
+ new Label { TabIndex = 3 } ,
62
+ new Label { TabIndex = 2 } ,
63
+ }
64
+ } ;
65
+
66
+ var page = new ContentPage { Content = stackLayout } ;
67
+
68
+ var tabIndexes = stackLayout . GetTabIndexesOnParentPage ( out int _ ) ;
69
+
70
+ Assert . AreEqual ( target , tabIndexes [ - 1 ] [ 0 ] ) ;
71
+ }
72
+
73
+ [ Test ]
74
+ public void FindNextElement_Forward_NextTabIndex ( )
75
+ {
76
+ Label target = new Label { TabIndex = 1 } ;
77
+ Label nextElement = new Label { TabIndex = 2 } ;
78
+ var stackLayout = new StackLayout
79
+ {
80
+ Children = {
81
+ new Label { TabIndex = 1 } ,
82
+ target ,
83
+ new Label { TabIndex = 3 } ,
84
+ nextElement ,
85
+ }
86
+ } ;
87
+
88
+ var page = new ContentPage { Content = stackLayout } ;
89
+
90
+ var tabIndexes = stackLayout . GetTabIndexesOnParentPage ( out int maxAttempts ) ;
91
+
92
+ int _ = target . TabIndex ;
93
+
94
+ var found = target . FindNextElement ( true , tabIndexes , ref _ ) ;
95
+
96
+ Assert . AreEqual ( nextElement , found ) ;
97
+ }
98
+
99
+ [ Test ]
100
+ public void FindNextElement_Forward_DeclarationOrder ( )
101
+ {
102
+ Label target = new Label { TabIndex = 1 } ;
103
+ Label nextElement = new Label { TabIndex = 2 } ;
104
+ var stackLayout = new StackLayout
105
+ {
106
+ Children = {
107
+ new Label { TabIndex = 1 } ,
108
+ target ,
109
+ nextElement ,
110
+ new Label { TabIndex = 2 } ,
111
+ }
112
+ } ;
113
+
114
+ var page = new ContentPage { Content = stackLayout } ;
115
+
116
+ var tabIndexes = stackLayout . GetTabIndexesOnParentPage ( out int maxAttempts ) ;
117
+
118
+ int _ = target . TabIndex ;
119
+
120
+ var found = target . FindNextElement ( true , tabIndexes , ref _ ) ;
121
+
122
+ Assert . AreEqual ( nextElement , found ) ;
123
+ }
124
+
125
+ [ Test ]
126
+ public void FindNextElement_Forward_TabIndex ( )
127
+ {
128
+ Label target = new Label { TabIndex = 1 } ;
129
+ Label nextElement = new Label { TabIndex = 2 } ;
130
+ var stackLayout = new StackLayout
131
+ {
132
+ Children = {
133
+ new Label { TabIndex = 1 } ,
134
+ target ,
135
+ nextElement ,
136
+ new Label { TabIndex = 2 } ,
137
+ }
138
+ } ;
139
+
140
+ var page = new ContentPage { Content = stackLayout } ;
141
+
142
+ var tabIndexes = stackLayout . GetTabIndexesOnParentPage ( out int maxAttempts ) ;
143
+
144
+ int tabIndex = target . TabIndex ;
145
+
146
+ var found = target . FindNextElement ( true , tabIndexes , ref tabIndex ) ;
147
+
148
+ Assert . AreEqual ( 2 , tabIndex ) ;
149
+ }
150
+
151
+ [ Test ]
152
+ public void FindNextElement_Backward_NextTabIndex ( )
153
+ {
154
+ Label target = new Label { TabIndex = 2 } ;
155
+ Label nextElement = new Label { TabIndex = 1 } ;
156
+ var stackLayout = new StackLayout
157
+ {
158
+ Children = {
159
+ new Label { TabIndex = 3 } ,
160
+ target ,
161
+ new Label { TabIndex = 3 } ,
162
+ nextElement ,
163
+ }
164
+ } ;
165
+
166
+ var page = new ContentPage { Content = stackLayout } ;
167
+
168
+ var tabIndexes = stackLayout . GetTabIndexesOnParentPage ( out int maxAttempts ) ;
169
+
170
+ int _ = target . TabIndex ;
171
+
172
+ var found = target . FindNextElement ( false , tabIndexes , ref _ ) ;
173
+
174
+ Assert . AreEqual ( nextElement , found ) ;
175
+ }
176
+
177
+ [ Test ]
178
+ public void FindNextElement_Backward_DeclarationOrder ( )
179
+ {
180
+ Label target = new Label { TabIndex = 2 } ;
181
+ Label nextElement = new Label { TabIndex = 1 } ;
182
+ var stackLayout = new StackLayout
183
+ {
184
+ Children = {
185
+ new Label { TabIndex = 3 } ,
186
+ target ,
187
+ nextElement ,
188
+ new Label { TabIndex = 1 } ,
189
+ }
190
+ } ;
191
+
192
+ var page = new ContentPage { Content = stackLayout } ;
193
+
194
+ var tabIndexes = stackLayout . GetTabIndexesOnParentPage ( out int maxAttempts ) ;
195
+
196
+ int _ = target . TabIndex ;
197
+
198
+ var found = target . FindNextElement ( false , tabIndexes , ref _ ) ;
199
+
200
+ Assert . AreEqual ( nextElement , found ) ;
201
+ }
202
+
203
+ [ Test ]
204
+ public void FindNextElement_Backward_TabIndex ( )
205
+ {
206
+ Label target = new Label { TabIndex = 2 } ;
207
+ Label nextElement = new Label { TabIndex = 1 } ;
208
+ var stackLayout = new StackLayout
209
+ {
210
+ Children = {
211
+ new Label { TabIndex = 3 } ,
212
+ target ,
213
+ nextElement ,
214
+ new Label { TabIndex = 2 } ,
215
+ }
216
+ } ;
217
+
218
+ var page = new ContentPage { Content = stackLayout } ;
219
+
220
+ var tabIndexes = stackLayout . GetTabIndexesOnParentPage ( out int maxAttempts ) ;
221
+
222
+ int tabIndex = target . TabIndex ;
223
+
224
+ var found = target . FindNextElement ( false , tabIndexes , ref tabIndex ) ;
225
+
226
+ Assert . AreEqual ( 1 , tabIndex ) ;
227
+ }
228
+ }
229
+ }
0 commit comments