From de7cbcf6ee5954c3385e7ac3ca84f8266be7b95c Mon Sep 17 00:00:00 2001 From: Felipe Leme Date: Fri, 25 Mar 2016 17:17:33 -0700 Subject: [PATCH] Added sanity check to verify process state. BUG: 26776313 BUG: 27324964 Change-Id: I209c04d5c6d89acdac9b5bdaee5a4dbd7700c53e --- ...ractRestrictBackgroundNetworkTestCase.java | 44 ++++++++++++++++++- .../cts/net/hostside/DataSaverModeTest.java | 6 +++ ...ostsideRestrictBackgroundNetworkTests.java | 1 - 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java index adaaf84848..68be6a5b5b 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java @@ -25,6 +25,7 @@ import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_WHITELI import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; +import android.app.ActivityManager; import android.app.Instrumentation; import android.content.BroadcastReceiver; import android.content.Context; @@ -86,7 +87,9 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation final int myUid = mContext.getPackageManager() .getPackageInfo(mContext.getPackageName(), 0).applicationInfo.uid; - Log.d(TAG, "UIDS: test app=" + myUid + ", app2=" + mUid); + Log.i(TAG, "Apps status on " + getName() + ":\n" + + "\ttest app: uid=" + myUid + ", state=" + getProcessStateByUid(myUid) + "\n" + + "\tapp2: uid=" + mUid + ", state=" + getProcessStateByUid(mUid)); } @Override @@ -158,6 +161,7 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation } protected void assertRestrictBackgroundStatus(int expectedApiStatus) throws Exception { + assertBackgroundState(); // Sanity check. final Intent intent = new Intent(ACTION_CHECK_NETWORK); final String resultData = sendOrderedBroadcast(intent); final String[] resultItems = resultData.split(RESULT_SEPARATOR); @@ -171,6 +175,7 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation } protected void assertBackgroundNetworkAccess(boolean expectAllowed) throws Exception { + assertBackgroundState(); // Sanity check. final Intent intent = new Intent(ACTION_CHECK_NETWORK); final String resultData = sendOrderedBroadcast(intent); final String[] resultItems = resultData.split(RESULT_SEPARATOR); @@ -178,6 +183,20 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation assertNetworkStatus(expectAllowed, networkStatus); } + protected final void assertBackgroundState() throws Exception { + final ProcessState state = getProcessStateByUid(mUid); + Log.v(TAG, "assertBackgroundState(): status for app2 (" + mUid + "): " + state); + final boolean isBackground = isBackground(state.state); + assertTrue("App2 is not on background state: " + state, isBackground); + } + + /** + * Returns whether an app state should be considered "background" for restriction purposes. + */ + protected boolean isBackground(int state) { + return state > 4; // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE; + } + private String getNetworkStatus(String[] resultItems) { return resultItems.length < 2 ? null : resultItems[1]; } @@ -386,4 +405,27 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation return "UNKNOWN_STATUS_" + status; } } + + private ProcessState getProcessStateByUid(int uid) throws Exception { + return new ProcessState(executeShellCommand("cmd activity get-uid-state " + uid)); + } + + private static class ProcessState { + private final String fullState; + final int state; + + ProcessState(String fullState) { + this.fullState = fullState; + try { + this.state = Integer.parseInt(fullState.split(" ")[0]); + } catch (Exception e) { + throw new IllegalArgumentException("Could not parse " + fullState); + } + } + + @Override + public String toString() { + return fullState; + } + } } diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/DataSaverModeTest.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/DataSaverModeTest.java index b9fca39312..de75a49935 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/DataSaverModeTest.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/DataSaverModeTest.java @@ -20,6 +20,12 @@ import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLE import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED; import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_WHITELISTED; +/* + * TODO: need to add more scenarios: + * - test access on foreground app + * - test access on foreground service app + * - make sure it tests transition of data saver status while app is on foreground + */ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase { @Override diff --git a/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java b/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java index 2bd76e6c93..15a4e0a84f 100644 --- a/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java +++ b/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java @@ -30,7 +30,6 @@ public class HostsideRestrictBackgroundNetworkTests extends HostsideNetworkTestC uninstallPackage(TEST_APP2_PKG, false); installPackage(TEST_APP2_APK); - } @Override