Tests for NotificationListenerServices managed profile whitelist

Tests that listeners in the personal side don't see
notifications sent from the managed profile if the
profile owner has set a whitelist that doesn't include
the listener.

Bug: 36657192
Test: cts-tradefed run cts-dev --module DevicePolicyManager --test com.android.cts.devicepolicy.ManagedProfileTest#testCrossProfileNotificationListeners_EmptyWhitelist
Test: cts-tradefed run cts-dev --module DevicePolicyManager --test com.android.cts.devicepolicy.ManagedProfileTest#testCrossProfileNotificationListeners_InWhitelist
Test: cts-tradefed run cts-dev --module DevicePolicyManager --test com.android.cts.devicepolicy.ManagedProfileTest#testCrossProfileNotificationListeners_NullWhitelist

Change-Id: I8d73bad159fa173eecd31ec943cad2dd2efb2c86
diff --git a/hostsidetests/devicepolicy/app/ManagedProfile/AndroidManifest.xml b/hostsidetests/devicepolicy/app/ManagedProfile/AndroidManifest.xml
index b8a5eae..2eaa44f 100644
--- a/hostsidetests/devicepolicy/app/ManagedProfile/AndroidManifest.xml
+++ b/hostsidetests/devicepolicy/app/ManagedProfile/AndroidManifest.xml
@@ -132,6 +132,15 @@
                 <category android:name="android.intent.category.DEFAULT"/>
             </intent-filter>
         </activity>
+
+        <service
+            android:name=".CrossProfileNotificationListenerService"
+            android:label="CrossProfileNotificationListenerService"
+            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
+            <intent-filter>
+                <action android:name="android.service.notification.NotificationListenerService" />
+            </intent-filter>
+        </service>
     </application>
 
     <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
diff --git a/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/CrossProfileNotificationListenerService.java b/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/CrossProfileNotificationListenerService.java
new file mode 100644
index 0000000..35fb2e4
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/CrossProfileNotificationListenerService.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2017 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.managedprofile;
+
+import static com.android.cts.managedprofile.NotificationListenerTest.ACTION_NOTIFICATION_POSTED;
+import static com.android.cts.managedprofile.NotificationListenerTest.ACTION_NOTIFICATION_REMOVED;
+import static com.android.cts.managedprofile.NotificationListenerTest.ACTION_LISTENER_CONNECTED;
+
+import android.content.Intent;
+import android.service.notification.NotificationListenerService;
+import android.service.notification.StatusBarNotification;
+import android.support.v4.content.LocalBroadcastManager;
+import android.util.Log;
+
+public class CrossProfileNotificationListenerService extends NotificationListenerService {
+
+    static final String NOTIFICATION_CHANNEL = "NotificationListenerTest";
+    private static final String TAG = NotificationListenerTest.TAG;
+
+    @Override
+    public void onNotificationPosted(StatusBarNotification sbn) {
+        Log.i(TAG, "onNotificationPosted(" + sbn + ")");
+        sendBroadcastForNotification(sbn, ACTION_NOTIFICATION_POSTED);
+    }
+
+    @Override
+    public void onNotificationRemoved(StatusBarNotification sbn) {
+        Log.i(TAG, "onNotificationRemoved(" + sbn + ")");
+        sendBroadcastForNotification(sbn, ACTION_NOTIFICATION_REMOVED);
+    }
+
+    @Override
+    public void onListenerConnected() {
+        Log.i(TAG, "onListenerConnected() " + android.os.Process.myPid());
+        LocalBroadcastManager.getInstance(this).sendBroadcast(
+                new Intent(ACTION_LISTENER_CONNECTED));
+    }
+
+    @Override
+    public void onListenerDisconnected() {
+        Log.i(TAG, "onListenerDisconnected()");
+    }
+
+    private void sendBroadcastForNotification(StatusBarNotification sbn, String action) {
+        if (NOTIFICATION_CHANNEL.equals(sbn.getNotification().getChannel())) {
+            LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(action));
+        } else {
+            Log.i(TAG, "Notification is for different channel "
+                    + sbn.getNotification().getChannel());
+        }
+    }
+}
diff --git a/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/NotificationListenerTest.java b/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/NotificationListenerTest.java
new file mode 100644
index 0000000..d4cbae7
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/NotificationListenerTest.java
@@ -0,0 +1,252 @@
+/*
+ * Copyright (C) 2017 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.managedprofile;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import android.app.Notification;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
+import android.app.UiAutomation;
+import android.app.admin.DevicePolicyManager;
+import android.content.BroadcastReceiver;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.os.Bundle;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.filters.SmallTest;
+import android.support.test.uiautomator.UiDevice;
+import android.support.v4.content.LocalBroadcastManager;
+import android.util.Log;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+@SmallTest
+public class NotificationListenerTest {
+
+    static final String TAG = "ListenerTest";
+    static final String ACTION_NOTIFICATION_POSTED = "notification_posted";
+    static final String ACTION_NOTIFICATION_REMOVED = "notification_removed";
+    static final String ACTION_LISTENER_CONNECTED = "listener_connected";
+
+    private static final String PARAM_PROFILE_ID = "profile-id";
+
+    static final String SENDER_COMPONENT =
+            "com.android.cts.managedprofiletests.notificationsender/.SendNotification";
+
+    private final LocalBroadcastReceiver mReceiver = new LocalBroadcastReceiver();
+    private Context mContext;
+    private DevicePolicyManager mDpm;
+    private NotificationManager mNotificationManager;
+    private UiDevice mDevice;
+    private int mProfileUserId;
+    private String mPreviousListeners;
+    private boolean mChangedListeners;
+
+    @Before
+    public void setUp() throws Exception {
+        mContext = InstrumentationRegistry.getTargetContext();
+        mDpm = mContext.getSystemService(DevicePolicyManager.class);
+        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
+        mProfileUserId = getParam(InstrumentationRegistry.getArguments(), PARAM_PROFILE_ID);
+        IntentFilter filter = new IntentFilter();
+        filter.addAction(ACTION_NOTIFICATION_POSTED);
+        filter.addAction(ACTION_NOTIFICATION_REMOVED);
+        filter.addAction(ACTION_LISTENER_CONNECTED);
+        LocalBroadcastManager.getInstance(mContext).registerReceiver(mReceiver, filter);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        LocalBroadcastManager.getInstance(mContext).unregisterReceiver(mReceiver);
+        if (mChangedListeners) {
+            if (mPreviousListeners != null) {
+                mDevice.executeShellCommand(
+                        "settings put secure enabled_notification_listeners "
+                        + mPreviousListeners);
+            } else {
+                mDevice.executeShellCommand(
+                        "settings delete secure enabled_notification_listeners");
+            }
+        }
+    }
+
+    @Test
+    public void testSetEmptyWhitelist() throws Exception {
+        mDpm.setPermittedCrossProfileNotificationListeners(
+                BaseManagedProfileTest.ADMIN_RECEIVER_COMPONENT,
+                Collections.<String>emptyList());
+    }
+
+    @Test
+    public void testAddListenerToWhitelist() throws Exception {
+        mDpm.setPermittedCrossProfileNotificationListeners(
+                BaseManagedProfileTest.ADMIN_RECEIVER_COMPONENT,
+                Collections.singletonList(mContext.getPackageName()));
+    }
+
+    @Test
+    public void testSetNullWhitelist() throws Exception {
+        mDpm.setPermittedCrossProfileNotificationListeners(
+                BaseManagedProfileTest.ADMIN_RECEIVER_COMPONENT, null);
+    }
+
+    @Test
+    public void testCanReceiveNotifications() throws Exception {
+        enableNotificationListener();
+
+        sendProfileNotification();
+        assertTrue(mReceiver.waitForNotificationPostedReceived());
+        cancelProfileNotification();
+        assertTrue(mReceiver.waitForNotificationRemovedReceived());
+
+        mReceiver.reset();
+
+        sendPersonalNotification();
+        assertTrue(mReceiver.waitForNotificationPostedReceived());
+        cancelPersonalNotification();
+        assertTrue(mReceiver.waitForNotificationRemovedReceived());
+    }
+
+    @Test
+    public void testCannotReceiveProfileNotifications() throws Exception {
+        enableNotificationListener();
+
+        sendProfileNotification();
+        // Don't see notification or cancellation from work profile.
+        assertFalse(mReceiver.waitForNotificationPostedReceived());
+        cancelProfileNotification();
+        assertFalse(mReceiver.waitForNotificationRemovedReceived());
+
+        mReceiver.reset();
+
+        // Do see the one from the personal side.
+        sendPersonalNotification();
+        assertTrue(mReceiver.waitForNotificationPostedReceived());
+        cancelPersonalNotification();
+        assertTrue(mReceiver.waitForNotificationRemovedReceived());
+    }
+
+    private void cancelProfileNotification() throws IOException {
+        mDevice.executeShellCommand(
+                "am start --user " + mProfileUserId + " -a CANCEL_NOTIFICATION -n "
+                + SENDER_COMPONENT);
+    }
+
+    private void cancelPersonalNotification() throws IOException {
+        mDevice.executeShellCommand(
+                "am start -a CANCEL_NOTIFICATION -n "
+                + SENDER_COMPONENT);
+    }
+
+    private void sendProfileNotification() throws IOException {
+        mDevice.executeShellCommand(
+                "am start --user " + mProfileUserId + " -a POST_NOTIFICATION -n "
+                + SENDER_COMPONENT);
+    }
+
+    private void sendPersonalNotification() throws IOException {
+        mDevice.executeShellCommand(
+                "am start -a POST_NOTIFICATION -n "
+                + SENDER_COMPONENT);
+    }
+
+    private void enableNotificationListener() throws Exception {
+        String listeners = mDevice.executeShellCommand(
+                "settings get secure enabled_notification_listeners").trim();
+        if (listeners.equals("null")) {
+            listeners = null;
+        }
+        String testListener = new ComponentName(
+                mContext, CrossProfileNotificationListenerService.class).flattenToString();
+
+        if (listeners == null || !listeners.contains(testListener)) {
+            mPreviousListeners = listeners;
+            mChangedListeners = true;
+            String newListeners;
+            if (listeners != null && listeners.length() > 0) {
+                newListeners = listeners + ":" + testListener;
+            } else {
+                newListeners = testListener;
+            }
+            mDevice.executeShellCommand(
+                    "settings put secure enabled_notification_listeners "
+                    + newListeners);
+            Log.i(TAG, "Enabled notification listener " + testListener);
+            assertTrue(mReceiver.waitForListenerConnected());
+        }
+    }
+
+    private int getParam(Bundle arguments, String key) throws Exception {
+        String serial = arguments.getString(key);
+        if (serial == null) {
+            throw new IllegalArgumentException("Missing argument " + key);
+        }
+        return Integer.parseInt(serial);
+    }
+
+    static class LocalBroadcastReceiver extends BroadcastReceiver {
+
+        private static final int TIMEOUT_SECONDS = 10;
+
+        private CountDownLatch mNotificationPostedLatch = new CountDownLatch(1);
+        private CountDownLatch mNotificationRemovedLatch = new CountDownLatch(1);
+        private CountDownLatch mListenerConnectedLatch = new CountDownLatch(1);
+
+        public void reset() {
+            mNotificationPostedLatch = new CountDownLatch(1);
+            mNotificationRemovedLatch = new CountDownLatch(1);
+            mListenerConnectedLatch = new CountDownLatch(1);
+        }
+
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            Log.i(TAG, "onReceive(" + intent + ")");
+            if (ACTION_NOTIFICATION_POSTED.equals(intent.getAction())) {
+                mNotificationPostedLatch.countDown();
+            } else if (ACTION_NOTIFICATION_REMOVED.equals(intent.getAction())) {
+                mNotificationRemovedLatch.countDown();
+            } else if (ACTION_LISTENER_CONNECTED.equals(intent.getAction())) {
+                mListenerConnectedLatch.countDown();
+            } else {
+                Log.e(TAG, "Received broadcast for unknown action: " + intent.getAction());
+            }
+        }
+
+        public boolean waitForNotificationPostedReceived() throws InterruptedException {
+            return mNotificationPostedLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
+        }
+
+        public boolean waitForNotificationRemovedReceived() throws InterruptedException {
+            return mNotificationRemovedLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
+        }
+
+        public boolean waitForListenerConnected() throws InterruptedException {
+            return mListenerConnectedLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
+        }
+    }
+
+}
diff --git a/hostsidetests/devicepolicy/app/NotificationSender/Android.mk b/hostsidetests/devicepolicy/app/NotificationSender/Android.mk
new file mode 100644
index 0000000..67509e5
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/NotificationSender/Android.mk
@@ -0,0 +1,37 @@
+# Copyright (C) 2017 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.
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+# Don't include this package in any target.
+LOCAL_MODULE_TAGS := optional
+
+# When built, explicitly put it in the data partition.
+LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+# Tag this module as a cts test artifact
+LOCAL_COMPATIBILITY_SUITE := cts
+
+LOCAL_PACKAGE_NAME := CtsNotificationSenderApp
+
+LOCAL_SDK_VERSION := current
+
+# tag this module as a cts test artifact
+LOCAL_COMPATIBILITY_SUITE := cts
+
+include $(BUILD_CTS_PACKAGE)
diff --git a/hostsidetests/devicepolicy/app/NotificationSender/AndroidManifest.xml b/hostsidetests/devicepolicy/app/NotificationSender/AndroidManifest.xml
new file mode 100644
index 0000000..d78092b
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/NotificationSender/AndroidManifest.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ * Copyright (C) 2017 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.
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.android.cts.managedprofiletests.notificationsender">
+
+    <application>
+      <activity android:name=".SendNotification" >
+      </activity>
+    </application>
+</manifest>
+
diff --git a/hostsidetests/devicepolicy/app/NotificationSender/res/raw/ic_contact_picture.png b/hostsidetests/devicepolicy/app/NotificationSender/res/raw/ic_contact_picture.png
new file mode 100644
index 0000000..37b558b
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/NotificationSender/res/raw/ic_contact_picture.png
Binary files differ
diff --git a/hostsidetests/devicepolicy/app/NotificationSender/src/com/android/cts/managedprofiletests/notificationsender/SendNotification.java b/hostsidetests/devicepolicy/app/NotificationSender/src/com/android/cts/managedprofiletests/notificationsender/SendNotification.java
new file mode 100644
index 0000000..80c4073
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/NotificationSender/src/com/android/cts/managedprofiletests/notificationsender/SendNotification.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2017 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.managedprofiletests.notificationsender;
+
+import android.app.Activity;
+import android.app.Notification;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.util.Log;
+
+import java.lang.Override;
+
+/**
+ * A simple activity to post notifications to test notification
+ * listener whitelists.
+ */
+public class SendNotification extends Activity {
+
+    private static final String TAG = "ListenerTest";
+
+    static final int NOTIFICATION_ID = 98765;
+    static final String NOTIFICATION_CHANNEL = "NotificationListenerTest";
+
+    @Override
+    public void onCreate(Bundle icicle) {
+        super.onCreate(icicle);
+        Intent intent = getIntent();
+        if (intent != null && "POST_NOTIFICATION".equals(intent.getAction())) {
+            Log.i(TAG, "posting from " + android.os.Process.myUserHandle());
+            sendNotification();
+        } else if (intent != null && "CANCEL_NOTIFICATION".equals(intent.getAction())) {
+            Log.i(TAG, "cancelling from " + android.os.Process.myUserHandle());
+            cancelNotification();
+        }
+        finish();
+    }
+
+    private void sendNotification() {
+        NotificationManager notificationManager =
+                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+        notificationManager.createNotificationChannel(new NotificationChannel(
+                        NOTIFICATION_CHANNEL, "Test channel", NotificationManager.IMPORTANCE_DEFAULT));
+        notificationManager.notify(NOTIFICATION_ID,
+                new Notification.Builder(getApplicationContext(), NOTIFICATION_CHANNEL)
+                .setSmallIcon(R.raw.ic_contact_picture)
+                .setContentTitle("Test title")
+                .build());
+    }
+
+    private void cancelNotification() {
+        NotificationManager notificationManager =
+                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+        notificationManager.cancel(NOTIFICATION_ID);
+        notificationManager.deleteNotificationChannel(NOTIFICATION_CHANNEL);
+    }
+}
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java
index 26da677..1b501ac 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java
@@ -22,6 +22,7 @@
 
 import junit.framework.AssertionFailedError;
 
+import java.util.Collections;
 import java.util.concurrent.Callable;
 import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
@@ -61,6 +62,12 @@
             = "content://com.android.cts.contact.directory.provider/";
     private static final String SET_CUSTOM_DIRECTORY_PREFIX_METHOD = "set_prefix";
 
+    private static final String NOTIFICATION_APK = "CtsNotificationSenderApp.apk";
+    private static final String NOTIFICATION_PKG =
+            "com.android.cts.managedprofiletests.notificationsender";
+    private static final String NOTIFICATION_ACTIVITY =
+            NOTIFICATION_PKG + ".SendNotification";
+
     private static final String ADMIN_RECEIVER_TEST_CLASS =
             MANAGED_PROFILE_PKG + ".BaseManagedProfileTest$BasicAdminReceiver";
 
@@ -75,6 +82,8 @@
 
     private static final long TIMEOUT_USER_LOCKED_MILLIS = TimeUnit.SECONDS.toMillis(15);
 
+    private static final String PARAM_PROFILE_ID = "profile-id";
+
     private int mParentUserId;
 
     // ID of the profile we'll create. This will always be a profile of the parent.
@@ -112,6 +121,7 @@
             getDevice().uninstallPackage(MANAGED_PROFILE_PKG);
             getDevice().uninstallPackage(INTENT_SENDER_PKG);
             getDevice().uninstallPackage(INTENT_RECEIVER_PKG);
+            getDevice().uninstallPackage(NOTIFICATION_PKG);
         }
         super.tearDown();
     }
@@ -353,6 +363,60 @@
 
     }
 
+    public void testCrossProfileNotificationListeners_EmptyWhitelist() throws Exception {
+        if (!mHasFeature) {
+            return;
+        }
+
+        installAppAsUser(NOTIFICATION_APK, mProfileUserId);
+        installAppAsUser(NOTIFICATION_APK, mParentUserId);
+
+        // Profile owner in the profile sets an empty whitelist
+        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".NotificationListenerTest",
+                "testSetEmptyWhitelist", mProfileUserId,
+                Collections.singletonMap(PARAM_PROFILE_ID, Integer.toString(mProfileUserId)));
+        // Listener outside the profile can only see personal notifications.
+        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".NotificationListenerTest",
+                "testCannotReceiveProfileNotifications", mParentUserId,
+                Collections.singletonMap(PARAM_PROFILE_ID, Integer.toString(mProfileUserId)));
+    }
+
+    public void testCrossProfileNotificationListeners_NullWhitelist() throws Exception {
+        if (!mHasFeature) {
+            return;
+        }
+
+        installAppAsUser(NOTIFICATION_APK, mProfileUserId);
+        installAppAsUser(NOTIFICATION_APK, mParentUserId);
+
+        // Profile owner in the profile sets a null whitelist
+        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".NotificationListenerTest",
+                "testSetNullWhitelist", mProfileUserId,
+                Collections.singletonMap(PARAM_PROFILE_ID, Integer.toString(mProfileUserId)));
+        // Listener outside the profile can see profile and personal notifications
+        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".NotificationListenerTest",
+                "testCanReceiveNotifications", mParentUserId,
+                Collections.singletonMap(PARAM_PROFILE_ID, Integer.toString(mProfileUserId)));
+    }
+
+    public void testCrossProfileNotificationListeners_InWhitelist() throws Exception {
+        if (!mHasFeature) {
+            return;
+        }
+
+        installAppAsUser(NOTIFICATION_APK, mProfileUserId);
+        installAppAsUser(NOTIFICATION_APK, mParentUserId);
+
+        // Profile owner in the profile adds listener to the whitelist
+        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".NotificationListenerTest",
+                "testAddListenerToWhitelist", mProfileUserId,
+                Collections.singletonMap(PARAM_PROFILE_ID, Integer.toString(mProfileUserId)));
+        // Listener outside the profile can see profile and personal notifications
+        runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".NotificationListenerTest",
+                "testCanReceiveNotifications", mParentUserId,
+                Collections.singletonMap(PARAM_PROFILE_ID, Integer.toString(mProfileUserId)));
+    }
+
     public void testCrossProfileCopyPaste() throws Exception {
         if (!mHasFeature) {
             return;