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