blob: 7ef31df6ab265c2eb211ea0e1609c7433081eae5 [file] [log] [blame]
Salvador Martinezeb9ab292018-01-19 17:50:24 -08001/*
2 * Copyright (C) 2018 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
17package com.android.settingslib.utils;
18
19import static com.google.common.truth.Truth.assertThat;
Fan Zhangf7802ea2018-08-28 15:15:19 -070020
Salvador Martinezeb9ab292018-01-19 17:50:24 -080021import static org.mockito.Mockito.spy;
22
23import android.content.Context;
James Lemieux5c50dc12018-02-12 01:30:32 -080024
Salvador Martinezeb9ab292018-01-19 17:50:24 -080025import org.junit.Before;
26import org.junit.Test;
27import org.junit.runner.RunWith;
28import org.mockito.MockitoAnnotations;
James Lemieuxec3ad9e2018-11-28 17:49:14 -080029import org.robolectric.RobolectricTestRunner;
Salvador Martinezeb9ab292018-01-19 17:50:24 -080030import org.robolectric.RuntimeEnvironment;
James Lemieux5c50dc12018-02-12 01:30:32 -080031
32import java.time.Duration;
Salvador Martinezb9bac492018-03-06 13:40:22 -080033import java.util.regex.Pattern;
Salvador Martinezeb9ab292018-01-19 17:50:24 -080034
James Lemieuxec3ad9e2018-11-28 17:49:14 -080035@RunWith(RobolectricTestRunner.class)
Salvador Martinezeb9ab292018-01-19 17:50:24 -080036public class PowerUtilTest {
James Lemieuxec3ad9e2018-11-28 17:49:14 -080037 private static final String TEST_BATTERY_LEVEL_10 = "10%";
38 private static final long SEVENTEEN_MIN_MILLIS = Duration.ofMinutes(17).toMillis();
39 private static final long FIVE_MINUTES_MILLIS = Duration.ofMinutes(5).toMillis();
40 private static final long TEN_MINUTES_MILLIS = Duration.ofMinutes(10).toMillis();
41 private static final long THREE_DAYS_MILLIS = Duration.ofDays(3).toMillis();
42 private static final long THIRTY_HOURS_MILLIS = Duration.ofHours(30).toMillis();
43 private static final String NORMAL_CASE_EXPECTED_PREFIX = "Should last until about";
44 private static final String ENHANCED_SUFFIX = " based on your usage";
Salvador Martinezb9e8cfa2018-04-05 11:05:40 -070045 // matches a time (ex: '1:15 PM', '2 AM', '23:00')
James Lemieuxec3ad9e2018-11-28 17:49:14 -080046 private static final String TIME_OF_DAY_REGEX = " (\\d)+:?(\\d)* ((AM)*)|((PM)*)";
Salvador Martinezb9bac492018-03-06 13:40:22 -080047 // matches a percentage with parenthesis (ex: '(10%)')
James Lemieuxec3ad9e2018-11-28 17:49:14 -080048 private static final String PERCENTAGE_REGEX = " \\(\\d?\\d%\\)";
Salvador Martinezeb9ab292018-01-19 17:50:24 -080049
50 private Context mContext;
51
52 @Before
53 public void setup() {
54 MockitoAnnotations.initMocks(this);
55 mContext = spy(RuntimeEnvironment.application);
56 }
57
58 @Test
59 public void testGetBatteryRemainingStringFormatted_moreThanFifteenMinutes_withPercentage() {
60 String info = PowerUtil.getBatteryRemainingStringFormatted(mContext,
61 SEVENTEEN_MIN_MILLIS,
62 TEST_BATTERY_LEVEL_10,
63 true /* basedOnUsage */);
64 String info2 = PowerUtil.getBatteryRemainingStringFormatted(mContext,
65 SEVENTEEN_MIN_MILLIS,
66 TEST_BATTERY_LEVEL_10,
67 false /* basedOnUsage */);
68
69 // We only add special mention for the long string
Salvador Martinezb9bac492018-03-06 13:40:22 -080070 // ex: Will last about 1:15 PM based on your usage (10%)
71 assertThat(info).containsMatch(Pattern.compile(
72 NORMAL_CASE_EXPECTED_PREFIX
73 + TIME_OF_DAY_REGEX
74 + ENHANCED_SUFFIX
75 + PERCENTAGE_REGEX));
Salvador Martinezeb9ab292018-01-19 17:50:24 -080076 // shortened string should not have extra text
Salvador Martinezb9bac492018-03-06 13:40:22 -080077 // ex: Will last about 1:15 PM (10%)
78 assertThat(info2).containsMatch(Pattern.compile(
79 NORMAL_CASE_EXPECTED_PREFIX
80 + TIME_OF_DAY_REGEX
81 + PERCENTAGE_REGEX));
Salvador Martinezeb9ab292018-01-19 17:50:24 -080082 }
83
84 @Test
85 public void testGetBatteryRemainingStringFormatted_moreThanFifteenMinutes_noPercentage() {
86 String info = PowerUtil.getBatteryRemainingStringFormatted(mContext,
87 SEVENTEEN_MIN_MILLIS,
88 null /* percentageString */,
89 true /* basedOnUsage */);
90 String info2 = PowerUtil.getBatteryRemainingStringFormatted(mContext,
91 SEVENTEEN_MIN_MILLIS,
92 null /* percentageString */,
93 false /* basedOnUsage */);
94
Salvador Martinezeec87362018-02-27 12:57:49 -080095 // We only have % when it is provided
Salvador Martinezb9bac492018-03-06 13:40:22 -080096 // ex: Will last about 1:15 PM based on your usage
97 assertThat(info).containsMatch(Pattern.compile(
98 NORMAL_CASE_EXPECTED_PREFIX
99 + TIME_OF_DAY_REGEX
100 + ENHANCED_SUFFIX
101 + "(" + PERCENTAGE_REGEX + "){0}")); // no percentage
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800102 // shortened string should not have extra text
Salvador Martinezb9bac492018-03-06 13:40:22 -0800103 // ex: Will last about 1:15 PM
104 assertThat(info2).containsMatch(Pattern.compile(
105 NORMAL_CASE_EXPECTED_PREFIX
106 + TIME_OF_DAY_REGEX
107 + "(" + PERCENTAGE_REGEX + "){0}")); // no percentage
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800108 }
109
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800110 @Test
111 public void testGetBatteryRemainingStringFormatted_lessThanSevenMinutes_usesCorrectString() {
112 String info = PowerUtil.getBatteryRemainingStringFormatted(mContext,
113 FIVE_MINUTES_MILLIS,
114 TEST_BATTERY_LEVEL_10 /* percentageString */,
115 true /* basedOnUsage */);
116 String info2 = PowerUtil.getBatteryRemainingStringFormatted(mContext,
117 FIVE_MINUTES_MILLIS,
118 null /* percentageString */,
119 true /* basedOnUsage */);
120
121 // additional battery percentage in this string
Salvador Martinezeec87362018-02-27 12:57:49 -0800122 assertThat(info).isEqualTo("Phone may shutdown soon (10%)");
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800123 // shortened string should not have percentage
Salvador Martinezeec87362018-02-27 12:57:49 -0800124 assertThat(info2).isEqualTo("Phone may shutdown soon");
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800125 }
126
127 @Test
128 public void testGetBatteryRemainingStringFormatted_betweenSevenAndFifteenMinutes_usesCorrectString() {
129 String info = PowerUtil.getBatteryRemainingStringFormatted(mContext,
130 TEN_MINUTES_MILLIS,
131 null /* percentageString */,
132 true /* basedOnUsage */);
133 String info2 = PowerUtil.getBatteryRemainingStringFormatted(mContext,
134 TEN_MINUTES_MILLIS,
135 TEST_BATTERY_LEVEL_10 /* percentageString */,
136 true /* basedOnUsage */);
137
138 // shortened string should not have percentage
Lei Yu128f5632018-03-21 15:56:38 -0700139 assertThat(info).isEqualTo("Less than 15 min remaining");
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800140 // Add percentage to string when provided
Lei Yu128f5632018-03-21 15:56:38 -0700141 assertThat(info2).isEqualTo("Less than 15 min remaining (10%)");
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800142 }
143
144 @Test
Salvador Martinezeec87362018-02-27 12:57:49 -0800145 public void testGetBatteryRemainingStringFormatted_betweenOneAndTwoDays_usesCorrectString() {
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800146 String info = PowerUtil.getBatteryRemainingStringFormatted(mContext,
Salvador Martinezeec87362018-02-27 12:57:49 -0800147 THIRTY_HOURS_MILLIS,
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800148 null /* percentageString */,
149 true /* basedOnUsage */);
150 String info2 = PowerUtil.getBatteryRemainingStringFormatted(mContext,
Salvador Martinezeec87362018-02-27 12:57:49 -0800151 THIRTY_HOURS_MILLIS,
152 TEST_BATTERY_LEVEL_10 /* percentageString */,
153 false /* basedOnUsage */);
154
155 // We only add special mention for the long string
Lei Yu128f5632018-03-21 15:56:38 -0700156 assertThat(info).isEqualTo("About 1 day, 6 hr left based on your usage");
Salvador Martinezeec87362018-02-27 12:57:49 -0800157 // shortened string should not have extra text
Lei Yu128f5632018-03-21 15:56:38 -0700158 assertThat(info2).isEqualTo("About 1 day, 6 hr left (10%)");
Salvador Martinezeec87362018-02-27 12:57:49 -0800159 }
160
161 @Test
162 public void testGetBatteryRemainingStringFormatted_moreThanTwoDays_usesCorrectString() {
163 String info = PowerUtil.getBatteryRemainingStringFormatted(mContext,
164 THREE_DAYS_MILLIS,
165 null /* percentageString */,
166 true /* basedOnUsage */);
167 String info2 = PowerUtil.getBatteryRemainingStringFormatted(mContext,
168 THREE_DAYS_MILLIS,
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800169 TEST_BATTERY_LEVEL_10 /* percentageString */,
170 true /* basedOnUsage */);
171
172 // shortened string should not have percentage
Salvador Martinezeec87362018-02-27 12:57:49 -0800173 assertThat(info).isEqualTo("More than 2 days remaining");
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800174 // Add percentage to string when provided
Salvador Martinezeec87362018-02-27 12:57:49 -0800175 assertThat(info2).isEqualTo("More than 2 days remaining (10%)");
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800176 }
Salvador Martinez2ee2b0a2018-04-13 13:34:46 -0700177
178 @Test
179 public void testRoundToNearestThreshold_roundsCorrectly() {
180 // test some pretty normal values
181 assertThat(PowerUtil.roundTimeToNearestThreshold(1200, 1000)).isEqualTo(1000);
182 assertThat(PowerUtil.roundTimeToNearestThreshold(800, 1000)).isEqualTo(1000);
183 assertThat(PowerUtil.roundTimeToNearestThreshold(1000, 1000)).isEqualTo(1000);
184
185 // test the weird stuff
186 assertThat(PowerUtil.roundTimeToNearestThreshold(80, -200)).isEqualTo(0);
187 assertThat(PowerUtil.roundTimeToNearestThreshold(-150, 100)).isEqualTo(200);
188 assertThat(PowerUtil.roundTimeToNearestThreshold(-120, 100)).isEqualTo(100);
189 assertThat(PowerUtil.roundTimeToNearestThreshold(-200, -75)).isEqualTo(225);
190 }
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800191}