1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package com.android.internal.os; 17 18 import android.os.BatteryStats; 19 import android.support.test.filters.SmallTest; 20 21 import junit.framework.TestCase; 22 23 /** 24 * Test BatteryStatsImpl.StopwatchTimer. 25 */ 26 public class BatteryStatsStopwatchTimerTest extends TestCase { 27 28 // Primarily testing previous bug that incremented count when timeBase was off and bug that gave 29 // negative values of count. 30 @SmallTest 31 public void testCount() throws Exception { 32 final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms 33 final BatteryStatsImpl.TimeBase timeBase = new BatteryStatsImpl.TimeBase(); 34 timeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime()); 35 final BatteryStatsImpl.StopwatchTimer timer = new BatteryStatsImpl.StopwatchTimer(clocks, 36 null, BatteryStats.SENSOR, null, timeBase); 37 int expectedCount = 0; 38 39 // for timeBase off tests 40 timeBase.setRunning(false, 1000 * clocks.realtime, 1000 * clocks.realtime); 41 42 // timeBase off, start, stop 43 timer.startRunningLocked(updateTime(clocks, 100)); // start 44 // Used to fail due to b/36730213 increasing count. 45 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 46 timer.startRunningLocked(updateTime(clocks, 110)); // start 47 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 48 timer.stopRunningLocked(updateTime(clocks, 120)); // stop 49 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 50 timer.stopRunningLocked(updateTime(clocks, 130)); // stop 51 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 52 53 // timeBase off, start and immediately stop 54 timer.startRunningLocked(updateTime(clocks, 200)); // start 55 timer.stopRunningLocked(updateTime(clocks, 200)); // stop 56 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 57 58 // timeBase off, start, reset, stop 59 timer.startRunningLocked(updateTime(clocks, 300)); // start 60 updateTime(clocks, 310); 61 timeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime()); 62 timer.reset(false); 63 expectedCount = 0; // count will be reset by reset() 64 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 65 timer.stopRunningLocked(updateTime(clocks, 350)); // stop 66 // Used to fail due to b/30099724 giving -1. 67 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 68 69 // timeBase off, start and immediately reset, stop 70 timer.startRunningLocked(updateTime(clocks, 400)); // start 71 timer.reset(false); 72 expectedCount = 0; // count will be reset by reset() 73 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 74 timer.stopRunningLocked(updateTime(clocks, 450)); // stop 75 // Used to fail due to b/30099724 giving -1. 76 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 77 78 79 // for timeBase on tests 80 updateTime(clocks, 2000); 81 timeBase.setRunning(true, 1000 * clocks.realtime, 1000 * clocks.realtime); 82 assertFalse(timer.isRunningLocked()); 83 84 // timeBase on, start, stop 85 timer.startRunningLocked(updateTime(clocks, 2100)); // start 86 expectedCount++; 87 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 88 timer.startRunningLocked(updateTime(clocks, 2110)); // start 89 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 90 timer.stopRunningLocked(updateTime(clocks, 2120)); // stop 91 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 92 timer.stopRunningLocked(updateTime(clocks, 2130)); // stop 93 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 94 95 // timeBase on, start and immediately stop 96 timer.startRunningLocked(updateTime(clocks, 2200)); // start 97 timer.stopRunningLocked(updateTime(clocks, 2200)); // stop 98 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 99 100 // timeBase on, start, reset, stop 101 timer.startRunningLocked(updateTime(clocks, 2300)); // start 102 updateTime(clocks, 2310); 103 timeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime()); 104 timer.reset(false); 105 expectedCount = 0; // count will be reset by reset() 106 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 107 timer.stopRunningLocked(updateTime(clocks, 2350)); // stop 108 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 109 110 // timeBase on, start and immediately reset, stop 111 timer.startRunningLocked(updateTime(clocks, 2400)); // start 112 timer.reset(false); 113 expectedCount = 0; // count will be reset by reset() 114 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 115 timer.stopRunningLocked(updateTime(clocks, 2450)); // stop 116 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 117 118 119 // change timeBase tests 120 // timeBase off, start 121 updateTime(clocks, 3000); 122 timeBase.setRunning(false, 1000 * clocks.realtime, 1000 * clocks.realtime); 123 timer.startRunningLocked(updateTime(clocks, 3100)); // start 124 // Used to fail due to b/36730213 increasing count. 125 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 126 // timeBase on, stop 127 updateTime(clocks, 3200); 128 timeBase.setRunning(true, 1000 * clocks.realtime, 1000 * clocks.realtime); 129 timer.stopRunningLocked(updateTime(clocks, 3300)); // stop 130 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 131 132 // timeBase on, start 133 timer.startRunningLocked(updateTime(clocks, 3400)); // start 134 expectedCount++; 135 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 136 // timeBase off, stop 137 updateTime(clocks, 3500); 138 timeBase.setRunning(false, 1000 * clocks.realtime, 1000 * clocks.realtime); 139 timer.stopRunningLocked(updateTime(clocks, 3600)); // stop 140 assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); 141 } 142 143 private static long updateTime(MockClocks clocks, long time) { 144 return clocks.realtime = clocks.uptime = time; 145 } 146 } 147