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 package com.android.tv.util; 17 18 import static org.junit.Assert.assertEquals; 19 import static org.junit.Assert.assertFalse; 20 import static org.junit.Assert.assertTrue; 21 22 import android.text.format.DateUtils; 23 24 import com.android.tv.testing.constants.ConfigConstants; 25 26 import org.junit.After; 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.robolectric.RobolectricTestRunner; 31 import org.robolectric.RuntimeEnvironment; 32 import org.robolectric.annotation.Config; 33 34 import java.util.Calendar; 35 import java.util.GregorianCalendar; 36 import java.util.Locale; 37 import java.util.TimeZone; 38 39 /** 40 * Tests for {@link com.android.tv.util.Utils#getDurationString}. 41 * 42 * <p>This test uses deprecated flags {@link DateUtils#FORMAT_12HOUR} and {@link 43 * DateUtils#FORMAT_24HOUR} to run this test independent to system's 12/24h format. 44 */ 45 @RunWith(RobolectricTestRunner.class) 46 @Config(sdk = ConfigConstants.SDK) 47 public class UtilsTest { 48 // TODO: Mock Context so we can specify current time and locale for test. 49 private Locale mLocale; 50 private static final long DATE_THIS_YEAR_2_1_MS = getFebOfThisYearInMillis(1, 0, 0); 51 52 // All possible list for a parameter to test parameter independent result. 53 private static final boolean[] PARAM_USE_SHORT_FORMAT = {false, true}; 54 55 @Before setUp()56 public void setUp() { 57 // Set locale to US 58 mLocale = Locale.getDefault(); 59 Locale.setDefault(Locale.US); 60 } 61 62 @After tearDown()63 public void tearDown() { 64 // Revive system locale. 65 Locale.setDefault(mLocale); 66 } 67 68 /** Return time in millis assuming that whose year is this year and month is Jan. */ getJanOfThisYearInMillis(int date, int hour, int minutes)69 private static long getJanOfThisYearInMillis(int date, int hour, int minutes) { 70 return new GregorianCalendar(getThisYear(), Calendar.JANUARY, date, hour, minutes) 71 .getTimeInMillis(); 72 } 73 getJanOfThisYearInMillis(int date, int hour)74 private static long getJanOfThisYearInMillis(int date, int hour) { 75 return getJanOfThisYearInMillis(date, hour, 0); 76 } 77 78 /** Return time in millis assuming that whose year is this year and month is Feb. */ getFebOfThisYearInMillis(int date, int hour, int minutes)79 private static long getFebOfThisYearInMillis(int date, int hour, int minutes) { 80 return new GregorianCalendar(getThisYear(), Calendar.FEBRUARY, date, hour, minutes) 81 .getTimeInMillis(); 82 } 83 getFebOfThisYearInMillis(int date, int hour)84 private static long getFebOfThisYearInMillis(int date, int hour) { 85 return getFebOfThisYearInMillis(date, hour, 0); 86 } 87 getThisYear()88 private static int getThisYear() { 89 return new GregorianCalendar().get(GregorianCalendar.YEAR); 90 } 91 92 @Test testSameDateAndTime()93 public void testSameDateAndTime() { 94 assertEquals( 95 "3:00 AM", 96 Utils.getDurationString( 97 RuntimeEnvironment.application, 98 DATE_THIS_YEAR_2_1_MS, 99 getFebOfThisYearInMillis(1, 3), 100 getFebOfThisYearInMillis(1, 3), 101 false, 102 DateUtils.FORMAT_12HOUR)); 103 assertEquals( 104 "03:00", 105 Utils.getDurationString( 106 RuntimeEnvironment.application, 107 DATE_THIS_YEAR_2_1_MS, 108 getFebOfThisYearInMillis(1, 3), 109 getFebOfThisYearInMillis(1, 3), 110 false, 111 DateUtils.FORMAT_24HOUR)); 112 } 113 114 @Test testDurationWithinToday()115 public void testDurationWithinToday() { 116 assertEquals( 117 "12:00 – 3:00 AM", 118 Utils.getDurationString( 119 RuntimeEnvironment.application, 120 DATE_THIS_YEAR_2_1_MS, 121 DATE_THIS_YEAR_2_1_MS, 122 getFebOfThisYearInMillis(1, 3), 123 false, 124 DateUtils.FORMAT_12HOUR)); 125 assertEquals( 126 "00:00 – 03:00", 127 Utils.getDurationString( 128 RuntimeEnvironment.application, 129 DATE_THIS_YEAR_2_1_MS, 130 DATE_THIS_YEAR_2_1_MS, 131 getFebOfThisYearInMillis(1, 3), 132 false, 133 DateUtils.FORMAT_24HOUR)); 134 } 135 136 @Test testDurationFromYesterdayToToday()137 public void testDurationFromYesterdayToToday() { 138 assertEquals( 139 "Jan 31, 3:00 AM – Feb 1, 4:00 AM", 140 Utils.getDurationString( 141 RuntimeEnvironment.application, 142 DATE_THIS_YEAR_2_1_MS, 143 getJanOfThisYearInMillis(31, 3), 144 getFebOfThisYearInMillis(1, 4), 145 false, 146 DateUtils.FORMAT_12HOUR)); 147 assertEquals( 148 "Jan 31, 03:00 – Feb 1, 04:00", 149 Utils.getDurationString( 150 RuntimeEnvironment.application, 151 DATE_THIS_YEAR_2_1_MS, 152 getJanOfThisYearInMillis(31, 3), 153 getFebOfThisYearInMillis(1, 4), 154 false, 155 DateUtils.FORMAT_24HOUR)); 156 assertEquals( 157 "1/31, 11:30 PM – 12:30 AM", 158 Utils.getDurationString( 159 RuntimeEnvironment.application, 160 DATE_THIS_YEAR_2_1_MS, 161 getJanOfThisYearInMillis(31, 23, 30), 162 getFebOfThisYearInMillis(1, 0, 30), 163 true, 164 DateUtils.FORMAT_12HOUR)); 165 assertEquals( 166 "1/31, 23:30 – 00:30", 167 Utils.getDurationString( 168 RuntimeEnvironment.application, 169 DATE_THIS_YEAR_2_1_MS, 170 getJanOfThisYearInMillis(31, 23, 30), 171 getFebOfThisYearInMillis(1, 0, 30), 172 true, 173 DateUtils.FORMAT_24HOUR)); 174 } 175 176 @Test testDurationFromTodayToTomorrow()177 public void testDurationFromTodayToTomorrow() { 178 assertEquals( 179 "Feb 1, 3:00 AM – Feb 2, 4:00 AM", 180 Utils.getDurationString( 181 RuntimeEnvironment.application, 182 DATE_THIS_YEAR_2_1_MS, 183 getFebOfThisYearInMillis(1, 3), 184 getFebOfThisYearInMillis(2, 4), 185 false, 186 DateUtils.FORMAT_12HOUR)); 187 assertEquals( 188 "Feb 1, 03:00 – Feb 2, 04:00", 189 Utils.getDurationString( 190 RuntimeEnvironment.application, 191 DATE_THIS_YEAR_2_1_MS, 192 getFebOfThisYearInMillis(1, 3), 193 getFebOfThisYearInMillis(2, 4), 194 false, 195 DateUtils.FORMAT_24HOUR)); 196 assertEquals( 197 "2/1, 3:00 AM – 2/2, 4:00 AM", 198 Utils.getDurationString( 199 RuntimeEnvironment.application, 200 DATE_THIS_YEAR_2_1_MS, 201 getFebOfThisYearInMillis(1, 3), 202 getFebOfThisYearInMillis(2, 4), 203 true, 204 DateUtils.FORMAT_12HOUR)); 205 assertEquals( 206 "2/1, 03:00 – 2/2, 04:00", 207 Utils.getDurationString( 208 RuntimeEnvironment.application, 209 DATE_THIS_YEAR_2_1_MS, 210 getFebOfThisYearInMillis(1, 3), 211 getFebOfThisYearInMillis(2, 4), 212 true, 213 DateUtils.FORMAT_24HOUR)); 214 215 assertEquals( 216 "Feb 1, 11:30 PM – Feb 2, 12:30 AM", 217 Utils.getDurationString( 218 RuntimeEnvironment.application, 219 DATE_THIS_YEAR_2_1_MS, 220 getFebOfThisYearInMillis(1, 23, 30), 221 getFebOfThisYearInMillis(2, 0, 30), 222 false, 223 DateUtils.FORMAT_12HOUR)); 224 assertEquals( 225 "Feb 1, 23:30 – Feb 2, 00:30", 226 Utils.getDurationString( 227 RuntimeEnvironment.application, 228 DATE_THIS_YEAR_2_1_MS, 229 getFebOfThisYearInMillis(1, 23, 30), 230 getFebOfThisYearInMillis(2, 0, 30), 231 false, 232 DateUtils.FORMAT_24HOUR)); 233 assertEquals( 234 "11:30 PM – 12:30 AM", 235 Utils.getDurationString( 236 RuntimeEnvironment.application, 237 DATE_THIS_YEAR_2_1_MS, 238 getFebOfThisYearInMillis(1, 23, 30), 239 getFebOfThisYearInMillis(2, 0, 30), 240 true, 241 DateUtils.FORMAT_12HOUR)); 242 assertEquals( 243 "23:30 – 00:30", 244 Utils.getDurationString( 245 RuntimeEnvironment.application, 246 DATE_THIS_YEAR_2_1_MS, 247 getFebOfThisYearInMillis(1, 23, 30), 248 getFebOfThisYearInMillis(2, 0, 30), 249 true, 250 DateUtils.FORMAT_24HOUR)); 251 } 252 253 @Test testDurationWithinTomorrow()254 public void testDurationWithinTomorrow() { 255 assertEquals( 256 "Feb 2, 2:00 – 4:00 AM", 257 Utils.getDurationString( 258 RuntimeEnvironment.application, 259 DATE_THIS_YEAR_2_1_MS, 260 getFebOfThisYearInMillis(2, 2), 261 getFebOfThisYearInMillis(2, 4), 262 false, 263 DateUtils.FORMAT_12HOUR)); 264 assertEquals( 265 "Feb 2, 02:00 – 04:00", 266 Utils.getDurationString( 267 RuntimeEnvironment.application, 268 DATE_THIS_YEAR_2_1_MS, 269 getFebOfThisYearInMillis(2, 2), 270 getFebOfThisYearInMillis(2, 4), 271 false, 272 DateUtils.FORMAT_24HOUR)); 273 assertEquals( 274 "2/2, 2:00 – 4:00 AM", 275 Utils.getDurationString( 276 RuntimeEnvironment.application, 277 DATE_THIS_YEAR_2_1_MS, 278 getFebOfThisYearInMillis(2, 2), 279 getFebOfThisYearInMillis(2, 4), 280 true, 281 DateUtils.FORMAT_12HOUR)); 282 assertEquals( 283 "2/2, 02:00 – 04:00", 284 Utils.getDurationString( 285 RuntimeEnvironment.application, 286 DATE_THIS_YEAR_2_1_MS, 287 getFebOfThisYearInMillis(2, 2), 288 getFebOfThisYearInMillis(2, 4), 289 true, 290 DateUtils.FORMAT_24HOUR)); 291 } 292 293 @Test testStartOfDay()294 public void testStartOfDay() { 295 assertEquals( 296 "12:00 – 1:00 AM", 297 Utils.getDurationString( 298 RuntimeEnvironment.application, 299 DATE_THIS_YEAR_2_1_MS, 300 DATE_THIS_YEAR_2_1_MS, 301 getFebOfThisYearInMillis(1, 1), 302 false, 303 DateUtils.FORMAT_12HOUR)); 304 assertEquals( 305 "00:00 – 01:00", 306 Utils.getDurationString( 307 RuntimeEnvironment.application, 308 DATE_THIS_YEAR_2_1_MS, 309 DATE_THIS_YEAR_2_1_MS, 310 getFebOfThisYearInMillis(1, 1), 311 false, 312 DateUtils.FORMAT_24HOUR)); 313 314 assertEquals( 315 "Feb 2, 12:00 – 1:00 AM", 316 Utils.getDurationString( 317 RuntimeEnvironment.application, 318 DATE_THIS_YEAR_2_1_MS, 319 getFebOfThisYearInMillis(2, 0), 320 getFebOfThisYearInMillis(2, 1), 321 false, 322 DateUtils.FORMAT_12HOUR)); 323 assertEquals( 324 "Feb 2, 00:00 – 01:00", 325 Utils.getDurationString( 326 RuntimeEnvironment.application, 327 DATE_THIS_YEAR_2_1_MS, 328 getFebOfThisYearInMillis(2, 0), 329 getFebOfThisYearInMillis(2, 1), 330 false, 331 DateUtils.FORMAT_24HOUR)); 332 assertEquals( 333 "2/2, 12:00 – 1:00 AM", 334 Utils.getDurationString( 335 RuntimeEnvironment.application, 336 DATE_THIS_YEAR_2_1_MS, 337 getFebOfThisYearInMillis(2, 0), 338 getFebOfThisYearInMillis(2, 1), 339 true, 340 DateUtils.FORMAT_12HOUR)); 341 assertEquals( 342 "2/2, 00:00 – 01:00", 343 Utils.getDurationString( 344 RuntimeEnvironment.application, 345 DATE_THIS_YEAR_2_1_MS, 346 getFebOfThisYearInMillis(2, 0), 347 getFebOfThisYearInMillis(2, 1), 348 true, 349 DateUtils.FORMAT_24HOUR)); 350 } 351 352 @Test testEndOfDay()353 public void testEndOfDay() { 354 for (boolean useShortFormat : PARAM_USE_SHORT_FORMAT) { 355 assertEquals( 356 "11:00 PM – 12:00 AM", 357 Utils.getDurationString( 358 RuntimeEnvironment.application, 359 DATE_THIS_YEAR_2_1_MS, 360 getFebOfThisYearInMillis(1, 23), 361 getFebOfThisYearInMillis(2, 0), 362 useShortFormat, 363 DateUtils.FORMAT_12HOUR)); 364 assertEquals( 365 "23:00 – 00:00", 366 Utils.getDurationString( 367 RuntimeEnvironment.application, 368 DATE_THIS_YEAR_2_1_MS, 369 getFebOfThisYearInMillis(1, 23), 370 getFebOfThisYearInMillis(2, 0), 371 useShortFormat, 372 DateUtils.FORMAT_24HOUR)); 373 } 374 375 assertEquals( 376 "Feb 2, 11:00 PM – 12:00 AM", 377 Utils.getDurationString( 378 RuntimeEnvironment.application, 379 DATE_THIS_YEAR_2_1_MS, 380 getFebOfThisYearInMillis(2, 23), 381 getFebOfThisYearInMillis(3, 0), 382 false, 383 DateUtils.FORMAT_12HOUR)); 384 assertEquals( 385 "Feb 2, 23:00 – 00:00", 386 Utils.getDurationString( 387 RuntimeEnvironment.application, 388 DATE_THIS_YEAR_2_1_MS, 389 getFebOfThisYearInMillis(2, 23), 390 getFebOfThisYearInMillis(3, 0), 391 false, 392 DateUtils.FORMAT_24HOUR)); 393 assertEquals( 394 "2/2, 11:00 PM – 12:00 AM", 395 Utils.getDurationString( 396 RuntimeEnvironment.application, 397 DATE_THIS_YEAR_2_1_MS, 398 getFebOfThisYearInMillis(2, 23), 399 getFebOfThisYearInMillis(3, 0), 400 true, 401 DateUtils.FORMAT_12HOUR)); 402 assertEquals( 403 "2/2, 23:00 – 00:00", 404 Utils.getDurationString( 405 RuntimeEnvironment.application, 406 DATE_THIS_YEAR_2_1_MS, 407 getFebOfThisYearInMillis(2, 23), 408 getFebOfThisYearInMillis(3, 0), 409 true, 410 DateUtils.FORMAT_24HOUR)); 411 assertEquals( 412 "2/2, 12:00 AM – 2/3, 12:00 AM", 413 Utils.getDurationString( 414 RuntimeEnvironment.application, 415 DATE_THIS_YEAR_2_1_MS, 416 getFebOfThisYearInMillis(2, 0), 417 getFebOfThisYearInMillis(3, 0), 418 true, 419 DateUtils.FORMAT_12HOUR)); 420 assertEquals( 421 "2/2, 00:00 – 2/3, 00:00", 422 Utils.getDurationString( 423 RuntimeEnvironment.application, 424 DATE_THIS_YEAR_2_1_MS, 425 getFebOfThisYearInMillis(2, 0), 426 getFebOfThisYearInMillis(3, 0), 427 true, 428 DateUtils.FORMAT_24HOUR)); 429 } 430 431 @Test testMidnight()432 public void testMidnight() { 433 for (boolean useShortFormat : PARAM_USE_SHORT_FORMAT) { 434 assertEquals( 435 "12:00 AM", 436 Utils.getDurationString( 437 RuntimeEnvironment.application, 438 DATE_THIS_YEAR_2_1_MS, 439 DATE_THIS_YEAR_2_1_MS, 440 DATE_THIS_YEAR_2_1_MS, 441 useShortFormat, 442 DateUtils.FORMAT_12HOUR)); 443 assertEquals( 444 "00:00", 445 Utils.getDurationString( 446 RuntimeEnvironment.application, 447 DATE_THIS_YEAR_2_1_MS, 448 DATE_THIS_YEAR_2_1_MS, 449 DATE_THIS_YEAR_2_1_MS, 450 useShortFormat, 451 DateUtils.FORMAT_24HOUR)); 452 } 453 } 454 455 @Test testIsInGivenDay()456 public void testIsInGivenDay() { 457 assertTrue( 458 Utils.isInGivenDay( 459 new GregorianCalendar(2015, Calendar.JANUARY, 1).getTimeInMillis(), 460 new GregorianCalendar(2015, Calendar.JANUARY, 1, 0, 30).getTimeInMillis())); 461 } 462 463 @Test testIsNotInGivenDay()464 public void testIsNotInGivenDay() { 465 assertFalse( 466 Utils.isInGivenDay( 467 new GregorianCalendar(2015, Calendar.JANUARY, 1).getTimeInMillis(), 468 new GregorianCalendar(2015, Calendar.JANUARY, 2).getTimeInMillis())); 469 } 470 471 @Test testIfTimeZoneApplied()472 public void testIfTimeZoneApplied() { 473 TimeZone timeZone = TimeZone.getDefault(); 474 475 TimeZone.setDefault(TimeZone.getTimeZone("Asia/Seoul")); 476 477 // 2015.01.01 00:00 in KST = 2014.12.31 15:00 in UTC 478 long date2015StartMs = new GregorianCalendar(2015, Calendar.JANUARY, 1).getTimeInMillis(); 479 480 // 2015.01.01 10:00 in KST = 2015.01.01 01:00 in UTC 481 long date2015Start10AMMs = 482 new GregorianCalendar(2015, Calendar.JANUARY, 1, 10, 0).getTimeInMillis(); 483 484 // Those two times aren't in the same day in UTC, but they are in KST. 485 assertTrue(Utils.isInGivenDay(date2015StartMs, date2015Start10AMMs)); 486 487 TimeZone.setDefault(timeZone); 488 } 489 490 @Test testIsInternalTvInputInvalidInternalInputId()491 public void testIsInternalTvInputInvalidInternalInputId() { 492 String inputId = "tv.comp"; 493 assertFalse(Utils.isInternalTvInput(RuntimeEnvironment.application, inputId)); 494 } 495 } 496