blob: c6cfdb80d851316d17bb764ab1e1dc270c0a5cc0 [file] [log] [blame]
jackqdyulei5dc1f362017-02-17 14:41:05 -08001/*
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 */
16package com.android.settingslib;
17
18import org.junit.Test;
19import org.junit.runner.RunWith;
20import org.robolectric.annotation.Config;
21
22import static com.google.common.truth.Truth.assertThat;
23
Daniel Nishi53982802017-05-23 15:54:34 -070024import static org.mockito.Matchers.eq;
25import static org.mockito.Mockito.mock;
26import static org.mockito.Mockito.when;
27
28import android.content.res.Resources;
29
Tony Mantler4ca9ebd2017-07-31 10:54:20 -070030@RunWith(SettingsLibRobolectricTestRunner.class)
jackqdyulei5dc1f362017-02-17 14:41:05 -080031@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
32public 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 Nishi53982802017-05-23 15:54:34 -070063
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 }
jackqdyulei5dc1f362017-02-17 14:41:05 -080077}