TimeFormatterUtil { public static String format(final Context context, final long milliseconds) { if (milliseconds < 0) { return ""; } if (milliseconds == 0) { return context.getString(R.string.msdkui_minutes, 0); } final int seconds = (int) (milliseconds / SECOND_IN_MILLISECONDS); final int days = seconds / (HOUR_IN_SECONDS * DAY_IN_HOURS); final int hours = seconds / HOUR_IN_SECONDS % DAY_IN_HOURS; if (days > 0) { if (hours > 0) { return context.getString(R.string.msdkui_days_hours, days, hours); } else { return context.getString(R.string.msdkui_days, days); } } final int minutes = (seconds / MINUTE_IN_SECONDS) % MINUTE_IN_SECONDS; if (hours > 0) { if (minutes > 0) { return context.getString(R.string.msdkui_hours_minutes, hours, minutes); } else { return context.getString(R.string.msdkui_hours, hours); } } if (minutes > 0) { return context.getString(R.string.msdkui_minutes, minutes); } return context.getString(R.string.msdkui_seconds, (seconds / 10) * 10); } private  TimeFormatterUtil(); static String format(final Context context, final long milliseconds); static String format(final Context context, final int seconds);  }
@Test public void testFormatWhenTimeIsLessThanZero() { String timeString = TimeFormatterUtil.format(getApplicationContext(), -1); assertThat(timeString, isEmptyString()); }
@Test public void testFormatWhenTimeIsZero() { String timeString = TimeFormatterUtil.format(getApplicationContext(), 0); assertThat(timeString, is("0 min")); }
@Test public void testFormatWhenTimeIsInSec() { final int min = 50; String timeString = TimeFormatterUtil.format(getApplicationContext(), min * SECOND_IN_MILLISECONDS); assertThat(timeString, is("50 s")); }
@Test public void testFormatWhenTimeIsInMin() { final int min = 10 * MINUTE_IN_SECONDS; String timeString = TimeFormatterUtil.format(getApplicationContext(), min * SECOND_IN_MILLISECONDS); assertThat(timeString, is("10 min")); }
@Test public void testFormatWhenTimeIsInHours() { final int min = 65 * MINUTE_IN_SECONDS; String timeString = TimeFormatterUtil.format(getApplicationContext(), min * SECOND_IN_MILLISECONDS); assertThat(timeString, is("1 h 5 min")); }
@Test public void testFormatWhenTimeIsInHour() { final int min = 60 * MINUTE_IN_SECONDS; String timeString = TimeFormatterUtil.format(getApplicationContext(), min * SECOND_IN_MILLISECONDS); assertThat(timeString, is("1 h")); }
@Test public void testFormatWhenTimeIsInDays() { final int min = DAY_IN_HOURS * 65 * MINUTE_IN_SECONDS; String timeString = TimeFormatterUtil.format(getApplicationContext(), min * SECOND_IN_MILLISECONDS); assertThat(timeString, is("1 d 2 h")); }
@Test public void testFormatWhenTimeIsInDay() { final int min = DAY_IN_HOURS * 60 * MINUTE_IN_SECONDS; String timeString = TimeFormatterUtil.format(getApplicationContext(), min * SECOND_IN_MILLISECONDS); assertThat(timeString, is("1 d")); }
@Test public void testFormatFromSeconds() { final int min = DAY_IN_HOURS * 60 * MINUTE_IN_SECONDS; String timeString = TimeFormatterUtil.format(getApplicationContext(), min); assertThat(timeString, is("1 d")); }