CTS for the READ_CLIPBOARD_IN_BACKGROUND permission
Update PermissionPolicyTest to match the updated permission config.
Add a test to verify that reading from the clipboard in the background
requires the READ_CLIPBOARD_IN_BACKGROUND permission.
Bug: 216314180
Test: atest CtsPermission2TestCases:PermissionPolicyTest
Test: atest CtsContentTestCases:ClipboardManagerTest
Test: atest IntentResolverUnitTests:UnbundledChooserActivityTest
Test: atest TextClassifierNotificationTests:CopyCodeActivityTest
Change-Id: I22b89a90dcd238c86ba68482e707a27bedd99399
diff --git a/tests/tests/content/src/android/content/cts/ClipboardManagerTest.java b/tests/tests/content/src/android/content/cts/ClipboardManagerTest.java
index c139b25..10df304 100644
--- a/tests/tests/content/src/android/content/cts/ClipboardManagerTest.java
+++ b/tests/tests/content/src/android/content/cts/ClipboardManagerTest.java
@@ -44,6 +44,8 @@
import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
+import com.android.compatibility.common.util.SystemUtil;
+
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -70,6 +72,7 @@
@After
public void cleanUp() {
+ mClipboardManager.clearPrimaryClip();
InstrumentationRegistry.getInstrumentation().getUiAutomation()
.dropShellPermissionIdentity();
}
@@ -293,6 +296,32 @@
}
@Test
+ public void testReadInBackgroundRequiresPermission() throws Exception {
+ ClipData clip = ClipData.newPlainText("TextLabel", "Text1");
+ mClipboardManager.setPrimaryClip(clip);
+
+ // Press the home button to unfocus the app.
+ mUiDevice.pressHome();
+ mUiDevice.wait(Until.gone(By.pkg(MockActivity.class.getPackageName())), 5000);
+
+ // Without the READ_CLIPBOARD_IN_BACKGROUND permission, we should see an empty clipboard.
+ assertThat(mClipboardManager.hasPrimaryClip()).isFalse();
+ assertThat(mClipboardManager.hasText()).isFalse();
+ assertThat(mClipboardManager.getPrimaryClip()).isNull();
+ assertThat(mClipboardManager.getPrimaryClipDescription()).isNull();
+
+ // Having the READ_CLIPBOARD_IN_BACKGROUND permission should allow us to read the clipboard
+ // even when we are not in the foreground. We use the shell identity to simulate holding
+ // this permission; in practice, only privileged system apps can hold this permission (e.g.
+ // an app that has the SYSTEM_TEXT_INTELLIGENCE role).
+ ClipData actual = SystemUtil.callWithShellPermissionIdentity(
+ () -> mClipboardManager.getPrimaryClip(),
+ android.Manifest.permission.READ_CLIPBOARD_IN_BACKGROUND);
+ assertThat(actual).isNotNull();
+ assertThat(actual.getItemAt(0).getText()).isEqualTo("Text1");
+ }
+
+ @Test
public void testClipSourceRecordedWhenClipSet() {
ClipData clipData = ClipData.newPlainText("TextLabel", "Text1");
mClipboardManager.setPrimaryClip(clipData);
diff --git a/tests/tests/permission2/res/raw/android_manifest.xml b/tests/tests/permission2/res/raw/android_manifest.xml
index f849e22..013f7fb 100644
--- a/tests/tests/permission2/res/raw/android_manifest.xml
+++ b/tests/tests/permission2/res/raw/android_manifest.xml
@@ -6045,10 +6045,10 @@
<permission android:name="android.permission.MANAGE_APPOPS"
android:protectionLevel="signature" />
- <!-- @hide Permission that allows background clipboard access.
- <p>Not for use by third-party applications. -->
+ <!-- @SystemApi Permission that allows background clipboard access.
+ @hide Not for use by third-party applications. -->
<permission android:name="android.permission.READ_CLIPBOARD_IN_BACKGROUND"
- android:protectionLevel="signature" />
+ android:protectionLevel="signature|role" />
<!-- @hide Permission that suppresses the notification when the clipboard is accessed.
<p>Not for use by third-party applications. -->