1 /* 2 * Copyright (C) 2016 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.internal.os; 18 19 import java.io.PrintWriter; 20 import java.io.StringWriter; 21 22 import android.os.BatteryStats; 23 import android.os.Parcel; 24 import android.support.test.filters.SmallTest; 25 import android.util.Log; 26 27 import junit.framework.Assert; 28 import junit.framework.TestCase; 29 30 import org.mockito.Mockito; 31 32 /** 33 * Provides test cases for android.os.BatteryStats. 34 */ 35 public class BatteryStatsTimeBaseTest extends TestCase { 36 private static final String TAG = "BatteryStatsTimeBaseTest"; 37 38 static class TestTimeBase extends BatteryStatsImpl.TimeBase { 39 40 public void populate(long uptime, long realtime, boolean running, long pastUptime, 41 long uptimeStart, long pastRealtime, long realtimeStart, 42 long unpluggedUptime, long unpluggedRealtime) { 43 mUptime = uptime; 44 mRealtime = realtime; 45 mRunning = running; 46 mPastUptime = pastUptime; 47 mUptimeStart = uptimeStart; 48 mPastRealtime = pastRealtime; 49 mRealtimeStart = realtimeStart; 50 mUnpluggedUptime = unpluggedUptime; 51 mUnpluggedRealtime = unpluggedRealtime; 52 } 53 54 public void verify(long uptime, long realtime, boolean running, long pastUptime, 55 long uptimeStart, long pastRealtime, long realtimeStart, 56 long unpluggedUptime, long unpluggedRealtime) { 57 Assert.assertEquals(uptime, mUptime); 58 Assert.assertEquals(realtime, mRealtime); 59 Assert.assertEquals(running, mRunning); 60 Assert.assertEquals(pastUptime, mPastUptime); 61 Assert.assertEquals(uptimeStart, mUptimeStart); 62 Assert.assertEquals(pastRealtime, mPastRealtime); 63 Assert.assertEquals(realtimeStart, mRealtimeStart); 64 Assert.assertEquals(unpluggedUptime, mUnpluggedUptime); 65 Assert.assertEquals(unpluggedRealtime, mUnpluggedRealtime); 66 } 67 } 68 69 /** 70 * Test the observers and the setRunning call. 71 */ 72 @SmallTest 73 public void testRunning() throws Exception { 74 TestTimeBase tb = new TestTimeBase(); 75 76 // Toggle running once, to accumulate past uptime and past realtime 77 // so the test values aren't 0. 78 tb.setRunning(true, 100, 10000); 79 tb.setRunning(false, 200, 11000); 80 Assert.assertEquals(100, tb.getUptimeStart()); 81 Assert.assertEquals(10000, tb.getRealtimeStart()); 82 83 // Create some observers 84 BatteryStatsImpl.TimeBaseObs observer1 = Mockito.mock(BatteryStatsImpl.TimeBaseObs.class); 85 BatteryStatsImpl.TimeBaseObs observer2 = Mockito.mock(BatteryStatsImpl.TimeBaseObs.class); 86 BatteryStatsImpl.TimeBaseObs observer3 = Mockito.mock(BatteryStatsImpl.TimeBaseObs.class); 87 88 // Add them 89 tb.add(observer1); 90 tb.add(observer2); 91 tb.add(observer3); 92 Assert.assertTrue(tb.hasObserver(observer1)); 93 Assert.assertTrue(tb.hasObserver(observer2)); 94 Assert.assertTrue(tb.hasObserver(observer3)); 95 96 // Remove one 97 tb.remove(observer3); 98 Assert.assertTrue(tb.hasObserver(observer1)); 99 Assert.assertTrue(tb.hasObserver(observer2)); 100 Assert.assertFalse(tb.hasObserver(observer3)); 101 102 // Start running, make sure we get a started call on the two active observers 103 // and not the third. 104 tb.setRunning(true, 250, 14000); 105 106 Assert.assertTrue(tb.isRunning()); 107 108 if (false) { 109 Log.d(TAG, "mUptimeStart=" + tb.getUptimeStart() 110 + " mRealtimeStart=" + tb.getRealtimeStart() 111 + " mUptime=" + tb.getUptime(250) 112 + " mRealtime=" + tb.getRealtime(14000) 113 + " isRunning=" + tb.isRunning()); 114 } 115 116 Assert.assertEquals(250, tb.getUptimeStart()); 117 Assert.assertEquals(14000, tb.getRealtimeStart()); 118 Assert.assertEquals(100, tb.getUptime(250)); 119 Assert.assertEquals(1000, tb.getRealtime(14000)); 120 121 Mockito.verify(observer1).onTimeStarted(14000, 100, 1000); 122 Mockito.verify(observer1, Mockito.never()).onTimeStopped(-1, -1, -1); 123 Mockito.verifyNoMoreInteractions(observer1); 124 Mockito.verify(observer2).onTimeStarted(14000, 100, 1000); 125 Mockito.verify(observer2, Mockito.never()).onTimeStopped(-1, -1, -1); 126 Mockito.verifyNoMoreInteractions(observer2); 127 128 Mockito.reset(observer1); 129 Mockito.reset(observer2); 130 Mockito.reset(observer3); 131 132 // Advance the "timer" and make sure the getters account for the current time passed in 133 Assert.assertEquals(400, tb.getUptime(550)); 134 Assert.assertEquals(1555, tb.getRealtime(14555)); 135 136 // Stop running, make sure we get a stopped call on the two active observers 137 // and not the third. 138 tb.setRunning(false, 402, 14002); 139 140 Assert.assertFalse(tb.isRunning()); 141 142 if (false) { 143 Log.d(TAG, "mUptimeStart=" + tb.getUptimeStart() 144 + " mRealtimeStart=" + tb.getRealtimeStart() 145 + " mUptime=" + tb.getUptime(250) 146 + " mRealtime=" + tb.getRealtime(14000) 147 + " isRunning=" + tb.isRunning()); 148 } 149 150 Assert.assertEquals(252, tb.getUptime(402)); 151 Assert.assertEquals(1002, tb.getRealtime(14002)); 152 153 Mockito.verify(observer1).onTimeStopped(14002, 252, 1002); 154 Mockito.verify(observer1, Mockito.never()).onTimeStopped(-1, -1, -1); 155 Mockito.verifyNoMoreInteractions(observer1); 156 Mockito.verify(observer2).onTimeStopped(14002, 252, 1002); 157 Mockito.verify(observer2, Mockito.never()).onTimeStopped(-1, -1, -1); 158 Mockito.verifyNoMoreInteractions(observer2); 159 160 // Advance the "timer" and make sure the getters account for the current time passed in 161 // is the same as the time when running went to false. 162 Assert.assertEquals(252, tb.getUptime(600)); 163 Assert.assertEquals(1002, tb.getRealtime(17000)); 164 } 165 166 /** 167 * Test that reset while running updates the plugged and unplugged times 168 */ 169 @SmallTest 170 public void testResetWhileRunning() throws Exception { 171 TestTimeBase tb = new TestTimeBase(); 172 tb.populate(100, 200, true, 300, 400, 500, 600, 700, 800); 173 174 tb.reset(666, 6666); 175 176 // Not sure if this is a bug: reset while running does not 177 // reset mPastUptime, but while it is running it does. 178 tb.verify(100, 200, true, 300, 666, 500, 6666, 300, 500); 179 } 180 181 /** 182 * Test that reset while running updates the plugged and unplugged times 183 */ 184 @SmallTest 185 public void testResetWhileNotRunning() throws Exception { 186 TestTimeBase tb = new TestTimeBase(); 187 tb.populate(100, 200, false, 300, 400, 500, 600, 700, 800); 188 189 tb.reset(666, 6666); 190 191 tb.verify(100, 200, false, 0, 400, 0, 600, 700, 800); 192 } 193 194 /** 195 * Test init 196 */ 197 @SmallTest 198 public void testInit() throws Exception { 199 TestTimeBase tb = new TestTimeBase(); 200 tb.populate(100, 200, false, 300, 400, 500, 600, 700, 800); 201 202 tb.init(666, 6666); 203 204 tb.verify(0, 0, false, 0, 666, 0, 6666, 0, 0); 205 } 206 207 /** 208 * Test writeToParcel and readFromParcel 209 */ 210 @SmallTest 211 public void testParcellingWhileRunning() throws Exception { 212 TestTimeBase tb1 = new TestTimeBase(); 213 214 tb1.populate(100, 200, true, 300, 400, 500, 600, 700, 800); 215 216 Parcel parcel = Parcel.obtain(); 217 tb1.writeToParcel(parcel, 666, 6666); 218 219 parcel.setDataPosition(0); 220 221 TestTimeBase tb2 = new TestTimeBase(); 222 tb2.readFromParcel(parcel); 223 224 // Running is not preserved across parceling 225 tb2.verify(100, 200, false, 300+666-400, 400, 500+6666-600, 600, 700, 800); 226 } 227 228 /** 229 * Test writeToParcel and readFromParcel 230 */ 231 @SmallTest 232 public void testParcellingWhileNotRunning() throws Exception { 233 TestTimeBase tb1 = new TestTimeBase(); 234 235 tb1.populate(100, 200, false, 300, 400, 500, 600, 700, 800); 236 237 Parcel parcel = Parcel.obtain(); 238 tb1.writeToParcel(parcel, 666, 6666); 239 240 parcel.setDataPosition(0); 241 242 TestTimeBase tb2 = new TestTimeBase(); 243 tb2.readFromParcel(parcel); 244 245 tb2.verify(100, 200, false, 300, 400, 500, 600, 700, 800); 246 } 247 248 /** 249 * Test writeSummaryToParcel and readSummaryFromParcel 250 */ 251 @SmallTest 252 public void testSummary() throws Exception { 253 TestTimeBase tb1 = new TestTimeBase(); 254 255 tb1.populate(100, 200, true, 300, 400, 500, 600, 700, 800); 256 257 Parcel parcel = Parcel.obtain(); 258 tb1.writeSummaryToParcel(parcel, 666, 6666); 259 260 parcel.setDataPosition(0); 261 262 TestTimeBase tb2 = new TestTimeBase(); 263 264 // readSummaryFromParcel doesn't affect the other fields. 265 // Not sure if this is deliberate 266 tb2.populate(1, 2, true, 3, 4, 5, 6, 7, 8); 267 268 tb2.readSummaryFromParcel(parcel); 269 270 tb2.verify(666, 6766, true, 3, 4, 5, 6, 7, 8); 271 } 272 273 /** 274 * Test computeUptime 275 */ 276 @SmallTest 277 public void testComputeUptime() throws Exception { 278 TestTimeBase tb = new TestTimeBase(); 279 280 tb.populate(100, 200, true, 300, 400, 500, 600, 50, 60); 281 282 Assert.assertEquals(100+300+666-400, 283 tb.computeUptime(666, BatteryStats.STATS_SINCE_CHARGED)); 284 Assert.assertEquals(300+666-400, 285 tb.computeUptime(666, BatteryStats.STATS_CURRENT)); 286 Assert.assertEquals(300+666-400-50, 287 tb.computeUptime(666, BatteryStats.STATS_SINCE_UNPLUGGED)); 288 289 Assert.assertEquals(0, tb.computeUptime(666, 6000)); 290 } 291 292 /** 293 * Test computeUptime 294 */ 295 @SmallTest 296 public void testComputeRealtime() throws Exception { 297 TestTimeBase tb = new TestTimeBase(); 298 299 tb.populate(100, 200, true, 300, 400, 500, 600, 50, 60); 300 301 Assert.assertEquals(200+500+6666-600, 302 tb.computeRealtime(6666, BatteryStats.STATS_SINCE_CHARGED)); 303 Assert.assertEquals(500+6666-600, 304 tb.computeRealtime(6666, BatteryStats.STATS_CURRENT)); 305 Assert.assertEquals(500+6666-600-60, 306 tb.computeRealtime(6666, BatteryStats.STATS_SINCE_UNPLUGGED)); 307 308 Assert.assertEquals(0, tb.computeUptime(666, 6000)); 309 } 310 311 /** 312 * Test dump 313 */ 314 @SmallTest 315 public void testDump() throws Exception { 316 TestTimeBase tb = new TestTimeBase(); 317 318 tb.populate(100, 200, true, 300, 400, 500, 600, 50, 60); 319 320 StringWriter sw = new StringWriter(); 321 PrintWriter pw = new PrintWriter(sw); 322 323 tb.dump(pw, "+++++ "); 324 325 pw.close(); 326 327 // note the spaces at the ends of the lines which come from formatTimeMs. 328 final String CORRECT = "+++++ mRunning=true\n" 329 + "+++++ mUptime=0ms \n" 330 + "+++++ mRealtime=0ms \n" 331 + "+++++ mPastUptime=0ms mUptimeStart=0ms mUnpluggedUptime=0ms \n" 332 + "+++++ mPastRealtime=0ms mRealtimeStart=0ms mUnpluggedRealtime=0ms \n"; 333 334 Assert.assertEquals(CORRECT, sw.toString()); 335 } 336 337 } 338 339