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