Test network restrictions while on foreground service state.

BUG: 27324964
BUG: 26776313

Change-Id: Idcd0a391333d243d17b6bd68c67becaad2b37fdd
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 a701c66..4c272ee 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
@@ -65,6 +65,8 @@
     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 @@
         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 @@
      * 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 @@
         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:
diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/BatterySaverModeNonMeteredTest.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/BatterySaverModeNonMeteredTest.java
index 5181057..8e83fa2 100644
--- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/BatterySaverModeNonMeteredTest.java
+++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/BatterySaverModeNonMeteredTest.java
@@ -25,7 +25,7 @@
 
         setPowerSaveMode(false);
         assertPowerSaveModeWhitelist(TEST_APP2_PKG, false); // Sanity check
-        registerApp2BroadcastReceiver();
+        registerBroadcastReceiver();
     }
 
     @Override
@@ -38,6 +38,10 @@
     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 {
diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/BatterySaverModeTest.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/BatterySaverModeTest.java
index 18e2b3e..6a8540a 100644
--- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/BatterySaverModeTest.java
+++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/BatterySaverModeTest.java
@@ -26,7 +26,7 @@
         setMeteredNetwork();
         setPowerSaveMode(false);
         assertPowerSaveModeWhitelist(TEST_APP2_PKG, false); // Sanity check
-        registerApp2BroadcastReceiver();
+        registerBroadcastReceiver();
     }
 
     @Override
@@ -39,6 +39,11 @@
     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 {
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 2971f9d..ff68090 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
@@ -37,7 +37,7 @@
 
         setMeteredNetwork();
         setRestrictBackground(false);
-        registerApp2BroadcastReceiver();
+        registerBroadcastReceiver();
    }
 
     @Override
@@ -75,6 +75,12 @@
         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 {
diff --git a/tests/cts/hostside/app2/AndroidManifest.xml b/tests/cts/hostside/app2/AndroidManifest.xml
index fa4cb43..9ce5781 100644
--- a/tests/cts/hostside/app2/AndroidManifest.xml
+++ b/tests/cts/hostside/app2/AndroidManifest.xml
@@ -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>
diff --git a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyForegroundService.java b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyForegroundService.java
new file mode 100644
index 0000000..bbafd4c
--- /dev/null
+++ b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyForegroundService.java
@@ -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;
+    }
+}
diff --git a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyService.java b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyService.java
index 55249f2..e6454c7 100644
--- a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyService.java
+++ b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyService.java
@@ -39,7 +39,7 @@
 
     @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));