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