2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
16 package com.android.internal.os;
18 import android.os.BatteryStats;
19 import android.os.Parcel;
20 import android.support.test.filters.SmallTest;
22 import junit.framework.TestCase;
24 import org.mockito.Mockito;
26 public class BatteryStatsSamplingTimerTest extends TestCase {
29 public void testSettingStalePreservesData() throws Exception {
30 final MockClocks clocks = new MockClocks();
31 final BatteryStatsImpl.SamplingTimer timer = new BatteryStatsImpl.SamplingTimer(clocks,
32 Mockito.mock(BatteryStatsImpl.TimeBase.class));
34 timer.onTimeStarted(100, 100, 100);
36 // First update is absorbed.
41 assertEquals(1, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
42 assertEquals(10, timer.getTotalTimeLocked(200, BatteryStats.STATS_SINCE_CHARGED));
46 assertEquals(1, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
47 assertEquals(10, timer.getTotalTimeLocked(200, BatteryStats.STATS_SINCE_CHARGED));
49 timer.onTimeStopped(200, 200, 200);
51 assertEquals(1, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
52 assertEquals(10, timer.getTotalTimeLocked(200, BatteryStats.STATS_SINCE_CHARGED));
56 public void testEndSampleAndContinueWhenTimeOrCountDecreases() throws Exception {
57 final MockClocks clocks = new MockClocks();
58 final BatteryStatsImpl.TimeBase timeBase = Mockito.mock(BatteryStatsImpl.TimeBase.class);
59 final BatteryStatsImpl.SamplingTimer timer = new BatteryStatsImpl.SamplingTimer(clocks,
62 // First once is absorbed.
67 assertEquals(0, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
68 assertEquals(0, timer.getTotalTimeLocked(200, BatteryStats.STATS_SINCE_CHARGED));
70 // This is less than we currently have, so we will end the sample. Time isn't running, so
71 // nothing should happen.
74 assertEquals(0, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
75 assertEquals(0, timer.getTotalTimeLocked(200, BatteryStats.STATS_SINCE_CHARGED));
77 timer.onTimeStarted(100, 100, 100);
82 assertEquals(100, timer.getTotalTimeLocked(200, BatteryStats.STATS_SINCE_CHARGED));
83 assertEquals(10, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
85 // This is less than we currently have, so we should end our sample and continue with the
86 // entire amount updated here.
89 assertEquals(150, timer.getTotalTimeLocked(200, BatteryStats.STATS_SINCE_CHARGED));
90 assertEquals(15, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
92 timer.onTimeStopped(200, 200, 200);
94 assertEquals(150, timer.getTotalTimeLocked(200, BatteryStats.STATS_SINCE_CHARGED));
95 assertEquals(15, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
99 public void testFirstUpdateIsAbsorbed() throws Exception {
100 final MockClocks clocks = new MockClocks();
101 final BatteryStatsImpl.TimeBase timeBase = Mockito.mock(BatteryStatsImpl.TimeBase.class);
103 BatteryStatsImpl.SamplingTimer timer = new BatteryStatsImpl.SamplingTimer(clocks, timeBase);
105 // This should be absorbed because it is our first update and we don't know what
106 // was being counted before.
109 assertEquals(0, timer.getTotalTimeLocked(10, BatteryStats.STATS_SINCE_CHARGED));
110 assertEquals(0, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
112 timer = new BatteryStatsImpl.SamplingTimer(clocks, timeBase);
113 timer.onTimeStarted(100, 100, 100);
115 // This should be absorbed.
118 assertEquals(0, timer.getTotalTimeLocked(100, BatteryStats.STATS_SINCE_CHARGED));
119 assertEquals(0, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
121 // This should NOT be aborbed, since we've already done that.
124 assertEquals(10, timer.getTotalTimeLocked(100, BatteryStats.STATS_SINCE_CHARGED));
125 assertEquals(1, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
127 timer.onTimeStopped(200, 200, 200);
128 timer.onTimeStarted(300, 300, 300);
130 // This should NOT be absorbed.
133 assertEquals(20, timer.getTotalTimeLocked(300, BatteryStats.STATS_SINCE_CHARGED));
134 assertEquals(2, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
138 public void testSampleTimerSummaryParceling() throws Exception {
139 final MockClocks clocks = new MockClocks();
143 final BatteryStatsImpl.TimeBase timeBase = new BatteryStatsImpl.TimeBase();
144 timeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime());
146 BatteryStatsImpl.SamplingTimer timer = new BatteryStatsImpl.SamplingTimer(clocks, timeBase);
148 // Start running on battery.
149 timeBase.setRunning(true, clocks.uptimeMillis(), clocks.elapsedRealtime());
151 // The first update on battery consumes the values as a way of starting cleanly.
156 clocks.realtime = 20;
159 assertEquals(10, timer.getTotalTimeLocked(clocks.elapsedRealtime(),
160 BatteryStats.STATS_SINCE_CHARGED));
161 assertEquals(1, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
163 // Grab a summary parcel while on battery.
164 final Parcel onBatterySummaryParcel = Parcel.obtain();
165 timer.writeSummaryFromParcelLocked(onBatterySummaryParcel, clocks.elapsedRealtime() * 1000);
166 onBatterySummaryParcel.setDataPosition(0);
168 // Stop running on battery.
169 timeBase.setRunning(false, clocks.uptimeMillis(), clocks.elapsedRealtime());
171 assertEquals(10, timer.getTotalTimeLocked(clocks.elapsedRealtime(),
172 BatteryStats.STATS_SINCE_CHARGED));
173 assertEquals(1, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
175 // Grab a summary parcel while not on battery.
176 final Parcel offBatterySummaryParcel = Parcel.obtain();
177 timer.writeSummaryFromParcelLocked(offBatterySummaryParcel,
178 clocks.elapsedRealtime() * 1000);
179 offBatterySummaryParcel.setDataPosition(0);
181 // Read the on battery summary from the parcel.
182 BatteryStatsImpl.SamplingTimer unparceledTimer = new BatteryStatsImpl.SamplingTimer(
184 unparceledTimer.readSummaryFromParcelLocked(onBatterySummaryParcel);
186 assertEquals(10, unparceledTimer.getTotalTimeLocked(0, BatteryStats.STATS_SINCE_CHARGED));
187 assertEquals(1, unparceledTimer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
189 // Read the off battery summary from the parcel.
190 unparceledTimer = new BatteryStatsImpl.SamplingTimer(clocks, timeBase);
191 unparceledTimer.readSummaryFromParcelLocked(offBatterySummaryParcel);
193 assertEquals(10, unparceledTimer.getTotalTimeLocked(0, BatteryStats.STATS_SINCE_CHARGED));
194 assertEquals(1, unparceledTimer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));