blob: a2a40e651f498d55565fc370ec8daa8229d558c5 [file] [log] [blame]
Tony Mak96d78f52017-05-03 18:53:21 +01001/*
2 * Copyright (C) 2017 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
Tony Mak96d78f52017-05-03 18:53:21 +010019import static junit.framework.Assert.assertEquals;
20import static junit.framework.Assert.assertNotNull;
21import static junit.framework.Assert.assertNull;
22import static org.mockito.ArgumentMatchers.any;
23import static org.mockito.ArgumentMatchers.anyInt;
24import static org.mockito.ArgumentMatchers.anyString;
25import static org.mockito.ArgumentMatchers.eq;
26import static org.mockito.ArgumentMatchers.nullable;
arangelov64439c1e2018-07-31 14:18:47 +010027import static org.mockito.Mockito.never;
28import static org.mockito.Mockito.spy;
Tony Mak96d78f52017-05-03 18:53:21 +010029import static org.mockito.Mockito.verify;
30import static org.mockito.Mockito.when;
31
arangelov64439c1e2018-07-31 14:18:47 +010032import android.annotation.Nullable;
33import android.content.ComponentName;
34import android.content.Context;
35import android.content.Intent;
36import android.content.pm.ActivityInfo;
37import android.content.pm.ApplicationInfo;
38import android.content.pm.IPackageManager;
39import android.content.pm.PackageManager;
40import android.content.pm.ResolveInfo;
41import android.content.pm.UserInfo;
42import android.net.Uri;
43import android.os.Bundle;
44import android.os.RemoteException;
45import android.os.UserHandle;
46import android.os.UserManager;
47import android.support.test.InstrumentationRegistry;
48import android.support.test.rule.ActivityTestRule;
49import android.support.test.runner.AndroidJUnit4;
50import java.util.ArrayList;
51import java.util.List;
52import org.junit.Before;
53import org.junit.Rule;
54import org.junit.Test;
55import org.junit.runner.RunWith;
56import org.mockito.ArgumentCaptor;
57import org.mockito.Mock;
58import org.mockito.MockitoAnnotations;
59
Tony Mak96d78f52017-05-03 18:53:21 +010060@RunWith(AndroidJUnit4.class)
61public class IntentForwarderActivityTest {
arangelov64439c1e2018-07-31 14:18:47 +010062
Tony Mak96d78f52017-05-03 18:53:21 +010063 private static final ComponentName FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME =
64 new ComponentName(
65 "android",
66 IntentForwarderActivity.FORWARD_INTENT_TO_MANAGED_PROFILE
67 );
68 private static final String TYPE_PLAIN_TEXT = "text/plain";
69
70 private static UserInfo MANAGED_PROFILE_INFO = new UserInfo();
arangelov38303742018-08-14 20:14:12 +010071
Tony Mak96d78f52017-05-03 18:53:21 +010072 static {
73 MANAGED_PROFILE_INFO.id = 10;
74 MANAGED_PROFILE_INFO.flags = UserInfo.FLAG_MANAGED_PROFILE;
75 }
76
77 private static UserInfo CURRENT_USER_INFO = new UserInfo();
arangelov38303742018-08-14 20:14:12 +010078
Tony Mak96d78f52017-05-03 18:53:21 +010079 static {
80 CURRENT_USER_INFO.id = UserHandle.myUserId();
81 CURRENT_USER_INFO.flags = 0;
82 }
83
84 private static IntentForwarderActivity.Injector sInjector;
85 private static ComponentName sComponentName;
arangelov64439c1e2018-07-31 14:18:47 +010086 private static String sActivityName;
87 private static String sPackageName;
Tony Mak96d78f52017-05-03 18:53:21 +010088
arangelov38303742018-08-14 20:14:12 +010089 @Mock
90 private IPackageManager mIPm;
91 @Mock
92 private PackageManager mPm;
93 @Mock
94 private UserManager mUserManager;
95 @Mock
96 private ApplicationInfo mApplicationInfo;
Tony Mak96d78f52017-05-03 18:53:21 +010097
98 @Rule
99 public ActivityTestRule<IntentForwarderWrapperActivity> mActivityRule =
100 new ActivityTestRule<>(IntentForwarderWrapperActivity.class, true, false);
101
102 private Context mContext;
arangelov64439c1e2018-07-31 14:18:47 +0100103 public static final String PHONE_NUMBER = "123-456-789";
Tony Mak96d78f52017-05-03 18:53:21 +0100104
105 @Before
106 public void setup() {
107 MockitoAnnotations.initMocks(this);
108 mContext = InstrumentationRegistry.getTargetContext();
arangelov64439c1e2018-07-31 14:18:47 +0100109 sInjector = spy(new TestInjector());
Tony Mak96d78f52017-05-03 18:53:21 +0100110 }
111
112 @Test
113 public void forwardToManagedProfile_canForward_sendIntent() throws Exception {
114 sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
115
116 // Intent can be forwarded.
117 when(mIPm.canForwardTo(
118 any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true);
119
120 // Managed profile exists.
121 List<UserInfo> profiles = new ArrayList<>();
122 profiles.add(CURRENT_USER_INFO);
123 profiles.add(MANAGED_PROFILE_INFO);
124 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
125
126 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class);
127 intent.setAction(Intent.ACTION_SEND);
128 intent.setType(TYPE_PLAIN_TEXT);
129 IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent);
130
131 ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
132 verify(mIPm).canForwardTo(intentCaptor.capture(), eq(TYPE_PLAIN_TEXT), anyInt(), anyInt());
133 assertEquals(Intent.ACTION_SEND, intentCaptor.getValue().getAction());
134
135 assertEquals(Intent.ACTION_SEND, intentCaptor.getValue().getAction());
136 assertNotNull(activity.mStartActivityIntent);
137 assertEquals(Intent.ACTION_SEND, activity.mStartActivityIntent.getAction());
138 assertNull(activity.mStartActivityIntent.getPackage());
139 assertNull(activity.mStartActivityIntent.getComponent());
140 assertEquals(CURRENT_USER_INFO.id, activity.mStartActivityIntent.getContentUserHint());
141
142 assertEquals(MANAGED_PROFILE_INFO.id, activity.mUserIdActivityLaunchedIn);
143 }
144
145 @Test
146 public void forwardToManagedProfile_cannotForward_sendIntent() throws Exception {
147 sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
148
149 // Intent cannot be forwarded.
150 when(mIPm.canForwardTo(
151 any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(false);
152
153 // Managed profile exists.
154 List<UserInfo> profiles = new ArrayList<>();
155 profiles.add(CURRENT_USER_INFO);
156 profiles.add(MANAGED_PROFILE_INFO);
157 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
158
159 // Create ACTION_SEND intent.
160 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class);
161 intent.setAction(Intent.ACTION_SEND);
162 IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent);
163
164 assertNull(activity.mStartActivityIntent);
165 }
166
167 @Test
168 public void forwardToManagedProfile_noManagedProfile_sendIntent() throws Exception {
169 sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
170
171 // Intent can be forwarded.
172 when(mIPm.canForwardTo(
173 any(Intent.class), anyString(), anyInt(), anyInt())).thenReturn(true);
174
175 // Managed profile does not exist.
176 List<UserInfo> profiles = new ArrayList<>();
177 profiles.add(CURRENT_USER_INFO);
178 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
179
180 // Create ACTION_SEND intent.
181 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class);
182 intent.setAction(Intent.ACTION_SEND);
183 IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent);
184
185 assertNull(activity.mStartActivityIntent);
186 }
187
188 @Test
189 public void forwardToManagedProfile_canForward_chooserIntent() throws Exception {
190 sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
191
192 // Intent can be forwarded.
193 when(mIPm.canForwardTo(
194 any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true);
195
196 // Manage profile exists.
197 List<UserInfo> profiles = new ArrayList<>();
198 profiles.add(CURRENT_USER_INFO);
199 profiles.add(MANAGED_PROFILE_INFO);
200 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
201
202 // Create chooser Intent
203 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class);
204 intent.setAction(Intent.ACTION_CHOOSER);
205 Intent sendIntent = new Intent(Intent.ACTION_SEND);
206 sendIntent.setComponent(new ComponentName("xx", "yyy"));
207 sendIntent.setType(TYPE_PLAIN_TEXT);
208 intent.putExtra(Intent.EXTRA_INTENT, sendIntent);
209 IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent);
210
211 ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
212 verify(mIPm).canForwardTo(intentCaptor.capture(), eq(TYPE_PLAIN_TEXT), anyInt(), anyInt());
213 assertEquals(Intent.ACTION_SEND, intentCaptor.getValue().getAction());
214
215 assertNotNull(activity.mStartActivityIntent);
216 assertEquals(Intent.ACTION_CHOOSER, activity.mStartActivityIntent.getAction());
217 assertNull(activity.mStartActivityIntent.getPackage());
218 assertNull(activity.mStartActivityIntent.getComponent());
219
220 Intent innerIntent = activity.mStartActivityIntent.getParcelableExtra(Intent.EXTRA_INTENT);
221 assertNotNull(innerIntent);
222 assertEquals(Intent.ACTION_SEND, innerIntent.getAction());
223 assertNull(innerIntent.getComponent());
224 assertNull(innerIntent.getPackage());
225 assertEquals(CURRENT_USER_INFO.id, innerIntent.getContentUserHint());
226
227 assertEquals(MANAGED_PROFILE_INFO.id, activity.mUserIdActivityLaunchedIn);
228 }
229
230 @Test
231 public void forwardToManagedProfile_canForward_selectorIntent() throws Exception {
232 sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
233
234 // Intent can be forwarded.
235 when(mIPm.canForwardTo(
236 any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true);
237
238 // Manage profile exists.
239 List<UserInfo> profiles = new ArrayList<>();
240 profiles.add(CURRENT_USER_INFO);
241 profiles.add(MANAGED_PROFILE_INFO);
242 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
243
244 // Create selector intent.
245 Intent intent = Intent.makeMainSelectorActivity(
246 Intent.ACTION_VIEW, Intent.CATEGORY_BROWSABLE);
247 IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent);
248
249 ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
250 verify(mIPm).canForwardTo(
251 intentCaptor.capture(), nullable(String.class), anyInt(), anyInt());
252 assertEquals(Intent.ACTION_VIEW, intentCaptor.getValue().getAction());
253
254 assertNotNull(activity.mStartActivityIntent);
255 assertEquals(Intent.ACTION_MAIN, activity.mStartActivityIntent.getAction());
256 assertNull(activity.mStartActivityIntent.getPackage());
257 assertNull(activity.mStartActivityIntent.getComponent());
258 assertEquals(CURRENT_USER_INFO.id, activity.mStartActivityIntent.getContentUserHint());
259
260 Intent innerIntent = activity.mStartActivityIntent.getSelector();
261 assertNotNull(innerIntent);
262 assertEquals(Intent.ACTION_VIEW, innerIntent.getAction());
263 assertNull(innerIntent.getComponent());
264 assertNull(innerIntent.getPackage());
265
266 assertEquals(MANAGED_PROFILE_INFO.id, activity.mUserIdActivityLaunchedIn);
267 }
268
arangelov64439c1e2018-07-31 14:18:47 +0100269 @Test
270 public void shouldSkipDisclosure_notWhitelisted() throws RemoteException {
271 setupShouldSkipDisclosureTest();
272 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100273 .setAction(Intent.ACTION_SEND)
274 .setType(TYPE_PLAIN_TEXT);
arangelov64439c1e2018-07-31 14:18:47 +0100275
276 mActivityRule.launchActivity(intent);
277
278 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
279 verify(sInjector).showToast(anyInt(), anyInt());
280 }
281
282 @Test
283 public void shouldSkipDisclosure_withResolverActivity() throws RemoteException {
284 setupShouldSkipDisclosureTest();
285 sActivityName = ResolverActivity.class.getName();
286 sPackageName = "android";
287 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100288 .setAction(Intent.ACTION_SEND)
289 .setType(TYPE_PLAIN_TEXT);
arangelov64439c1e2018-07-31 14:18:47 +0100290
291 mActivityRule.launchActivity(intent);
292
293 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
294 verify(sInjector, never()).showToast(anyInt(), anyInt());
295 }
296
297 @Test
298 public void shouldSkipDisclosure_callIntent_call() throws RemoteException {
299 setupShouldSkipDisclosureTest();
300 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100301 .setAction(Intent.ACTION_CALL);
302
303 mActivityRule.launchActivity(intent);
304
305 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
306 verify(sInjector, never()).showToast(anyInt(), anyInt());
307 }
308
309 @Test
310 public void shouldSkipDisclosure_callIntent_callPrivileged() throws RemoteException {
311 setupShouldSkipDisclosureTest();
312 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
313 .setAction(Intent.ACTION_CALL_PRIVILEGED);
314
315 mActivityRule.launchActivity(intent);
316
317 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
318 verify(sInjector, never()).showToast(anyInt(), anyInt());
319 }
320
321 @Test
322 public void shouldSkipDisclosure_callIntent_callEmergency() throws RemoteException {
323 setupShouldSkipDisclosureTest();
324 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
325 .setAction(Intent.ACTION_CALL_EMERGENCY);
arangelov64439c1e2018-07-31 14:18:47 +0100326
327 mActivityRule.launchActivity(intent);
328
329 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
330 verify(sInjector, never()).showToast(anyInt(), anyInt());
331 }
332
333 @Test
334 public void shouldSkipDisclosure_callIntent_dial() throws RemoteException {
335 setupShouldSkipDisclosureTest();
336 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100337 .setAction(Intent.ACTION_DIAL);
arangelov64439c1e2018-07-31 14:18:47 +0100338
339 mActivityRule.launchActivity(intent);
340
341 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
342 verify(sInjector, never()).showToast(anyInt(), anyInt());
343 }
344
345 @Test
346 public void shouldSkipDisclosure_callIntent_notCallOrDial() throws RemoteException {
347 setupShouldSkipDisclosureTest();
348 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100349 .setAction(Intent.ACTION_ALARM_CHANGED);
arangelov64439c1e2018-07-31 14:18:47 +0100350
351 mActivityRule.launchActivity(intent);
352
353 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
354 verify(sInjector).showToast(anyInt(), anyInt());
355 }
356
357 @Test
arangelov38303742018-08-14 20:14:12 +0100358 public void shouldSkipDisclosure_callIntent_actionViewTel() throws RemoteException {
359 setupShouldSkipDisclosureTest();
360 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
361 .setAction(Intent.ACTION_VIEW)
362 .addCategory(Intent.CATEGORY_BROWSABLE)
363 .setData(Uri.fromParts("tel", PHONE_NUMBER, null));
364
365 mActivityRule.launchActivity(intent);
366
367 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
368 verify(sInjector, never()).showToast(anyInt(), anyInt());
369 }
370
371 @Test
arangelov64439c1e2018-07-31 14:18:47 +0100372 public void shouldSkipDisclosure_textMessageIntent_sms() throws RemoteException {
373 setupShouldSkipDisclosureTest();
374 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100375 .setAction(Intent.ACTION_SENDTO)
376 .setData(Uri.fromParts("sms", PHONE_NUMBER, null));
arangelov64439c1e2018-07-31 14:18:47 +0100377
378 mActivityRule.launchActivity(intent);
379
380 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
381 verify(sInjector, never()).showToast(anyInt(), anyInt());
382 }
383
384 @Test
385 public void shouldSkipDisclosure_textMessageIntent_smsto() throws RemoteException {
386 setupShouldSkipDisclosureTest();
387 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100388 .setAction(Intent.ACTION_SENDTO)
389 .setData(Uri.fromParts("smsto", PHONE_NUMBER, null));
arangelov64439c1e2018-07-31 14:18:47 +0100390
391 mActivityRule.launchActivity(intent);
392
393 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
394 verify(sInjector, never()).showToast(anyInt(), anyInt());
395 }
396
397 @Test
398 public void shouldSkipDisclosure_textMessageIntent_mms() throws RemoteException {
399 setupShouldSkipDisclosureTest();
400 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100401 .setAction(Intent.ACTION_SENDTO)
402 .setData(Uri.fromParts("mms", PHONE_NUMBER, null));
arangelov64439c1e2018-07-31 14:18:47 +0100403
404 mActivityRule.launchActivity(intent);
405
406 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
407 verify(sInjector, never()).showToast(anyInt(), anyInt());
408 }
409
410 @Test
411 public void shouldSkipDisclosure_textMessageIntent_mmsto() throws RemoteException {
412 setupShouldSkipDisclosureTest();
413 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100414 .setAction(Intent.ACTION_SENDTO)
415 .setData(Uri.fromParts("mmsto", PHONE_NUMBER, null));
416
417 mActivityRule.launchActivity(intent);
418
419 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
420 verify(sInjector, never()).showToast(anyInt(), anyInt());
421 }
422
423 @Test
424 public void shouldSkipDisclosure_textMessageIntent_actionViewSms() throws RemoteException {
425 setupShouldSkipDisclosureTest();
426 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
427 .setAction(Intent.ACTION_VIEW)
428 .addCategory(Intent.CATEGORY_BROWSABLE)
429 .setData(Uri.fromParts("sms", PHONE_NUMBER, null));
430
431 mActivityRule.launchActivity(intent);
432
433 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
434 verify(sInjector, never()).showToast(anyInt(), anyInt());
435 }
436
437 @Test
438 public void shouldSkipDisclosure_textMessageIntent_actionViewSmsto() throws RemoteException {
439 setupShouldSkipDisclosureTest();
440 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
441 .setAction(Intent.ACTION_VIEW)
442 .addCategory(Intent.CATEGORY_BROWSABLE)
443 .setData(Uri.fromParts("smsto", PHONE_NUMBER, null));
444
445 mActivityRule.launchActivity(intent);
446
447 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
448 verify(sInjector, never()).showToast(anyInt(), anyInt());
449 }
450
451 @Test
452 public void shouldSkipDisclosure_textMessageIntent_actionViewMms() throws RemoteException {
453 setupShouldSkipDisclosureTest();
454 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
455 .setAction(Intent.ACTION_VIEW)
456 .addCategory(Intent.CATEGORY_BROWSABLE)
457 .setData(Uri.fromParts("mms", PHONE_NUMBER, null));
458
459 mActivityRule.launchActivity(intent);
460
461 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
462 verify(sInjector, never()).showToast(anyInt(), anyInt());
463 }
464
465 @Test
466 public void shouldSkipDisclosure_textMessageIntent_actionViewMmsto() throws RemoteException {
467 setupShouldSkipDisclosureTest();
468 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
469 .setAction(Intent.ACTION_VIEW)
470 .addCategory(Intent.CATEGORY_BROWSABLE)
471 .setData(Uri.fromParts("mmsto", PHONE_NUMBER, null));
arangelov64439c1e2018-07-31 14:18:47 +0100472
473 mActivityRule.launchActivity(intent);
474
475 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
476 verify(sInjector, never()).showToast(anyInt(), anyInt());
477 }
478
479 @Test
480 public void shouldSkipDisclosure_textMessageIntent_invalidUri() throws RemoteException {
481 setupShouldSkipDisclosureTest();
482 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100483 .setAction(Intent.ACTION_SENDTO)
484 .setData(Uri.fromParts("invalid", PHONE_NUMBER, null));
485
486 mActivityRule.launchActivity(intent);
487
488 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
489 verify(sInjector).showToast(anyInt(), anyInt());
490 }
491
492 @Test
493 public void shouldSkipDisclosure_viewBrowsableIntent_invalidUri() throws RemoteException {
494 setupShouldSkipDisclosureTest();
495 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
496 .setAction(Intent.ACTION_VIEW)
497 .addCategory(Intent.CATEGORY_BROWSABLE)
498 .setData(Uri.fromParts("invalid", PHONE_NUMBER, null));
499
500 mActivityRule.launchActivity(intent);
501
502 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
503 verify(sInjector).showToast(anyInt(), anyInt());
504 }
505
506 @Test
507 public void shouldSkipDisclosure_viewBrowsableIntent_normalUrl() throws RemoteException {
508 setupShouldSkipDisclosureTest();
509 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
510 .setAction(Intent.ACTION_VIEW)
511 .addCategory(Intent.CATEGORY_BROWSABLE)
512 .setData(Uri.fromParts("http", "apache.org", null));
arangelov64439c1e2018-07-31 14:18:47 +0100513
514 mActivityRule.launchActivity(intent);
515
516 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
517 verify(sInjector).showToast(anyInt(), anyInt());
518 }
519
520 private void setupShouldSkipDisclosureTest() throws RemoteException {
521 sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
522 sActivityName = "MyTestActivity";
523 sPackageName = "test.package.name";
524 when(mApplicationInfo.isSystemApp()).thenReturn(true);
525 // Managed profile exists.
526 List<UserInfo> profiles = new ArrayList<>();
527 profiles.add(CURRENT_USER_INFO);
528 profiles.add(MANAGED_PROFILE_INFO);
529 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
530 // Intent can be forwarded.
531 when(mIPm.canForwardTo(
arangelov38303742018-08-14 20:14:12 +0100532 any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true);
arangelov64439c1e2018-07-31 14:18:47 +0100533 }
Tony Mak96d78f52017-05-03 18:53:21 +0100534
535 public static class IntentForwarderWrapperActivity extends IntentForwarderActivity {
arangelov38303742018-08-14 20:14:12 +0100536
Tony Mak96d78f52017-05-03 18:53:21 +0100537 private Intent mStartActivityIntent;
538 private int mUserIdActivityLaunchedIn;
539
540 @Override
541 public void onCreate(@Nullable Bundle savedInstanceState) {
542 getIntent().setComponent(sComponentName);
543 super.onCreate(savedInstanceState);
544 }
545
546 @Override
547 protected Injector createInjector() {
548 return sInjector;
549 }
550
551 @Override
Alison Cichowlas76f0ccb2018-01-29 16:34:33 -0500552 public void startActivityAsCaller(Intent intent, @Nullable Bundle options, boolean
553 ignoreTargetSecurity, int userId) {
Tony Mak96d78f52017-05-03 18:53:21 +0100554 mStartActivityIntent = intent;
555 mUserIdActivityLaunchedIn = userId;
556 }
557 }
558
arangelov64439c1e2018-07-31 14:18:47 +0100559 public class TestInjector implements IntentForwarderActivity.Injector {
Tony Mak96d78f52017-05-03 18:53:21 +0100560
561 @Override
562 public IPackageManager getIPackageManager() {
563 return mIPm;
564 }
565
566 @Override
567 public UserManager getUserManager() {
568 return mUserManager;
569 }
570
571 @Override
572 public PackageManager getPackageManager() {
573 return mPm;
574 }
arangelov64439c1e2018-07-31 14:18:47 +0100575
576 @Override
577 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
578 ActivityInfo activityInfo = new ActivityInfo();
579 activityInfo.packageName = sPackageName;
580 activityInfo.name = sActivityName;
581 activityInfo.applicationInfo = mApplicationInfo;
582
583 ResolveInfo resolveInfo = new ResolveInfo();
584 resolveInfo.activityInfo = activityInfo;
585
586 return resolveInfo;
587 }
588
589 @Override
590 public void showToast(int messageId, int duration) {}
Tony Mak96d78f52017-05-03 18:53:21 +0100591 }
Alison Cichowlas76f0ccb2018-01-29 16:34:33 -0500592}