1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.text.format; 18 19 import static android.text.format.Formatter.FLAG_IEC_UNITS; 20 import static android.text.format.Formatter.FLAG_SI_UNITS; 21 22 import static org.junit.Assert.assertEquals; 23 24 import android.content.Context; 25 import android.content.res.Configuration; 26 import android.content.res.Resources; 27 import android.platform.test.annotations.Presubmit; 28 import android.support.test.InstrumentationRegistry; 29 import android.support.test.filters.SmallTest; 30 import android.support.test.runner.AndroidJUnit4; 31 import android.text.format.Formatter.BytesResult; 32 33 import org.junit.After; 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 38 import java.util.Locale; 39 40 @Presubmit 41 @SmallTest 42 @RunWith(AndroidJUnit4.class) 43 public class FormatterTest { 44 private Locale mOriginalLocale; 45 private Context mContext; 46 47 @Before 48 public void setup() { 49 mContext = InstrumentationRegistry.getContext(); 50 mOriginalLocale = mContext.getResources() 51 .getConfiguration().locale; 52 } 53 54 @After 55 public void tearDown() { 56 if (mOriginalLocale != null) { 57 setLocale(mOriginalLocale); 58 } 59 } 60 61 @Test 62 public void testFormatBytes() { 63 setLocale(Locale.US); 64 65 checkFormatBytes(0, true, "0", 0); 66 checkFormatBytes(0, false, "0", 0); 67 68 checkFormatBytes(1, true, "1", 1); 69 checkFormatBytes(1, false, "1", 1); 70 71 checkFormatBytes(12, true, "12", 12); 72 checkFormatBytes(12, false, "12", 12); 73 74 checkFormatBytes(123, true, "123", 123); 75 checkFormatBytes(123, false, "123", 123); 76 77 checkFormatBytes(900, true, "900", 900); 78 checkFormatBytes(900, false, "900", 900); 79 80 checkFormatBytes(901, true, "0.90", 900); 81 checkFormatBytes(901, false, "0.90", 900); 82 83 checkFormatBytes(912, true, "0.91", 910); 84 checkFormatBytes(912, false, "0.91", 910); 85 86 checkFormatBytes(9123, true, "9.1", 9100); 87 checkFormatBytes(9123, false, "9.12", 9120); 88 89 checkFormatBytes(9123456, true, "9.1", 9100000); 90 checkFormatBytes(9123456, false, "9.12", 9120000); 91 92 checkFormatBytes(-1, true, "-1", -1); 93 checkFormatBytes(-1, false, "-1", -1); 94 95 checkFormatBytes(-914, true, "-0.91", -910); 96 checkFormatBytes(-914, false, "-0.91", -910); 97 98 // Missing FLAG_CALCULATE_ROUNDED case. 99 BytesResult r = Formatter.formatBytes(mContext.getResources(), 1, 0); 100 assertEquals("1", r.value); 101 assertEquals(0, r.roundedBytes); // Didn't pass FLAG_CALCULATE_ROUNDED 102 103 // Make sure it works on different locales. 104 setLocale(new Locale("es", "ES")); 105 checkFormatBytes(9123000, false, "9,12", 9120000); 106 } 107 108 @Test 109 public void testFormatBytesSi() { 110 setLocale(Locale.US); 111 112 checkFormatBytes(1_000, FLAG_SI_UNITS, "1.00", 1_000); 113 checkFormatBytes(1_024, FLAG_SI_UNITS, "1.02", 1_020); 114 checkFormatBytes(1_500, FLAG_SI_UNITS, "1.50", 1_500); 115 checkFormatBytes(12_582_912L, FLAG_SI_UNITS, "12.58", 12_580_000L); 116 } 117 118 @Test 119 public void testFormatBytesIec() { 120 setLocale(Locale.US); 121 122 checkFormatBytes(1_000, FLAG_IEC_UNITS, "0.98", 1_003); 123 checkFormatBytes(1_024, FLAG_IEC_UNITS, "1.00", 1_024); 124 checkFormatBytes(1_500, FLAG_IEC_UNITS, "1.46", 1_495); 125 checkFormatBytes(12_500_000L, FLAG_IEC_UNITS, "11.92", 12_499_025L); 126 checkFormatBytes(12_582_912L, FLAG_IEC_UNITS, "12.00", 12_582_912L); 127 } 128 129 private static final long SECOND = 1000; 130 private static final long MINUTE = 60 * SECOND; 131 private static final long HOUR = 60 * MINUTE; 132 private static final long DAY = 24 * HOUR; 133 134 @Test 135 public void testFormatShortElapsedTime() { 136 setLocale(Locale.US); 137 assertEquals("3 days", Formatter.formatShortElapsedTime(mContext, 2 * DAY + 12 * HOUR)); 138 assertEquals("2 days", Formatter.formatShortElapsedTime(mContext, 2 * DAY + 11 * HOUR)); 139 assertEquals("2 days", Formatter.formatShortElapsedTime(mContext, 2 * DAY)); 140 assertEquals("1 day, 23 hr", 141 Formatter.formatShortElapsedTime(mContext, 1 * DAY + 23 * HOUR + 59 * MINUTE)); 142 assertEquals("1 day", 143 Formatter.formatShortElapsedTime(mContext, 1 * DAY + 59 * MINUTE)); 144 assertEquals("1 day", Formatter.formatShortElapsedTime(mContext, 1 * DAY)); 145 assertEquals("24 hr", Formatter.formatShortElapsedTime(mContext, 23 * HOUR + 30 * MINUTE)); 146 assertEquals("3 hr", Formatter.formatShortElapsedTime(mContext, 2 * HOUR + 30 * MINUTE)); 147 assertEquals("2 hr", Formatter.formatShortElapsedTime(mContext, 2 * HOUR)); 148 assertEquals("1 hr", Formatter.formatShortElapsedTime(mContext, 1 * HOUR)); 149 assertEquals("60 min", 150 Formatter.formatShortElapsedTime(mContext, 59 * MINUTE + 30 * SECOND)); 151 assertEquals("59 min", 152 Formatter.formatShortElapsedTime(mContext, 59 * MINUTE)); 153 assertEquals("3 min", Formatter.formatShortElapsedTime(mContext, 2 * MINUTE + 30 * SECOND)); 154 assertEquals("2 min", Formatter.formatShortElapsedTime(mContext, 2 * MINUTE)); 155 assertEquals("1 min, 59 sec", 156 Formatter.formatShortElapsedTime(mContext, 1 * MINUTE + 59 * SECOND + 999)); 157 assertEquals("1 min", Formatter.formatShortElapsedTime(mContext, 1 * MINUTE)); 158 assertEquals("59 sec", Formatter.formatShortElapsedTime(mContext, 59 * SECOND + 999)); 159 assertEquals("1 sec", Formatter.formatShortElapsedTime(mContext, 1 * SECOND)); 160 assertEquals("0 sec", Formatter.formatShortElapsedTime(mContext, 1)); 161 assertEquals("0 sec", Formatter.formatShortElapsedTime(mContext, 0)); 162 163 // Make sure it works on different locales. 164 setLocale(Locale.FRANCE); 165 assertEquals("2 j", Formatter.formatShortElapsedTime(mContext, 2 * DAY)); 166 } 167 168 @Test 169 public void testFormatShortElapsedTimeRoundingUpToMinutes() { 170 setLocale(Locale.US); 171 assertEquals("3 days", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 172 mContext, 2 * DAY + 12 * HOUR)); 173 assertEquals("2 days", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 174 mContext, 2 * DAY + 11 * HOUR)); 175 assertEquals("2 days", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 176 mContext, 2 * DAY)); 177 assertEquals("1 day, 23 hr", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 178 mContext, 1 * DAY + 23 * HOUR + 59 * MINUTE)); 179 assertEquals("1 day", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 180 mContext, 1 * DAY + 59 * MINUTE)); 181 assertEquals("1 day", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 182 mContext, 1 * DAY)); 183 assertEquals("24 hr", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 184 mContext, 23 * HOUR + 30 * MINUTE)); 185 assertEquals("3 hr", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 186 mContext, 2 * HOUR + 30 * MINUTE)); 187 assertEquals("2 hr", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 188 mContext, 2 * HOUR)); 189 assertEquals("1 hr", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 190 mContext, 1 * HOUR)); 191 assertEquals("1 hr", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 192 mContext, 59 * MINUTE + 30 * SECOND)); 193 assertEquals("59 min", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 194 mContext, 59 * MINUTE)); 195 assertEquals("3 min", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 196 mContext, 2 * MINUTE + 30 * SECOND)); 197 assertEquals("2 min", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 198 mContext, 2 * MINUTE)); 199 assertEquals("2 min", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 200 mContext, 1 * MINUTE + 59 * SECOND + 999)); 201 assertEquals("1 min", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 202 mContext, 1 * MINUTE)); 203 assertEquals("1 min", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 204 mContext, 59 * SECOND + 999)); 205 assertEquals("1 min", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 206 mContext, 1 * SECOND)); 207 assertEquals("1 min", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 208 mContext, 1)); 209 assertEquals("0 min", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 210 mContext, 0)); 211 212 // Make sure it works on different locales. 213 setLocale(new Locale("ru", "RU")); 214 assertEquals("1 .", Formatter.formatShortElapsedTimeRoundingUpToMinutes( 215 mContext, 1 * SECOND)); 216 } 217 218 private void checkFormatBytes(long bytes, boolean useShort, 219 String expectedString, long expectedRounded) { 220 checkFormatBytes(bytes, (useShort ? Formatter.FLAG_SHORTER : 0), 221 expectedString, expectedRounded); 222 } 223 224 private void checkFormatBytes(long bytes, int flags, 225 String expectedString, long expectedRounded) { 226 BytesResult r = Formatter.formatBytes(mContext.getResources(), bytes, 227 Formatter.FLAG_CALCULATE_ROUNDED | flags); 228 assertEquals(expectedString, r.value); 229 assertEquals(expectedRounded, r.roundedBytes); 230 } 231 232 private void setLocale(Locale locale) { 233 Resources res = mContext.getResources(); 234 Configuration config = res.getConfiguration(); 235 config.locale = locale; 236 res.updateConfiguration(config, res.getDisplayMetrics()); 237 238 Locale.setDefault(locale); 239 } 240 } 241