1 /*
2  * Copyright (C) 2018 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 package android.ext.services.autofill;
17 
18 import static android.ext.services.autofill.ExactMatch.MATCH_SUFFIX;
19 import static android.ext.services.autofill.ExactMatch.calculateScore;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.testng.Assert.assertThrows;
24 
25 import android.os.Bundle;
26 import android.view.autofill.AutofillValue;
27 
28 import org.junit.Test;
29 
30 public class ExactMatchTest {
31 
last4Bundle()32     private Bundle last4Bundle() {
33         final Bundle bundle = new Bundle();
34         bundle.putInt(MATCH_SUFFIX, 4);
35         return bundle;
36     }
37 
38     @Test
testCalculateScore_nullValue()39     public void testCalculateScore_nullValue() {
40         assertFloat(calculateScore(null, "TEST", null), 0);
41     }
42 
43     @Test
testCalculateScore_nonTextValue()44     public void testCalculateScore_nonTextValue() {
45         assertFloat(calculateScore(AutofillValue.forToggle(true), "TEST", null), 0);
46     }
47 
48     @Test
testCalculateScore_nullUserData()49     public void testCalculateScore_nullUserData() {
50         assertFloat(calculateScore(AutofillValue.forText("TEST"), null, null), 0);
51     }
52 
53     @Test
testCalculateScore_succeedMatchMixedCases_last4()54     public void testCalculateScore_succeedMatchMixedCases_last4() {
55         final Bundle last4 = last4Bundle();
56         assertFloat(calculateScore(AutofillValue.forText("TEST"), "1234 test", last4), 1);
57         assertFloat(calculateScore(AutofillValue.forText("test"), "1234 TEST", last4), 1);
58     }
59 
60     @Test
testCalculateScore_mismatchDifferentSizes_last4()61     public void testCalculateScore_mismatchDifferentSizes_last4() {
62         final Bundle last4 = last4Bundle();
63         assertFloat(calculateScore(AutofillValue.forText("TEST"), "TEST1", last4), 0);
64         assertFloat(calculateScore(AutofillValue.forText(""), "TEST", last4), 0);
65         assertFloat(calculateScore(AutofillValue.forText("TEST"), "", last4), 0);
66     }
67 
68     @Test
testCalculateScore_match()69     public void testCalculateScore_match() {
70         final Bundle last4 = last4Bundle();
71         assertFloat(calculateScore(AutofillValue.forText("1234 1234 1234 1234"),
72                 "xxxx xxxx xxxx 1234", last4), 1);
73         assertFloat(calculateScore(AutofillValue.forText("TEST"), "TEST", null), 1);
74         assertFloat(calculateScore(AutofillValue.forText("TEST 1234"), "1234", last4), 1);
75         assertFloat(calculateScore(AutofillValue.forText("TEST"), "test", null), 1);
76     }
77 
78     @Test
testCalculateScore_badBundle()79     public void testCalculateScore_badBundle() {
80         final Bundle bundle = new Bundle();
81         bundle.putInt(MATCH_SUFFIX, -2);
82         assertThrows(IllegalArgumentException.class, () -> calculateScore(
83                 AutofillValue.forText("TEST"), "TEST", bundle));
84 
85         final Bundle largeBundle = new Bundle();
86         largeBundle.putInt(MATCH_SUFFIX, 10);
87         assertFloat(calculateScore(AutofillValue.forText("TEST"), "TEST", largeBundle), 1);
88 
89         final Bundle stringBundle = new Bundle();
90         stringBundle.putString(MATCH_SUFFIX, "value");
91         assertThrows(IllegalArgumentException.class, () -> calculateScore(
92                 AutofillValue.forText("TEST"), "TEST", stringBundle));
93 
94     }
95 
assertFloat(float actualValue, float expectedValue)96     public static void assertFloat(float actualValue, float expectedValue) {
97         assertThat(actualValue).isWithin(0.01F).of(expectedValue);
98     }
99 }