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