Resolve locale not update issues in the chart view

- read locale from configuration rather than Locale.getDefault
- refine 12-24 format to align the current status bar short style
- resolve locale change not update chart percentage label
- extend timestamp label in the chart graph from 4 to 5 labels

Bug: 190150515
Bug: 190422902
Bug: 190226837
Test: make SettingsRoboTests
Change-Id: I5347964900123a6d112dbc37c2af87eb7d73f1d2
This commit is contained in:
ykhung
2021-06-09 11:41:33 +08:00
parent ae1797ec46
commit 2a75186e4b
7 changed files with 104 additions and 50 deletions

View File

@@ -35,6 +35,7 @@ import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.LocaleList;
import android.text.format.DateUtils;
import android.util.Pair;
@@ -100,6 +101,8 @@ public final class BatteryChartPreferenceControllerTest {
mFeatureFactory = FakeFeatureFactory.setupForTest();
mMetricsFeatureProvider = mFeatureFactory.metricsFeatureProvider;
mContext = spy(RuntimeEnvironment.application);
mContext.getResources().getConfiguration().setLocales(
new LocaleList(new Locale("en_US")));
mBatteryChartPreferenceController = createController();
mBatteryChartPreferenceController.mPrefContext = mContext;
mBatteryChartPreferenceController.mAppListPrefGroup = mAppListGroup;
@@ -573,14 +576,12 @@ public final class BatteryChartPreferenceControllerTest {
// Verifies the title in the preference group.
verify(mBatteryChartPreferenceController.mAppListPrefGroup)
.setTitle(captor.capture());
assertThat(captor.getValue())
.isEqualTo("App usage for 4 pm - 7 am");
assertThat(captor.getValue()).isEqualTo("App usage for 4 - 7");
// Verifies the title in the expandable divider.
captor = ArgumentCaptor.forClass(String.class);
verify(mBatteryChartPreferenceController.mExpandDividerPreference)
.setTitle(captor.capture());
assertThat(captor.getValue())
.isEqualTo("System usage for 4 pm - 7 am");
assertThat(captor.getValue()).isEqualTo("System usage for 4 - 7");
}
@Test
@@ -716,7 +717,8 @@ public final class BatteryChartPreferenceControllerTest {
private void setUpBatteryHistoryKeys() {
mBatteryChartPreferenceController.mBatteryHistoryKeys =
new long[] {1619196786769L, 0L, 1619247636826L};
ConvertUtils.utcToLocalTimeHour(/*timestamp=*/ 0, /*is24HourFormat=*/ false);
ConvertUtils.utcToLocalTimeHour(
mContext, /*timestamp=*/ 0, /*is24HourFormat=*/ false);
// Simulates the locale in GMT.
ConvertUtils.sSimpleDateFormatForHour
.setTimeZone(TimeZone.getTimeZone("GMT"));

View File

@@ -27,6 +27,7 @@ import static org.mockito.Mockito.when;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Context;
import android.os.LocaleList;
import android.view.accessibility.AccessibilityManager;
import com.android.settings.testutils.FakeFeatureFactory;
@@ -41,6 +42,7 @@ import org.robolectric.RuntimeEnvironment;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Locale;
import java.util.TimeZone;
@RunWith(RobolectricTestRunner.class)
@@ -60,6 +62,8 @@ public final class BatteryChartViewTest {
mFeatureFactory = FakeFeatureFactory.setupForTest();
mPowerUsageFeatureProvider = mFeatureFactory.powerUsageFeatureProvider;
mContext = spy(RuntimeEnvironment.application);
mContext.getResources().getConfiguration().setLocales(
new LocaleList(new Locale("en_US")));
mBatteryChartView = new BatteryChartView(mContext);
doReturn(mockAccessibilityManager).when(mContext)
.getSystemService(AccessibilityManager.class);
@@ -234,11 +238,11 @@ public final class BatteryChartViewTest {
final long timestamp = 1619196786769L;
ConvertUtils.sSimpleDateFormatForHour = null;
// Invokes the method first to create the SimpleDateFormat.
ConvertUtils.utcToLocalTimeHour(/*timestamp=*/ 0, /*is24HourFormat=*/ false);
ConvertUtils.utcToLocalTimeHour(
mContext, /*timestamp=*/ 0, /*is24HourFormat=*/ false);
ConvertUtils.sSimpleDateFormatForHour
.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
final String[] expectedTimestamps =
new String[] {"9 am", "5 pm", "1 am", "9 am"};
final String[] expectedTimestamps = new String[] {"00", "06", "12", "18", "00"};
mBatteryChartView.setLatestTimestamp(timestamp);

View File

@@ -24,6 +24,7 @@ import android.content.ContentValues;
import android.content.Context;
import android.os.BatteryManager;
import android.os.BatteryUsageStats;
import android.os.LocaleList;
import android.os.UserHandle;
import com.android.settings.testutils.FakeFeatureFactory;
@@ -315,6 +316,7 @@ public final class ConvertUtilsTest {
.isEqualTo(entry.mConsumePower * ratio);
}
@Test
public void testUtcToLocalTime_returnExpectedResult() {
ConvertUtils.sZoneId = null;
ConvertUtils.sLocale = null;
@@ -322,48 +324,76 @@ public final class ConvertUtilsTest {
final String expectedZoneId = "America/Los_Angeles";
ConvertUtils.sSimpleDateFormat = null;
// Invokes the method first to create the SimpleDateFormat.
ConvertUtils.utcToLocalTime(/*timestamp=*/ 0);
ConvertUtils.utcToLocalTime(mContext, /*timestamp=*/ 0);
ConvertUtils.sSimpleDateFormat
.setTimeZone(TimeZone.getTimeZone(expectedZoneId));
mContext.getResources().getConfiguration().setLocales(
new LocaleList(new Locale("en_US")));
assertThat(ConvertUtils.utcToLocalTime(timestamp))
.isEqualTo("Apr 23,2021 09:53:06");
assertThat(ConvertUtils.utcToLocalTime(mContext, timestamp))
.isEqualTo("Apr 24,2021 00:53:06");
assertThat(ConvertUtils.sZoneId).isNotEqualTo(expectedZoneId);
assertThat(ConvertUtils.sLocale).isEqualTo(Locale.getDefault());
assertThat(ConvertUtils.sLocale).isEqualTo(new Locale("en_US"));
}
@Test
public void testUtcToLocalTimeHour_12HourFormat_returnExpectedResult() {
ConvertUtils.sZoneIdForHour = null;
ConvertUtils.sLocaleForHour = null;
final long timestamp = 1619196786769L;
final long timestamp = 1619000086769L;
final String expectedZoneId = "America/Los_Angeles";
ConvertUtils.sSimpleDateFormatForHour = null;
// Invokes the method first to create the SimpleDateFormat.
ConvertUtils.utcToLocalTimeHour(/*timestamp=*/ 0, /*is24HourFormat=*/ false);
ConvertUtils.utcToLocalTimeHour(
mContext, /*timestamp=*/ 0, /*is24HourFormat=*/ false);
ConvertUtils.sSimpleDateFormatForHour
.setTimeZone(TimeZone.getTimeZone(expectedZoneId));
mContext.getResources().getConfiguration().setLocales(
new LocaleList(new Locale("en_US")));
assertThat(ConvertUtils.utcToLocalTimeHour(
timestamp, /*is24HourFormat=*/ false)).isEqualTo("9 am");
mContext, timestamp, /*is24HourFormat=*/ false)).isEqualTo("6");
assertThat(ConvertUtils.sZoneIdForHour).isNotEqualTo(expectedZoneId);
assertThat(ConvertUtils.sLocaleForHour).isEqualTo(Locale.getDefault());
assertThat(ConvertUtils.sLocaleForHour).isEqualTo(new Locale("en_US"));
}
@Test
public void testUtcToLocalTimeHour_24HourFormat_returnExpectedResult() {
ConvertUtils.sZoneIdForHour = null;
ConvertUtils.sLocaleForHour = null;
final long timestamp = 1619196786769L;
final long timestamp = 1619000086769L;
final String expectedZoneId = "America/Los_Angeles";
ConvertUtils.sSimpleDateFormatForHour = null;
// Invokes the method first to create the SimpleDateFormat.
ConvertUtils.utcToLocalTimeHour(/*timestamp=*/ 0, /*is24HourFormat=*/ true);
ConvertUtils.utcToLocalTimeHour(
mContext, /*timestamp=*/ 0, /*is24HourFormat=*/ false);
ConvertUtils.sSimpleDateFormatForHour
.setTimeZone(TimeZone.getTimeZone(expectedZoneId));
mContext.getResources().getConfiguration().setLocales(
new LocaleList(new Locale("en_US")));
assertThat(ConvertUtils.utcToLocalTimeHour(
timestamp, /*is24HourFormat=*/ true)).isEqualTo("09");
mContext, timestamp, /*is24HourFormat=*/ true)).isEqualTo("18");
assertThat(ConvertUtils.sZoneIdForHour).isNotEqualTo(expectedZoneId);
assertThat(ConvertUtils.sLocaleForHour).isEqualTo(Locale.getDefault());
assertThat(ConvertUtils.sLocaleForHour).isEqualTo(new Locale("en_US"));
}
@Test
public void getLocale_nullContext_returnDefaultLocale() {
assertThat(ConvertUtils.getLocale(/*context=*/ null))
.isEqualTo(Locale.getDefault());
}
@Test
public void getLocale_nullLocaleList_returnDefaultLocale() {
mContext.getResources().getConfiguration().setLocales(null);
assertThat(ConvertUtils.getLocale(mContext)).isEqualTo(Locale.getDefault());
}
@Test
public void getLocale_emptyLocaleList_returnDefaultLocale() {
mContext.getResources().getConfiguration().setLocales(new LocaleList());
assertThat(ConvertUtils.getLocale(mContext)).isEqualTo(Locale.getDefault());
}
private static BatteryHistEntry createBatteryHistEntry(