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 
17 package com.android.cts.deviceowner;
18 
19 import android.os.CpuUsageInfo;
20 import android.os.HardwarePropertiesManager;
21 
22 import com.android.bedstead.dpmwrapper.TestAppSystemServiceFactory;
23 
24 /**
25  * Test {@link HardwarePropertiesManager}
26  */
27 public class HardwarePropertiesManagerTest extends BaseDeviceOwnerTest {
28     public static final int MAX_FAN_SPEED = 20000;
29     public static final int MAX_DEVICE_TEMPERATURE = 200;
30     public static final int MONITORING_ITERATION_NUMBER = 10;
31 
32     // Time between checks in milliseconds.
33     public static final long SLEEP_TIME = 10;
34 
checkFanSpeed(float speed)35     private void checkFanSpeed(float speed) {
36         assertTrue(speed >= 0 && speed < MAX_FAN_SPEED);
37     }
38 
checkDeviceTemp(float temp, float throttlingTemp, float shutdownTemp)39     private void checkDeviceTemp(float temp, float throttlingTemp, float shutdownTemp) {
40         // Check validity of current temperature.
41         assertTrue(Math.abs(temp) < MAX_DEVICE_TEMPERATURE
42                 || temp == HardwarePropertiesManager.UNDEFINED_TEMPERATURE);
43 
44         // Compare current temperature and shutdown threshold to determine direction.
45         if (shutdownTemp != HardwarePropertiesManager.UNDEFINED_TEMPERATURE
46                 && throttlingTemp != HardwarePropertiesManager.UNDEFINED_TEMPERATURE) {
47 
48             // Cold (lower) thresholds.
49             if (throttlingTemp > shutdownTemp) {
50                 // Compare current temperature and shutdown threshold.
51                 assertTrue(temp > shutdownTemp
52                         || temp == HardwarePropertiesManager.UNDEFINED_TEMPERATURE);
53             }
54 
55             // Warm (upper) thresholds.
56             else if (throttlingTemp < shutdownTemp) {
57                 // Compare current temperature and shutdown threshold.
58                 assertTrue(temp < shutdownTemp
59                         || temp == HardwarePropertiesManager.UNDEFINED_TEMPERATURE);
60             }
61         }
62     }
63 
64     private void checkCpuUsageInfo(CpuUsageInfo info) {
65         assertTrue(info == null || (info.getActive() >= 0 && info.getTotal() >= 0
66                 && info.getTotal() >= info.getActive()));
67     }
68 
checkFanSpeeds(float[] fanSpeeds)69     private void checkFanSpeeds(float[] fanSpeeds) {
70         for (float speed : fanSpeeds) {
71             checkFanSpeed(speed);
72         }
73     }
74 
checkTemps(float[] temps, float[] throttlingThresholds, float[] shutdownThresholds)75     private void checkTemps(float[] temps, float[] throttlingThresholds,
76             float[] shutdownThresholds) {
77         assertEquals(temps.length, throttlingThresholds.length);
78         assertEquals(temps.length, shutdownThresholds.length);
79         for (int i = 0; i < temps.length; ++i) {
80             checkDeviceTemp(temps[i], throttlingThresholds[i], shutdownThresholds[i]);
81         }
82     }
83 
checkCpuUsages(CpuUsageInfo[] cpuUsages)84     private void checkCpuUsages(CpuUsageInfo[] cpuUsages) {
85         for (CpuUsageInfo info : cpuUsages) {
86             checkCpuUsageInfo(info);
87         }
88     }
89 
90     // Check validity of new array of fan speeds:
91     // the number of fans should be the same.
checkFanSpeeds(float[] speeds, float[] oldSpeeds)92     private void checkFanSpeeds(float[] speeds, float[] oldSpeeds) {
93         assertEquals(speeds.length, oldSpeeds.length);
94     }
95 
96     // Check validity of new array of cpu usages:
97     // The number of CPUs should be the same and total/active time should not decrease.
checkCpuUsages(CpuUsageInfo[] infos, CpuUsageInfo[] oldInfos)98     private void checkCpuUsages(CpuUsageInfo[] infos,
99             CpuUsageInfo[] oldInfos) {
100         assertEquals(infos.length, oldInfos.length);
101         for (int i = 0; i < infos.length; ++i) {
102             assertTrue(oldInfos[i] == null || infos[i] == null
103                     || (oldInfos[i].getActive() <= infos[i].getActive()
104                         && oldInfos[i].getTotal() <= infos[i].getTotal()));
105         }
106     }
107 
108     /**
109      * test points:
110      * 1. Get fan speeds, device temperatures and CPU usage information.
111      * 2. Check for validity.
112      * 3. Sleep.
113      * 4. Do it 10 times and compare with old ones.
114      */
testHardwarePropertiesManager()115     public void testHardwarePropertiesManager() throws InterruptedException,
116             SecurityException {
117         HardwarePropertiesManager hm = TestAppSystemServiceFactory
118                 .getHardwarePropertiesManager(getContext(), BasicAdminReceiver.class);
119 
120         float[] oldFanSpeeds = hm.getFanSpeeds();
121 
122         float[] cpuTemps = hm.getDeviceTemperatures(
123                 HardwarePropertiesManager.DEVICE_TEMPERATURE_CPU,
124                 HardwarePropertiesManager.TEMPERATURE_CURRENT);
125         float[] cpuThrottlingThresholds = hm.getDeviceTemperatures(
126                 HardwarePropertiesManager.DEVICE_TEMPERATURE_CPU,
127                 HardwarePropertiesManager.TEMPERATURE_THROTTLING);
128         float[] cpuShutdownThresholds = hm.getDeviceTemperatures(
129                 HardwarePropertiesManager.DEVICE_TEMPERATURE_CPU,
130                 HardwarePropertiesManager.TEMPERATURE_SHUTDOWN);
131 
132         float[] gpuTemps = hm.getDeviceTemperatures(
133                 HardwarePropertiesManager.DEVICE_TEMPERATURE_GPU,
134                 HardwarePropertiesManager.TEMPERATURE_CURRENT);
135         float[] gpuThrottlingThresholds = hm.getDeviceTemperatures(
136                 HardwarePropertiesManager.DEVICE_TEMPERATURE_GPU,
137                 HardwarePropertiesManager.TEMPERATURE_THROTTLING);
138         float[] gpuShutdownThresholds = hm.getDeviceTemperatures(
139                 HardwarePropertiesManager.DEVICE_TEMPERATURE_GPU,
140                 HardwarePropertiesManager.TEMPERATURE_SHUTDOWN);
141 
142         float[] batteryTemps = hm.getDeviceTemperatures(
143                 HardwarePropertiesManager.DEVICE_TEMPERATURE_BATTERY,
144                 HardwarePropertiesManager.TEMPERATURE_CURRENT);
145         float[] batteryThrottlingThresholds = hm.getDeviceTemperatures(
146                 HardwarePropertiesManager.DEVICE_TEMPERATURE_BATTERY,
147                 HardwarePropertiesManager.TEMPERATURE_THROTTLING);
148         float[] batteryShutdownThresholds = hm.getDeviceTemperatures(
149                 HardwarePropertiesManager.DEVICE_TEMPERATURE_BATTERY,
150                 HardwarePropertiesManager.TEMPERATURE_SHUTDOWN);
151 
152         float[] skinTemps = hm.getDeviceTemperatures(
153                 HardwarePropertiesManager.DEVICE_TEMPERATURE_SKIN,
154                 HardwarePropertiesManager.TEMPERATURE_CURRENT);
155         float[] skinThrottlingThresholds = hm.getDeviceTemperatures(
156                 HardwarePropertiesManager.DEVICE_TEMPERATURE_SKIN,
157                 HardwarePropertiesManager.TEMPERATURE_THROTTLING);
158         float[] skinShutdownThresholds = hm.getDeviceTemperatures(
159                 HardwarePropertiesManager.DEVICE_TEMPERATURE_SKIN,
160                 HardwarePropertiesManager.TEMPERATURE_SHUTDOWN);
161 
162         CpuUsageInfo[] oldCpuUsages = hm.getCpuUsages();
163 
164         checkFanSpeeds(oldFanSpeeds);
165         checkTemps(cpuTemps, cpuThrottlingThresholds, cpuShutdownThresholds);
166         checkTemps(gpuTemps, gpuThrottlingThresholds, gpuShutdownThresholds);
167         checkTemps(batteryTemps, batteryThrottlingThresholds, batteryShutdownThresholds);
168         checkTemps(skinTemps, skinThrottlingThresholds, skinShutdownThresholds);
169         checkCpuUsages(oldCpuUsages);
170 
171         for (int i = 0; i < MONITORING_ITERATION_NUMBER; i++) {
172             Thread.sleep(SLEEP_TIME);
173 
174             float[] fanSpeeds = hm.getFanSpeeds();
175             cpuTemps = hm.getDeviceTemperatures(
176                     HardwarePropertiesManager.DEVICE_TEMPERATURE_CPU,
177                     HardwarePropertiesManager.TEMPERATURE_CURRENT);
178             gpuTemps = hm.getDeviceTemperatures(
179                     HardwarePropertiesManager.DEVICE_TEMPERATURE_GPU,
180                     HardwarePropertiesManager.TEMPERATURE_CURRENT);
181             batteryTemps = hm.getDeviceTemperatures(
182                     HardwarePropertiesManager.DEVICE_TEMPERATURE_BATTERY,
183                     HardwarePropertiesManager.TEMPERATURE_CURRENT);
184             skinTemps = hm.getDeviceTemperatures(
185                     HardwarePropertiesManager.DEVICE_TEMPERATURE_SKIN,
186                     HardwarePropertiesManager.TEMPERATURE_CURRENT);
187             CpuUsageInfo[] cpuUsages = hm.getCpuUsages();
188 
189             checkFanSpeeds(fanSpeeds);
190             checkTemps(cpuTemps, cpuThrottlingThresholds, cpuShutdownThresholds);
191             checkTemps(gpuTemps, gpuThrottlingThresholds, gpuShutdownThresholds);
192             checkTemps(batteryTemps, batteryThrottlingThresholds, batteryShutdownThresholds);
193             checkTemps(skinTemps, skinThrottlingThresholds, skinShutdownThresholds);
194             checkCpuUsages(cpuUsages);
195 
196             // No need to compare length of old and new temperature arrays:
197             // they are compared through throttling and shutdown threshold arrays lengths.
198             checkFanSpeeds(fanSpeeds, oldFanSpeeds);
199             checkCpuUsages(cpuUsages, oldCpuUsages);
200 
201             oldFanSpeeds = fanSpeeds;
202             oldCpuUsages = cpuUsages;
203         }
204     }
205 }
206