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 package com.android.loganalysis.rule;
17 
18 import com.android.loganalysis.item.BatteryStatsDetailedInfoItem;
19 import com.android.loganalysis.item.BugreportItem;
20 import com.android.loganalysis.item.DumpsysBatteryStatsItem;
21 import com.android.loganalysis.item.DumpsysItem;
22 import com.android.loganalysis.item.WakelockItem;
23 import com.android.loganalysis.item.WakelockItem.WakeLockCategory;
24 
25 import junit.framework.TestCase;
26 
27 import org.json.JSONObject;
28 
29 /**
30  * Unit tests for {@link WakelockRule}
31  */
32 public class WakelockRuleTest extends TestCase {
33 
34     BugreportItem mBugreport;
35     DumpsysItem mDumpsys;
36     DumpsysBatteryStatsItem mDumpsysBatteryStats;
37     BatteryStatsDetailedInfoItem mBatteryStatsDetailedInfo;
38 
39     @Override
setUp()40     public void setUp() {
41         mBugreport =  new BugreportItem();
42         mDumpsys = new DumpsysItem();
43         mDumpsysBatteryStats = new DumpsysBatteryStatsItem();
44         mBatteryStatsDetailedInfo = new BatteryStatsDetailedInfoItem();
45 
46         mBatteryStatsDetailedInfo.setTimeOnBattery(3902004);
47         mDumpsysBatteryStats.setDetailedBatteryStatsItem(mBatteryStatsDetailedInfo);
48         mDumpsys.setBatteryInfo(mDumpsysBatteryStats);
49         mBugreport.setDumpsys(mDumpsys);
50     }
51 
52     /**
53      * Test wakelock analysis
54      */
testWakelockAnalysis()55     public void testWakelockAnalysis() throws Exception {
56         WakelockItem wakelock = new WakelockItem();
57         wakelock.addWakeLock("PowerManagerService.WakeLocks", 310006, 2,
58                 WakeLockCategory.KERNEL_WAKELOCK);
59         wakelock.addWakeLock("msm_serial_hs_rx", 133612, 258,
60                 WakeLockCategory.KERNEL_WAKELOCK);
61 
62         wakelock.addWakeLock("ProxyController", "1001", 3887565, 4,
63                 WakeLockCategory.PARTIAL_WAKELOCK);
64         wakelock.addWakeLock("AudioMix", "1013", 1979, 3,
65                 WakeLockCategory.PARTIAL_WAKELOCK);
66 
67         mBatteryStatsDetailedInfo.setWakelockItem(wakelock);
68         WakelockRule wakelockRule = new WakelockRule(mBugreport);
69         wakelockRule.applyRule();
70         JSONObject analysis = wakelockRule.getAnalysis();
71         assertNotNull(analysis);
72         assertTrue(analysis.has("WAKELOCK_ANALYSIS"));
73         assertEquals(analysis.getString("WAKELOCK_ANALYSIS"),
74                 "ProxyController PARTIAL_WAKELOCK is held for 0d 1h 4m 47s.");
75     }
76 
testNoSignificantWakelockAnalysis()77     public void testNoSignificantWakelockAnalysis() throws Exception {
78         WakelockItem wakelock = new WakelockItem();
79         wakelock.addWakeLock("PowerManagerService.WakeLocks", 310006, 2,
80                 WakeLockCategory.KERNEL_WAKELOCK);
81         wakelock.addWakeLock("msm_serial_hs_rx", 133612, 258,
82                 WakeLockCategory.KERNEL_WAKELOCK);
83 
84         wakelock.addWakeLock("ProxyController", "1001", 287565, 4,
85                 WakeLockCategory.PARTIAL_WAKELOCK);
86         wakelock.addWakeLock("AudioMix", "1013", 1979, 3,
87                 WakeLockCategory.PARTIAL_WAKELOCK);
88 
89         mBatteryStatsDetailedInfo.setWakelockItem(wakelock);
90         WakelockRule wakelockRule = new WakelockRule(mBugreport);
91         wakelockRule.applyRule();
92         JSONObject analysis = wakelockRule.getAnalysis();
93         assertNotNull(analysis);
94         assertTrue(analysis.has("WAKELOCK_ANALYSIS"));
95         assertEquals(analysis.getString("WAKELOCK_ANALYSIS"),
96                 "No wakelocks were held for more than 10% of time on battery.");
97     }
98 
testNoWakelockAnalysis()99     public void testNoWakelockAnalysis() throws Exception {
100         WakelockRule wakelockRule = new WakelockRule(mBugreport);
101         wakelockRule.applyRule();
102         JSONObject analysis = wakelockRule.getAnalysis();
103         assertNotNull(analysis);
104         assertTrue(analysis.has("WAKELOCK_ANALYSIS"));
105         assertEquals(analysis.getString("WAKELOCK_ANALYSIS"),
106                 "No wakelocks were held for more than 10% of time on battery.");
107     }
108 }
109