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 com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.mock; 22 import static org.mockito.Mockito.when; 23 import static org.testng.Assert.assertThrows; 24 25 import android.platform.test.annotations.AppModeFull; 26 import android.service.autofill.RegexValidator; 27 import android.service.autofill.ValueFinder; 28 import android.support.test.runner.AndroidJUnit4; 29 import android.view.autofill.AutofillId; 30 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 34 import java.util.regex.Pattern; 35 36 @RunWith(AndroidJUnit4.class) 37 @AppModeFull // Unit test 38 public class RegexValidatorTest { 39 40 @Test 41 public void allNullConstructor() { 42 assertThrows(NullPointerException.class, () -> new RegexValidator(null, null)); 43 } 44 45 @Test 46 public void nullRegexConstructor() { 47 assertThrows(NullPointerException.class, 48 () -> new RegexValidator(new AutofillId(1), null)); 49 } 50 51 @Test 52 public void nullAutofillIdConstructor() { 53 assertThrows(NullPointerException.class, 54 () -> new RegexValidator(null, Pattern.compile("."))); 55 } 56 57 @Test 58 public void unknownField() { 59 AutofillId unknownId = new AutofillId(42); 60 61 RegexValidator validator = new RegexValidator(unknownId, Pattern.compile(".*")); 62 63 ValueFinder finder = mock(ValueFinder.class); 64 65 when(finder.findByAutofillId(unknownId)).thenReturn(null); 66 assertThat(validator.isValid(finder)).isFalse(); 67 } 68 69 @Test 70 public void singleFieldValid() { 71 AutofillId creditCardFieldId = new AutofillId(1); 72 RegexValidator validator = new RegexValidator(creditCardFieldId, 73 Pattern.compile("^\\s*\\d{4}[\\s-]?\\d{4}[\\s-]?\\d{4}[\\s-]?(\\d{4})\\s*$")); 74 75 ValueFinder finder = mock(ValueFinder.class); 76 77 when(finder.findByAutofillId(creditCardFieldId)).thenReturn("1234 5678 9012 3456"); 78 assertThat(validator.isValid(finder)).isTrue(); 79 80 when(finder.findByAutofillId(creditCardFieldId)).thenReturn("invalid"); 81 assertThat(validator.isValid(finder)).isFalse(); 82 } 83 84 @Test 85 public void singleFieldInvalid() { 86 AutofillId id = new AutofillId(1); 87 RegexValidator validator = new RegexValidator(id, Pattern.compile("\\d*")); 88 89 ValueFinder finder = mock(ValueFinder.class); 90 91 when(finder.findByAutofillId(id)).thenReturn("123a456"); 92 93 // Regex has to match the whole value 94 assertThat(validator.isValid(finder)).isFalse(); 95 } 96 } 97