Always use GB as the unit in Storage Settings.

Bug: 36901322
Test: Settings unit tests

Change-Id: I08c711db6a271522942a01d1adf3e8e5223010f7
This commit is contained in:
Daniel Nishi
2017-04-19 11:37:02 -07:00
parent 0cdade8a78
commit 63a11c56de
8 changed files with 327 additions and 37 deletions

View File

@@ -15,7 +15,7 @@
*/
package com.android.settings.deviceinfo;
import static com.android.settings.TestUtils.KILOBYTE;
import static com.android.settings.utils.FileSizeFormatter.MEGABYTE_IN_BYTES;
import static com.google.common.truth.Truth.assertThat;
@@ -54,8 +54,8 @@ public class StorageItemPreferenceTest {
@Test
public void testAfterLoad() {
mPreference.setStorageSize(KILOBYTE, KILOBYTE * 10);
assertThat(((String) mPreference.getSummary())).isEqualTo("1.00KB");
mPreference.setStorageSize(MEGABYTE_IN_BYTES * 10, MEGABYTE_IN_BYTES * 100);
assertThat(((String) mPreference.getSummary())).isEqualTo("0.01GB");
}
@Test
@@ -66,7 +66,7 @@ public class StorageItemPreferenceTest {
(ProgressBar) holder.itemView.findViewById(android.R.id.progress);
mPreference.onBindViewHolder(holder);
mPreference.setStorageSize(KILOBYTE, KILOBYTE * 10);
mPreference.setStorageSize(MEGABYTE_IN_BYTES, MEGABYTE_IN_BYTES * 10);
assertThat(progressBar.getProgress()).isEqualTo(10);
}

View File

@@ -17,6 +17,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.Matchers.anyString;
import static org.mockito.Mockito.verify;
@@ -96,13 +97,13 @@ public class SecondaryUserControllerTest {
public void controllerUpdatesSummaryOfNewPreference() throws Exception {
mPrimaryUser.name = TEST_NAME;
mController.displayPreference(mScreen);
mController.setSize(10L);
mController.setSize(MEGABYTE_IN_BYTES * 10);
final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class);
verify(mGroup).addPreference(argumentCaptor.capture());
Preference preference = argumentCaptor.getValue();
assertThat(preference.getSummary()).isEqualTo("10.00B");
assertThat(preference.getSummary()).isEqualTo("0.01GB");
}
@Test
@@ -162,7 +163,12 @@ public class SecondaryUserControllerTest {
StorageAsyncLoader.AppsStorageResult userResult =
new StorageAsyncLoader.AppsStorageResult();
SparseArray<StorageAsyncLoader.AppsStorageResult> result = new SparseArray<>();
userResult.externalStats = new StorageStatsSource.ExternalStorageStats(99, 33, 33, 33);
userResult.externalStats =
new StorageStatsSource.ExternalStorageStats(
MEGABYTE_IN_BYTES * 30,
MEGABYTE_IN_BYTES * 10,
MEGABYTE_IN_BYTES * 10,
MEGABYTE_IN_BYTES * 10);
result.put(10, userResult);
mController.handleResult(result);
@@ -170,7 +176,7 @@ public class SecondaryUserControllerTest {
verify(mGroup).addPreference(argumentCaptor.capture());
Preference preference = argumentCaptor.getValue();
assertThat(preference.getSummary()).isEqualTo("99.00B");
assertThat(preference.getSummary()).isEqualTo("0.03GB");
}
@Test

View File

@@ -15,9 +15,9 @@
*/
package com.android.settings.deviceinfo.storage;
import static com.android.settings.TestUtils.KILOBYTE;
import static com.google.common.truth.Truth.assertThat;
import static com.android.settings.utils.FileSizeFormatter.MEGABYTE_IN_BYTES;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
@@ -28,6 +28,7 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
@@ -50,9 +51,11 @@ import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.deviceinfo.PrivateVolumeSettings;
import com.android.settings.deviceinfo.StorageItemPreference;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.SettingsShadowResources;
import com.android.settingslib.applications.StorageStatsSource;
import com.android.settingslib.deviceinfo.StorageVolumeProvider;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -80,6 +83,8 @@ public class StorageItemPreferenceControllerTest {
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
SettingsShadowResources.overrideResource("android:string/fileSizeSuffix", "%1$s %2$s");
SettingsShadowResources.overrideResource("android:string/gigabyteShort", "GB");
mContext = spy(RuntimeEnvironment.application.getApplicationContext());
FakeFeatureFactory.setupForTest(mContext);
mFakeFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
@@ -96,6 +101,11 @@ public class StorageItemPreferenceControllerTest {
mPreference.getLayoutResource(), new LinearLayout(mContext), false);
}
@After
public void tearDown() {
SettingsShadowResources.reset();
}
@Test
public void testUpdateStateWithInitialState() {
assertThat(mPreference.getSummary().toString()).isEqualTo(
@@ -243,28 +253,29 @@ public class StorageItemPreferenceControllerTest {
eq(StorageItemPreferenceController.FILES_KEY))).thenReturn(files);
mController.displayPreference(screen);
mController.setUsedSize(KILOBYTE * 200); // There should 87kB attributed.
mController.setUsedSize(MEGABYTE_IN_BYTES * 970); // There should 870MB attributed.
StorageAsyncLoader.AppsStorageResult result = new StorageAsyncLoader.AppsStorageResult();
result.gamesSize = KILOBYTE * 8;
result.videoAppsSize = KILOBYTE * 16;
result.musicAppsSize = KILOBYTE * 4;
result.otherAppsSize = KILOBYTE * 9;
result.systemSize = KILOBYTE * 10; // This value is ignored and overriden now.
result.externalStats = new StorageStatsSource.ExternalStorageStats(
KILOBYTE * 50, // total
KILOBYTE * 10, // audio
KILOBYTE * 15, // video
KILOBYTE * 20); // image
result.gamesSize = MEGABYTE_IN_BYTES * 80;
result.videoAppsSize = MEGABYTE_IN_BYTES * 160;
result.musicAppsSize = MEGABYTE_IN_BYTES * 40;
result.otherAppsSize = MEGABYTE_IN_BYTES * 90;
result.systemSize = MEGABYTE_IN_BYTES * 100; // This value is ignored and overridden now.
result.externalStats =
new StorageStatsSource.ExternalStorageStats(
MEGABYTE_IN_BYTES * 500, // total
MEGABYTE_IN_BYTES * 100, // audio
MEGABYTE_IN_BYTES * 150, // video
MEGABYTE_IN_BYTES * 200); // image
mController.onLoadFinished(result);
assertThat(audio.getSummary().toString()).isEqualTo("14.00KB"); // 4KB apps + 10KB files
assertThat(image.getSummary().toString()).isEqualTo("35.00KB"); // 15KB video + 20KB images
assertThat(games.getSummary().toString()).isEqualTo("8.00KB");
assertThat(movies.getSummary().toString()).isEqualTo("16.00KB");
assertThat(apps.getSummary().toString()).isEqualTo("9.00KB");
assertThat(system.getSummary().toString()).isEqualTo("113KB");
assertThat(files.getSummary().toString()).isEqualTo("5.00KB");
assertThat(audio.getSummary().toString()).isEqualTo("0.14GB");
assertThat(image.getSummary().toString()).isEqualTo("0.35GB");
assertThat(games.getSummary().toString()).isEqualTo("0.08GB");
assertThat(movies.getSummary().toString()).isEqualTo("0.16GB");
assertThat(apps.getSummary().toString()).isEqualTo("0.09GB");
assertThat(system.getSummary().toString()).isEqualTo("0.10GB");
assertThat(files.getSummary().toString()).isEqualTo("0.05GB");
}
@Test

View File

@@ -17,6 +17,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.spy;
import static org.mockito.Mockito.verify;
@@ -108,7 +109,12 @@ public class UserProfileControllerTest {
SparseArray<StorageAsyncLoader.AppsStorageResult> result = new SparseArray<>();
StorageAsyncLoader.AppsStorageResult userResult =
new StorageAsyncLoader.AppsStorageResult();
userResult.externalStats = new StorageStatsSource.ExternalStorageStats(99, 33, 33, 33);
userResult.externalStats =
new StorageStatsSource.ExternalStorageStats(
99 * MEGABYTE_IN_BYTES,
33 * MEGABYTE_IN_BYTES,
33 * MEGABYTE_IN_BYTES,
33 * MEGABYTE_IN_BYTES);
result.put(10, userResult);
mController.handleResult(result);
@@ -116,7 +122,7 @@ public class UserProfileControllerTest {
verify(mScreen).addPreference(argumentCaptor.capture());
Preference preference = argumentCaptor.getValue();
assertThat(preference.getSummary()).isEqualTo("99.00B");
assertThat(preference.getSummary()).isEqualTo("0.10GB");
}
@Test

View File

@@ -1,5 +1,11 @@
package com.android.settings.testutils.shadow;
import static android.util.TypedValue.TYPE_REFERENCE;
import static org.robolectric.RuntimeEnvironment.application;
import static org.robolectric.Shadows.shadowOf;
import static org.robolectric.internal.Shadow.directlyOn;
import android.annotation.DimenRes;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
@@ -12,6 +18,7 @@ import android.support.annotation.ArrayRes;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.util.TypedValue;
import com.android.settings.R;
@@ -20,21 +27,19 @@ import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.internal.Shadow;
import org.robolectric.res.StyleData;
import org.robolectric.res.StyleResolver;
import org.robolectric.res.builder.XmlResourceParserImpl;
import org.robolectric.shadows.ShadowAssetManager;
import org.robolectric.shadows.ShadowResources;
import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.ReflectionHelpers.ClassParameter;
import org.w3c.dom.Node;
import java.util.List;
import java.util.Map;
import static android.util.TypedValue.TYPE_REFERENCE;
import static org.robolectric.Shadows.shadowOf;
import static org.robolectric.internal.Shadow.directlyOn;
/**
* Shadow Resources and Theme classes to handle resource references that Robolectric shadows cannot
* handle because they are too new or private.
@@ -45,6 +50,25 @@ public class SettingsShadowResources extends ShadowResources {
@RealObject
public Resources realResources;
private static SparseArray<Object> sResourceOverrides = new SparseArray<>();
public static void overrideResource(int id, Object value) {
sResourceOverrides.put(id, value);
}
public static void overrideResource(String name, Object value) {
final Resources res = application.getResources();
final int resId = res.getIdentifier(name, null, null);
if (resId == 0) {
throw new Resources.NotFoundException("Cannot override \"" + name + "\"");
}
overrideResource(resId, value);
}
public static void reset() {
sResourceOverrides.clear();
}
@Implementation
public int getDimensionPixelSize(@DimenRes int id) throws NotFoundException {
// Handle requests for private dimension resources,
@@ -93,6 +117,16 @@ public class SettingsShadowResources extends ShadowResources {
return directlyOn(realResources, Resources.class).getIntArray(id);
}
@Implementation
public String getString(int id) {
final Object override = sResourceOverrides.get(id);
if (override instanceof String) {
return (String) override;
}
return Shadow.directlyOn(
realResources, Resources.class, "getString", ClassParameter.from(int.class, id));
}
@Implements(Theme.class)
public static class SettingsShadowTheme extends ShadowTheme {

View File

@@ -0,0 +1,109 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.utils;
import static com.android.settings.utils.FileSizeFormatter.GIGABYTE_IN_BYTES;
import static com.android.settings.utils.FileSizeFormatter.MEGABYTE_IN_BYTES;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@SmallTest
@RunWith(AndroidJUnit4.class)
public class FileSizeFormatterTest {
private Context mContext;
@Before
public void setUp() throws Exception {
mContext = InstrumentationRegistry.getTargetContext();
}
@Test
public void formatFileSize_zero() throws Exception {
assertThat(
FileSizeFormatter.formatFileSize(
mContext,
0 /* size */,
com.android.internal.R.string.gigabyteShort,
GIGABYTE_IN_BYTES))
.isEqualTo("0.00 GB");
}
@Test
public void formatFileSize_smallSize() throws Exception {
assertThat(
FileSizeFormatter.formatFileSize(
mContext,
MEGABYTE_IN_BYTES * 11 /* size */,
com.android.internal.R.string.gigabyteShort,
GIGABYTE_IN_BYTES))
.isEqualTo("0.01 GB");
}
@Test
public void formatFileSize_lessThanOneSize() throws Exception {
assertThat(
FileSizeFormatter.formatFileSize(
mContext,
MEGABYTE_IN_BYTES * 155 /* size */,
com.android.internal.R.string.gigabyteShort,
GIGABYTE_IN_BYTES))
.isEqualTo("0.16 GB");
}
@Test
public void formatFileSize_greaterThanOneSize() throws Exception {
assertThat(
FileSizeFormatter.formatFileSize(
mContext,
MEGABYTE_IN_BYTES * 1551 /* size */,
com.android.internal.R.string.gigabyteShort,
GIGABYTE_IN_BYTES))
.isEqualTo("1.6 GB");
}
@Test
public void formatFileSize_greaterThanTen() throws Exception {
// Should round down due to truncation
assertThat(
FileSizeFormatter.formatFileSize(
mContext,
GIGABYTE_IN_BYTES * 15 + MEGABYTE_IN_BYTES * 50 /* size */,
com.android.internal.R.string.gigabyteShort,
GIGABYTE_IN_BYTES))
.isEqualTo("15 GB");
}
@Test
public void formatFileSize_handlesNegativeFileSizes() throws Exception {
assertThat(
FileSizeFormatter.formatFileSize(
mContext,
MEGABYTE_IN_BYTES * -155 /* size */,
com.android.internal.R.string.gigabyteShort,
GIGABYTE_IN_BYTES))
.isEqualTo("-0.16 GB");
}
}