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