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 package android.permission.cts; 17 18 import android.os.PowerManager; 19 import android.test.AndroidTestCase; 20 21 import java.time.Duration; 22 23 public class PowerManagerServicePermissionTest extends AndroidTestCase { 24 testSetBatterySaver_requiresPermissions()25 public void testSetBatterySaver_requiresPermissions() { 26 PowerManager manager = getContext().getSystemService(PowerManager.class); 27 boolean batterySaverOn = manager.isPowerSaveMode(); 28 29 try { 30 manager.setPowerSaveModeEnabled(!batterySaverOn); 31 fail("Toggling battery saver requires POWER_SAVER or DEVICE_POWER permission"); 32 } catch (SecurityException e) { 33 // Expected Exception 34 } 35 } 36 testSetDynamicPowerSavings_requiresPermissions()37 public void testSetDynamicPowerSavings_requiresPermissions() { 38 try { 39 PowerManager manager = getContext().getSystemService(PowerManager.class); 40 manager.setDynamicPowerSaveHint(true, 0); 41 fail("Updating the dynamic power savings state requires the POWER_SAVER permission"); 42 } catch (SecurityException e) { 43 // Expected Exception 44 } 45 } 46 testSetBatteryDischargePrediction_requiresPermissions()47 public void testSetBatteryDischargePrediction_requiresPermissions() { 48 try { 49 PowerManager manager = getContext().getSystemService(PowerManager.class); 50 manager.setBatteryDischargePrediction(Duration.ofMillis(1000), false); 51 fail("Updating the discharge prediction requires the DEVICE_POWER" 52 + " or BATTERY_PREDICTION permission"); 53 } catch (SecurityException e) { 54 // Expected Exception 55 } 56 } 57 } 58