Bookatz | 956f36bf | 2017-04-28 09:48:17 -0700 | [diff] [blame] | 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 static android.os.BatteryStats.STATS_SINCE_CHARGED; |
Bookatz | c8c4496 | 2017-05-11 12:12:54 -0700 | [diff] [blame] | 19 | import static android.os.BatteryStats.WAKE_TYPE_PARTIAL; |
Bookatz | 956f36bf | 2017-04-28 09:48:17 -0700 | [diff] [blame] | 20 | |
Bookatz | c8c4496 | 2017-05-11 12:12:54 -0700 | [diff] [blame] | 21 | import android.app.ActivityManager; |
| 22 | import android.os.BatteryStats; |
Bookatz | 956f36bf | 2017-04-28 09:48:17 -0700 | [diff] [blame] | 23 | import android.os.WorkSource; |
| 24 | import android.support.test.filters.SmallTest; |
| 25 | |
| 26 | import junit.framework.TestCase; |
| 27 | |
| 28 | /** |
| 29 | * Test various BatteryStatsImpl noteStart methods. |
| 30 | */ |
| 31 | public class BatteryStatsNoteTest extends TestCase{ |
| 32 | private static final int UID = 10500; |
| 33 | private static final WorkSource WS = new WorkSource(UID); |
| 34 | |
Bookatz | c8c4496 | 2017-05-11 12:12:54 -0700 | [diff] [blame] | 35 | /** Test BatteryStatsImpl.Uid.noteBluetoothScanResultLocked. */ |
Bookatz | 956f36bf | 2017-04-28 09:48:17 -0700 | [diff] [blame] | 36 | @SmallTest |
| 37 | public void testNoteBluetoothScanResultLocked() throws Exception { |
| 38 | MockBatteryStatsImpl bi = new MockBatteryStatsImpl(new MockClocks()); |
| 39 | bi.updateTimeBasesLocked(true, true, 0, 0); |
Bookatz | b1f04f3 | 2017-05-19 13:57:32 -0700 | [diff] [blame] | 40 | bi.noteUidProcessStateLocked(UID, ActivityManager.PROCESS_STATE_TOP); |
Bookatz | 956f36bf | 2017-04-28 09:48:17 -0700 | [diff] [blame] | 41 | |
Bookatz | 4ebc064 | 2017-05-11 12:21:19 -0700 | [diff] [blame] | 42 | bi.noteBluetoothScanResultsFromSourceLocked(WS, 1); |
| 43 | bi.noteBluetoothScanResultsFromSourceLocked(WS, 100); |
| 44 | assertEquals(101, |
Bookatz | 956f36bf | 2017-04-28 09:48:17 -0700 | [diff] [blame] | 45 | bi.getUidStats().get(UID).getBluetoothScanResultCounter() |
| 46 | .getCountLocked(STATS_SINCE_CHARGED)); |
Bookatz | b1f04f3 | 2017-05-19 13:57:32 -0700 | [diff] [blame] | 47 | BatteryStats.Counter bgCntr = bi.getUidStats().get(UID).getBluetoothScanResultBgCounter(); |
| 48 | if (bgCntr != null) { |
| 49 | assertEquals(0, bgCntr.getCountLocked(STATS_SINCE_CHARGED)); |
| 50 | } |
| 51 | |
| 52 | bi.noteUidProcessStateLocked(UID, ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND); |
| 53 | bi.noteBluetoothScanResultsFromSourceLocked(WS, 17); |
| 54 | assertEquals(101 + 17, |
| 55 | bi.getUidStats().get(UID).getBluetoothScanResultCounter() |
| 56 | .getCountLocked(STATS_SINCE_CHARGED)); |
| 57 | assertEquals(17, |
| 58 | bi.getUidStats().get(UID).getBluetoothScanResultBgCounter() |
| 59 | .getCountLocked(STATS_SINCE_CHARGED)); |
Bookatz | 956f36bf | 2017-04-28 09:48:17 -0700 | [diff] [blame] | 60 | } |
Bookatz | c8c4496 | 2017-05-11 12:12:54 -0700 | [diff] [blame] | 61 | |
| 62 | /** Test BatteryStatsImpl.Uid.noteStartWakeLocked. */ |
| 63 | @SmallTest |
| 64 | public void testNoteStartWakeLocked() throws Exception { |
| 65 | final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms |
| 66 | MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks); |
| 67 | |
| 68 | int pid = 10; |
| 69 | String name = "name"; |
| 70 | |
| 71 | bi.updateTimeBasesLocked(true, true, 0, 0); |
| 72 | bi.noteUidProcessStateLocked(UID, ActivityManager.PROCESS_STATE_TOP); |
| 73 | bi.getUidStatsLocked(UID).noteStartWakeLocked(pid, name, WAKE_TYPE_PARTIAL, clocks.realtime); |
| 74 | |
| 75 | clocks.realtime = clocks.uptime = 100; |
| 76 | bi.noteUidProcessStateLocked(UID, ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND); |
| 77 | |
| 78 | clocks.realtime = clocks.uptime = 220; |
| 79 | bi.getUidStatsLocked(UID).noteStopWakeLocked(pid, name, WAKE_TYPE_PARTIAL, clocks.realtime); |
| 80 | |
| 81 | BatteryStats.Timer aggregTimer = bi.getUidStats().get(UID).getAggregatedPartialWakelockTimer(); |
| 82 | long actualTime = aggregTimer.getTotalTimeLocked(300_000, STATS_SINCE_CHARGED); |
| 83 | long bgTime = aggregTimer.getSubTimer().getTotalTimeLocked(300_000, STATS_SINCE_CHARGED); |
| 84 | assertEquals(220_000, actualTime); |
| 85 | assertEquals(120_000, bgTime); |
| 86 | } |
Bookatz | 956f36bf | 2017-04-28 09:48:17 -0700 | [diff] [blame] | 87 | } |