Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a793857b authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes Ia73a89d1,I78ee508d,I4aa035a4,I926eaa5f,I9389254b, ...

* changes:
  Update cachedStorageValueHelperTest to sdk 26
  Move AutoStorageManagementSwitchPrefCtrl to sdk 26
  Update SecondaryUserControllerTest to sdk 26
  Update StorageItemPrefCtrlTest to sdk 26
  Update StorageSummaryDonutPrefCtrl test to sdk 26
  Update UserProfileControllerTest to sdk 26
parents d786fc5b 1e9f1b0e
Loading
Loading
Loading
Loading
+21 −17
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.settings.deviceinfo.storage;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
@@ -34,6 +35,7 @@ import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;

import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.os.RoSystemProperties;
import com.android.settings.TestConfig;
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.deletionhelper.ActivationWarningFragment;
@@ -52,12 +54,13 @@ 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,
        sdk = TestConfig.SDK_VERSION_O,
        shadows = {SettingsShadowSystemProperties.class}
)
public class AutomaticStorageManagementSwitchPreferenceControllerTest {
@@ -79,13 +82,14 @@ public class AutomaticStorageManagementSwitchPreferenceControllerTest {
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mContext = RuntimeEnvironment.application.getApplicationContext();
        FeatureFactory factory = FeatureFactory.getFactory(mContext);
        final FeatureFactory factory = FeatureFactory.getFactory(mContext);
        mMetricsFeature = factory.getMetricsFeatureProvider();

        mController = new AutomaticStorageManagementSwitchPreferenceController(
                mContext, mMetricsFeature, mFragmentManager);
        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
    }

    @After
    public void tearDown() {
        SettingsShadowSystemProperties.clear();
@@ -98,15 +102,15 @@ public class AutomaticStorageManagementSwitchPreferenceControllerTest {

    @Test
    public void isAvailable_shouldAlwaysReturnFalse_forLowRamDevice() {
        SettingsShadowSystemProperties.set("ro.config.low_ram", "true");
        ReflectionHelpers.setStaticField(RoSystemProperties.class, "CONFIG_LOW_RAM", true);
        assertThat(mController.isAvailable()).isFalse();
        SettingsShadowSystemProperties.clear();
        ReflectionHelpers.setStaticField(RoSystemProperties.class, "CONFIG_LOW_RAM", false);
    }

    @Test
    public void onResume_shouldReflectEnabledStatus() {
        mController.displayPreference(mScreen);
        ContentResolver resolver = mContext.getContentResolver();
        final ContentResolver resolver = mContext.getContentResolver();
        Settings.Secure.putInt(resolver, Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 1);

        mController.onResume();
@@ -128,9 +132,9 @@ public class AutomaticStorageManagementSwitchPreferenceControllerTest {
        // FakeFeatureFactory uses mock contexts, so this test scaffolds itself rather than using
        // the instance variables.
        FakeFeatureFactory.setupForTest(mMockContext);
        FakeFeatureFactory factory =
        final FakeFeatureFactory factory =
                (FakeFeatureFactory) FakeFeatureFactory.getFactory(mMockContext);
        AutomaticStorageManagementSwitchPreferenceController controller =
        final AutomaticStorageManagementSwitchPreferenceController controller =
                new AutomaticStorageManagementSwitchPreferenceController(
                        mMockContext, factory.metricsFeatureProvider, mFragmentManager);

@@ -144,14 +148,14 @@ public class AutomaticStorageManagementSwitchPreferenceControllerTest {
    public void togglingShouldUpdateSettingsSecure() {
        mController.onSwitchToggled(true);

        ContentResolver resolver = mContext.getContentResolver();
        final ContentResolver resolver = mContext.getContentResolver();
        assertThat(Settings.Secure.getInt(
                resolver, Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 0)).isNotEqualTo(0);
    }

    @Test
    public void togglingOnShouldTriggerWarningFragment() {
        FragmentTransaction transaction = mock(FragmentTransaction.class);
        final FragmentTransaction transaction = mock(FragmentTransaction.class);
        when(mFragmentManager.beginTransaction()).thenReturn(transaction);
        SettingsShadowSystemProperties.set(
                AutomaticStorageManagementSwitchPreferenceController
@@ -164,7 +168,7 @@ public class AutomaticStorageManagementSwitchPreferenceControllerTest {

    @Test
    public void togglingOffShouldTriggerWarningFragment() {
        FragmentTransaction transaction = mock(FragmentTransaction.class);
        final FragmentTransaction transaction = mock(FragmentTransaction.class);
        when(mFragmentManager.beginTransaction()).thenReturn(transaction);

        mController.onSwitchToggled(false);
@@ -175,7 +179,7 @@ public class AutomaticStorageManagementSwitchPreferenceControllerTest {

    @Test
    public void togglingOnShouldNotTriggerWarningFragmentIfEnabledByDefault() {
        FragmentTransaction transaction = mock(FragmentTransaction.class);
        final FragmentTransaction transaction = mock(FragmentTransaction.class);
        when(mFragmentManager.beginTransaction()).thenReturn(transaction);
        SettingsShadowSystemProperties.set(
                AutomaticStorageManagementSwitchPreferenceController
@@ -188,7 +192,7 @@ public class AutomaticStorageManagementSwitchPreferenceControllerTest {

    @Test
    public void togglingOnShouldTriggerWarningFragmentIfEnabledByDefaultAndDisabledByPolicy() {
        FragmentTransaction transaction = mock(FragmentTransaction.class);
        final FragmentTransaction transaction = mock(FragmentTransaction.class);
        when(mFragmentManager.beginTransaction()).thenReturn(transaction);
        SettingsShadowSystemProperties.set(
                AutomaticStorageManagementSwitchPreferenceController
+9 −9
Original line number Diff line number Diff line
@@ -55,7 +55,7 @@ import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;

@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class CachedStorageValuesHelperTest {
    private Context mContext;

@@ -94,7 +94,7 @@ public class CachedStorageValuesHelperTest {
                .putLong(TIMESTAMP_KEY, 10000L)
                .apply();

        PrivateStorageInfo info = mCachedValuesHelper.getCachedPrivateStorageInfo();
        final PrivateStorageInfo info = mCachedValuesHelper.getCachedPrivateStorageInfo();

        assertThat(info.freeBytes).isEqualTo(1000L);
        assertThat(info.totalBytes).isEqualTo(6000L);
@@ -122,7 +122,7 @@ public class CachedStorageValuesHelperTest {
                .putLong(TIMESTAMP_KEY, 10000L)
                .apply();

        SparseArray<StorageAsyncLoader.AppsStorageResult> result =
        final SparseArray<StorageAsyncLoader.AppsStorageResult> result =
                mCachedValuesHelper.getCachedAppsStorageResult();

        StorageAsyncLoader.AppsStorageResult primaryResult = result.get(0);
@@ -161,7 +161,7 @@ public class CachedStorageValuesHelperTest {
                .putLong(TIMESTAMP_KEY, 10000L)
                .apply();

        PrivateStorageInfo info = mCachedValuesHelper.getCachedPrivateStorageInfo();
        final PrivateStorageInfo info = mCachedValuesHelper.getCachedPrivateStorageInfo();
        assertThat(info).isNull();
    }

@@ -187,7 +187,7 @@ public class CachedStorageValuesHelperTest {
                .putLong(TIMESTAMP_KEY, 10000L)
                .apply();

        SparseArray<StorageAsyncLoader.AppsStorageResult> result =
        final SparseArray<StorageAsyncLoader.AppsStorageResult> result =
                mCachedValuesHelper.getCachedAppsStorageResult();
        assertThat(result).isNull();
    }
@@ -214,7 +214,7 @@ public class CachedStorageValuesHelperTest {
                .putLong(TIMESTAMP_KEY, 10000L)
                .apply();

        PrivateStorageInfo info = mCachedValuesHelper.getCachedPrivateStorageInfo();
        final PrivateStorageInfo info = mCachedValuesHelper.getCachedPrivateStorageInfo();
        assertThat(info).isNull();
    }

@@ -240,20 +240,20 @@ public class CachedStorageValuesHelperTest {
                .putLong(TIMESTAMP_KEY, 10000L)
                .apply();

        SparseArray<StorageAsyncLoader.AppsStorageResult> result =
        final SparseArray<StorageAsyncLoader.AppsStorageResult> result =
                mCachedValuesHelper.getCachedAppsStorageResult();
        assertThat(result).isNull();
    }

    @Test
    public void getCachedPrivateStorageInfo_nullIfEmpty() throws Exception {
        PrivateStorageInfo info = mCachedValuesHelper.getCachedPrivateStorageInfo();
        final PrivateStorageInfo info = mCachedValuesHelper.getCachedPrivateStorageInfo();
        assertThat(info).isNull();
    }

    @Test
    public void getCachedAppsStorageResult_nullIfEmpty() throws Exception {
        SparseArray<StorageAsyncLoader.AppsStorageResult> result =
        final SparseArray<StorageAsyncLoader.AppsStorageResult> result =
                mCachedValuesHelper.getCachedAppsStorageResult();
        assertThat(result).isNull();
    }
+26 −30
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import static com.android.settings.utils.FileSizeFormatter.MEGABYTE_IN_BYTES;
import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@@ -55,7 +56,7 @@ import java.util.ArrayList;
import java.util.List;

@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class SecondaryUserControllerTest {
    private static final String TEST_NAME = "Fred";
    private static final String TARGET_PREFERENCE_GROUP_KEY = "pref_secondary_users";
@@ -90,7 +91,7 @@ public class SecondaryUserControllerTest {

        final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class);
        verify(mGroup).addPreference(argumentCaptor.capture());
        Preference preference = argumentCaptor.getValue();
        final Preference preference = argumentCaptor.getValue();
        assertThat(preference.getTitle()).isEqualTo(TEST_NAME);
    }

@@ -103,17 +104,17 @@ public class SecondaryUserControllerTest {

        verify(mGroup).addPreference(argumentCaptor.capture());

        Preference preference = argumentCaptor.getValue();
        final Preference preference = argumentCaptor.getValue();
        assertThat(preference.getSummary()).isEqualTo("0.01 GB");
    }

    @Test
    public void noSecondaryUserAddedIfNoneExist() throws Exception {
        ArrayList<UserInfo> userInfos = new ArrayList<>();
        final ArrayList<UserInfo> userInfos = new ArrayList<>();
        userInfos.add(mPrimaryUser);
        when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser);
        when(mUserManager.getUsers()).thenReturn(userInfos);
        List<AbstractPreferenceController> controllers =
        final List<AbstractPreferenceController> controllers =
                SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager);

        assertThat(controllers).hasSize(1);
@@ -123,15 +124,15 @@ public class SecondaryUserControllerTest {

    @Test
    public void secondaryUserAddedIfHasDistinctId() throws Exception {
        ArrayList<UserInfo> userInfos = new ArrayList<>();
        UserInfo secondaryUser = new UserInfo();
        final ArrayList<UserInfo> userInfos = new ArrayList<>();
        final UserInfo secondaryUser = new UserInfo();
        secondaryUser.id = 10;
        secondaryUser.profileGroupId = 101010; // this just has to be something not 0
        userInfos.add(mPrimaryUser);
        userInfos.add(secondaryUser);
        when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser);
        when(mUserManager.getUsers()).thenReturn(userInfos);
        List<AbstractPreferenceController> controllers =
        final List<AbstractPreferenceController> controllers =
                SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager);

        assertThat(controllers).hasSize(1);
@@ -140,15 +141,15 @@ public class SecondaryUserControllerTest {

    @Test
    public void profilesOfPrimaryUserAreNotIgnored() throws Exception {
        ArrayList<UserInfo> userInfos = new ArrayList<>();
        UserInfo secondaryUser = new UserInfo();
        final ArrayList<UserInfo> userInfos = new ArrayList<>();
        final UserInfo secondaryUser = new UserInfo();
        secondaryUser.id = mPrimaryUser.id;
        userInfos.add(mPrimaryUser);
        userInfos.add(secondaryUser);
        when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser);
        when(mUserManager.getUsers()).thenReturn(userInfos);

        List<AbstractPreferenceController> controllers =
        final List<AbstractPreferenceController> controllers =
                SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager);

        assertThat(controllers).hasSize(2);
@@ -161,9 +162,9 @@ public class SecondaryUserControllerTest {
        mPrimaryUser.name = TEST_NAME;
        mPrimaryUser.id = 10;
        mController.displayPreference(mScreen);
        StorageAsyncLoader.AppsStorageResult userResult =
        final StorageAsyncLoader.AppsStorageResult userResult =
                new StorageAsyncLoader.AppsStorageResult();
        SparseArray<StorageAsyncLoader.AppsStorageResult> result = new SparseArray<>();
        final SparseArray<StorageAsyncLoader.AppsStorageResult> result = new SparseArray<>();
        userResult.externalStats =
                new StorageStatsSource.ExternalStorageStats(
                        MEGABYTE_IN_BYTES * 30,
@@ -175,23 +176,23 @@ public class SecondaryUserControllerTest {
        mController.handleResult(result);
        final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class);
        verify(mGroup).addPreference(argumentCaptor.capture());
        Preference preference = argumentCaptor.getValue();
        final Preference preference = argumentCaptor.getValue();

        assertThat(preference.getSummary()).isEqualTo("0.03 GB");
    }

    @Test
    public void dontAddPrimaryProfileAsASecondaryProfile() throws Exception {
        ArrayList<UserInfo> userInfos = new ArrayList<>();
        final ArrayList<UserInfo> userInfos = new ArrayList<>();
        // The primary UserInfo may be a different object with a different name... but represent the
        // same user!
        UserInfo primaryUserRenamed = new UserInfo();
        final UserInfo primaryUserRenamed = new UserInfo();
        primaryUserRenamed.name = "Owner";
        primaryUserRenamed.flags = UserInfo.FLAG_PRIMARY;
        userInfos.add(primaryUserRenamed);
        when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser);
        when(mUserManager.getUsers()).thenReturn(userInfos);
        List<AbstractPreferenceController> controllers =
        final List<AbstractPreferenceController> controllers =
                SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager);

        assertThat(controllers).hasSize(1);
@@ -201,34 +202,29 @@ public class SecondaryUserControllerTest {

    @Test
    public void iconCallbackChangesPreferenceIcon() throws Exception {
        SparseArray<Drawable> icons = new SparseArray<>();
        Bitmap userBitmap =
                BitmapFactory.decodeResource(
                        RuntimeEnvironment.application.getResources(), R.drawable.home);
        UserIconDrawable drawable = new UserIconDrawable(100 /* size */).setIcon(userBitmap).bake();
        icons.put(10, drawable);
        final SparseArray<Drawable> icons = new SparseArray<>();
        final UserIconDrawable drawable = mock(UserIconDrawable.class);
        when(drawable.mutate()).thenReturn(drawable);
        mPrimaryUser.name = TEST_NAME;
        mPrimaryUser.id = 10;
        icons.put(mPrimaryUser.id, drawable);
        mController.displayPreference(mScreen);

        mController.handleUserIcons(icons);

        final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class);
        verify(mGroup).addPreference(argumentCaptor.capture());
        Preference preference = argumentCaptor.getValue();
        final Preference preference = argumentCaptor.getValue();
        assertThat(preference.getIcon()).isEqualTo(drawable);
    }

    @Test
    public void setIcon_doesntNpeOnNullPreference() throws Exception {
        SparseArray<Drawable> icons = new SparseArray<>();
        Bitmap userBitmap =
                BitmapFactory.decodeResource(
                        RuntimeEnvironment.application.getResources(), R.drawable.home);
        UserIconDrawable drawable = new UserIconDrawable(100 /* size */).setIcon(userBitmap).bake();
        icons.put(10, drawable);
        final SparseArray<Drawable> icons = new SparseArray<>();
        final UserIconDrawable drawable = mock(UserIconDrawable.class);
        mPrimaryUser.name = TEST_NAME;
        mPrimaryUser.id = 10;
        icons.put(mPrimaryUser.id, drawable);

        mController.handleUserIcons(icons);

+72 −60

File changed.

Preview size limit exceeded, changes collapsed.

+38 −18
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.storage.VolumeInfo;
import android.support.v7.preference.PreferenceViewHolder;
import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
@@ -56,7 +57,7 @@ import static com.android.settings.TestUtils.KILOBYTE;
import static com.android.settings.TestUtils.GIGABYTE;

@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O,
        shadows = {
                SettingsShadowResources.class,
                SettingsShadowResources.SettingsShadowTheme.class
@@ -94,43 +95,62 @@ public class StorageSummaryDonutPreferenceControllerTest {

    @Test
    public void testEmpty() throws Exception {
        final long totalSpace = 32 * GIGABYTE;
        final long usedSpace = 0;
        mController.updateBytes(0, 32 * GIGABYTE);
        mController.updateState(mPreference);

        assertThat(mPreference.getTitle().toString()).isEqualTo("0.00 B");
        assertThat(mPreference.getSummary().toString()).isEqualTo("Used of 32 GB");
        final Formatter.BytesResult usedSpaceResults = Formatter.formatBytes(
                mContext.getResources(), usedSpace, 0 /* flags */);
        assertThat(mPreference.getTitle().toString()).isEqualTo(
                usedSpaceResults.value + " " + usedSpaceResults.units);
        assertThat(mPreference.getSummary().toString()).isEqualTo(
                "Used of " + Formatter.formatShortFileSize(mContext, totalSpace));
    }

    @Test
    public void testTotalStorage() throws Exception {
        mController.updateBytes(KILOBYTE, KILOBYTE * 10);
        final long totalSpace = KILOBYTE * 10;
        final long usedSpace = KILOBYTE;
        mController.updateBytes(KILOBYTE, totalSpace);
        mController.updateState(mPreference);

        assertThat(mPreference.getTitle().toString()).isEqualTo("1.00 KB");
        assertThat(mPreference.getSummary().toString()).isEqualTo("Used of 10 KB");
        final Formatter.BytesResult usedSpaceResults = Formatter.formatBytes(
                mContext.getResources(), usedSpace, 0 /* flags */);
        assertThat(mPreference.getTitle().toString()).isEqualTo(
                usedSpaceResults.value + " " + usedSpaceResults.units);
        assertThat(mPreference.getSummary().toString()).isEqualTo(
                "Used of " + Formatter.formatShortFileSize(mContext, totalSpace));
    }

    @Test
    public void testPopulateWithVolume() throws Exception {
        VolumeInfo volume = Mockito.mock(VolumeInfo.class);
        File file = Mockito.mock(File.class);
        StorageVolumeProvider svp = Mockito.mock(StorageVolumeProvider.class);
        final long totalSpace = KILOBYTE * 10;
        final long freeSpace = KILOBYTE;
        final long usedSpace = totalSpace - freeSpace;
        final VolumeInfo volume = Mockito.mock(VolumeInfo.class);
        final File file = Mockito.mock(File.class);
        final StorageVolumeProvider svp = Mockito.mock(StorageVolumeProvider.class);
        when(volume.getPath()).thenReturn(file);
        when(file.getTotalSpace()).thenReturn(KILOBYTE * 10);
        when(file.getFreeSpace()).thenReturn(KILOBYTE);
        when(svp.getPrimaryStorageSize()).thenReturn(KILOBYTE * 10);
        when(file.getTotalSpace()).thenReturn(totalSpace);
        when(file.getFreeSpace()).thenReturn(freeSpace);
        when(svp.getPrimaryStorageSize()).thenReturn(totalSpace);

        mController.updateSizes(svp, volume);
        mController.updateState(mPreference);

        assertThat(mPreference.getTitle().toString()).isEqualTo("9.00 KB");
        assertThat(mPreference.getSummary().toString()).isEqualTo("Used of 10 KB");
        final Formatter.BytesResult usedSpaceResults = Formatter.formatBytes(
                mContext.getResources(), usedSpace, 0 /* flags */);
        assertThat(mPreference.getTitle().toString()).isEqualTo(
                usedSpaceResults.value + " " + usedSpaceResults.units);
        assertThat(mPreference.getSummary().toString()).isEqualTo(
                "Used of " + Formatter.formatShortFileSize(mContext, totalSpace));
    }

    @Test
    public void testFreeUpSpaceMetricIsTriggered() throws Exception {
        mPreference.onBindViewHolder(mHolder);
        Button button = (Button) mHolder.findViewById(R.id.deletion_helper_button);
        final Button button = (Button) mHolder.findViewById(R.id.deletion_helper_button);

        mPreference.onClick(button);

Loading