CTS: verify that disabled manifest receivers don't get broadcasts

Bug 23350688

Change-Id: I06e4d9b9df73db2c88e25fe4b09f315b318715f8
diff --git a/tests/tests/content/AndroidManifest.xml b/tests/tests/content/AndroidManifest.xml
index a20befb..bffb1f7 100644
--- a/tests/tests/content/AndroidManifest.xml
+++ b/tests/tests/content/AndroidManifest.xml
@@ -117,6 +117,14 @@
             </intent-filter>
         </receiver>
 
+        <!-- Receiver that will be explicitly disabled at runtime -->
+        <receiver android:name="android.content.cts.MockReceiverDisableable"
+                android:enabled="true">
+            <intent-filter android:priority="1">
+                <action android:name="android.content.cts.BroadcastReceiverTest.BROADCAST_DISABLED" />
+            </intent-filter>
+        </receiver>
+
         <activity android:name="android.content.cts.AvailableIntentsActivity"
             android:label="AvailableIntentsActivity">
             <intent-filter>
diff --git a/tests/tests/content/src/android/content/cts/BroadcastReceiverTest.java b/tests/tests/content/src/android/content/cts/BroadcastReceiverTest.java
index 526087b..43d6cfe 100644
--- a/tests/tests/content/src/android/content/cts/BroadcastReceiverTest.java
+++ b/tests/tests/content/src/android/content/cts/BroadcastReceiverTest.java
@@ -24,6 +24,7 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.ServiceConnection;
+import android.content.pm.PackageManager;
 import android.os.Bundle;
 import android.os.IBinder;
 import android.test.ActivityInstrumentationTestCase2;
@@ -45,10 +46,16 @@
             "android.content.cts.BroadcastReceiverTest.BROADCAST_MOCKTEST";
     private static final String ACTION_BROADCAST_TESTABORT =
             "android.content.cts.BroadcastReceiverTest.BROADCAST_TESTABORT";
+    private static final String ACTION_BROADCAST_DISABLED =
+            "android.content.cts.BroadcastReceiverTest.BROADCAST_DISABLED";
 
     private static final long SEND_BROADCAST_TIMEOUT = 5000;
     private static final long START_SERVICE_TIMEOUT  = 3000;
 
+    private static final ComponentName DISABLEABLE_RECEIVER =
+            new ComponentName("com.android.cts.content",
+                    "android.content.cts.MockReceiverDisableable");
+
     public BroadcastReceiverTest() {
         super("com.android.cts.content", MockActivity.class);
     }
@@ -127,6 +134,26 @@
         }
     }
 
+    private class MockReceiverInternalVerifyUncalled extends MockReceiverInternal {
+        final int mExpectedInitialCode;
+
+        public MockReceiverInternalVerifyUncalled(int initialCode) {
+            mExpectedInitialCode = initialCode;
+        }
+
+        @Override
+        public synchronized void onReceive(Context context, Intent intent) {
+            // only update to the expected final values if we're still in the
+            // initial conditions.  The intermediate receiver would have
+            // updated the result code if it [inappropriately] ran.
+            if (getResultCode() == mExpectedInitialCode) {
+                setResultCode(RESULT_INTERNAL_FINAL_CODE);
+            }
+
+            super.onReceive(context, intent);
+        }
+    }
+
     public void testOnReceive () throws InterruptedException {
         final MockActivity activity = getActivity();
 
@@ -202,6 +229,26 @@
                 resultExtras.getString(MockReceiverAbort.RESULT_EXTRAS_ABORT_KEY));
     }
 
+    public void testDisabledBroadcastReceiver() throws Exception {
+        final Context context = getInstrumentation().getContext();
+        PackageManager pm = context.getPackageManager();
+
+        MockReceiverInternalVerifyUncalled lastReceiver =
+                new MockReceiverInternalVerifyUncalled(RESULT_INITIAL_CODE);
+        assertEquals(0, lastReceiver.getResultCode());
+
+        pm.setComponentEnabledSetting(DISABLEABLE_RECEIVER,
+                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
+                PackageManager.DONT_KILL_APP);
+
+        context.sendOrderedBroadcast(
+                new Intent(ACTION_BROADCAST_DISABLED), null, lastReceiver,
+                null, RESULT_INITIAL_CODE, RESULT_INITIAL_DATA, new Bundle());
+        lastReceiver.waitForReceiver(SEND_BROADCAST_TIMEOUT);
+
+        assertEquals(RESULT_INTERNAL_FINAL_CODE, lastReceiver.getResultCode());
+    }
+
     public void testPeekService() throws InterruptedException {
         final MockActivity activity = getActivity();
 
diff --git a/tests/tests/content/src/android/content/cts/MockReceiverDisableable.java b/tests/tests/content/src/android/content/cts/MockReceiverDisableable.java
new file mode 100644
index 0000000..9ee73ed
--- /dev/null
+++ b/tests/tests/content/src/android/content/cts/MockReceiverDisableable.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2015 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 android.content.cts;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+
+public class MockReceiverDisableable extends BroadcastReceiver {
+    public static final int RESULT_CODE = 99;
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        setResultCode(RESULT_CODE);
+    }
+}