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