1 /*
2  * Copyright (C) 2020 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 
17 package android.autofillservice.cts.inline;
18 
19 import static android.autofillservice.cts.activities.SimpleSaveActivity.ID_COMMIT;
20 import static android.autofillservice.cts.activities.SimpleSaveActivity.ID_INPUT;
21 import static android.autofillservice.cts.activities.SimpleSaveActivity.ID_PASSWORD;
22 import static android.autofillservice.cts.testcore.Helper.assertTextAndValue;
23 import static android.autofillservice.cts.testcore.Helper.findNodeByResourceId;
24 import static android.autofillservice.cts.testcore.Helper.getContext;
25 import static android.autofillservice.cts.testcore.InstrumentedAutoFillServiceInlineEnabled.SERVICE_NAME;
26 import static android.service.autofill.SaveInfo.SAVE_DATA_TYPE_GENERIC;
27 
28 import android.autofillservice.cts.activities.SimpleSaveActivity;
29 import android.autofillservice.cts.commontests.AutoFillServiceTestCase;
30 import android.autofillservice.cts.testcore.AutofillActivityTestRule;
31 import android.autofillservice.cts.testcore.CannedFillResponse;
32 import android.autofillservice.cts.testcore.Helper;
33 import android.autofillservice.cts.testcore.InlineUiBot;
34 import android.autofillservice.cts.testcore.InstrumentedAutoFillService;
35 import android.platform.test.annotations.Presubmit;
36 import android.support.test.uiautomator.UiObject2;
37 
38 import androidx.annotation.NonNull;
39 
40 import org.junit.Test;
41 import org.junit.rules.TestRule;
42 
43 @Presubmit
44 public class InlineSimpleSaveActivityTest
45         extends AutoFillServiceTestCase.AutoActivityLaunch<SimpleSaveActivity> {
46 
47     private static final String TAG = "InlineSimpleSaveActivityTest";
48     protected SimpleSaveActivity mActivity;
49 
InlineSimpleSaveActivityTest()50     public InlineSimpleSaveActivityTest() {
51         super(getInlineUiBot());
52     }
53 
54     @Override
enableService()55     protected void enableService() {
56         Helper.enableAutofillService(getContext(), SERVICE_NAME);
57     }
58 
59     @NonNull
60     @Override
getActivityRule()61     protected AutofillActivityTestRule<SimpleSaveActivity> getActivityRule() {
62         return new AutofillActivityTestRule<SimpleSaveActivity>(SimpleSaveActivity.class) {
63             @Override
64             protected void afterActivityLaunched() {
65                 mActivity = getActivity();
66             }
67         };
68     }
69 
70     @Override
71     public TestRule getMainTestRule() {
72         return InlineUiBot.annotateRule(super.getMainTestRule());
73     }
74 
75     @Test
76     public void testAutofillSave() throws Exception {
77         // Set service.
78         enableService();
79 
80          // Set expectations.
81         sReplier.addResponse(new CannedFillResponse.Builder()
82                 .setRequiredSavableIds(SAVE_DATA_TYPE_GENERIC, ID_INPUT)
83                 .build());
84 
85         // Trigger auto-fill and IME.
86         mUiBot.selectByRelativeId(ID_INPUT);
87         mUiBot.waitForIdle();
88 
89         sReplier.getNextFillRequest();
90 
91         // Suggestion strip was never shown.
92         mUiBot.assertNoDatasetsEver();
93 
94         // Change input
95         mActivity.syncRunOnUiThread(() -> mActivity.getInput().setText("ID"));
96         mUiBot.waitForIdle();
97 
98         // Trigger save UI.
99         mUiBot.selectByRelativeId(ID_COMMIT);
100         mUiBot.waitForIdle();
101 
102         // Confirm the save UI shown
103         final UiObject2 saveUi = mUiBot.assertSaveShowing(SAVE_DATA_TYPE_GENERIC);
104 
105         // Save it...
106         mUiBot.saveForAutofill(saveUi, true);
107 
108         // ... and assert results
109         final InstrumentedAutoFillService.SaveRequest saveRequest = sReplier.getNextSaveRequest();
110         assertTextAndValue(findNodeByResourceId(saveRequest.structure, ID_INPUT), "ID");
111     }
112 
113     @Test
114     public void testAutofill_oneDatasetAndSave() throws Exception {
115         // Set service.
116         enableService();
117 
118         final CannedFillResponse.Builder builder = new CannedFillResponse.Builder()
119                 .setRequiredSavableIds(SAVE_DATA_TYPE_GENERIC, ID_INPUT, ID_PASSWORD)
120                 .addDataset(new CannedFillResponse.CannedDataset.Builder()
121                         .setField(ID_INPUT, "id")
122                         .setField(ID_PASSWORD, "pass")
123                         .setPresentation(createPresentation("YO"))
124                         .setInlinePresentation(createInlinePresentation("YO"))
125                         .build());
126         sReplier.addResponse(builder.build());
127         mActivity.expectAutoFill("id", "pass");
128 
129         // Trigger auto-fill and IME.
130         mUiBot.selectByRelativeId(ID_INPUT);
131         mUiBot.waitForIdle();
132 
133         sReplier.getNextFillRequest();
134 
135         // Confirm one suggestion
136         mUiBot.assertDatasets("YO");
137 
138         // Select suggestion
139         mUiBot.selectDataset("YO");
140         mUiBot.waitForIdle();
141 
142         // Check the results.
143         mActivity.expectAutoFill("id", "pass");
144 
145         // Change input
146         mActivity.syncRunOnUiThread(() -> mActivity.getInput().setText("ID"));
147         mUiBot.waitForIdle();
148 
149         // Trigger save UI.
150         mUiBot.selectByRelativeId(ID_COMMIT);
151         mUiBot.waitForIdle();
152 
153         // Confirm the save UI shown
154         final UiObject2 saveUi = mUiBot.assertUpdateShowing(SAVE_DATA_TYPE_GENERIC);
155 
156         // Save it...
157         mUiBot.saveForAutofill(saveUi, true);
158 
159         // ... and assert results
160         final InstrumentedAutoFillService.SaveRequest saveRequest = sReplier.getNextSaveRequest();
161         assertTextAndValue(findNodeByResourceId(saveRequest.structure, ID_INPUT), "ID");
162         assertTextAndValue(findNodeByResourceId(saveRequest.structure, ID_PASSWORD), "pass");
163     }
164 }
165