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.testng.Assert.assertThrows; 22 23 import android.platform.test.annotations.AppModeFull; 24 import android.service.autofill.TextValueSanitizer; 25 import android.support.test.runner.AndroidJUnit4; 26 import android.view.autofill.AutofillValue; 27 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 31 import java.util.regex.Pattern; 32 33 @RunWith(AndroidJUnit4.class) 34 @AppModeFull // Unit test 35 public class TextValueSanitizerTest { 36 37 @Test 38 public void testConstructor_nullValues() { 39 assertThrows(NullPointerException.class, 40 () -> new TextValueSanitizer(Pattern.compile("42"), null)); 41 assertThrows(NullPointerException.class, 42 () -> new TextValueSanitizer(null, "42")); 43 } 44 45 @Test 46 public void testSanitize_nullValue() { 47 final TextValueSanitizer sanitizer = new TextValueSanitizer(Pattern.compile("42"), "42"); 48 assertThat(sanitizer.sanitize(null)).isNull(); 49 } 50 51 @Test 52 public void testSanitize_nonTextValue() { 53 final TextValueSanitizer sanitizer = new TextValueSanitizer(Pattern.compile("42"), "42"); 54 final AutofillValue value = AutofillValue.forToggle(true); 55 assertThat(sanitizer.sanitize(value)).isNull(); 56 } 57 58 @Test 59 public void testSanitize_badRegex() { 60 final TextValueSanitizer sanitizer = new TextValueSanitizer(Pattern.compile(".*(\\d*).*"), 61 "$2"); // invalid group 62 final AutofillValue value = AutofillValue.forText("blah 42 blaH"); 63 assertThat(sanitizer.sanitize(value)).isNull(); 64 } 65 66 @Test 67 public void testSanitize_valueMismatch() { 68 final TextValueSanitizer sanitizer = new TextValueSanitizer(Pattern.compile("42"), "xxx"); 69 final AutofillValue value = AutofillValue.forText("43"); 70 assertThat(sanitizer.sanitize(value)).isNull(); 71 } 72 73 @Test 74 public void testSanitize_simpleMatch() { 75 final TextValueSanitizer sanitizer = new TextValueSanitizer(Pattern.compile("42"), 76 "forty-two"); 77 assertThat(sanitizer.sanitize(AutofillValue.forText("42")).getTextValue()) 78 .isEqualTo("forty-two"); 79 } 80 81 @Test 82 public void testSanitize_multipleMatches() { 83 final TextValueSanitizer sanitizer = new TextValueSanitizer(Pattern.compile(".*(\\d*).*"), 84 "Number"); 85 assertThat(sanitizer.sanitize(AutofillValue.forText("blah 42 blaH")).getTextValue()) 86 .isEqualTo("NumberNumber"); 87 } 88 89 @Test 90 public void testSanitize_groupSubstitutionMatch() { 91 final TextValueSanitizer sanitizer = 92 new TextValueSanitizer(Pattern.compile("\\s*(\\d*)\\s*"), "$1"); 93 assertThat(sanitizer.sanitize(AutofillValue.forText(" 42 ")).getTextValue()) 94 .isEqualTo("42"); 95 } 96 } 97