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 
17 package android.ext.services.autofill;
18 
19 import static android.service.autofill.AutofillFieldClassificationService.REQUIRED_ALGORITHM_EXACT_MATCH;
20 import static android.view.autofill.AutofillValue.forText;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 import static com.google.common.truth.Truth.assertWithMessage;
24 
25 import android.os.Bundle;
26 import android.view.autofill.AutofillValue;
27 
28 import org.junit.Test;
29 
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.List;
34 
35 /**
36  * Contains the base tests that does not rely on the specific algorithm implementation.
37  */
38 public class AutofillFieldClassificationServiceImplTest {
39 
40     private final AutofillFieldClassificationServiceImpl mService =
41             new AutofillFieldClassificationServiceImpl();
42 
43     @Test
testOnCalculateScores_nullActualValues()44     public void testOnCalculateScores_nullActualValues() {
45         assertThat(mService.onCalculateScores(null, null, null, null, null, null, null)).isNull();
46     }
47 
48     @Test
testOnCalculateScores_emptyActualValues()49     public void testOnCalculateScores_emptyActualValues() {
50         assertThat(mService.onCalculateScores(Collections.emptyList(), Arrays.asList("whatever"),
51                 null, null, null, null, null)).isNull();
52     }
53 
54     @Test
testOnCalculateScores_nullUserDataValues()55     public void testOnCalculateScores_nullUserDataValues() {
56         assertThat(mService.onCalculateScores(Arrays.asList(AutofillValue.forText("whatever")),
57                 null, null, null, null, null, null)).isNull();
58     }
59 
60     @Test
testOnCalculateScores_emptyUserDataValues()61     public void testOnCalculateScores_emptyUserDataValues() {
62         assertThat(mService.onCalculateScores(Arrays.asList(AutofillValue.forText("whatever")),
63                 Collections.emptyList(), null, null, null, null, null))
64                 .isNull();
65     }
66 
67     @Test
testCalculateScores()68     public void testCalculateScores() {
69         final List<AutofillValue> actualValues = Arrays.asList(forText("A"), forText("b"),
70                 forText("dude"));
71         final List<String> userDataValues = Arrays.asList("a", "b", "B", "ab", "c", "dude",
72                 "sweet_dude", "dude_sweet");
73         final List<String> categoryIds = Arrays.asList("cat", "cat", "cat", "cat", "cat", "last4",
74                 "last4", "last4");
75         final HashMap<String, String> algorithms = new HashMap<>(1);
76         algorithms.put("last4", REQUIRED_ALGORITHM_EXACT_MATCH);
77 
78         final Bundle last4Bundle = new Bundle();
79         last4Bundle.putInt("suffix", 4);
80 
81         final HashMap<String, Bundle> args = new HashMap<>(1);
82         args.put("last4", last4Bundle);
83 
84         final float[][] expectedScores = new float[][] {
85                 new float[] { 1F, 0F, 0F, 0.5F, 0F, 0F, 0F, 0F },
86                 new float[] { 0F, 1F, 1F, 0.5F, 0F, 0F, 0F, 0F },
87                 new float[] { 0F, 0F, 0F, 0F  , 0F, 1F, 1F, 0F }
88         };
89         final float[][] actualScores = mService.onCalculateScores(actualValues, userDataValues,
90                 categoryIds, null, null, algorithms, args);
91 
92         // Unfortunately, Truth does not have an easy way to compare float matrices and show useful
93         // messages in case of error, so we need to check.
94         assertWithMessage("actual=%s, expected=%s", toString(actualScores),
95                 toString(expectedScores)).that(actualScores.length).isEqualTo(3);
96         for (int i = 0; i < 3; i++) {
97             assertWithMessage("actual=%s, expected=%s", toString(actualScores),
98                     toString(expectedScores)).that(actualScores[i].length).isEqualTo(8);
99         }
100 
101         for (int i = 0; i < actualScores.length; i++) {
102             final float[] line = actualScores[i];
103             for (int j = 0; j < line.length; j++) {
104                 float cell = line[j];
105                 assertWithMessage("wrong score at [%s, %s]", i, j).that(cell).isWithin(0.01F)
106                         .of(expectedScores[i][j]);
107             }
108         }
109     }
110 
toString(float[][] matrix)111     public static String toString(float[][] matrix) {
112         final StringBuilder string = new StringBuilder("[ ");
113         for (int i = 0; i < matrix.length; i++) {
114             string.append(Arrays.toString(matrix[i])).append(" ");
115         }
116         return string.append(" ]").toString();
117     }
118 }
119