jackqdyulei | 5dc1f36 | 2017-02-17 14:41:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.settingslib; |
| 17 | |
| 18 | import org.junit.Test; |
| 19 | import org.junit.runner.RunWith; |
| 20 | import org.robolectric.annotation.Config; |
| 21 | |
| 22 | import static com.google.common.truth.Truth.assertThat; |
| 23 | |
Daniel Nishi | 5398280 | 2017-05-23 15:54:34 -0700 | [diff] [blame] | 24 | import static org.mockito.Matchers.eq; |
| 25 | import static org.mockito.Mockito.mock; |
| 26 | import static org.mockito.Mockito.when; |
| 27 | |
| 28 | import android.content.res.Resources; |
| 29 | |
Tony Mantler | 4ca9ebd | 2017-07-31 10:54:20 -0700 | [diff] [blame] | 30 | @RunWith(SettingsLibRobolectricTestRunner.class) |
jackqdyulei | 5dc1f36 | 2017-02-17 14:41:05 -0800 | [diff] [blame] | 31 | @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) |
| 32 | public class UtilsTest { |
| 33 | private static final double[] TEST_PERCENTAGES = {0, 0.4, 0.5, 0.6, 49, 49.3, 49.8, 50, 100}; |
| 34 | private static final String PERCENTAGE_0 = "0%"; |
| 35 | private static final String PERCENTAGE_1 = "1%"; |
| 36 | private static final String PERCENTAGE_49 = "49%"; |
| 37 | private static final String PERCENTAGE_50 = "50%"; |
| 38 | private static final String PERCENTAGE_100 = "100%"; |
| 39 | |
| 40 | @Test |
| 41 | public void testFormatPercentage_RoundTrue_RoundUpIfPossible() { |
| 42 | final String[] expectedPercentages = {PERCENTAGE_0, PERCENTAGE_0, PERCENTAGE_1, |
| 43 | PERCENTAGE_1, PERCENTAGE_49, PERCENTAGE_49, PERCENTAGE_50, PERCENTAGE_50, |
| 44 | PERCENTAGE_100}; |
| 45 | |
| 46 | for (int i = 0, size = TEST_PERCENTAGES.length; i < size; i++) { |
| 47 | final String percentage = Utils.formatPercentage(TEST_PERCENTAGES[i], true); |
| 48 | assertThat(percentage).isEqualTo(expectedPercentages[i]); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | @Test |
| 53 | public void testFormatPercentage_RoundFalse_NoRound() { |
| 54 | final String[] expectedPercentages = {PERCENTAGE_0, PERCENTAGE_0, PERCENTAGE_0, |
| 55 | PERCENTAGE_0, PERCENTAGE_49, PERCENTAGE_49, PERCENTAGE_49, PERCENTAGE_50, |
| 56 | PERCENTAGE_100}; |
| 57 | |
| 58 | for (int i = 0, size = TEST_PERCENTAGES.length; i < size; i++) { |
| 59 | final String percentage = Utils.formatPercentage(TEST_PERCENTAGES[i], false); |
| 60 | assertThat(percentage).isEqualTo(expectedPercentages[i]); |
| 61 | } |
| 62 | } |
Daniel Nishi | 5398280 | 2017-05-23 15:54:34 -0700 | [diff] [blame] | 63 | |
| 64 | @Test |
| 65 | public void testStorageManagerDaysToRetainUsesResources() { |
| 66 | Resources resources = mock(Resources.class); |
| 67 | when(resources.getInteger( |
| 68 | eq( |
| 69 | com.android |
| 70 | .internal |
| 71 | .R |
| 72 | .integer |
| 73 | .config_storageManagerDaystoRetainDefault))) |
| 74 | .thenReturn(60); |
| 75 | assertThat(Utils.getDefaultStorageManagerDaysToRetain(resources)).isEqualTo(60); |
| 76 | } |
jackqdyulei | 5dc1f36 | 2017-02-17 14:41:05 -0800 | [diff] [blame] | 77 | } |