1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.systemui.power; 16 17 import static android.os.HardwarePropertiesManager.DEVICE_TEMPERATURE_SKIN; 18 import static android.os.HardwarePropertiesManager.TEMPERATURE_CURRENT; 19 import static android.os.HardwarePropertiesManager.TEMPERATURE_SHUTDOWN; 20 import static android.provider.Settings.Global.SHOW_TEMPERATURE_WARNING; 21 22 import static org.mockito.Mockito.mock; 23 import static org.mockito.Mockito.never; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.content.Context; 28 import android.os.HardwarePropertiesManager; 29 import android.provider.Settings; 30 import android.testing.AndroidTestingRunner; 31 import android.testing.TestableLooper.RunWithLooper; 32 import android.testing.TestableResources; 33 import android.test.suitebuilder.annotation.SmallTest; 34 35 import com.android.systemui.R; 36 import com.android.systemui.SysuiTestCase; 37 import com.android.systemui.power.PowerUI.WarningsUI; 38 import com.android.systemui.statusbar.phone.StatusBar; 39 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 44 @RunWith(AndroidTestingRunner.class) 45 @RunWithLooper 46 @SmallTest 47 public class PowerUITest extends SysuiTestCase { 48 49 private HardwarePropertiesManager mHardProps; 50 private WarningsUI mMockWarnings; 51 private PowerUI mPowerUI; 52 53 @Before 54 public void setup() { 55 mMockWarnings = mDependency.injectMockDependency(WarningsUI.class); 56 mHardProps = mock(HardwarePropertiesManager.class); 57 mContext.putComponent(StatusBar.class, mock(StatusBar.class)); 58 mContext.addMockSystemService(Context.HARDWARE_PROPERTIES_SERVICE, mHardProps); 59 60 createPowerUi(); 61 } 62 63 @Test 64 public void testNoConfig_NoWarnings() { 65 setOverThreshold(); 66 Settings.Global.putString(mContext.getContentResolver(), SHOW_TEMPERATURE_WARNING, null); 67 TestableResources resources = mContext.getOrCreateTestableResources(); 68 resources.addOverride(R.integer.config_showTemperatureWarning, 0); 69 resources.addOverride(R.integer.config_warningTemperature, 55); 70 71 mPowerUI.start(); 72 verify(mMockWarnings, never()).showHighTemperatureWarning(); 73 } 74 75 @Test 76 public void testConfig_NoWarnings() { 77 setUnderThreshold(); 78 Settings.Global.putString(mContext.getContentResolver(), SHOW_TEMPERATURE_WARNING, null); 79 TestableResources resources = mContext.getOrCreateTestableResources(); 80 resources.addOverride(R.integer.config_showTemperatureWarning, 1); 81 resources.addOverride(R.integer.config_warningTemperature, 55); 82 83 mPowerUI.start(); 84 verify(mMockWarnings, never()).showHighTemperatureWarning(); 85 } 86 87 @Test 88 public void testConfig_Warnings() { 89 setOverThreshold(); 90 Settings.Global.putString(mContext.getContentResolver(), SHOW_TEMPERATURE_WARNING, null); 91 TestableResources resources = mContext.getOrCreateTestableResources(); 92 resources.addOverride(R.integer.config_showTemperatureWarning, 1); 93 resources.addOverride(R.integer.config_warningTemperature, 55); 94 95 mPowerUI.start(); 96 verify(mMockWarnings).showHighTemperatureWarning(); 97 } 98 99 @Test 100 public void testSettingOverrideConfig() { 101 setOverThreshold(); 102 Settings.Global.putInt(mContext.getContentResolver(), SHOW_TEMPERATURE_WARNING, 1); 103 TestableResources resources = mContext.getOrCreateTestableResources(); 104 resources.addOverride(R.integer.config_showTemperatureWarning, 0); 105 resources.addOverride(R.integer.config_warningTemperature, 55); 106 107 mPowerUI.start(); 108 verify(mMockWarnings).showHighTemperatureWarning(); 109 } 110 111 @Test 112 public void testShutdownBasedThreshold() { 113 int tolerance = 2; 114 Settings.Global.putString(mContext.getContentResolver(), SHOW_TEMPERATURE_WARNING, null); 115 TestableResources resources = mContext.getOrCreateTestableResources(); 116 resources.addOverride(R.integer.config_showTemperatureWarning, 1); 117 resources.addOverride(R.integer.config_warningTemperature, -1); 118 resources.addOverride(R.integer.config_warningTemperatureTolerance, tolerance); 119 when(mHardProps.getDeviceTemperatures(DEVICE_TEMPERATURE_SKIN, TEMPERATURE_SHUTDOWN)) 120 .thenReturn(new float[] { 55 + tolerance }); 121 122 setCurrentTemp(54); // Below threshold. 123 mPowerUI.start(); 124 verify(mMockWarnings, never()).showHighTemperatureWarning(); 125 126 setCurrentTemp(56); // Above threshold. 127 mPowerUI.updateTemperatureWarning(); 128 verify(mMockWarnings).showHighTemperatureWarning(); 129 } 130 131 private void setCurrentTemp(float temp) { 132 when(mHardProps.getDeviceTemperatures(DEVICE_TEMPERATURE_SKIN, TEMPERATURE_CURRENT)) 133 .thenReturn(new float[] { temp }); 134 } 135 136 private void setOverThreshold() { 137 setCurrentTemp(50000); 138 } 139 140 private void setUnderThreshold() { 141 setCurrentTemp(5); 142 } 143 144 private void createPowerUi() { 145 mPowerUI = new PowerUI(); 146 mPowerUI.mContext = mContext; 147 mPowerUI.mComponents = mContext.getComponents(); 148 } 149 } 150