blob: 3ad26f5d51088a8f67eea07aa3442baee8d50591 [file] [log] [blame]
Tony Makac9b4d82019-02-15 13:57:38 +00001/*
2 * Copyright (C) 2019 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
Tony Mak8ab9b182019-03-01 16:44:17 +000017package android.view.textclassifier.intent;
Tony Makac9b4d82019-02-15 13:57:38 +000018
19import static com.google.common.truth.Truth.assertThat;
20
21import static org.testng.Assert.assertThrows;
22
Tony Mak15b64be2019-04-01 20:02:29 +010023import android.content.ComponentName;
Tony Makac9b4d82019-02-15 13:57:38 +000024import android.content.Context;
25import android.content.Intent;
26import android.net.Uri;
Tony Mak72e17972019-03-16 10:28:42 +000027import android.os.Bundle;
28import android.view.textclassifier.FakeContextBuilder;
29import android.view.textclassifier.TextClassifier;
Tony Makac9b4d82019-02-15 13:57:38 +000030
Tony Makac9b4d82019-02-15 13:57:38 +000031import androidx.test.filters.SmallTest;
32import androidx.test.runner.AndroidJUnit4;
33
34import org.junit.Before;
35import org.junit.Test;
36import org.junit.runner.RunWith;
37
38@SmallTest
39@RunWith(AndroidJUnit4.class)
40public final class LabeledIntentTest {
41 private static final String TITLE_WITHOUT_ENTITY = "Map";
42 private static final String TITLE_WITH_ENTITY = "Map NW14D1";
43 private static final String DESCRIPTION = "Check the map";
Tony Mak15b64be2019-04-01 20:02:29 +010044 private static final String DESCRIPTION_WITH_APP_NAME = "Use %1$s to open map";
Tony Makac9b4d82019-02-15 13:57:38 +000045 private static final Intent INTENT =
46 new Intent(Intent.ACTION_VIEW).setDataAndNormalize(Uri.parse("http://www.android.com"));
47 private static final int REQUEST_CODE = 42;
Tony Mak72e17972019-03-16 10:28:42 +000048 private static final Bundle TEXT_LANGUAGES_BUNDLE = Bundle.EMPTY;
Tony Mak15b64be2019-04-01 20:02:29 +010049 private static final String APP_LABEL = "fake";
Tony Mak72e17972019-03-16 10:28:42 +000050
Tony Makac9b4d82019-02-15 13:57:38 +000051 private Context mContext;
52
53 @Before
54 public void setup() {
Tony Mak15b64be2019-04-01 20:02:29 +010055 final ComponentName component = FakeContextBuilder.DEFAULT_COMPONENT;
Tony Mak72e17972019-03-16 10:28:42 +000056 mContext = new FakeContextBuilder()
Tony Mak15b64be2019-04-01 20:02:29 +010057 .setIntentComponent(Intent.ACTION_VIEW, component)
58 .setAppLabel(component.getPackageName(), APP_LABEL)
Tony Mak72e17972019-03-16 10:28:42 +000059 .build();
Tony Makac9b4d82019-02-15 13:57:38 +000060 }
61
62 @Test
63 public void resolve_preferTitleWithEntity() {
64 LabeledIntent labeledIntent = new LabeledIntent(
65 TITLE_WITHOUT_ENTITY,
66 TITLE_WITH_ENTITY,
67 DESCRIPTION,
Tony Mak15b64be2019-04-01 20:02:29 +010068 null,
Tony Makac9b4d82019-02-15 13:57:38 +000069 INTENT,
70 REQUEST_CODE
71 );
72
Tony Mak72e17972019-03-16 10:28:42 +000073 LabeledIntent.Result result = labeledIntent.resolve(
74 mContext, /*titleChooser*/ null, TEXT_LANGUAGES_BUNDLE);
Tony Makac9b4d82019-02-15 13:57:38 +000075
76 assertThat(result).isNotNull();
77 assertThat(result.remoteAction.getTitle()).isEqualTo(TITLE_WITH_ENTITY);
78 assertThat(result.remoteAction.getContentDescription()).isEqualTo(DESCRIPTION);
79 Intent intent = result.resolvedIntent;
80 assertThat(intent.getAction()).isEqualTo(intent.getAction());
81 assertThat(intent.getComponent()).isNotNull();
Tony Mak72e17972019-03-16 10:28:42 +000082 assertThat(intent.hasExtra(TextClassifier.EXTRA_FROM_TEXT_CLASSIFIER)).isTrue();
Tony Makac9b4d82019-02-15 13:57:38 +000083 }
84
85 @Test
86 public void resolve_useAvailableTitle() {
87 LabeledIntent labeledIntent = new LabeledIntent(
88 TITLE_WITHOUT_ENTITY,
89 null,
90 DESCRIPTION,
Tony Mak15b64be2019-04-01 20:02:29 +010091 null,
Tony Makac9b4d82019-02-15 13:57:38 +000092 INTENT,
93 REQUEST_CODE
94 );
95
Tony Mak72e17972019-03-16 10:28:42 +000096 LabeledIntent.Result result = labeledIntent.resolve(
97 mContext, /*titleChooser*/ null, TEXT_LANGUAGES_BUNDLE);
Tony Makac9b4d82019-02-15 13:57:38 +000098
99 assertThat(result).isNotNull();
100 assertThat(result.remoteAction.getTitle()).isEqualTo(TITLE_WITHOUT_ENTITY);
101 assertThat(result.remoteAction.getContentDescription()).isEqualTo(DESCRIPTION);
102 Intent intent = result.resolvedIntent;
103 assertThat(intent.getAction()).isEqualTo(intent.getAction());
104 assertThat(intent.getComponent()).isNotNull();
105 }
106
107 @Test
108 public void resolve_titleChooser() {
109 LabeledIntent labeledIntent = new LabeledIntent(
110 TITLE_WITHOUT_ENTITY,
111 null,
112 DESCRIPTION,
Tony Mak15b64be2019-04-01 20:02:29 +0100113 null,
Tony Makac9b4d82019-02-15 13:57:38 +0000114 INTENT,
115 REQUEST_CODE
116 );
117
Tony Mak72e17972019-03-16 10:28:42 +0000118 LabeledIntent.Result result = labeledIntent.resolve(
119 mContext, (labeledIntent1, resolveInfo) -> "chooser", TEXT_LANGUAGES_BUNDLE);
Tony Makac9b4d82019-02-15 13:57:38 +0000120
121 assertThat(result).isNotNull();
122 assertThat(result.remoteAction.getTitle()).isEqualTo("chooser");
123 assertThat(result.remoteAction.getContentDescription()).isEqualTo(DESCRIPTION);
124 Intent intent = result.resolvedIntent;
125 assertThat(intent.getAction()).isEqualTo(intent.getAction());
126 assertThat(intent.getComponent()).isNotNull();
127 }
128
129 @Test
130 public void resolve_titleChooserReturnsNull() {
131 LabeledIntent labeledIntent = new LabeledIntent(
132 TITLE_WITHOUT_ENTITY,
133 null,
134 DESCRIPTION,
Tony Mak15b64be2019-04-01 20:02:29 +0100135 null,
Tony Makac9b4d82019-02-15 13:57:38 +0000136 INTENT,
137 REQUEST_CODE
138 );
139
Tony Mak72e17972019-03-16 10:28:42 +0000140 LabeledIntent.Result result = labeledIntent.resolve(
141 mContext, (labeledIntent1, resolveInfo) -> null, TEXT_LANGUAGES_BUNDLE);
Tony Makac9b4d82019-02-15 13:57:38 +0000142
143 assertThat(result).isNotNull();
144 assertThat(result.remoteAction.getTitle()).isEqualTo(TITLE_WITHOUT_ENTITY);
145 assertThat(result.remoteAction.getContentDescription()).isEqualTo(DESCRIPTION);
146 Intent intent = result.resolvedIntent;
147 assertThat(intent.getAction()).isEqualTo(intent.getAction());
148 assertThat(intent.getComponent()).isNotNull();
149 }
150
Tony Makac9b4d82019-02-15 13:57:38 +0000151 @Test
152 public void resolve_missingTitle() {
153 assertThrows(
154 IllegalArgumentException.class,
155 () ->
156 new LabeledIntent(
157 null,
158 null,
159 DESCRIPTION,
Tony Mak15b64be2019-04-01 20:02:29 +0100160 null,
Tony Makac9b4d82019-02-15 13:57:38 +0000161 INTENT,
162 REQUEST_CODE
163 ));
164 }
Tony Makc12035e2019-02-26 17:45:34 +0000165
166 @Test
167 public void resolve_noIntentHandler() {
Tony Mak72e17972019-03-16 10:28:42 +0000168 // See setup(). mContext can only resolve Intent.ACTION_VIEW.
169 Intent unresolvableIntent = new Intent(Intent.ACTION_TRANSLATE);
Tony Makc12035e2019-02-26 17:45:34 +0000170 LabeledIntent labeledIntent = new LabeledIntent(
171 TITLE_WITHOUT_ENTITY,
172 null,
173 DESCRIPTION,
Tony Mak15b64be2019-04-01 20:02:29 +0100174 null,
Tony Mak72e17972019-03-16 10:28:42 +0000175 unresolvableIntent,
Tony Makc12035e2019-02-26 17:45:34 +0000176 REQUEST_CODE);
177
Tony Mak72e17972019-03-16 10:28:42 +0000178 LabeledIntent.Result result = labeledIntent.resolve(mContext, null, null);
Tony Makc12035e2019-02-26 17:45:34 +0000179
180 assertThat(result).isNull();
181 }
Tony Mak15b64be2019-04-01 20:02:29 +0100182
183 @Test
184 public void resolve_descriptionWithAppName() {
185 LabeledIntent labeledIntent = new LabeledIntent(
186 TITLE_WITHOUT_ENTITY,
187 TITLE_WITH_ENTITY,
188 DESCRIPTION,
189 DESCRIPTION_WITH_APP_NAME,
190 INTENT,
191 REQUEST_CODE
192 );
193
194 LabeledIntent.Result result = labeledIntent.resolve(
195 mContext, /*titleChooser*/ null, TEXT_LANGUAGES_BUNDLE);
196
197 assertThat(result).isNotNull();
198 assertThat(result.remoteAction.getContentDescription()).isEqualTo("Use fake to open map");
199 }
Tony Makac9b4d82019-02-15 13:57:38 +0000200}