xref: /aosp_15_r20/cts/tests/autofillservice/src/android/autofillservice/cts/servicebehavior/ValidatorTest.java (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
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 
17 package android.autofillservice.cts.servicebehavior;
18 
19 import static android.autofillservice.cts.testcore.Helper.ID_PASSWORD;
20 import static android.autofillservice.cts.testcore.Helper.ID_USERNAME;
21 import static android.autofillservice.cts.testcore.Helper.findAutofillIdByResourceId;
22 import static android.service.autofill.SaveInfo.SAVE_DATA_TYPE_GENERIC;
23 
24 import static com.google.common.truth.Truth.assertThat;
25 
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.mock;
28 
29 import android.autofillservice.cts.commontests.AbstractLoginActivityTestCase;
30 import android.autofillservice.cts.testcore.CannedFillResponse;
31 import android.platform.test.annotations.AppModeFull;
32 import android.service.autofill.InternalValidator;
33 import android.service.autofill.LuhnChecksumValidator;
34 import android.service.autofill.ValueFinder;
35 import android.view.View;
36 import android.view.autofill.AutofillId;
37 
38 import org.junit.Test;
39 
40 /**
41  * Simple integration test to verify that the UI is only shown if the validator passes.
42  */
43 @AppModeFull(reason = "Service-specific test")
44 public class ValidatorTest extends AbstractLoginActivityTestCase {
45 
46     @Test
testShowUiWhenValidatorPass()47     public void testShowUiWhenValidatorPass() throws Exception {
48         integrationTest(true);
49     }
50 
51     @Test
testDontShowUiWhenValidatorFails()52     public void testDontShowUiWhenValidatorFails() throws Exception {
53         integrationTest(false);
54     }
55 
integrationTest(boolean willSaveBeShown)56     private void integrationTest(boolean willSaveBeShown) throws Exception {
57         enableService();
58 
59         final String username = willSaveBeShown ? "7992739871-3" : "4815162342-108";
60 
61         // Set response
62         sReplier.addResponse(new CannedFillResponse.Builder()
63                 .setRequiredSavableIds(SAVE_DATA_TYPE_GENERIC, ID_USERNAME, ID_PASSWORD)
64                 .setSaveInfoVisitor((contexts, builder) -> {
65                     final AutofillId usernameId =
66                             findAutofillIdByResourceId(contexts.get(0), ID_USERNAME);
67                     final LuhnChecksumValidator validator = new LuhnChecksumValidator(usernameId);
68                     // Validation check to make sure the validator is properly configured
69                     assertValidator(validator, usernameId, username, willSaveBeShown);
70                     builder.setValidator(validator);
71                 })
72                 .build());
73 
74         // Trigger auto-fill
75         mActivity.onPassword(View::requestFocus);
76 
77         // Wait for onFill() before proceeding.
78         sReplier.getNextFillRequest();
79 
80         // Trigger save.
81         mActivity.onUsername((v) -> v.setText(username));
82         mActivity.onPassword((v) -> v.setText("pass"));
83         mActivity.tapLogin();
84 
85         if (willSaveBeShown) {
86             mUiBot.saveForAutofill(true, SAVE_DATA_TYPE_GENERIC);
87             sReplier.getNextSaveRequest();
88         } else {
89             mUiBot.assertSaveNotShowing(SAVE_DATA_TYPE_GENERIC);
90         }
91     }
92 
assertValidator(InternalValidator validator, AutofillId id, String text, boolean valid)93     private void assertValidator(InternalValidator validator, AutofillId id, String text,
94             boolean valid) {
95         final ValueFinder valueFinder = mock(ValueFinder.class);
96         doReturn(text).when(valueFinder).findByAutofillId(id);
97         assertThat(validator.isValid(valueFinder)).isEqualTo(valid);
98     }
99 }
100