OSDN Git Service

DO NOT MERGE. Grant MMS Uri permissions as the calling UID.
[android-x86/frameworks-base.git] / core / tests / coretests / src / com / android / internal / os / BatteryStatsSamplingTimerTest.java
1 /*
2  * Copyright (C) 2016 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.os.Parcel;
20 import android.support.test.filters.SmallTest;
21
22 import junit.framework.TestCase;
23
24 import org.mockito.Mockito;
25
26 public class BatteryStatsSamplingTimerTest extends TestCase {
27
28     @SmallTest
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));
33
34         timer.onTimeStarted(100, 100, 100);
35
36         // First update is absorbed.
37         timer.update(10, 1);
38
39         timer.update(20, 2);
40
41         assertEquals(1, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
42         assertEquals(10, timer.getTotalTimeLocked(200, BatteryStats.STATS_SINCE_CHARGED));
43
44         timer.endSample();
45
46         assertEquals(1, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
47         assertEquals(10, timer.getTotalTimeLocked(200, BatteryStats.STATS_SINCE_CHARGED));
48
49         timer.onTimeStopped(200, 200, 200);
50
51         assertEquals(1, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
52         assertEquals(10, timer.getTotalTimeLocked(200, BatteryStats.STATS_SINCE_CHARGED));
53     }
54
55     @SmallTest
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,
60                 timeBase);
61
62         // First once is absorbed.
63         timer.update(10, 1);
64
65         timer.add(10, 1);
66
67         assertEquals(0, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
68         assertEquals(0, timer.getTotalTimeLocked(200, BatteryStats.STATS_SINCE_CHARGED));
69
70         // This is less than we currently have, so we will end the sample. Time isn't running, so
71         // nothing should happen.
72         timer.update(0, 0);
73
74         assertEquals(0, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
75         assertEquals(0, timer.getTotalTimeLocked(200, BatteryStats.STATS_SINCE_CHARGED));
76
77         timer.onTimeStarted(100, 100, 100);
78
79         // This should add.
80         timer.add(100, 10);
81
82         assertEquals(100, timer.getTotalTimeLocked(200, BatteryStats.STATS_SINCE_CHARGED));
83         assertEquals(10, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
84
85         // This is less than we currently have, so we should end our sample and continue with the
86         // entire amount updated here.
87         timer.update(50, 5);
88
89         assertEquals(150, timer.getTotalTimeLocked(200, BatteryStats.STATS_SINCE_CHARGED));
90         assertEquals(15, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
91
92         timer.onTimeStopped(200, 200, 200);
93
94         assertEquals(150, timer.getTotalTimeLocked(200, BatteryStats.STATS_SINCE_CHARGED));
95         assertEquals(15, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
96     }
97
98     @SmallTest
99     public void testFirstUpdateIsAbsorbed() throws Exception {
100         final MockClocks clocks = new MockClocks();
101         final BatteryStatsImpl.TimeBase timeBase = Mockito.mock(BatteryStatsImpl.TimeBase.class);
102
103         BatteryStatsImpl.SamplingTimer timer = new BatteryStatsImpl.SamplingTimer(clocks, timeBase);
104
105         // This should be absorbed because it is our first update and we don't know what
106         // was being counted before.
107         timer.update(10, 1);
108
109         assertEquals(0, timer.getTotalTimeLocked(10, BatteryStats.STATS_SINCE_CHARGED));
110         assertEquals(0, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
111
112         timer = new BatteryStatsImpl.SamplingTimer(clocks, timeBase);
113         timer.onTimeStarted(100, 100, 100);
114
115         // This should be absorbed.
116         timer.update(10, 1);
117
118         assertEquals(0, timer.getTotalTimeLocked(100, BatteryStats.STATS_SINCE_CHARGED));
119         assertEquals(0, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
120
121         // This should NOT be aborbed, since we've already done that.
122         timer.add(10, 1);
123
124         assertEquals(10, timer.getTotalTimeLocked(100, BatteryStats.STATS_SINCE_CHARGED));
125         assertEquals(1, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
126
127         timer.onTimeStopped(200, 200, 200);
128         timer.onTimeStarted(300, 300, 300);
129
130         // This should NOT be absorbed.
131         timer.add(10, 1);
132
133         assertEquals(20, timer.getTotalTimeLocked(300, BatteryStats.STATS_SINCE_CHARGED));
134         assertEquals(2, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
135     }
136
137     @SmallTest
138     public void testSampleTimerSummaryParceling() throws Exception {
139         final MockClocks clocks = new MockClocks();
140         clocks.realtime = 0;
141         clocks.uptime = 0;
142
143         final BatteryStatsImpl.TimeBase timeBase = new BatteryStatsImpl.TimeBase();
144         timeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime());
145
146         BatteryStatsImpl.SamplingTimer timer = new BatteryStatsImpl.SamplingTimer(clocks, timeBase);
147
148         // Start running on battery.
149         timeBase.setRunning(true, clocks.uptimeMillis(), clocks.elapsedRealtime());
150
151         // The first update on battery consumes the values as a way of starting cleanly.
152         timer.add(10, 1);
153
154         timer.add(10, 1);
155
156         clocks.realtime = 20;
157         clocks.uptime = 20;
158
159         assertEquals(10, timer.getTotalTimeLocked(clocks.elapsedRealtime(),
160                 BatteryStats.STATS_SINCE_CHARGED));
161         assertEquals(1, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
162
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);
167
168         // Stop running on battery.
169         timeBase.setRunning(false, clocks.uptimeMillis(), clocks.elapsedRealtime());
170
171         assertEquals(10, timer.getTotalTimeLocked(clocks.elapsedRealtime(),
172                 BatteryStats.STATS_SINCE_CHARGED));
173         assertEquals(1, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
174
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);
180
181         // Read the on battery summary from the parcel.
182         BatteryStatsImpl.SamplingTimer unparceledTimer = new BatteryStatsImpl.SamplingTimer(
183                 clocks, timeBase);
184         unparceledTimer.readSummaryFromParcelLocked(onBatterySummaryParcel);
185
186         assertEquals(10, unparceledTimer.getTotalTimeLocked(0, BatteryStats.STATS_SINCE_CHARGED));
187         assertEquals(1, unparceledTimer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
188
189         // Read the off battery summary from the parcel.
190         unparceledTimer = new BatteryStatsImpl.SamplingTimer(clocks, timeBase);
191         unparceledTimer.readSummaryFromParcelLocked(offBatterySummaryParcel);
192
193         assertEquals(10, unparceledTimer.getTotalTimeLocked(0, BatteryStats.STATS_SINCE_CHARGED));
194         assertEquals(1, unparceledTimer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
195     }
196 }