Jeff Sharkey | 53f6e8a | 2012-11-13 16:23:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
Andrei Stingaceanu | bbe51f8 | 2017-04-28 14:48:17 +0100 | [diff] [blame] | 19 | import static org.junit.Assert.assertEquals; |
| 20 | |
Roozbeh Pournader | 4262e94 | 2016-06-30 16:07:01 -0700 | [diff] [blame] | 21 | import android.content.res.Configuration; |
| 22 | import android.content.res.Resources; |
| 23 | import android.os.LocaleList; |
Siyamed Sinir | 68089c8 | 2016-06-29 16:55:35 -0700 | [diff] [blame] | 24 | import android.support.test.filters.SmallTest; |
Andrei Stingaceanu | bbe51f8 | 2017-04-28 14:48:17 +0100 | [diff] [blame] | 25 | import android.support.test.runner.AndroidJUnit4; |
Jeff Sharkey | 53f6e8a | 2012-11-13 16:23:59 -0800 | [diff] [blame] | 26 | |
Andrei Stingaceanu | bbe51f8 | 2017-04-28 14:48:17 +0100 | [diff] [blame] | 27 | import org.junit.After; |
| 28 | import org.junit.Before; |
| 29 | import org.junit.Test; |
| 30 | import org.junit.runner.RunWith; |
Roozbeh Pournader | 4262e94 | 2016-06-30 16:07:01 -0700 | [diff] [blame] | 31 | |
Roozbeh Pournader | 02f167c | 2017-06-06 11:31:33 -0700 | [diff] [blame] | 32 | import java.text.DateFormat; |
| 33 | import java.util.Calendar; |
| 34 | import java.util.Date; |
| 35 | import java.util.Locale; |
Roozbeh Pournader | 26baa6a | 2017-08-03 11:11:17 -0700 | [diff] [blame] | 36 | import java.util.TimeZone; |
Roozbeh Pournader | 02f167c | 2017-06-06 11:31:33 -0700 | [diff] [blame] | 37 | |
Andrei Stingaceanu | bbe51f8 | 2017-04-28 14:48:17 +0100 | [diff] [blame] | 38 | @SmallTest |
| 39 | @RunWith(AndroidJUnit4.class) |
| 40 | public class DateUtilsTest { |
Roozbeh Pournader | 4262e94 | 2016-06-30 16:07:01 -0700 | [diff] [blame] | 41 | |
| 42 | private static final LocaleList LOCALE_LIST_US = new LocaleList(Locale.US); |
| 43 | private LocaleList mOriginalLocales; |
Roozbeh Pournader | 26baa6a | 2017-08-03 11:11:17 -0700 | [diff] [blame] | 44 | private TimeZone mOriginalTimeZone; |
Roozbeh Pournader | 4262e94 | 2016-06-30 16:07:01 -0700 | [diff] [blame] | 45 | |
Andrei Stingaceanu | bbe51f8 | 2017-04-28 14:48:17 +0100 | [diff] [blame] | 46 | @Before |
| 47 | public void setup() { |
Roozbeh Pournader | 4262e94 | 2016-06-30 16:07:01 -0700 | [diff] [blame] | 48 | mOriginalLocales = Resources.getSystem().getConfiguration().getLocales(); |
| 49 | setLocales(LOCALE_LIST_US); |
Roozbeh Pournader | 26baa6a | 2017-08-03 11:11:17 -0700 | [diff] [blame] | 50 | mOriginalTimeZone = TimeZone.getDefault(); |
| 51 | TimeZone.setDefault(TimeZone.getTimeZone("GMT")); |
Roozbeh Pournader | 4262e94 | 2016-06-30 16:07:01 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Andrei Stingaceanu | bbe51f8 | 2017-04-28 14:48:17 +0100 | [diff] [blame] | 54 | @After |
| 55 | public void teardown() { |
Roozbeh Pournader | 4262e94 | 2016-06-30 16:07:01 -0700 | [diff] [blame] | 56 | setLocales(mOriginalLocales); |
Roozbeh Pournader | 26baa6a | 2017-08-03 11:11:17 -0700 | [diff] [blame] | 57 | TimeZone.setDefault(mOriginalTimeZone); |
Roozbeh Pournader | 4262e94 | 2016-06-30 16:07:01 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Andrei Stingaceanu | bbe51f8 | 2017-04-28 14:48:17 +0100 | [diff] [blame] | 60 | @Test |
| 61 | public void test_formatDuration_seconds() { |
Jeff Sharkey | 53f6e8a | 2012-11-13 16:23:59 -0800 | [diff] [blame] | 62 | assertEquals("0 seconds", DateUtils.formatDuration(0)); |
| 63 | assertEquals("0 seconds", DateUtils.formatDuration(1)); |
| 64 | assertEquals("0 seconds", DateUtils.formatDuration(499)); |
| 65 | assertEquals("1 second", DateUtils.formatDuration(500)); |
| 66 | assertEquals("1 second", DateUtils.formatDuration(1000)); |
| 67 | assertEquals("2 seconds", DateUtils.formatDuration(1500)); |
Roozbeh Pournader | c6bffbf | 2016-12-13 13:58:56 -0800 | [diff] [blame] | 68 | |
| 69 | assertEquals("0 seconds", DateUtils.formatDuration(0, DateUtils.LENGTH_LONG)); |
| 70 | assertEquals("1 second", DateUtils.formatDuration(1000, DateUtils.LENGTH_LONG)); |
| 71 | assertEquals("2 seconds", DateUtils.formatDuration(1500, DateUtils.LENGTH_LONG)); |
| 72 | |
| 73 | assertEquals("0 sec", DateUtils.formatDuration(0, DateUtils.LENGTH_SHORT)); |
| 74 | assertEquals("1 sec", DateUtils.formatDuration(1000, DateUtils.LENGTH_SHORT)); |
| 75 | assertEquals("2 sec", DateUtils.formatDuration(1500, DateUtils.LENGTH_SHORT)); |
| 76 | |
| 77 | assertEquals("0s", DateUtils.formatDuration(0, DateUtils.LENGTH_SHORTEST)); |
| 78 | assertEquals("1s", DateUtils.formatDuration(1000, DateUtils.LENGTH_SHORTEST)); |
| 79 | assertEquals("2s", DateUtils.formatDuration(1500, DateUtils.LENGTH_SHORTEST)); |
Jeff Sharkey | 53f6e8a | 2012-11-13 16:23:59 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Andrei Stingaceanu | bbe51f8 | 2017-04-28 14:48:17 +0100 | [diff] [blame] | 82 | @Test |
| 83 | public void test_formatDuration_Minutes() { |
Jeff Sharkey | 53f6e8a | 2012-11-13 16:23:59 -0800 | [diff] [blame] | 84 | assertEquals("59 seconds", DateUtils.formatDuration(59000)); |
| 85 | assertEquals("60 seconds", DateUtils.formatDuration(59500)); |
| 86 | assertEquals("1 minute", DateUtils.formatDuration(60000)); |
| 87 | assertEquals("2 minutes", DateUtils.formatDuration(120000)); |
Roozbeh Pournader | c6bffbf | 2016-12-13 13:58:56 -0800 | [diff] [blame] | 88 | |
| 89 | assertEquals("1 minute", DateUtils.formatDuration(60000, DateUtils.LENGTH_LONG)); |
| 90 | assertEquals("2 minutes", DateUtils.formatDuration(120000, DateUtils.LENGTH_LONG)); |
| 91 | |
| 92 | assertEquals("1 min", DateUtils.formatDuration(60000, DateUtils.LENGTH_SHORT)); |
| 93 | assertEquals("2 min", DateUtils.formatDuration(120000, DateUtils.LENGTH_SHORT)); |
| 94 | |
| 95 | assertEquals("1m", DateUtils.formatDuration(60000, DateUtils.LENGTH_SHORTEST)); |
| 96 | assertEquals("2m", DateUtils.formatDuration(120000, DateUtils.LENGTH_SHORTEST)); |
Jeff Sharkey | 53f6e8a | 2012-11-13 16:23:59 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Andrei Stingaceanu | bbe51f8 | 2017-04-28 14:48:17 +0100 | [diff] [blame] | 99 | @Test |
| 100 | public void test_formatDuration_Hours() { |
Jeff Sharkey | 53f6e8a | 2012-11-13 16:23:59 -0800 | [diff] [blame] | 101 | assertEquals("59 minutes", DateUtils.formatDuration(3540000)); |
| 102 | assertEquals("1 hour", DateUtils.formatDuration(3600000)); |
| 103 | assertEquals("48 hours", DateUtils.formatDuration(172800000)); |
Roozbeh Pournader | c6bffbf | 2016-12-13 13:58:56 -0800 | [diff] [blame] | 104 | |
| 105 | assertEquals("1 hour", DateUtils.formatDuration(3600000, DateUtils.LENGTH_LONG)); |
| 106 | assertEquals("48 hours", DateUtils.formatDuration(172800000, DateUtils.LENGTH_LONG)); |
| 107 | |
| 108 | assertEquals("1 hr", DateUtils.formatDuration(3600000, DateUtils.LENGTH_SHORT)); |
| 109 | assertEquals("48 hr", DateUtils.formatDuration(172800000, DateUtils.LENGTH_SHORT)); |
| 110 | |
| 111 | assertEquals("1h", DateUtils.formatDuration(3600000, DateUtils.LENGTH_SHORTEST)); |
| 112 | assertEquals("48h", DateUtils.formatDuration(172800000, DateUtils.LENGTH_SHORTEST)); |
Jeff Sharkey | 53f6e8a | 2012-11-13 16:23:59 -0800 | [diff] [blame] | 113 | } |
Andrei Stingaceanu | bbe51f8 | 2017-04-28 14:48:17 +0100 | [diff] [blame] | 114 | |
Roozbeh Pournader | 02f167c | 2017-06-06 11:31:33 -0700 | [diff] [blame] | 115 | @Test |
| 116 | public void testFormatSameDayTime() { |
| 117 | // This test assumes a default DateFormat.is24Hour setting. |
| 118 | DateFormat.is24Hour = null; |
| 119 | Date date = new Date(109, 0, 19, 3, 30, 15); |
| 120 | long fixedTime = date.getTime(); |
| 121 | |
| 122 | int currentYear = Calendar.getInstance().get(Calendar.YEAR); |
| 123 | Date dateWithCurrentYear = new Date(currentYear - 1900, 0, 19, 3, 30, 15); |
| 124 | |
| 125 | final long dayDuration = 5 * 24 * 60 * 60 * 1000; |
| 126 | assertEquals("Saturday, January 24, 2009", DateUtils.formatSameDayTime( |
| 127 | fixedTime + dayDuration, fixedTime, java.text.DateFormat.FULL, |
| 128 | java.text.DateFormat.FULL)); |
| 129 | assertEquals("Jan 24, 2009", DateUtils.formatSameDayTime(fixedTime + dayDuration, |
| 130 | fixedTime, java.text.DateFormat.DEFAULT, java.text.DateFormat.FULL)); |
| 131 | assertEquals("January 24, 2009", DateUtils.formatSameDayTime(fixedTime + dayDuration, |
| 132 | fixedTime, java.text.DateFormat.LONG, java.text.DateFormat.FULL)); |
| 133 | assertEquals("Jan 24, 2009", DateUtils.formatSameDayTime(fixedTime + dayDuration, |
| 134 | fixedTime, java.text.DateFormat.MEDIUM, java.text.DateFormat.FULL)); |
| 135 | assertEquals("1/24/09", DateUtils.formatSameDayTime(fixedTime + dayDuration, |
| 136 | fixedTime, java.text.DateFormat.SHORT, java.text.DateFormat.FULL)); |
| 137 | |
| 138 | final long hourDuration = 2 * 60 * 60 * 1000; |
Roozbeh Pournader | 5e730ab | 2017-08-15 14:31:50 -0700 | [diff] [blame] | 139 | assertEquals("5:30:15 AM Greenwich Mean Time", DateUtils.formatSameDayTime( |
| 140 | fixedTime + hourDuration, fixedTime, java.text.DateFormat.FULL, |
| 141 | java.text.DateFormat.FULL)); |
Roozbeh Pournader | 02f167c | 2017-06-06 11:31:33 -0700 | [diff] [blame] | 142 | assertEquals("5:30:15 AM", DateUtils.formatSameDayTime(fixedTime + hourDuration, |
| 143 | fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.DEFAULT)); |
Roozbeh Pournader | 5e730ab | 2017-08-15 14:31:50 -0700 | [diff] [blame] | 144 | assertEquals("5:30:15 AM GMT", DateUtils.formatSameDayTime(fixedTime + hourDuration, |
Roozbeh Pournader | 02f167c | 2017-06-06 11:31:33 -0700 | [diff] [blame] | 145 | fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.LONG)); |
| 146 | assertEquals("5:30:15 AM", DateUtils.formatSameDayTime(fixedTime + hourDuration, |
| 147 | fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.MEDIUM)); |
| 148 | assertEquals("5:30 AM", DateUtils.formatSameDayTime(fixedTime + hourDuration, |
| 149 | fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.SHORT)); |
| 150 | } |
| 151 | |
Andrei Stingaceanu | bbe51f8 | 2017-04-28 14:48:17 +0100 | [diff] [blame] | 152 | private void setLocales(LocaleList locales) { |
| 153 | final Resources systemResources = Resources.getSystem(); |
| 154 | final Configuration config = new Configuration(systemResources.getConfiguration()); |
| 155 | config.setLocales(locales); |
| 156 | // This is not very safe to call, but since DateUtils.formatDuration() is a static method |
| 157 | // (it gets its format strings from the system resources), we can't pass a modified Context |
| 158 | // to it. |
| 159 | systemResources.updateConfiguration(config, null); |
| 160 | } |
| 161 | |
Jeff Sharkey | 53f6e8a | 2012-11-13 16:23:59 -0800 | [diff] [blame] | 162 | } |