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 | |
Roozbeh Pournader | 4262e94 | 2016-06-30 16:07:01 -0700 | [diff] [blame] | 27 | import java.util.Locale; |
Andrei Stingaceanu | bbe51f8 | 2017-04-28 14:48:17 +0100 | [diff] [blame] | 28 | import org.junit.After; |
| 29 | import org.junit.Before; |
| 30 | import org.junit.Test; |
| 31 | import org.junit.runner.RunWith; |
Roozbeh Pournader | 4262e94 | 2016-06-30 16:07:01 -0700 | [diff] [blame] | 32 | |
Andrei Stingaceanu | bbe51f8 | 2017-04-28 14:48:17 +0100 | [diff] [blame] | 33 | @SmallTest |
| 34 | @RunWith(AndroidJUnit4.class) |
| 35 | public class DateUtilsTest { |
Roozbeh Pournader | 4262e94 | 2016-06-30 16:07:01 -0700 | [diff] [blame] | 36 | |
| 37 | private static final LocaleList LOCALE_LIST_US = new LocaleList(Locale.US); |
| 38 | private LocaleList mOriginalLocales; |
| 39 | |
Andrei Stingaceanu | bbe51f8 | 2017-04-28 14:48:17 +0100 | [diff] [blame] | 40 | @Before |
| 41 | public void setup() { |
Roozbeh Pournader | 4262e94 | 2016-06-30 16:07:01 -0700 | [diff] [blame] | 42 | mOriginalLocales = Resources.getSystem().getConfiguration().getLocales(); |
| 43 | setLocales(LOCALE_LIST_US); |
| 44 | } |
| 45 | |
Andrei Stingaceanu | bbe51f8 | 2017-04-28 14:48:17 +0100 | [diff] [blame] | 46 | @After |
| 47 | public void teardown() { |
Roozbeh Pournader | 4262e94 | 2016-06-30 16:07:01 -0700 | [diff] [blame] | 48 | setLocales(mOriginalLocales); |
Roozbeh Pournader | 4262e94 | 2016-06-30 16:07:01 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Andrei Stingaceanu | bbe51f8 | 2017-04-28 14:48:17 +0100 | [diff] [blame] | 51 | @Test |
| 52 | public void test_formatDuration_seconds() { |
Jeff Sharkey | 53f6e8a | 2012-11-13 16:23:59 -0800 | [diff] [blame] | 53 | assertEquals("0 seconds", DateUtils.formatDuration(0)); |
| 54 | assertEquals("0 seconds", DateUtils.formatDuration(1)); |
| 55 | assertEquals("0 seconds", DateUtils.formatDuration(499)); |
| 56 | assertEquals("1 second", DateUtils.formatDuration(500)); |
| 57 | assertEquals("1 second", DateUtils.formatDuration(1000)); |
| 58 | assertEquals("2 seconds", DateUtils.formatDuration(1500)); |
Roozbeh Pournader | c6bffbf | 2016-12-13 13:58:56 -0800 | [diff] [blame] | 59 | |
| 60 | assertEquals("0 seconds", DateUtils.formatDuration(0, DateUtils.LENGTH_LONG)); |
| 61 | assertEquals("1 second", DateUtils.formatDuration(1000, DateUtils.LENGTH_LONG)); |
| 62 | assertEquals("2 seconds", DateUtils.formatDuration(1500, DateUtils.LENGTH_LONG)); |
| 63 | |
| 64 | assertEquals("0 sec", DateUtils.formatDuration(0, DateUtils.LENGTH_SHORT)); |
| 65 | assertEquals("1 sec", DateUtils.formatDuration(1000, DateUtils.LENGTH_SHORT)); |
| 66 | assertEquals("2 sec", DateUtils.formatDuration(1500, DateUtils.LENGTH_SHORT)); |
| 67 | |
| 68 | assertEquals("0s", DateUtils.formatDuration(0, DateUtils.LENGTH_SHORTEST)); |
| 69 | assertEquals("1s", DateUtils.formatDuration(1000, DateUtils.LENGTH_SHORTEST)); |
| 70 | assertEquals("2s", DateUtils.formatDuration(1500, DateUtils.LENGTH_SHORTEST)); |
Jeff Sharkey | 53f6e8a | 2012-11-13 16:23:59 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Andrei Stingaceanu | bbe51f8 | 2017-04-28 14:48:17 +0100 | [diff] [blame] | 73 | @Test |
| 74 | public void test_formatDuration_Minutes() { |
Jeff Sharkey | 53f6e8a | 2012-11-13 16:23:59 -0800 | [diff] [blame] | 75 | assertEquals("59 seconds", DateUtils.formatDuration(59000)); |
| 76 | assertEquals("60 seconds", DateUtils.formatDuration(59500)); |
| 77 | assertEquals("1 minute", DateUtils.formatDuration(60000)); |
| 78 | assertEquals("2 minutes", DateUtils.formatDuration(120000)); |
Roozbeh Pournader | c6bffbf | 2016-12-13 13:58:56 -0800 | [diff] [blame] | 79 | |
| 80 | assertEquals("1 minute", DateUtils.formatDuration(60000, DateUtils.LENGTH_LONG)); |
| 81 | assertEquals("2 minutes", DateUtils.formatDuration(120000, DateUtils.LENGTH_LONG)); |
| 82 | |
| 83 | assertEquals("1 min", DateUtils.formatDuration(60000, DateUtils.LENGTH_SHORT)); |
| 84 | assertEquals("2 min", DateUtils.formatDuration(120000, DateUtils.LENGTH_SHORT)); |
| 85 | |
| 86 | assertEquals("1m", DateUtils.formatDuration(60000, DateUtils.LENGTH_SHORTEST)); |
| 87 | assertEquals("2m", DateUtils.formatDuration(120000, DateUtils.LENGTH_SHORTEST)); |
Jeff Sharkey | 53f6e8a | 2012-11-13 16:23:59 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Andrei Stingaceanu | bbe51f8 | 2017-04-28 14:48:17 +0100 | [diff] [blame] | 90 | @Test |
| 91 | public void test_formatDuration_Hours() { |
Jeff Sharkey | 53f6e8a | 2012-11-13 16:23:59 -0800 | [diff] [blame] | 92 | assertEquals("59 minutes", DateUtils.formatDuration(3540000)); |
| 93 | assertEquals("1 hour", DateUtils.formatDuration(3600000)); |
| 94 | assertEquals("48 hours", DateUtils.formatDuration(172800000)); |
Roozbeh Pournader | c6bffbf | 2016-12-13 13:58:56 -0800 | [diff] [blame] | 95 | |
| 96 | assertEquals("1 hour", DateUtils.formatDuration(3600000, DateUtils.LENGTH_LONG)); |
| 97 | assertEquals("48 hours", DateUtils.formatDuration(172800000, DateUtils.LENGTH_LONG)); |
| 98 | |
| 99 | assertEquals("1 hr", DateUtils.formatDuration(3600000, DateUtils.LENGTH_SHORT)); |
| 100 | assertEquals("48 hr", DateUtils.formatDuration(172800000, DateUtils.LENGTH_SHORT)); |
| 101 | |
| 102 | assertEquals("1h", DateUtils.formatDuration(3600000, DateUtils.LENGTH_SHORTEST)); |
| 103 | assertEquals("48h", DateUtils.formatDuration(172800000, DateUtils.LENGTH_SHORTEST)); |
Jeff Sharkey | 53f6e8a | 2012-11-13 16:23:59 -0800 | [diff] [blame] | 104 | } |
Andrei Stingaceanu | bbe51f8 | 2017-04-28 14:48:17 +0100 | [diff] [blame] | 105 | |
| 106 | private void setLocales(LocaleList locales) { |
| 107 | final Resources systemResources = Resources.getSystem(); |
| 108 | final Configuration config = new Configuration(systemResources.getConfiguration()); |
| 109 | config.setLocales(locales); |
| 110 | // This is not very safe to call, but since DateUtils.formatDuration() is a static method |
| 111 | // (it gets its format strings from the system resources), we can't pass a modified Context |
| 112 | // to it. |
| 113 | systemResources.updateConfiguration(config, null); |
| 114 | } |
| 115 | |
Jeff Sharkey | 53f6e8a | 2012-11-13 16:23:59 -0800 | [diff] [blame] | 116 | } |