blob: 8622b7efeee72e42753e58e189d4b59590c19710 [file] [log] [blame]
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001/*
2 * Copyright (C) 2016 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
17package com.android.internal.app;
18
Matt Pietal26038402019-01-08 07:29:34 -050019import static androidx.test.espresso.Espresso.onView;
20import static androidx.test.espresso.action.ViewActions.click;
Matt Pietal74c6ed02019-04-18 13:38:46 -040021import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
Matt Pietal26038402019-01-08 07:29:34 -050022import static androidx.test.espresso.assertion.ViewAssertions.matches;
23import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
24import static androidx.test.espresso.matcher.ViewMatchers.withId;
25import static androidx.test.espresso.matcher.ViewMatchers.withText;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090026
Mehdi Alizadeh06955f62019-09-11 17:23:10 -070027import static com.android.internal.app.ChooserActivity.TARGET_TYPE_CHOOSER_TARGET;
28import static com.android.internal.app.ChooserActivity.TARGET_TYPE_DEFAULT;
29import static com.android.internal.app.ChooserActivity.TARGET_TYPE_SHORTCUTS_FROM_PREDICTION_SERVICE;
30import static com.android.internal.app.ChooserActivity.TARGET_TYPE_SHORTCUTS_FROM_SHORTCUT_MANAGER;
arangelovb0802dc2019-10-18 18:03:44 +010031import static com.android.internal.app.ChooserListAdapter.CALLER_TARGET_SCORE_BOOST;
32import static com.android.internal.app.ChooserListAdapter.SHORTCUT_TARGET_SCORE_BOOST;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090033import static com.android.internal.app.ChooserWrapperActivity.sOverrides;
34
35import static org.hamcrest.CoreMatchers.is;
36import static org.hamcrest.CoreMatchers.not;
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -050037import static org.hamcrest.CoreMatchers.notNullValue;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090038import static org.hamcrest.MatcherAssert.assertThat;
Mehdi Alizadeh707c0cf2019-09-03 18:11:48 -070039import static org.junit.Assert.assertEquals;
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -050040import static org.mockito.Mockito.atLeastOnce;
Matt Pietalf38e9d22019-02-15 10:01:03 -050041import static org.mockito.Mockito.mock;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090042import static org.mockito.Mockito.times;
43import static org.mockito.Mockito.verify;
44import static org.mockito.Mockito.when;
45
46import android.app.usage.UsageStatsManager;
Matt Pietal26038402019-01-08 07:29:34 -050047import android.content.ClipData;
Matt Pietal1fa7d802019-01-30 10:44:15 -050048import android.content.ClipDescription;
49import android.content.ClipboardManager;
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -040050import android.content.ComponentName;
Matt Pietal1fa7d802019-01-30 10:44:15 -050051import android.content.Context;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090052import android.content.Intent;
George Hodulik145b3a52019-03-27 11:18:43 -070053import android.content.pm.PackageManager;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090054import android.content.pm.ResolveInfo;
Mehdi Alizadeh707c0cf2019-09-03 18:11:48 -070055import android.content.pm.ShortcutInfo;
56import android.content.pm.ShortcutManager.ShareShortcutInfo;
Matt Pietalf38e9d22019-02-15 10:01:03 -050057import android.database.Cursor;
Matt Pietal26038402019-01-08 07:29:34 -050058import android.graphics.Bitmap;
59import android.graphics.Canvas;
60import android.graphics.Color;
61import android.graphics.Paint;
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -040062import android.graphics.drawable.Icon;
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -050063import android.metrics.LogMaker;
Matt Pietal26038402019-01-08 07:29:34 -050064import android.net.Uri;
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -040065import android.service.chooser.ChooserTarget;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090066
Matt Pietal26038402019-01-08 07:29:34 -050067import androidx.test.platform.app.InstrumentationRegistry;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090068import androidx.test.rule.ActivityTestRule;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090069
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080070import com.android.internal.R;
71import com.android.internal.app.ResolverActivity.ResolvedComponentInfo;
arangelovb0802dc2019-10-18 18:03:44 +010072import com.android.internal.app.chooser.DisplayResolveInfo;
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -050073import com.android.internal.logging.MetricsLogger;
Susi Kharraz-Post0446fab2019-02-21 09:42:31 -050074import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080075
76import org.junit.Before;
77import org.junit.Rule;
78import org.junit.Test;
79import org.junit.runner.RunWith;
George Hodulik145b3a52019-03-27 11:18:43 -070080import org.junit.runners.Parameterized;
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -050081import org.mockito.ArgumentCaptor;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080082import org.mockito.Mockito;
83
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080084import java.util.ArrayList;
Susi Kharraz-Post8c14f772019-04-17 16:33:41 -040085import java.util.Arrays;
86import java.util.Collection;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080087import java.util.List;
Susi Kharraz-Post8c14f772019-04-17 16:33:41 -040088import java.util.function.Function;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080089
90/**
91 * Chooser activity instrumentation tests
92 */
George Hodulik145b3a52019-03-27 11:18:43 -070093@RunWith(Parameterized.class)
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080094public class ChooserActivityTest {
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -050095
George Hodulik145b3a52019-03-27 11:18:43 -070096 private static final Function<PackageManager, PackageManager> DEFAULT_PM = pm -> pm;
97 private static final Function<PackageManager, PackageManager> NO_APP_PREDICTION_SERVICE_PM =
98 pm -> {
99 PackageManager mock = Mockito.spy(pm);
100 when(mock.getAppPredictionServicePackageName()).thenReturn(null);
101 return mock;
102 };
103
104 @Parameterized.Parameters
105 public static Collection packageManagers() {
106 return Arrays.asList(new Object[][] {
107 {0, "Default PackageManager", DEFAULT_PM},
108 {1, "No App Prediction Service", NO_APP_PREDICTION_SERVICE_PM}
109 });
110 }
111
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -0500112 private static final int CONTENT_PREVIEW_IMAGE = 1;
113 private static final int CONTENT_PREVIEW_FILE = 2;
114 private static final int CONTENT_PREVIEW_TEXT = 3;
George Hodulik145b3a52019-03-27 11:18:43 -0700115 private Function<PackageManager, PackageManager> mPackageManagerOverride;
116 private int mTestNum;
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -0500117
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800118 @Rule
119 public ActivityTestRule<ChooserWrapperActivity> mActivityRule =
120 new ActivityTestRule<>(ChooserWrapperActivity.class, false,
121 false);
122
George Hodulik145b3a52019-03-27 11:18:43 -0700123 public ChooserActivityTest(
124 int testNum,
125 String testName,
126 Function<PackageManager, PackageManager> packageManagerOverride) {
127 mPackageManagerOverride = packageManagerOverride;
128 mTestNum = testNum;
129 }
130
Makoto Onuki99302b52017-03-29 12:42:26 -0700131 @Before
132 public void cleanOverrideData() {
133 sOverrides.reset();
George Hodulik145b3a52019-03-27 11:18:43 -0700134 sOverrides.createPackageManager = mPackageManagerOverride;
Makoto Onuki99302b52017-03-29 12:42:26 -0700135 }
136
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800137 @Test
138 public void customTitle() throws InterruptedException {
Matt Pietal26038402019-01-08 07:29:34 -0500139 Intent viewIntent = createViewTextIntent();
140 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
141
142 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
143 Mockito.anyBoolean(),
144 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
Matt Pietal95574b02019-03-13 08:12:25 -0400145 final ChooserWrapperActivity activity = mActivityRule.launchActivity(
146 Intent.createChooser(viewIntent, "chooser test"));
Matt Pietal26038402019-01-08 07:29:34 -0500147
148 waitForIdle();
Matt Pietal95574b02019-03-13 08:12:25 -0400149 assertThat(activity.getAdapter().getCount(), is(2));
150 assertThat(activity.getAdapter().getServiceTargetCount(), is(0));
Matt Pietal26038402019-01-08 07:29:34 -0500151 onView(withId(R.id.title)).check(matches(withText("chooser test")));
152 }
153
154 @Test
155 public void customTitleIgnoredForSendIntents() throws InterruptedException {
156 Intent sendIntent = createSendTextIntent();
Hakan Seyalioglu79b69f02017-01-12 17:08:02 -0800157 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
158
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800159 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
160 Mockito.anyBoolean(),
Hakan Seyalioglu79b69f02017-01-12 17:08:02 -0800161 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800162 mActivityRule.launchActivity(Intent.createChooser(sendIntent, "chooser test"));
163 waitForIdle();
Matt Pietal26038402019-01-08 07:29:34 -0500164 onView(withId(R.id.title)).check(matches(withText(R.string.whichSendApplication)));
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800165 }
166
167 @Test
168 public void emptyTitle() throws InterruptedException {
Matt Pietal26038402019-01-08 07:29:34 -0500169 Intent sendIntent = createSendTextIntent();
Hakan Seyalioglu79b69f02017-01-12 17:08:02 -0800170 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
171
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800172 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
173 Mockito.anyBoolean(),
Hakan Seyalioglu79b69f02017-01-12 17:08:02 -0800174 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800175 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
176 waitForIdle();
177 onView(withId(R.id.title))
178 .check(matches(withText(R.string.whichSendApplication)));
179 }
180
181 @Test
Matt Pietal26038402019-01-08 07:29:34 -0500182 public void emptyPreviewTitleAndThumbnail() throws InterruptedException {
183 Intent sendIntent = createSendTextIntentWithPreview(null, null);
184 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
185
186 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
187 Mockito.anyBoolean(),
188 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
189 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
190 waitForIdle();
191 onView(withId(R.id.content_preview_title)).check(matches(not(isDisplayed())));
192 onView(withId(R.id.content_preview_thumbnail)).check(matches(not(isDisplayed())));
193 }
194
195 @Test
196 public void visiblePreviewTitleWithoutThumbnail() throws InterruptedException {
197 String previewTitle = "My Content Preview Title";
198 Intent sendIntent = createSendTextIntentWithPreview(previewTitle, null);
199 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
200
201 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
202 Mockito.anyBoolean(),
203 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
204 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
205 waitForIdle();
206 onView(withId(R.id.content_preview_title)).check(matches(isDisplayed()));
207 onView(withId(R.id.content_preview_title)).check(matches(withText(previewTitle)));
208 onView(withId(R.id.content_preview_thumbnail)).check(matches(not(isDisplayed())));
209 }
210
211 @Test
212 public void visiblePreviewTitleWithInvalidThumbnail() throws InterruptedException {
213 String previewTitle = "My Content Preview Title";
214 Intent sendIntent = createSendTextIntentWithPreview(previewTitle,
215 Uri.parse("tel:(+49)12345789"));
216 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
217
218 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
219 Mockito.anyBoolean(),
220 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
221 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
222 waitForIdle();
223 onView(withId(R.id.content_preview_title)).check(matches(isDisplayed()));
224 onView(withId(R.id.content_preview_thumbnail)).check(matches(not(isDisplayed())));
225 }
226
227 @Test
228 public void visiblePreviewTitleAndThumbnail() throws InterruptedException {
229 String previewTitle = "My Content Preview Title";
230 Intent sendIntent = createSendTextIntentWithPreview(previewTitle,
231 Uri.parse("android.resource://com.android.frameworks.coretests/"
232 + com.android.frameworks.coretests.R.drawable.test320x240));
233 sOverrides.previewThumbnail = createBitmap();
234 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
235
236 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
237 Mockito.anyBoolean(),
238 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
239 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
240 waitForIdle();
241 onView(withId(R.id.content_preview_title)).check(matches(isDisplayed()));
242 onView(withId(R.id.content_preview_thumbnail)).check(matches(isDisplayed()));
243 }
244
245 @Test
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800246 public void twoOptionsAndUserSelectsOne() throws InterruptedException {
Matt Pietal26038402019-01-08 07:29:34 -0500247 Intent sendIntent = createSendTextIntent();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800248 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
249
250 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
251 Mockito.anyBoolean(),
252 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
253
254 final ChooserWrapperActivity activity = mActivityRule
255 .launchActivity(Intent.createChooser(sendIntent, null));
256 waitForIdle();
257
258 assertThat(activity.getAdapter().getCount(), is(2));
Matt Pietal74c6ed02019-04-18 13:38:46 -0400259 onView(withId(R.id.profile_button)).check(doesNotExist());
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800260
261 ResolveInfo[] chosen = new ResolveInfo[1];
262 sOverrides.onSafelyStartCallback = targetInfo -> {
263 chosen[0] = targetInfo.getResolveInfo();
264 return true;
265 };
266
267 ResolveInfo toChoose = resolvedComponentInfos.get(0).getResolveInfoAt(0);
268 onView(withText(toChoose.activityInfo.name))
269 .perform(click());
270 waitForIdle();
271 assertThat(chosen[0], is(toChoose));
272 }
273
274 @Test
Kang Li9fa2a2c2017-01-06 13:33:24 -0800275 public void updateChooserCountsAndModelAfterUserSelection() throws InterruptedException {
Matt Pietal26038402019-01-08 07:29:34 -0500276 Intent sendIntent = createSendTextIntent();
Kang Li64b018e2017-01-05 17:30:06 -0800277 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
278
279 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
280 Mockito.anyBoolean(),
281 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
282
283 final ChooserWrapperActivity activity = mActivityRule
284 .launchActivity(Intent.createChooser(sendIntent, null));
285 waitForIdle();
286 UsageStatsManager usm = activity.getUsageStatsManager();
287 verify(sOverrides.resolverListController, times(1))
Zhen Zhangaa458b12019-10-16 12:46:44 -0700288 .topK(Mockito.any(List.class), Mockito.anyInt());
Kang Li64b018e2017-01-05 17:30:06 -0800289 assertThat(activity.getIsSelected(), is(false));
290 sOverrides.onSafelyStartCallback = targetInfo -> {
291 return true;
292 };
Kang Li64b018e2017-01-05 17:30:06 -0800293 ResolveInfo toChoose = resolvedComponentInfos.get(0).getResolveInfoAt(0);
Kang Li64b018e2017-01-05 17:30:06 -0800294 onView(withText(toChoose.activityInfo.name))
295 .perform(click());
296 waitForIdle();
297 verify(sOverrides.resolverListController, times(1))
Kang Li9fa2a2c2017-01-06 13:33:24 -0800298 .updateChooserCounts(Mockito.anyString(), Mockito.anyInt(), Mockito.anyString());
299 verify(sOverrides.resolverListController, times(1))
Kang Li64b018e2017-01-05 17:30:06 -0800300 .updateModel(toChoose.activityInfo.getComponentName());
301 assertThat(activity.getIsSelected(), is(true));
Kang Li64b018e2017-01-05 17:30:06 -0800302 }
303
304 @Test
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800305 public void noResultsFromPackageManager() {
306 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
307 Mockito.anyBoolean(),
308 Mockito.isA(List.class))).thenReturn(null);
Matt Pietal26038402019-01-08 07:29:34 -0500309 Intent sendIntent = createSendTextIntent();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800310 final ChooserWrapperActivity activity = mActivityRule
311 .launchActivity(Intent.createChooser(sendIntent, null));
312 waitForIdle();
313 assertThat(activity.isFinishing(), is(false));
314
315 onView(withId(R.id.empty)).check(matches(isDisplayed()));
arangelov2c1c37a2019-12-06 14:43:34 +0000316 onView(withId(R.id.profile_pager)).check(matches(not(isDisplayed())));
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800317 InstrumentationRegistry.getInstrumentation().runOnMainSync(
318 () -> activity.getAdapter().handlePackagesChanged()
319 );
320 // backward compatibility. looks like we finish when data is empty after package change
321 assertThat(activity.isFinishing(), is(true));
322 }
323
324 @Test
325 public void autoLaunchSingleResult() throws InterruptedException {
326 ResolveInfo[] chosen = new ResolveInfo[1];
327 sOverrides.onSafelyStartCallback = targetInfo -> {
328 chosen[0] = targetInfo.getResolveInfo();
329 return true;
330 };
331
332 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(1);
333 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
334 Mockito.anyBoolean(),
335 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
336
Matt Pietal26038402019-01-08 07:29:34 -0500337 Intent sendIntent = createSendTextIntent();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800338 final ChooserWrapperActivity activity = mActivityRule
339 .launchActivity(Intent.createChooser(sendIntent, null));
340 waitForIdle();
341
342 assertThat(chosen[0], is(resolvedComponentInfos.get(0).getResolveInfoAt(0)));
343 assertThat(activity.isFinishing(), is(true));
344 }
345
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800346 @Test
347 public void hasOtherProfileOneOption() throws Exception {
Matt Pietal26038402019-01-08 07:29:34 -0500348 Intent sendIntent = createSendTextIntent();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800349 List<ResolvedComponentInfo> resolvedComponentInfos =
350 createResolvedComponentsForTestWithOtherProfile(2);
351 ResolveInfo toChoose = resolvedComponentInfos.get(1).getResolveInfoAt(0);
352
353 when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent(
354 Mockito.anyBoolean(),
355 Mockito.anyBoolean(),
356 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
357
358 final ChooserWrapperActivity activity = mActivityRule
359 .launchActivity(Intent.createChooser(sendIntent, null));
360 waitForIdle();
361
362 // The other entry is filtered to the other profile slot
363 assertThat(activity.getAdapter().getCount(), is(1));
364
365 ResolveInfo[] chosen = new ResolveInfo[1];
366 ChooserWrapperActivity.sOverrides.onSafelyStartCallback = targetInfo -> {
367 chosen[0] = targetInfo.getResolveInfo();
368 return true;
369 };
Makoto Onuki99302b52017-03-29 12:42:26 -0700370
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800371 // Make a stable copy of the components as the original list may be modified
372 List<ResolvedComponentInfo> stableCopy =
373 createResolvedComponentsForTestWithOtherProfile(2);
374 // Check that the "Other Profile" activity is put in the right spot
375 onView(withId(R.id.profile_button)).check(matches(
376 withText(stableCopy.get(0).getResolveInfoAt(0).activityInfo.name)));
377 onView(withText(stableCopy.get(1).getResolveInfoAt(0).activityInfo.name))
378 .perform(click());
379 waitForIdle();
380 assertThat(chosen[0], is(toChoose));
381 }
382
383 @Test
384 public void hasOtherProfileTwoOptionsAndUserSelectsOne() throws Exception {
Matt Pietal26038402019-01-08 07:29:34 -0500385 Intent sendIntent = createSendTextIntent();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800386 List<ResolvedComponentInfo> resolvedComponentInfos =
387 createResolvedComponentsForTestWithOtherProfile(3);
388 ResolveInfo toChoose = resolvedComponentInfos.get(1).getResolveInfoAt(0);
389
390 when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent(
391 Mockito.anyBoolean(),
392 Mockito.anyBoolean(),
393 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
394 when(ChooserWrapperActivity.sOverrides.resolverListController.getLastChosen())
395 .thenReturn(resolvedComponentInfos.get(0).getResolveInfoAt(0));
396
397 final ChooserWrapperActivity activity = mActivityRule
398 .launchActivity(Intent.createChooser(sendIntent, null));
399 waitForIdle();
400
401 // The other entry is filtered to the other profile slot
402 assertThat(activity.getAdapter().getCount(), is(2));
403
404 ResolveInfo[] chosen = new ResolveInfo[1];
405 ChooserWrapperActivity.sOverrides.onSafelyStartCallback = targetInfo -> {
406 chosen[0] = targetInfo.getResolveInfo();
407 return true;
408 };
409
410 // Make a stable copy of the components as the original list may be modified
411 List<ResolvedComponentInfo> stableCopy =
412 createResolvedComponentsForTestWithOtherProfile(3);
413 // Check that the "Other Profile" activity is put in the right spot
414 onView(withId(R.id.profile_button)).check(matches(
415 withText(stableCopy.get(0).getResolveInfoAt(0).activityInfo.name)));
416 onView(withText(stableCopy.get(1).getResolveInfoAt(0).activityInfo.name))
417 .perform(click());
418 waitForIdle();
419 assertThat(chosen[0], is(toChoose));
420 }
421
422 @Test
423 public void hasLastChosenActivityAndOtherProfile() throws Exception {
Matt Pietal26038402019-01-08 07:29:34 -0500424 Intent sendIntent = createSendTextIntent();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800425 List<ResolvedComponentInfo> resolvedComponentInfos =
426 createResolvedComponentsForTestWithOtherProfile(3);
427 ResolveInfo toChoose = resolvedComponentInfos.get(1).getResolveInfoAt(0);
428
429 when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent(
430 Mockito.anyBoolean(),
431 Mockito.anyBoolean(),
432 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
433
434 final ChooserWrapperActivity activity = mActivityRule
435 .launchActivity(Intent.createChooser(sendIntent, null));
436 waitForIdle();
437
438 // The other entry is filtered to the last used slot
439 assertThat(activity.getAdapter().getCount(), is(2));
440
441 ResolveInfo[] chosen = new ResolveInfo[1];
442 ChooserWrapperActivity.sOverrides.onSafelyStartCallback = targetInfo -> {
443 chosen[0] = targetInfo.getResolveInfo();
444 return true;
445 };
446
447 // Make a stable copy of the components as the original list may be modified
448 List<ResolvedComponentInfo> stableCopy =
449 createResolvedComponentsForTestWithOtherProfile(3);
450 // Check that the "Other Profile" activity is put in the right spot
451 onView(withId(R.id.profile_button)).check(matches(
452 withText(stableCopy.get(0).getResolveInfoAt(0).activityInfo.name)));
453 onView(withText(stableCopy.get(1).getResolveInfoAt(0).activityInfo.name))
454 .perform(click());
455 waitForIdle();
456 assertThat(chosen[0], is(toChoose));
457 }
458
Matt Pietal1fa7d802019-01-30 10:44:15 -0500459 @Test
460 public void copyTextToClipboard() throws Exception {
461 Intent sendIntent = createSendTextIntent();
Matt Pietal46d828c2019-02-05 08:07:07 -0500462 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
Matt Pietal1fa7d802019-01-30 10:44:15 -0500463
464 when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent(
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -0500465 Mockito.anyBoolean(),
466 Mockito.anyBoolean(),
467 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
Matt Pietal1fa7d802019-01-30 10:44:15 -0500468
469 final ChooserWrapperActivity activity = mActivityRule
470 .launchActivity(Intent.createChooser(sendIntent, null));
471 waitForIdle();
472
Matt Pietal46d828c2019-02-05 08:07:07 -0500473 onView(withId(R.id.copy_button)).check(matches(isDisplayed()));
Matt Pietal1fa7d802019-01-30 10:44:15 -0500474 onView(withId(R.id.copy_button)).perform(click());
Matt Pietal1fa7d802019-01-30 10:44:15 -0500475 ClipboardManager clipboard = (ClipboardManager) activity.getSystemService(
476 Context.CLIPBOARD_SERVICE);
477 ClipData clipData = clipboard.getPrimaryClip();
478 assertThat("testing intent sending", is(clipData.getItemAt(0).getText()));
479
480 ClipDescription clipDescription = clipData.getDescription();
481 assertThat("text/plain", is(clipDescription.getMimeType(0)));
482 }
483
Matt Pietal0ea391b2019-01-30 10:44:15 -0500484 @Test
Susi Kharraz-Post9e913182019-09-19 11:20:01 -0400485 public void copyTextToClipboardLogging() throws Exception {
486 Intent sendIntent = createSendTextIntent();
487 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
488
489 when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent(
490 Mockito.anyBoolean(),
491 Mockito.anyBoolean(),
492 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
493
494 MetricsLogger mockLogger = sOverrides.metricsLogger;
495 ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class);
496
497 final ChooserWrapperActivity activity = mActivityRule
498 .launchActivity(Intent.createChooser(sendIntent, null));
499 waitForIdle();
500
501 onView(withId(R.id.copy_button)).check(matches(isDisplayed()));
502 onView(withId(R.id.copy_button)).perform(click());
503
504 verify(mockLogger, atLeastOnce()).write(logMakerCaptor.capture());
505 // First is Activity shown, Second is "with preview"
506 assertThat(logMakerCaptor.getAllValues().get(2).getCategory(),
507 is(MetricsEvent.ACTION_ACTIVITY_CHOOSER_PICKED_SYSTEM_TARGET));
508 assertThat(logMakerCaptor
509 .getAllValues().get(2)
510 .getSubtype(),
511 is(1));
512 }
513
514 @Test
Matt Pietal0ea391b2019-01-30 10:44:15 -0500515 public void oneVisibleImagePreview() throws InterruptedException {
516 Uri uri = Uri.parse("android.resource://com.android.frameworks.coretests/"
517 + com.android.frameworks.coretests.R.drawable.test320x240);
518
519 ArrayList<Uri> uris = new ArrayList<>();
520 uris.add(uri);
521
Matt Pietal46d828c2019-02-05 08:07:07 -0500522 Intent sendIntent = createSendUriIntentWithPreview(uris);
Matt Pietal0ea391b2019-01-30 10:44:15 -0500523 sOverrides.previewThumbnail = createBitmap();
Matt Pietal46d828c2019-02-05 08:07:07 -0500524 sOverrides.isImageType = true;
Matt Pietal0ea391b2019-01-30 10:44:15 -0500525
526 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
527
528 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
529 Mockito.anyBoolean(),
530 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
531 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
532 waitForIdle();
533 onView(withId(R.id.content_preview_image_1_large)).check(matches(isDisplayed()));
534 onView(withId(R.id.content_preview_image_2_large)).check(matches(not(isDisplayed())));
535 onView(withId(R.id.content_preview_image_2_small)).check(matches(not(isDisplayed())));
536 onView(withId(R.id.content_preview_image_3_small)).check(matches(not(isDisplayed())));
537 }
538
539 @Test
540 public void twoVisibleImagePreview() throws InterruptedException {
541 Uri uri = Uri.parse("android.resource://com.android.frameworks.coretests/"
542 + com.android.frameworks.coretests.R.drawable.test320x240);
543
544 ArrayList<Uri> uris = new ArrayList<>();
545 uris.add(uri);
546 uris.add(uri);
547
Matt Pietal46d828c2019-02-05 08:07:07 -0500548 Intent sendIntent = createSendUriIntentWithPreview(uris);
Matt Pietal0ea391b2019-01-30 10:44:15 -0500549 sOverrides.previewThumbnail = createBitmap();
Matt Pietal46d828c2019-02-05 08:07:07 -0500550 sOverrides.isImageType = true;
Matt Pietal0ea391b2019-01-30 10:44:15 -0500551
552 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
553
554 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
555 Mockito.anyBoolean(),
556 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
557 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
558 waitForIdle();
559 onView(withId(R.id.content_preview_image_1_large)).check(matches(isDisplayed()));
560 onView(withId(R.id.content_preview_image_2_large)).check(matches(isDisplayed()));
561 onView(withId(R.id.content_preview_image_2_small)).check(matches(not(isDisplayed())));
562 onView(withId(R.id.content_preview_image_3_small)).check(matches(not(isDisplayed())));
563 }
564
565 @Test
566 public void threeOrMoreVisibleImagePreview() throws InterruptedException {
567 Uri uri = Uri.parse("android.resource://com.android.frameworks.coretests/"
568 + com.android.frameworks.coretests.R.drawable.test320x240);
569
570 ArrayList<Uri> uris = new ArrayList<>();
571 uris.add(uri);
572 uris.add(uri);
573 uris.add(uri);
574 uris.add(uri);
575 uris.add(uri);
576
Matt Pietal46d828c2019-02-05 08:07:07 -0500577 Intent sendIntent = createSendUriIntentWithPreview(uris);
Matt Pietal0ea391b2019-01-30 10:44:15 -0500578 sOverrides.previewThumbnail = createBitmap();
Matt Pietal46d828c2019-02-05 08:07:07 -0500579 sOverrides.isImageType = true;
Matt Pietal0ea391b2019-01-30 10:44:15 -0500580
581 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
582
583 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -0500584 Mockito.anyBoolean(),
585 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
Matt Pietal0ea391b2019-01-30 10:44:15 -0500586 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
587 waitForIdle();
588 onView(withId(R.id.content_preview_image_1_large)).check(matches(isDisplayed()));
589 onView(withId(R.id.content_preview_image_2_large)).check(matches(not(isDisplayed())));
590 onView(withId(R.id.content_preview_image_2_small)).check(matches(isDisplayed()));
591 onView(withId(R.id.content_preview_image_3_small)).check(matches(isDisplayed()));
592 }
593
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -0500594 @Test
595 public void testOnCreateLogging() {
596 Intent sendIntent = createSendTextIntent();
597 sendIntent.setType("TestType");
598
599 MetricsLogger mockLogger = sOverrides.metricsLogger;
600 ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class);
601 mActivityRule.launchActivity(Intent.createChooser(sendIntent, "logger test"));
602 waitForIdle();
603 verify(mockLogger, atLeastOnce()).write(logMakerCaptor.capture());
604 assertThat(logMakerCaptor.getAllValues().get(0).getCategory(),
Susi Kharraz-Post0446fab2019-02-21 09:42:31 -0500605 is(MetricsEvent.ACTION_ACTIVITY_CHOOSER_SHOWN));
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -0500606 assertThat(logMakerCaptor
607 .getAllValues().get(0)
Susi Kharraz-Post0446fab2019-02-21 09:42:31 -0500608 .getTaggedData(MetricsEvent.FIELD_TIME_TO_APP_TARGETS),
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -0500609 is(notNullValue()));
610 assertThat(logMakerCaptor
611 .getAllValues().get(0)
Susi Kharraz-Post0446fab2019-02-21 09:42:31 -0500612 .getTaggedData(MetricsEvent.FIELD_SHARESHEET_MIMETYPE),
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -0500613 is("TestType"));
Susi Kharraz-Post0446fab2019-02-21 09:42:31 -0500614 assertThat(logMakerCaptor
615 .getAllValues().get(0)
616 .getSubtype(),
617 is(MetricsEvent.PARENT_PROFILE));
618 }
619
620 @Test
621 public void testOnCreateLoggingFromWorkProfile() {
622 Intent sendIntent = createSendTextIntent();
623 sendIntent.setType("TestType");
624 sOverrides.alternateProfileSetting = MetricsEvent.MANAGED_PROFILE;
625 MetricsLogger mockLogger = sOverrides.metricsLogger;
626 ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class);
627 mActivityRule.launchActivity(Intent.createChooser(sendIntent, "logger test"));
628 waitForIdle();
629 verify(mockLogger, atLeastOnce()).write(logMakerCaptor.capture());
630 assertThat(logMakerCaptor.getAllValues().get(0).getCategory(),
631 is(MetricsEvent.ACTION_ACTIVITY_CHOOSER_SHOWN));
632 assertThat(logMakerCaptor
633 .getAllValues().get(0)
634 .getTaggedData(MetricsEvent.FIELD_TIME_TO_APP_TARGETS),
635 is(notNullValue()));
636 assertThat(logMakerCaptor
637 .getAllValues().get(0)
638 .getTaggedData(MetricsEvent.FIELD_SHARESHEET_MIMETYPE),
639 is("TestType"));
640 assertThat(logMakerCaptor
641 .getAllValues().get(0)
642 .getSubtype(),
643 is(MetricsEvent.MANAGED_PROFILE));
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -0500644 }
645
646 @Test
647 public void testEmptyPreviewLogging() {
648 Intent sendIntent = createSendTextIntentWithPreview(null, null);
649
650 MetricsLogger mockLogger = sOverrides.metricsLogger;
651 ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class);
652 mActivityRule.launchActivity(Intent.createChooser(sendIntent, "empty preview logger test"));
653 waitForIdle();
Matt Pietal46d828c2019-02-05 08:07:07 -0500654
655 verify(mockLogger, Mockito.times(1)).write(logMakerCaptor.capture());
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -0500656 // First invocation is from onCreate
Matt Pietal46d828c2019-02-05 08:07:07 -0500657 assertThat(logMakerCaptor.getAllValues().get(0).getCategory(),
Susi Kharraz-Post0446fab2019-02-21 09:42:31 -0500658 is(MetricsEvent.ACTION_ACTIVITY_CHOOSER_SHOWN));
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -0500659 }
660
661 @Test
662 public void testTitlePreviewLogging() {
663 Intent sendIntent = createSendTextIntentWithPreview("TestTitle", null);
664
665 MetricsLogger mockLogger = sOverrides.metricsLogger;
666 ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class);
Matt Pietal46d828c2019-02-05 08:07:07 -0500667
668 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
669
670 when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent(
671 Mockito.anyBoolean(),
672 Mockito.anyBoolean(),
673 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
674
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -0500675 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
676 waitForIdle();
arangelov2c1c37a2019-12-06 14:43:34 +0000677 // Second invocation is from onCreate
Matt Pietalfe28f9a2019-03-22 07:59:58 -0400678 verify(mockLogger, Mockito.times(2)).write(logMakerCaptor.capture());
arangelov2c1c37a2019-12-06 14:43:34 +0000679 assertThat(logMakerCaptor.getAllValues().get(0).getSubtype(),
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -0500680 is(CONTENT_PREVIEW_TEXT));
arangelov2c1c37a2019-12-06 14:43:34 +0000681 assertThat(logMakerCaptor.getAllValues().get(0).getCategory(),
682 is(MetricsEvent.ACTION_SHARE_WITH_PREVIEW));
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -0500683 }
684
685 @Test
686 public void testImagePreviewLogging() {
687 Uri uri = Uri.parse("android.resource://com.android.frameworks.coretests/"
688 + com.android.frameworks.coretests.R.drawable.test320x240);
689
690 ArrayList<Uri> uris = new ArrayList<>();
691 uris.add(uri);
692
Matt Pietal46d828c2019-02-05 08:07:07 -0500693 Intent sendIntent = createSendUriIntentWithPreview(uris);
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -0500694 sOverrides.previewThumbnail = createBitmap();
Matt Pietal46d828c2019-02-05 08:07:07 -0500695 sOverrides.isImageType = true;
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -0500696
697 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
698
699 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
700 Mockito.anyBoolean(),
701 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
702
703 MetricsLogger mockLogger = sOverrides.metricsLogger;
704 ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class);
705 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
706 waitForIdle();
Matt Pietalfe28f9a2019-03-22 07:59:58 -0400707 verify(mockLogger, Mockito.times(2)).write(logMakerCaptor.capture());
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -0500708 // First invocation is from onCreate
arangelov2c1c37a2019-12-06 14:43:34 +0000709 assertThat(logMakerCaptor.getAllValues().get(0).getSubtype(),
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -0500710 is(CONTENT_PREVIEW_IMAGE));
arangelov2c1c37a2019-12-06 14:43:34 +0000711 assertThat(logMakerCaptor.getAllValues().get(0).getCategory(),
712 is(MetricsEvent.ACTION_SHARE_WITH_PREVIEW));
Susi Kharraz-Post7e2115d2019-02-01 16:51:22 -0500713 }
714
Matt Pietal46d828c2019-02-05 08:07:07 -0500715 @Test
716 public void oneVisibleFilePreview() throws InterruptedException {
717 Uri uri = Uri.parse("content://com.android.frameworks.coretests/app.pdf");
718
719 ArrayList<Uri> uris = new ArrayList<>();
720 uris.add(uri);
721
722 Intent sendIntent = createSendUriIntentWithPreview(uris);
723
724 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
725
726 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
727 Mockito.anyBoolean(),
728 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
729 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
730 waitForIdle();
731 onView(withId(R.id.content_preview_filename)).check(matches(isDisplayed()));
732 onView(withId(R.id.content_preview_filename)).check(matches(withText("app.pdf")));
733 onView(withId(R.id.content_preview_file_icon)).check(matches(isDisplayed()));
734 }
735
736
737 @Test
738 public void moreThanOneVisibleFilePreview() throws InterruptedException {
739 Uri uri = Uri.parse("content://com.android.frameworks.coretests/app.pdf");
740
741 ArrayList<Uri> uris = new ArrayList<>();
742 uris.add(uri);
743 uris.add(uri);
744 uris.add(uri);
745
746 Intent sendIntent = createSendUriIntentWithPreview(uris);
747
748 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
749
750 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
751 Mockito.anyBoolean(),
752 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
753 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
754 waitForIdle();
755 onView(withId(R.id.content_preview_filename)).check(matches(isDisplayed()));
756 onView(withId(R.id.content_preview_filename)).check(matches(withText("app.pdf + 2 files")));
757 onView(withId(R.id.content_preview_file_icon)).check(matches(isDisplayed()));
758 }
759
Matt Pietalf38e9d22019-02-15 10:01:03 -0500760 @Test
761 public void contentProviderThrowSecurityException() throws InterruptedException {
762 Uri uri = Uri.parse("content://com.android.frameworks.coretests/app.pdf");
763
764 ArrayList<Uri> uris = new ArrayList<>();
765 uris.add(uri);
766
767 Intent sendIntent = createSendUriIntentWithPreview(uris);
768
769 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
770 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
771 Mockito.anyBoolean(),
772 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
773
774 sOverrides.resolverForceException = true;
775
776 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
777 waitForIdle();
778 onView(withId(R.id.content_preview_filename)).check(matches(isDisplayed()));
779 onView(withId(R.id.content_preview_filename)).check(matches(withText("app.pdf")));
780 onView(withId(R.id.content_preview_file_icon)).check(matches(isDisplayed()));
781 }
782
783 @Test
784 public void contentProviderReturnsNoColumns() throws InterruptedException {
785 Uri uri = Uri.parse("content://com.android.frameworks.coretests/app.pdf");
786
787 ArrayList<Uri> uris = new ArrayList<>();
788 uris.add(uri);
789 uris.add(uri);
790
791 Intent sendIntent = createSendUriIntentWithPreview(uris);
792
793 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
794 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
795 Mockito.anyBoolean(),
796 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
797
798 Cursor cursor = mock(Cursor.class);
799 when(cursor.getCount()).thenReturn(1);
800 Mockito.doNothing().when(cursor).close();
801 when(cursor.moveToFirst()).thenReturn(true);
802 when(cursor.getColumnIndex(Mockito.anyString())).thenReturn(-1);
803
804 sOverrides.resolverCursor = cursor;
805
806 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
807 waitForIdle();
808 onView(withId(R.id.content_preview_filename)).check(matches(isDisplayed()));
809 onView(withId(R.id.content_preview_filename)).check(matches(withText("app.pdf + 1 file")));
810 onView(withId(R.id.content_preview_file_icon)).check(matches(isDisplayed()));
811 }
812
Mehdi Alizadeh06955f62019-09-11 17:23:10 -0700813 @Test
814 public void testGetBaseScore() {
815 final float testBaseScore = 0.89f;
816
817 Intent sendIntent = createSendTextIntent();
818 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
819
820 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
821 Mockito.anyBoolean(),
822 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
arangelovb0802dc2019-10-18 18:03:44 +0100823 when(sOverrides.resolverListController.getScore(Mockito.isA(DisplayResolveInfo.class)))
824 .thenReturn(testBaseScore);
Mehdi Alizadeh06955f62019-09-11 17:23:10 -0700825
826 final ChooserWrapperActivity activity = mActivityRule
827 .launchActivity(Intent.createChooser(sendIntent, null));
828 waitForIdle();
829
arangelovb0802dc2019-10-18 18:03:44 +0100830 final DisplayResolveInfo testDri =
Mehdi Alizadeh06955f62019-09-11 17:23:10 -0700831 activity.createTestDisplayResolveInfo(sendIntent,
arangelovb0802dc2019-10-18 18:03:44 +0100832 ResolverDataProvider.createResolveInfo(3, 0), "testLabel", "testInfo", sendIntent,
833 /* resolveInfoPresentationGetter */ null);
834 final ChooserListAdapter adapter = activity.getAdapter();
Mehdi Alizadeh06955f62019-09-11 17:23:10 -0700835
836 assertThat(adapter.getBaseScore(null, 0), is(CALLER_TARGET_SCORE_BOOST));
837 assertThat(adapter.getBaseScore(testDri, TARGET_TYPE_DEFAULT), is(testBaseScore));
838 assertThat(adapter.getBaseScore(testDri, TARGET_TYPE_CHOOSER_TARGET), is(testBaseScore));
839 assertThat(adapter.getBaseScore(testDri, TARGET_TYPE_SHORTCUTS_FROM_PREDICTION_SERVICE),
840 is(SHORTCUT_TARGET_SCORE_BOOST));
841 assertThat(adapter.getBaseScore(testDri, TARGET_TYPE_SHORTCUTS_FROM_SHORTCUT_MANAGER),
842 is(testBaseScore * SHORTCUT_TARGET_SCORE_BOOST));
843 }
844
Mehdi Alizadehe870e972019-09-11 17:54:15 -0700845 /**
846 * The case when AppPrediction service is not defined in PackageManager is already covered
847 * as a test parameter {@link ChooserActivityTest#packageManagers}. This test is checking the
848 * case when the prediction service is defined but the component is not available on the device.
849 */
850 @Test
851 public void testIsAppPredictionServiceAvailable() {
852 Intent sendIntent = createSendTextIntent();
853 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
854 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
855 Mockito.anyBoolean(),
856 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
857
858 final ChooserWrapperActivity activity = mActivityRule
859 .launchActivity(Intent.createChooser(sendIntent, null));
860 waitForIdle();
861
862 if (activity.getPackageManager().getAppPredictionServicePackageName() == null) {
863 assertThat(activity.isAppPredictionServiceAvailable(), is(false));
864 } else {
865 assertThat(activity.isAppPredictionServiceAvailable(), is(true));
866
867 sOverrides.resources = Mockito.spy(activity.getResources());
868 when(sOverrides.resources.getString(R.string.config_defaultAppPredictionService))
869 .thenReturn("ComponentNameThatDoesNotExist");
870
871 assertThat(activity.isAppPredictionServiceAvailable(), is(false));
872 }
873 }
874
Mehdi Alizadeh707c0cf2019-09-03 18:11:48 -0700875 @Test
876 public void testConvertToChooserTarget_predictionService() {
877 Intent sendIntent = createSendTextIntent();
878 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
879 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
880 Mockito.anyBoolean(),
881 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
882
883 final ChooserWrapperActivity activity = mActivityRule
884 .launchActivity(Intent.createChooser(sendIntent, null));
885 waitForIdle();
886
887 List<ShareShortcutInfo> shortcuts = createShortcuts(activity);
888
889 int[] expectedOrderAllShortcuts = {0, 1, 2, 3};
890 float[] expectedScoreAllShortcuts = {1.0f, 0.99f, 0.98f, 0.97f};
891
892 List<ChooserTarget> chooserTargets = activity.convertToChooserTarget(shortcuts, shortcuts,
893 null, TARGET_TYPE_SHORTCUTS_FROM_PREDICTION_SERVICE);
894 assertCorrectShortcutToChooserTargetConversion(shortcuts, chooserTargets,
895 expectedOrderAllShortcuts, expectedScoreAllShortcuts);
896
897 List<ShareShortcutInfo> subset = new ArrayList<>();
898 subset.add(shortcuts.get(1));
899 subset.add(shortcuts.get(2));
900 subset.add(shortcuts.get(3));
901
902 int[] expectedOrderSubset = {1, 2, 3};
903 float[] expectedScoreSubset = {0.99f, 0.98f, 0.97f};
904
905 chooserTargets = activity.convertToChooserTarget(subset, shortcuts, null,
906 TARGET_TYPE_SHORTCUTS_FROM_PREDICTION_SERVICE);
907 assertCorrectShortcutToChooserTargetConversion(shortcuts, chooserTargets,
908 expectedOrderSubset, expectedScoreSubset);
909 }
910
911 @Test
912 public void testConvertToChooserTarget_shortcutManager() {
913 Intent sendIntent = createSendTextIntent();
914 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
915 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
916 Mockito.anyBoolean(),
917 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
918
919 final ChooserWrapperActivity activity = mActivityRule
920 .launchActivity(Intent.createChooser(sendIntent, null));
921 waitForIdle();
922
923 List<ShareShortcutInfo> shortcuts = createShortcuts(activity);
924
925 int[] expectedOrderAllShortcuts = {2, 0, 3, 1};
926 float[] expectedScoreAllShortcuts = {1.0f, 0.99f, 0.99f, 0.98f};
927
928 List<ChooserTarget> chooserTargets = activity.convertToChooserTarget(shortcuts, shortcuts,
929 null, TARGET_TYPE_SHORTCUTS_FROM_SHORTCUT_MANAGER);
930 assertCorrectShortcutToChooserTargetConversion(shortcuts, chooserTargets,
931 expectedOrderAllShortcuts, expectedScoreAllShortcuts);
932
933 List<ShareShortcutInfo> subset = new ArrayList<>();
934 subset.add(shortcuts.get(1));
935 subset.add(shortcuts.get(2));
936 subset.add(shortcuts.get(3));
937
938 int[] expectedOrderSubset = {2, 3, 1};
939 float[] expectedScoreSubset = {1.0f, 0.99f, 0.98f};
940
941 chooserTargets = activity.convertToChooserTarget(subset, shortcuts, null,
942 TARGET_TYPE_SHORTCUTS_FROM_SHORTCUT_MANAGER);
943 assertCorrectShortcutToChooserTargetConversion(shortcuts, chooserTargets,
944 expectedOrderSubset, expectedScoreSubset);
945 }
946
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -0400947 // This test is too long and too slow and should not be taken as an example for future tests.
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -0400948 @Test
949 public void testDirectTargetSelectionLogging() throws InterruptedException {
950 Intent sendIntent = createSendTextIntent();
951 // We need app targets for direct targets to get displayed
952 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
953 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
954 Mockito.anyBoolean(),
955 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
956
957 // Set up resources
958 MetricsLogger mockLogger = sOverrides.metricsLogger;
959 ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class);
960 // Create direct share target
Susi Kharraz-Post8c14f772019-04-17 16:33:41 -0400961 List<ChooserTarget> serviceTargets = createDirectShareTargets(1, "");
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -0400962 ResolveInfo ri = ResolverDataProvider.createResolveInfo(3, 0);
963
964 // Start activity
965 final ChooserWrapperActivity activity = mActivityRule
966 .launchActivity(Intent.createChooser(sendIntent, null));
967
968 // Insert the direct share target
969 InstrumentationRegistry.getInstrumentation().runOnMainSync(
970 () -> activity.getAdapter().addServiceResults(
971 activity.createTestDisplayResolveInfo(sendIntent,
972 ri,
973 "testLabel",
974 "testInfo",
arangelovb0802dc2019-10-18 18:03:44 +0100975 sendIntent,
976 /* resolveInfoPresentationGetter */ null),
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -0400977 serviceTargets,
Mehdi Alizadeh06955f62019-09-11 17:23:10 -0700978 TARGET_TYPE_CHOOSER_TARGET)
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -0400979 );
980 // Thread.sleep shouldn't be a thing in an integration test but it's
981 // necessary here because of the way the code is structured
982 // TODO: restructure the tests b/129870719
983 Thread.sleep(ChooserActivity.LIST_VIEW_UPDATE_INTERVAL_IN_MILLIS);
984
Susi Kharraz-Post8c14f772019-04-17 16:33:41 -0400985 assertThat("Chooser should have 3 targets (2 apps, 1 direct)",
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -0400986 activity.getAdapter().getCount(), is(3));
987 assertThat("Chooser should have exactly one selectable direct target",
988 activity.getAdapter().getSelectableServiceTargetCount(), is(1));
989 assertThat("The resolver info must match the resolver info used to create the target",
990 activity.getAdapter().getItem(0).getResolveInfo(), is(ri));
991
992 // Click on the direct target
993 String name = serviceTargets.get(0).getTitle().toString();
994 onView(withText(name))
995 .perform(click());
996 waitForIdle();
997
998 // Currently we're seeing 3 invocations
999 // 1. ChooserActivity.onCreate()
1000 // 2. ChooserActivity$ChooserRowAdapter.createContentPreviewView()
1001 // 3. ChooserActivity.startSelected -- which is the one we're after
1002 verify(mockLogger, Mockito.times(3)).write(logMakerCaptor.capture());
1003 assertThat(logMakerCaptor.getAllValues().get(2).getCategory(),
1004 is(MetricsEvent.ACTION_ACTIVITY_CHOOSER_PICKED_SERVICE_TARGET));
1005 String hashedName = (String) logMakerCaptor
1006 .getAllValues().get(2).getTaggedData(MetricsEvent.FIELD_HASHED_TARGET_NAME);
1007 assertThat("Hash is not predictable but must be obfuscated",
1008 hashedName, is(not(name)));
Susi Kharraz-Post8c14f772019-04-17 16:33:41 -04001009 assertThat("The packages shouldn't match for app target and direct target", logMakerCaptor
1010 .getAllValues().get(2).getTaggedData(MetricsEvent.FIELD_RANKED_POSITION), is(-1));
1011 }
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -04001012
Susi Kharraz-Post8c14f772019-04-17 16:33:41 -04001013 // This test is too long and too slow and should not be taken as an example for future tests.
1014 @Test
1015 public void testDirectTargetLoggingWithRankedAppTarget() throws InterruptedException {
1016 Intent sendIntent = createSendTextIntent();
1017 // We need app targets for direct targets to get displayed
1018 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
1019 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
1020 Mockito.anyBoolean(),
1021 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -04001022
Susi Kharraz-Post8c14f772019-04-17 16:33:41 -04001023 // Set up resources
1024 MetricsLogger mockLogger = sOverrides.metricsLogger;
1025 ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class);
1026 // Create direct share target
1027 List<ChooserTarget> serviceTargets = createDirectShareTargets(1,
1028 resolvedComponentInfos.get(0).getResolveInfoAt(0).activityInfo.packageName);
1029 ResolveInfo ri = ResolverDataProvider.createResolveInfo(3, 0);
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -04001030
1031 // Start activity
Susi Kharraz-Post8c14f772019-04-17 16:33:41 -04001032 final ChooserWrapperActivity activity = mActivityRule
1033 .launchActivity(Intent.createChooser(sendIntent, null));
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -04001034
1035 // Insert the direct share target
1036 InstrumentationRegistry.getInstrumentation().runOnMainSync(
Susi Kharraz-Post8c14f772019-04-17 16:33:41 -04001037 () -> activity.getAdapter().addServiceResults(
1038 activity.createTestDisplayResolveInfo(sendIntent,
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -04001039 ri,
1040 "testLabel",
1041 "testInfo",
arangelovb0802dc2019-10-18 18:03:44 +01001042 sendIntent,
1043 /* resolveInfoPresentationGetter */ null),
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -04001044 serviceTargets,
Mehdi Alizadeh06955f62019-09-11 17:23:10 -07001045 TARGET_TYPE_CHOOSER_TARGET)
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -04001046 );
1047 // Thread.sleep shouldn't be a thing in an integration test but it's
1048 // necessary here because of the way the code is structured
1049 // TODO: restructure the tests b/129870719
1050 Thread.sleep(ChooserActivity.LIST_VIEW_UPDATE_INTERVAL_IN_MILLIS);
1051
Susi Kharraz-Post8c14f772019-04-17 16:33:41 -04001052 assertThat("Chooser should have 3 targets (2 apps, 1 direct)",
1053 activity.getAdapter().getCount(), is(3));
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -04001054 assertThat("Chooser should have exactly one selectable direct target",
Susi Kharraz-Post8c14f772019-04-17 16:33:41 -04001055 activity.getAdapter().getSelectableServiceTargetCount(), is(1));
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -04001056 assertThat("The resolver info must match the resolver info used to create the target",
Susi Kharraz-Post8c14f772019-04-17 16:33:41 -04001057 activity.getAdapter().getItem(0).getResolveInfo(), is(ri));
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -04001058
1059 // Click on the direct target
Susi Kharraz-Post8c14f772019-04-17 16:33:41 -04001060 String name = serviceTargets.get(0).getTitle().toString();
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -04001061 onView(withText(name))
1062 .perform(click());
1063 waitForIdle();
1064
Susi Kharraz-Post8c14f772019-04-17 16:33:41 -04001065 // Currently we're seeing 3 invocations
1066 // 1. ChooserActivity.onCreate()
1067 // 2. ChooserActivity$ChooserRowAdapter.createContentPreviewView()
1068 // 3. ChooserActivity.startSelected -- which is the one we're after
1069 verify(mockLogger, Mockito.times(3)).write(logMakerCaptor.capture());
1070 assertThat(logMakerCaptor.getAllValues().get(2).getCategory(),
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -04001071 is(MetricsEvent.ACTION_ACTIVITY_CHOOSER_PICKED_SERVICE_TARGET));
Susi Kharraz-Post8c14f772019-04-17 16:33:41 -04001072 assertThat("The packages should match for app target and direct target", logMakerCaptor
1073 .getAllValues().get(2).getTaggedData(MetricsEvent.FIELD_RANKED_POSITION), is(0));
1074 }
1075
1076 // This test is too long and too slow and should not be taken as an example for future tests.
1077 @Test
1078 public void testDirectTargetLoggingWithAppTargetNotRanked() throws InterruptedException {
1079 Intent sendIntent = createSendTextIntent();
1080 // We need app targets for direct targets to get displayed
1081 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(15);
1082 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
1083 Mockito.anyBoolean(),
1084 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
1085
1086 // Set up resources
1087 MetricsLogger mockLogger = sOverrides.metricsLogger;
1088 ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class);
1089 // Create direct share target
1090 List<ChooserTarget> serviceTargets = createDirectShareTargets(1,
1091 resolvedComponentInfos.get(14).getResolveInfoAt(0).activityInfo.packageName);
1092 ResolveInfo ri = ResolverDataProvider.createResolveInfo(16, 0);
1093
1094 // Start activity
1095 final ChooserWrapperActivity activity = mActivityRule
1096 .launchActivity(Intent.createChooser(sendIntent, null));
1097 // Insert the direct share target
1098 InstrumentationRegistry.getInstrumentation().runOnMainSync(
1099 () -> activity.getAdapter().addServiceResults(
1100 activity.createTestDisplayResolveInfo(sendIntent,
1101 ri,
1102 "testLabel",
1103 "testInfo",
arangelovb0802dc2019-10-18 18:03:44 +01001104 sendIntent,
1105 /* resolveInfoPresentationGetter */ null),
Susi Kharraz-Post8c14f772019-04-17 16:33:41 -04001106 serviceTargets,
Mehdi Alizadeh06955f62019-09-11 17:23:10 -07001107 TARGET_TYPE_CHOOSER_TARGET)
Susi Kharraz-Post8c14f772019-04-17 16:33:41 -04001108 );
1109 // Thread.sleep shouldn't be a thing in an integration test but it's
1110 // necessary here because of the way the code is structured
1111 // TODO: restructure the tests b/129870719
1112 Thread.sleep(ChooserActivity.LIST_VIEW_UPDATE_INTERVAL_IN_MILLIS);
1113
1114 assertThat("Chooser should have 20 targets (4 apps, 1 direct, 15 A-Z)",
1115 activity.getAdapter().getCount(), is(20));
1116 assertThat("Chooser should have exactly one selectable direct target",
1117 activity.getAdapter().getSelectableServiceTargetCount(), is(1));
1118 assertThat("The resolver info must match the resolver info used to create the target",
1119 activity.getAdapter().getItem(0).getResolveInfo(), is(ri));
1120
1121 // Click on the direct target
1122 String name = serviceTargets.get(0).getTitle().toString();
1123 onView(withText(name))
1124 .perform(click());
1125 waitForIdle();
1126
1127 // Currently we're seeing 3 invocations
1128 // 1. ChooserActivity.onCreate()
1129 // 2. ChooserActivity$ChooserRowAdapter.createContentPreviewView()
1130 // 3. ChooserActivity.startSelected -- which is the one we're after
1131 verify(mockLogger, Mockito.times(3)).write(logMakerCaptor.capture());
1132 assertThat(logMakerCaptor.getAllValues().get(2).getCategory(),
1133 is(MetricsEvent.ACTION_ACTIVITY_CHOOSER_PICKED_SERVICE_TARGET));
1134 assertThat("The packages shouldn't match for app target and direct target", logMakerCaptor
1135 .getAllValues().get(2).getTaggedData(MetricsEvent.FIELD_RANKED_POSITION), is(-1));
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -04001136 }
1137
Matt Pietal26038402019-01-08 07:29:34 -05001138 private Intent createSendTextIntent() {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001139 Intent sendIntent = new Intent();
1140 sendIntent.setAction(Intent.ACTION_SEND);
1141 sendIntent.putExtra(Intent.EXTRA_TEXT, "testing intent sending");
Matt Pietalcdfcd9a2019-03-05 08:31:47 -05001142 sendIntent.setType("text/plain");
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001143 return sendIntent;
1144 }
1145
Matt Pietal26038402019-01-08 07:29:34 -05001146 private Intent createSendTextIntentWithPreview(String title, Uri imageThumbnail) {
1147 Intent sendIntent = new Intent();
1148 sendIntent.setAction(Intent.ACTION_SEND);
1149 sendIntent.putExtra(Intent.EXTRA_TEXT, "testing intent sending");
1150 sendIntent.putExtra(Intent.EXTRA_TITLE, title);
1151 if (imageThumbnail != null) {
1152 ClipData.Item clipItem = new ClipData.Item(imageThumbnail);
1153 sendIntent.setClipData(new ClipData("Clip Label", new String[]{"image/png"}, clipItem));
1154 }
1155
1156 return sendIntent;
1157 }
1158
Matt Pietal46d828c2019-02-05 08:07:07 -05001159 private Intent createSendUriIntentWithPreview(ArrayList<Uri> uris) {
Matt Pietal0ea391b2019-01-30 10:44:15 -05001160 Intent sendIntent = new Intent();
1161
1162 if (uris.size() > 1) {
1163 sendIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
1164 sendIntent.putExtra(Intent.EXTRA_STREAM, uris);
1165 } else {
1166 sendIntent.setAction(Intent.ACTION_SEND);
1167 sendIntent.putExtra(Intent.EXTRA_STREAM, uris.get(0));
1168 }
1169
1170 return sendIntent;
1171 }
1172
Matt Pietal26038402019-01-08 07:29:34 -05001173 private Intent createViewTextIntent() {
1174 Intent viewIntent = new Intent();
1175 viewIntent.setAction(Intent.ACTION_VIEW);
1176 viewIntent.putExtra(Intent.EXTRA_TEXT, "testing intent viewing");
1177 return viewIntent;
1178 }
1179
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001180 private List<ResolvedComponentInfo> createResolvedComponentsForTest(int numberOfResults) {
1181 List<ResolvedComponentInfo> infoList = new ArrayList<>(numberOfResults);
1182 for (int i = 0; i < numberOfResults; i++) {
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -08001183 infoList.add(ResolverDataProvider.createResolvedComponentInfo(i));
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001184 }
1185 return infoList;
1186 }
1187
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001188 private List<ResolvedComponentInfo> createResolvedComponentsForTestWithOtherProfile(
1189 int numberOfResults) {
1190 List<ResolvedComponentInfo> infoList = new ArrayList<>(numberOfResults);
1191 for (int i = 0; i < numberOfResults; i++) {
1192 if (i == 0) {
1193 infoList.add(ResolverDataProvider.createResolvedComponentInfoWithOtherId(i));
1194 } else {
1195 infoList.add(ResolverDataProvider.createResolvedComponentInfo(i));
1196 }
1197 }
1198 return infoList;
1199 }
1200
Susi Kharraz-Post8c14f772019-04-17 16:33:41 -04001201 private List<ChooserTarget> createDirectShareTargets(int numberOfResults, String packageName) {
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -04001202 Icon icon = Icon.createWithBitmap(createBitmap());
1203 String testTitle = "testTitle";
1204 List<ChooserTarget> targets = new ArrayList<>();
1205 for (int i = 0; i < numberOfResults; i++) {
Susi Kharraz-Post8c14f772019-04-17 16:33:41 -04001206 ComponentName componentName;
1207 if (packageName.isEmpty()) {
1208 componentName = ResolverDataProvider.createComponentName(i);
1209 } else {
1210 componentName = new ComponentName(packageName, packageName + ".class");
1211 }
Susi Kharraz-Post14cbfcd2019-04-01 11:07:59 -04001212 ChooserTarget tempTarget = new ChooserTarget(
1213 testTitle + i,
1214 icon,
1215 (float) (1 - ((i + 1) / 10.0)),
1216 componentName,
1217 null);
1218 targets.add(tempTarget);
1219 }
1220 return targets;
1221 }
1222
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001223 private void waitForIdle() {
1224 InstrumentationRegistry.getInstrumentation().waitForIdleSync();
1225 }
Matt Pietal26038402019-01-08 07:29:34 -05001226
1227 private Bitmap createBitmap() {
1228 int width = 200;
1229 int height = 200;
1230 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
1231 Canvas canvas = new Canvas(bitmap);
1232
1233 Paint paint = new Paint();
1234 paint.setColor(Color.RED);
1235 paint.setStyle(Paint.Style.FILL);
1236 canvas.drawPaint(paint);
1237
1238 paint.setColor(Color.WHITE);
1239 paint.setAntiAlias(true);
1240 paint.setTextSize(14.f);
1241 paint.setTextAlign(Paint.Align.CENTER);
1242 canvas.drawText("Hi!", (width / 2.f), (height / 2.f), paint);
1243
1244 return bitmap;
1245 }
Mehdi Alizadeh707c0cf2019-09-03 18:11:48 -07001246
1247 private List<ShareShortcutInfo> createShortcuts(Context context) {
1248 Intent testIntent = new Intent("TestIntent");
1249
1250 List<ShareShortcutInfo> shortcuts = new ArrayList<>();
1251 shortcuts.add(new ShareShortcutInfo(
1252 new ShortcutInfo.Builder(context, "shortcut1")
1253 .setIntent(testIntent).setShortLabel("label1").setRank(3).build(), // 0 2
1254 new ComponentName("package1", "class1")));
1255 shortcuts.add(new ShareShortcutInfo(
1256 new ShortcutInfo.Builder(context, "shortcut2")
1257 .setIntent(testIntent).setShortLabel("label2").setRank(7).build(), // 1 3
1258 new ComponentName("package2", "class2")));
1259 shortcuts.add(new ShareShortcutInfo(
1260 new ShortcutInfo.Builder(context, "shortcut3")
1261 .setIntent(testIntent).setShortLabel("label3").setRank(1).build(), // 2 0
1262 new ComponentName("package3", "class3")));
1263 shortcuts.add(new ShareShortcutInfo(
1264 new ShortcutInfo.Builder(context, "shortcut4")
1265 .setIntent(testIntent).setShortLabel("label4").setRank(3).build(), // 3 2
1266 new ComponentName("package4", "class4")));
1267
1268 return shortcuts;
1269 }
1270
1271 private void assertCorrectShortcutToChooserTargetConversion(List<ShareShortcutInfo> shortcuts,
1272 List<ChooserTarget> chooserTargets, int[] expectedOrder, float[] expectedScores) {
1273 assertEquals(expectedOrder.length, chooserTargets.size());
1274 for (int i = 0; i < chooserTargets.size(); i++) {
1275 ChooserTarget ct = chooserTargets.get(i);
1276 ShortcutInfo si = shortcuts.get(expectedOrder[i]).getShortcutInfo();
1277 ComponentName cn = shortcuts.get(expectedOrder[i]).getTargetComponent();
1278
1279 assertEquals(si.getId(), ct.getIntentExtras().getString(Intent.EXTRA_SHORTCUT_ID));
1280 assertEquals(si.getShortLabel(), ct.getTitle());
1281 assertThat(Math.abs(expectedScores[i] - ct.getScore()) < 0.000001, is(true));
1282 assertEquals(cn.flattenToString(), ct.getComponentName().flattenToString());
1283 }
1284 }
Matt Pietal26038402019-01-08 07:29:34 -05001285}