BatteryStatsNoteTest.java revision c8c44960c0051dacac0a6cb6c65ddfafc9d1cb84
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 */ 16package com.android.internal.os; 17 18import static android.os.BatteryStats.STATS_SINCE_CHARGED; 19import static android.os.BatteryStats.WAKE_TYPE_PARTIAL; 20 21import android.app.ActivityManager; 22import android.os.BatteryStats; 23import android.os.WorkSource; 24import android.support.test.filters.SmallTest; 25 26import junit.framework.TestCase; 27 28/** 29 * Test various BatteryStatsImpl noteStart methods. 30 */ 31public class BatteryStatsNoteTest extends TestCase{ 32 private static final int UID = 10500; 33 private static final WorkSource WS = new WorkSource(UID); 34 35 /** Test BatteryStatsImpl.Uid.noteBluetoothScanResultLocked. */ 36 @SmallTest 37 public void testNoteBluetoothScanResultLocked() throws Exception { 38 MockBatteryStatsImpl bi = new MockBatteryStatsImpl(new MockClocks()); 39 bi.updateTimeBasesLocked(true, true, 0, 0); 40 41 bi.noteBluetoothScanResultsFromSourceLocked(WS, 1); 42 bi.noteBluetoothScanResultsFromSourceLocked(WS, 100); 43 assertEquals(101, 44 bi.getUidStats().get(UID).getBluetoothScanResultCounter() 45 .getCountLocked(STATS_SINCE_CHARGED)); 46 } 47 48 /** Test BatteryStatsImpl.Uid.noteStartWakeLocked. */ 49 @SmallTest 50 public void testNoteStartWakeLocked() throws Exception { 51 final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms 52 MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks); 53 54 int pid = 10; 55 String name = "name"; 56 57 bi.updateTimeBasesLocked(true, true, 0, 0); 58 bi.noteUidProcessStateLocked(UID, ActivityManager.PROCESS_STATE_TOP); 59 bi.getUidStatsLocked(UID).noteStartWakeLocked(pid, name, WAKE_TYPE_PARTIAL, clocks.realtime); 60 61 clocks.realtime = clocks.uptime = 100; 62 bi.noteUidProcessStateLocked(UID, ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND); 63 64 clocks.realtime = clocks.uptime = 220; 65 bi.getUidStatsLocked(UID).noteStopWakeLocked(pid, name, WAKE_TYPE_PARTIAL, clocks.realtime); 66 67 BatteryStats.Timer aggregTimer = bi.getUidStats().get(UID).getAggregatedPartialWakelockTimer(); 68 long actualTime = aggregTimer.getTotalTimeLocked(300_000, STATS_SINCE_CHARGED); 69 long bgTime = aggregTimer.getSubTimer().getTotalTimeLocked(300_000, STATS_SINCE_CHARGED); 70 assertEquals(220_000, actualTime); 71 assertEquals(120_000, bgTime); 72 } 73} 74