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