1 /* 2 * Copyright (C) 2017 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.view.textclassifier.logging; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotNull; 21 import static org.junit.Assert.fail; 22 import static org.mockito.Mockito.doNothing; 23 import static org.mockito.Mockito.mock; 24 25 import android.metrics.LogMaker; 26 import android.util.ArrayMap; 27 import android.view.textclassifier.GenerateLinksLogger; 28 import android.view.textclassifier.TextClassifier; 29 import android.view.textclassifier.TextLinks; 30 31 import androidx.test.filters.SmallTest; 32 import androidx.test.runner.AndroidJUnit4; 33 34 import com.android.internal.logging.MetricsLogger; 35 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 36 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.ArgumentCaptor; 40 41 import java.util.List; 42 import java.util.Map; 43 import java.util.Objects; 44 45 @SmallTest 46 @RunWith(AndroidJUnit4.class) 47 public class GenerateLinksLoggerTest { 48 49 private static final String PACKAGE_NAME = "packageName"; 50 private static final String ZERO = "0"; 51 private static final int LATENCY_MS = 123; 52 53 @Test testLogGenerateLinks()54 public void testLogGenerateLinks() { 55 final String phoneText = "+12122537077"; 56 final String addressText = "1600 Amphitheater Parkway, Mountain View, CA"; 57 final String testText = "The number is " + phoneText + ", the address is " + addressText; 58 final int phoneOffset = testText.indexOf(phoneText); 59 final int addressOffset = testText.indexOf(addressText); 60 61 final Map<String, Float> phoneEntityScores = new ArrayMap<>(); 62 phoneEntityScores.put(TextClassifier.TYPE_PHONE, 0.9f); 63 phoneEntityScores.put(TextClassifier.TYPE_OTHER, 0.1f); 64 final Map<String, Float> addressEntityScores = new ArrayMap<>(); 65 addressEntityScores.put(TextClassifier.TYPE_ADDRESS, 1f); 66 67 TextLinks links = new TextLinks.Builder(testText) 68 .addLink(phoneOffset, phoneOffset + phoneText.length(), phoneEntityScores) 69 .addLink(addressOffset, addressOffset + addressText.length(), addressEntityScores) 70 .build(); 71 72 // Set up mock. 73 MetricsLogger metricsLogger = mock(MetricsLogger.class); 74 ArgumentCaptor<LogMaker> logMakerCapture = ArgumentCaptor.forClass(LogMaker.class); 75 doNothing().when(metricsLogger).write(logMakerCapture.capture()); 76 77 // Generate the log. 78 GenerateLinksLogger logger = new GenerateLinksLogger(1 /* sampleRate */, metricsLogger); 79 logger.logGenerateLinks(testText, links, PACKAGE_NAME, LATENCY_MS); 80 81 // Validate. 82 List<LogMaker> logs = logMakerCapture.getAllValues(); 83 assertEquals(3, logs.size()); 84 assertHasLog(logs, "" /* entityType */, 2, phoneText.length() + addressText.length(), 85 testText.length()); 86 assertHasLog(logs, TextClassifier.TYPE_ADDRESS, 1, addressText.length(), 87 testText.length()); 88 assertHasLog(logs, TextClassifier.TYPE_PHONE, 1, phoneText.length(), 89 testText.length()); 90 } 91 assertHasLog(List<LogMaker> logs, String entityType, int numLinks, int linkTextLength, int textLength)92 private void assertHasLog(List<LogMaker> logs, String entityType, int numLinks, 93 int linkTextLength, int textLength) { 94 for (LogMaker log : logs) { 95 if (!entityType.equals(getEntityType(log))) { 96 continue; 97 } 98 assertEquals(PACKAGE_NAME, log.getPackageName()); 99 assertNotNull(Objects.toString(log.getTaggedData(MetricsEvent.FIELD_LINKIFY_CALL_ID))); 100 assertEquals(numLinks, getIntValue(log, MetricsEvent.FIELD_LINKIFY_NUM_LINKS)); 101 assertEquals(linkTextLength, getIntValue(log, MetricsEvent.FIELD_LINKIFY_LINK_LENGTH)); 102 assertEquals(textLength, getIntValue(log, MetricsEvent.FIELD_LINKIFY_TEXT_LENGTH)); 103 assertEquals(LATENCY_MS, getIntValue(log, MetricsEvent.FIELD_LINKIFY_LATENCY)); 104 return; 105 } 106 fail("No log for entity type \"" + entityType + "\""); 107 } 108 getEntityType(LogMaker log)109 private static String getEntityType(LogMaker log) { 110 return Objects.toString(log.getTaggedData(MetricsEvent.FIELD_LINKIFY_ENTITY_TYPE), ""); 111 } 112 getIntValue(LogMaker log, int eventField)113 private static int getIntValue(LogMaker log, int eventField) { 114 return Integer.parseInt(Objects.toString(log.getTaggedData(eventField), ZERO)); 115 } 116 } 117