Add a CTS verifier for the new cross profile settings page

Bug: 136249261
Bug: 149743808
Test: manual testing
Change-Id: Ie388ab6efe1d5e3d6dc1bd2ccbc30cc2bbe7afec
diff --git a/apps/CrossProfileTestApp/Android.bp b/apps/CrossProfileTestApp/Android.bp
new file mode 100644
index 0000000..cbf092b
--- /dev/null
+++ b/apps/CrossProfileTestApp/Android.bp
@@ -0,0 +1,28 @@
+// Copyright (C) 2020 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.
+
+android_test_helper_app {
+    name: "CrossProfileTestApp",
+    defaults: ["cts_defaults"],
+    srcs: ["src/**/*.java"],
+    resource_dirs: ["res"],
+    sdk_version: "current",
+    min_sdk_version: "12",
+    // tag this module as a cts test artifact
+    test_suites: [
+        "cts",
+        "vts",
+        "general-tests",
+    ],
+}
diff --git a/apps/CrossProfileTestApp/AndroidManifest.xml b/apps/CrossProfileTestApp/AndroidManifest.xml
new file mode 100644
index 0000000..ab04e55
--- /dev/null
+++ b/apps/CrossProfileTestApp/AndroidManifest.xml
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2020 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.crossprofilepermissioncontrol" >
+
+    <application android:label="Cross Profile Test App">
+        <activity android:name=".TestActivity"
+                  android:exported="true">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+            <intent-filter>
+            <action android:name="com.android.cts.verifier.managedprovisioning.action.OPEN_CROSS_PROFILE_TEST_APP" />
+            <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity>
+    </application>
+    <uses-permission android:name="android.permission.INTERACT_ACROSS_PROFILES"/>
+</manifest>
diff --git a/apps/CrossProfileTestApp/OWNERS b/apps/CrossProfileTestApp/OWNERS
new file mode 100644
index 0000000..e889bfe
--- /dev/null
+++ b/apps/CrossProfileTestApp/OWNERS
@@ -0,0 +1,6 @@
+# Bug component: 149743808
+sandness@google.com
+arangelov@google.com
+alexkershaw@google.com
+scottjonathan@google.com
+kholoudm@google.com
diff --git a/apps/CrossProfileTestApp/res/layout/main.xml b/apps/CrossProfileTestApp/res/layout/main.xml
new file mode 100644
index 0000000..1b199ea
--- /dev/null
+++ b/apps/CrossProfileTestApp/res/layout/main.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2020 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.
+  -->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+              android:layout_width="match_parent"
+              android:layout_height="match_parent"
+              android:orientation="vertical"
+              android:gravity="center">
+    <Button
+        android:id="@+id/cross_profile_settings"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="@string/open_settings"/>
+
+    <TextView
+        android:id="@+id/cross_profile_app_text"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:textSize="30sp"
+        android:textAlignment="center"
+        android:gravity="center"/>
+</LinearLayout>
\ No newline at end of file
diff --git a/apps/CrossProfileTestApp/res/values/strings.xml b/apps/CrossProfileTestApp/res/values/strings.xml
new file mode 100644
index 0000000..93c0174
--- /dev/null
+++ b/apps/CrossProfileTestApp/res/values/strings.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2020 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.
+  -->
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+    <string name="open_settings">Open Settings</string>
+    <string name="cross_profile_enabled">INTERACTING ACROSS PROFILES ALLOWED</string>
+    <string name="cross_profile_disabled">INTERACTING ACROSS PROFILES NOT ALLOWED</string>
+
+</resources>
\ No newline at end of file
diff --git a/apps/CrossProfileTestApp/src/com/android/cts/crossprofilepermissioncontrol/TestActivity.java b/apps/CrossProfileTestApp/src/com/android/cts/crossprofilepermissioncontrol/TestActivity.java
new file mode 100644
index 0000000..e4364f1
--- /dev/null
+++ b/apps/CrossProfileTestApp/src/com/android/cts/crossprofilepermissioncontrol/TestActivity.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2020 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.crossprofilepermissioncontrol;
+
+import android.app.Activity;
+import android.app.AppOpsManager;
+import android.content.pm.CrossProfileApps;
+import android.os.Bundle;
+import android.os.Process;
+import android.view.View;
+import android.widget.Button;
+import android.widget.TextView;
+
+/**
+ * Test activity to check if the app can interact across profiles.
+ */
+public class TestActivity extends Activity {
+    private static final String INTERACT_ACROSS_PROFILES =
+            "android.permission.INTERACT_ACROSS_PROFILES";
+    private AppOpsManager mAppOpsManager;
+    private CrossProfileApps mCrossProfileApps;
+    private TextView mTextView;
+    private Button mSettingsButton;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.main);
+        mTextView = findViewById(R.id.cross_profile_app_text);
+        mSettingsButton = findViewById(R.id.cross_profile_settings);
+
+        mAppOpsManager = getSystemService(AppOpsManager.class);
+        mCrossProfileApps = getSystemService(CrossProfileApps.class);
+
+        mSettingsButton.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                startActivity(mCrossProfileApps.createRequestInteractAcrossProfilesIntent());
+            }
+        });
+    }
+
+    @Override
+    protected void onStart() {
+        super.onStart();
+        updateText();
+    }
+
+    private void updateText() {
+        final String op = AppOpsManager.permissionToOp(INTERACT_ACROSS_PROFILES);
+        if (AppOpsManager.MODE_ALLOWED == mAppOpsManager.checkOpNoThrow(
+                op, Process.myUid(), getPackageName())) {
+            mTextView.setText(R.string.cross_profile_enabled);
+        } else {
+            mTextView.setText(R.string.cross_profile_disabled);
+        }
+    }
+}
diff --git a/apps/CtsVerifier/Android.mk b/apps/CtsVerifier/Android.mk
index f5f0d5e..a7651b6 100644
--- a/apps/CtsVerifier/Android.mk
+++ b/apps/CtsVerifier/Android.mk
@@ -113,7 +113,8 @@
     CtsEmptyDeviceOwner \
     CtsPermissionApp \
     CtsForceStopHelper \
-    NotificationBot
+    NotificationBot \
+    CrossProfileTestApp
 
 # Apps to be installed as Instant App using adb install --instant
 pre-installed-instant-app := CtsVerifierInstantApp
diff --git a/apps/CtsVerifier/AndroidManifest.xml b/apps/CtsVerifier/AndroidManifest.xml
index 0703a59..0d5b5b1 100644
--- a/apps/CtsVerifier/AndroidManifest.xml
+++ b/apps/CtsVerifier/AndroidManifest.xml
@@ -2871,6 +2871,14 @@
                 android:label="@string/provisioning_byod_keyguard_disabled_features">
         </activity>
 
+        <activity android:name=".managedprovisioning.CrossProfilePermissionControlActivity"
+                  android:label="@string/provisioning_byod_cross_profile_permission_control">
+            <intent-filter>
+                <action android:name="com.android.cts.verifier.managedprovisioning.action.CROSS_PROFILE_PERMISSION_CONTROL" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity>
+
         <activity android:name=".managedprovisioning.DisallowAppsControlActivity"
                 android:label="@string/provisioning_byod_disallow_apps_control">
         </activity>
diff --git a/apps/CtsVerifier/res/values/strings.xml b/apps/CtsVerifier/res/values/strings.xml
index 4f9cc0a9..bbc663c 100755
--- a/apps/CtsVerifier/res/values/strings.xml
+++ b/apps/CtsVerifier/res/values/strings.xml
@@ -2775,6 +2775,44 @@
         \n
         Verify that you are prompted with the above choices and both options work as intended. Then mark this test accordingly.
     </string>
+    <string name="provisioning_byod_cross_profile_permission_control">Cross profile permission control</string>
+    <string name="provisioning_byod_cross_profile_permission_control_info">
+        This test exercises cross profile permission controls. Follow instructions above.
+    </string>
+    <string name="provisioning_byod_cross_profile_permission_control_instruction">
+        For this test you need to install CrossProfileTestApp.apk by running
+        adb install -r -t /path/to/CrossProfileTestApp.apk. Make sure the app is installed
+        on both the personal and work profiles.\n
+        Press the \"Prepare test\" button to enable configuring cross profile for the test app.\n
+        Then press through the following verification steps.\n
+        Mark the test as passed if all the following test cases are verified.
+    </string>
+    <string name="provisioning_byod_cross_profile_permission_control_prepare_button">Prepare test</string>
+    <string name="provisioning_byod_cross_profile_permission_disabled_by_default">Cross profile permission disabled by default</string>
+    <string name="provisioning_byod_cross_profile_permission_disabled_by_default_instruction">
+        Press the Go button to go to Settings &gt; Apps &amp; notifications &gt; Special app access >\n
+        Connected personal and work apps.\n
+        Choose the \"Cross profile test app\" from the list and check that the switch is disabled.\n
+        Go back to CTS Verifier and mark the test as \"Pass\" or \"Fail\".
+    </string>
+    <string name="provisioning_byod_cross_profile_permission_enabled">Cross profile permission enabled</string>
+    <string name="provisioning_byod_cross_profile_permission_enabled_instruction">
+        Press the Go button to open the \"Cross Profile Test App\".\n
+        Press the \"Open Settings\" button to go to Settings &gt; Apps &amp; notifications &gt; Special app access >
+        Connected personal and work apps > Cross profile test app, then enable the switch.\n
+        Verify that when you go back it takes you to the \"Cross Profile Test App\".\n
+        Now verify that it shows \"INTERACTING ACROSS PROFILES ALLOWED\" message.\n
+        Go back to CTS Verifier and mark the test as \"Pass\" or \"Fail\".
+    </string>
+    <string name="provisioning_byod_cross_profile_permission_disabled">Cross profile permission disabled</string>
+    <string name="provisioning_byod_cross_profile_permission_disabled_instruction">
+        Press the Go button to open the \"Cross Profile Test App\".\n
+        Press the \"Open Settings\" button to go to Settings &gt; Apps &amp; notifications &gt; Special app access >
+        Connected personal and work apps > Cross profile test app, then disable the switch.\n
+        Verify that when you go back it takes you to the \"Cross Profile Test App\".\n
+        Now verify that it shows \"INTERACTING ACROSS PROFILES NOT ALLOWED\" message.\n
+        Go back to CTS Verifier and mark the test as \"Pass\" or \"Fail\".
+    </string>
     <string name="provisioning_byod_app_linking_instruction">
         Please press the Go button to start an action.\n
         \n
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodFlowTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodFlowTestActivity.java
index b13e3aa..656cd89 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodFlowTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodFlowTestActivity.java
@@ -23,7 +23,6 @@
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
-import android.content.IntentFilter;
 import android.content.SharedPreferences;
 import android.content.pm.PackageManager;
 import android.net.ConnectivityManager;
@@ -90,6 +89,7 @@
     private DialogTestListItem mWorkAppVisibleTest;
     private DialogTestListItem mCrossProfileIntentFiltersTestFromPersonal;
     private DialogTestListItem mCrossProfileIntentFiltersTestFromWork;
+    private TestListItem mCrossProfilePermissionControl;
     private DialogTestListItem mAppLinkingTest;
     private TestListItem mNonMarketAppsTest;
     private DialogTestListItem mWorkNotificationBadgedTest;
@@ -451,6 +451,13 @@
             }
         };
 
+        mCrossProfilePermissionControl = TestListItem.newTest(this,
+                R.string.provisioning_byod_cross_profile_permission_control,
+                CrossProfilePermissionControlActivity.class.getName(),
+                new Intent(
+                        CrossProfilePermissionControlActivity.ACTION_CROSS_PROFILE_PERMISSION_CONTROL),
+                        null);
+
         mTurnOffWorkFeaturesTest = TestListItem.newTest(this,
                 R.string.provisioning_byod_turn_off_work,
                 TurnOffWorkActivity.class.getName(),
@@ -544,6 +551,7 @@
         adapter.add(mAppLinkingTest);
         */
         adapter.add(mIntentFiltersTest);
+        adapter.add(mCrossProfilePermissionControl);
         adapter.add(mNonMarketAppsTest);
         adapter.add(mPermissionLockdownTest);
         adapter.add(mKeyguardDisabledFeaturesTest);
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodFlowTestHelper.java b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodFlowTestHelper.java
index 24e0919..2bb1046 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodFlowTestHelper.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodFlowTestHelper.java
@@ -58,6 +58,7 @@
                 AuthenticationBoundKeyTestActivity.class.getName(),
                 VpnTestActivity.class.getName(),
                 AlwaysOnVpnSettingsTestActivity.class.getName(),
+                CrossProfilePermissionControlActivity.class.getName(),
                 IntermediateRecentActivity.class.getName(),
                 CommandReceiverActivity.class.getName(),
                 SetSupportMessageActivity.class.getName(),
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/CrossProfilePermissionControlActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/CrossProfilePermissionControlActivity.java
new file mode 100644
index 0000000..ef7dd67
--- /dev/null
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/CrossProfilePermissionControlActivity.java
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2020 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.verifier.managedprovisioning;
+
+import android.app.admin.DevicePolicyManager;
+import android.content.ComponentName;
+import android.content.Intent;
+import android.os.Bundle;
+import android.provider.Settings;
+import android.view.View;
+import android.view.View.OnClickListener;
+
+import com.android.cts.verifier.ArrayTestListAdapter;
+import com.android.cts.verifier.DialogTestListActivity;
+import com.android.cts.verifier.R;
+
+import java.util.Set;
+
+public class CrossProfilePermissionControlActivity extends DialogTestListActivity {
+    static final String ACTION_CROSS_PROFILE_PERMISSION_CONTROL =
+            "com.android.cts.verifier.managedprovisioning.action.CROSS_PROFILE_PERMISSION_CONTROL";
+    private static final String TEST_APP_PACKAGE_NAME =
+            "com.android.cts.crossprofilepermissioncontrol";
+    private static final String OPEN_TEST_APP_ACTION =
+            "com.android.cts.verifier.managedprovisioning.action.OPEN_CROSS_PROFILE_TEST_APP";
+
+    protected DevicePolicyManager mDpm;
+
+    public CrossProfilePermissionControlActivity() {
+        super(R.layout.provisioning_byod,
+                R.string.provisioning_byod_cross_profile_permission_control,
+                R.string.provisioning_byod_cross_profile_permission_control_info,
+                R.string.provisioning_byod_cross_profile_permission_control_instruction);
+    }
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        mDpm = getSystemService(DevicePolicyManager.class);
+        mPrepareTestButton.setText(
+                R.string.provisioning_byod_cross_profile_permission_control_prepare_button);
+        mPrepareTestButton.setOnClickListener(new OnClickListener() {
+                @Override
+                public void onClick(View v) {
+                    whitelistTestApp();
+                }
+            });
+    }
+
+    protected void whitelistTestApp() {
+        mDpm.setCrossProfilePackages(getAdminComponent(), Set.of(TEST_APP_PACKAGE_NAME));
+    }
+
+    protected ComponentName getAdminComponent() {
+        return DeviceAdminTestReceiver.getReceiverComponentName();
+    }
+
+    protected void setupInteractAcrossProfilesDisabledByDefault(ArrayTestListAdapter adapter) {
+        adapter.add(new DialogTestListItem(
+                this, R.string.provisioning_byod_cross_profile_permission_disabled_by_default,
+                getTestIdPrefix() + "interactAcrossProfilesDisabledByDefault",
+                R.string.provisioning_byod_cross_profile_permission_disabled_by_default_instruction,
+                new Intent(Settings.ACTION_MANAGE_CROSS_PROFILE_ACCESS)));
+    }
+
+    protected void setupInteractAcrossProfilesEnabled(ArrayTestListAdapter adapter) {
+        adapter.add(new DialogTestListItem(
+                this, R.string.provisioning_byod_cross_profile_permission_enabled,
+                getTestIdPrefix() + "interactAcrossProfilesEnabled",
+                R.string.provisioning_byod_cross_profile_permission_enabled_instruction,
+                new Intent(OPEN_TEST_APP_ACTION)));
+    }
+
+    protected void setupInteractAcrossProfilesDisabled(ArrayTestListAdapter adapter) {
+        adapter.add(new DialogTestListItem(this,
+                R.string.provisioning_byod_cross_profile_permission_disabled,
+                getTestIdPrefix() + "DisableUnredactedNotifications",
+                R.string.provisioning_byod_cross_profile_permission_disabled_instruction,
+                new Intent(OPEN_TEST_APP_ACTION)));
+    }
+
+    protected String getTestIdPrefix() {
+        return "BYOD_";
+    }
+
+    @Override
+    protected void setupTests(ArrayTestListAdapter adapter) {
+        setupInteractAcrossProfilesDisabledByDefault(adapter);
+        setupInteractAcrossProfilesEnabled(adapter);
+        setupInteractAcrossProfilesDisabled(adapter);
+    }
+}
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/DeviceAdminTestReceiver.java b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/DeviceAdminTestReceiver.java
index 11351ec..c9e2449 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/DeviceAdminTestReceiver.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/DeviceAdminTestReceiver.java
@@ -181,6 +181,8 @@
         filter.addAction(KeyChainTestActivity.ACTION_KEYCHAIN);
         filter.addAction(CommandReceiverActivity.ACTION_EXECUTE_COMMAND);
         filter.addAction(WorkProfileWidgetActivity.ACTION_TEST_WORK_PROFILE_WIDGET);
+        filter.addAction(
+                CrossProfilePermissionControlActivity.ACTION_CROSS_PROFILE_PERMISSION_CONTROL);
         dpm.addCrossProfileIntentFilter(getWho(context), filter,
                 DevicePolicyManager.FLAG_MANAGED_CAN_ACCESS_PARENT);
 
@@ -192,6 +194,7 @@
         filter.addAction(ByodFlowTestActivity.ACTION_TEST_RESULT);
         filter.addAction(CrossProfileTestActivity.ACTION_CROSS_PROFILE_TO_PERSONAL);
         filter.addAction(LocationListenerActivity.ACTION_SET_LOCATION_AND_CHECK_UPDATES);
+
         dpm.addCrossProfileIntentFilter(getWho(context), filter,
                 DevicePolicyManager.FLAG_PARENT_CAN_ACCESS_MANAGED);