Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.internal.app; |
| 18 | |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 19 | import static junit.framework.Assert.assertEquals; |
| 20 | import static junit.framework.Assert.assertNotNull; |
| 21 | import static junit.framework.Assert.assertNull; |
| 22 | import static org.mockito.ArgumentMatchers.any; |
| 23 | import static org.mockito.ArgumentMatchers.anyInt; |
| 24 | import static org.mockito.ArgumentMatchers.anyString; |
| 25 | import static org.mockito.ArgumentMatchers.eq; |
| 26 | import static org.mockito.ArgumentMatchers.nullable; |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 27 | import static org.mockito.Mockito.never; |
| 28 | import static org.mockito.Mockito.spy; |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 29 | import static org.mockito.Mockito.verify; |
| 30 | import static org.mockito.Mockito.when; |
| 31 | |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 32 | import android.annotation.Nullable; |
| 33 | import android.content.ComponentName; |
| 34 | import android.content.Context; |
| 35 | import android.content.Intent; |
| 36 | import android.content.pm.ActivityInfo; |
| 37 | import android.content.pm.ApplicationInfo; |
| 38 | import android.content.pm.IPackageManager; |
| 39 | import android.content.pm.PackageManager; |
| 40 | import android.content.pm.ResolveInfo; |
| 41 | import android.content.pm.UserInfo; |
| 42 | import android.net.Uri; |
| 43 | import android.os.Bundle; |
| 44 | import android.os.RemoteException; |
| 45 | import android.os.UserHandle; |
| 46 | import android.os.UserManager; |
| 47 | import android.support.test.InstrumentationRegistry; |
| 48 | import android.support.test.rule.ActivityTestRule; |
| 49 | import android.support.test.runner.AndroidJUnit4; |
| 50 | import java.util.ArrayList; |
| 51 | import java.util.List; |
| 52 | import org.junit.Before; |
| 53 | import org.junit.Rule; |
| 54 | import org.junit.Test; |
| 55 | import org.junit.runner.RunWith; |
| 56 | import org.mockito.ArgumentCaptor; |
| 57 | import org.mockito.Mock; |
| 58 | import org.mockito.MockitoAnnotations; |
| 59 | |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 60 | @RunWith(AndroidJUnit4.class) |
| 61 | public class IntentForwarderActivityTest { |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 62 | |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 63 | 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(); |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame^] | 71 | |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 72 | 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(); |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame^] | 78 | |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 79 | 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; |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 86 | private static String sActivityName; |
| 87 | private static String sPackageName; |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 88 | |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame^] | 89 | @Mock |
| 90 | private IPackageManager mIPm; |
| 91 | @Mock |
| 92 | private PackageManager mPm; |
| 93 | @Mock |
| 94 | private UserManager mUserManager; |
| 95 | @Mock |
| 96 | private ApplicationInfo mApplicationInfo; |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 97 | |
| 98 | @Rule |
| 99 | public ActivityTestRule<IntentForwarderWrapperActivity> mActivityRule = |
| 100 | new ActivityTestRule<>(IntentForwarderWrapperActivity.class, true, false); |
| 101 | |
| 102 | private Context mContext; |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 103 | public static final String PHONE_NUMBER = "123-456-789"; |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 104 | |
| 105 | @Before |
| 106 | public void setup() { |
| 107 | MockitoAnnotations.initMocks(this); |
| 108 | mContext = InstrumentationRegistry.getTargetContext(); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 109 | sInjector = spy(new TestInjector()); |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 110 | } |
| 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 | |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 269 | @Test |
| 270 | public void shouldSkipDisclosure_notWhitelisted() throws RemoteException { |
| 271 | setupShouldSkipDisclosureTest(); |
| 272 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame^] | 273 | .setAction(Intent.ACTION_SEND) |
| 274 | .setType(TYPE_PLAIN_TEXT); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 275 | |
| 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) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame^] | 288 | .setAction(Intent.ACTION_SEND) |
| 289 | .setType(TYPE_PLAIN_TEXT); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 290 | |
| 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) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame^] | 301 | .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); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 326 | |
| 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) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame^] | 337 | .setAction(Intent.ACTION_DIAL); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 338 | |
| 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) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame^] | 349 | .setAction(Intent.ACTION_ALARM_CHANGED); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 350 | |
| 351 | mActivityRule.launchActivity(intent); |
| 352 | |
| 353 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 354 | verify(sInjector).showToast(anyInt(), anyInt()); |
| 355 | } |
| 356 | |
| 357 | @Test |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame^] | 358 | 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 |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 372 | public void shouldSkipDisclosure_textMessageIntent_sms() throws RemoteException { |
| 373 | setupShouldSkipDisclosureTest(); |
| 374 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame^] | 375 | .setAction(Intent.ACTION_SENDTO) |
| 376 | .setData(Uri.fromParts("sms", PHONE_NUMBER, null)); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 377 | |
| 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) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame^] | 388 | .setAction(Intent.ACTION_SENDTO) |
| 389 | .setData(Uri.fromParts("smsto", PHONE_NUMBER, null)); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 390 | |
| 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) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame^] | 401 | .setAction(Intent.ACTION_SENDTO) |
| 402 | .setData(Uri.fromParts("mms", PHONE_NUMBER, null)); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 403 | |
| 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) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame^] | 414 | .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)); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 472 | |
| 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) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame^] | 483 | .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)); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 513 | |
| 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( |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame^] | 532 | any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 533 | } |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 534 | |
| 535 | public static class IntentForwarderWrapperActivity extends IntentForwarderActivity { |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame^] | 536 | |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 537 | 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 Cichowlas | 76f0ccb | 2018-01-29 16:34:33 -0500 | [diff] [blame] | 552 | public void startActivityAsCaller(Intent intent, @Nullable Bundle options, boolean |
| 553 | ignoreTargetSecurity, int userId) { |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 554 | mStartActivityIntent = intent; |
| 555 | mUserIdActivityLaunchedIn = userId; |
| 556 | } |
| 557 | } |
| 558 | |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 559 | public class TestInjector implements IntentForwarderActivity.Injector { |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 560 | |
| 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 | } |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 575 | |
| 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 Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 591 | } |
Alison Cichowlas | 76f0ccb | 2018-01-29 16:34:33 -0500 | [diff] [blame] | 592 | } |