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
This commit is contained in:
TreeHugger Robot
2017-12-04 22:06:36 +00:00
committed by Android (Google) Code Review
6 changed files with 179 additions and 148 deletions

View File

@@ -17,6 +17,7 @@
package com.android.settings.deviceinfo.storage; package com.android.settings.deviceinfo.storage;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@@ -34,6 +35,7 @@ import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen; import android.support.v7.preference.PreferenceScreen;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.os.RoSystemProperties;
import com.android.settings.TestConfig; import com.android.settings.TestConfig;
import com.android.settings.core.instrumentation.MetricsFeatureProvider; import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.deletionhelper.ActivationWarningFragment; import com.android.settings.deletionhelper.ActivationWarningFragment;
@@ -52,12 +54,13 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config( @Config(
manifest = TestConfig.MANIFEST_PATH, manifest = TestConfig.MANIFEST_PATH,
sdk = TestConfig.SDK_VERSION, sdk = TestConfig.SDK_VERSION_O,
shadows = {SettingsShadowSystemProperties.class} shadows = {SettingsShadowSystemProperties.class}
) )
public class AutomaticStorageManagementSwitchPreferenceControllerTest { public class AutomaticStorageManagementSwitchPreferenceControllerTest {
@@ -79,13 +82,14 @@ public class AutomaticStorageManagementSwitchPreferenceControllerTest {
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application.getApplicationContext(); mContext = RuntimeEnvironment.application.getApplicationContext();
FeatureFactory factory = FeatureFactory.getFactory(mContext); final FeatureFactory factory = FeatureFactory.getFactory(mContext);
mMetricsFeature = factory.getMetricsFeatureProvider(); mMetricsFeature = factory.getMetricsFeatureProvider();
mController = new AutomaticStorageManagementSwitchPreferenceController( mController = new AutomaticStorageManagementSwitchPreferenceController(
mContext, mMetricsFeature, mFragmentManager); mContext, mMetricsFeature, mFragmentManager);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
} }
@After @After
public void tearDown() { public void tearDown() {
SettingsShadowSystemProperties.clear(); SettingsShadowSystemProperties.clear();
@@ -98,15 +102,15 @@ public class AutomaticStorageManagementSwitchPreferenceControllerTest {
@Test @Test
public void isAvailable_shouldAlwaysReturnFalse_forLowRamDevice() { public void isAvailable_shouldAlwaysReturnFalse_forLowRamDevice() {
SettingsShadowSystemProperties.set("ro.config.low_ram", "true"); ReflectionHelpers.setStaticField(RoSystemProperties.class, "CONFIG_LOW_RAM", true);
assertThat(mController.isAvailable()).isFalse(); assertThat(mController.isAvailable()).isFalse();
SettingsShadowSystemProperties.clear(); ReflectionHelpers.setStaticField(RoSystemProperties.class, "CONFIG_LOW_RAM", false);
} }
@Test @Test
public void onResume_shouldReflectEnabledStatus() { public void onResume_shouldReflectEnabledStatus() {
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
ContentResolver resolver = mContext.getContentResolver(); final ContentResolver resolver = mContext.getContentResolver();
Settings.Secure.putInt(resolver, Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 1); Settings.Secure.putInt(resolver, Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 1);
mController.onResume(); mController.onResume();
@@ -128,9 +132,9 @@ public class AutomaticStorageManagementSwitchPreferenceControllerTest {
// FakeFeatureFactory uses mock contexts, so this test scaffolds itself rather than using // FakeFeatureFactory uses mock contexts, so this test scaffolds itself rather than using
// the instance variables. // the instance variables.
FakeFeatureFactory.setupForTest(mMockContext); FakeFeatureFactory.setupForTest(mMockContext);
FakeFeatureFactory factory = final FakeFeatureFactory factory =
(FakeFeatureFactory) FakeFeatureFactory.getFactory(mMockContext); (FakeFeatureFactory) FakeFeatureFactory.getFactory(mMockContext);
AutomaticStorageManagementSwitchPreferenceController controller = final AutomaticStorageManagementSwitchPreferenceController controller =
new AutomaticStorageManagementSwitchPreferenceController( new AutomaticStorageManagementSwitchPreferenceController(
mMockContext, factory.metricsFeatureProvider, mFragmentManager); mMockContext, factory.metricsFeatureProvider, mFragmentManager);
@@ -144,15 +148,15 @@ public class AutomaticStorageManagementSwitchPreferenceControllerTest {
public void togglingShouldUpdateSettingsSecure() { public void togglingShouldUpdateSettingsSecure() {
mController.onSwitchToggled(true); mController.onSwitchToggled(true);
ContentResolver resolver = mContext.getContentResolver(); final ContentResolver resolver = mContext.getContentResolver();
assertThat(Settings.Secure.getInt( assertThat(Settings.Secure.getInt(
resolver, Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 0)).isNotEqualTo(0); resolver, Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 0)).isNotEqualTo(0);
} }
@Test @Test
public void togglingOnShouldTriggerWarningFragment() { public void togglingOnShouldTriggerWarningFragment() {
FragmentTransaction transaction = mock(FragmentTransaction.class); final FragmentTransaction transaction = mock(FragmentTransaction.class);
when (mFragmentManager.beginTransaction()).thenReturn(transaction); when(mFragmentManager.beginTransaction()).thenReturn(transaction);
SettingsShadowSystemProperties.set( SettingsShadowSystemProperties.set(
AutomaticStorageManagementSwitchPreferenceController AutomaticStorageManagementSwitchPreferenceController
.STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY, "false"); .STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY, "false");
@@ -164,8 +168,8 @@ public class AutomaticStorageManagementSwitchPreferenceControllerTest {
@Test @Test
public void togglingOffShouldTriggerWarningFragment() { public void togglingOffShouldTriggerWarningFragment() {
FragmentTransaction transaction = mock(FragmentTransaction.class); final FragmentTransaction transaction = mock(FragmentTransaction.class);
when (mFragmentManager.beginTransaction()).thenReturn(transaction); when(mFragmentManager.beginTransaction()).thenReturn(transaction);
mController.onSwitchToggled(false); mController.onSwitchToggled(false);
@@ -175,8 +179,8 @@ public class AutomaticStorageManagementSwitchPreferenceControllerTest {
@Test @Test
public void togglingOnShouldNotTriggerWarningFragmentIfEnabledByDefault() { public void togglingOnShouldNotTriggerWarningFragmentIfEnabledByDefault() {
FragmentTransaction transaction = mock(FragmentTransaction.class); final FragmentTransaction transaction = mock(FragmentTransaction.class);
when (mFragmentManager.beginTransaction()).thenReturn(transaction); when(mFragmentManager.beginTransaction()).thenReturn(transaction);
SettingsShadowSystemProperties.set( SettingsShadowSystemProperties.set(
AutomaticStorageManagementSwitchPreferenceController AutomaticStorageManagementSwitchPreferenceController
.STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY, "true"); .STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY, "true");
@@ -188,7 +192,7 @@ public class AutomaticStorageManagementSwitchPreferenceControllerTest {
@Test @Test
public void togglingOnShouldTriggerWarningFragmentIfEnabledByDefaultAndDisabledByPolicy() { public void togglingOnShouldTriggerWarningFragmentIfEnabledByDefaultAndDisabledByPolicy() {
FragmentTransaction transaction = mock(FragmentTransaction.class); final FragmentTransaction transaction = mock(FragmentTransaction.class);
when(mFragmentManager.beginTransaction()).thenReturn(transaction); when(mFragmentManager.beginTransaction()).thenReturn(transaction);
SettingsShadowSystemProperties.set( SettingsShadowSystemProperties.set(
AutomaticStorageManagementSwitchPreferenceController AutomaticStorageManagementSwitchPreferenceController

View File

@@ -55,7 +55,7 @@ import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
@RunWith(SettingsRobolectricTestRunner.class) @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 { public class CachedStorageValuesHelperTest {
private Context mContext; private Context mContext;
@@ -94,7 +94,7 @@ public class CachedStorageValuesHelperTest {
.putLong(TIMESTAMP_KEY, 10000L) .putLong(TIMESTAMP_KEY, 10000L)
.apply(); .apply();
PrivateStorageInfo info = mCachedValuesHelper.getCachedPrivateStorageInfo(); final PrivateStorageInfo info = mCachedValuesHelper.getCachedPrivateStorageInfo();
assertThat(info.freeBytes).isEqualTo(1000L); assertThat(info.freeBytes).isEqualTo(1000L);
assertThat(info.totalBytes).isEqualTo(6000L); assertThat(info.totalBytes).isEqualTo(6000L);
@@ -122,7 +122,7 @@ public class CachedStorageValuesHelperTest {
.putLong(TIMESTAMP_KEY, 10000L) .putLong(TIMESTAMP_KEY, 10000L)
.apply(); .apply();
SparseArray<StorageAsyncLoader.AppsStorageResult> result = final SparseArray<StorageAsyncLoader.AppsStorageResult> result =
mCachedValuesHelper.getCachedAppsStorageResult(); mCachedValuesHelper.getCachedAppsStorageResult();
StorageAsyncLoader.AppsStorageResult primaryResult = result.get(0); StorageAsyncLoader.AppsStorageResult primaryResult = result.get(0);
@@ -161,7 +161,7 @@ public class CachedStorageValuesHelperTest {
.putLong(TIMESTAMP_KEY, 10000L) .putLong(TIMESTAMP_KEY, 10000L)
.apply(); .apply();
PrivateStorageInfo info = mCachedValuesHelper.getCachedPrivateStorageInfo(); final PrivateStorageInfo info = mCachedValuesHelper.getCachedPrivateStorageInfo();
assertThat(info).isNull(); assertThat(info).isNull();
} }
@@ -187,7 +187,7 @@ public class CachedStorageValuesHelperTest {
.putLong(TIMESTAMP_KEY, 10000L) .putLong(TIMESTAMP_KEY, 10000L)
.apply(); .apply();
SparseArray<StorageAsyncLoader.AppsStorageResult> result = final SparseArray<StorageAsyncLoader.AppsStorageResult> result =
mCachedValuesHelper.getCachedAppsStorageResult(); mCachedValuesHelper.getCachedAppsStorageResult();
assertThat(result).isNull(); assertThat(result).isNull();
} }
@@ -214,7 +214,7 @@ public class CachedStorageValuesHelperTest {
.putLong(TIMESTAMP_KEY, 10000L) .putLong(TIMESTAMP_KEY, 10000L)
.apply(); .apply();
PrivateStorageInfo info = mCachedValuesHelper.getCachedPrivateStorageInfo(); final PrivateStorageInfo info = mCachedValuesHelper.getCachedPrivateStorageInfo();
assertThat(info).isNull(); assertThat(info).isNull();
} }
@@ -240,20 +240,20 @@ public class CachedStorageValuesHelperTest {
.putLong(TIMESTAMP_KEY, 10000L) .putLong(TIMESTAMP_KEY, 10000L)
.apply(); .apply();
SparseArray<StorageAsyncLoader.AppsStorageResult> result = final SparseArray<StorageAsyncLoader.AppsStorageResult> result =
mCachedValuesHelper.getCachedAppsStorageResult(); mCachedValuesHelper.getCachedAppsStorageResult();
assertThat(result).isNull(); assertThat(result).isNull();
} }
@Test @Test
public void getCachedPrivateStorageInfo_nullIfEmpty() throws Exception { public void getCachedPrivateStorageInfo_nullIfEmpty() throws Exception {
PrivateStorageInfo info = mCachedValuesHelper.getCachedPrivateStorageInfo(); final PrivateStorageInfo info = mCachedValuesHelper.getCachedPrivateStorageInfo();
assertThat(info).isNull(); assertThat(info).isNull();
} }
@Test @Test
public void getCachedAppsStorageResult_nullIfEmpty() throws Exception { public void getCachedAppsStorageResult_nullIfEmpty() throws Exception {
SparseArray<StorageAsyncLoader.AppsStorageResult> result = final SparseArray<StorageAsyncLoader.AppsStorageResult> result =
mCachedValuesHelper.getCachedAppsStorageResult(); mCachedValuesHelper.getCachedAppsStorageResult();
assertThat(result).isNull(); assertThat(result).isNull();
} }

View File

@@ -21,6 +21,7 @@ import static com.android.settings.utils.FileSizeFormatter.MEGABYTE_IN_BYTES;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -55,7 +56,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class) @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 { public class SecondaryUserControllerTest {
private static final String TEST_NAME = "Fred"; private static final String TEST_NAME = "Fred";
private static final String TARGET_PREFERENCE_GROUP_KEY = "pref_secondary_users"; 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); final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class);
verify(mGroup).addPreference(argumentCaptor.capture()); verify(mGroup).addPreference(argumentCaptor.capture());
Preference preference = argumentCaptor.getValue(); final Preference preference = argumentCaptor.getValue();
assertThat(preference.getTitle()).isEqualTo(TEST_NAME); assertThat(preference.getTitle()).isEqualTo(TEST_NAME);
} }
@@ -103,17 +104,17 @@ public class SecondaryUserControllerTest {
verify(mGroup).addPreference(argumentCaptor.capture()); verify(mGroup).addPreference(argumentCaptor.capture());
Preference preference = argumentCaptor.getValue(); final Preference preference = argumentCaptor.getValue();
assertThat(preference.getSummary()).isEqualTo("0.01 GB"); assertThat(preference.getSummary()).isEqualTo("0.01 GB");
} }
@Test @Test
public void noSecondaryUserAddedIfNoneExist() throws Exception { public void noSecondaryUserAddedIfNoneExist() throws Exception {
ArrayList<UserInfo> userInfos = new ArrayList<>(); final ArrayList<UserInfo> userInfos = new ArrayList<>();
userInfos.add(mPrimaryUser); userInfos.add(mPrimaryUser);
when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser); when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser);
when(mUserManager.getUsers()).thenReturn(userInfos); when(mUserManager.getUsers()).thenReturn(userInfos);
List<AbstractPreferenceController> controllers = final List<AbstractPreferenceController> controllers =
SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager); SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager);
assertThat(controllers).hasSize(1); assertThat(controllers).hasSize(1);
@@ -123,15 +124,15 @@ public class SecondaryUserControllerTest {
@Test @Test
public void secondaryUserAddedIfHasDistinctId() throws Exception { public void secondaryUserAddedIfHasDistinctId() throws Exception {
ArrayList<UserInfo> userInfos = new ArrayList<>(); final ArrayList<UserInfo> userInfos = new ArrayList<>();
UserInfo secondaryUser = new UserInfo(); final UserInfo secondaryUser = new UserInfo();
secondaryUser.id = 10; secondaryUser.id = 10;
secondaryUser.profileGroupId = 101010; // this just has to be something not 0 secondaryUser.profileGroupId = 101010; // this just has to be something not 0
userInfos.add(mPrimaryUser); userInfos.add(mPrimaryUser);
userInfos.add(secondaryUser); userInfos.add(secondaryUser);
when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser); when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser);
when(mUserManager.getUsers()).thenReturn(userInfos); when(mUserManager.getUsers()).thenReturn(userInfos);
List<AbstractPreferenceController> controllers = final List<AbstractPreferenceController> controllers =
SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager); SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager);
assertThat(controllers).hasSize(1); assertThat(controllers).hasSize(1);
@@ -140,15 +141,15 @@ public class SecondaryUserControllerTest {
@Test @Test
public void profilesOfPrimaryUserAreNotIgnored() throws Exception { public void profilesOfPrimaryUserAreNotIgnored() throws Exception {
ArrayList<UserInfo> userInfos = new ArrayList<>(); final ArrayList<UserInfo> userInfos = new ArrayList<>();
UserInfo secondaryUser = new UserInfo(); final UserInfo secondaryUser = new UserInfo();
secondaryUser.id = mPrimaryUser.id; secondaryUser.id = mPrimaryUser.id;
userInfos.add(mPrimaryUser); userInfos.add(mPrimaryUser);
userInfos.add(secondaryUser); userInfos.add(secondaryUser);
when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser); when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser);
when(mUserManager.getUsers()).thenReturn(userInfos); when(mUserManager.getUsers()).thenReturn(userInfos);
List<AbstractPreferenceController> controllers = final List<AbstractPreferenceController> controllers =
SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager); SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager);
assertThat(controllers).hasSize(2); assertThat(controllers).hasSize(2);
@@ -161,9 +162,9 @@ public class SecondaryUserControllerTest {
mPrimaryUser.name = TEST_NAME; mPrimaryUser.name = TEST_NAME;
mPrimaryUser.id = 10; mPrimaryUser.id = 10;
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
StorageAsyncLoader.AppsStorageResult userResult = final StorageAsyncLoader.AppsStorageResult userResult =
new StorageAsyncLoader.AppsStorageResult(); new StorageAsyncLoader.AppsStorageResult();
SparseArray<StorageAsyncLoader.AppsStorageResult> result = new SparseArray<>(); final SparseArray<StorageAsyncLoader.AppsStorageResult> result = new SparseArray<>();
userResult.externalStats = userResult.externalStats =
new StorageStatsSource.ExternalStorageStats( new StorageStatsSource.ExternalStorageStats(
MEGABYTE_IN_BYTES * 30, MEGABYTE_IN_BYTES * 30,
@@ -175,23 +176,23 @@ public class SecondaryUserControllerTest {
mController.handleResult(result); mController.handleResult(result);
final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class); final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class);
verify(mGroup).addPreference(argumentCaptor.capture()); verify(mGroup).addPreference(argumentCaptor.capture());
Preference preference = argumentCaptor.getValue(); final Preference preference = argumentCaptor.getValue();
assertThat(preference.getSummary()).isEqualTo("0.03 GB"); assertThat(preference.getSummary()).isEqualTo("0.03 GB");
} }
@Test @Test
public void dontAddPrimaryProfileAsASecondaryProfile() throws Exception { 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 // The primary UserInfo may be a different object with a different name... but represent the
// same user! // same user!
UserInfo primaryUserRenamed = new UserInfo(); final UserInfo primaryUserRenamed = new UserInfo();
primaryUserRenamed.name = "Owner"; primaryUserRenamed.name = "Owner";
primaryUserRenamed.flags = UserInfo.FLAG_PRIMARY; primaryUserRenamed.flags = UserInfo.FLAG_PRIMARY;
userInfos.add(primaryUserRenamed); userInfos.add(primaryUserRenamed);
when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser); when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser);
when(mUserManager.getUsers()).thenReturn(userInfos); when(mUserManager.getUsers()).thenReturn(userInfos);
List<AbstractPreferenceController> controllers = final List<AbstractPreferenceController> controllers =
SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager); SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager);
assertThat(controllers).hasSize(1); assertThat(controllers).hasSize(1);
@@ -201,34 +202,29 @@ public class SecondaryUserControllerTest {
@Test @Test
public void iconCallbackChangesPreferenceIcon() throws Exception { public void iconCallbackChangesPreferenceIcon() throws Exception {
SparseArray<Drawable> icons = new SparseArray<>(); final SparseArray<Drawable> icons = new SparseArray<>();
Bitmap userBitmap = final UserIconDrawable drawable = mock(UserIconDrawable.class);
BitmapFactory.decodeResource( when(drawable.mutate()).thenReturn(drawable);
RuntimeEnvironment.application.getResources(), R.drawable.home);
UserIconDrawable drawable = new UserIconDrawable(100 /* size */).setIcon(userBitmap).bake();
icons.put(10, drawable);
mPrimaryUser.name = TEST_NAME; mPrimaryUser.name = TEST_NAME;
mPrimaryUser.id = 10; mPrimaryUser.id = 10;
icons.put(mPrimaryUser.id, drawable);
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
mController.handleUserIcons(icons); mController.handleUserIcons(icons);
final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class); final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class);
verify(mGroup).addPreference(argumentCaptor.capture()); verify(mGroup).addPreference(argumentCaptor.capture());
Preference preference = argumentCaptor.getValue(); final Preference preference = argumentCaptor.getValue();
assertThat(preference.getIcon()).isEqualTo(drawable); assertThat(preference.getIcon()).isEqualTo(drawable);
} }
@Test @Test
public void setIcon_doesntNpeOnNullPreference() throws Exception { public void setIcon_doesntNpeOnNullPreference() throws Exception {
SparseArray<Drawable> icons = new SparseArray<>(); final SparseArray<Drawable> icons = new SparseArray<>();
Bitmap userBitmap = final UserIconDrawable drawable = mock(UserIconDrawable.class);
BitmapFactory.decodeResource(
RuntimeEnvironment.application.getResources(), R.drawable.home);
UserIconDrawable drawable = new UserIconDrawable(100 /* size */).setIcon(userBitmap).bake();
icons.put(10, drawable);
mPrimaryUser.name = TEST_NAME; mPrimaryUser.name = TEST_NAME;
mPrimaryUser.id = 10; mPrimaryUser.id = 10;
icons.put(mPrimaryUser.id, drawable);
mController.handleUserIcons(icons); mController.handleUserIcons(icons);

View File

@@ -28,7 +28,10 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.app.Activity;
import android.app.Fragment; import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
@@ -67,14 +70,20 @@ import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class StorageItemPreferenceControllerTest { public class StorageItemPreferenceControllerTest {
private Context mContext; private Context mContext;
private VolumeInfo mVolume; private VolumeInfo mVolume;
@Mock(answer = Answers.RETURNS_DEEP_STUBS) @Mock
private Fragment mFragment; private Fragment mFragment;
@Mock @Mock
private StorageVolumeProvider mSvp; private StorageVolumeProvider mSvp;
@Mock
private Activity mActivity;
@Mock
private FragmentManager mFragmentManager;
@Mock
private FragmentTransaction mFragmentTransaction;
private StorageItemPreferenceController mController; private StorageItemPreferenceController mController;
private StorageItemPreference mPreference; private StorageItemPreference mPreference;
private FakeFeatureFactory mFakeFeatureFactory; private FakeFeatureFactory mFakeFeatureFactory;
@@ -83,6 +92,9 @@ public class StorageItemPreferenceControllerTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
when(mFragment.getActivity()).thenReturn(mActivity);
when(mFragment.getFragmentManager()).thenReturn(mFragmentManager);
when(mFragmentManager.beginTransaction()).thenReturn(mFragmentTransaction);
mContext = spy(RuntimeEnvironment.application.getApplicationContext()); mContext = spy(RuntimeEnvironment.application.getApplicationContext());
FakeFeatureFactory.setupForTest(mContext); FakeFeatureFactory.setupForTest(mContext);
mFakeFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); mFakeFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
@@ -94,7 +106,7 @@ public class StorageItemPreferenceControllerTest {
mPreference = new StorageItemPreference(mContext); mPreference = new StorageItemPreference(mContext);
// Inflate the preference and the widget. // Inflate the preference and the widget.
LayoutInflater inflater = LayoutInflater.from(mContext); final LayoutInflater inflater = LayoutInflater.from(mContext);
final View view = inflater.inflate( final View view = inflater.inflate(
mPreference.getLayoutResource(), new LinearLayout(mContext), false); mPreference.getLayoutResource(), new LinearLayout(mContext), false);
} }
@@ -116,10 +128,10 @@ public class StorageItemPreferenceControllerTest {
mController.handlePreferenceTreeClick(mPreference); mController.handlePreferenceTreeClick(mPreference);
final ArgumentCaptor<Intent> argumentCaptor = ArgumentCaptor.forClass(Intent.class); final ArgumentCaptor<Intent> argumentCaptor = ArgumentCaptor.forClass(Intent.class);
verify(mFragment.getActivity()).startActivityAsUser(argumentCaptor.capture(), verify(mActivity).startActivityAsUser(argumentCaptor.capture(),
nullable(UserHandle.class)); nullable(UserHandle.class));
Intent intent = argumentCaptor.getValue(); final Intent intent = argumentCaptor.getValue();
assertThat(intent.getAction()).isEqualTo(Intent.ACTION_MAIN); assertThat(intent.getAction()).isEqualTo(Intent.ACTION_MAIN);
assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName()); assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName());
assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)) assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT))
@@ -136,7 +148,7 @@ public class StorageItemPreferenceControllerTest {
final ArgumentCaptor<Intent> argumentCaptor = ArgumentCaptor.forClass(Intent.class); final ArgumentCaptor<Intent> argumentCaptor = ArgumentCaptor.forClass(Intent.class);
verify(mFragment.getActivity()).startActivityAsUser(argumentCaptor.capture(), verify(mFragment.getActivity()).startActivityAsUser(argumentCaptor.capture(),
nullable(UserHandle.class)); nullable(UserHandle.class));
Intent intent = argumentCaptor.getValue(); final Intent intent = argumentCaptor.getValue();
assertThat(intent.getAction()).isEqualTo(Intent.ACTION_MAIN); assertThat(intent.getAction()).isEqualTo(Intent.ACTION_MAIN);
assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName()); assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName());
@@ -164,7 +176,7 @@ public class StorageItemPreferenceControllerTest {
verify(mFragment.getActivity()).startActivityAsUser(argumentCaptor.capture(), verify(mFragment.getActivity()).startActivityAsUser(argumentCaptor.capture(),
nullable(UserHandle.class)); nullable(UserHandle.class));
Intent intent = argumentCaptor.getValue(); final Intent intent = argumentCaptor.getValue();
assertThat(intent.getAction()).isEqualTo(Intent.ACTION_MAIN); assertThat(intent.getAction()).isEqualTo(Intent.ACTION_MAIN);
assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName()); assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName());
assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)).isEqualTo( assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)).isEqualTo(
@@ -273,14 +285,14 @@ public class StorageItemPreferenceControllerTest {
@Test @Test
public void testMeasurementCompletedUpdatesPreferences() { public void testMeasurementCompletedUpdatesPreferences() {
StorageItemPreference audio = new StorageItemPreference(mContext); final StorageItemPreference audio = new StorageItemPreference(mContext);
StorageItemPreference image = new StorageItemPreference(mContext); final StorageItemPreference image = new StorageItemPreference(mContext);
StorageItemPreference games = new StorageItemPreference(mContext); final StorageItemPreference games = new StorageItemPreference(mContext);
StorageItemPreference movies = new StorageItemPreference(mContext); final StorageItemPreference movies = new StorageItemPreference(mContext);
StorageItemPreference apps = new StorageItemPreference(mContext); final StorageItemPreference apps = new StorageItemPreference(mContext);
StorageItemPreference system = new StorageItemPreference(mContext); final StorageItemPreference system = new StorageItemPreference(mContext);
StorageItemPreference files = new StorageItemPreference(mContext); final StorageItemPreference files = new StorageItemPreference(mContext);
PreferenceScreen screen = mock(PreferenceScreen.class); final PreferenceScreen screen = mock(PreferenceScreen.class);
when(screen.findPreference( when(screen.findPreference(
eq(StorageItemPreferenceController.AUDIO_KEY))).thenReturn(audio); eq(StorageItemPreferenceController.AUDIO_KEY))).thenReturn(audio);
when(screen.findPreference( when(screen.findPreference(
@@ -298,7 +310,7 @@ public class StorageItemPreferenceControllerTest {
mController.displayPreference(screen); mController.displayPreference(screen);
mController.setUsedSize(MEGABYTE_IN_BYTES * 970); // There should 870MB attributed. mController.setUsedSize(MEGABYTE_IN_BYTES * 970); // There should 870MB attributed.
StorageAsyncLoader.AppsStorageResult result = new StorageAsyncLoader.AppsStorageResult(); final StorageAsyncLoader.AppsStorageResult result = new StorageAsyncLoader.AppsStorageResult();
result.gamesSize = MEGABYTE_IN_BYTES * 80; result.gamesSize = MEGABYTE_IN_BYTES * 80;
result.videoAppsSize = MEGABYTE_IN_BYTES * 160; result.videoAppsSize = MEGABYTE_IN_BYTES * 160;
result.musicAppsSize = MEGABYTE_IN_BYTES * 40; result.musicAppsSize = MEGABYTE_IN_BYTES * 40;
@@ -310,7 +322,7 @@ public class StorageItemPreferenceControllerTest {
MEGABYTE_IN_BYTES * 150, // video MEGABYTE_IN_BYTES * 150, // video
MEGABYTE_IN_BYTES * 200, 0); // image MEGABYTE_IN_BYTES * 200, 0); // image
SparseArray<StorageAsyncLoader.AppsStorageResult> results = new SparseArray<>(); final SparseArray<StorageAsyncLoader.AppsStorageResult> results = new SparseArray<>();
results.put(0, result); results.put(0, result);
mController.onLoadFinished(results, 0); mController.onLoadFinished(results, 0);
@@ -324,21 +336,21 @@ public class StorageItemPreferenceControllerTest {
@Test @Test
public void settingUserIdAppliesNewIcons() { public void settingUserIdAppliesNewIcons() {
StorageItemPreference audio = spy(new StorageItemPreference(mContext)); final StorageItemPreference audio = spy(new StorageItemPreference(mContext));
audio.setIcon(R.drawable.ic_media_stream); audio.setIcon(R.drawable.ic_media_stream);
StorageItemPreference video = spy(new StorageItemPreference(mContext)); final StorageItemPreference video = spy(new StorageItemPreference(mContext));
video.setIcon(R.drawable.ic_local_movies); video.setIcon(R.drawable.ic_local_movies);
StorageItemPreference image = spy(new StorageItemPreference(mContext)); final StorageItemPreference image = spy(new StorageItemPreference(mContext));
image.setIcon(R.drawable.ic_photo_library); image.setIcon(R.drawable.ic_photo_library);
StorageItemPreference games = spy(new StorageItemPreference(mContext)); final StorageItemPreference games = spy(new StorageItemPreference(mContext));
games.setIcon(R.drawable.ic_videogame_vd_theme_24); games.setIcon(R.drawable.ic_videogame_vd_theme_24);
StorageItemPreference apps = spy(new StorageItemPreference(mContext)); final StorageItemPreference apps = spy(new StorageItemPreference(mContext));
apps.setIcon(R.drawable.ic_storage_apps); apps.setIcon(R.drawable.ic_storage_apps);
StorageItemPreference system = spy(new StorageItemPreference(mContext)); final StorageItemPreference system = spy(new StorageItemPreference(mContext));
system.setIcon(R.drawable.ic_system_update_vd_theme_24); system.setIcon(R.drawable.ic_system_update_vd_theme_24);
StorageItemPreference files = spy(new StorageItemPreference(mContext)); final StorageItemPreference files = spy(new StorageItemPreference(mContext));
files.setIcon(R.drawable.ic_folder_vd_theme_24); files.setIcon(R.drawable.ic_folder_vd_theme_24);
PreferenceScreen screen = mock(PreferenceScreen.class); final PreferenceScreen screen = mock(PreferenceScreen.class);
when(screen.findPreference( when(screen.findPreference(
eq(StorageItemPreferenceController.AUDIO_KEY))).thenReturn(audio); eq(StorageItemPreferenceController.AUDIO_KEY))).thenReturn(audio);
when(screen.findPreference( when(screen.findPreference(
@@ -368,13 +380,13 @@ public class StorageItemPreferenceControllerTest {
@Test @Test
public void displayPreference_dontHideFilePreferenceWhenEmulatedInternalStorageUsed() { public void displayPreference_dontHideFilePreferenceWhenEmulatedInternalStorageUsed() {
StorageItemPreference audio = new StorageItemPreference(mContext); final StorageItemPreference audio = new StorageItemPreference(mContext);
StorageItemPreference image = new StorageItemPreference(mContext); final StorageItemPreference image = new StorageItemPreference(mContext);
StorageItemPreference games = new StorageItemPreference(mContext); final StorageItemPreference games = new StorageItemPreference(mContext);
StorageItemPreference apps = new StorageItemPreference(mContext); final StorageItemPreference apps = new StorageItemPreference(mContext);
StorageItemPreference system = new StorageItemPreference(mContext); final StorageItemPreference system = new StorageItemPreference(mContext);
StorageItemPreference files = new StorageItemPreference(mContext); final StorageItemPreference files = new StorageItemPreference(mContext);
PreferenceScreen screen = mock(PreferenceScreen.class); final PreferenceScreen screen = mock(PreferenceScreen.class);
when(screen.findPreference(eq(StorageItemPreferenceController.AUDIO_KEY))) when(screen.findPreference(eq(StorageItemPreferenceController.AUDIO_KEY)))
.thenReturn(audio); .thenReturn(audio);
when(screen.findPreference(eq(StorageItemPreferenceController.PHOTO_KEY))) when(screen.findPreference(eq(StorageItemPreferenceController.PHOTO_KEY)))
@@ -397,13 +409,13 @@ public class StorageItemPreferenceControllerTest {
@Test @Test
public void displayPreference_hideFilePreferenceWhenEmulatedStorageUnreadable() { public void displayPreference_hideFilePreferenceWhenEmulatedStorageUnreadable() {
StorageItemPreference audio = new StorageItemPreference(mContext); final StorageItemPreference audio = new StorageItemPreference(mContext);
StorageItemPreference image = new StorageItemPreference(mContext); final StorageItemPreference image = new StorageItemPreference(mContext);
StorageItemPreference games = new StorageItemPreference(mContext); final StorageItemPreference games = new StorageItemPreference(mContext);
StorageItemPreference apps = new StorageItemPreference(mContext); final StorageItemPreference apps = new StorageItemPreference(mContext);
StorageItemPreference system = new StorageItemPreference(mContext); final StorageItemPreference system = new StorageItemPreference(mContext);
StorageItemPreference files = new StorageItemPreference(mContext); final StorageItemPreference files = new StorageItemPreference(mContext);
PreferenceScreen screen = mock(PreferenceScreen.class); final PreferenceScreen screen = mock(PreferenceScreen.class);
when(screen.findPreference(eq(StorageItemPreferenceController.AUDIO_KEY))) when(screen.findPreference(eq(StorageItemPreferenceController.AUDIO_KEY)))
.thenReturn(audio); .thenReturn(audio);
when(screen.findPreference(eq(StorageItemPreferenceController.PHOTO_KEY))) when(screen.findPreference(eq(StorageItemPreferenceController.PHOTO_KEY)))
@@ -426,13 +438,13 @@ public class StorageItemPreferenceControllerTest {
@Test @Test
public void displayPreference_hideFilePreferenceWhenNoEmulatedInternalStorage() { public void displayPreference_hideFilePreferenceWhenNoEmulatedInternalStorage() {
StorageItemPreference audio = new StorageItemPreference(mContext); final StorageItemPreference audio = new StorageItemPreference(mContext);
StorageItemPreference image = new StorageItemPreference(mContext); final StorageItemPreference image = new StorageItemPreference(mContext);
StorageItemPreference games = new StorageItemPreference(mContext); final StorageItemPreference games = new StorageItemPreference(mContext);
StorageItemPreference apps = new StorageItemPreference(mContext); final StorageItemPreference apps = new StorageItemPreference(mContext);
StorageItemPreference system = new StorageItemPreference(mContext); final StorageItemPreference system = new StorageItemPreference(mContext);
StorageItemPreference files = new StorageItemPreference(mContext); final StorageItemPreference files = new StorageItemPreference(mContext);
PreferenceScreen screen = mock(PreferenceScreen.class); final PreferenceScreen screen = mock(PreferenceScreen.class);
when(screen.findPreference(eq(StorageItemPreferenceController.AUDIO_KEY))) when(screen.findPreference(eq(StorageItemPreferenceController.AUDIO_KEY)))
.thenReturn(audio); .thenReturn(audio);
when(screen.findPreference(eq(StorageItemPreferenceController.PHOTO_KEY))) when(screen.findPreference(eq(StorageItemPreferenceController.PHOTO_KEY)))
@@ -454,13 +466,13 @@ public class StorageItemPreferenceControllerTest {
@Test @Test
public void displayPreference_updateFilePreferenceToHideAfterSettingVolume() { public void displayPreference_updateFilePreferenceToHideAfterSettingVolume() {
StorageItemPreference audio = new StorageItemPreference(mContext); final StorageItemPreference audio = new StorageItemPreference(mContext);
StorageItemPreference image = new StorageItemPreference(mContext); final StorageItemPreference image = new StorageItemPreference(mContext);
StorageItemPreference games = new StorageItemPreference(mContext); final StorageItemPreference games = new StorageItemPreference(mContext);
StorageItemPreference apps = new StorageItemPreference(mContext); final StorageItemPreference apps = new StorageItemPreference(mContext);
StorageItemPreference system = new StorageItemPreference(mContext); final StorageItemPreference system = new StorageItemPreference(mContext);
StorageItemPreference files = new StorageItemPreference(mContext); final StorageItemPreference files = new StorageItemPreference(mContext);
PreferenceScreen screen = mock(PreferenceScreen.class); final PreferenceScreen screen = mock(PreferenceScreen.class);
when(screen.findPreference(eq(StorageItemPreferenceController.AUDIO_KEY))) when(screen.findPreference(eq(StorageItemPreferenceController.AUDIO_KEY)))
.thenReturn(audio); .thenReturn(audio);
when(screen.findPreference(eq(StorageItemPreferenceController.PHOTO_KEY))) when(screen.findPreference(eq(StorageItemPreferenceController.PHOTO_KEY)))
@@ -486,13 +498,13 @@ public class StorageItemPreferenceControllerTest {
@Test @Test
public void displayPreference_updateFilePreferenceToShowAfterSettingVolume() { public void displayPreference_updateFilePreferenceToShowAfterSettingVolume() {
StorageItemPreference audio = new StorageItemPreference(mContext); final StorageItemPreference audio = new StorageItemPreference(mContext);
StorageItemPreference image = new StorageItemPreference(mContext); final StorageItemPreference image = new StorageItemPreference(mContext);
StorageItemPreference games = new StorageItemPreference(mContext); final StorageItemPreference games = new StorageItemPreference(mContext);
StorageItemPreference apps = new StorageItemPreference(mContext); final StorageItemPreference apps = new StorageItemPreference(mContext);
StorageItemPreference system = new StorageItemPreference(mContext); final StorageItemPreference system = new StorageItemPreference(mContext);
StorageItemPreference files = new StorageItemPreference(mContext); final StorageItemPreference files = new StorageItemPreference(mContext);
PreferenceScreen screen = mock(PreferenceScreen.class); final PreferenceScreen screen = mock(PreferenceScreen.class);
when(screen.findPreference(eq(StorageItemPreferenceController.AUDIO_KEY))) when(screen.findPreference(eq(StorageItemPreferenceController.AUDIO_KEY)))
.thenReturn(audio); .thenReturn(audio);
when(screen.findPreference(eq(StorageItemPreferenceController.PHOTO_KEY))) when(screen.findPreference(eq(StorageItemPreferenceController.PHOTO_KEY)))

View File

@@ -28,6 +28,7 @@ import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
import android.os.storage.VolumeInfo; import android.os.storage.VolumeInfo;
import android.support.v7.preference.PreferenceViewHolder; import android.support.v7.preference.PreferenceViewHolder;
import android.text.format.Formatter;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
@@ -56,11 +57,11 @@ import static com.android.settings.TestUtils.KILOBYTE;
import static com.android.settings.TestUtils.GIGABYTE; import static com.android.settings.TestUtils.GIGABYTE;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O,
shadows = { shadows = {
SettingsShadowResources.class, SettingsShadowResources.class,
SettingsShadowResources.SettingsShadowTheme.class SettingsShadowResources.SettingsShadowTheme.class
}) })
public class StorageSummaryDonutPreferenceControllerTest { public class StorageSummaryDonutPreferenceControllerTest {
private Context mContext; private Context mContext;
private StorageSummaryDonutPreferenceController mController; private StorageSummaryDonutPreferenceController mController;
@@ -94,43 +95,62 @@ public class StorageSummaryDonutPreferenceControllerTest {
@Test @Test
public void testEmpty() throws Exception { public void testEmpty() throws Exception {
final long totalSpace = 32 * GIGABYTE;
final long usedSpace = 0;
mController.updateBytes(0, 32 * GIGABYTE); mController.updateBytes(0, 32 * GIGABYTE);
mController.updateState(mPreference); mController.updateState(mPreference);
assertThat(mPreference.getTitle().toString()).isEqualTo("0.00 B"); final Formatter.BytesResult usedSpaceResults = Formatter.formatBytes(
assertThat(mPreference.getSummary().toString()).isEqualTo("Used of 32 GB"); 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 @Test
public void testTotalStorage() throws Exception { 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); mController.updateState(mPreference);
assertThat(mPreference.getTitle().toString()).isEqualTo("1.00 KB"); final Formatter.BytesResult usedSpaceResults = Formatter.formatBytes(
assertThat(mPreference.getSummary().toString()).isEqualTo("Used of 10 KB"); 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 @Test
public void testPopulateWithVolume() throws Exception { public void testPopulateWithVolume() throws Exception {
VolumeInfo volume = Mockito.mock(VolumeInfo.class); final long totalSpace = KILOBYTE * 10;
File file = Mockito.mock(File.class); final long freeSpace = KILOBYTE;
StorageVolumeProvider svp = Mockito.mock(StorageVolumeProvider.class); 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(volume.getPath()).thenReturn(file);
when(file.getTotalSpace()).thenReturn(KILOBYTE * 10); when(file.getTotalSpace()).thenReturn(totalSpace);
when(file.getFreeSpace()).thenReturn(KILOBYTE); when(file.getFreeSpace()).thenReturn(freeSpace);
when(svp.getPrimaryStorageSize()).thenReturn(KILOBYTE * 10); when(svp.getPrimaryStorageSize()).thenReturn(totalSpace);
mController.updateSizes(svp, volume); mController.updateSizes(svp, volume);
mController.updateState(mPreference); mController.updateState(mPreference);
assertThat(mPreference.getTitle().toString()).isEqualTo("9.00 KB"); final Formatter.BytesResult usedSpaceResults = Formatter.formatBytes(
assertThat(mPreference.getSummary().toString()).isEqualTo("Used of 10 KB"); 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 @Test
public void testFreeUpSpaceMetricIsTriggered() throws Exception { public void testFreeUpSpaceMetricIsTriggered() throws Exception {
mPreference.onBindViewHolder(mHolder); 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); mPreference.onClick(button);

View File

@@ -19,6 +19,7 @@ package com.android.settings.deviceinfo.storage;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static com.android.settings.utils.FileSizeFormatter.MEGABYTE_IN_BYTES; import static com.android.settings.utils.FileSizeFormatter.MEGABYTE_IN_BYTES;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -53,7 +54,7 @@ import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class UserProfileControllerTest { public class UserProfileControllerTest {
private static final String TEST_NAME = "Fred"; private static final String TEST_NAME = "Fred";
@@ -82,7 +83,7 @@ public class UserProfileControllerTest {
public void controllerAddsPrimaryProfilePreference() throws Exception { public void controllerAddsPrimaryProfilePreference() throws Exception {
final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class); final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class);
verify(mScreen).addPreference(argumentCaptor.capture()); verify(mScreen).addPreference(argumentCaptor.capture());
Preference preference = argumentCaptor.getValue(); final Preference preference = argumentCaptor.getValue();
assertThat(preference.getTitle()).isEqualTo(TEST_NAME); assertThat(preference.getTitle()).isEqualTo(TEST_NAME);
assertThat(preference.getKey()).isEqualTo("pref_profile_10"); assertThat(preference.getKey()).isEqualTo("pref_profile_10");
@@ -93,12 +94,12 @@ public class UserProfileControllerTest {
final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class); final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class);
verify(mScreen).addPreference(argumentCaptor.capture()); verify(mScreen).addPreference(argumentCaptor.capture());
Preference preference = argumentCaptor.getValue(); final Preference preference = argumentCaptor.getValue();
assertThat(mController.handlePreferenceTreeClick(preference)).isTrue(); assertThat(mController.handlePreferenceTreeClick(preference)).isTrue();
final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
verify(mContext).startActivity(intentCaptor.capture()); verify(mContext).startActivity(intentCaptor.capture());
Intent intent = intentCaptor.getValue(); final Intent intent = intentCaptor.getValue();
assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName()); assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName());
assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)).isEqualTo( assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)).isEqualTo(
StorageProfileFragment.class.getName()); StorageProfileFragment.class.getName());
@@ -106,8 +107,8 @@ public class UserProfileControllerTest {
@Test @Test
public void acceptingResultUpdatesPreferenceSize() throws Exception { public void acceptingResultUpdatesPreferenceSize() throws Exception {
SparseArray<StorageAsyncLoader.AppsStorageResult> result = new SparseArray<>(); final SparseArray<StorageAsyncLoader.AppsStorageResult> result = new SparseArray<>();
StorageAsyncLoader.AppsStorageResult userResult = final StorageAsyncLoader.AppsStorageResult userResult =
new StorageAsyncLoader.AppsStorageResult(); new StorageAsyncLoader.AppsStorageResult();
userResult.externalStats = userResult.externalStats =
new StorageStatsSource.ExternalStorageStats( new StorageStatsSource.ExternalStorageStats(
@@ -120,25 +121,23 @@ public class UserProfileControllerTest {
mController.handleResult(result); mController.handleResult(result);
final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class); final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class);
verify(mScreen).addPreference(argumentCaptor.capture()); verify(mScreen).addPreference(argumentCaptor.capture());
Preference preference = argumentCaptor.getValue(); final Preference preference = argumentCaptor.getValue();
assertThat(preference.getSummary()).isEqualTo("0.10 GB"); assertThat(preference.getSummary()).isEqualTo("0.10 GB");
} }
@Test @Test
public void iconCallbackChangesPreferenceIcon() throws Exception { public void iconCallbackChangesPreferenceIcon() throws Exception {
SparseArray<Drawable> icons = new SparseArray<>(); final SparseArray<Drawable> icons = new SparseArray<>();
Bitmap userBitmap = final UserIconDrawable drawable = mock(UserIconDrawable.class);
BitmapFactory.decodeResource( when(drawable.mutate()).thenReturn(drawable);
RuntimeEnvironment.application.getResources(), R.drawable.home); icons.put(mPrimaryProfile.id, drawable);
UserIconDrawable drawable = new UserIconDrawable(100 /* size */).setIcon(userBitmap).bake();
icons.put(10, drawable);
mController.handleUserIcons(icons); mController.handleUserIcons(icons);
final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class); final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class);
verify(mScreen).addPreference(argumentCaptor.capture()); verify(mScreen).addPreference(argumentCaptor.capture());
Preference preference = argumentCaptor.getValue(); final Preference preference = argumentCaptor.getValue();
assertThat(preference.getIcon()).isEqualTo(drawable); assertThat(preference.getIcon()).isEqualTo(drawable);
} }
} }