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; 20 import static org.mockito.Mockito.spy; 21 22 import android.content.Context; 23 24 import com.android.settingslib.SettingsLibRobolectricTestRunner; 25 26 import org.junit.Before; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.mockito.MockitoAnnotations; 30 import org.robolectric.RuntimeEnvironment; 31 32 import java.time.Duration; 33 import java.util.regex.Pattern; 34 35 @RunWith(SettingsLibRobolectricTestRunner.class) 36 public class PowerUtilTest { 37 public static final String TEST_BATTERY_LEVEL_10 = "10%"; 38 public static final long SEVENTEEN_MIN_MILLIS = Duration.ofMinutes(17).toMillis(); 39 public static final long FIVE_MINUTES_MILLIS = Duration.ofMinutes(5).toMillis(); 40 public static final long TEN_MINUTES_MILLIS = Duration.ofMinutes(10).toMillis(); 41 public static final long THREE_DAYS_MILLIS = Duration.ofDays(3).toMillis(); 42 public static final long THIRTY_HOURS_MILLIS = Duration.ofHours(30).toMillis(); 43 public static final String NORMAL_CASE_EXPECTED_PREFIX = "Should last until about"; 44 public static final String ENHANCED_SUFFIX = " based on your usage"; 45 // matches a time (ex: '1:15 PM', '2 AM', '23:00') 46 public static final String TIME_OF_DAY_REGEX = " (\\d)+:?(\\d)* ((AM)*)|((PM)*)"; 47 // matches a percentage with parenthesis (ex: '(10%)') 48 public static final String PERCENTAGE_REGEX = " \\(\\d?\\d%\\)"; 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 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)); 76 // shortened string should not have extra text 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)); 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 95 // We only have % when it is provided 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 102 // shortened string should not have extra text 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 108 } 109 110 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 123 assertThat(info).isEqualTo("Phone may shutdown soon (10%)"); 124 // shortened string should not have percentage 125 assertThat(info2).isEqualTo("Phone may shutdown soon"); 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 140 assertThat(info).isEqualTo("Less than 15 min remaining"); 141 // Add percentage to string when provided 142 assertThat(info2).isEqualTo("Less than 15 min remaining (10%)"); 143 } 144 145 @Test 146 public void testGetBatteryRemainingStringFormatted_betweenOneAndTwoDays_usesCorrectString() { 147 String info = PowerUtil.getBatteryRemainingStringFormatted(mContext, 148 THIRTY_HOURS_MILLIS, 149 null /* percentageString */, 150 true /* basedOnUsage */); 151 String info2 = PowerUtil.getBatteryRemainingStringFormatted(mContext, 152 THIRTY_HOURS_MILLIS, 153 TEST_BATTERY_LEVEL_10 /* percentageString */, 154 false /* basedOnUsage */); 155 156 // We only add special mention for the long string 157 assertThat(info).isEqualTo("About 1 day, 6 hr left based on your usage"); 158 // shortened string should not have extra text 159 assertThat(info2).isEqualTo("About 1 day, 6 hr left (10%)"); 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, 170 TEST_BATTERY_LEVEL_10 /* percentageString */, 171 true /* basedOnUsage */); 172 173 // shortened string should not have percentage 174 assertThat(info).isEqualTo("More than 2 days remaining"); 175 // Add percentage to string when provided 176 assertThat(info2).isEqualTo("More than 2 days remaining (10%)"); 177 } 178 179 @Test 180 public void testRoundToNearestThreshold_roundsCorrectly() { 181 // test some pretty normal values 182 assertThat(PowerUtil.roundTimeToNearestThreshold(1200, 1000)).isEqualTo(1000); 183 assertThat(PowerUtil.roundTimeToNearestThreshold(800, 1000)).isEqualTo(1000); 184 assertThat(PowerUtil.roundTimeToNearestThreshold(1000, 1000)).isEqualTo(1000); 185 186 // test the weird stuff 187 assertThat(PowerUtil.roundTimeToNearestThreshold(80, -200)).isEqualTo(0); 188 assertThat(PowerUtil.roundTimeToNearestThreshold(-150, 100)).isEqualTo(200); 189 assertThat(PowerUtil.roundTimeToNearestThreshold(-120, 100)).isEqualTo(100); 190 assertThat(PowerUtil.roundTimeToNearestThreshold(-200, -75)).isEqualTo(225); 191 } 192 } 193