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