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"; |
Salvador Martinez | b9e8cfa | 2018-04-05 11:05:40 -0700 | [diff] [blame] | 45 | // matches a time (ex: '1:15 PM', '2 AM', '23:00') |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 46 | private static final String TIME_OF_DAY_REGEX = " (\\d)+:?(\\d)* ((AM)*)|((PM)*)"; |
Salvador Martinez | b9bac49 | 2018-03-06 13:40:22 -0800 | [diff] [blame] | 47 | // matches a percentage with parenthesis (ex: '(10%)') |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 48 | private static final String PERCENTAGE_REGEX = " \\(\\d?\\d%\\)"; |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 49 | |
| 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 Martinez | b9bac49 | 2018-03-06 13:40:22 -0800 | [diff] [blame] | 70 | // 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 Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 76 | // shortened string should not have extra text |
Salvador Martinez | b9bac49 | 2018-03-06 13:40:22 -0800 | [diff] [blame] | 77 | // 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 Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 82 | } |
| 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 Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 95 | // We only have % when it is provided |
Salvador Martinez | b9bac49 | 2018-03-06 13:40:22 -0800 | [diff] [blame] | 96 | // 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 Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 102 | // shortened string should not have extra text |
Salvador Martinez | b9bac49 | 2018-03-06 13:40:22 -0800 | [diff] [blame] | 103 | // 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 Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 110 | @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 Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 122 | assertThat(info).isEqualTo("Phone may shutdown soon (10%)"); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 123 | // shortened string should not have percentage |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 124 | assertThat(info2).isEqualTo("Phone may shutdown soon"); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 125 | } |
| 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 Yu | 128f563 | 2018-03-21 15:56:38 -0700 | [diff] [blame] | 139 | assertThat(info).isEqualTo("Less than 15 min remaining"); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 140 | // Add percentage to string when provided |
Lei Yu | 128f563 | 2018-03-21 15:56:38 -0700 | [diff] [blame] | 141 | assertThat(info2).isEqualTo("Less than 15 min remaining (10%)"); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Test |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 145 | public void testGetBatteryRemainingStringFormatted_betweenOneAndTwoDays_usesCorrectString() { |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 146 | String info = PowerUtil.getBatteryRemainingStringFormatted(mContext, |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 147 | THIRTY_HOURS_MILLIS, |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 148 | null /* percentageString */, |
| 149 | true /* basedOnUsage */); |
| 150 | String info2 = PowerUtil.getBatteryRemainingStringFormatted(mContext, |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 151 | THIRTY_HOURS_MILLIS, |
| 152 | TEST_BATTERY_LEVEL_10 /* percentageString */, |
| 153 | false /* basedOnUsage */); |
| 154 | |
| 155 | // We only add special mention for the long string |
Lei Yu | 128f563 | 2018-03-21 15:56:38 -0700 | [diff] [blame] | 156 | 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] | 157 | // shortened string should not have extra text |
Lei Yu | 128f563 | 2018-03-21 15:56:38 -0700 | [diff] [blame] | 158 | assertThat(info2).isEqualTo("About 1 day, 6 hr left (10%)"); |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 159 | } |
| 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 Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 169 | TEST_BATTERY_LEVEL_10 /* percentageString */, |
| 170 | true /* basedOnUsage */); |
| 171 | |
| 172 | // shortened string should not have percentage |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 173 | assertThat(info).isEqualTo("More than 2 days remaining"); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 174 | // Add percentage to string when provided |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 175 | assertThat(info2).isEqualTo("More than 2 days remaining (10%)"); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 176 | } |
Salvador Martinez | 2ee2b0a | 2018-04-13 13:34:46 -0700 | [diff] [blame] | 177 | |
| 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 Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 191 | } |