Test network restrictions while on foreground service state.

BUG: 27324964
BUG: 26776313

Change-Id: Idcd0a391333d243d17b6bd68c67becaad2b37fdd
This commit is contained in:
Felipe Leme
2016-03-29 17:31:25 -07:00
parent 9a4643cd0e
commit eb7e5053c1
7 changed files with 80 additions and 6 deletions

View File

@@ -65,6 +65,8 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
private static final String STATUS_NETWORK_AVAILABLE_PREFIX = "NetworkAvailable:"; private static final String STATUS_NETWORK_AVAILABLE_PREFIX = "NetworkAvailable:";
private static final int SECOND_IN_MS = 1000; private static final int SECOND_IN_MS = 1000;
private static final int NETWORK_TIMEOUT_MS = 15 * SECOND_IN_MS; private static final int NETWORK_TIMEOUT_MS = 15 * SECOND_IN_MS;
private static final int PROCESS_STATE_FOREGROUND_SERVICE = 4;
// Must be higher than NETWORK_TIMEOUT_MS // Must be higher than NETWORK_TIMEOUT_MS
private static final int ORDERED_BROADCAST_TIMEOUT_MS = NETWORK_TIMEOUT_MS * 4; private static final int ORDERED_BROADCAST_TIMEOUT_MS = NETWORK_TIMEOUT_MS * 4;
@@ -193,11 +195,18 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
assertTrue("App2 is not on background state: " + state, isBackground); assertTrue("App2 is not on background state: " + state, isBackground);
} }
protected final void assertForegroundServiceState() throws Exception {
final ProcessState state = getProcessState(mUid);
Log.v(TAG, "assertForegroundServiceState(): status for app2 (" + mUid + "): " + state);
assertEquals("App2 is not on foreground service state: " + state,
PROCESS_STATE_FOREGROUND_SERVICE, state.state);
}
/** /**
* Returns whether an app state should be considered "background" for restriction purposes. * Returns whether an app state should be considered "background" for restriction purposes.
*/ */
protected boolean isBackground(int state) { protected boolean isBackground(int state) {
return state > 4; // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE; return state >= PROCESS_STATE_FOREGROUND_SERVICE;
} }
private String getNetworkStatus(String[] resultItems) { private String getNetworkStatus(String[] resultItems) {
@@ -379,7 +388,7 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
* The service must run in a separate app because otherwise it would be killed every time * The service must run in a separate app because otherwise it would be killed every time
* {@link #runDeviceTests(String, String)} is executed. * {@link #runDeviceTests(String, String)} is executed.
*/ */
protected void registerApp2BroadcastReceiver() throws Exception { protected void registerBroadcastReceiver() throws Exception {
executeShellCommand("am startservice com.android.cts.net.hostside.app2/.MyService"); executeShellCommand("am startservice com.android.cts.net.hostside.app2/.MyService");
// Wait until receiver is ready. // Wait until receiver is ready.
final int maxTries = 5; final int maxTries = 5;
@@ -396,6 +405,11 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
fail("app2 receiver is not ready"); fail("app2 receiver is not ready");
} }
protected void startForegroundService() throws Exception {
executeShellCommand(
"am startservice com.android.cts.net.hostside.app2/.MyForegroundService");
}
private String toString(int status) { private String toString(int status) {
switch (status) { switch (status) {
case RESTRICT_BACKGROUND_STATUS_DISABLED: case RESTRICT_BACKGROUND_STATUS_DISABLED:

View File

@@ -25,7 +25,7 @@ public class BatterySaverModeNonMeteredTest extends AbstractRestrictBackgroundNe
setPowerSaveMode(false); setPowerSaveMode(false);
assertPowerSaveModeWhitelist(TEST_APP2_PKG, false); // Sanity check assertPowerSaveModeWhitelist(TEST_APP2_PKG, false); // Sanity check
registerApp2BroadcastReceiver(); registerBroadcastReceiver();
} }
@Override @Override
@@ -38,6 +38,10 @@ public class BatterySaverModeNonMeteredTest extends AbstractRestrictBackgroundNe
public void testBackgroundNetworkAccess_enabled() throws Exception { public void testBackgroundNetworkAccess_enabled() throws Exception {
setPowerSaveMode(true); setPowerSaveMode(true);
assertBackgroundNetworkAccess(false); assertBackgroundNetworkAccess(false);
// Make sure app is allowed if running a foreground service.
startForegroundService();
assertForegroundServiceState();
assertBackgroundNetworkAccess(true);
} }
public void testBackgroundNetworkAccess_whitelisted() throws Exception { public void testBackgroundNetworkAccess_whitelisted() throws Exception {

View File

@@ -26,7 +26,7 @@ public class BatterySaverModeTest extends AbstractRestrictBackgroundNetworkTestC
setMeteredNetwork(); setMeteredNetwork();
setPowerSaveMode(false); setPowerSaveMode(false);
assertPowerSaveModeWhitelist(TEST_APP2_PKG, false); // Sanity check assertPowerSaveModeWhitelist(TEST_APP2_PKG, false); // Sanity check
registerApp2BroadcastReceiver(); registerBroadcastReceiver();
} }
@Override @Override
@@ -39,6 +39,11 @@ public class BatterySaverModeTest extends AbstractRestrictBackgroundNetworkTestC
public void testBackgroundNetworkAccess_enabled() throws Exception { public void testBackgroundNetworkAccess_enabled() throws Exception {
setPowerSaveMode(true); setPowerSaveMode(true);
assertBackgroundNetworkAccess(false); assertBackgroundNetworkAccess(false);
// Make sure app is allowed if running a foreground service.
startForegroundService();
assertForegroundServiceState();
assertBackgroundNetworkAccess(true);
} }
public void testBackgroundNetworkAccess_whitelisted() throws Exception { public void testBackgroundNetworkAccess_whitelisted() throws Exception {

View File

@@ -37,7 +37,7 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase
setMeteredNetwork(); setMeteredNetwork();
setRestrictBackground(false); setRestrictBackground(false);
registerApp2BroadcastReceiver(); registerBroadcastReceiver();
} }
@Override @Override
@@ -75,6 +75,12 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase
removeRestrictBackgroundWhitelist(mUid); removeRestrictBackgroundWhitelist(mUid);
assertRestrictBackgroundChangedReceived(1); assertRestrictBackgroundChangedReceived(1);
assertRestrictBackgroundStatus(RESTRICT_BACKGROUND_STATUS_ENABLED); assertRestrictBackgroundStatus(RESTRICT_BACKGROUND_STATUS_ENABLED);
// Make sure app is allowed if running a foreground service.
assertBackgroundNetworkAccess(false);
startForegroundService();
assertForegroundServiceState();
assertBackgroundNetworkAccess(true);
} }
public void testGetRestrictBackgroundStatus_blacklisted() throws Exception { public void testGetRestrictBackgroundStatus_blacklisted() throws Exception {

View File

@@ -32,6 +32,7 @@
--> -->
<application> <application>
<service android:name=".MyService" android:exported="true"/> <service android:name=".MyService" android:exported="true"/>
<service android:name=".MyForegroundService" android:exported="true"/>
<receiver android:name=".MyBroadcastReceiver" > <receiver android:name=".MyBroadcastReceiver" >
<intent-filter> <intent-filter>

View File

@@ -0,0 +1,44 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.cts.net.hostside.app2;
import static com.android.cts.net.hostside.app2.Common.TAG;
import android.R;
import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
/**
* Service used to change app state to FOREGROUND_SERVICE.
*/
public class MyForegroundService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "MyForegroundService.onStartCommand: " + intent);
startForeground(42, new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_dialog_alert) // any icon is fine
.build());
return START_STICKY;
}
}

View File

@@ -39,7 +39,7 @@ public class MyService extends Service {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand: " + intent); Log.d(TAG, "MyService.onStartCommand: " + intent);
final Context context = getApplicationContext(); final Context context = getApplicationContext();
final MyBroadcastReceiver myReceiver = new MyBroadcastReceiver(DYNAMIC_RECEIVER); final MyBroadcastReceiver myReceiver = new MyBroadcastReceiver(DYNAMIC_RECEIVER);
context.registerReceiver(myReceiver, new IntentFilter(ACTION_RECEIVER_READY)); context.registerReceiver(myReceiver, new IntentFilter(ACTION_RECEIVER_READY));