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