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.ModemAppPower;
27 import com.android.powermodel.component.ModemRemainderPower;
28 
29 /**
30  * Tests {@link PowerReport}.
31  */
32 public class PowerReportTest {
33     private static final double EPSILON = 0.001;
34     private static final double MS_PER_HR = 3600000.0;
35 
36     private static final double AVERAGE_MODEM_POWER = ((11+16+19+22+73+132) / 6.0);
37     private static final double GMAIL_MODEM_MAH = ((9925+5577) / (double)(97840+72941))
38             * 5113727 * AVERAGE_MODEM_POWER * (1.0 / 3600 / 1000);
39     private static final double GMAIL_MAH
40             = GMAIL_MODEM_MAH;
41 
42     private static final double REMAINDER_MODEM_MAH
43             =  (1.0 / 3600 / 1000)
44             * ((3066958 * 16) + (0 * 19) + (34678 * 22) + (1643364 * 73) + (7045084 * 132)
45                 + (2443805 * 12)
46                 + (4923676 * AVERAGE_MODEM_POWER));
47     private static final double REMAINDER_MAH
48             = REMAINDER_MODEM_MAH;
49 
50     private static final double TOTAL_MAH
51             = GMAIL_MAH
52             + REMAINDER_MAH;
53 
loadPowerProfileStream()54     private static InputStream loadPowerProfileStream() {
55         return PowerProfileTest.class.getResourceAsStream("/power_profile.xml");
56     }
57 
loadCsvStream()58     private static InputStream loadCsvStream() {
59         return BatteryStatsReaderTest.class.getResourceAsStream("/bs.csv");
60     }
61 
loadPowerReport()62     private static PowerReport loadPowerReport() throws Exception {
63         final PowerProfile profile = PowerProfile.parse(loadPowerProfileStream());
64         final ActivityReport activity = BatteryStatsReader.parse(loadCsvStream());
65         return PowerReport.createReport(profile, activity);
66     }
67 
testModemApp()68     @Test public void testModemApp() throws Exception {
69         final PowerReport report = loadPowerReport();
70 
71         final List<AppPower> gmailList = report.findApp("com.google.android.gm");
72         Assert.assertEquals(1, gmailList.size());
73         final AppPower gmail = gmailList.get(0);
74 
75         final ModemAppPower modem = (ModemAppPower)gmail.getComponentPower(Component.MODEM);
76         Assert.assertNotNull(modem);
77         Assert.assertEquals(GMAIL_MODEM_MAH, modem.powerMah, EPSILON);
78     }
79 
testModemRemainder()80     @Test public void testModemRemainder() throws Exception {
81         final PowerReport report = loadPowerReport();
82 
83         final AppPower remainder = report.findApp(SpecialApp.REMAINDER);
84         Assert.assertNotNull(remainder);
85 
86         final ModemRemainderPower modem
87                 = (ModemRemainderPower)remainder.getComponentPower(Component.MODEM);
88         Assert.assertNotNull(modem);
89 
90         Assert.assertArrayEquals(new double[] {
91                     3066958 * 16.0 / MS_PER_HR,
92                     0 * 19.0 / MS_PER_HR,
93                     34678 * 22.0 / MS_PER_HR,
94                     1643364 * 73.0 / MS_PER_HR,
95                     7045084 * 132.0 / MS_PER_HR },
96                 modem.strengthMah, EPSILON);
97         Assert.assertEquals(2443805 * 12 / MS_PER_HR, modem.scanningMah, EPSILON);
98         Assert.assertEquals(4923676 * AVERAGE_MODEM_POWER / MS_PER_HR, modem.activeMah, EPSILON);
99 
100         Assert.assertEquals(REMAINDER_MODEM_MAH, modem.powerMah, EPSILON);
101     }
102 
testAppTotal()103     @Test public void testAppTotal() throws Exception {
104         final PowerReport report = loadPowerReport();
105 
106         final List<AppPower> gmailList = report.findApp("com.google.android.gm");
107         Assert.assertEquals(1, gmailList.size());
108         final AppPower gmail = gmailList.get(0);
109 
110         Assert.assertEquals(GMAIL_MAH, gmail.getAppPowerMah(), EPSILON);
111     }
112 
testRemainderTotal()113     @Test public void testRemainderTotal() throws Exception {
114         final PowerReport report = loadPowerReport();
115 
116         final AppPower remainder = report.findApp(SpecialApp.REMAINDER);
117         Assert.assertNotNull(remainder);
118 
119         Assert.assertEquals(REMAINDER_MAH, remainder.getAppPowerMah(), EPSILON);
120     }
121 
testTotal()122     @Test public void testTotal() throws Exception {
123         final PowerReport report = loadPowerReport();
124 
125         Assert.assertEquals(TOTAL_MAH, report.getTotalPowerMah(), EPSILON);
126     }
127 }
128 
129