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