Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settingslib.utils; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
Fan Zhang | f7802ea | 2018-08-28 15:15:19 -0700 | [diff] [blame] | 20 | |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 21 | import static org.mockito.Mockito.spy; |
| 22 | |
| 23 | import android.content.Context; |
James Lemieux | 5c50dc1 | 2018-02-12 01:30:32 -0800 | [diff] [blame] | 24 | |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | import org.junit.runner.RunWith; |
| 28 | import org.mockito.MockitoAnnotations; |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 29 | import org.robolectric.RobolectricTestRunner; |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 30 | import org.robolectric.RuntimeEnvironment; |
James Lemieux | 5c50dc1 | 2018-02-12 01:30:32 -0800 | [diff] [blame] | 31 | |
| 32 | import java.time.Duration; |
Salvador Martinez | b9bac49 | 2018-03-06 13:40:22 -0800 | [diff] [blame] | 33 | import java.util.regex.Pattern; |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 34 | |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 35 | @RunWith(RobolectricTestRunner.class) |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 36 | public class PowerUtilTest { |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 37 | 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 Tsai | 9753fe6 | 2019-03-06 10:46:46 +0800 | [diff] [blame] | 45 | private static final String EXTEND_PREFIX = "Extend battery life past"; |
Salvador Martinez | b9e8cfa | 2018-04-05 11:05:40 -0700 | [diff] [blame] | 46 | // matches a time (ex: '1:15 PM', '2 AM', '23:00') |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 47 | private static final String TIME_OF_DAY_REGEX = " (\\d)+:?(\\d)* ((AM)*)|((PM)*)"; |
Salvador Martinez | b9bac49 | 2018-03-06 13:40:22 -0800 | [diff] [blame] | 48 | // matches a percentage with parenthesis (ex: '(10%)') |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 49 | private static final String PERCENTAGE_REGEX = " \\(\\d?\\d%\\)"; |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 50 | |
| 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 Martinez | b9bac49 | 2018-03-06 13:40:22 -0800 | [diff] [blame] | 71 | // 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 Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 77 | // shortened string should not have extra text |
Salvador Martinez | b9bac49 | 2018-03-06 13:40:22 -0800 | [diff] [blame] | 78 | // 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 Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 83 | } |
| 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 Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 96 | // We only have % when it is provided |
Salvador Martinez | b9bac49 | 2018-03-06 13:40:22 -0800 | [diff] [blame] | 97 | // 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 Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 103 | // shortened string should not have extra text |
Salvador Martinez | b9bac49 | 2018-03-06 13:40:22 -0800 | [diff] [blame] | 104 | // 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 Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 111 | @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 Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 123 | assertThat(info).isEqualTo("Phone may shutdown soon (10%)"); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 124 | // shortened string should not have percentage |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 125 | assertThat(info2).isEqualTo("Phone may shutdown soon"); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 126 | } |
| 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 Yu | 128f563 | 2018-03-21 15:56:38 -0700 | [diff] [blame] | 140 | assertThat(info).isEqualTo("Less than 15 min remaining"); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 141 | // Add percentage to string when provided |
Lei Yu | 128f563 | 2018-03-21 15:56:38 -0700 | [diff] [blame] | 142 | assertThat(info2).isEqualTo("Less than 15 min remaining (10%)"); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Test |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 146 | public void testGetBatteryRemainingStringFormatted_betweenOneAndTwoDays_usesCorrectString() { |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 147 | String info = PowerUtil.getBatteryRemainingStringFormatted(mContext, |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 148 | THIRTY_HOURS_MILLIS, |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 149 | null /* percentageString */, |
| 150 | true /* basedOnUsage */); |
| 151 | String info2 = PowerUtil.getBatteryRemainingStringFormatted(mContext, |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 152 | THIRTY_HOURS_MILLIS, |
| 153 | TEST_BATTERY_LEVEL_10 /* percentageString */, |
| 154 | false /* basedOnUsage */); |
| 155 | |
| 156 | // We only add special mention for the long string |
Lei Yu | 128f563 | 2018-03-21 15:56:38 -0700 | [diff] [blame] | 157 | assertThat(info).isEqualTo("About 1 day, 6 hr left based on your usage"); |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 158 | // shortened string should not have extra text |
Lei Yu | 128f563 | 2018-03-21 15:56:38 -0700 | [diff] [blame] | 159 | assertThat(info2).isEqualTo("About 1 day, 6 hr left (10%)"); |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 160 | } |
| 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 Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 170 | TEST_BATTERY_LEVEL_10 /* percentageString */, |
| 171 | true /* basedOnUsage */); |
| 172 | |
| 173 | // shortened string should not have percentage |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 174 | assertThat(info).isEqualTo("More than 2 days remaining"); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 175 | // Add percentage to string when provided |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 176 | assertThat(info2).isEqualTo("More than 2 days remaining (10%)"); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 177 | } |
Salvador Martinez | 2ee2b0a | 2018-04-13 13:34:46 -0700 | [diff] [blame] | 178 | |
| 179 | @Test |
Raff Tsai | 9753fe6 | 2019-03-06 10:46:46 +0800 | [diff] [blame] | 180 | 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 Martinez | 2ee2b0a | 2018-04-13 13:34:46 -0700 | [diff] [blame] | 198 | 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 Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 210 | } |