blob: b6f56ada445e5ec7d9bee8e2f7e78991eea2a27c [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;
21import static androidx.test.espresso.assertion.ViewAssertions.matches;
22import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
23import static androidx.test.espresso.matcher.ViewMatchers.withId;
24import static androidx.test.espresso.matcher.ViewMatchers.withText;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090025
26import static com.android.internal.app.ChooserWrapperActivity.sOverrides;
27
28import static org.hamcrest.CoreMatchers.is;
29import static org.hamcrest.CoreMatchers.not;
30import static org.hamcrest.MatcherAssert.assertThat;
31import static org.mockito.Mockito.times;
32import static org.mockito.Mockito.verify;
33import static org.mockito.Mockito.when;
34
35import android.app.usage.UsageStatsManager;
Matt Pietal26038402019-01-08 07:29:34 -050036import android.content.ClipData;
Matt Pietal1fa7d802019-01-30 10:44:15 -050037import android.content.ClipDescription;
38import android.content.ClipboardManager;
39import android.content.Context;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090040import android.content.Intent;
41import android.content.pm.ResolveInfo;
Matt Pietal26038402019-01-08 07:29:34 -050042import android.graphics.Bitmap;
43import android.graphics.Canvas;
44import android.graphics.Color;
45import android.graphics.Paint;
46import android.net.Uri;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090047
Matt Pietal26038402019-01-08 07:29:34 -050048import androidx.test.ext.junit.runners.AndroidJUnit4;
49import androidx.test.platform.app.InstrumentationRegistry;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090050import androidx.test.rule.ActivityTestRule;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090051
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080052import com.android.internal.R;
53import com.android.internal.app.ResolverActivity.ResolvedComponentInfo;
54
55import org.junit.Before;
56import org.junit.Rule;
57import org.junit.Test;
58import org.junit.runner.RunWith;
59import org.mockito.Mockito;
60
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080061import java.util.ArrayList;
62import java.util.List;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080063
64/**
65 * Chooser activity instrumentation tests
66 */
67@RunWith(AndroidJUnit4.class)
68public class ChooserActivityTest {
69 @Rule
70 public ActivityTestRule<ChooserWrapperActivity> mActivityRule =
71 new ActivityTestRule<>(ChooserWrapperActivity.class, false,
72 false);
73
Makoto Onuki99302b52017-03-29 12:42:26 -070074 @Before
75 public void cleanOverrideData() {
76 sOverrides.reset();
77 }
78
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080079 @Test
80 public void customTitle() throws InterruptedException {
Matt Pietal26038402019-01-08 07:29:34 -050081 Intent viewIntent = createViewTextIntent();
82 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
83
84 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
85 Mockito.anyBoolean(),
86 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
87 mActivityRule.launchActivity(Intent.createChooser(viewIntent, "chooser test"));
88
89 waitForIdle();
90 onView(withId(R.id.title)).check(matches(withText("chooser test")));
91 }
92
93 @Test
94 public void customTitleIgnoredForSendIntents() throws InterruptedException {
95 Intent sendIntent = createSendTextIntent();
Hakan Seyalioglu79b69f02017-01-12 17:08:02 -080096 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
97
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080098 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
99 Mockito.anyBoolean(),
Hakan Seyalioglu79b69f02017-01-12 17:08:02 -0800100 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800101 mActivityRule.launchActivity(Intent.createChooser(sendIntent, "chooser test"));
102 waitForIdle();
Matt Pietal26038402019-01-08 07:29:34 -0500103 onView(withId(R.id.title)).check(matches(withText(R.string.whichSendApplication)));
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800104 }
105
106 @Test
107 public void emptyTitle() throws InterruptedException {
Matt Pietal26038402019-01-08 07:29:34 -0500108 Intent sendIntent = createSendTextIntent();
Hakan Seyalioglu79b69f02017-01-12 17:08:02 -0800109 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
110
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800111 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
112 Mockito.anyBoolean(),
Hakan Seyalioglu79b69f02017-01-12 17:08:02 -0800113 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800114 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
115 waitForIdle();
116 onView(withId(R.id.title))
117 .check(matches(withText(R.string.whichSendApplication)));
118 }
119
120 @Test
Matt Pietal26038402019-01-08 07:29:34 -0500121 public void emptyPreviewTitleAndThumbnail() throws InterruptedException {
122 Intent sendIntent = createSendTextIntentWithPreview(null, null);
123 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
124
125 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
126 Mockito.anyBoolean(),
127 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
128 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
129 waitForIdle();
130 onView(withId(R.id.content_preview_title)).check(matches(not(isDisplayed())));
131 onView(withId(R.id.content_preview_thumbnail)).check(matches(not(isDisplayed())));
132 }
133
134 @Test
135 public void visiblePreviewTitleWithoutThumbnail() throws InterruptedException {
136 String previewTitle = "My Content Preview Title";
137 Intent sendIntent = createSendTextIntentWithPreview(previewTitle, null);
138 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
139
140 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
141 Mockito.anyBoolean(),
142 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
143 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
144 waitForIdle();
145 onView(withId(R.id.content_preview_title)).check(matches(isDisplayed()));
146 onView(withId(R.id.content_preview_title)).check(matches(withText(previewTitle)));
147 onView(withId(R.id.content_preview_thumbnail)).check(matches(not(isDisplayed())));
148 }
149
150 @Test
151 public void visiblePreviewTitleWithInvalidThumbnail() throws InterruptedException {
152 String previewTitle = "My Content Preview Title";
153 Intent sendIntent = createSendTextIntentWithPreview(previewTitle,
154 Uri.parse("tel:(+49)12345789"));
155 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
156
157 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
158 Mockito.anyBoolean(),
159 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
160 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
161 waitForIdle();
162 onView(withId(R.id.content_preview_title)).check(matches(isDisplayed()));
163 onView(withId(R.id.content_preview_thumbnail)).check(matches(not(isDisplayed())));
164 }
165
166 @Test
167 public void visiblePreviewTitleAndThumbnail() throws InterruptedException {
168 String previewTitle = "My Content Preview Title";
169 Intent sendIntent = createSendTextIntentWithPreview(previewTitle,
170 Uri.parse("android.resource://com.android.frameworks.coretests/"
171 + com.android.frameworks.coretests.R.drawable.test320x240));
172 sOverrides.previewThumbnail = createBitmap();
173 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
174
175 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
176 Mockito.anyBoolean(),
177 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
178 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
179 waitForIdle();
180 onView(withId(R.id.content_preview_title)).check(matches(isDisplayed()));
181 onView(withId(R.id.content_preview_thumbnail)).check(matches(isDisplayed()));
182 }
183
184 @Test
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800185 public void twoOptionsAndUserSelectsOne() throws InterruptedException {
Matt Pietal26038402019-01-08 07:29:34 -0500186 Intent sendIntent = createSendTextIntent();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800187 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
188
189 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
190 Mockito.anyBoolean(),
191 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
192
193 final ChooserWrapperActivity activity = mActivityRule
194 .launchActivity(Intent.createChooser(sendIntent, null));
195 waitForIdle();
196
197 assertThat(activity.getAdapter().getCount(), is(2));
198 onView(withId(R.id.profile_button)).check(matches(not(isDisplayed())));
199
200 ResolveInfo[] chosen = new ResolveInfo[1];
201 sOverrides.onSafelyStartCallback = targetInfo -> {
202 chosen[0] = targetInfo.getResolveInfo();
203 return true;
204 };
205
206 ResolveInfo toChoose = resolvedComponentInfos.get(0).getResolveInfoAt(0);
207 onView(withText(toChoose.activityInfo.name))
208 .perform(click());
209 waitForIdle();
210 assertThat(chosen[0], is(toChoose));
211 }
212
213 @Test
Kang Li9fa2a2c2017-01-06 13:33:24 -0800214 public void updateChooserCountsAndModelAfterUserSelection() throws InterruptedException {
Matt Pietal26038402019-01-08 07:29:34 -0500215 Intent sendIntent = createSendTextIntent();
Kang Li64b018e2017-01-05 17:30:06 -0800216 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
217
218 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
219 Mockito.anyBoolean(),
220 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
221
222 final ChooserWrapperActivity activity = mActivityRule
223 .launchActivity(Intent.createChooser(sendIntent, null));
224 waitForIdle();
225 UsageStatsManager usm = activity.getUsageStatsManager();
226 verify(sOverrides.resolverListController, times(1))
227 .sort(Mockito.any(List.class));
228 assertThat(activity.getIsSelected(), is(false));
229 sOverrides.onSafelyStartCallback = targetInfo -> {
230 return true;
231 };
Kang Li64b018e2017-01-05 17:30:06 -0800232 ResolveInfo toChoose = resolvedComponentInfos.get(0).getResolveInfoAt(0);
Kang Li64b018e2017-01-05 17:30:06 -0800233 onView(withText(toChoose.activityInfo.name))
234 .perform(click());
235 waitForIdle();
236 verify(sOverrides.resolverListController, times(1))
Kang Li9fa2a2c2017-01-06 13:33:24 -0800237 .updateChooserCounts(Mockito.anyString(), Mockito.anyInt(), Mockito.anyString());
238 verify(sOverrides.resolverListController, times(1))
Kang Li64b018e2017-01-05 17:30:06 -0800239 .updateModel(toChoose.activityInfo.getComponentName());
240 assertThat(activity.getIsSelected(), is(true));
Kang Li64b018e2017-01-05 17:30:06 -0800241 }
242
243 @Test
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800244 public void noResultsFromPackageManager() {
245 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
246 Mockito.anyBoolean(),
247 Mockito.isA(List.class))).thenReturn(null);
Matt Pietal26038402019-01-08 07:29:34 -0500248 Intent sendIntent = createSendTextIntent();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800249 final ChooserWrapperActivity activity = mActivityRule
250 .launchActivity(Intent.createChooser(sendIntent, null));
251 waitForIdle();
252 assertThat(activity.isFinishing(), is(false));
253
254 onView(withId(R.id.empty)).check(matches(isDisplayed()));
255 onView(withId(R.id.resolver_list)).check(matches(not(isDisplayed())));
256 InstrumentationRegistry.getInstrumentation().runOnMainSync(
257 () -> activity.getAdapter().handlePackagesChanged()
258 );
259 // backward compatibility. looks like we finish when data is empty after package change
260 assertThat(activity.isFinishing(), is(true));
261 }
262
263 @Test
264 public void autoLaunchSingleResult() throws InterruptedException {
265 ResolveInfo[] chosen = new ResolveInfo[1];
266 sOverrides.onSafelyStartCallback = targetInfo -> {
267 chosen[0] = targetInfo.getResolveInfo();
268 return true;
269 };
270
271 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(1);
272 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
273 Mockito.anyBoolean(),
274 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
275
Matt Pietal26038402019-01-08 07:29:34 -0500276 Intent sendIntent = createSendTextIntent();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800277 final ChooserWrapperActivity activity = mActivityRule
278 .launchActivity(Intent.createChooser(sendIntent, null));
279 waitForIdle();
280
281 assertThat(chosen[0], is(resolvedComponentInfos.get(0).getResolveInfoAt(0)));
282 assertThat(activity.isFinishing(), is(true));
283 }
284
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800285 @Test
286 public void hasOtherProfileOneOption() throws Exception {
Matt Pietal26038402019-01-08 07:29:34 -0500287 Intent sendIntent = createSendTextIntent();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800288 List<ResolvedComponentInfo> resolvedComponentInfos =
289 createResolvedComponentsForTestWithOtherProfile(2);
290 ResolveInfo toChoose = resolvedComponentInfos.get(1).getResolveInfoAt(0);
291
292 when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent(
293 Mockito.anyBoolean(),
294 Mockito.anyBoolean(),
295 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
296
297 final ChooserWrapperActivity activity = mActivityRule
298 .launchActivity(Intent.createChooser(sendIntent, null));
299 waitForIdle();
300
301 // The other entry is filtered to the other profile slot
302 assertThat(activity.getAdapter().getCount(), is(1));
303
304 ResolveInfo[] chosen = new ResolveInfo[1];
305 ChooserWrapperActivity.sOverrides.onSafelyStartCallback = targetInfo -> {
306 chosen[0] = targetInfo.getResolveInfo();
307 return true;
308 };
Makoto Onuki99302b52017-03-29 12:42:26 -0700309
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800310 // Make a stable copy of the components as the original list may be modified
311 List<ResolvedComponentInfo> stableCopy =
312 createResolvedComponentsForTestWithOtherProfile(2);
313 // Check that the "Other Profile" activity is put in the right spot
314 onView(withId(R.id.profile_button)).check(matches(
315 withText(stableCopy.get(0).getResolveInfoAt(0).activityInfo.name)));
316 onView(withText(stableCopy.get(1).getResolveInfoAt(0).activityInfo.name))
317 .perform(click());
318 waitForIdle();
319 assertThat(chosen[0], is(toChoose));
320 }
321
322 @Test
323 public void hasOtherProfileTwoOptionsAndUserSelectsOne() throws Exception {
Matt Pietal26038402019-01-08 07:29:34 -0500324 Intent sendIntent = createSendTextIntent();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800325 List<ResolvedComponentInfo> resolvedComponentInfos =
326 createResolvedComponentsForTestWithOtherProfile(3);
327 ResolveInfo toChoose = resolvedComponentInfos.get(1).getResolveInfoAt(0);
328
329 when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent(
330 Mockito.anyBoolean(),
331 Mockito.anyBoolean(),
332 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
333 when(ChooserWrapperActivity.sOverrides.resolverListController.getLastChosen())
334 .thenReturn(resolvedComponentInfos.get(0).getResolveInfoAt(0));
335
336 final ChooserWrapperActivity activity = mActivityRule
337 .launchActivity(Intent.createChooser(sendIntent, null));
338 waitForIdle();
339
340 // The other entry is filtered to the other profile slot
341 assertThat(activity.getAdapter().getCount(), is(2));
342
343 ResolveInfo[] chosen = new ResolveInfo[1];
344 ChooserWrapperActivity.sOverrides.onSafelyStartCallback = targetInfo -> {
345 chosen[0] = targetInfo.getResolveInfo();
346 return true;
347 };
348
349 // Make a stable copy of the components as the original list may be modified
350 List<ResolvedComponentInfo> stableCopy =
351 createResolvedComponentsForTestWithOtherProfile(3);
352 // Check that the "Other Profile" activity is put in the right spot
353 onView(withId(R.id.profile_button)).check(matches(
354 withText(stableCopy.get(0).getResolveInfoAt(0).activityInfo.name)));
355 onView(withText(stableCopy.get(1).getResolveInfoAt(0).activityInfo.name))
356 .perform(click());
357 waitForIdle();
358 assertThat(chosen[0], is(toChoose));
359 }
360
361 @Test
362 public void hasLastChosenActivityAndOtherProfile() throws Exception {
Matt Pietal26038402019-01-08 07:29:34 -0500363 Intent sendIntent = createSendTextIntent();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800364 List<ResolvedComponentInfo> resolvedComponentInfos =
365 createResolvedComponentsForTestWithOtherProfile(3);
366 ResolveInfo toChoose = resolvedComponentInfos.get(1).getResolveInfoAt(0);
367
368 when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent(
369 Mockito.anyBoolean(),
370 Mockito.anyBoolean(),
371 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
372
373 final ChooserWrapperActivity activity = mActivityRule
374 .launchActivity(Intent.createChooser(sendIntent, null));
375 waitForIdle();
376
377 // The other entry is filtered to the last used slot
378 assertThat(activity.getAdapter().getCount(), is(2));
379
380 ResolveInfo[] chosen = new ResolveInfo[1];
381 ChooserWrapperActivity.sOverrides.onSafelyStartCallback = targetInfo -> {
382 chosen[0] = targetInfo.getResolveInfo();
383 return true;
384 };
385
386 // Make a stable copy of the components as the original list may be modified
387 List<ResolvedComponentInfo> stableCopy =
388 createResolvedComponentsForTestWithOtherProfile(3);
389 // Check that the "Other Profile" activity is put in the right spot
390 onView(withId(R.id.profile_button)).check(matches(
391 withText(stableCopy.get(0).getResolveInfoAt(0).activityInfo.name)));
392 onView(withText(stableCopy.get(1).getResolveInfoAt(0).activityInfo.name))
393 .perform(click());
394 waitForIdle();
395 assertThat(chosen[0], is(toChoose));
396 }
397
Matt Pietal1fa7d802019-01-30 10:44:15 -0500398 @Test
399 public void copyTextToClipboard() throws Exception {
400 Intent sendIntent = createSendTextIntent();
401 List<ResolvedComponentInfo> resolvedComponentInfos =
402 createResolvedComponentsForTestWithOtherProfile(1);
403
404 when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent(
405 Mockito.anyBoolean(),
406 Mockito.anyBoolean(),
407 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
408
409 final ChooserWrapperActivity activity = mActivityRule
410 .launchActivity(Intent.createChooser(sendIntent, null));
411 waitForIdle();
412
413 onView(withId(R.id.copy_button)).perform(click());
414
415 ClipboardManager clipboard = (ClipboardManager) activity.getSystemService(
416 Context.CLIPBOARD_SERVICE);
417 ClipData clipData = clipboard.getPrimaryClip();
418 assertThat("testing intent sending", is(clipData.getItemAt(0).getText()));
419
420 ClipDescription clipDescription = clipData.getDescription();
421 assertThat("text/plain", is(clipDescription.getMimeType(0)));
422 }
423
Matt Pietal0ea391b2019-01-30 10:44:15 -0500424 @Test
425 public void oneVisibleImagePreview() throws InterruptedException {
426 Uri uri = Uri.parse("android.resource://com.android.frameworks.coretests/"
427 + com.android.frameworks.coretests.R.drawable.test320x240);
428
429 ArrayList<Uri> uris = new ArrayList<>();
430 uris.add(uri);
431
432 Intent sendIntent = createSendImageIntentWithPreview(uris);
433 sOverrides.previewThumbnail = createBitmap();
434
435 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
436
437 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
438 Mockito.anyBoolean(),
439 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
440 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
441 waitForIdle();
442 onView(withId(R.id.content_preview_image_1_large)).check(matches(isDisplayed()));
443 onView(withId(R.id.content_preview_image_2_large)).check(matches(not(isDisplayed())));
444 onView(withId(R.id.content_preview_image_2_small)).check(matches(not(isDisplayed())));
445 onView(withId(R.id.content_preview_image_3_small)).check(matches(not(isDisplayed())));
446 }
447
448 @Test
449 public void twoVisibleImagePreview() throws InterruptedException {
450 Uri uri = Uri.parse("android.resource://com.android.frameworks.coretests/"
451 + com.android.frameworks.coretests.R.drawable.test320x240);
452
453 ArrayList<Uri> uris = new ArrayList<>();
454 uris.add(uri);
455 uris.add(uri);
456
457 Intent sendIntent = createSendImageIntentWithPreview(uris);
458 sOverrides.previewThumbnail = createBitmap();
459
460 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
461
462 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
463 Mockito.anyBoolean(),
464 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
465 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
466 waitForIdle();
467 onView(withId(R.id.content_preview_image_1_large)).check(matches(isDisplayed()));
468 onView(withId(R.id.content_preview_image_2_large)).check(matches(isDisplayed()));
469 onView(withId(R.id.content_preview_image_2_small)).check(matches(not(isDisplayed())));
470 onView(withId(R.id.content_preview_image_3_small)).check(matches(not(isDisplayed())));
471 }
472
473 @Test
474 public void threeOrMoreVisibleImagePreview() throws InterruptedException {
475 Uri uri = Uri.parse("android.resource://com.android.frameworks.coretests/"
476 + com.android.frameworks.coretests.R.drawable.test320x240);
477
478 ArrayList<Uri> uris = new ArrayList<>();
479 uris.add(uri);
480 uris.add(uri);
481 uris.add(uri);
482 uris.add(uri);
483 uris.add(uri);
484
485 Intent sendIntent = createSendImageIntentWithPreview(uris);
486 sOverrides.previewThumbnail = createBitmap();
487
488 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
489
490 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
491 Mockito.anyBoolean(),
492 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
493 mActivityRule.launchActivity(Intent.createChooser(sendIntent, null));
494 waitForIdle();
495 onView(withId(R.id.content_preview_image_1_large)).check(matches(isDisplayed()));
496 onView(withId(R.id.content_preview_image_2_large)).check(matches(not(isDisplayed())));
497 onView(withId(R.id.content_preview_image_2_small)).check(matches(isDisplayed()));
498 onView(withId(R.id.content_preview_image_3_small)).check(matches(isDisplayed()));
499 }
500
Matt Pietal26038402019-01-08 07:29:34 -0500501 private Intent createSendTextIntent() {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800502 Intent sendIntent = new Intent();
503 sendIntent.setAction(Intent.ACTION_SEND);
504 sendIntent.putExtra(Intent.EXTRA_TEXT, "testing intent sending");
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800505 return sendIntent;
506 }
507
Matt Pietal26038402019-01-08 07:29:34 -0500508 private Intent createSendTextIntentWithPreview(String title, Uri imageThumbnail) {
509 Intent sendIntent = new Intent();
510 sendIntent.setAction(Intent.ACTION_SEND);
511 sendIntent.putExtra(Intent.EXTRA_TEXT, "testing intent sending");
512 sendIntent.putExtra(Intent.EXTRA_TITLE, title);
513 if (imageThumbnail != null) {
514 ClipData.Item clipItem = new ClipData.Item(imageThumbnail);
515 sendIntent.setClipData(new ClipData("Clip Label", new String[]{"image/png"}, clipItem));
516 }
517
518 return sendIntent;
519 }
520
Matt Pietal0ea391b2019-01-30 10:44:15 -0500521 private Intent createSendImageIntentWithPreview(ArrayList<Uri> uris) {
522 Intent sendIntent = new Intent();
523
524 if (uris.size() > 1) {
525 sendIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
526 sendIntent.putExtra(Intent.EXTRA_STREAM, uris);
527 } else {
528 sendIntent.setAction(Intent.ACTION_SEND);
529 sendIntent.putExtra(Intent.EXTRA_STREAM, uris.get(0));
530 }
531
532 return sendIntent;
533 }
534
Matt Pietal26038402019-01-08 07:29:34 -0500535 private Intent createViewTextIntent() {
536 Intent viewIntent = new Intent();
537 viewIntent.setAction(Intent.ACTION_VIEW);
538 viewIntent.putExtra(Intent.EXTRA_TEXT, "testing intent viewing");
539 return viewIntent;
540 }
541
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800542 private List<ResolvedComponentInfo> createResolvedComponentsForTest(int numberOfResults) {
543 List<ResolvedComponentInfo> infoList = new ArrayList<>(numberOfResults);
544 for (int i = 0; i < numberOfResults; i++) {
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -0800545 infoList.add(ResolverDataProvider.createResolvedComponentInfo(i));
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800546 }
547 return infoList;
548 }
549
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800550 private List<ResolvedComponentInfo> createResolvedComponentsForTestWithOtherProfile(
551 int numberOfResults) {
552 List<ResolvedComponentInfo> infoList = new ArrayList<>(numberOfResults);
553 for (int i = 0; i < numberOfResults; i++) {
554 if (i == 0) {
555 infoList.add(ResolverDataProvider.createResolvedComponentInfoWithOtherId(i));
556 } else {
557 infoList.add(ResolverDataProvider.createResolvedComponentInfo(i));
558 }
559 }
560 return infoList;
561 }
562
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800563 private void waitForIdle() {
564 InstrumentationRegistry.getInstrumentation().waitForIdleSync();
565 }
Matt Pietal26038402019-01-08 07:29:34 -0500566
567 private Bitmap createBitmap() {
568 int width = 200;
569 int height = 200;
570 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
571 Canvas canvas = new Canvas(bitmap);
572
573 Paint paint = new Paint();
574 paint.setColor(Color.RED);
575 paint.setStyle(Paint.Style.FILL);
576 canvas.drawPaint(paint);
577
578 paint.setColor(Color.WHITE);
579 paint.setAntiAlias(true);
580 paint.setTextSize(14.f);
581 paint.setTextAlign(Paint.Align.CENTER);
582 canvas.drawText("Hi!", (width / 2.f), (height / 2.f), paint);
583
584 return bitmap;
585 }
586}