Merge "Acquire a wakelock before sending KEYCODE_SLEEP" into android13-tests-dev

This commit is contained in:
Shubhangi Pawar
2022-12-13 00:16:21 +00:00
committed by Gerrit Code Review
2 changed files with 9 additions and 0 deletions

View File

@@ -30,6 +30,7 @@
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/> <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application android:requestLegacyExternalStorage="true"> <application android:requestLegacyExternalStorage="true">
<uses-library android:name="android.test.runner"/> <uses-library android:name="android.test.runner"/>

View File

@@ -56,6 +56,7 @@ import android.os.Binder;
import android.os.Bundle; import android.os.Bundle;
import android.os.RemoteCallback; import android.os.RemoteCallback;
import android.os.SystemClock; import android.os.SystemClock;
import android.os.PowerManager;
import android.provider.DeviceConfig; import android.provider.DeviceConfig;
import android.service.notification.NotificationListenerService; import android.service.notification.NotificationListenerService;
import android.util.Log; import android.util.Log;
@@ -163,6 +164,8 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
private int mMyUid; private int mMyUid;
private MyServiceClient mServiceClient; private MyServiceClient mServiceClient;
private DeviceConfigStateHelper mDeviceIdleDeviceConfigStateHelper; private DeviceConfigStateHelper mDeviceIdleDeviceConfigStateHelper;
private PowerManager mPowerManager;
private PowerManager.WakeLock mLock;
@Rule @Rule
public final RuleChain mRuleChain = RuleChain.outerRule(new RequiredPropertiesRule()) public final RuleChain mRuleChain = RuleChain.outerRule(new RequiredPropertiesRule())
@@ -181,8 +184,10 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
mMyUid = getUid(mContext.getPackageName()); mMyUid = getUid(mContext.getPackageName());
mServiceClient = new MyServiceClient(mContext); mServiceClient = new MyServiceClient(mContext);
mServiceClient.bind(); mServiceClient.bind();
mPowerManager = mContext.getSystemService(PowerManager.class);
executeShellCommand("cmd netpolicy start-watching " + mUid); executeShellCommand("cmd netpolicy start-watching " + mUid);
setAppIdle(false); setAppIdle(false);
mLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
Log.i(TAG, "Apps status:\n" Log.i(TAG, "Apps status:\n"
+ "\ttest app: uid=" + mMyUid + ", state=" + getProcessStateByUid(mMyUid) + "\n" + "\ttest app: uid=" + mMyUid + ", state=" + getProcessStateByUid(mMyUid) + "\n"
@@ -192,6 +197,7 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
executeShellCommand("cmd netpolicy stop-watching"); executeShellCommand("cmd netpolicy stop-watching");
mServiceClient.unbind(); mServiceClient.unbind();
if (mLock.isHeld()) mLock.release();
} }
protected int getUid(String packageName) throws Exception { protected int getUid(String packageName) throws Exception {
@@ -695,11 +701,13 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
} }
protected void turnScreenOff() throws Exception { protected void turnScreenOff() throws Exception {
if (!mLock.isHeld()) mLock.acquire();
executeSilentShellCommand("input keyevent KEYCODE_SLEEP"); executeSilentShellCommand("input keyevent KEYCODE_SLEEP");
} }
protected void turnScreenOn() throws Exception { protected void turnScreenOn() throws Exception {
executeSilentShellCommand("input keyevent KEYCODE_WAKEUP"); executeSilentShellCommand("input keyevent KEYCODE_WAKEUP");
if (mLock.isHeld()) mLock.release();
executeSilentShellCommand("wm dismiss-keyguard"); executeSilentShellCommand("wm dismiss-keyguard");
} }