Define Utils.formatRelativeTime() and use it

Previously, relative times were formatted using formatElapsedTime()
(appending translations of "ago" to them), sometimes resulting in
grammatically hard-to-understand or unnatural localizations. Now we
use ICU's RelativeDateTimeFormatter, which uses grammatically correct
and natural localizations from CLDR data.

Bug: 64507689
Bug: 64605781
Bug: 64556849
Bug: 64550172
Test: make -j RunSettingsRoboTests
Change-Id: Ia2d098b190ab99e7748ef6f03b919f5c6174ba7d
This commit is contained in:
Roozbeh Pournader
2017-08-26 14:05:15 -07:00
parent ad3a7f4066
commit 4de9df2943
7 changed files with 153 additions and 26 deletions

View File

@@ -173,6 +173,105 @@ public class UtilsTest {
assertThat(ttsSpans[0].getType()).isEqualTo(TtsSpan.TYPE_MEASURE);
}
@Test
public void testFormatRelativeTime_WithSeconds_ShowSeconds() {
final double testMillis = 40 * DateUtils.SECOND_IN_MILLIS;
final String expectedTime = "40 sec. ago";
assertThat(Utils.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo(
expectedTime);
}
@Test
public void testFormatRelativeTime_NoSeconds_DoNotShowSeconds() {
final double testMillis = 40 * DateUtils.SECOND_IN_MILLIS;
final String expectedTime = "1 min. ago";
assertThat(Utils.formatRelativeTime(mContext, testMillis, false).toString()).isEqualTo(
expectedTime);
}
@Test
public void testFormatRelativeTime_LessThanTwoMinutes_withSeconds() {
final double testMillis = 119 * DateUtils.SECOND_IN_MILLIS;
final String expectedTime = "119 sec. ago";
assertThat(Utils.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo(
expectedTime);
}
@Test
public void testFormatRelativeTime_LessThanTwoMinutes_NoSeconds() {
final double testMillis = 119 * DateUtils.SECOND_IN_MILLIS;
final String expectedTime = "2 min. ago";
assertThat(Utils.formatRelativeTime(mContext, testMillis, false).toString()).isEqualTo(
expectedTime);
}
@Test
public void testFormatRelativeTime_TwoMinutes_withSeconds() {
final double testMillis = 2 * DateUtils.MINUTE_IN_MILLIS;
final String expectedTime = "2 min. ago";
assertThat(Utils.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo(
expectedTime);
}
@Test
public void testFormatRelativeTime_LessThanTwoHours_withSeconds() {
final double testMillis = 119 * DateUtils.MINUTE_IN_MILLIS;
final String expectedTime = "119 min. ago";
assertThat(Utils.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo(
expectedTime);
}
@Test
public void testFormatRelativeTime_TwoHours_withSeconds() {
final double testMillis = 2 * DateUtils.HOUR_IN_MILLIS;
final String expectedTime = "2 hr. ago";
assertThat(Utils.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo(
expectedTime);
}
@Test
public void testFormatRelativeTime_LessThanTwoDays_withSeconds() {
final double testMillis = 47 * DateUtils.HOUR_IN_MILLIS;
final String expectedTime = "47 hr. ago";
assertThat(Utils.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo(
expectedTime);
}
@Test
public void testFormatRelativeTime_TwoDays_withSeconds() {
final double testMillis = 2 * DateUtils.DAY_IN_MILLIS;
final String expectedTime = "2 days ago";
assertThat(Utils.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo(
expectedTime);
}
@Test
public void testFormatRelativeTime_FormatZero_WithSeconds() {
final double testMillis = 0;
final String expectedTime = "0 sec. ago";
assertThat(Utils.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo(
expectedTime);
}
@Test
public void testFormatRelativeTime_FormatZero_NoSeconds() {
final double testMillis = 0;
final String expectedTime = "0 min. ago";
assertThat(Utils.formatRelativeTime(mContext, testMillis, false).toString()).isEqualTo(
expectedTime);
}
@Test
public void testInitializeVolumeDoesntBreakOnNullVolume() {
VolumeInfo info = new VolumeInfo("id", 0, new DiskInfo("id", 0), "");

View File

@@ -249,8 +249,6 @@ public class RecentAppsPreferenceControllerTest {
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(stats);
when(mMockContext.getResources().getText(eq(R.string.recent_app_summary)))
.thenReturn(mContext.getResources().getText(R.string.recent_app_summary));
final Configuration configuration = new Configuration();
configuration.locale = Locale.US;
when(mMockContext.getResources().getConfiguration()).thenReturn(configuration);
@@ -258,7 +256,7 @@ public class RecentAppsPreferenceControllerTest {
mController = new RecentAppsPreferenceController(mMockContext, mAppState, null);
mController.displayPreference(mScreen);
verify(mCategory).addPreference(argThat(summaryMatches("0m ago")));
verify(mCategory).addPreference(argThat(summaryMatches("0 min. ago")));
}
private static ArgumentMatcher<Preference> summaryMatches(String expected) {

View File

@@ -411,18 +411,11 @@ public class PowerUsageSummaryTest {
@Test
public void testUpdateLastFullChargePreference_showCorrectSummary() {
final CharSequence formattedString = mRealContext.getText(
R.string.power_last_full_charge_summary);
final CharSequence timeSequence = Utils.formatElapsedTime(mRealContext,
TIME_SINCE_LAST_FULL_CHARGE_MS, false);
final CharSequence expectedSummary = TextUtils.expandTemplate(
formattedString, timeSequence);
doReturn(formattedString).when(mFragment).getText(R.string.power_last_full_charge_summary);
doReturn(mRealContext).when(mFragment).getContext();
mFragment.updateLastFullChargePreference(TIME_SINCE_LAST_FULL_CHARGE_MS);
assertThat(mLastFullChargePref.getSubtitle()).isEqualTo(expectedSummary);
assertThat(mLastFullChargePref.getSubtitle()).isEqualTo("2 hr. ago");
}
@Test