Add CTS case for compat framework gate of restrict SAF

Test: atest DocumentsTest
Bug: 144816835
Change-Id: I7cf6629246a07a428d92b935f5103749d24b61ab
diff --git a/hostsidetests/appsecurity/Android.mk b/hostsidetests/appsecurity/Android.mk
index ee149f0..55d7720 100644
--- a/hostsidetests/appsecurity/Android.mk
+++ b/hostsidetests/appsecurity/Android.mk
@@ -28,6 +28,9 @@
 	truth-prebuilt \
 	hamcrest-library
 
+LOCAL_STATIC_JAVA_LIBRARIES := \
+	CompatChangeGatingTestBase
+
 LOCAL_JAVA_RESOURCE_DIRS := res
 
 LOCAL_CTS_TEST_PACKAGE := android.appsecurity
diff --git a/hostsidetests/appsecurity/src/android/appsecurity/cts/DocumentsTest.java b/hostsidetests/appsecurity/src/android/appsecurity/cts/DocumentsTest.java
index 1465bc3..00ddbb0 100644
--- a/hostsidetests/appsecurity/src/android/appsecurity/cts/DocumentsTest.java
+++ b/hostsidetests/appsecurity/src/android/appsecurity/cts/DocumentsTest.java
@@ -18,6 +18,8 @@
 
 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
 
+import com.google.common.collect.ImmutableSet;
+
 /**
  * Set of tests that verify behavior of
  * {@link android.provider.DocumentsContract} and related intents.
@@ -27,6 +29,8 @@
     private static final String PROVIDER_APK = "CtsDocumentProvider.apk";
     private static final String DUMMYIME_APK = "CtsDummyIme.apk";
 
+    private static final long RESTRICT_STORAGE_ACCESS_FRAMEWORK = 141600225L;
+
     @Override
     protected void setUp() throws Exception {
         super.setUp();
@@ -65,10 +69,6 @@
         runDeviceTests(CLIENT_PKG, ".DocumentsClientTest", "testTree");
     }
 
-    public void testTree_blockFromTree() throws Exception {
-        runDeviceTests(CLIENT_PKG, ".DocumentsClientTest", "testTree_blockFromTree");
-    }
-
     public void testGetContent_rootsShowing() throws Exception {
         runDeviceTests(CLIENT_PKG, ".DocumentsClientTest", "testGetContent_rootsShowing");
     }
@@ -119,4 +119,18 @@
     public void testEject() throws Exception {
         runDeviceTests(CLIENT_PKG, ".DocumentsClientTest", "testEject");
     }
+
+    public void testRestrictStorageAccessFrameworkEnabled_blockFromTree() throws Exception {
+        runDeviceCompatTest(CLIENT_PKG, ".DocumentsClientTest",
+                "testRestrictStorageAccessFrameworkEnabled_blockFromTree",
+                /* enabledChanges */ ImmutableSet.of(RESTRICT_STORAGE_ACCESS_FRAMEWORK),
+                /* disabledChanges */ ImmutableSet.of());
+    }
+
+    public void testRestrictStorageAccessFrameworkDisabled_notBlockFromTree() throws Exception {
+        runDeviceCompatTest(CLIENT_PKG, ".DocumentsClientTest",
+                "testRestrictStorageAccessFrameworkDisabled_notBlockFromTree",
+                /* enabledChanges */ ImmutableSet.of(),
+                /* disabledChanges */ ImmutableSet.of(RESTRICT_STORAGE_ACCESS_FRAMEWORK));
+    }
 }
diff --git a/hostsidetests/appsecurity/src/android/appsecurity/cts/DocumentsTestCase.java b/hostsidetests/appsecurity/src/android/appsecurity/cts/DocumentsTestCase.java
index 1af0650..d7a9ffc 100644
--- a/hostsidetests/appsecurity/src/android/appsecurity/cts/DocumentsTestCase.java
+++ b/hostsidetests/appsecurity/src/android/appsecurity/cts/DocumentsTestCase.java
@@ -16,23 +16,21 @@
 
 package android.appsecurity.cts;
 
+import android.compat.cts.CompatChangeGatingTestCase;
+
 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
-import com.android.tradefed.build.IBuildInfo;
 import com.android.tradefed.device.DeviceNotAvailableException;
-import com.android.tradefed.testtype.DeviceTestCase;
 import com.android.tradefed.testtype.IAbi;
 import com.android.tradefed.testtype.IAbiReceiver;
-import com.android.tradefed.testtype.IBuildReceiver;
 
 /**
  * Base class for {@link android.provider.DocumentsContract} and related test cases.
  */
-abstract class DocumentsTestCase extends DeviceTestCase implements IAbiReceiver, IBuildReceiver {
+abstract class DocumentsTestCase extends CompatChangeGatingTestCase implements IAbiReceiver {
     protected static final String CLIENT_PKG = "com.android.cts.documentclient";
     protected static final String CLIENT_APK = "CtsDocumentClient.apk";
 
     protected IAbi mAbi;
-    protected IBuildInfo mCtsBuild;
 
     @Override
     public void setAbi(IAbi abi) {
@@ -40,17 +38,11 @@
     }
 
     @Override
-    public void setBuild(IBuildInfo buildInfo) {
-        mCtsBuild = buildInfo;
-    }
-
-    @Override
     protected void setUp() throws Exception {
         super.setUp();
 
         Utils.prepareSingleUser(getDevice());
         assertNotNull(mAbi);
-        assertNotNull(mCtsBuild);
 
         reinstallClientPackage();
     }
diff --git a/hostsidetests/appsecurity/test-apps/DocumentClient/AndroidManifest.xml b/hostsidetests/appsecurity/test-apps/DocumentClient/AndroidManifest.xml
index 79eb44d..e87cd6f 100644
--- a/hostsidetests/appsecurity/test-apps/DocumentClient/AndroidManifest.xml
+++ b/hostsidetests/appsecurity/test-apps/DocumentClient/AndroidManifest.xml
@@ -16,7 +16,9 @@
 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
         package="com.android.cts.documentclient">
-    <application android:forceQueryable="true">
+    <uses-sdk android:minSdkVersion="29" android:targetSdkVersion="29"/>
+
+    <application android:debuggable="true" android:forceQueryable="true">
         <uses-library android:name="android.test.runner" />
         <activity android:name=".MyActivity" />
     </application>
diff --git a/hostsidetests/appsecurity/test-apps/DocumentClient/src/com/android/cts/documentclient/DocumentsClientTest.java b/hostsidetests/appsecurity/test-apps/DocumentClient/src/com/android/cts/documentclient/DocumentsClientTest.java
index 1f302dd..40d8ff4 100644
--- a/hostsidetests/appsecurity/test-apps/DocumentClient/src/com/android/cts/documentclient/DocumentsClientTest.java
+++ b/hostsidetests/appsecurity/test-apps/DocumentClient/src/com/android/cts/documentclient/DocumentsClientTest.java
@@ -357,19 +357,58 @@
         }
     }
 
-    public void testTree_blockFromTree() throws Exception {
+    public void testRestrictStorageAccessFrameworkEnabled_blockFromTree() throws Exception {
         if (!supportedHardware()) return;
 
         final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
         mActivity.startActivityForResult(intent, REQUEST_CODE);
 
         mDevice.waitForIdle();
+
+        // save button is disabled for the storage root
+        assertFalse(findSaveButton().isEnabled());
+
+        findDocument("Download").click();
+        mDevice.waitForIdle();
+
+        // save button is disabled for Download folder
+        assertFalse(findSaveButton().isEnabled());
+
         findRoot("CtsCreate").click();
+        mDevice.waitForIdle();
+
+        // save button is disabled for CtsCreate root
+        assertFalse(findSaveButton().isEnabled());
+
+        findDocument("DIR2").click();
+
+        mDevice.waitForIdle();
+        // save button is enabled for dir2
+        assertTrue(findSaveButton().isEnabled());
+    }
+
+    public void testRestrictStorageAccessFrameworkDisabled_notBlockFromTree() throws Exception {
+        if (!supportedHardware()) return;
+
+        final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
+        mActivity.startActivityForResult(intent, REQUEST_CODE);
 
         mDevice.waitForIdle();
 
-        // save button is disabled for root
-        assertFalse(findSaveButton().isEnabled());
+        // save button is enabled for for the storage root
+        assertTrue(findSaveButton().isEnabled());
+
+        findDocument("Download").click();
+        mDevice.waitForIdle();
+
+        // save button is enabled for Download folder
+        assertTrue(findSaveButton().isEnabled());
+
+        findRoot("CtsCreate").click();
+        mDevice.waitForIdle();
+
+        // save button is enabled for CtsCreate root
+        assertTrue(findSaveButton().isEnabled());
 
         findDocument("DIR2").click();
 
@@ -661,14 +700,6 @@
         // assert the default root is internal storage root
         assertToolbarTitleEquals(title);
 
-        // save button is disabled for the root
-        assertFalse(findSaveButton().isEnabled());
-
-        findDocument("Download").click();
-        mDevice.waitForIdle();
-        // save button is disabled for Download folder
-        assertFalse(findSaveButton().isEnabled());
-
         // no Downloads root
         assertFalse(findRoot("Downloads").exists());
     }