davidln | afe538d | 2019-02-06 15:58:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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.car.settings.applications; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import static org.mockito.Mockito.mock; |
| 22 | import static org.mockito.Mockito.when; |
| 23 | |
| 24 | import android.app.Activity; |
| 25 | import android.app.AlertDialog; |
| 26 | import android.car.userlib.CarUserManagerHelper; |
| 27 | import android.content.BroadcastReceiver; |
| 28 | import android.content.ComponentName; |
| 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
| 31 | import android.content.pm.ActivityInfo; |
| 32 | import android.content.pm.ApplicationInfo; |
| 33 | import android.content.pm.PackageInfo; |
| 34 | import android.content.pm.PackageManager; |
| 35 | import android.content.pm.ResolveInfo; |
| 36 | import android.content.pm.Signature; |
| 37 | import android.content.pm.UserInfo; |
| 38 | import android.os.Bundle; |
| 39 | import android.os.Handler; |
| 40 | import android.os.UserHandle; |
| 41 | import android.os.UserManager; |
| 42 | import android.widget.Button; |
| 43 | |
davidln | afe538d | 2019-02-06 15:58:57 -0800 | [diff] [blame] | 44 | import com.android.car.settings.R; |
| 45 | import com.android.car.settings.common.ConfirmationDialogFragment; |
| 46 | import com.android.car.settings.testutils.BaseTestActivity; |
| 47 | import com.android.car.settings.testutils.ShadowActivityManager; |
davidln | afe538d | 2019-02-06 15:58:57 -0800 | [diff] [blame] | 48 | import com.android.car.settings.testutils.ShadowApplicationPackageManager; |
| 49 | import com.android.car.settings.testutils.ShadowCarUserManagerHelper; |
| 50 | import com.android.car.settings.testutils.ShadowDevicePolicyManager; |
| 51 | import com.android.car.settings.testutils.ShadowIconDrawableFactory; |
| 52 | import com.android.car.settings.testutils.ShadowPermissionControllerManager; |
| 53 | import com.android.car.settings.testutils.ShadowSmsApplication; |
| 54 | import com.android.car.settings.testutils.ShadowUserManager; |
| 55 | import com.android.car.settings.testutils.ShadowUtils; |
| 56 | import com.android.settingslib.Utils; |
| 57 | import com.android.settingslib.applications.ApplicationsState; |
| 58 | |
| 59 | import org.junit.After; |
| 60 | import org.junit.Before; |
| 61 | import org.junit.Test; |
| 62 | import org.junit.runner.RunWith; |
| 63 | import org.mockito.Mock; |
| 64 | import org.mockito.MockitoAnnotations; |
davidln | 15c9351 | 2019-06-07 16:58:00 -0700 | [diff] [blame] | 65 | import org.robolectric.RobolectricTestRunner; |
davidln | afe538d | 2019-02-06 15:58:57 -0800 | [diff] [blame] | 66 | import org.robolectric.RuntimeEnvironment; |
| 67 | import org.robolectric.android.controller.ActivityController; |
| 68 | import org.robolectric.annotation.Config; |
| 69 | import org.robolectric.shadow.api.Shadow; |
| 70 | import org.robolectric.shadows.ShadowAlertDialog; |
| 71 | import org.robolectric.shadows.ShadowApplication; |
| 72 | import org.robolectric.util.ReflectionHelpers; |
| 73 | |
| 74 | import java.util.Arrays; |
| 75 | import java.util.Collections; |
| 76 | |
| 77 | /** Unit test for {@link ApplicationDetailsFragment}. */ |
davidln | 15c9351 | 2019-06-07 16:58:00 -0700 | [diff] [blame] | 78 | @RunWith(RobolectricTestRunner.class) |
davidln | afe538d | 2019-02-06 15:58:57 -0800 | [diff] [blame] | 79 | @Config(shadows = { |
| 80 | ShadowActivityManager.class, |
davidln | afe538d | 2019-02-06 15:58:57 -0800 | [diff] [blame] | 81 | ShadowApplicationPackageManager.class, |
| 82 | ShadowCarUserManagerHelper.class, |
| 83 | ShadowDevicePolicyManager.class, |
| 84 | ShadowIconDrawableFactory.class, |
| 85 | ShadowPermissionControllerManager.class, |
| 86 | ShadowSmsApplication.class, |
| 87 | ShadowUserManager.class, |
| 88 | ShadowUtils.class}) |
| 89 | public class ApplicationDetailsFragmentTest { |
| 90 | |
| 91 | private static final String PACKAGE_NAME = "com.android.car.settings.test"; |
| 92 | |
| 93 | @Mock |
| 94 | private CarUserManagerHelper mCarUserManagerHelper; |
| 95 | |
| 96 | private Context mContext; |
| 97 | private TestActivity mActivity; |
| 98 | private ActivityController<TestActivity> mController; |
| 99 | private ApplicationDetailsFragment mFragment; |
| 100 | |
| 101 | @Before |
| 102 | public void setUp() { |
| 103 | MockitoAnnotations.initMocks(this); |
| 104 | ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper); |
| 105 | int userId = UserHandle.myUserId(); |
| 106 | UserInfo userInfo = new UserInfo(); |
| 107 | userInfo.id = userId; |
| 108 | when(mCarUserManagerHelper.getCurrentProcessUserId()).thenReturn(userId); |
| 109 | when(mCarUserManagerHelper.getAllUsers()).thenReturn(Collections.singletonList(userInfo)); |
| 110 | UserManager mockUserManager = mock(UserManager.class); |
| 111 | when(mockUserManager.getUserInfo(userId)).thenReturn(userInfo); |
| 112 | ShadowUserManager.setInstance(mockUserManager); |
| 113 | |
| 114 | mContext = RuntimeEnvironment.application; |
| 115 | getShadowUserManager().addProfile(userId, userId, "profileName", /* profileFlags= */ 0); |
| 116 | |
| 117 | mActivity = new TestActivity(); |
| 118 | mController = ActivityController.of(mActivity); |
| 119 | mController.create(); |
| 120 | |
| 121 | mFragment = ApplicationDetailsFragment.getInstance(PACKAGE_NAME); |
| 122 | } |
| 123 | |
| 124 | @After |
| 125 | public void tearDown() { |
| 126 | // Prevent caching from interfering across tests. |
| 127 | ReflectionHelpers.setStaticField(ApplicationsState.class, "sInstance", null); |
| 128 | ReflectionHelpers.setStaticField(Utils.class, "sSystemSignature", null); |
| 129 | ShadowApplicationPackageManager.reset(); |
| 130 | ShadowCarUserManagerHelper.reset(); |
| 131 | ShadowDevicePolicyManager.reset(); |
| 132 | ShadowSmsApplication.reset(); |
| 133 | ShadowUserManager.reset(); |
| 134 | ShadowUtils.reset(); |
| 135 | } |
| 136 | |
| 137 | @Test |
| 138 | public void onStart_packageNotExplicitlyStopped_enablesForceStopButton() { |
| 139 | getShadowPackageManager().addPackage(createPackageInfoWithApplicationInfo(PACKAGE_NAME)); |
| 140 | mActivity.launchFragment(mFragment); |
| 141 | |
| 142 | mController.start(); |
| 143 | |
| 144 | assertThat(findForceStopButton(mActivity).isEnabled()).isTrue(); |
| 145 | } |
| 146 | |
| 147 | @Test |
| 148 | public void onStart_packageHasActiveAdmins_disablesForceStopButton() { |
| 149 | getShadowPackageManager().addPackage(createPackageInfoWithApplicationInfo(PACKAGE_NAME)); |
| 150 | mActivity.launchFragment(mFragment); |
| 151 | |
| 152 | getShadowDevicePolicyManager().setPackageHasActiveAdmins( |
| 153 | PACKAGE_NAME, /* hasActiveAdmins= */ true); |
| 154 | mController.start(); |
| 155 | |
| 156 | assertThat(findForceStopButton(mActivity).isEnabled()).isFalse(); |
| 157 | } |
| 158 | |
| 159 | @Test |
| 160 | public void onStart_appsControlUserRestriction_disablesForceStopButton() { |
| 161 | getShadowPackageManager().addPackage(createPackageInfoWithApplicationInfo(PACKAGE_NAME)); |
| 162 | mActivity.launchFragment(mFragment); |
| 163 | |
| 164 | when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction( |
| 165 | UserManager.DISALLOW_APPS_CONTROL)).thenReturn(true); |
| 166 | mController.start(); |
| 167 | |
| 168 | assertThat(findForceStopButton(mActivity).isEnabled()).isFalse(); |
| 169 | } |
| 170 | |
| 171 | @Test |
| 172 | public void onStart_packageExplicitlyStopped_queriesPackageRestart() { |
| 173 | int uid = 123; |
| 174 | PackageInfo packageInfo = createPackageInfoWithApplicationInfo(PACKAGE_NAME); |
| 175 | packageInfo.applicationInfo.uid = uid; |
| 176 | getShadowPackageManager().addPackage(packageInfo); |
| 177 | mActivity.launchFragment(mFragment); |
| 178 | packageInfo.applicationInfo.flags |= ApplicationInfo.FLAG_STOPPED; |
| 179 | |
| 180 | mController.start(); |
| 181 | |
| 182 | Intent broadcast = mActivity.getMostRecentOrderedBroadcastIntent(); |
| 183 | assertThat(broadcast).isNotNull(); |
| 184 | assertThat(broadcast.getAction()).isEqualTo(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 185 | assertThat(broadcast.getStringArrayExtra(Intent.EXTRA_PACKAGES)).isEqualTo( |
| 186 | new String[]{PACKAGE_NAME}); |
| 187 | assertThat(broadcast.getIntExtra(Intent.EXTRA_UID, 0)).isEqualTo(uid); |
| 188 | } |
| 189 | |
| 190 | @Test |
| 191 | public void onStart_packageExplicitlyStopped_success_enablesForceStopButton() { |
| 192 | PackageInfo packageInfo = createPackageInfoWithApplicationInfo(PACKAGE_NAME); |
| 193 | getShadowPackageManager().addPackage(packageInfo); |
| 194 | mActivity.launchFragment(mFragment); |
| 195 | |
| 196 | packageInfo.applicationInfo.flags |= ApplicationInfo.FLAG_STOPPED; |
| 197 | mController.start(); |
| 198 | BroadcastReceiver receiver = mActivity.getMostRecentOrderedBroadcastResultReceiver(); |
| 199 | receiver.setPendingResult( |
| 200 | new BroadcastReceiver.PendingResult(Activity.RESULT_OK, |
| 201 | "resultData", |
| 202 | /* resultExtras= */ null, |
| 203 | BroadcastReceiver.PendingResult.TYPE_UNREGISTERED, |
| 204 | /* ordered= */ true, |
| 205 | /* sticky= */ false, |
| 206 | /* token= */ null, |
| 207 | UserHandle.myUserId(), |
| 208 | /* flags= */ 0)); |
| 209 | receiver.onReceive(mContext, mActivity.getMostRecentOrderedBroadcastIntent()); |
| 210 | |
| 211 | assertThat(findForceStopButton(mActivity).isEnabled()).isTrue(); |
| 212 | } |
| 213 | |
| 214 | @Test |
| 215 | public void onStart_packageExplicitlyStopped_failure_disablesForceStopButton() { |
| 216 | PackageInfo packageInfo = createPackageInfoWithApplicationInfo(PACKAGE_NAME); |
| 217 | getShadowPackageManager().addPackage(packageInfo); |
| 218 | mActivity.launchFragment(mFragment); |
| 219 | |
| 220 | packageInfo.applicationInfo.flags |= ApplicationInfo.FLAG_STOPPED; |
| 221 | mController.start(); |
| 222 | BroadcastReceiver receiver = mActivity.getMostRecentOrderedBroadcastResultReceiver(); |
| 223 | receiver.setPendingResult( |
| 224 | new BroadcastReceiver.PendingResult(Activity.RESULT_CANCELED, |
| 225 | "resultData", |
| 226 | /* resultExtras= */ null, |
| 227 | BroadcastReceiver.PendingResult.TYPE_UNREGISTERED, |
| 228 | /* ordered= */ true, |
| 229 | /* sticky= */ false, |
| 230 | /* token= */ null, |
| 231 | UserHandle.myUserId(), |
| 232 | /* flags= */ 0)); |
| 233 | receiver.onReceive(mContext, mActivity.getMostRecentOrderedBroadcastIntent()); |
| 234 | |
| 235 | assertThat(findForceStopButton(mActivity).isEnabled()).isFalse(); |
| 236 | } |
| 237 | |
| 238 | @Test |
| 239 | public void onStart_bundledApp_showsDisableButton() { |
| 240 | PackageInfo packageInfo = createPackageInfoWithApplicationInfo(PACKAGE_NAME); |
| 241 | packageInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 242 | getShadowPackageManager().addPackage(packageInfo); |
| 243 | mActivity.launchFragment(mFragment); |
| 244 | |
| 245 | mController.start(); |
| 246 | |
| 247 | assertThat(findDisableButton(mActivity).getText()).isEqualTo( |
| 248 | mContext.getString(R.string.disable_text)); |
| 249 | } |
| 250 | |
| 251 | @Test |
| 252 | public void onStart_bundledApp_notEnabled_showsEnableButton() { |
| 253 | PackageInfo packageInfo = createPackageInfoWithApplicationInfo(PACKAGE_NAME); |
| 254 | packageInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 255 | packageInfo.applicationInfo.enabled = false; |
| 256 | getShadowPackageManager().addPackage(packageInfo); |
| 257 | mActivity.launchFragment(mFragment); |
| 258 | |
| 259 | mController.start(); |
| 260 | |
| 261 | assertThat(findDisableButton(mActivity).getText()).isEqualTo( |
| 262 | mContext.getString(R.string.enable_text)); |
| 263 | } |
| 264 | |
| 265 | @Test |
| 266 | public void onStart_bundledApp_enabled_disableUntilUsed_showsEnableButton() { |
| 267 | PackageInfo packageInfo = createPackageInfoWithApplicationInfo(PACKAGE_NAME); |
| 268 | packageInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 269 | packageInfo.applicationInfo.enabledSetting = |
| 270 | PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED; |
| 271 | getShadowPackageManager().addPackage(packageInfo); |
| 272 | mActivity.launchFragment(mFragment); |
| 273 | |
| 274 | mController.start(); |
| 275 | |
| 276 | assertThat(findDisableButton(mActivity).getText()).isEqualTo( |
| 277 | mContext.getString(R.string.enable_text)); |
| 278 | } |
| 279 | |
| 280 | @Test |
| 281 | public void onStart_bundledApp_homePackage_disablesDisableButton() { |
| 282 | PackageInfo packageInfo = createPackageInfoWithApplicationInfo(PACKAGE_NAME); |
| 283 | packageInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 284 | getShadowPackageManager().addPackage(packageInfo); |
| 285 | |
| 286 | ResolveInfo homeActivity = new ResolveInfo(); |
| 287 | ActivityInfo activityInfo = new ActivityInfo(); |
| 288 | activityInfo.packageName = PACKAGE_NAME; |
| 289 | homeActivity.activityInfo = activityInfo; |
| 290 | |
| 291 | getShadowPackageManager().setHomeActivities(Collections.singletonList(homeActivity)); |
| 292 | mActivity.launchFragment(mFragment); |
| 293 | |
| 294 | mController.start(); |
| 295 | |
| 296 | assertThat(findDisableButton(mActivity).isEnabled()).isFalse(); |
| 297 | } |
| 298 | |
| 299 | @Test |
| 300 | public void onStart_bundledApp_systemPackage_disablesDisableButton() { |
| 301 | PackageInfo packageInfo = createPackageInfoWithApplicationInfo(PACKAGE_NAME); |
| 302 | packageInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 303 | Signature[] signatures = new Signature[]{new Signature(PACKAGE_NAME.getBytes())}; |
| 304 | packageInfo.signatures = signatures; |
| 305 | getShadowPackageManager().addPackage(packageInfo); |
| 306 | |
| 307 | PackageInfo systemPackage = createPackageInfoWithApplicationInfo("android"); |
| 308 | systemPackage.signatures = signatures; |
| 309 | getShadowPackageManager().addPackage(systemPackage); |
| 310 | |
| 311 | mActivity.launchFragment(mFragment); |
| 312 | |
| 313 | mController.start(); |
| 314 | |
| 315 | assertThat(findDisableButton(mActivity).isEnabled()).isFalse(); |
| 316 | } |
| 317 | |
| 318 | @Test |
| 319 | public void onStart_bundledApp_enabledApp_keepEnabledPackage_disablesDisableButton() { |
| 320 | PackageInfo packageInfo = createPackageInfoWithApplicationInfo(PACKAGE_NAME); |
| 321 | packageInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 322 | getShadowPackageManager().addPackage(packageInfo); |
| 323 | mActivity.launchFragment(mFragment); |
| 324 | |
| 325 | ShadowSmsApplication.setDefaultSmsApplication(new ComponentName(PACKAGE_NAME, "cls")); |
| 326 | mController.start(); |
| 327 | |
| 328 | assertThat(findDisableButton(mActivity).isEnabled()).isFalse(); |
| 329 | } |
| 330 | |
| 331 | @Test |
| 332 | public void onStart_appNotBundled_showsUninstallButton() { |
| 333 | getShadowPackageManager().addPackage(createPackageInfoWithApplicationInfo(PACKAGE_NAME)); |
| 334 | mActivity.launchFragment(mFragment); |
| 335 | |
| 336 | mController.start(); |
| 337 | |
| 338 | assertThat(findUninstallButton(mActivity).getText()).isEqualTo( |
| 339 | mContext.getString(R.string.uninstall_text)); |
| 340 | } |
| 341 | |
| 342 | @Test |
| 343 | public void onStart_packageHasActiveAdmins_disablesUninstallButton() { |
| 344 | getShadowPackageManager().addPackage(createPackageInfoWithApplicationInfo(PACKAGE_NAME)); |
| 345 | mActivity.launchFragment(mFragment); |
| 346 | |
| 347 | getShadowDevicePolicyManager().setPackageHasActiveAdmins( |
| 348 | PACKAGE_NAME, /* hasActiveAdmins= */ true); |
| 349 | mController.start(); |
| 350 | |
| 351 | assertThat(findUninstallButton(mActivity).isEnabled()).isFalse(); |
| 352 | } |
| 353 | |
| 354 | @Test |
| 355 | public void onStart_deviceProvisioningPackage_disablesUninstallButton() { |
| 356 | getShadowPackageManager().addPackage(createPackageInfoWithApplicationInfo(PACKAGE_NAME)); |
| 357 | mActivity.launchFragment(mFragment); |
| 358 | |
| 359 | ShadowUtils.setDeviceProvisioningPackage(PACKAGE_NAME); |
| 360 | mController.start(); |
| 361 | |
| 362 | assertThat(findUninstallButton(mActivity).isEnabled()).isFalse(); |
| 363 | } |
| 364 | |
| 365 | @Test |
| 366 | public void onStart_uninstallQueued_disablesUninstallButton() { |
| 367 | getShadowPackageManager().addPackage(createPackageInfoWithApplicationInfo(PACKAGE_NAME)); |
| 368 | mActivity.launchFragment(mFragment); |
| 369 | |
| 370 | getShadowDevicePolicyManager().setIsUninstallInQueue(true); |
| 371 | mController.start(); |
| 372 | |
| 373 | assertThat(findUninstallButton(mActivity).isEnabled()).isFalse(); |
| 374 | } |
| 375 | |
| 376 | @Test |
| 377 | public void onStart_noDefaultHome_onlyHomeApp_disablesUninstallButton() { |
| 378 | getShadowPackageManager().addPackage(createPackageInfoWithApplicationInfo(PACKAGE_NAME)); |
| 379 | |
| 380 | ResolveInfo homeActivity = new ResolveInfo(); |
| 381 | ActivityInfo activityInfo = new ActivityInfo(); |
| 382 | activityInfo.packageName = PACKAGE_NAME; |
| 383 | homeActivity.activityInfo = activityInfo; |
| 384 | |
| 385 | getShadowPackageManager().setHomeActivities(Collections.singletonList(homeActivity)); |
| 386 | mActivity.launchFragment(mFragment); |
| 387 | |
| 388 | mController.start(); |
| 389 | |
| 390 | assertThat(findUninstallButton(mActivity).isEnabled()).isFalse(); |
| 391 | } |
| 392 | |
| 393 | @Test |
| 394 | public void onStart_noDefaultHome_multipleHomeApps_enablesUninstallButton() { |
| 395 | getShadowPackageManager().addPackage(createPackageInfoWithApplicationInfo(PACKAGE_NAME)); |
| 396 | |
| 397 | ResolveInfo homeActivity = new ResolveInfo(); |
| 398 | ActivityInfo activityInfo = new ActivityInfo(); |
| 399 | activityInfo.packageName = PACKAGE_NAME; |
| 400 | homeActivity.activityInfo = activityInfo; |
| 401 | |
| 402 | ResolveInfo altHomeActivity = new ResolveInfo(); |
| 403 | ActivityInfo altActivityInfo = new ActivityInfo(); |
| 404 | altActivityInfo.packageName = PACKAGE_NAME + ".Someotherhome"; |
| 405 | altHomeActivity.activityInfo = altActivityInfo; |
| 406 | |
| 407 | getShadowPackageManager().setHomeActivities(Arrays.asList(homeActivity, altHomeActivity)); |
| 408 | mActivity.launchFragment(mFragment); |
| 409 | |
| 410 | mController.start(); |
| 411 | |
| 412 | assertThat(findUninstallButton(mActivity).isEnabled()).isTrue(); |
| 413 | } |
| 414 | |
| 415 | @Test |
| 416 | public void onStart_defaultHomeApp_disablesUninstallButton() { |
| 417 | getShadowPackageManager().addPackage(createPackageInfoWithApplicationInfo(PACKAGE_NAME)); |
| 418 | |
| 419 | ResolveInfo homeActivity = new ResolveInfo(); |
| 420 | ActivityInfo activityInfo = new ActivityInfo(); |
| 421 | activityInfo.packageName = PACKAGE_NAME; |
| 422 | homeActivity.activityInfo = activityInfo; |
| 423 | |
| 424 | ResolveInfo altHomeActivity = new ResolveInfo(); |
| 425 | ActivityInfo altActivityInfo = new ActivityInfo(); |
| 426 | altActivityInfo.packageName = PACKAGE_NAME + ".Someotherhome"; |
| 427 | altHomeActivity.activityInfo = altActivityInfo; |
| 428 | |
| 429 | getShadowPackageManager().setHomeActivities(Arrays.asList(homeActivity, altHomeActivity)); |
| 430 | getShadowPackageManager().setDefaultHomeActivity(new ComponentName(PACKAGE_NAME, "cls")); |
| 431 | mActivity.launchFragment(mFragment); |
| 432 | |
| 433 | mController.start(); |
| 434 | |
| 435 | assertThat(findUninstallButton(mActivity).isEnabled()).isFalse(); |
| 436 | } |
| 437 | |
| 438 | @Test |
| 439 | public void onStart_appsControlUserRestriction_disablesUninstallButton() { |
| 440 | getShadowPackageManager().addPackage(createPackageInfoWithApplicationInfo(PACKAGE_NAME)); |
| 441 | mActivity.launchFragment(mFragment); |
| 442 | |
| 443 | when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction( |
| 444 | UserManager.DISALLOW_APPS_CONTROL)).thenReturn(true); |
| 445 | mController.start(); |
| 446 | |
| 447 | assertThat(findUninstallButton(mActivity).isEnabled()).isFalse(); |
| 448 | } |
| 449 | |
| 450 | @Test |
| 451 | public void onStart_uninstallAppsRestriction_disablesUninstallButton() { |
| 452 | getShadowPackageManager().addPackage(createPackageInfoWithApplicationInfo(PACKAGE_NAME)); |
| 453 | mActivity.launchFragment(mFragment); |
| 454 | |
| 455 | when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction( |
| 456 | UserManager.DISALLOW_UNINSTALL_APPS)).thenReturn(true); |
| 457 | mController.start(); |
| 458 | |
| 459 | assertThat(findUninstallButton(mActivity).isEnabled()).isFalse(); |
| 460 | } |
| 461 | |
| 462 | @Test |
| 463 | public void forceStopClicked_showsDialog() { |
| 464 | getShadowPackageManager().addPackage(createPackageInfoWithApplicationInfo(PACKAGE_NAME)); |
| 465 | mActivity.launchFragment(mFragment); |
| 466 | mController.start(); |
| 467 | |
| 468 | findForceStopButton(mActivity).performClick(); |
| 469 | |
| 470 | assertThat(mFragment.getFragmentManager().findFragmentByTag( |
| 471 | ApplicationDetailsFragment.FORCE_STOP_CONFIRM_DIALOG_TAG)).isInstanceOf( |
| 472 | ConfirmationDialogFragment.class); |
| 473 | } |
| 474 | |
| 475 | @Test |
| 476 | public void forceStopDialogConfirmed_forceStopsPackage() { |
| 477 | getShadowPackageManager().addPackage(createPackageInfoWithApplicationInfo(PACKAGE_NAME)); |
| 478 | mActivity.launchFragment(mFragment); |
| 479 | mController.start(); |
| 480 | findForceStopButton(mActivity).performClick(); |
| 481 | AlertDialog dialog = ShadowAlertDialog.getLatestAlertDialog(); |
| 482 | |
| 483 | dialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick(); |
| 484 | |
| 485 | assertThat(getShadowActivityManager().getMostRecentlyStoppedPackage()).isEqualTo( |
| 486 | PACKAGE_NAME); |
| 487 | } |
| 488 | |
| 489 | @Test |
| 490 | public void disableClicked_showsDialog() { |
| 491 | // Use bundled app to get disable button. |
| 492 | PackageInfo packageInfo = createPackageInfoWithApplicationInfo(PACKAGE_NAME); |
| 493 | packageInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 494 | getShadowPackageManager().addPackage(packageInfo); |
| 495 | mActivity.launchFragment(mFragment); |
| 496 | mController.start(); |
| 497 | |
| 498 | findDisableButton(mActivity).performClick(); |
| 499 | |
| 500 | assertThat(mFragment.getFragmentManager().findFragmentByTag( |
| 501 | ApplicationDetailsFragment.DISABLE_CONFIRM_DIALOG_TAG)).isInstanceOf( |
| 502 | ConfirmationDialogFragment.class); |
| 503 | } |
| 504 | |
| 505 | @Test |
| 506 | public void disableDialogConfirmed_disablesPackage() { |
| 507 | // Use bundled app to get disable button. |
| 508 | PackageInfo packageInfo = createPackageInfoWithApplicationInfo(PACKAGE_NAME); |
| 509 | packageInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 510 | getShadowPackageManager().addPackage(packageInfo); |
| 511 | mActivity.launchFragment(mFragment); |
| 512 | mController.start(); |
| 513 | findDisableButton(mActivity).performClick(); |
| 514 | AlertDialog dialog = ShadowAlertDialog.getLatestAlertDialog(); |
| 515 | |
| 516 | dialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick(); |
| 517 | |
| 518 | assertThat( |
| 519 | mContext.getPackageManager().getApplicationEnabledSetting(PACKAGE_NAME)).isEqualTo( |
| 520 | PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER); |
| 521 | } |
| 522 | |
| 523 | @Test |
| 524 | public void enableClicked_enablesPackage() { |
| 525 | // Use disabled bundled app to get enable button. |
| 526 | PackageInfo packageInfo = createPackageInfoWithApplicationInfo(PACKAGE_NAME); |
| 527 | packageInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 528 | packageInfo.applicationInfo.enabled = false; |
| 529 | getShadowPackageManager().addPackage(packageInfo); |
| 530 | mActivity.launchFragment(mFragment); |
| 531 | mController.start(); |
| 532 | |
| 533 | findDisableButton(mActivity).performClick(); |
| 534 | |
| 535 | assertThat( |
| 536 | mContext.getPackageManager().getApplicationEnabledSetting(PACKAGE_NAME)).isEqualTo( |
| 537 | PackageManager.COMPONENT_ENABLED_STATE_DEFAULT); |
| 538 | } |
| 539 | |
| 540 | @Test |
| 541 | public void uninstallClicked_startsUninstallActivity() { |
| 542 | getShadowPackageManager().addPackage(createPackageInfoWithApplicationInfo(PACKAGE_NAME)); |
| 543 | mActivity.launchFragment(mFragment); |
| 544 | mController.start(); |
| 545 | |
| 546 | findUninstallButton(mActivity).performClick(); |
| 547 | |
| 548 | Intent intent = ShadowApplication.getInstance().getNextStartedActivity(); |
| 549 | assertThat(intent.getAction()).isEqualTo(Intent.ACTION_UNINSTALL_PACKAGE); |
| 550 | assertThat(intent.getBooleanExtra(Intent.EXTRA_UNINSTALL_ALL_USERS, false)).isTrue(); |
| 551 | assertThat(intent.getBooleanExtra(Intent.EXTRA_RETURN_RESULT, false)).isTrue(); |
| 552 | assertThat(intent.getData().toString()).isEqualTo("package:" + PACKAGE_NAME); |
| 553 | } |
| 554 | |
| 555 | @Test |
| 556 | public void processActivityResult_uninstall_resultOk_goesBack() { |
| 557 | getShadowPackageManager().addPackage(createPackageInfoWithApplicationInfo(PACKAGE_NAME)); |
| 558 | mActivity.launchFragment(mFragment); |
| 559 | mController.start(); |
| 560 | findUninstallButton(mActivity).performClick(); |
| 561 | |
| 562 | mFragment.processActivityResult(ApplicationDetailsFragment.UNINSTALL_REQUEST_CODE, |
| 563 | Activity.RESULT_OK, /* data= */ null); |
| 564 | |
| 565 | assertThat(mActivity.getOnBackPressedFlag()).isTrue(); |
| 566 | } |
| 567 | |
| 568 | private Button findForceStopButton(Activity activity) { |
| 569 | return activity.findViewById(R.id.action_button2); |
| 570 | } |
| 571 | |
| 572 | private Button findDisableButton(Activity activity) { |
| 573 | // Same button is used with a different handler. This method is purely for readability. |
| 574 | return findUninstallButton(activity); |
| 575 | } |
| 576 | |
| 577 | private Button findUninstallButton(Activity activity) { |
| 578 | return activity.findViewById(R.id.action_button1); |
| 579 | } |
| 580 | |
| 581 | private PackageInfo createPackageInfoWithApplicationInfo(String packageName) { |
| 582 | PackageInfo packageInfo = new PackageInfo(); |
| 583 | packageInfo.packageName = packageName; |
| 584 | packageInfo.applicationInfo = createApplicationInfo(packageName); |
| 585 | return packageInfo; |
| 586 | } |
| 587 | |
| 588 | private ApplicationInfo createApplicationInfo(String packageName) { |
| 589 | ApplicationInfo applicationInfo = new ApplicationInfo(); |
| 590 | applicationInfo.packageName = packageName; |
| 591 | applicationInfo.sourceDir = |
| 592 | RuntimeEnvironment.getTempDirectory() |
| 593 | .createIfNotExists(applicationInfo.packageName + "-sourceDir") |
| 594 | .toAbsolutePath() |
| 595 | .toString(); |
| 596 | return applicationInfo; |
| 597 | } |
| 598 | |
| 599 | private ShadowUserManager getShadowUserManager() { |
| 600 | return Shadow.extract(UserManager.get(mContext)); |
| 601 | } |
| 602 | |
| 603 | private ShadowApplicationPackageManager getShadowPackageManager() { |
| 604 | return Shadow.extract(mContext.getPackageManager()); |
| 605 | } |
| 606 | |
| 607 | private ShadowDevicePolicyManager getShadowDevicePolicyManager() { |
| 608 | return Shadow.extract(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE)); |
| 609 | } |
| 610 | |
| 611 | private ShadowActivityManager getShadowActivityManager() { |
| 612 | return Shadow.extract(mContext.getSystemService(Context.ACTIVITY_SERVICE)); |
| 613 | } |
| 614 | |
| 615 | /** We extend the test activity here to add functionality that isn't useful elsewhere. */ |
| 616 | private static class TestActivity extends BaseTestActivity { |
| 617 | |
| 618 | private Intent mOrderedBroadcastIntent; |
| 619 | private BroadcastReceiver mOrderedBroadcastResultReceiver; |
| 620 | |
| 621 | @Override |
| 622 | public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, |
| 623 | String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, |
| 624 | int initialCode, String initialData, Bundle initialExtras) { |
| 625 | mOrderedBroadcastIntent = intent; |
| 626 | mOrderedBroadcastResultReceiver = resultReceiver; |
| 627 | } |
| 628 | |
| 629 | Intent getMostRecentOrderedBroadcastIntent() { |
| 630 | return mOrderedBroadcastIntent; |
| 631 | } |
| 632 | |
| 633 | BroadcastReceiver getMostRecentOrderedBroadcastResultReceiver() { |
| 634 | return mOrderedBroadcastResultReceiver; |
| 635 | } |
| 636 | } |
| 637 | } |