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.os.cts.batterysaving; 17 18 import static com.android.compatibility.common.util.BatteryUtils.enableBatterySaver; 19 import static com.android.compatibility.common.util.BatteryUtils.runDumpsysBatteryReset; 20 import static com.android.compatibility.common.util.BatteryUtils.turnOnScreen; 21 import static com.android.compatibility.common.util.SystemUtil.runCommandAndPrintOnLogcat; 22 import static com.android.compatibility.common.util.SystemUtil.runShellCommand; 23 import static com.android.compatibility.common.util.TestUtils.waitUntil; 24 25 import android.content.Context; 26 import android.content.pm.PackageManager; 27 import android.location.LocationManager; 28 import android.os.BatteryManager; 29 import android.os.PowerManager; 30 import android.util.Log; 31 32 import androidx.test.InstrumentationRegistry; 33 34 import com.android.compatibility.common.util.BatteryUtils; 35 import com.android.compatibility.common.util.BeforeAfterRule; 36 import com.android.compatibility.common.util.OnFailureRule; 37 38 import org.junit.Rule; 39 import org.junit.rules.RuleChain; 40 import org.junit.runner.Description; 41 import org.junit.runners.model.Statement; 42 43 public class BatterySavingTestBase { 44 private static final String TAG = "BatterySavingTestBase"; 45 46 public static final int DEFAULT_TIMEOUT_SECONDS = 30; 47 48 public static final boolean DEBUG = false; 49 50 protected final BroadcastRpc mRpc = new BroadcastRpc(); 51 52 private final OnFailureRule mDumpOnFailureRule = new OnFailureRule(TAG) { 53 @Override 54 protected void onTestFailure(Statement base, Description description, Throwable t) { 55 runCommandAndPrintOnLogcat(TAG, "dumpsys power"); 56 runCommandAndPrintOnLogcat(TAG, "dumpsys alarm"); 57 runCommandAndPrintOnLogcat(TAG, "dumpsys jobscheduler"); 58 runCommandAndPrintOnLogcat(TAG, "dumpsys content"); 59 runCommandAndPrintOnLogcat(TAG, "dumpsys battery"); 60 } 61 }; 62 63 private final BeforeAfterRule mInitializeAndCleanupRule = new BeforeAfterRule() { 64 @Override 65 protected void onBefore(Statement base, Description description) throws Throwable { 66 BatteryUtils.assumeBatterySaverFeature(); 67 68 turnOnScreen(true); 69 } 70 71 @Override 72 protected void onAfter(Statement base, Description description) throws Throwable { 73 runDumpsysBatteryReset(); 74 turnOnScreen(true); 75 enableBatterySaver(false); 76 } 77 }; 78 79 @Rule 80 public RuleChain Rules = RuleChain.outerRule(mInitializeAndCleanupRule) 81 .around(mDumpOnFailureRule); 82 getLogTag()83 public String getLogTag() { 84 return TAG; 85 } 86 87 /** Print a debug log on logcat. */ debug(String message)88 public void debug(String message) { 89 if (DEBUG || Log.isLoggable(TAG, Log.DEBUG)) { 90 Log.d(getLogTag(), message); 91 } 92 } 93 waitUntilAlarmForceAppStandby(boolean expected)94 public void waitUntilAlarmForceAppStandby(boolean expected) throws Exception { 95 waitUntil("Force all apps standby still " + !expected + " (alarm)", () -> 96 runShellCommand("dumpsys alarm").contains("Force all apps standby: " + expected)); 97 } 98 waitUntilJobForceAppStandby(boolean expected)99 public void waitUntilJobForceAppStandby(boolean expected) throws Exception { 100 waitUntil("Force all apps standby still " + !expected + " (job)", () -> 101 runShellCommand("dumpsys jobscheduler") 102 .contains("Force all apps standby: " + expected)); 103 } 104 waitUntilForceBackgroundCheck(boolean expected)105 public void waitUntilForceBackgroundCheck(boolean expected) throws Exception { 106 waitUntil("Force background check still " + !expected + " (job)", () -> 107 runShellCommand("dumpsys activity").contains("mForceBackgroundCheck=" + expected)); 108 } 109 getContext()110 public static Context getContext() { 111 return InstrumentationRegistry.getContext(); 112 } 113 getPackageManager()114 public PackageManager getPackageManager() { 115 return getContext().getPackageManager(); 116 } 117 getPowerManager()118 public PowerManager getPowerManager() { 119 return getContext().getSystemService(PowerManager.class); 120 } 121 getBatteryManager()122 public BatteryManager getBatteryManager() { 123 return getContext().getSystemService(BatteryManager.class); 124 } 125 getLocationManager()126 public LocationManager getLocationManager() { 127 return getContext().getSystemService(LocationManager.class); 128 } 129 } 130