Merge "Convert install app detail to dashboard fragment."

This commit is contained in:
TreeHugger Robot
2017-12-01 02:16:26 +00:00
committed by Android (Google) Code Review
28 changed files with 2806 additions and 455 deletions

View File

@@ -1,3 +1,4 @@
com.android.settings.applications.AppInfoDashboardFragment
com.android.settings.bluetooth.DevicePickerFragment
com.android.settings.bluetooth.BluetoothDeviceDetailsFragment
com.android.settings.bluetooth.BluetoothPairingDetail

View File

@@ -46,7 +46,6 @@ com.android.settings.applications.RunningServices
com.android.settings.applications.ConfirmConvertToFbe
com.android.settings.deviceinfo.PublicVolumeSettings
com.android.settings.applications.InstalledAppDetails
com.android.settings.applications.AppInfoDashboardFragment
com.android.settings.accessibility.ToggleAccessibilityServicePreferenceFragment
com.android.settings.print.PrintServiceSettingsFragment
com.android.settings.deviceinfo.PrivateVolumeSettings

View File

@@ -0,0 +1,459 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.applications;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.AlertDialog;
import android.app.AppOpsManager;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.UserManager;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.preference.PreferenceScreen;
import android.view.View;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.TestConfig;
import com.android.settings.applications.instantapps.InstantAppButtonsController;
import com.android.settings.applications.instantapps.InstantAppButtonsController.ShowDialogDelegate;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.widget.ActionButtonPreferenceTest;
import com.android.settings.wrapper.DevicePolicyManagerWrapper;
import com.android.settingslib.Utils;
import com.android.settingslib.applications.AppUtils;
import com.android.settingslib.applications.ApplicationsState.AppEntry;
import com.android.settingslib.applications.instantapps.InstantAppDataProvider;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.util.ReflectionHelpers;
import java.util.HashSet;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(
manifest = TestConfig.MANIFEST_PATH,
sdk = TestConfig.SDK_VERSION_O,
shadows = AppInfoDashboardFragmentTest.ShadowUtils.class
)
public final class AppInfoDashboardFragmentTest {
private static final String PACKAGE_NAME = "test_package_name";
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private UserManager mUserManager;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private SettingsActivity mActivity;
@Mock
private DevicePolicyManagerWrapper mDevicePolicyManager;
@Mock
private PackageManager mPackageManager;
@Mock
private AppOpsManager mAppOpsManager;
private FakeFeatureFactory mFeatureFactory;
private AppInfoDashboardFragment mAppDetail;
private Context mShadowContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mFeatureFactory = FakeFeatureFactory.setupForTest(mContext);
mShadowContext = RuntimeEnvironment.application;
mAppDetail = spy(new AppInfoDashboardFragment());
doReturn(mActivity).when(mAppDetail).getActivity();
doReturn(mShadowContext).when(mAppDetail).getContext();
doReturn(mPackageManager).when(mActivity).getPackageManager();
doReturn(mAppOpsManager).when(mActivity).getSystemService(Context.APP_OPS_SERVICE);
mAppDetail.mActionButtons = ActionButtonPreferenceTest.createMock();
// Default to not considering any apps to be instant (individual tests can override this).
ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
(InstantAppDataProvider) (i -> false));
}
@Test
public void shouldShowUninstallForAll_installForOneOtherUserOnly_shouldReturnTrue() {
when(mDevicePolicyManager.packageHasActiveAdmins(nullable(String.class))).thenReturn(false);
when(mUserManager.getUsers().size()).thenReturn(2);
ReflectionHelpers.setField(mAppDetail, "mDpm", mDevicePolicyManager);
ReflectionHelpers.setField(mAppDetail, "mUserManager", mUserManager);
final ApplicationInfo info = new ApplicationInfo();
info.enabled = true;
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = info;
final PackageInfo packageInfo = mock(PackageInfo.class);
ReflectionHelpers.setField(mAppDetail, "mPackageInfo", packageInfo);
assertThat(mAppDetail.shouldShowUninstallForAll(appEntry)).isTrue();
}
@Test
public void shouldShowUninstallForAll_installForSelfOnly_shouldReturnFalse() {
when(mDevicePolicyManager.packageHasActiveAdmins(nullable(String.class))).thenReturn(false);
when(mUserManager.getUsers().size()).thenReturn(2);
ReflectionHelpers.setField(mAppDetail, "mDpm", mDevicePolicyManager);
ReflectionHelpers.setField(mAppDetail, "mUserManager", mUserManager);
final ApplicationInfo info = new ApplicationInfo();
info.flags = ApplicationInfo.FLAG_INSTALLED;
info.enabled = true;
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = info;
final PackageInfo packageInfo = mock(PackageInfo.class);
ReflectionHelpers.setField(mAppDetail, "mPackageInfo", packageInfo);
assertThat(mAppDetail.shouldShowUninstallForAll(appEntry)).isFalse();
}
@Test
public void launchFragment_hasNoPackageInfo_shouldFinish() {
ReflectionHelpers.setField(mAppDetail, "mPackageInfo", null);
assertThat(mAppDetail.ensurePackageInfoAvailable(mActivity)).isFalse();
verify(mActivity).finishAndRemoveTask();
}
@Test
public void launchFragment_hasPackageInfo_shouldReturnTrue() {
final PackageInfo packageInfo = mock(PackageInfo.class);
ReflectionHelpers.setField(mAppDetail, "mPackageInfo", packageInfo);
assertThat(mAppDetail.ensurePackageInfoAvailable(mActivity)).isTrue();
verify(mActivity, never()).finishAndRemoveTask();
}
@Test
public void packageSizeChange_isOtherPackage_shouldNotRefreshUi() {
ReflectionHelpers.setField(mAppDetail, "mPackageName", PACKAGE_NAME);
mAppDetail.onPackageSizeChanged("Not_" + PACKAGE_NAME);
verify(mAppDetail, never()).refreshUi();
}
@Test
public void packageSizeChange_isOwnPackage_shouldRefreshUi() {
doReturn(Boolean.TRUE).when(mAppDetail).refreshUi();
ReflectionHelpers.setField(mAppDetail, "mPackageName", PACKAGE_NAME);
mAppDetail.onPackageSizeChanged(PACKAGE_NAME);
verify(mAppDetail).refreshUi();
}
// Tests that we don't show the "uninstall for all users" button for instant apps.
@Test
public void instantApps_noUninstallForAllButton() {
// Make this app appear to be instant.
ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
(InstantAppDataProvider) (i -> true));
when(mDevicePolicyManager.packageHasActiveAdmins(nullable(String.class))).thenReturn(false);
when(mUserManager.getUsers().size()).thenReturn(2);
final ApplicationInfo info = new ApplicationInfo();
info.enabled = true;
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = info;
final PackageInfo packageInfo = mock(PackageInfo.class);
ReflectionHelpers.setField(mAppDetail, "mDpm", mDevicePolicyManager);
ReflectionHelpers.setField(mAppDetail, "mUserManager", mUserManager);
ReflectionHelpers.setField(mAppDetail, "mPackageInfo", packageInfo);
assertThat(mAppDetail.shouldShowUninstallForAll(appEntry)).isFalse();
}
// Tests that we don't show the uninstall button for instant apps"
@Test
public void instantApps_noUninstallButton() {
// Make this app appear to be instant.
ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
(InstantAppDataProvider) (i -> true));
final ApplicationInfo info = new ApplicationInfo();
info.flags = ApplicationInfo.FLAG_INSTALLED;
info.enabled = true;
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = info;
final PackageInfo packageInfo = mock(PackageInfo.class);
packageInfo.applicationInfo = info;
ReflectionHelpers.setField(mAppDetail, "mUserManager", mUserManager);
ReflectionHelpers.setField(mAppDetail, "mAppEntry", appEntry);
ReflectionHelpers.setField(mAppDetail, "mPackageInfo", packageInfo);
mAppDetail.initUninstallButtonForUserApp();
verify(mAppDetail.mActionButtons).setButton1Visible(false);
}
// Tests that we don't show the force stop button for instant apps (they aren't allowed to run
// when they aren't in the foreground).
@Test
public void instantApps_noForceStop() {
// Make this app appear to be instant.
ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
(InstantAppDataProvider) (i -> true));
final PackageInfo packageInfo = mock(PackageInfo.class);
final AppEntry appEntry = mock(AppEntry.class);
final ApplicationInfo info = new ApplicationInfo();
appEntry.info = info;
ReflectionHelpers.setField(mAppDetail, "mDpm", mDevicePolicyManager);
ReflectionHelpers.setField(mAppDetail, "mPackageInfo", packageInfo);
ReflectionHelpers.setField(mAppDetail, "mAppEntry", appEntry);
mAppDetail.checkForceStop();
verify(mAppDetail.mActionButtons).setButton2Visible(false);
}
@Test
public void instantApps_buttonControllerHandlesDialog() {
InstantAppButtonsController mockController = mock(InstantAppButtonsController.class);
ReflectionHelpers.setField(
mAppDetail, "mInstantAppButtonsController", mockController);
// Make sure first that button controller is not called for supported dialog id
AlertDialog mockDialog = mock(AlertDialog.class);
when(mockController.createDialog(InstantAppButtonsController.DLG_CLEAR_APP))
.thenReturn(mockDialog);
assertThat(mAppDetail.createDialog(InstantAppButtonsController.DLG_CLEAR_APP, 0))
.isEqualTo(mockDialog);
verify(mockController).createDialog(InstantAppButtonsController.DLG_CLEAR_APP);
}
// A helper class for testing the InstantAppButtonsController - it lets us look up the
// preference associated with a key for instant app buttons and get back a mock
// LayoutPreference (to avoid a null pointer exception).
public static class InstalledAppDetailsWithMockInstantButtons extends InstalledAppDetails {
@Mock
private LayoutPreference mInstantButtons;
public InstalledAppDetailsWithMockInstantButtons() {
super();
MockitoAnnotations.initMocks(this);
}
@Override
public Preference findPreference(CharSequence key) {
if (key == "instant_app_buttons") {
return mInstantButtons;
}
return super.findPreference(key);
}
}
@Test
public void instantApps_instantSpecificButtons() {
// Make this app appear to be instant.
ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
(InstantAppDataProvider) (i -> true));
final PackageInfo packageInfo = mock(PackageInfo.class);
final InstalledAppDetailsWithMockInstantButtons
fragment = new InstalledAppDetailsWithMockInstantButtons();
ReflectionHelpers.setField(fragment, "mPackageInfo", packageInfo);
ReflectionHelpers.setField(fragment, "mApplicationFeatureProvider",
mFeatureFactory.applicationFeatureProvider);
final InstantAppButtonsController buttonsController =
mock(InstantAppButtonsController.class);
when(buttonsController.setPackageName(nullable(String.class)))
.thenReturn(buttonsController);
when(mFeatureFactory.applicationFeatureProvider.newInstantAppButtonsController(
nullable(Fragment.class), nullable(View.class), nullable(ShowDialogDelegate.class)))
.thenReturn(buttonsController);
fragment.maybeAddInstantAppButtons();
verify(buttonsController).setPackageName(nullable(String.class));
verify(buttonsController).show();
}
@Test
public void instantApps_removeCorrectPref() {
PreferenceScreen mockPreferenceScreen = mock(PreferenceScreen.class);
PreferenceManager mockPreferenceManager = mock(PreferenceManager.class);
AppDomainsPreference mockAppDomainsPref = mock(AppDomainsPreference.class);
PackageInfo mockPackageInfo = mock(PackageInfo.class);
PackageManager mockPackageManager = mock(PackageManager.class);
ReflectionHelpers.setField(
mAppDetail, "mInstantAppDomainsPreference", mockAppDomainsPref);
ReflectionHelpers.setField(
mAppDetail, "mPreferenceManager", mockPreferenceManager);
ReflectionHelpers.setField(
mAppDetail, "mPackageInfo", mockPackageInfo);
ReflectionHelpers.setField(
mAppDetail, "mPm", mockPackageManager);
when(mockPreferenceManager.getPreferenceScreen()).thenReturn(mockPreferenceScreen);
ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
(InstantAppDataProvider) (i -> false));
mAppDetail.prepareInstantAppPrefs();
// For the non instant case we remove the app domain pref, and leave the launch pref
verify(mockPreferenceScreen).removePreference(mockAppDomainsPref);
// For the instant app case we remove the launch preff, and leave the app domain pref
ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
(InstantAppDataProvider) (i -> true));
mAppDetail.prepareInstantAppPrefs();
// Will be 1 still due to above call
verify(mockPreferenceScreen, times(1))
.removePreference(mockAppDomainsPref);
}
@Test
public void onActivityResult_uninstalledUpdates_shouldInvalidateOptionsMenu() {
doReturn(true).when(mAppDetail).refreshUi();
mAppDetail.onActivityResult(InstalledAppDetails.REQUEST_UNINSTALL, 0, mock(Intent.class));
verify(mActivity).invalidateOptionsMenu();
}
@Test
public void handleDisableable_appIsHomeApp_buttonShouldNotWork() {
final ApplicationInfo info = new ApplicationInfo();
info.packageName = "pkg";
info.enabled = true;
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = info;
final HashSet<String> homePackages = new HashSet<>();
homePackages.add(info.packageName);
ReflectionHelpers.setField(mAppDetail, "mHomePackages", homePackages);
ReflectionHelpers.setField(mAppDetail, "mAppEntry", appEntry);
assertThat(mAppDetail.handleDisableable()).isFalse();
verify(mAppDetail.mActionButtons).setButton1Text(R.string.disable_text);
}
@Test
public void handleDisableable_appIsEnabled_buttonShouldWork() {
final ApplicationInfo info = new ApplicationInfo();
info.packageName = "pkg";
info.enabled = true;
info.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = info;
when(mFeatureFactory.applicationFeatureProvider.getKeepEnabledPackages()).thenReturn(
new HashSet<>());
ReflectionHelpers.setField(mAppDetail, "mApplicationFeatureProvider",
mFeatureFactory.applicationFeatureProvider);
ReflectionHelpers.setField(mAppDetail, "mAppEntry", appEntry);
assertThat(mAppDetail.handleDisableable()).isTrue();
verify(mAppDetail.mActionButtons).setButton1Text(R.string.disable_text);
}
@Test
@Config(shadows = ShadowUtils.class)
public void handleDisableable_appIsDisabled_buttonShouldShowEnable() {
final ApplicationInfo info = new ApplicationInfo();
info.packageName = "pkg";
info.enabled = false;
info.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = info;
when(mFeatureFactory.applicationFeatureProvider.getKeepEnabledPackages()).thenReturn(
new HashSet<>());
ReflectionHelpers.setField(mAppDetail, "mApplicationFeatureProvider",
mFeatureFactory.applicationFeatureProvider);
ReflectionHelpers.setField(mAppDetail, "mAppEntry", appEntry);
assertThat(mAppDetail.handleDisableable()).isTrue();
verify(mAppDetail.mActionButtons).setButton1Text(R.string.enable_text);
verify(mAppDetail.mActionButtons).setButton1Positive(true);
}
@Test
public void handleDisableable_appIsEnabledAndInKeepEnabledWhitelist_buttonShouldNotWork() {
final ApplicationInfo info = new ApplicationInfo();
info.packageName = "pkg";
info.enabled = true;
info.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = info;
final HashSet<String> packages = new HashSet<>();
packages.add(info.packageName);
when(mFeatureFactory.applicationFeatureProvider.getKeepEnabledPackages()).thenReturn(
packages);
ReflectionHelpers.setField(mAppDetail, "mApplicationFeatureProvider",
mFeatureFactory.applicationFeatureProvider);
ReflectionHelpers.setField(mAppDetail, "mAppEntry", appEntry);
assertThat(mAppDetail.handleDisableable()).isFalse();
verify(mAppDetail.mActionButtons).setButton1Text(R.string.disable_text);
}
@Test
public void initUninstallButtonForUserApp_shouldSetNegativeButton() {
final ApplicationInfo info = new ApplicationInfo();
info.flags = ApplicationInfo.FLAG_INSTALLED;
info.enabled = true;
final PackageInfo packageInfo = mock(PackageInfo.class);
packageInfo.applicationInfo = info;
ReflectionHelpers.setField(mAppDetail, "mUserManager", mUserManager);
ReflectionHelpers.setField(mAppDetail, "mPackageInfo", packageInfo);
mAppDetail.initUninstallButtonForUserApp();
verify(mAppDetail.mActionButtons).setButton1Positive(false);
}
@Implements(Utils.class)
public static class ShadowUtils {
@Implementation
public static boolean isSystemPackage(Resources resources, PackageManager pm,
PackageInfo pkg) {
return false;
}
}
}

View File

@@ -0,0 +1,198 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.applications.appinfo;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyDouble;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.AppOpsManager;
import android.app.LoaderManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.BatteryStats;
import android.os.Bundle;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.internal.os.BatterySipper;
import com.android.internal.os.BatteryStatsHelper;
import com.android.settings.SettingsActivity;
import com.android.settings.TestConfig;
import com.android.settings.applications.AppInfoDashboardFragment;
import com.android.settings.fuelgauge.BatteryUtils;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.ArrayList;
import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class AppBatteryPreferenceControllerTest {
private static final int TARGET_UID = 111;
private static final int OTHER_UID = 222;
private static final double BATTERY_LEVEL = 60;
@Mock
private SettingsActivity mActivity;
@Mock
private BatteryUtils mBatteryUtils;
@Mock
private BatterySipper mBatterySipper;
@Mock
private BatterySipper mOtherBatterySipper;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private BatteryStatsHelper mBatteryStatsHelper;
@Mock
private BatteryStats.Uid mUid;
@Mock
private PreferenceScreen mScreen;
@Mock
private PackageManager mPackageManager;
@Mock
private LoaderManager mLoaderManager;
private Context mContext;
private AppInfoDashboardFragment mFragment;
private AppBatteryPreferenceController mController;
private Preference mBatteryPreference;
@Before
public void setUp() throws NameNotFoundException {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mContext.getPackageManager()).thenReturn(mPackageManager);
mFragment = spy(new AppInfoDashboardFragment());
mBatteryPreference = spy(new Preference(mContext));
mBatterySipper.drainType = BatterySipper.DrainType.IDLE;
mBatterySipper.uidObj = mUid;
doReturn(TARGET_UID).when(mBatterySipper).getUid();
doReturn(OTHER_UID).when(mOtherBatterySipper).getUid();
mController = spy(new AppBatteryPreferenceController(
mContext, mFragment, "package1", null /* lifecycle */));
mController.mBatteryUtils = mBatteryUtils;
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mBatteryPreference);
}
@Test
public void getAvailabilityStatus_shouldAlwaysReturnAvailable() {
assertThat(mController.getAvailabilityStatus()).isEqualTo(mController.AVAILABLE);
}
@Test
public void findTargetSipper_findCorrectSipper() {
final List<BatterySipper> usageList = new ArrayList<>();
usageList.add(mBatterySipper);
usageList.add(mOtherBatterySipper);
doReturn(usageList).when(mBatteryStatsHelper).getUsageList();
assertThat(mController.findTargetSipper(mBatteryStatsHelper, TARGET_UID)).isEqualTo(
mBatterySipper);
}
@Test
public void updateBattery_noBatteryStats_summaryNo() {
mController.displayPreference(mScreen);
mController.updateBattery();
assertThat(mBatteryPreference.getSummary()).isEqualTo(
"No battery use since last full charge");
}
@Test
public void updateBattery_hasBatteryStats_summaryPercent() {
mController.mBatteryHelper = mBatteryStatsHelper;
mController.mSipper = mBatterySipper;
doReturn(BATTERY_LEVEL).when(mBatteryUtils).calculateBatteryPercent(anyDouble(),
anyDouble(), anyDouble(), anyInt());
doReturn(new ArrayList<>()).when(mBatteryStatsHelper).getUsageList();
mController.displayPreference(mScreen);
mController.updateBattery();
assertThat(mBatteryPreference.getSummary()).isEqualTo("60% use since last full charge");
}
@Test
public void isBatteryStatsAvailable_hasBatteryStatsHelperAndSipper_returnTrue() {
mController.mBatteryHelper = mBatteryStatsHelper;
mController.mSipper = mBatterySipper;
assertThat(mController.isBatteryStatsAvailable()).isTrue();
}
@Test
public void isBatteryStatsAvailable_parametersNull_returnFalse() {
assertThat(mController.isBatteryStatsAvailable()).isFalse();
}
@Test
public void launchPowerUsageDetailFragment_shouldNotCrash() {
when(mActivity.getSystemService(Context.APP_OPS_SERVICE))
.thenReturn(mock(AppOpsManager.class));
when(mFragment.getActivity()).thenReturn(mActivity);
final String key = mController.getPreferenceKey();
when(mBatteryPreference.getKey()).thenReturn(key);
mController.mSipper = mBatterySipper;
mController.mBatteryHelper = mBatteryStatsHelper;
// Should not crash
mController.handlePreferenceTreeClick(mBatteryPreference);
}
@Test
public void onResume_shouldRestartBatteryStatsLoader() {
doReturn(mLoaderManager).when(mFragment).getLoaderManager();
mController.onResume();
verify(mLoaderManager).restartLoader(AppInfoDashboardFragment.LOADER_BATTERY, Bundle.EMPTY,
mController);
}
@Test
public void onPause_shouldDestroyBatteryStatsLoader() {
doReturn(mLoaderManager).when(mFragment).getLoaderManager();
mController.onPause();
verify(mLoaderManager).destroyLoader(AppInfoDashboardFragment.LOADER_BATTERY);
}
}

View File

@@ -0,0 +1,140 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.applications.appinfo;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.LoaderManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.net.ConnectivityManager;
import android.net.INetworkStatsSession;
import android.os.Bundle;
import android.support.v7.preference.Preference;
import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.applications.AppInfoDashboardFragment;
import com.android.settings.datausage.AppDataUsage;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.applications.ApplicationsState.AppEntry;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class AppDataUsagePreferenceControllerTest {
@Mock
private LoaderManager mLoaderManager;
@Mock
private AppInfoDashboardFragment mFragment;
private Context mContext;
private AppDataUsagePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application.getApplicationContext());
mController = spy(
new AppDataUsagePreferenceController(mContext, mFragment, null /* lifecycle */));
}
@Test
public void getAvailabilityStatus_bandwidthControlEnabled_shouldReturnAvailable() {
doReturn(true).when(mController).isBandwidthControlEnabled();
assertThat(mController.getAvailabilityStatus()).isEqualTo(mController.AVAILABLE);
}
@Test
public void getAvailabilityStatus_bandwidthControlDisabled_shouldReturnDisabled() {
doReturn(false).when(mController).isBandwidthControlEnabled();
assertThat(mController.getAvailabilityStatus()).isEqualTo(mController.DISABLED_UNSUPPORTED);
}
@Test
public void onResume_noSession_shouldNotRestartDataLoader() {
doReturn(mLoaderManager).when(mFragment).getLoaderManager();
mController.onResume();
verify(mLoaderManager, never()).restartLoader(
AppInfoDashboardFragment.LOADER_CHART_DATA, Bundle.EMPTY, mController);
}
@Test
public void onResume_hasSession_shouldRestartDataLoader() {
final ConnectivityManager connectivityManager = mock(ConnectivityManager.class);
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
.thenReturn(connectivityManager);
when(connectivityManager.isNetworkSupported(anyInt())).thenReturn(true);
doReturn(mLoaderManager).when(mFragment).getLoaderManager();
ReflectionHelpers.setField(mController, "mStatsSession", mock(INetworkStatsSession.class));
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = new ApplicationInfo();
when(mFragment.getAppEntry()).thenReturn(appEntry);
mController.onResume();
verify(mLoaderManager).restartLoader(
eq(AppInfoDashboardFragment.LOADER_CHART_DATA), any(Bundle.class), eq(mController));
}
@Test
public void onPause_shouldDestroyDataLoader() {
doReturn(mLoaderManager).when(mFragment).getLoaderManager();
mController.onPause();
verify(mLoaderManager).destroyLoader(AppInfoDashboardFragment.LOADER_CHART_DATA);
}
@Test
public void getDetailFragmentClass_shouldReturnAppDataUsage() {
assertThat(mController.getDetailFragmentClass()).isEqualTo(AppDataUsage.class);
}
@Test
public void updateState_shouldUpdatePreferenceSummary() {
final Preference preference = mock(Preference.class);
mController.updateState(preference);
verify(preference).setSummary(any());
}
}

View File

@@ -0,0 +1,120 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.applications.appinfo;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.pm.ApplicationInfo;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.TestConfig;
import com.android.settings.applications.AppInfoDashboardFragment;
import com.android.settings.notification.AppNotificationSettings;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.applications.ApplicationsState;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class AppInfoPreferenceControllerBaseTest {
@Mock
private SettingsActivity mActivity;
@Mock
private AppInfoDashboardFragment mFragment;
@Mock
private PreferenceScreen mScreen;
@Mock
private Preference mPreference;
private TestPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mController = new TestPreferenceController(mFragment);
final String key = mController.getPreferenceKey();
when(mScreen.findPreference(key)).thenReturn(mPreference);
when(mPreference.getKey()).thenReturn(key);
when(mFragment.getActivity()).thenReturn(mActivity);
}
@Test
public void getAvailabilityStatus_shouldReturnAvailable() {
assertThat(mController.getAvailabilityStatus()).isEqualTo(mController.AVAILABLE);
}
@Test
public void refreshUi_shouldUpdatePreference() {
mController.displayPreference(mScreen);
mController.refreshUi();
assertThat(mController.preferenceUpdated).isTrue();
}
@Test
public void handlePreferenceTreeClick_shouldStartDetailFragmentClass() {
final ApplicationsState.AppEntry appEntry = mock(ApplicationsState.AppEntry.class);
appEntry.info = new ApplicationInfo();
when(mFragment.getAppEntry()).thenReturn(appEntry);
mController.handlePreferenceTreeClick(mPreference);
verify(mActivity).startPreferencePanel(any(),
eq(mController.getDetailFragmentClass().getName()), any(), anyInt(), any(), any(),
anyInt());
}
private class TestPreferenceController extends AppInfoPreferenceControllerBase {
private boolean preferenceUpdated;
public TestPreferenceController(AppInfoDashboardFragment parent) {
super(RuntimeEnvironment.application, parent, "TestKey");
}
@Override
public void updateState(Preference preference) {
preferenceUpdated = true;
}
@Override
public Class<? extends SettingsPreferenceFragment> getDetailFragmentClass() {
return AppNotificationSettings.class;
}
}
}

View File

@@ -0,0 +1,108 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.applications.appinfo;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.provider.Settings;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.TestConfig;
import com.android.settings.applications.AppInfoDashboardFragment;
import com.android.settings.applications.ProcStatsData;
import com.android.settings.applications.ProcessStatsDetail;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class AppMemoryPreferenceControllerTest {
@Mock
private SettingsActivity mActivity;
@Mock
private AppInfoDashboardFragment mFragment;
@Mock
private PreferenceScreen mScreen;
@Mock
private Preference mPreference;
private Context mContext;
private AppMemoryPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController =
spy(new AppMemoryPreferenceController(mContext, mFragment, null /* lifecycle */));
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
final String key = mController.getPreferenceKey();
when(mPreference.getKey()).thenReturn(key);
when(mFragment.getActivity()).thenReturn(mActivity);
}
@Test
public void getAvailabilityStatus_developmentSettingsEnabled_shouldReturnAvailable() {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
assertThat(mController.getAvailabilityStatus()).isEqualTo(mController.AVAILABLE);
}
@Test
public void getAvailabilityStatus_developmentSettingsDisabled_shouldReturnDisabled() {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0);
assertThat(mController.getAvailabilityStatus())
.isEqualTo(mController.DISABLED_DEPENDENT_SETTING);
}
@Test
public void handlePreferenceTreeClick_shouldStartProcessStatsDetail() {
final ProcStatsData data = mock(ProcStatsData.class);
when(data.getMemInfo()).thenReturn(mock(ProcStatsData.MemInfo.class));
ReflectionHelpers.setField(mController, "mStatsManager", data);
mController.handlePreferenceTreeClick(mPreference);
verify(mActivity).startPreferencePanel(any(), eq(ProcessStatsDetail.class.getName()), any(),
eq(R.string.memory_usage), any(), any(), anyInt());
}
}

View File

@@ -0,0 +1,92 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.applications.appinfo;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.applications.AppInfoDashboardFragment;
import com.android.settings.notification.AppNotificationSettings;
import com.android.settings.notification.NotificationBackend;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.applications.ApplicationsState;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class AppNotificationPreferenceControllerTest {
@Mock
private AppInfoDashboardFragment mFragment;
@Mock
private PreferenceScreen mScreen;
@Mock
private Preference mPreference;
private Context mContext;
private AppNotificationPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController =
spy(new AppNotificationPreferenceController(mContext, mFragment));
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
final String key = mController.getPreferenceKey();
when(mPreference.getKey()).thenReturn(key);
}
@Test
public void getDetailFragmentClass_shouldReturnAppNotificationSettings() {
assertThat(mController.getDetailFragmentClass()).isEqualTo(AppNotificationSettings.class);
}
@Test
public void updateState_shouldSetSummary() {
final ApplicationsState.AppEntry appEntry = mock(ApplicationsState.AppEntry.class);
appEntry.info = new ApplicationInfo();
when(mFragment.getAppEntry()).thenReturn(appEntry);
ReflectionHelpers.setField(mController, "mBackend", new NotificationBackend());
mController.displayPreference(mScreen);
mController.updateState(mPreference);
verify(mPreference).setSummary(any());
}
}

View File

@@ -0,0 +1,166 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.applications.appinfo;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.applications.AppInfoDashboardFragment;
import com.android.settings.applications.AppLaunchSettings;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.applications.AppUtils;
import com.android.settingslib.applications.ApplicationsState.AppEntry;
import com.android.settingslib.applications.instantapps.InstantAppDataProvider;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class AppOpenByDefaultPreferenceControllerTest {
@Mock
private AppInfoDashboardFragment mFragment;
@Mock
private PreferenceScreen mScreen;
@Mock
private Preference mPreference;
private Context mContext;
private AppOpenByDefaultPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application.getApplicationContext();
mController = spy(new AppOpenByDefaultPreferenceController(mContext, mFragment));
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
}
@Test
public void getDetailFragmentClass_shouldReturnAppLaunchSettings() {
assertThat(mController.getDetailFragmentClass()).isEqualTo(AppLaunchSettings.class);
}
@Test
public void displayPreference_noAppEntry_shouldDisablePreference() {
mController.displayPreference(mScreen);
verify(mPreference).setEnabled(false);
}
@Test
public void displayPreference_noAppInfo_shouldDisablePreference() {
final AppEntry appEntry = mock(AppEntry.class);
when(mFragment.getAppEntry()).thenReturn(appEntry);
mController.displayPreference(mScreen);
verify(mPreference).setEnabled(false);
}
@Test
public void displayPreference_appNotInstalled_shouldDisablePreference() {
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = new ApplicationInfo();
when(mFragment.getAppEntry()).thenReturn(appEntry);
mController.displayPreference(mScreen);
verify(mPreference).setEnabled(false);
}
@Test
public void displayPreference_appDisabled_shouldDisablePreference() {
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = new ApplicationInfo();
appEntry.info.flags &= ApplicationInfo.FLAG_INSTALLED;
appEntry.info.enabled = false;
when(mFragment.getAppEntry()).thenReturn(appEntry);
mController.displayPreference(mScreen);
verify(mPreference).setEnabled(false);
}
@Test
public void displayPreference_appEnabled_shouldNotDisablePreference() {
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = new ApplicationInfo();
appEntry.info.flags |= ApplicationInfo.FLAG_INSTALLED;
appEntry.info.enabled = true;
when(mFragment.getAppEntry()).thenReturn(appEntry);
mController.displayPreference(mScreen);
verify(mPreference, never()).setEnabled(false);
}
@Test
public void updateState_noPackageInfo_shouldNotShowPreference() {
mController.updateState(mPreference);
verify(mPreference).setVisible(false);
}
@Test
public void updateState_isInstantApp_shouldNotShowPreference() {
when(mFragment.getPackageInfo()).thenReturn(new PackageInfo());
ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
(InstantAppDataProvider) (i -> true));
mController.updateState(mPreference);
verify(mPreference).setVisible(false);
}
@Test
public void updateState_notInstantApp_shouldShowPreferenceAndSetSummary() {
when(mFragment.getPackageInfo()).thenReturn(new PackageInfo());
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = new ApplicationInfo();
when(mFragment.getAppEntry()).thenReturn(appEntry);
ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
(InstantAppDataProvider) (i -> false));
mController.updateState(mPreference);
verify(mPreference).setVisible(true);
verify(mPreference).setSummary(any());
}
}

View File

@@ -0,0 +1,145 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.applications.appinfo;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.TestConfig;
import com.android.settings.applications.AppInfoDashboardFragment;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.applications.ApplicationsState;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.ArrayList;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class AppPermissionPreferenceControllerTest {
@Mock
private SettingsActivity mActivity;
@Mock
private AppInfoDashboardFragment mFragment;
@Mock
private PreferenceScreen mScreen;
@Mock
private Preference mPreference;
private Context mContext;
private AppPermissionPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController = new AppPermissionPreferenceController(mContext, mFragment, "Package1");
when(mScreen.findPreference(any())).thenReturn(mPreference);
final String key = mController.getPreferenceKey();
when(mPreference.getKey()).thenReturn(key);
when(mFragment.getActivity()).thenReturn(mActivity);
}
@Test
public void getAvailabilityStatus_isAlwaysAvailable() {
assertThat(mController.getAvailabilityStatus()).isEqualTo(mController.AVAILABLE);
}
@Test
public void onPermissionSummaryResult_noRequestedPermission_shouldDisablePreference() {
mController.displayPreference(mScreen);
mController.mPermissionCallback.onPermissionSummaryResult(
1, 0, 1, new ArrayList<CharSequence>());
verify(mPreference).setEnabled(false);
verify(mPreference).setSummary(mContext.getString(
R.string.runtime_permissions_summary_no_permissions_requested));
}
@Test
public void onPermissionSummaryResult_noGrantedPermission_shouldSetNoPermissionSummary() {
mController.displayPreference(mScreen);
mController.mPermissionCallback.onPermissionSummaryResult(
1, 5, 0, new ArrayList<CharSequence>());
verify(mPreference).setEnabled(true);
verify(mPreference).setSummary(mContext.getString(
R.string.runtime_permissions_summary_no_permissions_granted));
}
@Test
public void onPermissionSummaryResult_hasRuntimePermission_shouldSetPermissionAsSummary() {
mController.displayPreference(mScreen);
final String permission = "Storage";
final ArrayList<CharSequence> labels = new ArrayList<>();
labels.add(permission);
mController.mPermissionCallback.onPermissionSummaryResult(1, 5, 0, labels);
verify(mPreference).setEnabled(true);
verify(mPreference).setSummary(permission);
}
@Test
public void onPermissionSummaryResult_hasAdditionalPermission_shouldSetAdditionalSummary() {
mController.displayPreference(mScreen);
final String permission = "Storage";
final ArrayList<CharSequence> labels = new ArrayList<>();
labels.add(permission);
mController.mPermissionCallback.onPermissionSummaryResult(1, 5, 2, labels);
verify(mPreference).setEnabled(true);
verify(mPreference).setSummary("Storage and 2 additional permissions");
}
@Test
public void handlePreferenceTreeClick_shouldStartManagePermissionsActivity() {
final ApplicationsState.AppEntry appEntry = mock(ApplicationsState.AppEntry.class);
appEntry.info = new ApplicationInfo();
when(mFragment.getAppEntry()).thenReturn(appEntry);
mController.handlePreferenceTreeClick(mPreference);
verify(mActivity).startActivityForResult(argThat(intent-> intent != null &&
Intent.ACTION_MANAGE_APP_PERMISSIONS.equals(intent.getAction())), anyInt());
}
}

View File

@@ -0,0 +1,126 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.applications.appinfo;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.LoaderManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.support.v7.preference.Preference;
import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.applications.AppInfoDashboardFragment;
import com.android.settings.applications.AppStorageSettings;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.applications.ApplicationsState.AppEntry;
import com.android.settingslib.applications.StorageStatsSource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class AppStoragePreferenceControllerTest {
@Mock
private LoaderManager mLoaderManager;
@Mock
private AppInfoDashboardFragment mFragment;
private Context mContext;
private AppStoragePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application.getApplicationContext();
mController =
spy(new AppStoragePreferenceController(mContext, mFragment, null /* lifecycle */));
}
@Test
public void onResume_shouldRestartStorageLoader() {
doReturn(mLoaderManager).when(mFragment).getLoaderManager();
mController.onResume();
verify(mLoaderManager).restartLoader(AppInfoDashboardFragment.LOADER_STORAGE, Bundle.EMPTY,
mController);
}
@Test
public void onPause_shouldDestroyStorageLoader() {
doReturn(mLoaderManager).when(mFragment).getLoaderManager();
mController.onPause();
verify(mLoaderManager).destroyLoader(AppInfoDashboardFragment.LOADER_STORAGE);
}
@Test
public void getDetailFragmentClass_shouldReturnAppStorageSettings() {
assertThat(mController.getDetailFragmentClass()).isEqualTo(AppStorageSettings.class);
}
@Test
public void updateState_shouldUpdatePreferenceSummary() {
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = new ApplicationInfo();
when(mFragment.getAppEntry()).thenReturn(appEntry);
Preference preference = mock(Preference.class);
mController.updateState(preference);
verify(preference).setSummary(any());
}
@Test
public void getStorageSummary_shouldWorkForExternal() {
final StorageStatsSource.AppStorageStats stats =
mock(StorageStatsSource.AppStorageStats.class);
when(stats.getTotalBytes()).thenReturn(1L);
assertThat(mController.getStorageSummary(stats, true))
.isEqualTo("1 B used in external storage");
}
@Test
public void getStorageSummary_shouldWorkForInternal() {
final StorageStatsSource.AppStorageStats stats =
mock(StorageStatsSource.AppStorageStats.class);
when(stats.getTotalBytes()).thenReturn(1L);
assertThat(mController.getStorageSummary(stats, false))
.isEqualTo("1 B used in internal storage");
}
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.applications.appinfo;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.support.v7.preference.Preference;
import com.android.settings.TestConfig;
import com.android.settings.applications.AppInfoDashboardFragment;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class AppVersionPreferenceControllerTest {
@Mock
private AppInfoDashboardFragment mFragment;
@Mock
private Preference mPreference;
private Context mContext;
private AppVersionPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController = new AppVersionPreferenceController(mContext, mFragment);
}
@Test
public void updateState_shouldUpdatePreferenceSummary() {
final PackageInfo packageInfo = mock(PackageInfo.class);
packageInfo.versionName = "test1234";
when(mFragment.getPackageInfo()).thenReturn(packageInfo);
mController.updateState(mPreference);
verify(mPreference).setSummary("version test1234");
}
}