Test network restrictions while on foreground service state.

am: eb7e505

* commit 'eb7e5053c14775439f7df880462c53344080f60c':
  Test network restrictions while on foreground service state.

Change-Id: I27a363f75b64208a9873b7b48e0ccb9e5eff9de4
This commit is contained in:
Felipe Leme
2016-03-30 22:43:36 +00:00
committed by android-build-merger
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 int SECOND_IN_MS = 1000;
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
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);
}
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.
*/
protected boolean isBackground(int state) {
return state > 4; // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE;
return state >= PROCESS_STATE_FOREGROUND_SERVICE;
}
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
* {@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");
// Wait until receiver is ready.
final int maxTries = 5;
@@ -396,6 +405,11 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
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) {
switch (status) {
case RESTRICT_BACKGROUND_STATUS_DISABLED:

View File

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

View File

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

View File

@@ -37,7 +37,7 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase
setMeteredNetwork();
setRestrictBackground(false);
registerApp2BroadcastReceiver();
registerBroadcastReceiver();
}
@Override
@@ -75,6 +75,12 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase
removeRestrictBackgroundWhitelist(mUid);
assertRestrictBackgroundChangedReceived(1);
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 {

View File

@@ -32,6 +32,7 @@
-->
<application>
<service android:name=".MyService" android:exported="true"/>
<service android:name=".MyForegroundService" android:exported="true"/>
<receiver android:name=".MyBroadcastReceiver" >
<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
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 MyBroadcastReceiver myReceiver = new MyBroadcastReceiver(DYNAMIC_RECEIVER);
context.registerReceiver(myReceiver, new IntentFilter(ACTION_RECEIVER_READY));