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.EditDistanceScorer.getScore; 19 import static android.ext.services.autofill.EditDistanceScorer.getScores; 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.view.autofill.AutofillValue; 26 27 import org.junit.Test; 28 29 import java.util.Arrays; 30 import java.util.List; 31 32 public class EditDistanceScorerTest { 33 34 @Test 35 public void testGetScore_nullValue() { 36 assertFloat(getScore(null, "D'OH!"), 0); 37 } 38 39 @Test 40 public void testGetScore_nonTextValue() { 41 assertFloat(getScore(AutofillValue.forToggle(true), "D'OH!"), 0); 42 } 43 44 @Test 45 public void testGetScore_nullUserData() { 46 assertFloat(getScore(AutofillValue.forText("D'OH!"), null), 0); 47 } 48 49 @Test 50 public void testGetScore_fullMatch() { 51 assertFloat(getScore(AutofillValue.forText("D'OH!"), "D'OH!"), 1); 52 assertFloat(getScore(AutofillValue.forText(""), ""), 1); 53 } 54 55 @Test 56 public void testGetScore_fullMatchMixedCase() { 57 assertFloat(getScore(AutofillValue.forText("D'OH!"), "D'oH!"), 1); 58 } 59 60 @Test 61 public void testGetScore_mismatchDifferentSizes() { 62 assertFloat(getScore(AutofillValue.forText("X"), "Xy"), 0.50F); 63 assertFloat(getScore(AutofillValue.forText("Xy"), "X"), 0.50F); 64 assertFloat(getScore(AutofillValue.forText("One"), "MoreThanOne"), 0.27F); 65 assertFloat(getScore(AutofillValue.forText("MoreThanOne"), "One"), 0.27F); 66 assertFloat(getScore(AutofillValue.forText("1600 Amphitheatre Parkway"), 67 "1600 Amphitheatre Pkwy"), 0.88F); 68 assertFloat(getScore(AutofillValue.forText("1600 Amphitheatre Pkwy"), 69 "1600 Amphitheatre Parkway"), 0.88F); 70 } 71 72 @Test 73 public void testGetScore_partialMatch() { 74 assertFloat(getScore(AutofillValue.forText("Dude"), "Dxxx"), 0.25F); 75 assertFloat(getScore(AutofillValue.forText("Dude"), "DUxx"), 0.50F); 76 assertFloat(getScore(AutofillValue.forText("Dude"), "DUDx"), 0.75F); 77 assertFloat(getScore(AutofillValue.forText("Dxxx"), "Dude"), 0.25F); 78 assertFloat(getScore(AutofillValue.forText("DUxx"), "Dude"), 0.50F); 79 assertFloat(getScore(AutofillValue.forText("DUDx"), "Dude"), 0.75F); 80 } 81 82 @Test 83 public void testGetScores() { 84 final List<AutofillValue> actualValues = Arrays.asList(forText("A"), forText("b")); 85 final List<String> userDataValues = Arrays.asList("a", "B", "ab", "c"); 86 final float[][] expectedScores = new float[][] { 87 new float[] { 1F, 0F, 0.5F, 0F }, 88 new float[] { 0F, 1F, 0.5F, 0F } 89 }; 90 final float[][] actualScores = getScores(actualValues, userDataValues); 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(2); 96 assertWithMessage("actual=%s, expected=%s", toString(actualScores), 97 toString(expectedScores)).that(actualScores[0].length).isEqualTo(4); 98 assertWithMessage("actual=%s, expected=%s", toString(actualScores), 99 toString(expectedScores)).that(actualScores[1].length).isEqualTo(4); 100 for (int i = 0; i < actualScores.length; i++) { 101 final float[] line = actualScores[i]; 102 for (int j = 0; j < line.length; j++) { 103 float cell = line[j]; 104 assertWithMessage("wrong score at [%s, %s]", i, j).that(cell).isWithin(0.01F) 105 .of(expectedScores[i][j]); 106 } 107 } 108 } 109 110 public static void assertFloat(float actualValue, float expectedValue) { 111 assertThat(actualValue).isWithin(0.01F).of(expectedValue); 112 } 113 114 public static String toString(float[][] matrix) { 115 final StringBuilder string = new StringBuilder("[ "); 116 for (int i = 0; i < matrix.length; i++) { 117 string.append(Arrays.toString(matrix[i])).append(" "); 118 } 119 return string.append(" ]").toString(); 120 } 121 } 122