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.resetBatterySaver; 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 import com.android.compatibility.common.util.ProtoUtils; 38 import com.android.compatibility.common.util.UserSettings; 39 import com.android.compatibility.common.util.UserSettings.Namespace; 40 import com.android.server.job.nano.JobSchedulerServiceDumpProto; 41 import com.android.server.job.nano.StateControllerProto; 42 43 import org.junit.Rule; 44 import org.junit.rules.RuleChain; 45 import org.junit.runner.Description; 46 import org.junit.runners.model.Statement; 47 48 public class BatterySavingTestBase { 49 private static final String TAG = "BatterySavingTestBase"; 50 51 public static final int DEFAULT_TIMEOUT_SECONDS = 30; 52 53 public static final boolean DEBUG = false; 54 55 protected static final UserSettings sGlobalSettings = new UserSettings(Namespace.GLOBAL); 56 57 protected final BroadcastRpc mRpc = new BroadcastRpc(); 58 59 private final OnFailureRule mDumpOnFailureRule = new OnFailureRule(TAG) { 60 @Override 61 protected void onTestFailure(Statement base, Description description, Throwable t) { 62 runCommandAndPrintOnLogcat(TAG, "dumpsys power"); 63 runCommandAndPrintOnLogcat(TAG, "dumpsys alarm"); 64 runCommandAndPrintOnLogcat(TAG, "dumpsys jobscheduler"); 65 runCommandAndPrintOnLogcat(TAG, "dumpsys content"); 66 runCommandAndPrintOnLogcat(TAG, "dumpsys battery"); 67 } 68 }; 69 70 private final BeforeAfterRule mInitializeAndCleanupRule = new BeforeAfterRule() { 71 @Override 72 protected void onBefore(Statement base, Description description) throws Throwable { 73 BatteryUtils.assumeBatterySaverFeature(); 74 75 turnOnScreen(true); 76 } 77 78 @Override 79 protected void onAfter(Statement base, Description description) throws Throwable { 80 runDumpsysBatteryReset(); 81 turnOnScreen(true); 82 resetBatterySaver(); 83 } 84 }; 85 86 @Rule 87 public RuleChain Rules = RuleChain.outerRule(mInitializeAndCleanupRule) 88 .around(mDumpOnFailureRule); 89 getLogTag()90 public String getLogTag() { 91 return TAG; 92 } 93 94 /** Print a debug log on logcat. */ debug(String message)95 public void debug(String message) { 96 if (DEBUG || Log.isLoggable(TAG, Log.DEBUG)) { 97 Log.d(getLogTag(), message); 98 } 99 } 100 waitUntilAlarmForceAppStandby(boolean expected)101 public void waitUntilAlarmForceAppStandby(boolean expected) throws Exception { 102 waitUntil("Force all apps standby still " + !expected + " (alarm)", () -> 103 runShellCommand("dumpsys alarm").contains("Force all apps standby: " + expected)); 104 } 105 waitUntilJobForceAppStandby(boolean expected)106 public void waitUntilJobForceAppStandby(boolean expected) throws Exception { 107 waitUntil("Force all apps standby still " + !expected + " (job)", () -> { 108 JobSchedulerServiceDumpProto proto = ProtoUtils.getProto( 109 InstrumentationRegistry.getInstrumentation().getUiAutomation(), 110 JobSchedulerServiceDumpProto.class, 111 ProtoUtils.DUMPSYS_JOB_SCHEDULER); 112 for (StateControllerProto controllerProto : proto.controllers) { 113 if (controllerProto.hasBackground()) { 114 return controllerProto.getBackground().appStateTracker.forceAllAppsStandby 115 == expected; 116 } 117 } 118 return false; 119 }); 120 } 121 waitUntilForceBackgroundCheck(boolean expected)122 public void waitUntilForceBackgroundCheck(boolean expected) throws Exception { 123 waitUntil("Force background check still " + !expected + " (job)", () -> 124 runShellCommand("dumpsys activity").contains("mForceBackgroundCheck=" + expected)); 125 } 126 getContext()127 public static Context getContext() { 128 return InstrumentationRegistry.getContext(); 129 } 130 getPackageManager()131 public PackageManager getPackageManager() { 132 return getContext().getPackageManager(); 133 } 134 getPowerManager()135 public PowerManager getPowerManager() { 136 return getContext().getSystemService(PowerManager.class); 137 } 138 getBatteryManager()139 public BatteryManager getBatteryManager() { 140 return getContext().getSystemService(BatteryManager.class); 141 } 142 getLocationManager()143 public LocationManager getLocationManager() { 144 return getContext().getSystemService(LocationManager.class); 145 } 146 } 147