blob: ed54f53037504a4c793bdba71dc930c3b7712dd3 [file] [log] [blame]
Bookatz956f36bf2017-04-28 09:48:17 -07001/*
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;
Bookatzc8c44962017-05-11 12:12:54 -070019import static android.os.BatteryStats.WAKE_TYPE_PARTIAL;
Bookatz956f36bf2017-04-28 09:48:17 -070020
Bookatzc8c44962017-05-11 12:12:54 -070021import android.app.ActivityManager;
22import android.os.BatteryStats;
Bookatz956f36bf2017-04-28 09:48:17 -070023import 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
Bookatzc8c44962017-05-11 12:12:54 -070035 /** Test BatteryStatsImpl.Uid.noteBluetoothScanResultLocked. */
Bookatz956f36bf2017-04-28 09:48:17 -070036 @SmallTest
37 public void testNoteBluetoothScanResultLocked() throws Exception {
38 MockBatteryStatsImpl bi = new MockBatteryStatsImpl(new MockClocks());
39 bi.updateTimeBasesLocked(true, true, 0, 0);
Bookatzb1f04f32017-05-19 13:57:32 -070040 bi.noteUidProcessStateLocked(UID, ActivityManager.PROCESS_STATE_TOP);
Bookatz956f36bf2017-04-28 09:48:17 -070041
Bookatz4ebc0642017-05-11 12:21:19 -070042 bi.noteBluetoothScanResultsFromSourceLocked(WS, 1);
43 bi.noteBluetoothScanResultsFromSourceLocked(WS, 100);
44 assertEquals(101,
Bookatz956f36bf2017-04-28 09:48:17 -070045 bi.getUidStats().get(UID).getBluetoothScanResultCounter()
46 .getCountLocked(STATS_SINCE_CHARGED));
Bookatzb1f04f32017-05-19 13:57:32 -070047 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));
Bookatz956f36bf2017-04-28 09:48:17 -070060 }
Bookatzc8c44962017-05-11 12:12:54 -070061
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 }
Bookatz956f36bf2017-04-28 09:48:17 -070087}