• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package android.autofillservice.cts.dropdown;
17 
18 import static android.autofillservice.cts.activities.GridActivity.ID_L1C1;
19 import static android.autofillservice.cts.activities.GridActivity.ID_L1C2;
20 import static android.autofillservice.cts.activities.GridActivity.ID_L2C1;
21 import static android.autofillservice.cts.activities.GridActivity.ID_L2C2;
22 import static android.autofillservice.cts.activities.GridActivity.ID_L3C1;
23 import static android.autofillservice.cts.activities.GridActivity.ID_L3C2;
24 import static android.autofillservice.cts.activities.GridActivity.ID_L4C1;
25 import static android.autofillservice.cts.activities.GridActivity.ID_L4C2;
26 import static android.autofillservice.cts.testcore.Helper.UNUSED_AUTOFILL_VALUE;
27 import static android.autofillservice.cts.testcore.Helper.assertHasFlags;
28 import static android.autofillservice.cts.testcore.Helper.assertTextIsSanitized;
29 import static android.autofillservice.cts.testcore.Helper.assertValue;
30 import static android.autofillservice.cts.testcore.Helper.getContext;
31 import static android.autofillservice.cts.testcore.Helper.getMaxPartitions;
32 import static android.autofillservice.cts.testcore.Helper.setMaxPartitions;
33 import static android.service.autofill.FillRequest.FLAG_MANUAL_REQUEST;
34 import static android.service.autofill.SaveInfo.SAVE_DATA_TYPE_ADDRESS;
35 import static android.service.autofill.SaveInfo.SAVE_DATA_TYPE_CREDIT_CARD;
36 import static android.service.autofill.SaveInfo.SAVE_DATA_TYPE_GENERIC;
37 import static android.service.autofill.SaveInfo.SAVE_DATA_TYPE_PASSWORD;
38 import static android.service.autofill.SaveInfo.SAVE_DATA_TYPE_USERNAME;
39 
40 import static com.google.common.truth.Truth.assertThat;
41 import static com.google.common.truth.Truth.assertWithMessage;
42 
43 import android.app.assist.AssistStructure.ViewNode;
44 import android.autofillservice.cts.activities.AuthenticationActivity;
45 import android.autofillservice.cts.activities.GridActivity.FillExpectation;
46 import android.autofillservice.cts.commontests.AbstractGridActivityTestCase;
47 import android.autofillservice.cts.testcore.CannedFillResponse;
48 import android.autofillservice.cts.testcore.CannedFillResponse.CannedDataset;
49 import android.autofillservice.cts.testcore.InstrumentedAutoFillService.FillRequest;
50 import android.autofillservice.cts.testcore.InstrumentedAutoFillService.SaveRequest;
51 import android.content.IntentSender;
52 import android.os.Bundle;
53 import android.platform.test.annotations.AppModeFull;
54 import android.service.autofill.FillResponse;
55 
56 import org.junit.Test;
57 
58 /**
59  * Test case for an activity containing multiple partitions.
60  */
61 @AppModeFull(reason = "Service-specific test")
62 public class PartitionedActivityTest extends AbstractGridActivityTestCase {
63 
64     @Test
testAutofillTwoPartitionsSkipFirst()65     public void testAutofillTwoPartitionsSkipFirst() throws Exception {
66         // Set service.
67         enableService();
68 
69         // Prepare 1st partition.
70         final CannedFillResponse response1 = new CannedFillResponse.Builder()
71                 .addDataset(new CannedDataset.Builder()
72                         .setField(ID_L1C1, "l1c1", createPresentation("l1c1"))
73                         .setField(ID_L1C2, "l1c2", createPresentation("l1c2"))
74                         .build())
75                 .build();
76         sReplier.addResponse(response1);
77 
78         // Trigger auto-fill on 1st partition.
79         focusCell(1, 1);
80         final FillRequest fillRequest1 = sReplier.getNextFillRequest();
81         assertThat(fillRequest1.flags).isEqualTo(0);
82         final ViewNode p1l1c1 = assertTextIsSanitized(fillRequest1.structure, ID_L1C1);
83         final ViewNode p1l1c2 = assertTextIsSanitized(fillRequest1.structure, ID_L1C2);
84         assertWithMessage("Focus on p1l1c1").that(p1l1c1.isFocused()).isTrue();
85         assertWithMessage("Focus on p1l1c2").that(p1l1c2.isFocused()).isFalse();
86 
87         // Make sure UI is shown, but don't tap it.
88         mUiBot.assertDatasets("l1c1");
89         focusCell(1, 2);
90         mUiBot.assertDatasets("l1c2");
91 
92         // Now tap a field in a different partition
93         final CannedFillResponse response2 = new CannedFillResponse.Builder()
94                 .addDataset(new CannedDataset.Builder()
95                         .setField(ID_L2C1, "l2c1", createPresentation("l2c1"))
96                         .setField(ID_L2C2, "l2c2", createPresentation("l2c2"))
97                         .build())
98                 .build();
99         sReplier.addResponse(response2);
100 
101         // Trigger auto-fill on 2nd partition.
102         focusCell(2, 1);
103         final FillRequest fillRequest2 = sReplier.getNextFillRequest();
104         assertThat(fillRequest2.flags).isEqualTo(0);
105         final ViewNode p2l1c1 = assertTextIsSanitized(fillRequest2.structure, ID_L1C1);
106         final ViewNode p2l1c2 = assertTextIsSanitized(fillRequest2.structure, ID_L1C2);
107         final ViewNode p2l2c1 = assertTextIsSanitized(fillRequest2.structure, ID_L2C1);
108         final ViewNode p2l2c2 = assertTextIsSanitized(fillRequest2.structure, ID_L2C2);
109         assertWithMessage("Focus on p2l1c1").that(p2l1c1.isFocused()).isFalse();
110         assertWithMessage("Focus on p2l1c2").that(p2l1c2.isFocused()).isFalse();
111         assertWithMessage("Focus on p2l2c1").that(p2l2c1.isFocused()).isTrue();
112         assertWithMessage("Focus on p2l2c2").that(p2l2c2.isFocused()).isFalse();
113         // Make sure UI is shown, but don't tap it.
114         mUiBot.assertDatasets("l2c1");
115         focusCell(2, 2);
116         mUiBot.assertDatasets("l2c2");
117 
118         // Now fill them
119         final FillExpectation expectation1 = mActivity.expectAutofill()
120                 .onCell(1, 1, "l1c1").onCell(1, 2, "l1c2");
121         focusCell(1, 1);
122         mUiBot.selectDataset("l1c1");
123         expectation1.assertAutoFilled();
124 
125         // Change previous values to make sure they are not filled again
126         mActivity.setText(1, 1, "L1C1");
127         mActivity.setText(1, 2, "L1C2");
128 
129         final FillExpectation expectation2 = mActivity.expectAutofill()
130                 .onCell(2, 1, "l2c1").onCell(2, 2, "l2c2");
131         focusCell(2, 2);
132         mUiBot.selectDataset("l2c2");
133         expectation2.assertAutoFilled();
134 
135         // Make sure previous partition didn't change
136         assertThat(mActivity.getText(1, 1)).isEqualTo("L1C1");
137         assertThat(mActivity.getText(1, 2)).isEqualTo("L1C2");
138     }
139 
140     @Test
testAutofillTwoPartitionsInSequence()141     public void testAutofillTwoPartitionsInSequence() throws Exception {
142         // Set service.
143         enableService();
144 
145         // 1st partition
146         // Prepare.
147         final CannedFillResponse response1 = new CannedFillResponse.Builder()
148                 .addDataset(new CannedDataset.Builder()
149                         .setPresentation(createPresentation("Partition 1"))
150                         .setField(ID_L1C1, "l1c1")
151                         .setField(ID_L1C2, "l1c2")
152                         .build())
153                 .build();
154         sReplier.addResponse(response1);
155         final FillExpectation expectation1 = mActivity.expectAutofill()
156                 .onCell(1, 1, "l1c1")
157                 .onCell(1, 2, "l1c2");
158 
159         // Trigger auto-fill.
160         focusCell(1, 1);
161         final FillRequest fillRequest1 = sReplier.getNextFillRequest();
162         assertThat(fillRequest1.flags).isEqualTo(0);
163 
164         assertTextIsSanitized(fillRequest1.structure, ID_L1C1);
165         assertTextIsSanitized(fillRequest1.structure, ID_L1C2);
166 
167         // Auto-fill it.
168         mUiBot.selectDataset("Partition 1");
169 
170         // Check the results.
171         expectation1.assertAutoFilled();
172 
173         // 2nd partition
174         // Prepare.
175         final CannedFillResponse response2 = new CannedFillResponse.Builder()
176                 .addDataset(new CannedDataset.Builder()
177                         .setPresentation(createPresentation("Partition 2"))
178                         .setField(ID_L2C1, "l2c1")
179                         .setField(ID_L2C2, "l2c2")
180                         .build())
181                 .build();
182         sReplier.addResponse(response2);
183         final FillExpectation expectation2 = mActivity.expectAutofill()
184                 .onCell(2, 1, "l2c1")
185                 .onCell(2, 2, "l2c2");
186 
187         // Trigger auto-fill.
188         focusCell(2, 1);
189         final FillRequest fillRequest2 = sReplier.getNextFillRequest();
190         assertThat(fillRequest2.flags).isEqualTo(0);
191 
192         assertValue(fillRequest2.structure, ID_L1C1, "l1c1");
193         assertValue(fillRequest2.structure, ID_L1C2, "l1c2");
194         assertTextIsSanitized(fillRequest2.structure, ID_L2C1);
195         assertTextIsSanitized(fillRequest2.structure, ID_L2C2);
196 
197         // Auto-fill it.
198         mUiBot.selectDataset("Partition 2");
199 
200         // Check the results.
201         expectation2.assertAutoFilled();
202     }
203 
204     @Test
testAutofill4PartitionsAutomatically()205     public void testAutofill4PartitionsAutomatically() throws Exception {
206         autofill4PartitionsTest(false);
207     }
208 
209     @Test
testAutofill4PartitionsManually()210     public void testAutofill4PartitionsManually() throws Exception {
211         autofill4PartitionsTest(true);
212     }
213 
autofill4PartitionsTest(boolean manually)214     private void autofill4PartitionsTest(boolean manually) throws Exception {
215         // Set service.
216         enableService();
217 
218         // 1st partition
219         // Prepare.
220         final CannedFillResponse response1 = new CannedFillResponse.Builder()
221                 .addDataset(new CannedDataset.Builder()
222                         .setPresentation(createPresentation("Partition 1"))
223                         .setField(ID_L1C1, "l1c1")
224                         .setField(ID_L1C2, "l1c2")
225                         .build())
226                 .build();
227         sReplier.addResponse(response1);
228         final FillExpectation expectation1 = mActivity.expectAutofill()
229                 .onCell(1, 1, "l1c1")
230                 .onCell(1, 2, "l1c2");
231 
232         // Trigger auto-fill.
233         mActivity.triggerAutofill(manually, 1, 1);
234         final FillRequest fillRequest1 = sReplier.getNextFillRequest();
235 
236         if (manually) {
237             assertHasFlags(fillRequest1.flags, FLAG_MANUAL_REQUEST);
238             assertValue(fillRequest1.structure, ID_L1C1, "");
239         } else {
240             assertThat(fillRequest1.flags).isEqualTo(0);
241             assertTextIsSanitized(fillRequest1.structure, ID_L1C1);
242         }
243         assertTextIsSanitized(fillRequest1.structure, ID_L1C2);
244 
245         // Auto-fill it.
246         mUiBot.selectDataset("Partition 1");
247 
248         // Check the results.
249         expectation1.assertAutoFilled();
250 
251         // 2nd partition
252         // Prepare.
253         final CannedFillResponse response2 = new CannedFillResponse.Builder()
254                 .addDataset(new CannedDataset.Builder()
255                         .setPresentation(createPresentation("Partition 2"))
256                         .setField(ID_L2C1, "l2c1")
257                         .setField(ID_L2C2, "l2c2")
258                         .build())
259                 .build();
260         sReplier.addResponse(response2);
261         final FillExpectation expectation2 = mActivity.expectAutofill()
262                 .onCell(2, 1, "l2c1")
263                 .onCell(2, 2, "l2c2");
264 
265         // Trigger auto-fill.
266         mActivity.triggerAutofill(manually, 2, 1);
267         final FillRequest fillRequest2 = sReplier.getNextFillRequest();
268 
269         assertValue(fillRequest2.structure, ID_L1C1, "l1c1");
270         assertValue(fillRequest2.structure, ID_L1C2, "l1c2");
271         if (manually) {
272             assertHasFlags(fillRequest2.flags, FLAG_MANUAL_REQUEST);
273             assertValue(fillRequest2.structure, ID_L2C1, "");
274         } else {
275             assertThat(fillRequest2.flags).isEqualTo(0);
276             assertTextIsSanitized(fillRequest2.structure, ID_L2C1);
277         }
278         assertTextIsSanitized(fillRequest2.structure, ID_L2C2);
279 
280         // Auto-fill it.
281         mUiBot.selectDataset("Partition 2");
282 
283         // Check the results.
284         expectation2.assertAutoFilled();
285 
286         // 3rd partition
287         // Prepare.
288         final CannedFillResponse response3 = new CannedFillResponse.Builder()
289                 .addDataset(new CannedDataset.Builder()
290                         .setPresentation(createPresentation("Partition 3"))
291                         .setField(ID_L3C1, "l3c1")
292                         .setField(ID_L3C2, "l3c2")
293                         .build())
294                 .build();
295         sReplier.addResponse(response3);
296         final FillExpectation expectation3 = mActivity.expectAutofill()
297                 .onCell(3, 1, "l3c1")
298                 .onCell(3, 2, "l3c2");
299 
300         // Trigger auto-fill.
301         mActivity.triggerAutofill(manually, 3, 1);
302         final FillRequest fillRequest3 = sReplier.getNextFillRequest();
303 
304         assertValue(fillRequest3.structure, ID_L1C1, "l1c1");
305         assertValue(fillRequest3.structure, ID_L1C2, "l1c2");
306         assertValue(fillRequest3.structure, ID_L2C1, "l2c1");
307         assertValue(fillRequest3.structure, ID_L2C2, "l2c2");
308         if (manually) {
309             assertHasFlags(fillRequest3.flags, FLAG_MANUAL_REQUEST);
310             assertValue(fillRequest3.structure, ID_L3C1, "");
311         } else {
312             assertThat(fillRequest3.flags).isEqualTo(0);
313             assertTextIsSanitized(fillRequest3.structure, ID_L3C1);
314         }
315         assertTextIsSanitized(fillRequest3.structure, ID_L3C2);
316 
317         // Auto-fill it.
318         mUiBot.selectDataset("Partition 3");
319 
320         // Check the results.
321         expectation3.assertAutoFilled();
322 
323         // 4th partition
324         // Prepare.
325         final CannedFillResponse response4 = new CannedFillResponse.Builder()
326                 .addDataset(new CannedDataset.Builder()
327                         .setPresentation(createPresentation("Partition 4"))
328                         .setField(ID_L4C1, "l4c1")
329                         .setField(ID_L4C2, "l4c2")
330                         .build())
331                 .build();
332         sReplier.addResponse(response4);
333         final FillExpectation expectation4 = mActivity.expectAutofill()
334                 .onCell(4, 1, "l4c1")
335                 .onCell(4, 2, "l4c2");
336 
337         // Trigger auto-fill.
338         mActivity.triggerAutofill(manually, 4, 1);
339         final FillRequest fillRequest4 = sReplier.getNextFillRequest();
340 
341         assertValue(fillRequest4.structure, ID_L1C1, "l1c1");
342         assertValue(fillRequest4.structure, ID_L1C2, "l1c2");
343         assertValue(fillRequest4.structure, ID_L2C1, "l2c1");
344         assertValue(fillRequest4.structure, ID_L2C2, "l2c2");
345         assertValue(fillRequest4.structure, ID_L3C1, "l3c1");
346         assertValue(fillRequest4.structure, ID_L3C2, "l3c2");
347         if (manually) {
348             assertHasFlags(fillRequest4.flags, FLAG_MANUAL_REQUEST);
349             assertValue(fillRequest4.structure, ID_L4C1, "");
350         } else {
351             assertThat(fillRequest4.flags).isEqualTo(0);
352             assertTextIsSanitized(fillRequest4.structure, ID_L4C1);
353         }
354         assertTextIsSanitized(fillRequest4.structure, ID_L4C2);
355 
356         // Auto-fill it.
357         mUiBot.selectDataset("Partition 4");
358 
359         // Check the results.
360         expectation4.assertAutoFilled();
361     }
362 
363     @Test
testAutofill4PartitionsMixManualAndAuto()364     public void testAutofill4PartitionsMixManualAndAuto() throws Exception {
365         // Set service.
366         enableService();
367 
368         // 1st partition - auto
369         // Prepare.
370         final CannedFillResponse response1 = new CannedFillResponse.Builder()
371                 .addDataset(new CannedDataset.Builder()
372                         .setPresentation(createPresentation("Partition 1"))
373                         .setField(ID_L1C1, "l1c1")
374                         .setField(ID_L1C2, "l1c2")
375                         .build())
376                 .build();
377         sReplier.addResponse(response1);
378         final FillExpectation expectation1 = mActivity.expectAutofill()
379                 .onCell(1, 1, "l1c1")
380                 .onCell(1, 2, "l1c2");
381 
382         // Trigger auto-fill.
383         focusCell(1, 1);
384         final FillRequest fillRequest1 = sReplier.getNextFillRequest();
385         assertThat(fillRequest1.flags).isEqualTo(0);
386 
387         assertTextIsSanitized(fillRequest1.structure, ID_L1C1);
388         assertTextIsSanitized(fillRequest1.structure, ID_L1C2);
389 
390         // Auto-fill it.
391         mUiBot.selectDataset("Partition 1");
392 
393         // Check the results.
394         expectation1.assertAutoFilled();
395 
396         // 2nd partition - manual
397         // Prepare
398         // Must set text before creating expectation, and it must be a subset of the dataset values,
399         // otherwise the UI won't be shown because of filtering
400         mActivity.setText(2, 1, "l2");
401         final CannedFillResponse response2 = new CannedFillResponse.Builder()
402                 .addDataset(new CannedDataset.Builder()
403                         .setPresentation(createPresentation("Partition 2"))
404                         .setField(ID_L2C1, "l2c1")
405                         .setField(ID_L2C2, "l2c2")
406                         .build())
407                 .build();
408         sReplier.addResponse(response2);
409         final FillExpectation expectation2 = mActivity.expectAutofill()
410                 .onCell(2, 1, "l2c1")
411                 .onCell(2, 2, "l2c2");
412 
413         // Trigger auto-fill.
414         mActivity.forceAutofill(2, 1);
415         final FillRequest fillRequest2 = sReplier.getNextFillRequest();
416         assertHasFlags(fillRequest2.flags, FLAG_MANUAL_REQUEST);
417 
418         assertValue(fillRequest2.structure, ID_L1C1, "l1c1");
419         assertValue(fillRequest2.structure, ID_L1C2, "l1c2");
420         assertValue(fillRequest2.structure, ID_L2C1, "l2");
421         assertTextIsSanitized(fillRequest2.structure, ID_L2C2);
422 
423         // Auto-fill it.
424         mUiBot.selectDataset("Partition 2");
425 
426         // Check the results.
427         expectation2.assertAutoFilled();
428 
429         // 3rd partition - auto
430         // Prepare.
431         final CannedFillResponse response3 = new CannedFillResponse.Builder()
432                 .addDataset(new CannedDataset.Builder()
433                         .setPresentation(createPresentation("Partition 3"))
434                         .setField(ID_L3C1, "l3c1")
435                         .setField(ID_L3C2, "l3c2")
436                         .build())
437                 .build();
438         sReplier.addResponse(response3);
439         final FillExpectation expectation3 = mActivity.expectAutofill()
440                 .onCell(3, 1, "l3c1")
441                 .onCell(3, 2, "l3c2");
442 
443         // Trigger auto-fill.
444         focusCell(3, 1);
445         final FillRequest fillRequest3 = sReplier.getNextFillRequest();
446         assertThat(fillRequest3.flags).isEqualTo(0);
447 
448         assertValue(fillRequest3.structure, ID_L1C1, "l1c1");
449         assertValue(fillRequest3.structure, ID_L1C2, "l1c2");
450         assertValue(fillRequest3.structure, ID_L2C1, "l2c1");
451         assertValue(fillRequest3.structure, ID_L2C2, "l2c2");
452         assertTextIsSanitized(fillRequest3.structure, ID_L3C1);
453         assertTextIsSanitized(fillRequest3.structure, ID_L3C2);
454 
455         // Auto-fill it.
456         mUiBot.selectDataset("Partition 3");
457 
458         // Check the results.
459         expectation3.assertAutoFilled();
460 
461         // 4th partition - manual
462         // Must set text before creating expectation, and it must be a subset of the dataset values,
463         // otherwise the UI won't be shown because of filtering
464         mActivity.setText(4, 1, "l4");
465         final CannedFillResponse response4 = new CannedFillResponse.Builder()
466                 .addDataset(new CannedDataset.Builder()
467                         .setPresentation(createPresentation("Partition 4"))
468                         .setField(ID_L4C1, "l4c1")
469                         .setField(ID_L4C2, "l4c2")
470                         .build())
471                 .build();
472         sReplier.addResponse(response4);
473         final FillExpectation expectation4 = mActivity.expectAutofill()
474                 .onCell(4, 1, "l4c1")
475                 .onCell(4, 2, "l4c2");
476 
477         // Trigger auto-fill.
478         mActivity.forceAutofill(4, 1);
479         final FillRequest fillRequest4 = sReplier.getNextFillRequest();
480         assertHasFlags(fillRequest4.flags, FLAG_MANUAL_REQUEST);
481 
482         assertValue(fillRequest4.structure, ID_L1C1, "l1c1");
483         assertValue(fillRequest4.structure, ID_L1C2, "l1c2");
484         assertValue(fillRequest4.structure, ID_L2C1, "l2c1");
485         assertValue(fillRequest4.structure, ID_L2C2, "l2c2");
486         assertValue(fillRequest4.structure, ID_L3C1, "l3c1");
487         assertValue(fillRequest4.structure, ID_L3C2, "l3c2");
488         assertValue(fillRequest4.structure, ID_L4C1, "l4");
489         assertTextIsSanitized(fillRequest4.structure, ID_L4C2);
490 
491         // Auto-fill it.
492         mUiBot.selectDataset("Partition 4");
493 
494         // Check the results.
495         expectation4.assertAutoFilled();
496     }
497 
498     @Test
testAutofillBundleDataIsPassedAlong()499     public void testAutofillBundleDataIsPassedAlong() throws Exception {
500         // Set service.
501         enableService();
502 
503         final Bundle extras = new Bundle();
504         extras.putString("numbers", "4");
505 
506         // Prepare 1st partition.
507         final CannedFillResponse response1 = new CannedFillResponse.Builder()
508                 .addDataset(new CannedDataset.Builder()
509                         .setField(ID_L1C1, "l1c1", createPresentation("l1c1"))
510                         .setField(ID_L1C2, "l1c2", createPresentation("l1c2"))
511                         .build())
512                 .setExtras(extras)
513                 .build();
514         sReplier.addResponse(response1);
515 
516         // Trigger auto-fill on 1st partition.
517         focusCell(1, 1);
518         final FillRequest fillRequest1 = sReplier.getNextFillRequest();
519         assertThat(fillRequest1.flags).isEqualTo(0);
520         assertThat(fillRequest1.data).isNull();
521         mUiBot.assertDatasets("l1c1");
522 
523         // Prepare 2nd partition; it replaces 'number' and adds 'numbers2'
524         extras.clear();
525         extras.putString("numbers", "48");
526         extras.putString("numbers2", "1516");
527 
528         final CannedFillResponse response2 = new CannedFillResponse.Builder()
529                 .addDataset(new CannedDataset.Builder()
530                         .setField(ID_L2C1, "l2c1", createPresentation("l2c1"))
531                         .setField(ID_L2C2, "l2c2", createPresentation("l2c2"))
532                         .build())
533                 .setExtras(extras)
534                 .build();
535         sReplier.addResponse(response2);
536 
537         // Trigger auto-fill on 2nd partition
538         focusCell(2, 1);
539         final FillRequest fillRequest2 = sReplier.getNextFillRequest();
540         assertThat(fillRequest2.flags).isEqualTo(0);
541         assertWithMessage("null bundle on request 2").that(fillRequest2.data).isNotNull();
542         assertWithMessage("wrong number of extras on request 2 bundle")
543                 .that(fillRequest2.data.size()).isEqualTo(1);
544         assertThat(fillRequest2.data.getString("numbers")).isEqualTo("4");
545 
546         // Prepare 3nd partition; it has no extras
547         final CannedFillResponse response3 = new CannedFillResponse.Builder()
548                 .addDataset(new CannedDataset.Builder()
549                         .setField(ID_L3C1, "l3c1", createPresentation("l3c1"))
550                         .setField(ID_L3C2, "l3c2", createPresentation("l3c2"))
551                         .build())
552                 .setExtras(null)
553                 .build();
554         sReplier.addResponse(response3);
555 
556         // Trigger auto-fill on 3rd partition
557         focusCell(3, 1);
558         final FillRequest fillRequest3 = sReplier.getNextFillRequest();
559         assertThat(fillRequest3.flags).isEqualTo(0);
560         assertWithMessage("null bundle on request 3").that(fillRequest2.data).isNotNull();
561         assertWithMessage("wrong number of extras on request 3 bundle")
562                 .that(fillRequest3.data.size()).isEqualTo(2);
563         assertThat(fillRequest3.data.getString("numbers")).isEqualTo("48");
564         assertThat(fillRequest3.data.getString("numbers2")).isEqualTo("1516");
565 
566 
567         // Prepare 4th partition; it contains just 'numbers4'
568         extras.clear();
569         extras.putString("numbers4", "2342");
570 
571         final CannedFillResponse response4 = new CannedFillResponse.Builder()
572                 .addDataset(new CannedDataset.Builder()
573                         .setField(ID_L4C1, "l4c1", createPresentation("l4c1"))
574                         .setField(ID_L4C2, "l4c2", createPresentation("l4c2"))
575                         .build())
576                 .setRequiredSavableIds(SAVE_DATA_TYPE_PASSWORD, ID_L1C1)
577                 .setExtras(extras)
578                 .build();
579         sReplier.addResponse(response4);
580 
581         // Trigger auto-fill on 4th partition
582         focusCell(4, 1);
583         final FillRequest fillRequest4 = sReplier.getNextFillRequest();
584         assertThat(fillRequest4.flags).isEqualTo(0);
585         assertWithMessage("non-null bundle on request 4").that(fillRequest4.data).isNull();
586 
587         // Trigger save
588         mActivity.setText(1, 1, "L1C1");
589         mActivity.save();
590 
591         mUiBot.saveForAutofill(true, SAVE_DATA_TYPE_PASSWORD);
592         final SaveRequest saveRequest = sReplier.getNextSaveRequest();
593 
594         assertWithMessage("wrong number of extras on save request bundle")
595                 .that(saveRequest.data.size()).isEqualTo(1);
596         assertThat(saveRequest.data.getString("numbers4")).isEqualTo("2342");
597     }
598 
599     @Test
testSaveOneSaveInfoOnFirstPartitionWithIdsOnSecond()600     public void testSaveOneSaveInfoOnFirstPartitionWithIdsOnSecond() throws Exception {
601         // Set service.
602         enableService();
603 
604         // Trigger 1st partition.
605         final CannedFillResponse response1 = new CannedFillResponse.Builder()
606                 .addDataset(new CannedDataset.Builder()
607                         .setField(ID_L1C1, "l1c1", createPresentation("l1c1"))
608                         .setField(ID_L1C2, "l1c2", createPresentation("l1c2"))
609                         .build())
610                 .build();
611         sReplier.addResponse(response1);
612         focusCell(1, 1);
613         sReplier.getNextFillRequest();
614 
615         // Trigger 2nd partition.
616         final CannedFillResponse response2 = new CannedFillResponse.Builder()
617                 .addDataset(new CannedDataset.Builder()
618                         .setField(ID_L2C1, "l2c1", createPresentation("l2c1"))
619                         .setField(ID_L2C2, "l2c2", createPresentation("l2c2"))
620                         .build())
621                 .setRequiredSavableIds(SAVE_DATA_TYPE_PASSWORD, ID_L2C1)
622                 .build();
623         sReplier.addResponse(response2);
624         focusCell(2, 1);
625         sReplier.getNextFillRequest();
626 
627         // Trigger save
628         mActivity.setText(2, 1, "L2C1");
629         mActivity.save();
630 
631         mUiBot.saveForAutofill(true, SAVE_DATA_TYPE_PASSWORD);
632         final SaveRequest saveRequest = sReplier.getNextSaveRequest();
633         assertValue(saveRequest.structure, ID_L2C1, "L2C1");
634     }
635 
636     @Test
testSaveOneSaveInfoOnSecondPartitionWithIdsOnFirst()637     public void testSaveOneSaveInfoOnSecondPartitionWithIdsOnFirst() throws Exception {
638         // Set service.
639         enableService();
640 
641         // Trigger 1st partition.
642         final CannedFillResponse response1 = new CannedFillResponse.Builder()
643                 .addDataset(new CannedDataset.Builder()
644                         .setField(ID_L1C1, "l1c1", createPresentation("l1c1"))
645                         .setField(ID_L1C2, "l1c2", createPresentation("l1c2"))
646                         .build())
647                 .build();
648         sReplier.addResponse(response1);
649         focusCell(1, 1);
650         sReplier.getNextFillRequest();
651 
652         // Trigger 2nd partition.
653         final CannedFillResponse response2 = new CannedFillResponse.Builder()
654                 .addDataset(new CannedDataset.Builder()
655                         .setField(ID_L2C1, "l2c1", createPresentation("l2c1"))
656                         .setField(ID_L2C2, "l2c2", createPresentation("l2c2"))
657                         .build())
658                 .setRequiredSavableIds(SAVE_DATA_TYPE_PASSWORD, ID_L1C1)
659                 .build();
660         sReplier.addResponse(response2);
661         focusCell(2, 1);
662         sReplier.getNextFillRequest();
663 
664         // Trigger save
665         mActivity.setText(1, 1, "L1C1");
666         mActivity.save();
667 
668         mUiBot.saveForAutofill(true, SAVE_DATA_TYPE_PASSWORD);
669         final SaveRequest saveRequest = sReplier.getNextSaveRequest();
670         assertValue(saveRequest.structure, ID_L1C1, "L1C1");
671     }
672 
673     @Test
testSaveTwoSaveInfosDifferentTypes()674     public void testSaveTwoSaveInfosDifferentTypes() throws Exception {
675         // Set service.
676         enableService();
677 
678         // Trigger 1st partition.
679         final CannedFillResponse response1 = new CannedFillResponse.Builder()
680                 .addDataset(new CannedDataset.Builder()
681                         .setField(ID_L1C1, "l1c1", createPresentation("l1c1"))
682                         .setField(ID_L1C2, "l1c2", createPresentation("l1c2"))
683                         .build())
684                 .setRequiredSavableIds(SAVE_DATA_TYPE_PASSWORD, ID_L1C1)
685                 .build();
686         sReplier.addResponse(response1);
687         focusCell(1, 1);
688         sReplier.getNextFillRequest();
689 
690         // Trigger 2nd partition.
691         final CannedFillResponse response2 = new CannedFillResponse.Builder()
692                 .addDataset(new CannedDataset.Builder()
693                         .setField(ID_L2C1, "l2c1", createPresentation("l2c1"))
694                         .setField(ID_L2C2, "l2c2", createPresentation("l2c2"))
695                         .build())
696                 .setRequiredSavableIds(SAVE_DATA_TYPE_PASSWORD | SAVE_DATA_TYPE_CREDIT_CARD,
697                         ID_L2C1)
698                 .build();
699         sReplier.addResponse(response2);
700         focusCell(2, 1);
701         sReplier.getNextFillRequest();
702 
703         // Trigger save
704         mActivity.setText(1, 1, "L1C1");
705         mActivity.setText(2, 1, "L2C1");
706         mActivity.save();
707 
708         mUiBot.saveForAutofill(true, SAVE_DATA_TYPE_PASSWORD, SAVE_DATA_TYPE_CREDIT_CARD);
709         final SaveRequest saveRequest = sReplier.getNextSaveRequest();
710         assertValue(saveRequest.structure, ID_L1C1, "L1C1");
711         assertValue(saveRequest.structure, ID_L2C1, "L2C1");
712     }
713 
714     @Test
testSaveThreeSaveInfosDifferentTypes()715     public void testSaveThreeSaveInfosDifferentTypes() throws Exception {
716         // Set service.
717         enableService();
718 
719         // Trigger 1st partition.
720         final CannedFillResponse response1 = new CannedFillResponse.Builder()
721                 .addDataset(new CannedDataset.Builder()
722                         .setField(ID_L1C1, "l1c1", createPresentation("l1c1"))
723                         .setField(ID_L1C2, "l1c2", createPresentation("l1c2"))
724                         .build())
725                 .setRequiredSavableIds(SAVE_DATA_TYPE_PASSWORD, ID_L1C1)
726                 .build();
727         sReplier.addResponse(response1);
728         focusCell(1, 1);
729         sReplier.getNextFillRequest();
730 
731         // Trigger 2nd partition.
732         final CannedFillResponse response2 = new CannedFillResponse.Builder()
733                 .addDataset(new CannedDataset.Builder()
734                         .setField(ID_L2C1, "l2c1", createPresentation("l2c1"))
735                         .setField(ID_L2C2, "l2c2", createPresentation("l2c2"))
736                         .build())
737                 .setRequiredSavableIds(SAVE_DATA_TYPE_PASSWORD | SAVE_DATA_TYPE_CREDIT_CARD,
738                         ID_L2C1)
739                 .build();
740         sReplier.addResponse(response2);
741         focusCell(2, 1);
742         sReplier.getNextFillRequest();
743 
744         // Trigger 3rd partition.
745         final CannedFillResponse response3 = new CannedFillResponse.Builder()
746                 .addDataset(new CannedDataset.Builder()
747                         .setField(ID_L3C1, "l3c1", createPresentation("l3c1"))
748                         .setField(ID_L3C2, "l3c2", createPresentation("l3c2"))
749                         .build())
750                 .setRequiredSavableIds(SAVE_DATA_TYPE_PASSWORD | SAVE_DATA_TYPE_CREDIT_CARD
751                         | SAVE_DATA_TYPE_USERNAME, ID_L3C1)
752                 .build();
753         sReplier.addResponse(response3);
754         focusCell(3, 1);
755         sReplier.getNextFillRequest();
756 
757         // Trigger save
758         mActivity.setText(1, 1, "L1C1");
759         mActivity.setText(2, 1, "L2C1");
760         mActivity.setText(3, 1, "L3C1");
761         mActivity.save();
762 
763         mUiBot.saveForAutofill(true, SAVE_DATA_TYPE_PASSWORD, SAVE_DATA_TYPE_CREDIT_CARD,
764                 SAVE_DATA_TYPE_USERNAME);
765         final SaveRequest saveRequest = sReplier.getNextSaveRequest();
766         assertValue(saveRequest.structure, ID_L1C1, "L1C1");
767         assertValue(saveRequest.structure, ID_L2C1, "L2C1");
768         assertValue(saveRequest.structure, ID_L3C1, "L3C1");
769     }
770 
771     @Test
testSaveThreeSaveInfosDifferentTypesIncludingGeneric()772     public void testSaveThreeSaveInfosDifferentTypesIncludingGeneric() throws Exception {
773         // Set service.
774         enableService();
775 
776         // Trigger 1st partition.
777         final CannedFillResponse response1 = new CannedFillResponse.Builder()
778                 .addDataset(new CannedDataset.Builder()
779                         .setField(ID_L1C1, "l1c1", createPresentation("l1c1"))
780                         .setField(ID_L1C2, "l1c2", createPresentation("l1c2"))
781                         .build())
782                 .setRequiredSavableIds(SAVE_DATA_TYPE_PASSWORD, ID_L1C1)
783                 .build();
784         sReplier.addResponse(response1);
785         focusCell(1, 1);
786         sReplier.getNextFillRequest();
787 
788         // Trigger 2nd partition.
789         final CannedFillResponse response2 = new CannedFillResponse.Builder()
790                 .addDataset(new CannedDataset.Builder()
791                         .setField(ID_L2C1, "l2c1", createPresentation("l2c1"))
792                         .setField(ID_L2C2, "l2c2", createPresentation("l2c2"))
793                         .build())
794                 .setRequiredSavableIds(SAVE_DATA_TYPE_PASSWORD | SAVE_DATA_TYPE_GENERIC, ID_L2C1)
795                 .build();
796         sReplier.addResponse(response2);
797         focusCell(2, 1);
798         sReplier.getNextFillRequest();
799 
800         // Trigger 3rd partition.
801         final CannedFillResponse response3 = new CannedFillResponse.Builder()
802                 .addDataset(new CannedDataset.Builder()
803                         .setField(ID_L3C1, "l3c1", createPresentation("l3c1"))
804                         .setField(ID_L3C2, "l3c2", createPresentation("l3c2"))
805                         .build())
806                 .setRequiredSavableIds(
807                         SAVE_DATA_TYPE_PASSWORD | SAVE_DATA_TYPE_GENERIC | SAVE_DATA_TYPE_USERNAME,
808                         ID_L3C1)
809                 .build();
810         sReplier.addResponse(response3);
811         focusCell(3, 1);
812         sReplier.getNextFillRequest();
813 
814 
815         // Trigger save
816         mActivity.setText(1, 1, "L1C1");
817         mActivity.setText(2, 1, "L2C1");
818         mActivity.setText(3, 1, "L3C1");
819         mActivity.save();
820 
821         // Make sure GENERIC type is not shown on snackbar
822         mUiBot.saveForAutofill(true, SAVE_DATA_TYPE_PASSWORD, SAVE_DATA_TYPE_USERNAME);
823         final SaveRequest saveRequest = sReplier.getNextSaveRequest();
824         assertValue(saveRequest.structure, ID_L1C1, "L1C1");
825         assertValue(saveRequest.structure, ID_L2C1, "L2C1");
826         assertValue(saveRequest.structure, ID_L3C1, "L3C1");
827     }
828 
829     @Test
testSaveMoreThanThreeSaveInfosDifferentTypes()830     public void testSaveMoreThanThreeSaveInfosDifferentTypes() throws Exception {
831         // Set service.
832         enableService();
833 
834         // Trigger 1st partition.
835         final CannedFillResponse response1 = new CannedFillResponse.Builder()
836                 .addDataset(new CannedDataset.Builder()
837                         .setField(ID_L1C1, "l1c1", createPresentation("l1c1"))
838                         .setField(ID_L1C2, "l1c2", createPresentation("l1c2"))
839                         .build())
840                 .setRequiredSavableIds(SAVE_DATA_TYPE_PASSWORD, ID_L1C1)
841                 .build();
842         sReplier.addResponse(response1);
843         focusCell(1, 1);
844         sReplier.getNextFillRequest();
845 
846         // Trigger 2nd partition.
847         final CannedFillResponse response2 = new CannedFillResponse.Builder()
848                 .addDataset(new CannedDataset.Builder()
849                         .setField(ID_L2C1, "l2c1", createPresentation("l2c1"))
850                         .setField(ID_L2C2, "l2c2", createPresentation("l2c2"))
851                         .build())
852                 .setRequiredSavableIds(SAVE_DATA_TYPE_PASSWORD | SAVE_DATA_TYPE_CREDIT_CARD,
853                         ID_L2C1)
854                 .build();
855         sReplier.addResponse(response2);
856         focusCell(2, 1);
857         sReplier.getNextFillRequest();
858 
859         // Trigger 3rd partition.
860         final CannedFillResponse response3 = new CannedFillResponse.Builder()
861                 .addDataset(new CannedDataset.Builder()
862                         .setField(ID_L3C1, "l3c1", createPresentation("l3c1"))
863                         .setField(ID_L3C2, "l3c2", createPresentation("l3c2"))
864                         .build())
865                 .setRequiredSavableIds(SAVE_DATA_TYPE_PASSWORD | SAVE_DATA_TYPE_CREDIT_CARD
866                         | SAVE_DATA_TYPE_USERNAME, ID_L3C1)
867                 .build();
868         sReplier.addResponse(response3);
869         focusCell(3, 1);
870         sReplier.getNextFillRequest();
871 
872         // Trigger 4th partition.
873         final CannedFillResponse response4 = new CannedFillResponse.Builder()
874                 .addDataset(new CannedDataset.Builder()
875                         .setField(ID_L4C1, "l4c1", createPresentation("l4c1"))
876                         .setField(ID_L4C2, "l4c2", createPresentation("l4c2"))
877                         .build())
878                 .setRequiredSavableIds(SAVE_DATA_TYPE_PASSWORD | SAVE_DATA_TYPE_CREDIT_CARD
879                         | SAVE_DATA_TYPE_USERNAME | SAVE_DATA_TYPE_ADDRESS, ID_L4C1)
880                 .build();
881         sReplier.addResponse(response4);
882         focusCell(4, 1);
883         sReplier.getNextFillRequest();
884 
885 
886         // Trigger save
887         mActivity.setText(1, 1, "L1C1");
888         mActivity.setText(2, 1, "L2C1");
889         mActivity.setText(3, 1, "L3C1");
890         mActivity.setText(4, 1, "L4C1");
891         mActivity.save();
892 
893         mUiBot.saveForAutofill(true, SAVE_DATA_TYPE_GENERIC);
894         final SaveRequest saveRequest = sReplier.getNextSaveRequest();
895         assertValue(saveRequest.structure, ID_L1C1, "L1C1");
896         assertValue(saveRequest.structure, ID_L2C1, "L2C1");
897         assertValue(saveRequest.structure, ID_L3C1, "L3C1");
898         assertValue(saveRequest.structure, ID_L4C1, "L4C1");
899     }
900 
901     @Test
testIgnoredFieldsDontTriggerAutofill()902     public void testIgnoredFieldsDontTriggerAutofill() throws Exception {
903         // Set service.
904         enableService();
905 
906         // Prepare 1st partition.
907         final CannedFillResponse response1 = new CannedFillResponse.Builder()
908                 .addDataset(new CannedDataset.Builder()
909                         .setField(ID_L1C1, "l1c1", createPresentation("l1c1"))
910                         .setField(ID_L1C2, "l1c2", createPresentation("l1c2"))
911                         .build())
912                 .setIgnoreFields(ID_L2C1, ID_L2C2)
913                 .build();
914         sReplier.addResponse(response1);
915 
916         // Trigger auto-fill on 1st partition.
917         focusCell(1, 1);
918         final FillRequest fillRequest1 = sReplier.getNextFillRequest();
919         assertThat(fillRequest1.flags).isEqualTo(0);
920         final ViewNode p1l1c1 = assertTextIsSanitized(fillRequest1.structure, ID_L1C1);
921         final ViewNode p1l1c2 = assertTextIsSanitized(fillRequest1.structure, ID_L1C2);
922         assertWithMessage("Focus on p1l1c1").that(p1l1c1.isFocused()).isTrue();
923         assertWithMessage("Focus on p1l1c2").that(p1l1c2.isFocused()).isFalse();
924 
925         // Make sure UI is shown on 1st partition
926         mUiBot.assertDatasets("l1c1");
927         focusCell(1, 2);
928         mUiBot.assertDatasets("l1c2");
929 
930         // Make sure UI is not shown on ignored partition
931         focusCell(2, 1);
932         mUiBot.assertNoDatasets();
933         focusCellNoWindowChange(2, 2);
934         mUiBot.assertNoDatasetsEver();
935     }
936 
937     /**
938      * Tests scenario where each partition has more than one dataset, but they don't overlap, i.e.,
939      * each {@link FillResponse} only contain fields within the partition.
940      */
941     @Test
testAutofillMultipleDatasetsNoOverlap()942     public void testAutofillMultipleDatasetsNoOverlap() throws Exception {
943         // Set service.
944         enableService();
945 
946         /**
947          * 1st partition.
948          */
949         // Set expectations.
950         final CannedFillResponse response1 = new CannedFillResponse.Builder()
951                 .addDataset(new CannedDataset.Builder()
952                         .setPresentation(createPresentation("P1D1"))
953                         .setField(ID_L1C1, "l1c1")
954                         .setField(ID_L1C2, "l1c2")
955                         .build())
956                 .addDataset(new CannedDataset.Builder()
957                         .setPresentation(createPresentation("P1D2"))
958                         .setField(ID_L1C1, "L1C1")
959                         .build())
960                 .build();
961         sReplier.addResponse(response1);
962         final FillExpectation expectation1 = mActivity.expectAutofill()
963                 .onCell(1, 1, "l1c1")
964                 .onCell(1, 2, "l1c2");
965 
966         // Trigger partition.
967         focusCell(1, 1);
968         sReplier.getNextFillRequest();
969 
970 
971         /**
972          * 2nd partition.
973          */
974         // Set expectations.
975         final CannedFillResponse response2 = new CannedFillResponse.Builder()
976                 .addDataset(new CannedDataset.Builder()
977                         .setPresentation(createPresentation("P2D1"))
978                         .setField(ID_L2C1, "l2c1")
979                         .setField(ID_L2C2, "l2c2")
980                         .build())
981                 .addDataset(new CannedDataset.Builder()
982                         .setPresentation(createPresentation("P2D2"))
983                         .setField(ID_L2C2, "L2C2")
984                         .build())
985                 .build();
986         sReplier.addResponse(response2);
987         final FillExpectation expectation2 = mActivity.expectAutofill()
988                 .onCell(2, 2, "L2C2");
989 
990         // Trigger partition.
991         focusCell(2, 1);
992         sReplier.getNextFillRequest();
993 
994         /**
995          * 3rd partition.
996          */
997         // Set expectations.
998         final CannedFillResponse response3 = new CannedFillResponse.Builder()
999                 .addDataset(new CannedDataset.Builder()
1000                         .setPresentation(createPresentation("P3D1"))
1001                         .setField(ID_L3C1, "l3c1")
1002                         .setField(ID_L3C2, "l3c2")
1003                         .build())
1004                 .addDataset(new CannedDataset.Builder()
1005                         .setPresentation(createPresentation("P3D2"))
1006                         .setField(ID_L3C1, "L3C1")
1007                         .setField(ID_L3C2, "L3C2")
1008                         .build())
1009                 .build();
1010         sReplier.addResponse(response3);
1011         final FillExpectation expectation3 = mActivity.expectAutofill()
1012                 .onCell(3, 1, "L3C1")
1013                 .onCell(3, 2, "L3C2");
1014 
1015         // Trigger partition.
1016         focusCell(3, 1);
1017         sReplier.getNextFillRequest();
1018 
1019         /**
1020          * 4th partition.
1021          */
1022         // Set expectations.
1023         final CannedFillResponse response4 = new CannedFillResponse.Builder()
1024                 .addDataset(new CannedDataset.Builder()
1025                         .setPresentation(createPresentation("P4D1"))
1026                         .setField(ID_L4C1, "l4c1")
1027                         .build())
1028                 .addDataset(new CannedDataset.Builder()
1029                         .setPresentation(createPresentation("P4D2"))
1030                         .setField(ID_L4C1, "L4C1")
1031                         .setField(ID_L4C2, "L4C2")
1032                         .build())
1033                 .build();
1034         sReplier.addResponse(response4);
1035         final FillExpectation expectation4 = mActivity.expectAutofill()
1036                 .onCell(4, 1, "l4c1");
1037 
1038         // Trigger partition.
1039         focusCell(4, 1);
1040         sReplier.getNextFillRequest();
1041 
1042         /*
1043          *  Now move focus around to make sure the proper values are displayed each time.
1044          */
1045         focusCell(1, 1);
1046         mUiBot.assertDatasets("P1D1", "P1D2");
1047         focusCell(1, 2);
1048         mUiBot.assertDatasets("P1D1");
1049 
1050         focusCell(2, 1);
1051         mUiBot.assertDatasets("P2D1");
1052         focusCell(2, 2);
1053         mUiBot.assertDatasets("P2D1", "P2D2");
1054 
1055         focusCell(4, 1);
1056         mUiBot.assertDatasets("P4D1", "P4D2");
1057         focusCell(4, 2);
1058         mUiBot.assertDatasets("P4D2");
1059 
1060         focusCell(3, 2);
1061         mUiBot.assertDatasets("P3D1", "P3D2");
1062         focusCell(3, 1);
1063         mUiBot.assertDatasets("P3D1", "P3D2");
1064 
1065         /*
1066          *  Finally, autofill and check results.
1067          */
1068         focusCell(4, 1);
1069         mUiBot.selectDataset("P4D1");
1070         expectation4.assertAutoFilled();
1071 
1072         focusCell(1, 1);
1073         mUiBot.selectDataset("P1D1");
1074         expectation1.assertAutoFilled();
1075 
1076         focusCell(3, 1);
1077         mUiBot.selectDataset("P3D2");
1078         expectation3.assertAutoFilled();
1079 
1080         focusCell(2, 2);
1081         mUiBot.selectDataset("P2D2");
1082         expectation2.assertAutoFilled();
1083     }
1084 
1085     /**
1086      * Tests scenario where each partition has more than one dataset, but they overlap, i.e.,
1087      * some fields are present in more than one partition.
1088      *
1089      * <p>Whenever a new partition defines a field previously present in another partittion, that
1090      * partition will "own" that field.
1091      *
1092      * <p>In the end, 4th partition will one all fields in 2 datasets; and this test cases picks
1093      * the first.
1094      */
1095     @Test
testAutofillMultipleDatasetsOverlappingPicksFirst()1096     public void testAutofillMultipleDatasetsOverlappingPicksFirst() throws Exception {
1097         autofillMultipleDatasetsOverlapping(true);
1098     }
1099 
1100     /**
1101      * Tests scenario where each partition has more than one dataset, but they overlap, i.e.,
1102      * some fields are present in more than one partition.
1103      *
1104      * <p>Whenever a new partition defines a field previously present in another partittion, that
1105      * partition will "own" that field.
1106      *
1107      * <p>In the end, 4th partition will one all fields in 2 datasets; and this test cases picks
1108      * the second.
1109      */
1110     @Test
testAutofillMultipleDatasetsOverlappingPicksSecond()1111     public void testAutofillMultipleDatasetsOverlappingPicksSecond() throws Exception {
1112         autofillMultipleDatasetsOverlapping(false);
1113     }
1114 
autofillMultipleDatasetsOverlapping(boolean pickFirst)1115     private void autofillMultipleDatasetsOverlapping(boolean pickFirst) throws Exception {
1116         // Set service.
1117         enableService();
1118 
1119         /**
1120          * 1st partition.
1121          */
1122         // Set expectations.
1123         final CannedFillResponse response1 = new CannedFillResponse.Builder()
1124                 .addDataset(new CannedDataset.Builder()
1125                         .setPresentation(createPresentation("P1D1"))
1126                         .setField(ID_L1C1, "1l1c1")
1127                         .setField(ID_L1C2, "1l1c2")
1128                         .build())
1129                 .addDataset(new CannedDataset.Builder()
1130                         .setPresentation(createPresentation("P1D2"))
1131                         .setField(ID_L1C1, "1L1C1")
1132                         .build())
1133                 .build();
1134         sReplier.addResponse(response1);
1135 
1136         // Trigger partition.
1137         focusCell(1, 1);
1138         sReplier.getNextFillRequest();
1139 
1140         // Asserts proper datasets are shown on each field defined so far.
1141         mUiBot.assertDatasets("P1D1", "P1D2");
1142         focusCell(1, 2);
1143         mUiBot.assertDatasets("P1D1");
1144 
1145         /**
1146          * 2nd partition.
1147          */
1148         // Set expectations.
1149         final CannedFillResponse response2 = new CannedFillResponse.Builder()
1150                 .addDataset(new CannedDataset.Builder()
1151                         .setPresentation(createPresentation("P2D1"))
1152                         .setField(ID_L1C1, "2l1c1") // from previous partition
1153                         .setField(ID_L2C1, "2l2c1")
1154                         .setField(ID_L2C2, "2l2c2")
1155                         .build())
1156                 .addDataset(new CannedDataset.Builder()
1157                         .setPresentation(createPresentation("P2D2"))
1158                         .setField(ID_L2C2, "2L2C2")
1159                         .build())
1160                 .build();
1161         sReplier.addResponse(response2);
1162 
1163         // Trigger partition.
1164         focusCell(2, 1);
1165         sReplier.getNextFillRequest();
1166 
1167         // Asserts proper datasets are shown on each field defined so far.
1168         focusCell(1, 1);
1169         mUiBot.assertDatasets("P2D1"); // changed
1170         focusCell(1, 2);
1171         mUiBot.assertDatasets("P1D1");
1172         focusCell(2, 1);
1173         mUiBot.assertDatasets("P2D1");
1174         focusCell(2, 2);
1175         mUiBot.assertDatasets("P2D1", "P2D2");
1176 
1177         /**
1178          * 3rd partition.
1179          */
1180         // Set expectations.
1181         final CannedFillResponse response3 = new CannedFillResponse.Builder()
1182                 .addDataset(new CannedDataset.Builder()
1183                         .setPresentation(createPresentation("P3D1"))
1184                         .setField(ID_L1C2, "3l1c2")
1185                         .setField(ID_L3C1, "3l3c1")
1186                         .setField(ID_L3C2, "3l3c2")
1187                         .build())
1188                 .addDataset(new CannedDataset.Builder()
1189                         .setPresentation(createPresentation("P3D2"))
1190                         .setField(ID_L2C2, "3l2c2")
1191                         .setField(ID_L3C1, "3L3C1")
1192                         .setField(ID_L3C2, "3L3C2")
1193                         .build())
1194                 .build();
1195         sReplier.addResponse(response3);
1196 
1197         // Trigger partition.
1198         focusCell(3, 1);
1199         sReplier.getNextFillRequest();
1200 
1201         // Asserts proper datasets are shown on each field defined so far.
1202         focusCell(1, 1);
1203         mUiBot.assertDatasets("P2D1");
1204         focusCell(1, 2);
1205         mUiBot.assertDatasets("P3D1"); // changed
1206         focusCell(2, 1);
1207         mUiBot.assertDatasets("P2D1");
1208         focusCell(2, 2);
1209         mUiBot.assertDatasets("P3D2"); // changed
1210         focusCell(3, 2);
1211         mUiBot.assertDatasets("P3D1", "P3D2");
1212         focusCell(3, 1);
1213         mUiBot.assertDatasets("P3D1", "P3D2");
1214 
1215         /**
1216          * 4th partition.
1217          */
1218         // Set expectations.
1219         final CannedFillResponse response4 = new CannedFillResponse.Builder()
1220                 .addDataset(new CannedDataset.Builder()
1221                         .setPresentation(createPresentation("P4D1"))
1222                         .setField(ID_L1C1, "4l1c1")
1223                         .setField(ID_L1C2, "4l1c2")
1224                         .setField(ID_L2C1, "4l2c1")
1225                         .setField(ID_L2C2, "4l2c2")
1226                         .setField(ID_L3C1, "4l3c1")
1227                         .setField(ID_L3C2, "4l3c2")
1228                         .setField(ID_L4C1, "4l4c1")
1229                         .build())
1230                 .addDataset(new CannedDataset.Builder()
1231                         .setPresentation(createPresentation("P4D2"))
1232                         .setField(ID_L1C1, "4L1C1")
1233                         .setField(ID_L1C2, "4L1C2")
1234                         .setField(ID_L2C1, "4L2C1")
1235                         .setField(ID_L2C2, "4L2C2")
1236                         .setField(ID_L3C1, "4L3C1")
1237                         .setField(ID_L3C2, "4L3C2")
1238                         .setField(ID_L1C1, "4L1C1")
1239                         .setField(ID_L4C1, "4L4C1")
1240                         .setField(ID_L4C2, "4L4C2")
1241                         .build())
1242                 .build();
1243         sReplier.addResponse(response4);
1244 
1245         // Trigger partition.
1246         focusCell(4, 1);
1247         sReplier.getNextFillRequest();
1248 
1249         // Asserts proper datasets are shown on each field defined so far.
1250         focusCell(1, 1);
1251         mUiBot.assertDatasets("P4D1", "P4D2");
1252         focusCell(1, 2);
1253         mUiBot.assertDatasets("P4D1", "P4D2");
1254         focusCell(2, 1);
1255         mUiBot.assertDatasets("P4D1", "P4D2");
1256         focusCell(2, 2);
1257         mUiBot.assertDatasets("P4D1", "P4D2");
1258         focusCell(3, 2);
1259         mUiBot.assertDatasets("P4D1", "P4D2");
1260         focusCell(3, 1);
1261         mUiBot.assertDatasets("P4D1", "P4D2");
1262         focusCell(4, 1);
1263         mUiBot.assertDatasets("P4D1", "P4D2");
1264         focusCell(4, 2);
1265         mUiBot.assertDatasets("P4D2");
1266 
1267         /*
1268          * Finally, autofill and check results.
1269          */
1270         final FillExpectation expectation = mActivity.expectAutofill();
1271         final String chosenOne;
1272         if (pickFirst) {
1273             expectation
1274                 .onCell(1, 1, "4l1c1")
1275                 .onCell(1, 2, "4l1c2")
1276                 .onCell(2, 1, "4l2c1")
1277                 .onCell(2, 2, "4l2c2")
1278                 .onCell(3, 1, "4l3c1")
1279                 .onCell(3, 2, "4l3c2")
1280                 .onCell(4, 1, "4l4c1");
1281             chosenOne = "P4D1";
1282         } else {
1283             expectation
1284                 .onCell(1, 1, "4L1C1")
1285                 .onCell(1, 2, "4L1C2")
1286                 .onCell(2, 1, "4L2C1")
1287                 .onCell(2, 2, "4L2C2")
1288                 .onCell(3, 1, "4L3C1")
1289                 .onCell(3, 2, "4L3C2")
1290                 .onCell(4, 1, "4L4C1")
1291                 .onCell(4, 2, "4L4C2");
1292             chosenOne = "P4D2";
1293         }
1294 
1295         focusCell(4, 1);
1296         mUiBot.selectDataset(chosenOne);
1297         expectation.assertAutoFilled();
1298     }
1299 
1300     @Test
testAutofillMultipleAuthDatasetsInSequence()1301     public void testAutofillMultipleAuthDatasetsInSequence() throws Exception {
1302         // Set service.
1303         enableService();
1304 
1305         /**
1306          * 1st partition.
1307          */
1308         // Set expectations.
1309         final IntentSender auth11 = AuthenticationActivity.createSender(getContext(), 11,
1310                 new CannedDataset.Builder()
1311                         .setField(ID_L1C1, "l1c1")
1312                         .setField(ID_L1C2, "l1c2")
1313                         .build());
1314         final IntentSender auth12 = AuthenticationActivity.createSender(getContext(), 12);
1315         final CannedFillResponse response1 = new CannedFillResponse.Builder()
1316                 .addDataset(new CannedDataset.Builder()
1317                         .setAuthentication(auth11)
1318                         .setField(ID_L1C1, UNUSED_AUTOFILL_VALUE)
1319                         .setField(ID_L1C2, UNUSED_AUTOFILL_VALUE)
1320                         .setPresentation(createPresentation("P1D1"))
1321                         .build())
1322                 .addDataset(new CannedDataset.Builder()
1323                         .setAuthentication(auth12)
1324                         .setField(ID_L1C1, UNUSED_AUTOFILL_VALUE)
1325                         .setPresentation(createPresentation("P1D2"))
1326                         .build())
1327                 .build();
1328         sReplier.addResponse(response1);
1329         final FillExpectation expectation1 = mActivity.expectAutofill()
1330                 .onCell(1, 1, "l1c1")
1331                 .onCell(1, 2, "l1c2");
1332 
1333         // Trigger partition.
1334         focusCell(1, 1);
1335         sReplier.getNextFillRequest();
1336 
1337         // Focus around different fields in the partition.
1338         mUiBot.assertDatasets("P1D1", "P1D2");
1339         focusCell(1, 2);
1340         mUiBot.assertDatasets("P1D1");
1341 
1342         // Autofill it...
1343         mUiBot.selectDataset("P1D1");
1344         // ... and assert result
1345         expectation1.assertAutoFilled();
1346 
1347         /**
1348          * 2nd partition.
1349          */
1350         // Set expectations.
1351         final IntentSender auth21 = AuthenticationActivity.createSender(getContext(), 21);
1352         final IntentSender auth22 = AuthenticationActivity.createSender(getContext(), 22,
1353                 new CannedDataset.Builder()
1354                     .setField(ID_L2C2, "L2C2")
1355                     .build());
1356         final CannedFillResponse response2 = new CannedFillResponse.Builder()
1357                 .addDataset(new CannedDataset.Builder()
1358                         .setAuthentication(auth21)
1359                         .setPresentation(createPresentation("P2D1"))
1360                         .setField(ID_L2C1, UNUSED_AUTOFILL_VALUE)
1361                         .setField(ID_L2C2, UNUSED_AUTOFILL_VALUE)
1362                         .build())
1363                 .addDataset(new CannedDataset.Builder()
1364                         .setAuthentication(auth22)
1365                         .setPresentation(createPresentation("P2D2"))
1366                         .setField(ID_L2C2, UNUSED_AUTOFILL_VALUE)
1367                         .build())
1368                 .build();
1369         sReplier.addResponse(response2);
1370         final FillExpectation expectation2 = mActivity.expectAutofill()
1371                 .onCell(2, 2, "L2C2");
1372 
1373         // Trigger partition.
1374         focusCell(2, 1);
1375         sReplier.getNextFillRequest();
1376 
1377         // Focus around different fields in the partition.
1378         mUiBot.assertDatasets("P2D1");
1379         focusCell(2, 2);
1380         mUiBot.assertDatasets("P2D1", "P2D2");
1381 
1382         // Autofill it...
1383         mUiBot.selectDataset("P2D2");
1384         // ... and assert result
1385         expectation2.assertAutoFilled();
1386 
1387         /**
1388          * 3rd partition.
1389          */
1390         // Set expectations.
1391         final IntentSender auth31 = AuthenticationActivity.createSender(getContext(), 31,
1392                 new CannedDataset.Builder()
1393                         .setField(ID_L3C1, "l3c1")
1394                         .setField(ID_L3C2, "l3c2")
1395                         .build());
1396         final IntentSender auth32 = AuthenticationActivity.createSender(getContext(), 32);
1397         final CannedFillResponse response3 = new CannedFillResponse.Builder()
1398                 .addDataset(new CannedDataset.Builder()
1399                         .setAuthentication(auth31)
1400                         .setPresentation(createPresentation("P3D1"))
1401                         .setField(ID_L3C1, UNUSED_AUTOFILL_VALUE)
1402                         .setField(ID_L3C2, UNUSED_AUTOFILL_VALUE)
1403                         .build())
1404                 .addDataset(new CannedDataset.Builder()
1405                         .setAuthentication(auth32)
1406                         .setPresentation(createPresentation("P3D2"))
1407                         .setField(ID_L3C1, UNUSED_AUTOFILL_VALUE)
1408                         .setField(ID_L3C2, UNUSED_AUTOFILL_VALUE)
1409                         .build())
1410                 .build();
1411         sReplier.addResponse(response3);
1412         final FillExpectation expectation3 = mActivity.expectAutofill()
1413                 .onCell(3, 1, "l3c1")
1414                 .onCell(3, 2, "l3c2");
1415 
1416         // Trigger partition.
1417         focusCell(3, 2);
1418         sReplier.getNextFillRequest();
1419 
1420         // Focus around different fields in the partition.
1421         mUiBot.assertDatasets("P3D1", "P3D2");
1422         focusCell(3, 1);
1423         mUiBot.assertDatasets("P3D1", "P3D2");
1424 
1425         // Autofill it...
1426         mUiBot.selectDataset("P3D1");
1427         // ... and assert result
1428         expectation3.assertAutoFilled();
1429 
1430         /**
1431          * 4th partition.
1432          */
1433         // Set expectations.
1434         final IntentSender auth41 = AuthenticationActivity.createSender(getContext(), 41);
1435         final IntentSender auth42 = AuthenticationActivity.createSender(getContext(), 42,
1436                 new CannedDataset.Builder()
1437                     .setField(ID_L4C1, "L4C1")
1438                     .setField(ID_L4C2, "L4C2")
1439                     .build());
1440         final CannedFillResponse response4 = new CannedFillResponse.Builder()
1441                 .addDataset(new CannedDataset.Builder()
1442                         .setAuthentication(auth41)
1443                         .setPresentation(createPresentation("P4D1"))
1444                         .setField(ID_L4C1, UNUSED_AUTOFILL_VALUE)
1445                         .build())
1446                 .addDataset(new CannedDataset.Builder()
1447                         .setAuthentication(auth42)
1448                         .setPresentation(createPresentation("P4D2"))
1449                         .setField(ID_L4C1, UNUSED_AUTOFILL_VALUE)
1450                         .setField(ID_L4C2, UNUSED_AUTOFILL_VALUE)
1451                         .build())
1452                 .build();
1453         sReplier.addResponse(response4);
1454         final FillExpectation expectation4 = mActivity.expectAutofill()
1455                 .onCell(4, 1, "L4C1")
1456                 .onCell(4, 2, "L4C2");
1457 
1458         // Trigger partition.
1459         focusCell(4, 1);
1460         sReplier.getNextFillRequest();
1461 
1462         // Focus around different fields in the partition.
1463         mUiBot.assertDatasets("P4D1", "P4D2");
1464         focusCell(4, 2);
1465         mUiBot.assertDatasets("P4D2");
1466 
1467         // Autofill it...
1468         mUiBot.selectDataset("P4D2");
1469         // ... and assert result
1470         expectation4.assertAutoFilled();
1471     }
1472 
1473     /**
1474      * Tests scenario where each partition has more than one dataset and all datasets require auth,
1475      * but they don't overlap, i.e., each {@link FillResponse} only contain fields within the
1476      * partition.
1477      */
1478     @Test
testAutofillMultipleAuthDatasetsNoOverlap()1479     public void testAutofillMultipleAuthDatasetsNoOverlap() throws Exception {
1480         // Set service.
1481         enableService();
1482 
1483         /**
1484          * 1st partition.
1485          */
1486         // Set expectations.
1487         final IntentSender auth11 = AuthenticationActivity.createSender(getContext(), 11,
1488                 new CannedDataset.Builder()
1489                         .setField(ID_L1C1, "l1c1")
1490                         .setField(ID_L1C2, "l1c2")
1491                         .build());
1492         final IntentSender auth12 = AuthenticationActivity.createSender(getContext(), 12);
1493         final CannedFillResponse response1 = new CannedFillResponse.Builder()
1494                 .addDataset(new CannedDataset.Builder()
1495                         .setAuthentication(auth11)
1496                         .setField(ID_L1C1, UNUSED_AUTOFILL_VALUE)
1497                         .setField(ID_L1C2, UNUSED_AUTOFILL_VALUE)
1498                         .setPresentation(createPresentation("P1D1"))
1499                         .build())
1500                 .addDataset(new CannedDataset.Builder()
1501                         .setAuthentication(auth12)
1502                         .setField(ID_L1C1, UNUSED_AUTOFILL_VALUE)
1503                         .setPresentation(createPresentation("P1D2"))
1504                         .build())
1505                 .build();
1506         sReplier.addResponse(response1);
1507         final FillExpectation expectation1 = mActivity.expectAutofill()
1508                 .onCell(1, 1, "l1c1")
1509                 .onCell(1, 2, "l1c2");
1510 
1511         // Trigger partition.
1512         focusCell(1, 1);
1513         sReplier.getNextFillRequest();
1514 
1515         /**
1516          * 2nd partition.
1517          */
1518         // Set expectations.
1519         final IntentSender auth21 = AuthenticationActivity.createSender(getContext(), 21);
1520         final IntentSender auth22 = AuthenticationActivity.createSender(getContext(), 22,
1521                 new CannedDataset.Builder()
1522                     .setField(ID_L2C2, "L2C2")
1523                     .build());
1524         final CannedFillResponse response2 = new CannedFillResponse.Builder()
1525                 .addDataset(new CannedDataset.Builder()
1526                         .setAuthentication(auth21)
1527                         .setPresentation(createPresentation("P2D1"))
1528                         .setField(ID_L2C1, UNUSED_AUTOFILL_VALUE)
1529                         .setField(ID_L2C2, UNUSED_AUTOFILL_VALUE)
1530                         .build())
1531                 .addDataset(new CannedDataset.Builder()
1532                         .setAuthentication(auth22)
1533                         .setPresentation(createPresentation("P2D2"))
1534                         .setField(ID_L2C2, UNUSED_AUTOFILL_VALUE)
1535                         .build())
1536                 .build();
1537         sReplier.addResponse(response2);
1538         final FillExpectation expectation2 = mActivity.expectAutofill()
1539                 .onCell(2, 2, "L2C2");
1540 
1541         // Trigger partition.
1542         focusCell(2, 1);
1543         sReplier.getNextFillRequest();
1544 
1545         /**
1546          * 3rd partition.
1547          */
1548         // Set expectations.
1549         final IntentSender auth31 = AuthenticationActivity.createSender(getContext(), 31,
1550                 new CannedDataset.Builder()
1551                         .setField(ID_L3C1, "l3c1")
1552                         .setField(ID_L3C2, "l3c2")
1553                         .build());
1554         final IntentSender auth32 = AuthenticationActivity.createSender(getContext(), 32);
1555         final CannedFillResponse response3 = new CannedFillResponse.Builder()
1556                 .addDataset(new CannedDataset.Builder()
1557                         .setAuthentication(auth31)
1558                         .setPresentation(createPresentation("P3D1"))
1559                         .setField(ID_L3C1, UNUSED_AUTOFILL_VALUE)
1560                         .setField(ID_L3C2, UNUSED_AUTOFILL_VALUE)
1561                         .build())
1562                 .addDataset(new CannedDataset.Builder()
1563                         .setAuthentication(auth32)
1564                         .setPresentation(createPresentation("P3D2"))
1565                         .setField(ID_L3C1, UNUSED_AUTOFILL_VALUE)
1566                         .setField(ID_L3C2, UNUSED_AUTOFILL_VALUE)
1567                         .build())
1568                 .build();
1569         sReplier.addResponse(response3);
1570         final FillExpectation expectation3 = mActivity.expectAutofill()
1571                 .onCell(3, 1, "l3c1")
1572                 .onCell(3, 2, "l3c2");
1573 
1574         // Trigger partition.
1575         focusCell(3, 2);
1576         sReplier.getNextFillRequest();
1577 
1578         /**
1579          * 4th partition.
1580          */
1581         // Set expectations.
1582         final IntentSender auth41 = AuthenticationActivity.createSender(getContext(), 41);
1583         final IntentSender auth42 = AuthenticationActivity.createSender(getContext(), 42,
1584                 new CannedDataset.Builder()
1585                     .setField(ID_L4C1, "L4C1")
1586                     .setField(ID_L4C2, "L4C2")
1587                     .build());
1588         final CannedFillResponse response4 = new CannedFillResponse.Builder()
1589                 .addDataset(new CannedDataset.Builder()
1590                         .setAuthentication(auth41)
1591                         .setPresentation(createPresentation("P4D1"))
1592                         .setField(ID_L4C1, UNUSED_AUTOFILL_VALUE)
1593                         .build())
1594                 .addDataset(new CannedDataset.Builder()
1595                         .setAuthentication(auth42)
1596                         .setPresentation(createPresentation("P4D2"))
1597                         .setField(ID_L4C1, UNUSED_AUTOFILL_VALUE)
1598                         .setField(ID_L4C2, UNUSED_AUTOFILL_VALUE)
1599                         .build())
1600                 .build();
1601         sReplier.addResponse(response4);
1602         final FillExpectation expectation4 = mActivity.expectAutofill()
1603                 .onCell(4, 1, "L4C1")
1604                 .onCell(4, 2, "L4C2");
1605 
1606         focusCell(4, 1);
1607         sReplier.getNextFillRequest();
1608 
1609         /*
1610          *  Now move focus around to make sure the proper values are displayed each time.
1611          */
1612         focusCell(1, 1);
1613         mUiBot.assertDatasets("P1D1", "P1D2");
1614         focusCell(1, 2);
1615         mUiBot.assertDatasets("P1D1");
1616 
1617         focusCell(2, 1);
1618         mUiBot.assertDatasets("P2D1");
1619         focusCell(2, 2);
1620         mUiBot.assertDatasets("P2D1", "P2D2");
1621 
1622         focusCell(4, 1);
1623         mUiBot.assertDatasets("P4D1", "P4D2");
1624         focusCell(4, 2);
1625         mUiBot.assertDatasets("P4D2");
1626 
1627         focusCell(3, 2);
1628         mUiBot.assertDatasets("P3D1", "P3D2");
1629         focusCell(3, 1);
1630         mUiBot.assertDatasets("P3D1", "P3D2");
1631 
1632         /*
1633          *  Finally, autofill and check results.
1634          */
1635         focusCell(4, 1);
1636         mUiBot.selectDataset("P4D2");
1637         expectation4.assertAutoFilled();
1638 
1639         focusCell(1, 1);
1640         mUiBot.selectDataset("P1D1");
1641         expectation1.assertAutoFilled();
1642 
1643         focusCell(3, 1);
1644         mUiBot.selectDataset("P3D1");
1645         expectation3.assertAutoFilled();
1646 
1647         focusCell(2, 2);
1648         mUiBot.selectDataset("P2D2");
1649         expectation2.assertAutoFilled();
1650     }
1651 
1652     /**
1653      * Tests scenario where each partition has more than one dataset and some datasets require auth,
1654      * but they don't overlap, i.e., each {@link FillResponse} only contain fields within the
1655      * partition.
1656      */
1657     @Test
testAutofillMultipleDatasetsMixedAuthNoAuthNoOverlap()1658     public void testAutofillMultipleDatasetsMixedAuthNoAuthNoOverlap() throws Exception {
1659         // Set service.
1660         enableService();
1661 
1662         /**
1663          * 1st partition.
1664          */
1665         // Set expectations.
1666         final IntentSender auth12 = AuthenticationActivity.createSender(getContext(), 12);
1667         final CannedFillResponse response1 = new CannedFillResponse.Builder()
1668                 .addDataset(new CannedDataset.Builder()
1669                         .setField(ID_L1C1, "l1c1")
1670                         .setField(ID_L1C2, "l1c2")
1671                         .setPresentation(createPresentation("P1D1"))
1672                         .build())
1673                 .addDataset(new CannedDataset.Builder()
1674                         .setAuthentication(auth12)
1675                         .setField(ID_L1C1, UNUSED_AUTOFILL_VALUE)
1676                         .setPresentation(createPresentation("P1D2"))
1677                         .build())
1678                 .build();
1679         sReplier.addResponse(response1);
1680         final FillExpectation expectation1 = mActivity.expectAutofill()
1681                 .onCell(1, 1, "l1c1")
1682                 .onCell(1, 2, "l1c2");
1683 
1684         // Trigger partition.
1685         focusCell(1, 1);
1686         sReplier.getNextFillRequest();
1687 
1688         /**
1689          * 2nd partition.
1690          */
1691         // Set expectations.
1692         final IntentSender auth22 = AuthenticationActivity.createSender(getContext(), 22,
1693                 new CannedDataset.Builder()
1694                     .setField(ID_L2C2, "L2C2")
1695                     .build());
1696         final CannedFillResponse response2 = new CannedFillResponse.Builder()
1697                 .addDataset(new CannedDataset.Builder()
1698                         .setPresentation(createPresentation("P2D1"))
1699                         .setField(ID_L2C1, "l2c1")
1700                         .setField(ID_L2C2, "l2c2")
1701                         .build())
1702                 .addDataset(new CannedDataset.Builder()
1703                         .setAuthentication(auth22)
1704                         .setPresentation(createPresentation("P2D2"))
1705                         .setField(ID_L2C2, UNUSED_AUTOFILL_VALUE)
1706                         .build())
1707                 .build();
1708         sReplier.addResponse(response2);
1709         final FillExpectation expectation2 = mActivity.expectAutofill()
1710                 .onCell(2, 2, "L2C2");
1711 
1712         // Trigger partition.
1713         focusCell(2, 1);
1714         sReplier.getNextFillRequest();
1715 
1716         /**
1717          * 3rd partition.
1718          */
1719         // Set expectations.
1720         final IntentSender auth31 = AuthenticationActivity.createSender(getContext(), 31,
1721                 new CannedDataset.Builder()
1722                         .setField(ID_L3C1, "l3c1")
1723                         .setField(ID_L3C2, "l3c2")
1724                         .build());
1725         final CannedFillResponse response3 = new CannedFillResponse.Builder()
1726                 .addDataset(new CannedDataset.Builder()
1727                         .setAuthentication(auth31)
1728                         .setPresentation(createPresentation("P3D1"))
1729                         .setField(ID_L3C1, UNUSED_AUTOFILL_VALUE)
1730                         .setField(ID_L3C2, UNUSED_AUTOFILL_VALUE)
1731                         .build())
1732                 .addDataset(new CannedDataset.Builder()
1733                         .setPresentation(createPresentation("P3D2"))
1734                         .setField(ID_L3C1, "L3C1")
1735                         .setField(ID_L3C2, "L3C2")
1736                         .build())
1737                 .build();
1738         sReplier.addResponse(response3);
1739         final FillExpectation expectation3 = mActivity.expectAutofill()
1740                 .onCell(3, 1, "l3c1")
1741                 .onCell(3, 2, "l3c2");
1742 
1743         // Trigger partition.
1744         focusCell(3, 2);
1745         sReplier.getNextFillRequest();
1746 
1747         /**
1748          * 4th partition.
1749          */
1750         // Set expectations.
1751         final IntentSender auth41 = AuthenticationActivity.createSender(getContext(), 41);
1752         final CannedFillResponse response4 = new CannedFillResponse.Builder()
1753                 .addDataset(new CannedDataset.Builder()
1754                         .setAuthentication(auth41)
1755                         .setPresentation(createPresentation("P4D1"))
1756                         .setField(ID_L4C1, UNUSED_AUTOFILL_VALUE)
1757                         .build())
1758                 .addDataset(new CannedDataset.Builder()
1759                         .setPresentation(createPresentation("P4D2"))
1760                         .setField(ID_L4C1, "L4C1")
1761                         .setField(ID_L4C2, "L4C2")
1762                         .build())
1763                 .build();
1764         sReplier.addResponse(response4);
1765         final FillExpectation expectation4 = mActivity.expectAutofill()
1766                 .onCell(4, 1, "L4C1")
1767                 .onCell(4, 2, "L4C2");
1768 
1769         focusCell(4, 1);
1770         sReplier.getNextFillRequest();
1771 
1772         /*
1773          *  Now move focus around to make sure the proper values are displayed each time.
1774          */
1775         focusCell(1, 1);
1776         mUiBot.assertDatasets("P1D1", "P1D2");
1777         focusCell(1, 2);
1778         mUiBot.assertDatasets("P1D1");
1779 
1780         focusCell(2, 1);
1781         mUiBot.assertDatasets("P2D1");
1782         focusCell(2, 2);
1783         mUiBot.assertDatasets("P2D1", "P2D2");
1784 
1785         focusCell(4, 1);
1786         mUiBot.assertDatasets("P4D1", "P4D2");
1787         focusCell(4, 2);
1788         mUiBot.assertDatasets("P4D2");
1789 
1790         focusCell(3, 2);
1791         mUiBot.assertDatasets("P3D1", "P3D2");
1792         focusCell(3, 1);
1793         mUiBot.assertDatasets("P3D1", "P3D2");
1794 
1795         /*
1796          *  Finally, autofill and check results.
1797          */
1798         focusCell(4, 1);
1799         mUiBot.selectDataset("P4D2");
1800         expectation4.assertAutoFilled();
1801 
1802         focusCell(1, 1);
1803         mUiBot.selectDataset("P1D1");
1804         expectation1.assertAutoFilled();
1805 
1806         focusCell(3, 1);
1807         mUiBot.selectDataset("P3D1");
1808         expectation3.assertAutoFilled();
1809 
1810         focusCell(2, 2);
1811         mUiBot.selectDataset("P2D2");
1812         expectation2.assertAutoFilled();
1813     }
1814 
1815     /**
1816      * Tests scenario where each partition has more than one dataset - some authenticated and some
1817      * not - but they overlap, i.e., some fields are present in more than one partition.
1818      *
1819      * <p>Whenever a new partition defines a field previously present in another partittion, that
1820      * partition will "own" that field.
1821      *
1822      * <p>In the end, 4th partition will one all fields in 2 datasets; and this test cases picks
1823      * the first.
1824      */
1825     @Test
testAutofillMultipleAuthDatasetsOverlapPickFirst()1826     public void testAutofillMultipleAuthDatasetsOverlapPickFirst() throws Exception {
1827         autofillMultipleAuthDatasetsOverlapping(true);
1828     }
1829 
1830     /**
1831      * Tests scenario where each partition has more than one dataset - some authenticated and some
1832      * not - but they overlap, i.e., some fields are present in more than one partition.
1833      *
1834      * <p>Whenever a new partition defines a field previously present in another partittion, that
1835      * partition will "own" that field.
1836      *
1837      * <p>In the end, 4th partition will one all fields in 2 datasets; and this test cases picks
1838      * the second.
1839      */
1840     @Test
testAutofillMultipleAuthDatasetsOverlapPickSecond()1841     public void testAutofillMultipleAuthDatasetsOverlapPickSecond() throws Exception {
1842         autofillMultipleAuthDatasetsOverlapping(false);
1843     }
1844 
autofillMultipleAuthDatasetsOverlapping(boolean pickFirst)1845     private void autofillMultipleAuthDatasetsOverlapping(boolean pickFirst) throws Exception {
1846         // Set service.
1847         enableService();
1848 
1849         /**
1850          * 1st partition.
1851          */
1852         // Set expectations.
1853         final IntentSender auth12 = AuthenticationActivity.createSender(getContext(), 12);
1854         final CannedFillResponse response1 = new CannedFillResponse.Builder()
1855                 .addDataset(new CannedDataset.Builder()
1856                         .setField(ID_L1C1, "1l1c1")
1857                         .setField(ID_L1C2, "1l1c2")
1858                         .setPresentation(createPresentation("P1D1"))
1859                         .build())
1860                 .addDataset(new CannedDataset.Builder()
1861                         .setAuthentication(auth12)
1862                         .setField(ID_L1C1, UNUSED_AUTOFILL_VALUE)
1863                         .setPresentation(createPresentation("P1D2"))
1864                         .build())
1865                 .build();
1866         sReplier.addResponse(response1);
1867         // Trigger partition.
1868         focusCell(1, 1);
1869         sReplier.getNextFillRequest();
1870 
1871         // Asserts proper datasets are shown on each field defined so far.
1872         mUiBot.assertDatasets("P1D1", "P1D2");
1873         focusCell(1, 2);
1874         mUiBot.assertDatasets("P1D1");
1875 
1876         /**
1877          * 2nd partition.
1878          */
1879         // Set expectations.
1880         final IntentSender auth21 = AuthenticationActivity.createSender(getContext(), 22,
1881                 new CannedDataset.Builder()
1882                     .setField(ID_L1C1, "2l1c1") // from previous partition
1883                     .setField(ID_L2C1, "2l2c1")
1884                     .setField(ID_L2C2, "2l2c2")
1885                     .build());
1886         final CannedFillResponse response2 = new CannedFillResponse.Builder()
1887                 .addDataset(new CannedDataset.Builder()
1888                         .setAuthentication(auth21)
1889                         .setPresentation(createPresentation("P2D1"))
1890                         .setField(ID_L1C1, UNUSED_AUTOFILL_VALUE) // from previous partition
1891                         .setField(ID_L2C1, UNUSED_AUTOFILL_VALUE)
1892                         .setField(ID_L2C2, UNUSED_AUTOFILL_VALUE)
1893                         .build())
1894                 .addDataset(new CannedDataset.Builder()
1895                         .setPresentation(createPresentation("P2D2"))
1896                         .setField(ID_L2C2, "2L2C2")
1897                         .build())
1898                 .build();
1899         sReplier.addResponse(response2);
1900 
1901         // Trigger partition.
1902         focusCell(2, 1);
1903         sReplier.getNextFillRequest();
1904 
1905         // Asserts proper datasets are shown on each field defined so far.
1906         focusCell(1, 1);
1907         mUiBot.assertDatasets("P2D1"); // changed
1908         focusCell(1, 2);
1909         mUiBot.assertDatasets("P1D1");
1910         focusCell(2, 1);
1911         mUiBot.assertDatasets("P2D1");
1912         focusCell(2, 2);
1913         mUiBot.assertDatasets("P2D1", "P2D2");
1914 
1915         /**
1916          * 3rd partition.
1917          */
1918         // Set expectations.
1919         final IntentSender auth31 = AuthenticationActivity.createSender(getContext(), 31,
1920                 new CannedDataset.Builder()
1921                         .setField(ID_L1C2, "3l1c2") // from previous partition
1922                         .setField(ID_L3C1, "3l3c1")
1923                         .setField(ID_L3C2, "3l3c2")
1924                         .build());
1925         final IntentSender auth32 = AuthenticationActivity.createSender(getContext(), 32);
1926         final CannedFillResponse response3 = new CannedFillResponse.Builder()
1927                 .addDataset(new CannedDataset.Builder()
1928                         .setAuthentication(auth31)
1929                         .setPresentation(createPresentation("P3D1"))
1930                         .setField(ID_L1C2, UNUSED_AUTOFILL_VALUE) // from previous partition
1931                         .setField(ID_L3C1, UNUSED_AUTOFILL_VALUE)
1932                         .setField(ID_L3C2, UNUSED_AUTOFILL_VALUE)
1933                         .build())
1934                 .addDataset(new CannedDataset.Builder()
1935                         .setAuthentication(auth32)
1936                         .setPresentation(createPresentation("P3D2"))
1937                         .setField(ID_L2C2, UNUSED_AUTOFILL_VALUE) // from previous partition
1938                         .setField(ID_L3C1, UNUSED_AUTOFILL_VALUE)
1939                         .setField(ID_L3C2, UNUSED_AUTOFILL_VALUE)
1940                         .build())
1941                 .build();
1942         sReplier.addResponse(response3);
1943 
1944         // Trigger partition.
1945         focusCell(3, 1);
1946         sReplier.getNextFillRequest();
1947 
1948         // Asserts proper datasets are shown on each field defined so far.
1949         focusCell(1, 1);
1950         mUiBot.assertDatasets("P2D1");
1951         focusCell(1, 2);
1952         mUiBot.assertDatasets("P3D1"); // changed
1953         focusCell(2, 1);
1954         mUiBot.assertDatasets("P2D1");
1955         focusCell(2, 2);
1956         mUiBot.assertDatasets("P3D2"); // changed
1957         focusCell(3, 2);
1958         mUiBot.assertDatasets("P3D1", "P3D2");
1959         focusCell(3, 1);
1960         mUiBot.assertDatasets("P3D1", "P3D2");
1961 
1962         /**
1963          * 4th partition.
1964          */
1965         // Set expectations.
1966         final IntentSender auth41 = AuthenticationActivity.createSender(getContext(), 41,
1967                 new CannedDataset.Builder()
1968                         .setField(ID_L1C1, "4l1c1") // from previous partition
1969                         .setField(ID_L1C2, "4l1c2") // from previous partition
1970                         .setField(ID_L2C1, "4l2c1") // from previous partition
1971                         .setField(ID_L2C2, "4l2c2") // from previous partition
1972                         .setField(ID_L3C1, "4l3c1") // from previous partition
1973                         .setField(ID_L3C2, "4l3c2") // from previous partition
1974                         .setField(ID_L4C1, "4l4c1")
1975                         .build());
1976         final IntentSender auth42 = AuthenticationActivity.createSender(getContext(), 42,
1977                 new CannedDataset.Builder()
1978                         .setField(ID_L1C1, "4L1C1") // from previous partition
1979                         .setField(ID_L1C2, "4L1C2") // from previous partition
1980                         .setField(ID_L2C1, "4L2C1") // from previous partition
1981                         .setField(ID_L2C2, "4L2C2") // from previous partition
1982                         .setField(ID_L3C1, "4L3C1") // from previous partition
1983                         .setField(ID_L3C2, "4L3C2") // from previous partition
1984                         .setField(ID_L1C1, "4L1C1") // from previous partition
1985                         .setField(ID_L4C1, "4L4C1")
1986                         .setField(ID_L4C2, "4L4C2")
1987                         .build());
1988         final CannedFillResponse response4 = new CannedFillResponse.Builder()
1989                 .addDataset(new CannedDataset.Builder()
1990                         .setAuthentication(auth41)
1991                         .setPresentation(createPresentation("P4D1"))
1992                         .setField(ID_L1C1, UNUSED_AUTOFILL_VALUE) // from previous partition
1993                         .setField(ID_L1C2, UNUSED_AUTOFILL_VALUE) // from previous partition
1994                         .setField(ID_L2C1, UNUSED_AUTOFILL_VALUE) // from previous partition
1995                         .setField(ID_L2C2, UNUSED_AUTOFILL_VALUE) // from previous partition
1996                         .setField(ID_L3C1, UNUSED_AUTOFILL_VALUE) // from previous partition
1997                         .setField(ID_L3C2, UNUSED_AUTOFILL_VALUE) // from previous partition
1998                         .setField(ID_L4C1, UNUSED_AUTOFILL_VALUE)
1999                         .build())
2000                 .addDataset(new CannedDataset.Builder()
2001                         .setAuthentication(auth42)
2002                         .setPresentation(createPresentation("P4D2"))
2003                         .setField(ID_L1C1, UNUSED_AUTOFILL_VALUE) // from previous partition
2004                         .setField(ID_L1C2, UNUSED_AUTOFILL_VALUE) // from previous partition
2005                         .setField(ID_L2C1, UNUSED_AUTOFILL_VALUE) // from previous partition
2006                         .setField(ID_L2C2, UNUSED_AUTOFILL_VALUE) // from previous partition
2007                         .setField(ID_L3C1, UNUSED_AUTOFILL_VALUE) // from previous partition
2008                         .setField(ID_L3C2, UNUSED_AUTOFILL_VALUE) // from previous partition
2009                         .setField(ID_L1C1, UNUSED_AUTOFILL_VALUE) // from previous partition
2010                         .setField(ID_L4C1, UNUSED_AUTOFILL_VALUE)
2011                         .setField(ID_L4C2, UNUSED_AUTOFILL_VALUE)
2012                         .build())
2013                 .build();
2014         sReplier.addResponse(response4);
2015 
2016         // Trigger partition.
2017         focusCell(4, 1);
2018         sReplier.getNextFillRequest();
2019 
2020         // Asserts proper datasets are shown on each field defined so far.
2021         focusCell(1, 1);
2022         mUiBot.assertDatasets("P4D1", "P4D2");
2023         focusCell(1, 2);
2024         mUiBot.assertDatasets("P4D1", "P4D2");
2025         focusCell(2, 1);
2026         mUiBot.assertDatasets("P4D1", "P4D2");
2027         focusCell(2, 2);
2028         mUiBot.assertDatasets("P4D1", "P4D2");
2029         focusCell(3, 2);
2030         mUiBot.assertDatasets("P4D1", "P4D2");
2031         focusCell(3, 1);
2032         mUiBot.assertDatasets("P4D1", "P4D2");
2033         focusCell(4, 1);
2034         mUiBot.assertDatasets("P4D1", "P4D2");
2035         focusCell(4, 2);
2036         mUiBot.assertDatasets("P4D2");
2037 
2038         /*
2039          * Finally, autofill and check results.
2040          */
2041         final FillExpectation expectation = mActivity.expectAutofill();
2042         final String chosenOne;
2043         if (pickFirst) {
2044             expectation
2045                 .onCell(1, 1, "4l1c1")
2046                 .onCell(1, 2, "4l1c2")
2047                 .onCell(2, 1, "4l2c1")
2048                 .onCell(2, 2, "4l2c2")
2049                 .onCell(3, 1, "4l3c1")
2050                 .onCell(3, 2, "4l3c2")
2051                 .onCell(4, 1, "4l4c1");
2052             chosenOne = "P4D1";
2053         } else {
2054             expectation
2055                 .onCell(1, 1, "4L1C1")
2056                 .onCell(1, 2, "4L1C2")
2057                 .onCell(2, 1, "4L2C1")
2058                 .onCell(2, 2, "4L2C2")
2059                 .onCell(3, 1, "4L3C1")
2060                 .onCell(3, 2, "4L3C2")
2061                 .onCell(4, 1, "4L4C1")
2062                 .onCell(4, 2, "4L4C2");
2063             chosenOne = "P4D2";
2064         }
2065 
2066         focusCell(4, 1);
2067         mUiBot.selectDataset(chosenOne);
2068         expectation.assertAutoFilled();
2069     }
2070 
2071     @Test
testAutofillAllResponsesAuthenticated()2072     public void testAutofillAllResponsesAuthenticated() throws Exception {
2073         // Set service.
2074         enableService();
2075 
2076         // Prepare 1st partition.
2077         final IntentSender auth1 = AuthenticationActivity.createSender(getContext(), 1,
2078                 new CannedFillResponse.Builder()
2079                         .addDataset(new CannedDataset.Builder()
2080                                 .setPresentation(createPresentation("Partition 1"))
2081                                 .setField(ID_L1C1, "l1c1")
2082                                 .setField(ID_L1C2, "l1c2")
2083                                 .build())
2084                         .build());
2085         final CannedFillResponse response1 = new CannedFillResponse.Builder()
2086                 .setPresentation(createPresentation("Auth 1"))
2087                 .setAuthentication(auth1, ID_L1C1, ID_L1C2)
2088                 .build();
2089         sReplier.addResponse(response1);
2090         final FillExpectation expectation1 = mActivity.expectAutofill()
2091                 .onCell(1, 1, "l1c1")
2092                 .onCell(1, 2, "l1c2");
2093         focusCell(1, 1);
2094         sReplier.getNextFillRequest();
2095 
2096         mUiBot.assertDatasets("Auth 1");
2097 
2098         // Prepare 2nd partition.
2099         final IntentSender auth2 = AuthenticationActivity.createSender(getContext(), 2,
2100                 new CannedFillResponse.Builder()
2101                         .addDataset(new CannedDataset.Builder()
2102                                 .setPresentation(createPresentation("Partition 2"))
2103                                 .setField(ID_L2C1, "l2c1")
2104                                 .setField(ID_L2C2, "l2c2")
2105                                 .build())
2106                         .build());
2107         final CannedFillResponse response2 = new CannedFillResponse.Builder()
2108                 .setPresentation(createPresentation("Auth 2"))
2109                 .setAuthentication(auth2, ID_L2C1, ID_L2C2)
2110                 .build();
2111         sReplier.addResponse(response2);
2112         final FillExpectation expectation2 = mActivity.expectAutofill()
2113                 .onCell(2, 1, "l2c1")
2114                 .onCell(2, 2, "l2c2");
2115         focusCell(2, 1);
2116         sReplier.getNextFillRequest();
2117 
2118         mUiBot.assertDatasets("Auth 2");
2119 
2120         // Prepare 3rd partition.
2121         final IntentSender auth3 = AuthenticationActivity.createSender(getContext(), 3,
2122                 new CannedFillResponse.Builder()
2123                         .addDataset(new CannedDataset.Builder()
2124                                 .setPresentation(createPresentation("Partition 3"))
2125                                 .setField(ID_L3C1, "l3c1")
2126                                 .setField(ID_L3C2, "l3c2")
2127                                 .build())
2128                         .build());
2129         final CannedFillResponse response3 = new CannedFillResponse.Builder()
2130                 .setPresentation(createPresentation("Auth 3"))
2131                 .setAuthentication(auth3, ID_L3C1, ID_L3C2)
2132                 .build();
2133         sReplier.addResponse(response3);
2134         final FillExpectation expectation3 = mActivity.expectAutofill()
2135                 .onCell(3, 1, "l3c1")
2136                 .onCell(3, 2, "l3c2");
2137         focusCell(3, 1);
2138         sReplier.getNextFillRequest();
2139 
2140         mUiBot.assertDatasets("Auth 3");
2141 
2142         // Prepare 4th partition.
2143         final IntentSender auth4 = AuthenticationActivity.createSender(getContext(), 4,
2144                 new CannedFillResponse.Builder()
2145                         .addDataset(new CannedDataset.Builder()
2146                                 .setPresentation(createPresentation("Partition 4"))
2147                                 .setField(ID_L4C1, "l4c1")
2148                                 .setField(ID_L4C2, "l4c2")
2149                                 .build())
2150                         .build());
2151         final CannedFillResponse response4 = new CannedFillResponse.Builder()
2152                 .setPresentation(createPresentation("Auth 4"))
2153                 .setAuthentication(auth4, ID_L4C1, ID_L4C2)
2154                 .build();
2155         sReplier.addResponse(response4);
2156         final FillExpectation expectation4 = mActivity.expectAutofill()
2157                 .onCell(4, 1, "l4c1")
2158                 .onCell(4, 2, "l4c2");
2159         focusCell(4, 1);
2160         sReplier.getNextFillRequest();
2161 
2162         mUiBot.assertDatasets("Auth 4");
2163 
2164         // Now play around the focus to make sure they still display the right values.
2165 
2166         focusCell(1, 2);
2167         mUiBot.assertDatasets("Auth 1");
2168         focusCell(1, 1);
2169         mUiBot.assertDatasets("Auth 1");
2170 
2171         focusCell(3, 1);
2172         mUiBot.assertDatasets("Auth 3");
2173         focusCell(3, 2);
2174         mUiBot.assertDatasets("Auth 3");
2175 
2176         focusCell(2, 1);
2177         mUiBot.assertDatasets("Auth 2");
2178         focusCell(4, 2);
2179         mUiBot.assertDatasets("Auth 4");
2180 
2181         focusCell(2, 2);
2182         mUiBot.assertDatasets("Auth 2");
2183         focusCell(4, 1);
2184         mUiBot.assertDatasets("Auth 4");
2185 
2186         // Finally, autofill and check them.
2187         focusCell(2, 1);
2188         mUiBot.selectDataset("Auth 2");
2189         mUiBot.selectDataset("Partition 2");
2190         expectation2.assertAutoFilled();
2191 
2192         focusCell(4, 1);
2193         mUiBot.selectDataset("Auth 4");
2194         mUiBot.selectDataset("Partition 4");
2195         expectation4.assertAutoFilled();
2196 
2197         focusCell(3, 1);
2198         mUiBot.selectDataset("Auth 3");
2199         mUiBot.selectDataset("Partition 3");
2200         expectation3.assertAutoFilled();
2201 
2202         focusCell(1, 1);
2203         mUiBot.selectDataset("Auth 1");
2204         mUiBot.selectDataset("Partition 1");
2205         expectation1.assertAutoFilled();
2206     }
2207 
2208     @Test
testNoMorePartitionsAfterLimitReached()2209     public void testNoMorePartitionsAfterLimitReached() throws Exception {
2210         final int maxBefore = getMaxPartitions();
2211         try {
2212             setMaxPartitions(1);
2213             // Set service.
2214             enableService();
2215 
2216             // Prepare 1st partition.
2217             final CannedFillResponse response1 = new CannedFillResponse.Builder()
2218                     .addDataset(new CannedDataset.Builder()
2219                             .setField(ID_L1C1, "l1c1", createPresentation("l1c1"))
2220                             .setField(ID_L1C2, "l1c2", createPresentation("l1c2"))
2221                             .build())
2222                     .build();
2223             sReplier.addResponse(response1);
2224 
2225             // Trigger autofill.
2226             focusCell(1, 1);
2227             sReplier.getNextFillRequest();
2228 
2229             // Make sure UI is shown, but don't tap it.
2230             mUiBot.assertDatasets("l1c1");
2231             focusCell(1, 2);
2232             mUiBot.assertDatasets("l1c2");
2233 
2234             // Prepare 2nd partition.
2235             final CannedFillResponse response2 = new CannedFillResponse.Builder()
2236                     .addDataset(new CannedDataset.Builder()
2237                             .setField(ID_L2C1, "l2c1", createPresentation("l2c1"))
2238                             .build())
2239                     .build();
2240             sReplier.addResponse(response2);
2241 
2242             // Trigger autofill on 2nd partition.
2243             focusCell(2, 1);
2244 
2245             // Make sure it was ignored.
2246             mUiBot.assertNoDatasets();
2247 
2248             // Make sure 1st partition is still working.
2249             focusCell(1, 2);
2250             mUiBot.assertDatasets("l1c2");
2251             focusCell(1, 1);
2252             mUiBot.assertDatasets("l1c1");
2253 
2254             // Prepare 3rd partition.
2255             final CannedFillResponse response3 = new CannedFillResponse.Builder()
2256                     .addDataset(new CannedDataset.Builder()
2257                             .setField(ID_L3C2, "l3c2", createPresentation("l3c2"))
2258                             .build())
2259                     .build();
2260             sReplier.addResponse(response3);
2261             // Trigger autofill on 3rd partition.
2262             focusCell(3, 2);
2263 
2264             // Make sure it was ignored.
2265             mUiBot.assertNoDatasets();
2266 
2267             // Make sure 1st partition is still working...
2268             focusCell(1, 2);
2269             mUiBot.assertDatasets("l1c2");
2270             focusCell(1, 1);
2271             mUiBot.assertDatasets("l1c1");
2272 
2273             //...and can be autofilled.
2274             final FillExpectation expectation = mActivity.expectAutofill()
2275                     .onCell(1, 1, "l1c1");
2276             mUiBot.selectDataset("l1c1");
2277             expectation.assertAutoFilled();
2278         } finally {
2279             setMaxPartitions(maxBefore);
2280         }
2281     }
2282 }
2283