blob: 61fdbd54ead139502e8536e1ee46d0f295776d81 [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";
Raff Tsai9753fe62019-03-06 10:46:46 +080045 private static final String EXTEND_PREFIX = "Extend battery life past";
Salvador Martinezb9e8cfa2018-04-05 11:05:40 -070046 // matches a time (ex: '1:15 PM', '2 AM', '23:00')
James Lemieuxec3ad9e2018-11-28 17:49:14 -080047 private static final String TIME_OF_DAY_REGEX = " (\\d)+:?(\\d)* ((AM)*)|((PM)*)";
Salvador Martinezb9bac492018-03-06 13:40:22 -080048 // matches a percentage with parenthesis (ex: '(10%)')
James Lemieuxec3ad9e2018-11-28 17:49:14 -080049 private static final String PERCENTAGE_REGEX = " \\(\\d?\\d%\\)";
Salvador Martinezeb9ab292018-01-19 17:50:24 -080050
51 private Context mContext;
52
53 @Before
54 public void setup() {
55 MockitoAnnotations.initMocks(this);
56 mContext = spy(RuntimeEnvironment.application);
57 }
58
59 @Test
60 public void testGetBatteryRemainingStringFormatted_moreThanFifteenMinutes_withPercentage() {
61 String info = PowerUtil.getBatteryRemainingStringFormatted(mContext,
62 SEVENTEEN_MIN_MILLIS,
63 TEST_BATTERY_LEVEL_10,
64 true /* basedOnUsage */);
65 String info2 = PowerUtil.getBatteryRemainingStringFormatted(mContext,
66 SEVENTEEN_MIN_MILLIS,
67 TEST_BATTERY_LEVEL_10,
68 false /* basedOnUsage */);
69
70 // We only add special mention for the long string
Salvador Martinezb9bac492018-03-06 13:40:22 -080071 // ex: Will last about 1:15 PM based on your usage (10%)
72 assertThat(info).containsMatch(Pattern.compile(
73 NORMAL_CASE_EXPECTED_PREFIX
74 + TIME_OF_DAY_REGEX
75 + ENHANCED_SUFFIX
76 + PERCENTAGE_REGEX));
Salvador Martinezeb9ab292018-01-19 17:50:24 -080077 // shortened string should not have extra text
Salvador Martinezb9bac492018-03-06 13:40:22 -080078 // ex: Will last about 1:15 PM (10%)
79 assertThat(info2).containsMatch(Pattern.compile(
80 NORMAL_CASE_EXPECTED_PREFIX
81 + TIME_OF_DAY_REGEX
82 + PERCENTAGE_REGEX));
Salvador Martinezeb9ab292018-01-19 17:50:24 -080083 }
84
85 @Test
86 public void testGetBatteryRemainingStringFormatted_moreThanFifteenMinutes_noPercentage() {
87 String info = PowerUtil.getBatteryRemainingStringFormatted(mContext,
88 SEVENTEEN_MIN_MILLIS,
89 null /* percentageString */,
90 true /* basedOnUsage */);
91 String info2 = PowerUtil.getBatteryRemainingStringFormatted(mContext,
92 SEVENTEEN_MIN_MILLIS,
93 null /* percentageString */,
94 false /* basedOnUsage */);
95
Salvador Martinezeec87362018-02-27 12:57:49 -080096 // We only have % when it is provided
Salvador Martinezb9bac492018-03-06 13:40:22 -080097 // ex: Will last about 1:15 PM based on your usage
98 assertThat(info).containsMatch(Pattern.compile(
99 NORMAL_CASE_EXPECTED_PREFIX
100 + TIME_OF_DAY_REGEX
101 + ENHANCED_SUFFIX
102 + "(" + PERCENTAGE_REGEX + "){0}")); // no percentage
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800103 // shortened string should not have extra text
Salvador Martinezb9bac492018-03-06 13:40:22 -0800104 // ex: Will last about 1:15 PM
105 assertThat(info2).containsMatch(Pattern.compile(
106 NORMAL_CASE_EXPECTED_PREFIX
107 + TIME_OF_DAY_REGEX
108 + "(" + PERCENTAGE_REGEX + "){0}")); // no percentage
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800109 }
110
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800111 @Test
112 public void testGetBatteryRemainingStringFormatted_lessThanSevenMinutes_usesCorrectString() {
113 String info = PowerUtil.getBatteryRemainingStringFormatted(mContext,
114 FIVE_MINUTES_MILLIS,
115 TEST_BATTERY_LEVEL_10 /* percentageString */,
116 true /* basedOnUsage */);
117 String info2 = PowerUtil.getBatteryRemainingStringFormatted(mContext,
118 FIVE_MINUTES_MILLIS,
119 null /* percentageString */,
120 true /* basedOnUsage */);
121
122 // additional battery percentage in this string
Salvador Martinezeec87362018-02-27 12:57:49 -0800123 assertThat(info).isEqualTo("Phone may shutdown soon (10%)");
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800124 // shortened string should not have percentage
Salvador Martinezeec87362018-02-27 12:57:49 -0800125 assertThat(info2).isEqualTo("Phone may shutdown soon");
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800126 }
127
128 @Test
129 public void testGetBatteryRemainingStringFormatted_betweenSevenAndFifteenMinutes_usesCorrectString() {
130 String info = PowerUtil.getBatteryRemainingStringFormatted(mContext,
131 TEN_MINUTES_MILLIS,
132 null /* percentageString */,
133 true /* basedOnUsage */);
134 String info2 = PowerUtil.getBatteryRemainingStringFormatted(mContext,
135 TEN_MINUTES_MILLIS,
136 TEST_BATTERY_LEVEL_10 /* percentageString */,
137 true /* basedOnUsage */);
138
139 // shortened string should not have percentage
Lei Yu128f5632018-03-21 15:56:38 -0700140 assertThat(info).isEqualTo("Less than 15 min remaining");
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800141 // Add percentage to string when provided
Lei Yu128f5632018-03-21 15:56:38 -0700142 assertThat(info2).isEqualTo("Less than 15 min remaining (10%)");
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800143 }
144
145 @Test
Salvador Martinezeec87362018-02-27 12:57:49 -0800146 public void testGetBatteryRemainingStringFormatted_betweenOneAndTwoDays_usesCorrectString() {
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800147 String info = PowerUtil.getBatteryRemainingStringFormatted(mContext,
Salvador Martinezeec87362018-02-27 12:57:49 -0800148 THIRTY_HOURS_MILLIS,
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800149 null /* percentageString */,
150 true /* basedOnUsage */);
151 String info2 = PowerUtil.getBatteryRemainingStringFormatted(mContext,
Salvador Martinezeec87362018-02-27 12:57:49 -0800152 THIRTY_HOURS_MILLIS,
153 TEST_BATTERY_LEVEL_10 /* percentageString */,
154 false /* basedOnUsage */);
155
156 // We only add special mention for the long string
Lei Yu128f5632018-03-21 15:56:38 -0700157 assertThat(info).isEqualTo("About 1 day, 6 hr left based on your usage");
Salvador Martinezeec87362018-02-27 12:57:49 -0800158 // shortened string should not have extra text
Lei Yu128f5632018-03-21 15:56:38 -0700159 assertThat(info2).isEqualTo("About 1 day, 6 hr left (10%)");
Salvador Martinezeec87362018-02-27 12:57:49 -0800160 }
161
162 @Test
163 public void testGetBatteryRemainingStringFormatted_moreThanTwoDays_usesCorrectString() {
164 String info = PowerUtil.getBatteryRemainingStringFormatted(mContext,
165 THREE_DAYS_MILLIS,
166 null /* percentageString */,
167 true /* basedOnUsage */);
168 String info2 = PowerUtil.getBatteryRemainingStringFormatted(mContext,
169 THREE_DAYS_MILLIS,
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800170 TEST_BATTERY_LEVEL_10 /* percentageString */,
171 true /* basedOnUsage */);
172
173 // shortened string should not have percentage
Salvador Martinezeec87362018-02-27 12:57:49 -0800174 assertThat(info).isEqualTo("More than 2 days remaining");
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800175 // Add percentage to string when provided
Salvador Martinezeec87362018-02-27 12:57:49 -0800176 assertThat(info2).isEqualTo("More than 2 days remaining (10%)");
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800177 }
Salvador Martinez2ee2b0a2018-04-13 13:34:46 -0700178
179 @Test
Raff Tsai9753fe62019-03-06 10:46:46 +0800180 public void getBatteryTipStringFormatted_moreThanOneDay_usesCorrectString() {
181 String info = PowerUtil.getBatteryTipStringFormatted(mContext,
182 THREE_DAYS_MILLIS);
183
184 assertThat(info).isEqualTo("More than 3 days remaining");
185 }
186
187 @Test
188 public void getBatteryTipStringFormatted_lessThanOneDay_usesCorrectString() {
189 String info = PowerUtil.getBatteryTipStringFormatted(mContext,
190 SEVENTEEN_MIN_MILLIS);
191
192 // ex: Extend battery life past 1:15 PM
193 assertThat(info).containsMatch(Pattern.compile(
194 EXTEND_PREFIX + TIME_OF_DAY_REGEX));
195 }
196
197 @Test
Salvador Martinez2ee2b0a2018-04-13 13:34:46 -0700198 public void testRoundToNearestThreshold_roundsCorrectly() {
199 // test some pretty normal values
200 assertThat(PowerUtil.roundTimeToNearestThreshold(1200, 1000)).isEqualTo(1000);
201 assertThat(PowerUtil.roundTimeToNearestThreshold(800, 1000)).isEqualTo(1000);
202 assertThat(PowerUtil.roundTimeToNearestThreshold(1000, 1000)).isEqualTo(1000);
203
204 // test the weird stuff
205 assertThat(PowerUtil.roundTimeToNearestThreshold(80, -200)).isEqualTo(0);
206 assertThat(PowerUtil.roundTimeToNearestThreshold(-150, 100)).isEqualTo(200);
207 assertThat(PowerUtil.roundTimeToNearestThreshold(-120, 100)).isEqualTo(100);
208 assertThat(PowerUtil.roundTimeToNearestThreshold(-200, -75)).isEqualTo(225);
209 }
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800210}