blob: 9b13af2a43577419bff5be6bd6e614d89057750f [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;
Alison Cichowlas3e340502018-08-07 17:15:01 -040022
Tony Mak96d78f52017-05-03 18:53:21 +010023import static org.mockito.ArgumentMatchers.any;
24import static org.mockito.ArgumentMatchers.anyInt;
25import static org.mockito.ArgumentMatchers.anyString;
26import static org.mockito.ArgumentMatchers.eq;
27import static org.mockito.ArgumentMatchers.nullable;
arangelov64439c1e2018-07-31 14:18:47 +010028import static org.mockito.Mockito.never;
29import static org.mockito.Mockito.spy;
Tony Mak96d78f52017-05-03 18:53:21 +010030import static org.mockito.Mockito.verify;
31import static org.mockito.Mockito.when;
32
arangelov64439c1e2018-07-31 14:18:47 +010033import android.annotation.Nullable;
34import android.content.ComponentName;
35import android.content.Context;
36import android.content.Intent;
37import android.content.pm.ActivityInfo;
38import android.content.pm.ApplicationInfo;
39import android.content.pm.IPackageManager;
40import android.content.pm.PackageManager;
41import android.content.pm.ResolveInfo;
42import android.content.pm.UserInfo;
43import android.net.Uri;
44import android.os.Bundle;
Alison Cichowlas3e340502018-08-07 17:15:01 -040045import android.os.IBinder;
arangelov64439c1e2018-07-31 14:18:47 +010046import android.os.RemoteException;
47import android.os.UserHandle;
48import android.os.UserManager;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090049
50import androidx.test.InstrumentationRegistry;
51import androidx.test.rule.ActivityTestRule;
52import androidx.test.runner.AndroidJUnit4;
Alison Cichowlas3e340502018-08-07 17:15:01 -040053
arangelov64439c1e2018-07-31 14:18:47 +010054import org.junit.Before;
55import org.junit.Rule;
56import org.junit.Test;
57import org.junit.runner.RunWith;
58import org.mockito.ArgumentCaptor;
59import org.mockito.Mock;
60import org.mockito.MockitoAnnotations;
61
Alison Cichowlas3e340502018-08-07 17:15:01 -040062import java.util.ArrayList;
63import java.util.List;
64
Tony Mak96d78f52017-05-03 18:53:21 +010065@RunWith(AndroidJUnit4.class)
66public class IntentForwarderActivityTest {
arangelov64439c1e2018-07-31 14:18:47 +010067
Tony Mak96d78f52017-05-03 18:53:21 +010068 private static final ComponentName FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME =
69 new ComponentName(
70 "android",
71 IntentForwarderActivity.FORWARD_INTENT_TO_MANAGED_PROFILE
72 );
73 private static final String TYPE_PLAIN_TEXT = "text/plain";
74
75 private static UserInfo MANAGED_PROFILE_INFO = new UserInfo();
arangelov38303742018-08-14 20:14:12 +010076
Tony Mak96d78f52017-05-03 18:53:21 +010077 static {
78 MANAGED_PROFILE_INFO.id = 10;
79 MANAGED_PROFILE_INFO.flags = UserInfo.FLAG_MANAGED_PROFILE;
80 }
81
82 private static UserInfo CURRENT_USER_INFO = new UserInfo();
arangelov38303742018-08-14 20:14:12 +010083
Tony Mak96d78f52017-05-03 18:53:21 +010084 static {
85 CURRENT_USER_INFO.id = UserHandle.myUserId();
86 CURRENT_USER_INFO.flags = 0;
87 }
88
89 private static IntentForwarderActivity.Injector sInjector;
90 private static ComponentName sComponentName;
arangelov64439c1e2018-07-31 14:18:47 +010091 private static String sActivityName;
92 private static String sPackageName;
Tony Mak96d78f52017-05-03 18:53:21 +010093
arangelov38303742018-08-14 20:14:12 +010094 @Mock
95 private IPackageManager mIPm;
96 @Mock
97 private PackageManager mPm;
98 @Mock
99 private UserManager mUserManager;
100 @Mock
101 private ApplicationInfo mApplicationInfo;
Tony Mak96d78f52017-05-03 18:53:21 +0100102
103 @Rule
104 public ActivityTestRule<IntentForwarderWrapperActivity> mActivityRule =
105 new ActivityTestRule<>(IntentForwarderWrapperActivity.class, true, false);
106
107 private Context mContext;
arangelov64439c1e2018-07-31 14:18:47 +0100108 public static final String PHONE_NUMBER = "123-456-789";
Tony Mak96d78f52017-05-03 18:53:21 +0100109
110 @Before
111 public void setup() {
112 MockitoAnnotations.initMocks(this);
113 mContext = InstrumentationRegistry.getTargetContext();
arangelov64439c1e2018-07-31 14:18:47 +0100114 sInjector = spy(new TestInjector());
Tony Mak96d78f52017-05-03 18:53:21 +0100115 }
116
117 @Test
118 public void forwardToManagedProfile_canForward_sendIntent() throws Exception {
119 sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
120
121 // Intent can be forwarded.
122 when(mIPm.canForwardTo(
123 any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true);
124
125 // Managed profile exists.
126 List<UserInfo> profiles = new ArrayList<>();
127 profiles.add(CURRENT_USER_INFO);
128 profiles.add(MANAGED_PROFILE_INFO);
129 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
130
131 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class);
132 intent.setAction(Intent.ACTION_SEND);
133 intent.setType(TYPE_PLAIN_TEXT);
134 IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent);
135
136 ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
137 verify(mIPm).canForwardTo(intentCaptor.capture(), eq(TYPE_PLAIN_TEXT), anyInt(), anyInt());
138 assertEquals(Intent.ACTION_SEND, intentCaptor.getValue().getAction());
139
140 assertEquals(Intent.ACTION_SEND, intentCaptor.getValue().getAction());
141 assertNotNull(activity.mStartActivityIntent);
142 assertEquals(Intent.ACTION_SEND, activity.mStartActivityIntent.getAction());
143 assertNull(activity.mStartActivityIntent.getPackage());
144 assertNull(activity.mStartActivityIntent.getComponent());
145 assertEquals(CURRENT_USER_INFO.id, activity.mStartActivityIntent.getContentUserHint());
146
147 assertEquals(MANAGED_PROFILE_INFO.id, activity.mUserIdActivityLaunchedIn);
148 }
149
150 @Test
151 public void forwardToManagedProfile_cannotForward_sendIntent() throws Exception {
152 sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
153
154 // Intent cannot be forwarded.
155 when(mIPm.canForwardTo(
156 any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(false);
157
158 // Managed profile exists.
159 List<UserInfo> profiles = new ArrayList<>();
160 profiles.add(CURRENT_USER_INFO);
161 profiles.add(MANAGED_PROFILE_INFO);
162 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
163
164 // Create ACTION_SEND intent.
165 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class);
166 intent.setAction(Intent.ACTION_SEND);
167 IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent);
168
169 assertNull(activity.mStartActivityIntent);
170 }
171
172 @Test
173 public void forwardToManagedProfile_noManagedProfile_sendIntent() throws Exception {
174 sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
175
176 // Intent can be forwarded.
177 when(mIPm.canForwardTo(
178 any(Intent.class), anyString(), anyInt(), anyInt())).thenReturn(true);
179
180 // Managed profile does not exist.
181 List<UserInfo> profiles = new ArrayList<>();
182 profiles.add(CURRENT_USER_INFO);
183 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
184
185 // Create ACTION_SEND intent.
186 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class);
187 intent.setAction(Intent.ACTION_SEND);
188 IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent);
189
190 assertNull(activity.mStartActivityIntent);
191 }
192
193 @Test
194 public void forwardToManagedProfile_canForward_chooserIntent() throws Exception {
195 sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
196
197 // Intent can be forwarded.
198 when(mIPm.canForwardTo(
199 any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true);
200
201 // Manage profile exists.
202 List<UserInfo> profiles = new ArrayList<>();
203 profiles.add(CURRENT_USER_INFO);
204 profiles.add(MANAGED_PROFILE_INFO);
205 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
206
207 // Create chooser Intent
208 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class);
209 intent.setAction(Intent.ACTION_CHOOSER);
210 Intent sendIntent = new Intent(Intent.ACTION_SEND);
211 sendIntent.setComponent(new ComponentName("xx", "yyy"));
212 sendIntent.setType(TYPE_PLAIN_TEXT);
213 intent.putExtra(Intent.EXTRA_INTENT, sendIntent);
214 IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent);
215
216 ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
217 verify(mIPm).canForwardTo(intentCaptor.capture(), eq(TYPE_PLAIN_TEXT), anyInt(), anyInt());
218 assertEquals(Intent.ACTION_SEND, intentCaptor.getValue().getAction());
219
220 assertNotNull(activity.mStartActivityIntent);
221 assertEquals(Intent.ACTION_CHOOSER, activity.mStartActivityIntent.getAction());
222 assertNull(activity.mStartActivityIntent.getPackage());
223 assertNull(activity.mStartActivityIntent.getComponent());
224
225 Intent innerIntent = activity.mStartActivityIntent.getParcelableExtra(Intent.EXTRA_INTENT);
226 assertNotNull(innerIntent);
227 assertEquals(Intent.ACTION_SEND, innerIntent.getAction());
228 assertNull(innerIntent.getComponent());
229 assertNull(innerIntent.getPackage());
230 assertEquals(CURRENT_USER_INFO.id, innerIntent.getContentUserHint());
231
232 assertEquals(MANAGED_PROFILE_INFO.id, activity.mUserIdActivityLaunchedIn);
233 }
234
235 @Test
236 public void forwardToManagedProfile_canForward_selectorIntent() throws Exception {
237 sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
238
239 // Intent can be forwarded.
240 when(mIPm.canForwardTo(
241 any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true);
242
243 // Manage profile exists.
244 List<UserInfo> profiles = new ArrayList<>();
245 profiles.add(CURRENT_USER_INFO);
246 profiles.add(MANAGED_PROFILE_INFO);
247 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
248
249 // Create selector intent.
250 Intent intent = Intent.makeMainSelectorActivity(
251 Intent.ACTION_VIEW, Intent.CATEGORY_BROWSABLE);
252 IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent);
253
254 ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
255 verify(mIPm).canForwardTo(
256 intentCaptor.capture(), nullable(String.class), anyInt(), anyInt());
257 assertEquals(Intent.ACTION_VIEW, intentCaptor.getValue().getAction());
258
259 assertNotNull(activity.mStartActivityIntent);
260 assertEquals(Intent.ACTION_MAIN, activity.mStartActivityIntent.getAction());
261 assertNull(activity.mStartActivityIntent.getPackage());
262 assertNull(activity.mStartActivityIntent.getComponent());
263 assertEquals(CURRENT_USER_INFO.id, activity.mStartActivityIntent.getContentUserHint());
264
265 Intent innerIntent = activity.mStartActivityIntent.getSelector();
266 assertNotNull(innerIntent);
267 assertEquals(Intent.ACTION_VIEW, innerIntent.getAction());
268 assertNull(innerIntent.getComponent());
269 assertNull(innerIntent.getPackage());
270
271 assertEquals(MANAGED_PROFILE_INFO.id, activity.mUserIdActivityLaunchedIn);
272 }
273
arangelov64439c1e2018-07-31 14:18:47 +0100274 @Test
275 public void shouldSkipDisclosure_notWhitelisted() throws RemoteException {
276 setupShouldSkipDisclosureTest();
277 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100278 .setAction(Intent.ACTION_SEND)
279 .setType(TYPE_PLAIN_TEXT);
arangelov64439c1e2018-07-31 14:18:47 +0100280
281 mActivityRule.launchActivity(intent);
282
283 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
284 verify(sInjector).showToast(anyInt(), anyInt());
285 }
286
287 @Test
288 public void shouldSkipDisclosure_withResolverActivity() throws RemoteException {
289 setupShouldSkipDisclosureTest();
290 sActivityName = ResolverActivity.class.getName();
291 sPackageName = "android";
292 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100293 .setAction(Intent.ACTION_SEND)
294 .setType(TYPE_PLAIN_TEXT);
arangelov64439c1e2018-07-31 14:18:47 +0100295
296 mActivityRule.launchActivity(intent);
297
298 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
299 verify(sInjector, never()).showToast(anyInt(), anyInt());
300 }
301
302 @Test
303 public void shouldSkipDisclosure_callIntent_call() throws RemoteException {
304 setupShouldSkipDisclosureTest();
305 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100306 .setAction(Intent.ACTION_CALL);
307
308 mActivityRule.launchActivity(intent);
309
310 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
311 verify(sInjector, never()).showToast(anyInt(), anyInt());
312 }
313
314 @Test
315 public void shouldSkipDisclosure_callIntent_callPrivileged() throws RemoteException {
316 setupShouldSkipDisclosureTest();
317 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
318 .setAction(Intent.ACTION_CALL_PRIVILEGED);
319
320 mActivityRule.launchActivity(intent);
321
322 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
323 verify(sInjector, never()).showToast(anyInt(), anyInt());
324 }
325
326 @Test
327 public void shouldSkipDisclosure_callIntent_callEmergency() throws RemoteException {
328 setupShouldSkipDisclosureTest();
329 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
330 .setAction(Intent.ACTION_CALL_EMERGENCY);
arangelov64439c1e2018-07-31 14:18:47 +0100331
332 mActivityRule.launchActivity(intent);
333
334 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
335 verify(sInjector, never()).showToast(anyInt(), anyInt());
336 }
337
338 @Test
339 public void shouldSkipDisclosure_callIntent_dial() throws RemoteException {
340 setupShouldSkipDisclosureTest();
341 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100342 .setAction(Intent.ACTION_DIAL);
arangelov64439c1e2018-07-31 14:18:47 +0100343
344 mActivityRule.launchActivity(intent);
345
346 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
347 verify(sInjector, never()).showToast(anyInt(), anyInt());
348 }
349
350 @Test
351 public void shouldSkipDisclosure_callIntent_notCallOrDial() throws RemoteException {
352 setupShouldSkipDisclosureTest();
353 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100354 .setAction(Intent.ACTION_ALARM_CHANGED);
arangelov64439c1e2018-07-31 14:18:47 +0100355
356 mActivityRule.launchActivity(intent);
357
358 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
359 verify(sInjector).showToast(anyInt(), anyInt());
360 }
361
362 @Test
arangelov38303742018-08-14 20:14:12 +0100363 public void shouldSkipDisclosure_callIntent_actionViewTel() throws RemoteException {
364 setupShouldSkipDisclosureTest();
365 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
366 .setAction(Intent.ACTION_VIEW)
367 .addCategory(Intent.CATEGORY_BROWSABLE)
368 .setData(Uri.fromParts("tel", PHONE_NUMBER, null));
369
370 mActivityRule.launchActivity(intent);
371
372 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
373 verify(sInjector, never()).showToast(anyInt(), anyInt());
374 }
375
376 @Test
arangelov64439c1e2018-07-31 14:18:47 +0100377 public void shouldSkipDisclosure_textMessageIntent_sms() throws RemoteException {
378 setupShouldSkipDisclosureTest();
379 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100380 .setAction(Intent.ACTION_SENDTO)
381 .setData(Uri.fromParts("sms", PHONE_NUMBER, null));
arangelov64439c1e2018-07-31 14:18:47 +0100382
383 mActivityRule.launchActivity(intent);
384
385 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
386 verify(sInjector, never()).showToast(anyInt(), anyInt());
387 }
388
389 @Test
390 public void shouldSkipDisclosure_textMessageIntent_smsto() throws RemoteException {
391 setupShouldSkipDisclosureTest();
392 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100393 .setAction(Intent.ACTION_SENDTO)
394 .setData(Uri.fromParts("smsto", PHONE_NUMBER, null));
arangelov64439c1e2018-07-31 14:18:47 +0100395
396 mActivityRule.launchActivity(intent);
397
398 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
399 verify(sInjector, never()).showToast(anyInt(), anyInt());
400 }
401
402 @Test
403 public void shouldSkipDisclosure_textMessageIntent_mms() throws RemoteException {
404 setupShouldSkipDisclosureTest();
405 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100406 .setAction(Intent.ACTION_SENDTO)
407 .setData(Uri.fromParts("mms", PHONE_NUMBER, null));
arangelov64439c1e2018-07-31 14:18:47 +0100408
409 mActivityRule.launchActivity(intent);
410
411 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
412 verify(sInjector, never()).showToast(anyInt(), anyInt());
413 }
414
415 @Test
416 public void shouldSkipDisclosure_textMessageIntent_mmsto() throws RemoteException {
417 setupShouldSkipDisclosureTest();
418 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100419 .setAction(Intent.ACTION_SENDTO)
420 .setData(Uri.fromParts("mmsto", PHONE_NUMBER, null));
421
422 mActivityRule.launchActivity(intent);
423
424 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
425 verify(sInjector, never()).showToast(anyInt(), anyInt());
426 }
427
428 @Test
429 public void shouldSkipDisclosure_textMessageIntent_actionViewSms() throws RemoteException {
430 setupShouldSkipDisclosureTest();
431 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
432 .setAction(Intent.ACTION_VIEW)
433 .addCategory(Intent.CATEGORY_BROWSABLE)
434 .setData(Uri.fromParts("sms", PHONE_NUMBER, null));
435
436 mActivityRule.launchActivity(intent);
437
438 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
439 verify(sInjector, never()).showToast(anyInt(), anyInt());
440 }
441
442 @Test
443 public void shouldSkipDisclosure_textMessageIntent_actionViewSmsto() throws RemoteException {
444 setupShouldSkipDisclosureTest();
445 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
446 .setAction(Intent.ACTION_VIEW)
447 .addCategory(Intent.CATEGORY_BROWSABLE)
448 .setData(Uri.fromParts("smsto", PHONE_NUMBER, null));
449
450 mActivityRule.launchActivity(intent);
451
452 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
453 verify(sInjector, never()).showToast(anyInt(), anyInt());
454 }
455
456 @Test
457 public void shouldSkipDisclosure_textMessageIntent_actionViewMms() throws RemoteException {
458 setupShouldSkipDisclosureTest();
459 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
460 .setAction(Intent.ACTION_VIEW)
461 .addCategory(Intent.CATEGORY_BROWSABLE)
462 .setData(Uri.fromParts("mms", PHONE_NUMBER, null));
463
464 mActivityRule.launchActivity(intent);
465
466 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
467 verify(sInjector, never()).showToast(anyInt(), anyInt());
468 }
469
470 @Test
471 public void shouldSkipDisclosure_textMessageIntent_actionViewMmsto() throws RemoteException {
472 setupShouldSkipDisclosureTest();
473 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
474 .setAction(Intent.ACTION_VIEW)
475 .addCategory(Intent.CATEGORY_BROWSABLE)
476 .setData(Uri.fromParts("mmsto", PHONE_NUMBER, null));
arangelov64439c1e2018-07-31 14:18:47 +0100477
478 mActivityRule.launchActivity(intent);
479
480 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
481 verify(sInjector, never()).showToast(anyInt(), anyInt());
482 }
483
484 @Test
485 public void shouldSkipDisclosure_textMessageIntent_invalidUri() throws RemoteException {
486 setupShouldSkipDisclosureTest();
487 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100488 .setAction(Intent.ACTION_SENDTO)
489 .setData(Uri.fromParts("invalid", PHONE_NUMBER, null));
490
491 mActivityRule.launchActivity(intent);
492
493 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
494 verify(sInjector).showToast(anyInt(), anyInt());
495 }
496
497 @Test
498 public void shouldSkipDisclosure_viewBrowsableIntent_invalidUri() throws RemoteException {
499 setupShouldSkipDisclosureTest();
500 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
501 .setAction(Intent.ACTION_VIEW)
502 .addCategory(Intent.CATEGORY_BROWSABLE)
503 .setData(Uri.fromParts("invalid", PHONE_NUMBER, null));
504
505 mActivityRule.launchActivity(intent);
506
507 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
508 verify(sInjector).showToast(anyInt(), anyInt());
509 }
510
511 @Test
512 public void shouldSkipDisclosure_viewBrowsableIntent_normalUrl() throws RemoteException {
513 setupShouldSkipDisclosureTest();
514 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
515 .setAction(Intent.ACTION_VIEW)
516 .addCategory(Intent.CATEGORY_BROWSABLE)
517 .setData(Uri.fromParts("http", "apache.org", null));
arangelov64439c1e2018-07-31 14:18:47 +0100518
519 mActivityRule.launchActivity(intent);
520
521 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
522 verify(sInjector).showToast(anyInt(), anyInt());
523 }
524
525 private void setupShouldSkipDisclosureTest() throws RemoteException {
526 sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
527 sActivityName = "MyTestActivity";
528 sPackageName = "test.package.name";
529 when(mApplicationInfo.isSystemApp()).thenReturn(true);
530 // Managed profile exists.
531 List<UserInfo> profiles = new ArrayList<>();
532 profiles.add(CURRENT_USER_INFO);
533 profiles.add(MANAGED_PROFILE_INFO);
534 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
535 // Intent can be forwarded.
536 when(mIPm.canForwardTo(
arangelov38303742018-08-14 20:14:12 +0100537 any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true);
arangelov64439c1e2018-07-31 14:18:47 +0100538 }
Tony Mak96d78f52017-05-03 18:53:21 +0100539
540 public static class IntentForwarderWrapperActivity extends IntentForwarderActivity {
arangelov38303742018-08-14 20:14:12 +0100541
Tony Mak96d78f52017-05-03 18:53:21 +0100542 private Intent mStartActivityIntent;
543 private int mUserIdActivityLaunchedIn;
544
545 @Override
546 public void onCreate(@Nullable Bundle savedInstanceState) {
547 getIntent().setComponent(sComponentName);
548 super.onCreate(savedInstanceState);
549 }
550
551 @Override
552 protected Injector createInjector() {
553 return sInjector;
554 }
555
556 @Override
Alison Cichowlas3e340502018-08-07 17:15:01 -0400557 public void startActivityAsCaller(Intent intent, @Nullable Bundle options,
558 IBinder permissionToken, boolean ignoreTargetSecurity, int userId) {
Tony Mak96d78f52017-05-03 18:53:21 +0100559 mStartActivityIntent = intent;
560 mUserIdActivityLaunchedIn = userId;
561 }
562 }
563
arangelov64439c1e2018-07-31 14:18:47 +0100564 public class TestInjector implements IntentForwarderActivity.Injector {
Tony Mak96d78f52017-05-03 18:53:21 +0100565
566 @Override
567 public IPackageManager getIPackageManager() {
568 return mIPm;
569 }
570
571 @Override
572 public UserManager getUserManager() {
573 return mUserManager;
574 }
575
576 @Override
577 public PackageManager getPackageManager() {
578 return mPm;
579 }
arangelov64439c1e2018-07-31 14:18:47 +0100580
581 @Override
582 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
583 ActivityInfo activityInfo = new ActivityInfo();
584 activityInfo.packageName = sPackageName;
585 activityInfo.name = sActivityName;
586 activityInfo.applicationInfo = mApplicationInfo;
587
588 ResolveInfo resolveInfo = new ResolveInfo();
589 resolveInfo.activityInfo = activityInfo;
590
591 return resolveInfo;
592 }
593
594 @Override
595 public void showToast(int messageId, int duration) {}
Tony Mak96d78f52017-05-03 18:53:21 +0100596 }
Alison Cichowlas3e340502018-08-07 17:15:01 -0400597}