Added CTS tests for RESTRICT_BACKGROUND_CHANGED.
These tests require a second app (besides the test app) that defines a
service; the host-side test then launches the service whose only purpose
is to define a broadcast receiver, which in turn will count the number
of intents received in a shared preferences file. Then the test app will
read the shared preferences and assert the proper number of intents have
been received.
BUG: 26451391
Change-Id: I4c5d5e57c09a0bd57a7f6581820cc9115318dd47
diff --git a/tests/cts/hostside/app/AndroidManifest.xml b/tests/cts/hostside/app/AndroidManifest.xml
index cdde7dc..f44fdd1 100644
--- a/tests/cts/hostside/app/AndroidManifest.xml
+++ b/tests/cts/hostside/app/AndroidManifest.xml
@@ -15,7 +15,8 @@
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.android.cts.net.hostside">
+ package="com.android.cts.net.hostside"
+ android:sharedUserId="com.android.cts.net.hostside.apps">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/ConnectivityManagerTest.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/ConnectivityManagerTest.java
index 5d3812c..300e39d 100644
--- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/ConnectivityManagerTest.java
+++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/ConnectivityManagerTest.java
@@ -16,10 +16,14 @@
package com.android.cts.net.hostside;
+import static android.net.ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED;
import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLED;
import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED;
import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_WHITELISTED;
+
import android.app.Activity;
+import android.content.Context;
+import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.test.InstrumentationTestCase;
import android.util.Log;
@@ -34,6 +38,9 @@
public class ConnectivityManagerTest extends InstrumentationTestCase {
private static final String TAG = "ConnectivityManagerTest";
+ static final String MANIFEST_RECEIVER = "ManifestReceiver";
+ static final String DYNAMIC_RECEIVER = "DynamicReceiver";
+
private ConnectivityManager mCM;
@Override
@@ -41,7 +48,7 @@
super.setUp();
mCM = (ConnectivityManager) getInstrumentation().getContext().getSystemService(
Activity.CONNECTIVITY_SERVICE);
- }
+ }
public void testGetRestrictBackgroundStatus_disabled() {
assertRestrictBackgroundStatus(RESTRICT_BACKGROUND_STATUS_DISABLED);
@@ -55,6 +62,46 @@
assertRestrictBackgroundStatus(RESTRICT_BACKGROUND_STATUS_ENABLED);
}
+ public void testRestrictBackgroundChangedReceivedOnce() throws Exception {
+ assertRestrictBackgroundChangedReceived(DYNAMIC_RECEIVER, 1);
+ assertRestrictBackgroundChangedReceived(MANIFEST_RECEIVER, 0);
+ }
+
+ public void testRestrictBackgroundChangedReceivedTwice() throws Exception {
+ assertRestrictBackgroundChangedReceived(DYNAMIC_RECEIVER, 2);
+ assertRestrictBackgroundChangedReceived(MANIFEST_RECEIVER, 0);
+ }
+
+ private void assertRestrictBackgroundChangedReceived(String receiverName, int expectedCount)
+ throws Exception {
+ int attempts = 0;
+ int count = 0;
+ final int maxAttempts = 5;
+ final int sleepTime = 10;
+ do {
+ attempts++;
+ count = getNumberBroadcastsReceived(getInstrumentation().getContext(), receiverName,
+ ACTION_RESTRICT_BACKGROUND_CHANGED);
+ if (count == expectedCount) {
+ break;
+ }
+ Log.d(TAG, "Count is " + count + " after " + attempts + " attempts; sleeping "
+ + sleepTime + " seconds before trying again");
+ Thread.sleep(sleepTime * 1000);
+ } while (attempts <= maxAttempts);
+ assertEquals("Number of expected broadcasts for " + receiverName + " not reached after "
+ + maxAttempts * sleepTime + " seconds", expectedCount, count);
+ }
+
+ static int getNumberBroadcastsReceived(Context context, String receiverName, String action)
+ throws Exception {
+ final Context sharedContext = context.createPackageContext(
+ "com.android.cts.net.hostside.app2", Context.CONTEXT_IGNORE_SECURITY);
+ final SharedPreferences prefs = sharedContext.getSharedPreferences(receiverName,
+ Context.MODE_PRIVATE);
+ return prefs.getInt(action, 0);
+ }
+
private void assertRestrictBackgroundStatus(int expectedStatus) {
final String expected = toString(expectedStatus);
Log.d(TAG, getName() + " (expecting " + expected + ")");
diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/MyActivity.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/MyActivity.java
index 375c852..0d0bc58 100644
--- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/MyActivity.java
+++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/MyActivity.java
@@ -18,13 +18,9 @@
import android.app.Activity;
import android.content.Intent;
-import android.net.VpnService;
import android.os.Bundle;
-import android.os.ParcelFileDescriptor;
import android.view.WindowManager;
-import java.util.Arrays;
-import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/MyVpnService.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/MyVpnService.java
index a3f400c..90a3ce4 100644
--- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/MyVpnService.java
+++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/MyVpnService.java
@@ -26,7 +26,6 @@
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
-import java.util.ArrayList;
public class MyVpnService extends VpnService {