diff --git a/tests/robotests/src/com/android/settings/deviceinfo/storage/AutomaticStorageManagementSwitchPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/storage/AutomaticStorageManagementSwitchPreferenceControllerTest.java index 7d47fc26c33..fe640d27e25 100644 --- a/tests/robotests/src/com/android/settings/deviceinfo/storage/AutomaticStorageManagementSwitchPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/deviceinfo/storage/AutomaticStorageManagementSwitchPreferenceControllerTest.java @@ -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,13 +54,14 @@ 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, - shadows = {SettingsShadowSystemProperties.class} + manifest = TestConfig.MANIFEST_PATH, + 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,15 +148,15 @@ 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); - when (mFragmentManager.beginTransaction()).thenReturn(transaction); + final FragmentTransaction transaction = mock(FragmentTransaction.class); + when(mFragmentManager.beginTransaction()).thenReturn(transaction); SettingsShadowSystemProperties.set( AutomaticStorageManagementSwitchPreferenceController .STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY, "false"); @@ -164,8 +168,8 @@ public class AutomaticStorageManagementSwitchPreferenceControllerTest { @Test public void togglingOffShouldTriggerWarningFragment() { - FragmentTransaction transaction = mock(FragmentTransaction.class); - when (mFragmentManager.beginTransaction()).thenReturn(transaction); + final FragmentTransaction transaction = mock(FragmentTransaction.class); + when(mFragmentManager.beginTransaction()).thenReturn(transaction); mController.onSwitchToggled(false); @@ -175,8 +179,8 @@ public class AutomaticStorageManagementSwitchPreferenceControllerTest { @Test public void togglingOnShouldNotTriggerWarningFragmentIfEnabledByDefault() { - FragmentTransaction transaction = mock(FragmentTransaction.class); - when (mFragmentManager.beginTransaction()).thenReturn(transaction); + final FragmentTransaction transaction = mock(FragmentTransaction.class); + when(mFragmentManager.beginTransaction()).thenReturn(transaction); SettingsShadowSystemProperties.set( AutomaticStorageManagementSwitchPreferenceController .STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY, "true"); @@ -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 diff --git a/tests/robotests/src/com/android/settings/deviceinfo/storage/CachedStorageValuesHelperTest.java b/tests/robotests/src/com/android/settings/deviceinfo/storage/CachedStorageValuesHelperTest.java index 154a7a1afbf..7b3556f3df0 100644 --- a/tests/robotests/src/com/android/settings/deviceinfo/storage/CachedStorageValuesHelperTest.java +++ b/tests/robotests/src/com/android/settings/deviceinfo/storage/CachedStorageValuesHelperTest.java @@ -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 result = + final SparseArray 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 result = + final SparseArray 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 result = + final SparseArray 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 result = + final SparseArray result = mCachedValuesHelper.getCachedAppsStorageResult(); assertThat(result).isNull(); } diff --git a/tests/robotests/src/com/android/settings/deviceinfo/storage/SecondaryUserControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/storage/SecondaryUserControllerTest.java index f0683979b2d..804c6dcb3d9 100644 --- a/tests/robotests/src/com/android/settings/deviceinfo/storage/SecondaryUserControllerTest.java +++ b/tests/robotests/src/com/android/settings/deviceinfo/storage/SecondaryUserControllerTest.java @@ -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 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 userInfos = new ArrayList<>(); + final ArrayList userInfos = new ArrayList<>(); userInfos.add(mPrimaryUser); when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser); when(mUserManager.getUsers()).thenReturn(userInfos); - List controllers = + final List controllers = SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager); assertThat(controllers).hasSize(1); @@ -123,15 +124,15 @@ public class SecondaryUserControllerTest { @Test public void secondaryUserAddedIfHasDistinctId() throws Exception { - ArrayList userInfos = new ArrayList<>(); - UserInfo secondaryUser = new UserInfo(); + final ArrayList 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 controllers = + final List controllers = SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager); assertThat(controllers).hasSize(1); @@ -140,15 +141,15 @@ public class SecondaryUserControllerTest { @Test public void profilesOfPrimaryUserAreNotIgnored() throws Exception { - ArrayList userInfos = new ArrayList<>(); - UserInfo secondaryUser = new UserInfo(); + final ArrayList 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 controllers = + final List 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 result = new SparseArray<>(); + final SparseArray result = new SparseArray<>(); userResult.externalStats = new StorageStatsSource.ExternalStorageStats( MEGABYTE_IN_BYTES * 30, @@ -175,23 +176,23 @@ public class SecondaryUserControllerTest { mController.handleResult(result); final ArgumentCaptor 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 userInfos = new ArrayList<>(); + final ArrayList 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 controllers = + final List controllers = SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager); assertThat(controllers).hasSize(1); @@ -201,34 +202,29 @@ public class SecondaryUserControllerTest { @Test public void iconCallbackChangesPreferenceIcon() throws Exception { - SparseArray 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 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 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 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 icons = new SparseArray<>(); + final UserIconDrawable drawable = mock(UserIconDrawable.class); mPrimaryUser.name = TEST_NAME; mPrimaryUser.id = 10; + icons.put(mPrimaryUser.id, drawable); mController.handleUserIcons(icons); diff --git a/tests/robotests/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceControllerTest.java index e6c161efd3f..a1a48ae1b6a 100644 --- a/tests/robotests/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceControllerTest.java @@ -28,7 +28,10 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import android.app.Activity; import android.app.Fragment; +import android.app.FragmentManager; +import android.app.FragmentTransaction; import android.content.Context; import android.content.Intent; import android.graphics.drawable.Drawable; @@ -67,14 +70,20 @@ import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; @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 { private Context mContext; private VolumeInfo mVolume; - @Mock(answer = Answers.RETURNS_DEEP_STUBS) + @Mock private Fragment mFragment; @Mock private StorageVolumeProvider mSvp; + @Mock + private Activity mActivity; + @Mock + private FragmentManager mFragmentManager; + @Mock + private FragmentTransaction mFragmentTransaction; private StorageItemPreferenceController mController; private StorageItemPreference mPreference; private FakeFeatureFactory mFakeFeatureFactory; @@ -83,6 +92,9 @@ public class StorageItemPreferenceControllerTest { @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); + when(mFragment.getActivity()).thenReturn(mActivity); + when(mFragment.getFragmentManager()).thenReturn(mFragmentManager); + when(mFragmentManager.beginTransaction()).thenReturn(mFragmentTransaction); mContext = spy(RuntimeEnvironment.application.getApplicationContext()); FakeFeatureFactory.setupForTest(mContext); mFakeFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); @@ -94,7 +106,7 @@ public class StorageItemPreferenceControllerTest { mPreference = new StorageItemPreference(mContext); // Inflate the preference and the widget. - LayoutInflater inflater = LayoutInflater.from(mContext); + final LayoutInflater inflater = LayoutInflater.from(mContext); final View view = inflater.inflate( mPreference.getLayoutResource(), new LinearLayout(mContext), false); } @@ -116,10 +128,10 @@ public class StorageItemPreferenceControllerTest { mController.handlePreferenceTreeClick(mPreference); final ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(Intent.class); - verify(mFragment.getActivity()).startActivityAsUser(argumentCaptor.capture(), + verify(mActivity).startActivityAsUser(argumentCaptor.capture(), nullable(UserHandle.class)); - Intent intent = argumentCaptor.getValue(); + final Intent intent = argumentCaptor.getValue(); assertThat(intent.getAction()).isEqualTo(Intent.ACTION_MAIN); assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName()); assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)) @@ -136,7 +148,7 @@ public class StorageItemPreferenceControllerTest { final ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(Intent.class); verify(mFragment.getActivity()).startActivityAsUser(argumentCaptor.capture(), nullable(UserHandle.class)); - Intent intent = argumentCaptor.getValue(); + final Intent intent = argumentCaptor.getValue(); assertThat(intent.getAction()).isEqualTo(Intent.ACTION_MAIN); assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName()); @@ -164,7 +176,7 @@ public class StorageItemPreferenceControllerTest { verify(mFragment.getActivity()).startActivityAsUser(argumentCaptor.capture(), nullable(UserHandle.class)); - Intent intent = argumentCaptor.getValue(); + final Intent intent = argumentCaptor.getValue(); assertThat(intent.getAction()).isEqualTo(Intent.ACTION_MAIN); assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName()); assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)).isEqualTo( @@ -273,14 +285,14 @@ public class StorageItemPreferenceControllerTest { @Test public void testMeasurementCompletedUpdatesPreferences() { - StorageItemPreference audio = new StorageItemPreference(mContext); - StorageItemPreference image = new StorageItemPreference(mContext); - StorageItemPreference games = new StorageItemPreference(mContext); - StorageItemPreference movies = new StorageItemPreference(mContext); - StorageItemPreference apps = new StorageItemPreference(mContext); - StorageItemPreference system = new StorageItemPreference(mContext); - StorageItemPreference files = new StorageItemPreference(mContext); - PreferenceScreen screen = mock(PreferenceScreen.class); + final StorageItemPreference audio = new StorageItemPreference(mContext); + final StorageItemPreference image = new StorageItemPreference(mContext); + final StorageItemPreference games = new StorageItemPreference(mContext); + final StorageItemPreference movies = new StorageItemPreference(mContext); + final StorageItemPreference apps = new StorageItemPreference(mContext); + final StorageItemPreference system = new StorageItemPreference(mContext); + final StorageItemPreference files = new StorageItemPreference(mContext); + final PreferenceScreen screen = mock(PreferenceScreen.class); when(screen.findPreference( eq(StorageItemPreferenceController.AUDIO_KEY))).thenReturn(audio); when(screen.findPreference( @@ -298,7 +310,7 @@ public class StorageItemPreferenceControllerTest { mController.displayPreference(screen); 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.videoAppsSize = MEGABYTE_IN_BYTES * 160; result.musicAppsSize = MEGABYTE_IN_BYTES * 40; @@ -310,7 +322,7 @@ public class StorageItemPreferenceControllerTest { MEGABYTE_IN_BYTES * 150, // video MEGABYTE_IN_BYTES * 200, 0); // image - SparseArray results = new SparseArray<>(); + final SparseArray results = new SparseArray<>(); results.put(0, result); mController.onLoadFinished(results, 0); @@ -324,21 +336,21 @@ public class StorageItemPreferenceControllerTest { @Test public void settingUserIdAppliesNewIcons() { - StorageItemPreference audio = spy(new StorageItemPreference(mContext)); + final StorageItemPreference audio = spy(new StorageItemPreference(mContext)); 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); - StorageItemPreference image = spy(new StorageItemPreference(mContext)); + final StorageItemPreference image = spy(new StorageItemPreference(mContext)); 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); - StorageItemPreference apps = spy(new StorageItemPreference(mContext)); + final StorageItemPreference apps = spy(new StorageItemPreference(mContext)); 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); - StorageItemPreference files = spy(new StorageItemPreference(mContext)); + final StorageItemPreference files = spy(new StorageItemPreference(mContext)); files.setIcon(R.drawable.ic_folder_vd_theme_24); - PreferenceScreen screen = mock(PreferenceScreen.class); + final PreferenceScreen screen = mock(PreferenceScreen.class); when(screen.findPreference( eq(StorageItemPreferenceController.AUDIO_KEY))).thenReturn(audio); when(screen.findPreference( @@ -368,13 +380,13 @@ public class StorageItemPreferenceControllerTest { @Test public void displayPreference_dontHideFilePreferenceWhenEmulatedInternalStorageUsed() { - StorageItemPreference audio = new StorageItemPreference(mContext); - StorageItemPreference image = new StorageItemPreference(mContext); - StorageItemPreference games = new StorageItemPreference(mContext); - StorageItemPreference apps = new StorageItemPreference(mContext); - StorageItemPreference system = new StorageItemPreference(mContext); - StorageItemPreference files = new StorageItemPreference(mContext); - PreferenceScreen screen = mock(PreferenceScreen.class); + final StorageItemPreference audio = new StorageItemPreference(mContext); + final StorageItemPreference image = new StorageItemPreference(mContext); + final StorageItemPreference games = new StorageItemPreference(mContext); + final StorageItemPreference apps = new StorageItemPreference(mContext); + final StorageItemPreference system = new StorageItemPreference(mContext); + final StorageItemPreference files = new StorageItemPreference(mContext); + final PreferenceScreen screen = mock(PreferenceScreen.class); when(screen.findPreference(eq(StorageItemPreferenceController.AUDIO_KEY))) .thenReturn(audio); when(screen.findPreference(eq(StorageItemPreferenceController.PHOTO_KEY))) @@ -397,13 +409,13 @@ public class StorageItemPreferenceControllerTest { @Test public void displayPreference_hideFilePreferenceWhenEmulatedStorageUnreadable() { - StorageItemPreference audio = new StorageItemPreference(mContext); - StorageItemPreference image = new StorageItemPreference(mContext); - StorageItemPreference games = new StorageItemPreference(mContext); - StorageItemPreference apps = new StorageItemPreference(mContext); - StorageItemPreference system = new StorageItemPreference(mContext); - StorageItemPreference files = new StorageItemPreference(mContext); - PreferenceScreen screen = mock(PreferenceScreen.class); + final StorageItemPreference audio = new StorageItemPreference(mContext); + final StorageItemPreference image = new StorageItemPreference(mContext); + final StorageItemPreference games = new StorageItemPreference(mContext); + final StorageItemPreference apps = new StorageItemPreference(mContext); + final StorageItemPreference system = new StorageItemPreference(mContext); + final StorageItemPreference files = new StorageItemPreference(mContext); + final PreferenceScreen screen = mock(PreferenceScreen.class); when(screen.findPreference(eq(StorageItemPreferenceController.AUDIO_KEY))) .thenReturn(audio); when(screen.findPreference(eq(StorageItemPreferenceController.PHOTO_KEY))) @@ -426,13 +438,13 @@ public class StorageItemPreferenceControllerTest { @Test public void displayPreference_hideFilePreferenceWhenNoEmulatedInternalStorage() { - StorageItemPreference audio = new StorageItemPreference(mContext); - StorageItemPreference image = new StorageItemPreference(mContext); - StorageItemPreference games = new StorageItemPreference(mContext); - StorageItemPreference apps = new StorageItemPreference(mContext); - StorageItemPreference system = new StorageItemPreference(mContext); - StorageItemPreference files = new StorageItemPreference(mContext); - PreferenceScreen screen = mock(PreferenceScreen.class); + final StorageItemPreference audio = new StorageItemPreference(mContext); + final StorageItemPreference image = new StorageItemPreference(mContext); + final StorageItemPreference games = new StorageItemPreference(mContext); + final StorageItemPreference apps = new StorageItemPreference(mContext); + final StorageItemPreference system = new StorageItemPreference(mContext); + final StorageItemPreference files = new StorageItemPreference(mContext); + final PreferenceScreen screen = mock(PreferenceScreen.class); when(screen.findPreference(eq(StorageItemPreferenceController.AUDIO_KEY))) .thenReturn(audio); when(screen.findPreference(eq(StorageItemPreferenceController.PHOTO_KEY))) @@ -454,13 +466,13 @@ public class StorageItemPreferenceControllerTest { @Test public void displayPreference_updateFilePreferenceToHideAfterSettingVolume() { - StorageItemPreference audio = new StorageItemPreference(mContext); - StorageItemPreference image = new StorageItemPreference(mContext); - StorageItemPreference games = new StorageItemPreference(mContext); - StorageItemPreference apps = new StorageItemPreference(mContext); - StorageItemPreference system = new StorageItemPreference(mContext); - StorageItemPreference files = new StorageItemPreference(mContext); - PreferenceScreen screen = mock(PreferenceScreen.class); + final StorageItemPreference audio = new StorageItemPreference(mContext); + final StorageItemPreference image = new StorageItemPreference(mContext); + final StorageItemPreference games = new StorageItemPreference(mContext); + final StorageItemPreference apps = new StorageItemPreference(mContext); + final StorageItemPreference system = new StorageItemPreference(mContext); + final StorageItemPreference files = new StorageItemPreference(mContext); + final PreferenceScreen screen = mock(PreferenceScreen.class); when(screen.findPreference(eq(StorageItemPreferenceController.AUDIO_KEY))) .thenReturn(audio); when(screen.findPreference(eq(StorageItemPreferenceController.PHOTO_KEY))) @@ -486,13 +498,13 @@ public class StorageItemPreferenceControllerTest { @Test public void displayPreference_updateFilePreferenceToShowAfterSettingVolume() { - StorageItemPreference audio = new StorageItemPreference(mContext); - StorageItemPreference image = new StorageItemPreference(mContext); - StorageItemPreference games = new StorageItemPreference(mContext); - StorageItemPreference apps = new StorageItemPreference(mContext); - StorageItemPreference system = new StorageItemPreference(mContext); - StorageItemPreference files = new StorageItemPreference(mContext); - PreferenceScreen screen = mock(PreferenceScreen.class); + final StorageItemPreference audio = new StorageItemPreference(mContext); + final StorageItemPreference image = new StorageItemPreference(mContext); + final StorageItemPreference games = new StorageItemPreference(mContext); + final StorageItemPreference apps = new StorageItemPreference(mContext); + final StorageItemPreference system = new StorageItemPreference(mContext); + final StorageItemPreference files = new StorageItemPreference(mContext); + final PreferenceScreen screen = mock(PreferenceScreen.class); when(screen.findPreference(eq(StorageItemPreferenceController.AUDIO_KEY))) .thenReturn(audio); when(screen.findPreference(eq(StorageItemPreferenceController.PHOTO_KEY))) diff --git a/tests/robotests/src/com/android/settings/deviceinfo/storage/StorageSummaryDonutPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/storage/StorageSummaryDonutPreferenceControllerTest.java index 8ebfb89f82e..1c1824dd3fe 100644 --- a/tests/robotests/src/com/android/settings/deviceinfo/storage/StorageSummaryDonutPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/deviceinfo/storage/StorageSummaryDonutPreferenceControllerTest.java @@ -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,11 +57,11 @@ 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 -}) + SettingsShadowResources.class, + SettingsShadowResources.SettingsShadowTheme.class + }) public class StorageSummaryDonutPreferenceControllerTest { private Context mContext; private StorageSummaryDonutPreferenceController mController; @@ -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); diff --git a/tests/robotests/src/com/android/settings/deviceinfo/storage/UserProfileControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/storage/UserProfileControllerTest.java index 8da52608838..98e796e8d25 100644 --- a/tests/robotests/src/com/android/settings/deviceinfo/storage/UserProfileControllerTest.java +++ b/tests/robotests/src/com/android/settings/deviceinfo/storage/UserProfileControllerTest.java @@ -19,6 +19,7 @@ package com.android.settings.deviceinfo.storage; import static com.google.common.truth.Truth.assertThat; 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.verify; import static org.mockito.Mockito.when; @@ -53,7 +54,7 @@ import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; @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 { private static final String TEST_NAME = "Fred"; @@ -82,7 +83,7 @@ public class UserProfileControllerTest { public void controllerAddsPrimaryProfilePreference() throws Exception { final ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(Preference.class); verify(mScreen).addPreference(argumentCaptor.capture()); - Preference preference = argumentCaptor.getValue(); + final Preference preference = argumentCaptor.getValue(); assertThat(preference.getTitle()).isEqualTo(TEST_NAME); assertThat(preference.getKey()).isEqualTo("pref_profile_10"); @@ -93,12 +94,12 @@ public class UserProfileControllerTest { final ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(Preference.class); verify(mScreen).addPreference(argumentCaptor.capture()); - Preference preference = argumentCaptor.getValue(); + final Preference preference = argumentCaptor.getValue(); assertThat(mController.handlePreferenceTreeClick(preference)).isTrue(); final ArgumentCaptor intentCaptor = ArgumentCaptor.forClass(Intent.class); verify(mContext).startActivity(intentCaptor.capture()); - Intent intent = intentCaptor.getValue(); + final Intent intent = intentCaptor.getValue(); assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName()); assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)).isEqualTo( StorageProfileFragment.class.getName()); @@ -106,8 +107,8 @@ public class UserProfileControllerTest { @Test public void acceptingResultUpdatesPreferenceSize() throws Exception { - SparseArray result = new SparseArray<>(); - StorageAsyncLoader.AppsStorageResult userResult = + final SparseArray result = new SparseArray<>(); + final StorageAsyncLoader.AppsStorageResult userResult = new StorageAsyncLoader.AppsStorageResult(); userResult.externalStats = new StorageStatsSource.ExternalStorageStats( @@ -120,25 +121,23 @@ public class UserProfileControllerTest { mController.handleResult(result); final ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(Preference.class); verify(mScreen).addPreference(argumentCaptor.capture()); - Preference preference = argumentCaptor.getValue(); + final Preference preference = argumentCaptor.getValue(); assertThat(preference.getSummary()).isEqualTo("0.10 GB"); } @Test public void iconCallbackChangesPreferenceIcon() throws Exception { - SparseArray 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 icons = new SparseArray<>(); + final UserIconDrawable drawable = mock(UserIconDrawable.class); + when(drawable.mutate()).thenReturn(drawable); + icons.put(mPrimaryProfile.id, drawable); mController.handleUserIcons(icons); final ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(Preference.class); verify(mScreen).addPreference(argumentCaptor.capture()); - Preference preference = argumentCaptor.getValue(); + final Preference preference = argumentCaptor.getValue(); assertThat(preference.getIcon()).isEqualTo(drawable); } }