Merge "Added sanity check to verify process state." into nyc-dev
This commit is contained in:
@@ -25,6 +25,7 @@ import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_WHITELI
|
|||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import android.app.ActivityManager;
|
||||||
import android.app.Instrumentation;
|
import android.app.Instrumentation;
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@@ -86,7 +87,9 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
|
|||||||
final int myUid = mContext.getPackageManager()
|
final int myUid = mContext.getPackageManager()
|
||||||
.getPackageInfo(mContext.getPackageName(), 0).applicationInfo.uid;
|
.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
|
@Override
|
||||||
@@ -158,6 +161,7 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void assertRestrictBackgroundStatus(int expectedApiStatus) throws Exception {
|
protected void assertRestrictBackgroundStatus(int expectedApiStatus) throws Exception {
|
||||||
|
assertBackgroundState(); // Sanity check.
|
||||||
final Intent intent = new Intent(ACTION_CHECK_NETWORK);
|
final Intent intent = new Intent(ACTION_CHECK_NETWORK);
|
||||||
final String resultData = sendOrderedBroadcast(intent);
|
final String resultData = sendOrderedBroadcast(intent);
|
||||||
final String[] resultItems = resultData.split(RESULT_SEPARATOR);
|
final String[] resultItems = resultData.split(RESULT_SEPARATOR);
|
||||||
@@ -171,6 +175,7 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void assertBackgroundNetworkAccess(boolean expectAllowed) throws Exception {
|
protected void assertBackgroundNetworkAccess(boolean expectAllowed) throws Exception {
|
||||||
|
assertBackgroundState(); // Sanity check.
|
||||||
final Intent intent = new Intent(ACTION_CHECK_NETWORK);
|
final Intent intent = new Intent(ACTION_CHECK_NETWORK);
|
||||||
final String resultData = sendOrderedBroadcast(intent);
|
final String resultData = sendOrderedBroadcast(intent);
|
||||||
final String[] resultItems = resultData.split(RESULT_SEPARATOR);
|
final String[] resultItems = resultData.split(RESULT_SEPARATOR);
|
||||||
@@ -178,6 +183,20 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
|
|||||||
assertNetworkStatus(expectAllowed, networkStatus);
|
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) {
|
private String getNetworkStatus(String[] resultItems) {
|
||||||
return resultItems.length < 2 ? null : resultItems[1];
|
return resultItems.length < 2 ? null : resultItems[1];
|
||||||
}
|
}
|
||||||
@@ -386,4 +405,27 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
|
|||||||
return "UNKNOWN_STATUS_" + status;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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_ENABLED;
|
||||||
import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_WHITELISTED;
|
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 {
|
public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -30,7 +30,6 @@ public class HostsideRestrictBackgroundNetworkTests extends HostsideNetworkTestC
|
|||||||
|
|
||||||
uninstallPackage(TEST_APP2_PKG, false);
|
uninstallPackage(TEST_APP2_PKG, false);
|
||||||
installPackage(TEST_APP2_APK);
|
installPackage(TEST_APP2_APK);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user