Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright 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.Helper.ID_USERNAME;
     19 import static android.autofillservice.cts.Helper.assertTextIsSanitized;
     20 import static android.autofillservice.cts.SimpleSaveActivity.ID_PASSWORD;
     21 
     22 import android.autofillservice.cts.CannedFillResponse.CannedDataset;
     23 import android.autofillservice.cts.InstrumentedAutoFillService.FillRequest;
     24 import android.support.test.uiautomator.UiObject2;
     25 import android.view.View;
     26 
     27 import org.junit.Before;
     28 import org.junit.Rule;
     29 import org.junit.Test;
     30 
     31 public class DialogLauncherActivityTest extends AutoFillServiceTestCase {
     32 
     33     @Rule
     34     public final AutofillActivityTestRule<DialogLauncherActivity> mActivityRule =
     35             new AutofillActivityTestRule<DialogLauncherActivity>(DialogLauncherActivity.class);
     36 
     37     private DialogLauncherActivity mActivity;
     38 
     39     @Before
     40     public void setActivity() {
     41         mActivity = mActivityRule.getActivity();
     42     }
     43 
     44     @Test
     45     public void testAutofill_noDatasets() throws Exception {
     46         autofillNoDatasetsTest(false);
     47     }
     48 
     49     @Test
     50     public void testAutofill_noDatasets_afterResizing() throws Exception {
     51         autofillNoDatasetsTest(true);
     52     }
     53 
     54     private void autofillNoDatasetsTest(boolean resize) throws Exception {
     55         enableService();
     56         mActivity.launchDialog(mUiBot);
     57 
     58         if (resize) {
     59             mActivity.maximizeDialog();
     60         }
     61 
     62         // Set expectations.
     63         sReplier.addResponse(CannedFillResponse.NO_RESPONSE);
     64 
     65         // Trigger autofill.
     66         mActivity.onUsername(View::requestFocus);
     67         final FillRequest fillRequest = sReplier.getNextFillRequest();
     68 
     69         // Asserts results.
     70         try {
     71             mUiBot.assertNoDatasetsEver();
     72             // Make sure nodes were properly generated.
     73             assertTextIsSanitized(fillRequest.structure, ID_USERNAME);
     74             assertTextIsSanitized(fillRequest.structure, ID_PASSWORD);
     75         } catch (AssertionError e) {
     76             Helper.dumpStructure("D'OH!", fillRequest.structure);
     77             throw e;
     78         }
     79     }
     80 
     81     @Test
     82     public void testAutofill_oneDataset() throws Exception {
     83         autofillOneDatasetTest(false);
     84     }
     85 
     86     @Test
     87     public void testAutofill_oneDataset_afterResizing() throws Exception {
     88         autofillOneDatasetTest(true);
     89     }
     90 
     91     private void autofillOneDatasetTest(boolean resize) throws Exception {
     92         enableService();
     93         mActivity.launchDialog(mUiBot);
     94 
     95         if (resize) {
     96             mActivity.maximizeDialog();
     97         }
     98 
     99         // Set expectations.
    100         mActivity.expectAutofill("dude", "sweet");
    101         sReplier.addResponse(new CannedDataset.Builder()
    102                 .setField(ID_USERNAME, "dude")
    103                 .setField(ID_PASSWORD, "sweet")
    104                 .setPresentation(createPresentation("The Dude"))
    105                 .build());
    106 
    107         // Trigger autofill.
    108         mActivity.onUsername(View::requestFocus);
    109         sReplier.getNextFillRequest();
    110 
    111         final UiObject2 picker = mUiBot.assertDatasets("The Dude");
    112         if (!Helper.isAutofillWindowFullScreen(mActivity)) {
    113             mActivity.assertInDialogBounds(picker.getVisibleBounds());
    114         }
    115 
    116         // Asserts results.
    117         mUiBot.selectDataset("The Dude");
    118         mActivity.assertAutofilled();
    119     }
    120 }
    121