1 /*
2  * Copyright (C) 2018 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.powermodel;
18 
19 import java.io.ByteArrayInputStream;
20 import java.io.InputStream;
21 import java.nio.charset.StandardCharsets;
22 import java.util.List;
23 import org.junit.Test;
24 import org.junit.Assert;
25 
26 import com.android.powermodel.component.ModemAppActivity;
27 import com.android.powermodel.component.ModemGlobalActivity;
28 import com.android.powermodel.component.ModemRemainderActivity;
29 
30 /**
31  * Tests {@link BatteryStatsReader}.
32  */
33 public class BatteryStatsReaderTest {
loadCsvStream()34     private static InputStream loadCsvStream() {
35         return BatteryStatsReaderTest.class.getResourceAsStream("/bs.csv");
36     }
37 
testModemGlobal()38     @Test public void testModemGlobal() throws Exception {
39         final ActivityReport report = BatteryStatsReader.parse(loadCsvStream());
40 
41         final AppActivity global = report.findApp(SpecialApp.GLOBAL);
42         Assert.assertNotNull(global);
43 
44         final ModemGlobalActivity modem
45                 = (ModemGlobalActivity)global.getComponentActivity(Component.MODEM);
46         Assert.assertNotNull(modem);
47         Assert.assertEquals(97840, modem.rxPacketCount);
48         Assert.assertEquals(72941, modem.txPacketCount);
49         Assert.assertEquals(5113727, modem.totalActiveTimeMs);
50     }
51 
testModemApp()52     @Test public void testModemApp() throws Exception {
53         final ActivityReport report = BatteryStatsReader.parse(loadCsvStream());
54 
55         final List<AppActivity> gmailList = report.findApp("com.google.android.gm");
56         Assert.assertEquals(1, gmailList.size());
57         final AppActivity gmail = gmailList.get(0);
58 
59         final ModemAppActivity modem
60                 = (ModemAppActivity)gmail.getComponentActivity(Component.MODEM);
61         Assert.assertNotNull(modem);
62         Assert.assertEquals(9925, modem.rxPacketCount);
63         Assert.assertEquals(5577, modem.txPacketCount);
64     }
65 
testModemRemainder()66     @Test public void testModemRemainder() throws Exception {
67         final ActivityReport report = BatteryStatsReader.parse(loadCsvStream());
68 
69         final AppActivity remainder = report.findApp(SpecialApp.REMAINDER);
70         Assert.assertNotNull(remainder);
71 
72         final ModemRemainderActivity modem
73                 = (ModemRemainderActivity)remainder.getComponentActivity(Component.MODEM);
74         Assert.assertNotNull(modem);
75         Assert.assertArrayEquals(new long[] { 3066958, 0, 34678, 1643364, 7045084 },
76                 modem.strengthTimeMs);
77         Assert.assertEquals(2443805, modem.scanningTimeMs);
78         Assert.assertEquals(4923676, modem.activeTimeMs);
79     }
80 }
81