• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.textclassifier;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.icu.util.ULocale;
22 import android.view.textclassifier.ConversationAction;
23 import android.view.textclassifier.ConversationActions;
24 import android.view.textclassifier.TextClassification;
25 import android.view.textclassifier.TextClassifier;
26 import android.view.textclassifier.TextLanguage;
27 import android.view.textclassifier.TextLinks;
28 import android.view.textclassifier.TextLinks.TextLink;
29 import android.view.textclassifier.TextSelection;
30 import androidx.test.ext.junit.runners.AndroidJUnit4;
31 import androidx.test.filters.SmallTest;
32 import com.android.textclassifier.testing.ExtServicesTextClassifierRule;
33 import com.google.common.collect.ImmutableList;
34 import java.util.ArrayList;
35 import java.util.List;
36 import org.junit.Before;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 
41 /**
42  * End-to-end tests for the {@link TextClassifier} APIs. Unlike {@link TextClassifierImplTest}.
43  *
44  * <p>Unlike {@link TextClassifierImplTest}, we are trying to run the tests in a environment that is
45  * closer to the production environment. For example, we are not injecting the model files.
46  */
47 @SmallTest
48 @RunWith(AndroidJUnit4.class)
49 public class TextClassifierApiTest {
50 
51   private TextClassifier textClassifier;
52 
53   @Rule
54   public final ExtServicesTextClassifierRule extServicesTextClassifierRule =
55       new ExtServicesTextClassifierRule();
56 
57   @Before
setup()58   public void setup() {
59     extServicesTextClassifierRule.enableVerboseLogging();
60     // Verbose logging only takes effect after restarting ExtServices
61     extServicesTextClassifierRule.forceStopExtServices();
62 
63     textClassifier = extServicesTextClassifierRule.getTextClassifier();
64   }
65 
66   @Test
suggestSelection()67   public void suggestSelection() {
68     String text = "Visit http://www.android.com for more information";
69     String selected = "http";
70     String suggested = "http://www.android.com";
71     int startIndex = text.indexOf(selected);
72     int endIndex = startIndex + selected.length();
73     int smartStartIndex = text.indexOf(suggested);
74     int smartEndIndex = smartStartIndex + suggested.length();
75 
76     TextSelection.Request request =
77         new TextSelection.Request.Builder(text, startIndex, endIndex).build();
78 
79     TextSelection selection = textClassifier.suggestSelection(request);
80     assertThat(selection.getEntityCount()).isGreaterThan(0);
81     assertThat(selection.getEntity(0)).isEqualTo(TextClassifier.TYPE_URL);
82     assertThat(selection.getSelectionStartIndex()).isEqualTo(smartStartIndex);
83     assertThat(selection.getSelectionEndIndex()).isEqualTo(smartEndIndex);
84   }
85 
86   @Test
classifyText()87   public void classifyText() {
88     String text = "Contact me at http://www.android.com";
89     String classifiedText = "http://www.android.com";
90     int startIndex = text.indexOf(classifiedText);
91     int endIndex = startIndex + classifiedText.length();
92     TextClassification.Request request =
93         new TextClassification.Request.Builder(text, startIndex, endIndex).build();
94 
95     TextClassification classification = textClassifier.classifyText(request);
96     assertThat(classification.getEntityCount()).isGreaterThan(0);
97     assertThat(classification.getEntity(0)).isEqualTo(TextClassifier.TYPE_URL);
98     assertThat(classification.getText()).isEqualTo(classifiedText);
99     assertThat(classification.getActions()).isNotEmpty();
100   }
101 
102   @Test
generateLinks()103   public void generateLinks() {
104     String text = "Check this out, http://www.android.com";
105 
106     TextLinks.Request request = new TextLinks.Request.Builder(text).build();
107 
108     TextLinks textLinks = textClassifier.generateLinks(request);
109 
110     List<TextLink> links = new ArrayList<>(textLinks.getLinks());
111     assertThat(textLinks.getText().toString()).isEqualTo(text);
112     assertThat(links).hasSize(1);
113     assertThat(links.get(0).getEntityCount()).isGreaterThan(0);
114     assertThat(links.get(0).getEntity(0)).isEqualTo(TextClassifier.TYPE_URL);
115     assertThat(links.get(0).getConfidenceScore(TextClassifier.TYPE_URL)).isGreaterThan(0f);
116   }
117 
118   @Test
detectedLanguage()119   public void detectedLanguage() {
120     String text = "朝、ピカチュウ";
121     TextLanguage.Request request = new TextLanguage.Request.Builder(text).build();
122 
123     TextLanguage textLanguage = textClassifier.detectLanguage(request);
124 
125     assertThat(textLanguage.getLocaleHypothesisCount()).isGreaterThan(0);
126     assertThat(textLanguage.getLocale(0).getLanguage()).isEqualTo("ja");
127     assertThat(textLanguage.getConfidenceScore(ULocale.JAPANESE)).isGreaterThan(0f);
128   }
129 
130   @Test
suggestConversationActions()131   public void suggestConversationActions() {
132     ConversationActions.Message message =
133         new ConversationActions.Message.Builder(ConversationActions.Message.PERSON_USER_OTHERS)
134             .setText("Check this out: https://www.android.com")
135             .build();
136     ConversationActions.Request request =
137         new ConversationActions.Request.Builder(ImmutableList.of(message)).build();
138 
139     ConversationActions conversationActions = textClassifier.suggestConversationActions(request);
140 
141     assertThat(conversationActions.getConversationActions()).hasSize(1);
142     ConversationAction conversationAction = conversationActions.getConversationActions().get(0);
143     assertThat(conversationAction.getType()).isEqualTo(ConversationAction.TYPE_OPEN_URL);
144     assertThat(conversationAction.getAction()).isNotNull();
145   }
146 }
147