1 /*
2  * Copyright (C) 2019 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.CreditCardMatcher.OPTIONAL_ARG_SUFFIX_LENGTH;
19 import static android.ext.services.autofill.CreditCardMatcher.REQUIRED_ARG_MAX_CREDIT_CARD_LENGTH;
20 import static android.ext.services.autofill.CreditCardMatcher.REQUIRED_ARG_MIN_CREDIT_CARD_LENGTH;
21 import static android.ext.services.autofill.CreditCardMatcher.calculateScore;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static org.testng.Assert.assertThrows;
26 
27 import android.os.Bundle;
28 import android.view.autofill.AutofillValue;
29 
30 import org.junit.Test;
31 
32 public class CreditCardMatcherTest {
33     @Test
testCalculateScore_ForCreditCard()34     public void testCalculateScore_ForCreditCard() {
35         final Bundle forCreditCard = forCreditCardBundle();
36         assertFloat(calculateScore(AutofillValue.forText("5678"), "1234123412345678",
37                 forCreditCard), 1F);
38         assertFloat(calculateScore(AutofillValue.forText("5678"), "12341234125678",
39                 forCreditCard), 1F);
40         assertFloat(calculateScore(AutofillValue.forText("1234"), "1234123412341234",
41                 forCreditCard), 1F);
42         assertFloat(calculateScore(AutofillValue.forText("1234"), "1234123412345678",
43                 forCreditCard), 0F);
44         assertFloat(calculateScore(AutofillValue.forText("1234"), "12341234",
45                 forCreditCard), 0F);
46         assertFloat(calculateScore(AutofillValue.forText("1234"), "12341234123412341234",
47                 forCreditCard), 0F);
48     }
49 
50     @Test
testCalculateScore_BadBundle()51     public void testCalculateScore_BadBundle() {
52         final Bundle bundle = new Bundle();
53 
54         // Both length args are negative
55         bundle.putInt(REQUIRED_ARG_MIN_CREDIT_CARD_LENGTH, -1);
56         bundle.putInt(REQUIRED_ARG_MAX_CREDIT_CARD_LENGTH, -1);
57         assertThrows(IllegalArgumentException.class, () -> calculateScore(
58                 AutofillValue.forText("TEST"), "TEST", bundle));
59 
60         // min_length is negative
61         bundle.putInt(REQUIRED_ARG_MIN_CREDIT_CARD_LENGTH, -1);
62         bundle.putInt(REQUIRED_ARG_MAX_CREDIT_CARD_LENGTH, 1);
63         assertThrows(IllegalArgumentException.class, () -> calculateScore(
64                 AutofillValue.forText("TEST"), "TEST", bundle));
65 
66         // max_length is negative
67         bundle.putInt(REQUIRED_ARG_MIN_CREDIT_CARD_LENGTH, 1);
68         bundle.putInt(REQUIRED_ARG_MAX_CREDIT_CARD_LENGTH, -1);
69         assertThrows(IllegalArgumentException.class, () -> calculateScore(
70                 AutofillValue.forText("TEST"), "TEST", bundle));
71 
72         // max is less than min
73         bundle.putInt(REQUIRED_ARG_MIN_CREDIT_CARD_LENGTH, 4);
74         bundle.putInt(REQUIRED_ARG_MAX_CREDIT_CARD_LENGTH, 3);
75         assertThrows(IllegalArgumentException.class, () -> calculateScore(
76                 AutofillValue.forText("TEST"), "TEST", bundle));
77 
78         // suffix argument is invalid
79         bundle.putInt(REQUIRED_ARG_MIN_CREDIT_CARD_LENGTH, 13);
80         bundle.putInt(REQUIRED_ARG_MAX_CREDIT_CARD_LENGTH, 19);
81         bundle.putInt(OPTIONAL_ARG_SUFFIX_LENGTH, -1);
82         assertThrows(IllegalArgumentException.class, () -> calculateScore(
83                 AutofillValue.forText("TEST"), "TEST", bundle));
84     }
85 
forCreditCardBundle()86     private Bundle forCreditCardBundle() {
87         final Bundle bundle = new Bundle();
88         bundle.putInt(REQUIRED_ARG_MIN_CREDIT_CARD_LENGTH, 13);
89         bundle.putInt(REQUIRED_ARG_MAX_CREDIT_CARD_LENGTH, 19);
90         bundle.putInt(OPTIONAL_ARG_SUFFIX_LENGTH, 4);
91         return bundle;
92     }
93 
assertFloat(float actualValue, float expectedValue)94     public static void assertFloat(float actualValue, float expectedValue) {
95         assertThat(actualValue).isWithin(0.01F).of(expectedValue);
96     }
97 }
98