blob: 911490f30799227251763cdc5e0cee3934e74e66 [file] [log] [blame]
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -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
Tadashi G. Takaoka7a4047f2019-02-02 09:55:59 +090019import static androidx.test.espresso.Espresso.onView;
20import static androidx.test.espresso.action.ViewActions.click;
21import static androidx.test.espresso.assertion.ViewAssertions.matches;
arangelovb4fc3972020-01-15 15:10:12 +000022import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
23import static androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed;
Tadashi G. Takaoka7a4047f2019-02-02 09:55:59 +090024import static androidx.test.espresso.matcher.ViewMatchers.isEnabled;
25import static androidx.test.espresso.matcher.ViewMatchers.withId;
26import static androidx.test.espresso.matcher.ViewMatchers.withText;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090027
arangelovb4fc3972020-01-15 15:10:12 +000028import static com.android.internal.app.MatcherUtils.first;
Mike Digmanba232682019-03-27 14:55:26 -070029import static com.android.internal.app.ResolverDataProvider.createPackageManagerMockedInfo;
arangelov7981b122020-01-16 10:58:27 +000030import static com.android.internal.app.ResolverDataProvider.createResolvedComponentInfoWithOtherId;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090031import static com.android.internal.app.ResolverWrapperActivity.sOverrides;
32
arangelovb4fc3972020-01-15 15:10:12 +000033import static org.hamcrest.CoreMatchers.allOf;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090034import static org.hamcrest.CoreMatchers.is;
35import static org.hamcrest.CoreMatchers.not;
36import static org.hamcrest.MatcherAssert.assertThat;
arangelov7981b122020-01-16 10:58:27 +000037import static org.mockito.ArgumentMatchers.eq;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090038import static org.mockito.Mockito.when;
39
40import android.content.Intent;
41import android.content.pm.ResolveInfo;
arangelovb4fc3972020-01-15 15:10:12 +000042import android.os.UserHandle;
Mike Digmanba232682019-03-27 14:55:26 -070043import android.text.TextUtils;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090044import android.view.View;
45import android.widget.RelativeLayout;
46
47import androidx.test.InstrumentationRegistry;
Zhen Zhangcb55c8a2019-10-22 14:49:09 -070048import androidx.test.espresso.Espresso;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090049import androidx.test.rule.ActivityTestRule;
50import androidx.test.runner.AndroidJUnit4;
51
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -080052import com.android.internal.R;
53import com.android.internal.app.ResolverActivity.ResolvedComponentInfo;
Mike Digmanba232682019-03-27 14:55:26 -070054import com.android.internal.app.ResolverDataProvider.PackageManagerMockedInfo;
arangelovb0802dc2019-10-18 18:03:44 +010055import com.android.internal.app.ResolverListAdapter.ActivityInfoPresentationGetter;
56import com.android.internal.app.ResolverListAdapter.ResolveInfoPresentationGetter;
Ben Lin93b86972017-10-25 11:38:40 -070057import com.android.internal.widget.ResolverDrawerLayout;
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -080058
arangelovb4fc3972020-01-15 15:10:12 +000059import org.hamcrest.BaseMatcher;
60import org.hamcrest.Description;
61import org.hamcrest.Matcher;
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -080062import org.junit.Before;
Alison Cichowlasc1a1bfe2019-11-21 15:52:27 -050063import org.junit.Ignore;
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -080064import org.junit.Rule;
65import org.junit.Test;
66import org.junit.runner.RunWith;
67import org.mockito.Mockito;
68
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -080069import java.util.ArrayList;
70import java.util.List;
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -080071
72/**
73 * Resolver activity instrumentation tests
74 */
75@RunWith(AndroidJUnit4.class)
76public class ResolverActivityTest {
77 @Rule
78 public ActivityTestRule<ResolverWrapperActivity> mActivityRule =
79 new ActivityTestRule<>(ResolverWrapperActivity.class, false,
80 false);
81
82 @Before
83 public void cleanOverrideData() {
84 sOverrides.reset();
85 }
86
87 @Test
88 public void twoOptionsAndUserSelectsOne() throws InterruptedException {
89 Intent sendIntent = createSendImageIntent();
90 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
91
92 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
93 Mockito.anyBoolean(),
94 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
95
96 final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent);
arangelovb0802dc2019-10-18 18:03:44 +010097 Espresso.registerIdlingResources(activity.getAdapter().getLabelIdlingResource());
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -080098 waitForIdle();
99
100 assertThat(activity.getAdapter().getCount(), is(2));
101
102 ResolveInfo[] chosen = new ResolveInfo[1];
103 sOverrides.onSafelyStartCallback = targetInfo -> {
104 chosen[0] = targetInfo.getResolveInfo();
105 return true;
106 };
107
108 ResolveInfo toChoose = resolvedComponentInfos.get(0).getResolveInfoAt(0);
109 onView(withText(toChoose.activityInfo.name))
110 .perform(click());
111 onView(withId(R.id.button_once))
112 .perform(click());
113 waitForIdle();
114 assertThat(chosen[0], is(toChoose));
115 }
116
Alison Cichowlasc1a1bfe2019-11-21 15:52:27 -0500117 @Ignore // Failing - b/144929805
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -0800118 @Test
Ben Lin058ab3e2018-02-23 11:28:09 -0800119 public void setMaxHeight() throws Exception {
120 Intent sendIntent = createSendImageIntent();
121 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
122
123 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
124 Mockito.anyBoolean(),
125 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
126 waitForIdle();
127
128 final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent);
arangelov2c1c37a2019-12-06 14:43:34 +0000129 final View viewPager = activity.findViewById(R.id.profile_pager);
130 final int initialResolverHeight = viewPager.getHeight();
Ben Lin058ab3e2018-02-23 11:28:09 -0800131
132 activity.runOnUiThread(() -> {
133 ResolverDrawerLayout layout = (ResolverDrawerLayout)
134 activity.findViewById(
135 R.id.contentPanel);
arangelov2c1c37a2019-12-06 14:43:34 +0000136 ((ResolverDrawerLayout.LayoutParams) viewPager.getLayoutParams()).maxHeight
Ben Lin058ab3e2018-02-23 11:28:09 -0800137 = initialResolverHeight - 1;
138 // Force a relayout
139 layout.invalidate();
140 layout.requestLayout();
141 });
142 waitForIdle();
143 assertThat("Drawer should be capped at maxHeight",
arangelov2c1c37a2019-12-06 14:43:34 +0000144 viewPager.getHeight() == (initialResolverHeight - 1));
Ben Lin058ab3e2018-02-23 11:28:09 -0800145
146 activity.runOnUiThread(() -> {
147 ResolverDrawerLayout layout = (ResolverDrawerLayout)
148 activity.findViewById(
149 R.id.contentPanel);
arangelov2c1c37a2019-12-06 14:43:34 +0000150 ((ResolverDrawerLayout.LayoutParams) viewPager.getLayoutParams()).maxHeight
Ben Lin058ab3e2018-02-23 11:28:09 -0800151 = initialResolverHeight + 1;
152 // Force a relayout
153 layout.invalidate();
154 layout.requestLayout();
155 });
156 waitForIdle();
157 assertThat("Drawer should not change height if its height is less than maxHeight",
arangelov2c1c37a2019-12-06 14:43:34 +0000158 viewPager.getHeight() == initialResolverHeight);
Ben Lin058ab3e2018-02-23 11:28:09 -0800159 }
160
Alison Cichowlasc1a1bfe2019-11-21 15:52:27 -0500161 @Ignore // Failing - b/144929805
Ben Lin058ab3e2018-02-23 11:28:09 -0800162 @Test
Ben Lin93b86972017-10-25 11:38:40 -0700163 public void setShowAtTopToTrue() throws Exception {
164 Intent sendIntent = createSendImageIntent();
165 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
166
167 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
168 Mockito.anyBoolean(),
169 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
170 waitForIdle();
171
172 final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent);
arangelov2c1c37a2019-12-06 14:43:34 +0000173 final View viewPager = activity.findViewById(R.id.profile_pager);
174 final View divider = activity.findViewById(R.id.divider);
Ben Lin93b86972017-10-25 11:38:40 -0700175 final RelativeLayout profileView =
176 (RelativeLayout) activity.findViewById(R.id.profile_button).getParent();
177 assertThat("Drawer should show at bottom by default",
arangelov2c1c37a2019-12-06 14:43:34 +0000178 profileView.getBottom() + divider.getHeight() == viewPager.getTop()
179 && profileView.getTop() > 0);
Ben Lin93b86972017-10-25 11:38:40 -0700180
181 activity.runOnUiThread(() -> {
182 ResolverDrawerLayout layout = (ResolverDrawerLayout)
183 activity.findViewById(
184 R.id.contentPanel);
185 layout.setShowAtTop(true);
186 });
187 waitForIdle();
188 assertThat("Drawer should show at top with new attribute",
arangelov2c1c37a2019-12-06 14:43:34 +0000189 profileView.getBottom() + divider.getHeight() == viewPager.getTop()
190 && profileView.getTop() == 0);
Ben Lin93b86972017-10-25 11:38:40 -0700191 }
192
193 @Test
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -0800194 public void hasLastChosenActivity() throws Exception {
195 Intent sendIntent = createSendImageIntent();
196 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800197 ResolveInfo toChoose = resolvedComponentInfos.get(0).getResolveInfoAt(0);
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -0800198
199 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
200 Mockito.anyBoolean(),
201 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
202 when(sOverrides.resolverListController.getLastChosen())
203 .thenReturn(resolvedComponentInfos.get(0).getResolveInfoAt(0));
204
205 final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent);
206 waitForIdle();
207
208 // The other entry is filtered to the last used slot
209 assertThat(activity.getAdapter().getCount(), is(1));
Sumir Katariadb688af2017-05-10 17:33:47 -0700210 assertThat(activity.getAdapter().getPlaceholderCount(), is(1));
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -0800211
212 ResolveInfo[] chosen = new ResolveInfo[1];
213 sOverrides.onSafelyStartCallback = targetInfo -> {
214 chosen[0] = targetInfo.getResolveInfo();
215 return true;
216 };
217
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800218 onView(withId(R.id.button_once)).perform(click());
219 waitForIdle();
220 assertThat(chosen[0], is(toChoose));
221 }
222
223 @Test
224 public void hasOtherProfileOneOption() throws Exception {
arangelovb4fc3972020-01-15 15:10:12 +0000225 // enable the work tab feature flag
226 ResolverActivity.ENABLE_TABBED_VIEW = true;
227
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800228 Intent sendIntent = createSendImageIntent();
229 List<ResolvedComponentInfo> resolvedComponentInfos =
230 createResolvedComponentsForTestWithOtherProfile(2);
231 ResolveInfo toChoose = resolvedComponentInfos.get(1).getResolveInfoAt(0);
232
233 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
234 Mockito.anyBoolean(),
235 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
236
237 final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent);
arangelovb0802dc2019-10-18 18:03:44 +0100238 Espresso.registerIdlingResources(activity.getAdapter().getLabelIdlingResource());
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800239 waitForIdle();
240
241 // The other entry is filtered to the last used slot
242 assertThat(activity.getAdapter().getCount(), is(1));
243
244 ResolveInfo[] chosen = new ResolveInfo[1];
245 sOverrides.onSafelyStartCallback = targetInfo -> {
246 chosen[0] = targetInfo.getResolveInfo();
247 return true;
248 };
249
250 // Make a stable copy of the components as the original list may be modified
251 List<ResolvedComponentInfo> stableCopy =
252 createResolvedComponentsForTestWithOtherProfile(2);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800253 onView(withText(stableCopy.get(1).getResolveInfoAt(0).activityInfo.name))
254 .perform(click());
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -0800255 onView(withId(R.id.button_once))
256 .perform(click());
257 waitForIdle();
258 assertThat(chosen[0], is(toChoose));
259 }
260
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800261 @Test
262 public void hasOtherProfileTwoOptionsAndUserSelectsOne() throws Exception {
arangelovb4fc3972020-01-15 15:10:12 +0000263 // enable the work tab feature flag
264 ResolverActivity.ENABLE_TABBED_VIEW = true;
265
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800266 Intent sendIntent = createSendImageIntent();
267 List<ResolvedComponentInfo> resolvedComponentInfos =
268 createResolvedComponentsForTestWithOtherProfile(3);
269 ResolveInfo toChoose = resolvedComponentInfos.get(1).getResolveInfoAt(0);
270
271 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
272 Mockito.anyBoolean(),
273 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
274
275 final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent);
arangelovb0802dc2019-10-18 18:03:44 +0100276 Espresso.registerIdlingResources(activity.getAdapter().getLabelIdlingResource());
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800277 waitForIdle();
278
279 // The other entry is filtered to the other profile slot
280 assertThat(activity.getAdapter().getCount(), is(2));
281
282 ResolveInfo[] chosen = new ResolveInfo[1];
283 sOverrides.onSafelyStartCallback = targetInfo -> {
284 chosen[0] = targetInfo.getResolveInfo();
285 return true;
286 };
287
288 // Confirm that the button bar is disabled by default
289 onView(withId(R.id.button_once)).check(matches(not(isEnabled())));
290
291 // Make a stable copy of the components as the original list may be modified
292 List<ResolvedComponentInfo> stableCopy =
293 createResolvedComponentsForTestWithOtherProfile(2);
294
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800295 onView(withText(stableCopy.get(1).getResolveInfoAt(0).activityInfo.name))
296 .perform(click());
297 onView(withId(R.id.button_once)).perform(click());
298 waitForIdle();
299 assertThat(chosen[0], is(toChoose));
300 }
301
302
303 @Test
304 public void hasLastChosenActivityAndOtherProfile() throws Exception {
arangelovb4fc3972020-01-15 15:10:12 +0000305 // enable the work tab feature flag
306 ResolverActivity.ENABLE_TABBED_VIEW = true;
307
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800308 // In this case we prefer the other profile and don't display anything about the last
309 // chosen activity.
310 Intent sendIntent = createSendImageIntent();
311 List<ResolvedComponentInfo> resolvedComponentInfos =
312 createResolvedComponentsForTestWithOtherProfile(3);
313 ResolveInfo toChoose = resolvedComponentInfos.get(1).getResolveInfoAt(0);
314
315 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
316 Mockito.anyBoolean(),
317 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
318 when(sOverrides.resolverListController.getLastChosen())
319 .thenReturn(resolvedComponentInfos.get(1).getResolveInfoAt(0));
320
321 final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent);
arangelovb0802dc2019-10-18 18:03:44 +0100322 Espresso.registerIdlingResources(activity.getAdapter().getLabelIdlingResource());
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800323 waitForIdle();
324
325 // The other entry is filtered to the other profile slot
326 assertThat(activity.getAdapter().getCount(), is(2));
327
328 ResolveInfo[] chosen = new ResolveInfo[1];
329 sOverrides.onSafelyStartCallback = targetInfo -> {
330 chosen[0] = targetInfo.getResolveInfo();
331 return true;
332 };
333
334 // Confirm that the button bar is disabled by default
335 onView(withId(R.id.button_once)).check(matches(not(isEnabled())));
336
337 // Make a stable copy of the components as the original list may be modified
338 List<ResolvedComponentInfo> stableCopy =
339 createResolvedComponentsForTestWithOtherProfile(2);
340
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800341 onView(withText(stableCopy.get(1).getResolveInfoAt(0).activityInfo.name))
342 .perform(click());
343 onView(withId(R.id.button_once)).perform(click());
344 waitForIdle();
345 assertThat(chosen[0], is(toChoose));
346 }
347
Mike Digmanba232682019-03-27 14:55:26 -0700348 @Test
349 public void getActivityLabelAndSubLabel() throws Exception {
350 ActivityInfoPresentationGetter pg;
351 PackageManagerMockedInfo info;
352
353 info = createPackageManagerMockedInfo(false);
354 pg = new ActivityInfoPresentationGetter(
355 info.ctx, 0, info.activityInfo);
356 assertThat("Label should match app label", pg.getLabel().equals(
357 info.setAppLabel));
358 assertThat("Sublabel should match activity label if set",
359 pg.getSubLabel().equals(info.setActivityLabel));
360
361 info = createPackageManagerMockedInfo(true);
362 pg = new ActivityInfoPresentationGetter(
363 info.ctx, 0, info.activityInfo);
364 assertThat("With override permission label should match activity label if set",
365 pg.getLabel().equals(info.setActivityLabel));
366 assertThat("With override permission sublabel should be empty",
367 TextUtils.isEmpty(pg.getSubLabel()));
368 }
369
370 @Test
371 public void getResolveInfoLabelAndSubLabel() throws Exception {
372 ResolveInfoPresentationGetter pg;
373 PackageManagerMockedInfo info;
374
375 info = createPackageManagerMockedInfo(false);
376 pg = new ResolveInfoPresentationGetter(
377 info.ctx, 0, info.resolveInfo);
378 assertThat("Label should match app label", pg.getLabel().equals(
379 info.setAppLabel));
380 assertThat("Sublabel should match resolve info label if set",
381 pg.getSubLabel().equals(info.setResolveInfoLabel));
382
383 info = createPackageManagerMockedInfo(true);
384 pg = new ResolveInfoPresentationGetter(
385 info.ctx, 0, info.resolveInfo);
386 assertThat("With override permission label should match resolve info label if set",
387 pg.getLabel().equals(info.setResolveInfoLabel));
388 assertThat("With override permission sublabel should be empty",
389 TextUtils.isEmpty(pg.getSubLabel()));
390 }
391
arangelovb4fc3972020-01-15 15:10:12 +0000392 @Test
393 public void testWorkTab_displayedWhenWorkProfileUserAvailable() {
394 // enable the work tab feature flag
395 ResolverActivity.ENABLE_TABBED_VIEW = true;
396 Intent sendIntent = createSendImageIntent();
397 markWorkProfileUserAvailable();
398
399 mActivityRule.launchActivity(sendIntent);
400 waitForIdle();
401
402 onView(withId(R.id.tabs)).check(matches(isDisplayed()));
403 }
404
405 @Test
406 public void testWorkTab_hiddenWhenWorkProfileUserNotAvailable() {
407 // enable the work tab feature flag
408 ResolverActivity.ENABLE_TABBED_VIEW = true;
409 Intent sendIntent = createSendImageIntent();
410
411 mActivityRule.launchActivity(sendIntent);
412 waitForIdle();
413
414 onView(withId(R.id.tabs)).check(matches(not(isDisplayed())));
415 }
416
417 @Test
418 public void testWorkTab_workTabListEmptyBeforeGoingToTab() {
419 // enable the work tab feature flag
420 ResolverActivity.ENABLE_TABBED_VIEW = true;
421 List<ResolvedComponentInfo> personalResolvedComponentInfos =
422 createResolvedComponentsForTest(3);
423 List<ResolvedComponentInfo> workResolvedComponentInfos = createResolvedComponentsForTest(4);
424 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
425 Mockito.anyBoolean(),
426 Mockito.isA(List.class))).thenReturn(personalResolvedComponentInfos);
427 when(sOverrides.workResolverListController.getResolversForIntent(Mockito.anyBoolean(),
428 Mockito.anyBoolean(),
429 Mockito.isA(List.class))).thenReturn(workResolvedComponentInfos);
430 Intent sendIntent = createSendImageIntent();
431 markWorkProfileUserAvailable();
432
433 final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent);
434 waitForIdle();
435
436 assertThat(activity.getCurrentUserHandle().getIdentifier(), is(0));
437 // The work list adapter must only be filled when we open the work tab
438 assertThat(activity.getWorkListAdapter().getCount(), is(0));
439 }
440
441 @Test
442 public void testWorkTab_workTabUsesExpectedAdapter() {
443 // enable the work tab feature flag
444 ResolverActivity.ENABLE_TABBED_VIEW = true;
445 List<ResolvedComponentInfo> personalResolvedComponentInfos =
446 createResolvedComponentsForTest(3);
447 List<ResolvedComponentInfo> workResolvedComponentInfos = createResolvedComponentsForTest(4);
448 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
449 Mockito.anyBoolean(),
450 Mockito.isA(List.class))).thenReturn(personalResolvedComponentInfos);
451 when(sOverrides.workResolverListController.getResolversForIntent(Mockito.anyBoolean(),
452 Mockito.anyBoolean(),
453 Mockito.isA(List.class))).thenReturn(workResolvedComponentInfos);
454 Intent sendIntent = createSendImageIntent();
455 markWorkProfileUserAvailable();
456
457 final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent);
458 waitForIdle();
459 onView(withText(R.string.resolver_work_tab)).perform(click());
460
461 assertThat(activity.getCurrentUserHandle().getIdentifier(), is(10));
462 assertThat(activity.getWorkListAdapter().getCount(), is(4));
463 }
464
465 @Test
466 public void testWorkTab_personalTabUsesExpectedAdapter() {
467 // enable the work tab feature flag
468 ResolverActivity.ENABLE_TABBED_VIEW = true;
469 List<ResolvedComponentInfo> personalResolvedComponentInfos =
arangelov7981b122020-01-16 10:58:27 +0000470 createResolvedComponentsForTestWithOtherProfile(3);
arangelovb4fc3972020-01-15 15:10:12 +0000471 List<ResolvedComponentInfo> workResolvedComponentInfos = createResolvedComponentsForTest(4);
arangelov7981b122020-01-16 10:58:27 +0000472 when(sOverrides.resolverListController.getResolversForIntentAsUser(
arangelovb4fc3972020-01-15 15:10:12 +0000473 Mockito.anyBoolean(),
arangelov7981b122020-01-16 10:58:27 +0000474 Mockito.anyBoolean(),
475 Mockito.isA(List.class),
476 eq(UserHandle.SYSTEM))).thenReturn(new ArrayList<>(personalResolvedComponentInfos));
477 when(sOverrides.resolverListController.getResolversForIntentAsUser(
478 Mockito.anyBoolean(),
479 Mockito.anyBoolean(),
480 Mockito.isA(List.class),
481 eq(sOverrides.workProfileUserHandle))).thenReturn(new ArrayList<>(workResolvedComponentInfos));
482 when(sOverrides.workResolverListController.getResolversForIntentAsUser(Mockito.anyBoolean(),
483 Mockito.anyBoolean(),
484 Mockito.isA(List.class),
485 eq(sOverrides.workProfileUserHandle))).thenReturn(new ArrayList<>(workResolvedComponentInfos));
arangelovb4fc3972020-01-15 15:10:12 +0000486 when(sOverrides.workResolverListController.getResolversForIntent(Mockito.anyBoolean(),
487 Mockito.anyBoolean(),
arangelov7981b122020-01-16 10:58:27 +0000488 Mockito.isA(List.class))).thenReturn(new ArrayList<>(workResolvedComponentInfos));
489 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
490 Mockito.anyBoolean(),
491 Mockito.isA(List.class))).thenReturn(new ArrayList<>(personalResolvedComponentInfos));
492 when(sOverrides.workResolverListController.getResolversForIntentAsUser(Mockito.anyBoolean(),
493 Mockito.anyBoolean(),
494 Mockito.isA(List.class),
495 eq(UserHandle.SYSTEM))).thenReturn(new ArrayList<>(personalResolvedComponentInfos));
496
arangelovb4fc3972020-01-15 15:10:12 +0000497 Intent sendIntent = createSendImageIntent();
498 markWorkProfileUserAvailable();
499
500 final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent);
501 waitForIdle();
502 onView(withText(R.string.resolver_work_tab)).perform(click());
503
504 assertThat(activity.getCurrentUserHandle().getIdentifier(), is(10));
505 assertThat(activity.getPersonalListAdapter().getCount(), is(3));
506 }
507
508 @Test
509 public void testWorkTab_workProfileHasExpectedNumberOfTargets() throws InterruptedException {
510 // enable the work tab feature flag
511 ResolverActivity.ENABLE_TABBED_VIEW = true;
512 markWorkProfileUserAvailable();
513 List<ResolvedComponentInfo> personalResolvedComponentInfos =
514 createResolvedComponentsForTest(3);
515 List<ResolvedComponentInfo> workResolvedComponentInfos = createResolvedComponentsForTest(4);
516 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
517 Mockito.anyBoolean(),
518 Mockito.isA(List.class))).thenReturn(personalResolvedComponentInfos);
519 when(sOverrides.workResolverListController.getResolversForIntent(Mockito.anyBoolean(),
520 Mockito.anyBoolean(),
521 Mockito.isA(List.class))).thenReturn(workResolvedComponentInfos);
522 Intent sendIntent = createSendImageIntent();
523
524 final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent);
525 waitForIdle();
arangelov7981b122020-01-16 10:58:27 +0000526
arangelovb4fc3972020-01-15 15:10:12 +0000527 onView(withText(R.string.resolver_work_tab))
528 .perform(click());
arangelovb4fc3972020-01-15 15:10:12 +0000529 waitForIdle();
530 assertThat(activity.getWorkListAdapter().getCount(), is(4));
531 }
532
533 @Test
534 public void testWorkTab_selectingWorkTabAppOpensAppInWorkProfile() throws InterruptedException {
535 // enable the work tab feature flag
536 ResolverActivity.ENABLE_TABBED_VIEW = true;
537 markWorkProfileUserAvailable();
538 List<ResolvedComponentInfo> personalResolvedComponentInfos =
539 createResolvedComponentsForTest(3);
540 List<ResolvedComponentInfo> workResolvedComponentInfos = createResolvedComponentsForTest(4);
541 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
542 Mockito.anyBoolean(),
543 Mockito.isA(List.class))).thenReturn(personalResolvedComponentInfos);
544 when(sOverrides.workResolverListController.getResolversForIntent(Mockito.anyBoolean(),
545 Mockito.anyBoolean(),
546 Mockito.isA(List.class))).thenReturn(workResolvedComponentInfos);
547 Intent sendIntent = createSendImageIntent();
548 ResolveInfo[] chosen = new ResolveInfo[1];
549 sOverrides.onSafelyStartCallback = targetInfo -> {
550 chosen[0] = targetInfo.getResolveInfo();
551 return true;
552 };
553
554 mActivityRule.launchActivity(sendIntent);
555 waitForIdle();
556 onView(withText(R.string.resolver_work_tab))
557 .perform(click());
558 waitForIdle();
559 // wait for the share sheet to expand
560 Thread.sleep(ChooserActivity.LIST_VIEW_UPDATE_INTERVAL_IN_MILLIS);
561 onView(first(allOf(withText(workResolvedComponentInfos.get(0)
562 .getResolveInfoAt(0).activityInfo.applicationInfo.name), isCompletelyDisplayed())))
563 .perform(click());
564 onView(withId(R.id.button_once))
565 .perform(click());
566
567 waitForIdle();
568 assertThat(chosen[0], is(workResolvedComponentInfos.get(0).getResolveInfoAt(0)));
569 }
570
571 @Test
572 public void testWorkTab_noPersonalApps_workTabHasExpectedNumberOfTargets()
573 throws InterruptedException {
574 // enable the work tab feature flag
575 ResolverActivity.ENABLE_TABBED_VIEW = true;
576 markWorkProfileUserAvailable();
arangelov7981b122020-01-16 10:58:27 +0000577 List<ResolvedComponentInfo> personalResolvedComponentInfos =
578 createResolvedComponentsForTestWithOtherProfile(1);
arangelovb4fc3972020-01-15 15:10:12 +0000579 List<ResolvedComponentInfo> workResolvedComponentInfos = createResolvedComponentsForTest(4);
arangelov7981b122020-01-16 10:58:27 +0000580
581 when(sOverrides.resolverListController.getResolversForIntentAsUser(
582 Mockito.anyBoolean(),
583 Mockito.anyBoolean(),
584 Mockito.isA(List.class),
585 eq(UserHandle.SYSTEM))).thenReturn(new ArrayList<>(personalResolvedComponentInfos));
586 when(sOverrides.resolverListController.getResolversForIntentAsUser(
587 Mockito.anyBoolean(),
588 Mockito.anyBoolean(),
589 Mockito.isA(List.class),
590 eq(sOverrides.workProfileUserHandle))).thenReturn(new ArrayList<>(workResolvedComponentInfos));
591 when(sOverrides.workResolverListController.getResolversForIntentAsUser(Mockito.anyBoolean(),
592 Mockito.anyBoolean(),
593 Mockito.isA(List.class),
594 eq(sOverrides.workProfileUserHandle))).thenReturn(new ArrayList<>(workResolvedComponentInfos));
arangelovb4fc3972020-01-15 15:10:12 +0000595 when(sOverrides.workResolverListController.getResolversForIntent(Mockito.anyBoolean(),
596 Mockito.anyBoolean(),
arangelov7981b122020-01-16 10:58:27 +0000597 Mockito.isA(List.class))).thenReturn(new ArrayList<>(workResolvedComponentInfos));
598 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
599 Mockito.anyBoolean(),
600 Mockito.isA(List.class))).thenReturn(new ArrayList<>(personalResolvedComponentInfos));
601 when(sOverrides.workResolverListController.getResolversForIntentAsUser(Mockito.anyBoolean(),
602 Mockito.anyBoolean(),
603 Mockito.isA(List.class),
604 eq(UserHandle.SYSTEM))).thenReturn(new ArrayList<>(personalResolvedComponentInfos));
605
arangelovb4fc3972020-01-15 15:10:12 +0000606 Intent sendIntent = createSendImageIntent();
607
608 final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent);
609 waitForIdle();
610 onView(withText(R.string.resolver_work_tab))
611 .perform(click());
612
613 waitForIdle();
614 assertThat(activity.getWorkListAdapter().getCount(), is(4));
615 }
616
617 @Ignore // b/148156663
618 @Test
619 public void testWorkTab_noPersonalApps_canStartWorkApps()
620 throws InterruptedException {
621 // enable the work tab feature flag
622 ResolverActivity.ENABLE_TABBED_VIEW = true;
623 markWorkProfileUserAvailable();
624 List<ResolvedComponentInfo> workResolvedComponentInfos = createResolvedComponentsForTest(4);
625 when(sOverrides.workResolverListController.getResolversForIntent(Mockito.anyBoolean(),
626 Mockito.anyBoolean(),
627 Mockito.isA(List.class))).thenReturn(workResolvedComponentInfos);
628 Intent sendIntent = createSendImageIntent();
629 ResolveInfo[] chosen = new ResolveInfo[1];
630 sOverrides.onSafelyStartCallback = targetInfo -> {
631 chosen[0] = targetInfo.getResolveInfo();
632 return true;
633 };
634
635 mActivityRule.launchActivity(sendIntent);
636 waitForIdle();
637 onView(withText(R.string.resolver_work_tab))
638 .perform(click());
639 waitForIdle();
640 // wait for the share sheet to expand
641 Thread.sleep(ChooserActivity.LIST_VIEW_UPDATE_INTERVAL_IN_MILLIS);
642 onView(first(allOf(withText(workResolvedComponentInfos.get(0)
643 .getResolveInfoAt(0).activityInfo.applicationInfo.name), isCompletelyDisplayed())))
644 .perform(click());
645 onView(withId(R.id.button_once))
646 .perform(click());
647 waitForIdle();
648
649 assertThat(chosen[0], is(workResolvedComponentInfos.get(0).getResolveInfoAt(0)));
650 }
651
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -0800652 private Intent createSendImageIntent() {
653 Intent sendIntent = new Intent();
654 sendIntent.setAction(Intent.ACTION_SEND);
655 sendIntent.putExtra(Intent.EXTRA_TEXT, "testing intent sending");
656 sendIntent.setType("image/jpeg");
657 return sendIntent;
658 }
659
660 private List<ResolvedComponentInfo> createResolvedComponentsForTest(int numberOfResults) {
661 List<ResolvedComponentInfo> infoList = new ArrayList<>(numberOfResults);
662 for (int i = 0; i < numberOfResults; i++) {
663 infoList.add(ResolverDataProvider.createResolvedComponentInfo(i));
664 }
665 return infoList;
666 }
667
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800668 private List<ResolvedComponentInfo> createResolvedComponentsForTestWithOtherProfile(
669 int numberOfResults) {
670 List<ResolvedComponentInfo> infoList = new ArrayList<>(numberOfResults);
671 for (int i = 0; i < numberOfResults; i++) {
672 if (i == 0) {
673 infoList.add(ResolverDataProvider.createResolvedComponentInfoWithOtherId(i));
674 } else {
675 infoList.add(ResolverDataProvider.createResolvedComponentInfo(i));
676 }
677 }
678 return infoList;
679 }
680
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -0800681 private void waitForIdle() {
682 InstrumentationRegistry.getInstrumentation().waitForIdleSync();
683 }
arangelovb4fc3972020-01-15 15:10:12 +0000684
685 private void markWorkProfileUserAvailable() {
686 ResolverWrapperActivity.sOverrides.workProfileUserHandle = UserHandle.of(10);
687 }
Ben Lin93b86972017-10-25 11:38:40 -0700688}