Refactor ScopedStorageTest to run on any volume

Add mechanism to run ScopedStorageTest test cases on different volumes,
and not necessarily the main volume.

Also add a TEST_MAPPING file.

Test: atest CtsScopedStorageHostTest
Bug: 148841336
Change-Id: I8e2eb0f14e10dbe5dcf4d5e12d9ac4f44ed1c2ee
diff --git a/hostsidetests/scopedstorage/TEST_MAPPING b/hostsidetests/scopedstorage/TEST_MAPPING
new file mode 100644
index 0000000..8f4fbd1
--- /dev/null
+++ b/hostsidetests/scopedstorage/TEST_MAPPING
@@ -0,0 +1,7 @@
+{
+  "presubmit": [
+    {
+      "name": "CtsScopedStorageHostTest"
+    }
+  ]
+}
diff --git a/hostsidetests/scopedstorage/legacy/src/android/scopedstorage/cts/legacy/LegacyStorageTest.java b/hostsidetests/scopedstorage/legacy/src/android/scopedstorage/cts/legacy/LegacyStorageTest.java
index fedd206..3262766 100644
--- a/hostsidetests/scopedstorage/legacy/src/android/scopedstorage/cts/legacy/LegacyStorageTest.java
+++ b/hostsidetests/scopedstorage/legacy/src/android/scopedstorage/cts/legacy/LegacyStorageTest.java
@@ -18,9 +18,6 @@
 
 import static android.scopedstorage.cts.lib.TestUtils.BYTES_DATA1;
 import static android.scopedstorage.cts.lib.TestUtils.BYTES_DATA2;
-import static android.scopedstorage.cts.lib.TestUtils.DCIM_DIR;
-import static android.scopedstorage.cts.lib.TestUtils.EXTERNAL_STORAGE_DIR;
-import static android.scopedstorage.cts.lib.TestUtils.MOVIES_DIR;
 import static android.scopedstorage.cts.lib.TestUtils.STR_DATA1;
 import static android.scopedstorage.cts.lib.TestUtils.STR_DATA2;
 import static android.scopedstorage.cts.lib.TestUtils.assertCanRenameDirectory;
@@ -92,9 +89,9 @@
     private static final String TAG = "LegacyFileAccessTest";
     static final String THIS_PACKAGE_NAME = InstrumentationRegistry.getContext().getPackageName();
 
-    static final String IMAGE_FILE_NAME = "FilePathAccessTest_file.jpg";
-    static final String VIDEO_FILE_NAME = "LegacyAccessTest_file.mp4";
-    static final String NONMEDIA_FILE_NAME = "LegacyAccessTest_file.pdf";
+    static final String IMAGE_FILE_NAME = "LegacyStorageTest_file.jpg";
+    static final String VIDEO_FILE_NAME = "LegacyStorageTest_file.mp4";
+    static final String NONMEDIA_FILE_NAME = "LegacyStorageTest_file.pdf";
 
     private static final TestApp TEST_APP_A = new TestApp("TestAppA",
             "android.scopedstorage.cts.testapp.A", 1, false, "CtsScopedStorageTestAppA.apk");
@@ -121,17 +118,17 @@
         pollForPermission(Manifest.permission.READ_EXTERNAL_STORAGE, /*granted*/ false);
         pollForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, /*granted*/ true);
         // Can create file under root dir
-        assertCanCreateFile(new File(EXTERNAL_STORAGE_DIR, "LegacyFileAccessTest.txt"));
+        assertCanCreateFile(new File(TestUtils.getExternalStorageDir(), "LegacyFileAccessTest.txt"));
 
         // Can create music file under DCIM
-        assertCanCreateFile(new File(TestUtils.DCIM_DIR, "LegacyFileAccessTest.mp3"));
+        assertCanCreateFile(new File(TestUtils.getDcimDir(), "LegacyFileAccessTest.mp3"));
 
         // Can create random file under external files dir
-        assertCanCreateFile(new File(InstrumentationRegistry.getContext().getExternalFilesDir(null),
+        assertCanCreateFile(new File(TestUtils.getExternalFilesDir(),
                 "LegacyFileAccessTest"));
 
         // However, even legacy apps can't create files under other app's directories
-        final File otherAppDir = new File(TestUtils.ANDROID_DATA_DIR, "com.android.shell");
+        final File otherAppDir = new File(TestUtils.getAndroidDataDir(), "com.android.shell");
         final File file = new File(otherAppDir, "LegacyFileAccessTest.txt");
 
         // otherAppDir was already created by the host test
@@ -151,10 +148,10 @@
         pollForPermission(Manifest.permission.READ_EXTERNAL_STORAGE, /*granted*/ false);
         pollForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, /*granted*/ true);
         // Can create a top-level direcotry
-        final File topLevelDir = new File(EXTERNAL_STORAGE_DIR, "LegacyFileAccessTest");
+        final File topLevelDir = new File(TestUtils.getExternalStorageDir(), "LegacyFileAccessTest");
         assertCanCreateDir(topLevelDir);
 
-        final File otherAppDir = new File(TestUtils.ANDROID_DATA_DIR, "com.android.shell");
+        final File otherAppDir = new File(TestUtils.getAndroidDataDir(), "com.android.shell");
 
         // However, even legacy apps can't create dirs under other app's directories
         final File subDir = new File(otherAppDir, "LegacyFileAccessTest");
@@ -162,7 +159,7 @@
         assertThat(subDir.mkdir()).isFalse();
 
         // Try to list a directory and fail because it requires READ permission
-        assertThat(TestUtils.MUSIC_DIR.list()).isNull();
+        assertThat(TestUtils.getMusicDir().list()).isNull();
     }
 
     /**
@@ -173,7 +170,8 @@
         pollForPermission(Manifest.permission.READ_EXTERNAL_STORAGE, /*granted*/ false);
         pollForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, /*granted*/ false);
         // Can't create file under root dir
-        final File newTxtFile = new File(EXTERNAL_STORAGE_DIR, "LegacyFileAccessTest.txt");
+        final File newTxtFile = new File(TestUtils.getExternalStorageDir(),
+                "LegacyFileAccessTest.txt");
         try {
             newTxtFile.createNewFile();
             fail("File creation expected to fail: " + newTxtFile);
@@ -181,7 +179,7 @@
         }
 
         // Can't create music file under /MUSIC
-        final File newMusicFile = new File(TestUtils.MUSIC_DIR, "LegacyFileAccessTest.mp3");
+        final File newMusicFile = new File(TestUtils.getMusicDir(), "LegacyFileAccessTest.mp3");
         try {
             newMusicFile.createNewFile();
             fail("File creation expected to fail: " + newMusicFile);
@@ -189,11 +187,12 @@
         }
 
         // Can't create a top-level direcotry
-        final File topLevelDir = new File(EXTERNAL_STORAGE_DIR, "LegacyFileAccessTest");
+        final File topLevelDir = new File(TestUtils.getExternalStorageDir(), "LegacyFileAccessTest");
         assertThat(topLevelDir.mkdir()).isFalse();
 
         // Can't read existing file
-        final File existingFile = new File(EXTERNAL_STORAGE_DIR, "LegacyAccessHostTest_shell");
+        final File existingFile = new File(TestUtils.getExternalStorageDir(),
+                "LegacyAccessHostTest_shell");
         try {
             Os.open(existingFile.getPath(), OsConstants.O_RDONLY, /*mode*/ 0);
             fail("Opening file for read expected to fail: " + existingFile);
@@ -204,29 +203,29 @@
         assertThat(existingFile.delete()).isFalse();
 
         // try to list a directory and fail
-        assertThat(TestUtils.MUSIC_DIR.list()).isNull();
-        assertThat(EXTERNAL_STORAGE_DIR.list()).isNull();
+        assertThat(TestUtils.getMusicDir().list()).isNull();
+        assertThat(TestUtils.getExternalStorageDir().list()).isNull();
 
         // However, even without permissions, we can access our own external dir
         final File fileInDataDir =
-                new File(InstrumentationRegistry.getContext().getExternalFilesDir(null),
+                new File(TestUtils.getExternalFilesDir(),
                         "LegacyFileAccessTest");
         try {
             assertThat(fileInDataDir.createNewFile()).isTrue();
             assertThat(Arrays.asList(fileInDataDir.getParentFile().list()))
-                    .containsExactly("LegacyFileAccessTest");
+                    .contains("LegacyFileAccessTest");
         } finally {
             fileInDataDir.delete();
         }
 
         // we can access our own external media directory without permissions.
         final File fileInMediaDir =
-                new File(InstrumentationRegistry.getContext().getExternalMediaDirs()[0],
+                new File(TestUtils.getExternalMediaDir(),
                         "LegacyFileAccessTest");
         try {
             assertThat(fileInMediaDir.createNewFile()).isTrue();
             assertThat(Arrays.asList(fileInMediaDir.getParentFile().list()))
-                    .containsExactly("LegacyFileAccessTest");
+                    .contains("LegacyFileAccessTest");
         } finally {
             fileInMediaDir.delete();
         }
@@ -238,10 +237,11 @@
         pollForPermission(Manifest.permission.READ_EXTERNAL_STORAGE, /*granted*/ true);
         pollForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, /*granted*/ false);
         // can list directory content
-        assertThat(TestUtils.MUSIC_DIR.list()).isNotNull();
+        assertThat(TestUtils.getMusicDir().list()).isNotNull();
 
         // try to write a file and fail
-        final File existingFile = new File(EXTERNAL_STORAGE_DIR, "LegacyAccessHostTest_shell");
+        final File existingFile = new File(TestUtils.getExternalStorageDir(),
+                "LegacyAccessHostTest_shell");
 
         // can open file for read
         FileDescriptor fd = null;
@@ -261,7 +261,7 @@
         }
 
         // try to create file and fail, because it requires WRITE
-        final File newFile = new File(TestUtils.MUSIC_DIR, "LegacyFileAccessTest.mp3");
+        final File newFile = new File(TestUtils.getMusicDir(), "LegacyFileAccessTest.mp3");
         try {
             newFile.createNewFile();
             fail("Creating file expected to fail: " + newFile);
@@ -269,7 +269,7 @@
         }
 
         // try to mkdir and fail, because it requires WRITE
-        final File newDir = new File(EXTERNAL_STORAGE_DIR, "LegacyFileAccessTest");
+        final File newDir = new File(TestUtils.getExternalStorageDir(), "LegacyFileAccessTest");
         try {
             assertThat(newDir.mkdir()).isFalse();
         } finally {
@@ -286,7 +286,7 @@
         pollForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, /*granted*/ false);
 
         // can list a non-media file created by other package.
-        assertThat(Arrays.asList(EXTERNAL_STORAGE_DIR.list()))
+        assertThat(Arrays.asList(TestUtils.getExternalStorageDir().list()))
                 .contains("LegacyAccessHostTest_shell");
     }
 
@@ -299,11 +299,13 @@
         pollForPermission(Manifest.permission.READ_EXTERNAL_STORAGE, /*granted*/ true);
         pollForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, /*granted*/ true);
 
-        final File musicFile1 = new File(TestUtils.DCIM_DIR, "LegacyFileAccessTest.mp3");
-        final File musicFile2 = new File(EXTERNAL_STORAGE_DIR, "LegacyFileAccessTest.mp3");
-        final File musicFile3 = new File(TestUtils.MOVIES_DIR, "LegacyFileAccessTest.mp3");
-        final File nonMediaDir1 = new File(TestUtils.DCIM_DIR, "LegacyFileAccessTest");
-        final File nonMediaDir2 = new File(EXTERNAL_STORAGE_DIR, "LegacyFileAccessTest");
+        final File musicFile1 = new File(TestUtils.getDcimDir(), "LegacyFileAccessTest.mp3");
+        final File musicFile2 = new File(TestUtils.getExternalStorageDir(),
+                "LegacyFileAccessTest.mp3");
+        final File musicFile3 = new File(TestUtils.getMoviesDir(), "LegacyFileAccessTest.mp3");
+        final File nonMediaDir1 = new File(TestUtils.getDcimDir(), "LegacyFileAccessTest");
+        final File nonMediaDir2 = new File(TestUtils.getExternalStorageDir(),
+                "LegacyFileAccessTest");
         final File pdfFile1 = new File(nonMediaDir1, "LegacyFileAccessTest.pdf");
         final File pdfFile2 = new File(nonMediaDir2, "LegacyFileAccessTest.pdf");
         try {
@@ -340,13 +342,14 @@
         pollForPermission(Manifest.permission.READ_EXTERNAL_STORAGE, /*granted*/ true);
         pollForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, /*granted*/ false);
 
-        final File shellFile1 = new File(EXTERNAL_STORAGE_DIR, "LegacyAccessHostTest_shell");
-        final File shellFile2 = new File(TestUtils.DOWNLOAD_DIR, "LegacyFileAccessTest_shell");
+        final File shellFile1 = new File(TestUtils.getExternalStorageDir(),
+                "LegacyAccessHostTest_shell");
+        final File shellFile2 = new File(TestUtils.getDownloadDir(), "LegacyFileAccessTest_shell");
         final File mediaFile1 =
-                new File(InstrumentationRegistry.getContext().getExternalMediaDirs()[0],
+                new File(TestUtils.getExternalMediaDir(),
                         "LegacyFileAccessTest1");
         final File mediaFile2 =
-                new File(InstrumentationRegistry.getContext().getExternalMediaDirs()[0],
+                new File(TestUtils.getExternalMediaDir(),
                         "LegacyFileAccessTest2");
         try {
             // app can't rename shell file.
@@ -373,13 +376,14 @@
         pollForPermission(Manifest.permission.READ_EXTERNAL_STORAGE, /*granted*/ false);
         pollForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, /*granted*/ false);
 
-        final File shellFile1 = new File(EXTERNAL_STORAGE_DIR, "LegacyAccessHostTest_shell");
-        final File shellFile2 = new File(TestUtils.DOWNLOAD_DIR, "LegacyFileAccessTest_shell");
+        final File shellFile1 = new File(TestUtils.getExternalStorageDir(),
+                "LegacyAccessHostTest_shell");
+        final File shellFile2 = new File(TestUtils.getDownloadDir(), "LegacyFileAccessTest_shell");
         final File mediaFile1 =
-                new File(InstrumentationRegistry.getContext().getExternalMediaDirs()[0],
+                new File(TestUtils.getExternalMediaDir(),
                         "LegacyFileAccessTest1");
         final File mediaFile2 =
-                new File(InstrumentationRegistry.getContext().getExternalMediaDirs()[0],
+                new File(TestUtils.getExternalMediaDir(),
                         "LegacyFileAccessTest2");
         try {
             // app can't rename shell file.
@@ -404,8 +408,8 @@
     @Test
     public void testRenameDirectoryAndUpdateDB_hasW() throws Exception {
         final String testDirectoryName = "LegacyFileAccessTestDirectory";
-        File directoryOldPath = new File(DCIM_DIR, testDirectoryName);
-        File directoryNewPath = new File(MOVIES_DIR, testDirectoryName);
+        File directoryOldPath = new File(TestUtils.getDcimDir(), testDirectoryName);
+        File directoryNewPath = new File(TestUtils.getMoviesDir(), testDirectoryName);
         try {
             if (directoryOldPath.exists()) {
                 executeShellCommand("rm -r " + directoryOldPath.getPath());
@@ -435,8 +439,8 @@
         pollForPermission(Manifest.permission.READ_EXTERNAL_STORAGE, /*granted*/ true);
         pollForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, /*granted*/ true);
 
-        final File videoFile = new File(EXTERNAL_STORAGE_DIR, VIDEO_FILE_NAME);
-        final File otherAppPdfFile = new File(TestUtils.DOWNLOAD_DIR, NONMEDIA_FILE_NAME);
+        final File videoFile = new File(TestUtils.getExternalStorageDir(), VIDEO_FILE_NAME);
+        final File otherAppPdfFile = new File(TestUtils.getDownloadDir(), NONMEDIA_FILE_NAME);
 
         try {
             assertThat(videoFile.createNewFile()).isTrue();
@@ -471,13 +475,13 @@
         pollForPermission(Manifest.permission.READ_EXTERNAL_STORAGE, /*granted*/ true);
         pollForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, /*granted*/ true);
 
-        final File videoFile = new File(EXTERNAL_STORAGE_DIR, VIDEO_FILE_NAME);
+        final File videoFile = new File(TestUtils.getExternalStorageDir(), VIDEO_FILE_NAME);
         try {
             assertThat(videoFile.createNewFile()).isTrue();
 
             installApp(TEST_APP_A, true);
             // videoFile is inserted to database, non-legacy app can see this videoFile on 'ls'.
-            assertThat(listAs(TEST_APP_A, EXTERNAL_STORAGE_DIR.getAbsolutePath()))
+            assertThat(listAs(TEST_APP_A, TestUtils.getExternalStorageDir().getAbsolutePath()))
                     .contains(VIDEO_FILE_NAME);
 
             // videoFile is in database, row ID for videoFile can not be -1.
@@ -498,8 +502,8 @@
         pollForPermission(Manifest.permission.READ_EXTERNAL_STORAGE, /*granted*/ true);
         pollForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, /*granted*/ true);
 
-        final File videoFile = new File(TestUtils.DCIM_DIR, VIDEO_FILE_NAME);
-        final File renamedVideoFile = new File(TestUtils.DCIM_DIR, "Renamed_" + VIDEO_FILE_NAME);
+        final File videoFile = new File(TestUtils.getDcimDir(), VIDEO_FILE_NAME);
+        final File renamedVideoFile = new File(TestUtils.getDcimDir(), "Renamed_" + VIDEO_FILE_NAME);
         final ContentResolver cr = getContentResolver();
 
         try {
@@ -531,8 +535,8 @@
         pollForPermission(Manifest.permission.READ_EXTERNAL_STORAGE, /*granted*/ true);
         pollForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, /*granted*/ true);
 
-        final File imageFile = new File(TestUtils.DCIM_DIR, IMAGE_FILE_NAME);
-        final File temporaryImageFile = new File(TestUtils.DCIM_DIR, IMAGE_FILE_NAME + "_.tmp");
+        final File imageFile = new File(TestUtils.getDcimDir(), IMAGE_FILE_NAME);
+        final File temporaryImageFile = new File(TestUtils.getDcimDir(), IMAGE_FILE_NAME + "_.tmp");
         final ContentResolver cr = getContentResolver();
 
         try {
@@ -574,9 +578,9 @@
         pollForPermission(Manifest.permission.READ_EXTERNAL_STORAGE, /*granted*/ true);
         pollForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, /*granted*/ true);
 
-        final File directoryNoMedia = new File(TestUtils.DCIM_DIR, ".directoryNoMedia");
+        final File directoryNoMedia = new File(TestUtils.getDcimDir(), ".directoryNoMedia");
         final File imageInNoMediaDir = new File(directoryNoMedia, IMAGE_FILE_NAME);
-        final File renamedImageInDCIM = new File(TestUtils.DCIM_DIR, IMAGE_FILE_NAME);
+        final File renamedImageInDCIM = new File(TestUtils.getDcimDir(), IMAGE_FILE_NAME);
         final File noMediaFile = new File(directoryNoMedia, ".nomedia");
         final ContentResolver cr = getContentResolver();
 
diff --git a/hostsidetests/scopedstorage/libs/ScopedStorageTestLib/src/android/scopedstorage/cts/lib/TestUtils.java b/hostsidetests/scopedstorage/libs/ScopedStorageTestLib/src/android/scopedstorage/cts/lib/TestUtils.java
index ce546e4..865a3b8 100644
--- a/hostsidetests/scopedstorage/libs/ScopedStorageTestLib/src/android/scopedstorage/cts/lib/TestUtils.java
+++ b/hostsidetests/scopedstorage/libs/ScopedStorageTestLib/src/android/scopedstorage/cts/lib/TestUtils.java
@@ -70,6 +70,8 @@
 import java.util.List;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.function.Supplier;
 
 /**
  * General helper functions for ScopedStorageTest tests.
@@ -96,37 +98,7 @@
     public static final byte[] BYTES_DATA2 = STR_DATA2.getBytes();
 
     // Root of external storage
-    public static final File EXTERNAL_STORAGE_DIR = Environment.getExternalStorageDirectory();
-    // Default top-level directories
-    public static final File ALARMS_DIR =
-            new File(EXTERNAL_STORAGE_DIR, Environment.DIRECTORY_ALARMS);
-    public static final File ANDROID_DIR = new File(EXTERNAL_STORAGE_DIR, "Android");
-    public static final File AUDIOBOOKS_DIR =
-            new File(EXTERNAL_STORAGE_DIR, Environment.DIRECTORY_AUDIOBOOKS);
-    public static final File DCIM_DIR = new File(EXTERNAL_STORAGE_DIR, Environment.DIRECTORY_DCIM);
-    public static final File DOCUMENTS_DIR =
-            new File(EXTERNAL_STORAGE_DIR, Environment.DIRECTORY_DOCUMENTS);
-    public static final File DOWNLOAD_DIR =
-            new File(EXTERNAL_STORAGE_DIR, Environment.DIRECTORY_DOWNLOADS);
-    public static final File MUSIC_DIR =
-            new File(EXTERNAL_STORAGE_DIR, Environment.DIRECTORY_MUSIC);
-    public static final File MOVIES_DIR =
-            new File(EXTERNAL_STORAGE_DIR, Environment.DIRECTORY_MOVIES);
-    public static final File NOTIFICATIONS_DIR =
-            new File(EXTERNAL_STORAGE_DIR, Environment.DIRECTORY_NOTIFICATIONS);
-    public static final File PICTURES_DIR =
-            new File(EXTERNAL_STORAGE_DIR, Environment.DIRECTORY_PICTURES);
-    public static final File PODCASTS_DIR =
-            new File(EXTERNAL_STORAGE_DIR, Environment.DIRECTORY_PODCASTS);
-    public static final File RINGTONES_DIR =
-            new File(EXTERNAL_STORAGE_DIR, Environment.DIRECTORY_RINGTONES);
-
-    public static final File[] DEFAULT_TOP_LEVEL_DIRS = new File[] {ALARMS_DIR, ANDROID_DIR,
-            AUDIOBOOKS_DIR, DCIM_DIR, DOCUMENTS_DIR, DOWNLOAD_DIR, MUSIC_DIR, MOVIES_DIR,
-            NOTIFICATIONS_DIR, PICTURES_DIR, PODCASTS_DIR, RINGTONES_DIR};
-
-    public static final File ANDROID_DATA_DIR = new File(ANDROID_DIR, "data");
-    public static final File ANDROID_MEDIA_DIR = new File(ANDROID_DIR, "media");
+    private static File sExternalStorageDirectory = Environment.getExternalStorageDirectory();
 
     private static final long POLLING_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(10);
     private static final long POLLING_SLEEP_MILLIS = 100;
@@ -135,11 +107,11 @@
      * Creates the top level default directories.
      *
      * <p>Those are usually created by MediaProvider, but some naughty tests might delete them
-     * and not restore them afterwards. so we make sure we create them before we make any
+     * and not restore them afterwards, so we make sure we create them before we make any
      * assumptions about their existence.
      */
     public static void setupDefaultDirectories() {
-        for (File dir : DEFAULT_TOP_LEVEL_DIRS) {
+        for (File dir : getDefaultTopLevelDirs()) {
             dir.mkdir();
         }
     }
@@ -601,28 +573,19 @@
      * Polls for external storage to be mounted.
      */
     public static void pollForExternalStorageState() throws Exception {
-        for (int i = 0; i < POLLING_TIMEOUT_MILLIS / POLLING_SLEEP_MILLIS; i++) {
-            if (Environment.getExternalStorageState(Environment.getExternalStorageDirectory())
-                            .equals(Environment.MEDIA_MOUNTED)) {
-                return;
-            }
-            Thread.sleep(POLLING_SLEEP_MILLIS);
-        }
-        fail("Timed out while waiting for ExternalStorageState to be MEDIA_MOUNTED");
+        pollForCondition(
+                () -> Environment.getExternalStorageState(getExternalStorageDir())
+                        .equals(Environment.MEDIA_MOUNTED),
+                "Timed out while waiting for ExternalStorageState to be MEDIA_MOUNTED");
     }
 
     /**
      * Polls until we're granted or denied a given permission.
      */
     public static void pollForPermission(String perm, boolean granted) throws Exception {
-        for (int i = 0; i < POLLING_TIMEOUT_MILLIS / POLLING_SLEEP_MILLIS; i++) {
-            if (granted == checkPermissionAndAppOp(perm)) {
-                return;
-            }
-            Thread.sleep(POLLING_SLEEP_MILLIS);
-        }
-        fail("Timed out while waiting for permission " + perm + " to be "
-                + (granted ? "granted" : "revoked"));
+        pollForCondition(() -> granted == checkPermissionAndAppOp(perm),
+                "Timed out while waiting for permission " + perm + " to be "
+                        + (granted ? "granted" : "revoked"));
     }
 
     /**
@@ -646,6 +609,131 @@
         }
     }
 
+    /**
+     * Asserts that {@code dir} is a directory and that it contains all of {@code expectedContent}
+     */
+    public static void assertDirectoryContains(@NonNull File dir, File... expectedContent) {
+        assertThat(dir.isDirectory()).isTrue();
+        final List<File> actualContent = Arrays.asList(dir.listFiles());
+        for (File f : expectedContent) {
+            assertThat(actualContent).contains(f);
+        }
+    }
+
+    public static File getExternalStorageDir() {
+        return sExternalStorageDirectory;
+    }
+
+    public static void setExternalStorageVolume(@NonNull String volName) {
+        sExternalStorageDirectory = new File("/storage/" + volName);
+    }
+
+    /**
+     * Resets the root directory of external storage to the default.
+     *
+     * @see Environment#getExternalStorageDirectory()
+     */
+    public static void resetDefaultExternalStorageVolume() {
+        sExternalStorageDirectory = Environment.getExternalStorageDirectory();
+    }
+
+    /**
+     * Creates and returns the Android data sub-directory belonging to the calling package.
+     */
+    public static File getExternalFilesDir() {
+        final String packageName = getContext().getPackageName();
+        final File res = new File(getAndroidDataDir(), packageName + "/files");
+        if (!res.equals(getContext().getExternalFilesDir(null))) {
+            res.mkdirs();
+        }
+        return res;
+    }
+
+    /**
+     * Creates and returns the Android media sub-directory belonging to the calling package.
+     */
+    public static File getExternalMediaDir() {
+        final String packageName = getContext().getPackageName();
+        final File res = new File(getAndroidMediaDir(), packageName);
+        if (!res.equals(getContext().getExternalMediaDirs()[0])) {
+            res.mkdirs();
+        }
+        return res;
+    }
+
+    public static File getAlarmsDir() {
+        return new File(getExternalStorageDir(),
+                Environment.DIRECTORY_ALARMS);
+    }
+
+    public static File getAndroidDir() {
+        return new File(getExternalStorageDir(),
+                "Android");
+    }
+
+    public static File getAudiobooksDir() {
+        return new File(getExternalStorageDir(),
+                Environment.DIRECTORY_AUDIOBOOKS);
+    }
+
+    public static File getDcimDir() {
+        return new File(getExternalStorageDir(), Environment.DIRECTORY_DCIM);
+    }
+
+    public static File getDocumentsDir() {
+        return new File(getExternalStorageDir(),
+                Environment.DIRECTORY_DOCUMENTS);
+    }
+
+    public static File getDownloadDir() {
+        return new File(getExternalStorageDir(),
+                Environment.DIRECTORY_DOWNLOADS);
+    }
+
+    public static File getMusicDir() {
+        return new File(getExternalStorageDir(),
+                Environment.DIRECTORY_MUSIC);
+    }
+
+    public static File getMoviesDir() {
+        return new File(getExternalStorageDir(),
+                Environment.DIRECTORY_MOVIES);
+    }
+
+    public static File getNotificationsDir() {
+        return new File(getExternalStorageDir(),
+                Environment.DIRECTORY_NOTIFICATIONS);
+    }
+
+    public static File getPicturesDir() {
+        return new File(getExternalStorageDir(),
+                Environment.DIRECTORY_PICTURES);
+    }
+
+    public static File getPodcastsDir() {
+        return new File(getExternalStorageDir(),
+                Environment.DIRECTORY_PODCASTS);
+    }
+
+    public static File getRingtonesDir() {
+        return new File(getExternalStorageDir(),
+                Environment.DIRECTORY_RINGTONES);
+    }
+
+    public static File getAndroidDataDir() {
+        return new File(getAndroidDir(), "data");
+    }
+
+    public static File getAndroidMediaDir() {
+        return new File(getAndroidDir(), "media");
+    }
+
+    public static File[] getDefaultTopLevelDirs() {
+        return new File [] { getAlarmsDir(), getAndroidDir(), getAudiobooksDir(), getDcimDir(),
+                getDocumentsDir(), getDownloadDir(), getMusicDir(), getMoviesDir(),
+                getNotificationsDir(), getPicturesDir(), getPodcastsDir(), getRingtonesDir() };
+    }
+
     private static void assertInputStreamContent(InputStream in, byte[] expectedContent)
             throws IOException {
         assertThat(ByteStreams.toByteArray(in)).isEqualTo(expectedContent);
@@ -838,14 +926,24 @@
         return c;
     }
 
-    /**
-     * Asserts that {@code dir} is a directory and that it contains all of {@code expectedContent}
-     */
-    public static void assertDirectoryContains(@NonNull File dir, File... expectedContent) {
-        assertThat(dir.isDirectory()).isTrue();
-        final List<File> actualContent = Arrays.asList(dir.listFiles());
-        for (File f : expectedContent) {
-            assertThat(actualContent).contains(f);
+    private static boolean partitionDisk() {
+        try {
+            final String listDisks = executeShellCommand("sm list-disks").trim();
+            executeShellCommand("sm partition " + listDisks + " public");
+            return true;
+        } catch (Exception e) {
+            return false;
         }
     }
+
+    private static void pollForCondition(Supplier<Boolean> condition, String errorMessage)
+            throws Exception {
+        for (int i = 0; i < POLLING_TIMEOUT_MILLIS / POLLING_SLEEP_MILLIS; i++) {
+            if (condition.get()) {
+                return;
+            }
+            Thread.sleep(POLLING_SLEEP_MILLIS);
+        }
+        throw new TimeoutException(errorMessage);
+    }
 }
diff --git a/hostsidetests/scopedstorage/src/android/scopedstorage/cts/ScopedStorageTest.java b/hostsidetests/scopedstorage/src/android/scopedstorage/cts/ScopedStorageTest.java
index 2867aec..0dcd865 100644
--- a/hostsidetests/scopedstorage/src/android/scopedstorage/cts/ScopedStorageTest.java
+++ b/hostsidetests/scopedstorage/src/android/scopedstorage/cts/ScopedStorageTest.java
@@ -23,22 +23,8 @@
 import static android.scopedstorage.cts.lib.RedactionTestHelper.assertExifMetadataMismatch;
 import static android.scopedstorage.cts.lib.RedactionTestHelper.getExifMetadata;
 import static android.scopedstorage.cts.lib.RedactionTestHelper.getExifMetadataFromRawResource;
-import static android.scopedstorage.cts.lib.TestUtils.ALARMS_DIR;
-import static android.scopedstorage.cts.lib.TestUtils.ANDROID_DATA_DIR;
-import static android.scopedstorage.cts.lib.TestUtils.ANDROID_MEDIA_DIR;
-import static android.scopedstorage.cts.lib.TestUtils.AUDIOBOOKS_DIR;
 import static android.scopedstorage.cts.lib.TestUtils.BYTES_DATA1;
 import static android.scopedstorage.cts.lib.TestUtils.BYTES_DATA2;
-import static android.scopedstorage.cts.lib.TestUtils.DCIM_DIR;
-import static android.scopedstorage.cts.lib.TestUtils.DEFAULT_TOP_LEVEL_DIRS;
-import static android.scopedstorage.cts.lib.TestUtils.DOCUMENTS_DIR;
-import static android.scopedstorage.cts.lib.TestUtils.DOWNLOAD_DIR;
-import static android.scopedstorage.cts.lib.TestUtils.MOVIES_DIR;
-import static android.scopedstorage.cts.lib.TestUtils.MUSIC_DIR;
-import static android.scopedstorage.cts.lib.TestUtils.NOTIFICATIONS_DIR;
-import static android.scopedstorage.cts.lib.TestUtils.PICTURES_DIR;
-import static android.scopedstorage.cts.lib.TestUtils.PODCASTS_DIR;
-import static android.scopedstorage.cts.lib.TestUtils.RINGTONES_DIR;
 import static android.scopedstorage.cts.lib.TestUtils.STR_DATA1;
 import static android.scopedstorage.cts.lib.TestUtils.STR_DATA2;
 import static android.scopedstorage.cts.lib.TestUtils.allowAppOpsToUid;
@@ -57,10 +43,28 @@
 import static android.scopedstorage.cts.lib.TestUtils.deleteWithMediaProvider;
 import static android.scopedstorage.cts.lib.TestUtils.denyAppOpsToUid;
 import static android.scopedstorage.cts.lib.TestUtils.executeShellCommand;
+import static android.scopedstorage.cts.lib.TestUtils.getAlarmsDir;
+import static android.scopedstorage.cts.lib.TestUtils.getAndroidDataDir;
+import static android.scopedstorage.cts.lib.TestUtils.getAndroidDir;
+import static android.scopedstorage.cts.lib.TestUtils.getAndroidMediaDir;
+import static android.scopedstorage.cts.lib.TestUtils.getAudiobooksDir;
 import static android.scopedstorage.cts.lib.TestUtils.getContentResolver;
+import static android.scopedstorage.cts.lib.TestUtils.getDcimDir;
+import static android.scopedstorage.cts.lib.TestUtils.getDefaultTopLevelDirs;
+import static android.scopedstorage.cts.lib.TestUtils.getDocumentsDir;
+import static android.scopedstorage.cts.lib.TestUtils.getDownloadDir;
+import static android.scopedstorage.cts.lib.TestUtils.getExternalFilesDir;
+import static android.scopedstorage.cts.lib.TestUtils.getExternalMediaDir;
+import static android.scopedstorage.cts.lib.TestUtils.getExternalStorageDir;
 import static android.scopedstorage.cts.lib.TestUtils.getFileMimeTypeFromDatabase;
 import static android.scopedstorage.cts.lib.TestUtils.getFileRowIdFromDatabase;
 import static android.scopedstorage.cts.lib.TestUtils.getFileUri;
+import static android.scopedstorage.cts.lib.TestUtils.getMoviesDir;
+import static android.scopedstorage.cts.lib.TestUtils.getMusicDir;
+import static android.scopedstorage.cts.lib.TestUtils.getNotificationsDir;
+import static android.scopedstorage.cts.lib.TestUtils.getPicturesDir;
+import static android.scopedstorage.cts.lib.TestUtils.getPodcastsDir;
+import static android.scopedstorage.cts.lib.TestUtils.getRingtonesDir;
 import static android.scopedstorage.cts.lib.TestUtils.grantPermission;
 import static android.scopedstorage.cts.lib.TestUtils.installApp;
 import static android.scopedstorage.cts.lib.TestUtils.installAppWithStoragePermissions;
@@ -135,13 +139,8 @@
     static final String TAG = "ScopedStorageTest";
     static final String THIS_PACKAGE_NAME = getContext().getPackageName();
 
-    static final File EXTERNAL_STORAGE_DIR = Environment.getExternalStorageDirectory();
-
     static final String TEST_DIRECTORY_NAME = "ScopedStorageTestDirectory";
 
-    static final File EXTERNAL_FILES_DIR = getContext().getExternalFilesDir(null);
-    static final File EXTERNAL_MEDIA_DIR = getContext().getExternalMediaDirs()[0];
-
     static final String AUDIO_FILE_NAME = "ScopedStorageTest_file.mp3";
     static final String PLAYLIST_FILE_NAME = "ScopedStorageTest_file.m3u";
     static final String SUBTITLE_FILE_NAME = "ScopedStorageTest_file.srt";
@@ -150,8 +149,6 @@
     static final String NONMEDIA_FILE_NAME = "ScopedStorageTest_file.pdf";
 
     static final String FILE_CREATION_ERROR_MESSAGE = "No such file or directory";
-    private static final File ANDROID_DIR =
-            new File(Environment.getExternalStorageDirectory(), "Android");
 
     private static final TestApp TEST_APP_A = new TestApp("TestAppA",
             "android.scopedstorage.cts.testapp.A", 1, false, "CtsScopedStorageTestAppA.apk");
@@ -172,7 +169,7 @@
         assumeTrue(getBoolean("persist.sys.fuse", false));
 
         pollForExternalStorageState();
-        EXTERNAL_FILES_DIR.mkdirs();
+        getExternalFilesDir().mkdirs();
     }
 
     /**
@@ -188,66 +185,72 @@
      */
     @Test
     public void testTypePathConformity() throws Exception {
+        final File dcimDir = getDcimDir();
+        final File documentsDir = getDocumentsDir();
+        final File downloadDir = getDownloadDir();
+        final File moviesDir = getMoviesDir();
+        final File musicDir = getMusicDir();
+        final File picturesDir = getPicturesDir();
         // Only audio files can be created in Music
         assertThrows(IOException.class, "Operation not permitted",
-                () -> { new File(MUSIC_DIR, NONMEDIA_FILE_NAME).createNewFile(); });
+                () -> { new File(musicDir, NONMEDIA_FILE_NAME).createNewFile(); });
         assertThrows(IOException.class, "Operation not permitted",
-                () -> { new File(MUSIC_DIR, VIDEO_FILE_NAME).createNewFile(); });
+                () -> { new File(musicDir, VIDEO_FILE_NAME).createNewFile(); });
         assertThrows(IOException.class, "Operation not permitted",
-                () -> { new File(MUSIC_DIR, IMAGE_FILE_NAME).createNewFile(); });
+                () -> { new File(musicDir, IMAGE_FILE_NAME).createNewFile(); });
         // Only video files can be created in Movies
         assertThrows(IOException.class, "Operation not permitted",
-                () -> { new File(MOVIES_DIR, NONMEDIA_FILE_NAME).createNewFile(); });
+                () -> { new File(moviesDir, NONMEDIA_FILE_NAME).createNewFile(); });
         assertThrows(IOException.class, "Operation not permitted",
-                () -> { new File(MOVIES_DIR, AUDIO_FILE_NAME).createNewFile(); });
+                () -> { new File(moviesDir, AUDIO_FILE_NAME).createNewFile(); });
         assertThrows(IOException.class, "Operation not permitted",
-                () -> { new File(MOVIES_DIR, IMAGE_FILE_NAME).createNewFile(); });
+                () -> { new File(moviesDir, IMAGE_FILE_NAME).createNewFile(); });
         // Only image and video files can be created in DCIM
         assertThrows(IOException.class, "Operation not permitted",
-                () -> { new File(DCIM_DIR, NONMEDIA_FILE_NAME).createNewFile(); });
+                () -> { new File(dcimDir, NONMEDIA_FILE_NAME).createNewFile(); });
         assertThrows(IOException.class, "Operation not permitted",
-                () -> { new File(DCIM_DIR, AUDIO_FILE_NAME).createNewFile(); });
+                () -> { new File(dcimDir, AUDIO_FILE_NAME).createNewFile(); });
         // Only image and video files can be created in Pictures
         assertThrows(IOException.class, "Operation not permitted",
-                () -> { new File(PICTURES_DIR, NONMEDIA_FILE_NAME).createNewFile(); });
+                () -> { new File(picturesDir, NONMEDIA_FILE_NAME).createNewFile(); });
         assertThrows(IOException.class, "Operation not permitted",
-                () -> { new File(PICTURES_DIR, AUDIO_FILE_NAME).createNewFile(); });
+                () -> { new File(picturesDir, AUDIO_FILE_NAME).createNewFile(); });
         assertThrows(IOException.class, "Operation not permitted",
-                () -> { new File(PICTURES_DIR, PLAYLIST_FILE_NAME).createNewFile(); });
+                () -> { new File(picturesDir, PLAYLIST_FILE_NAME).createNewFile(); });
         assertThrows(IOException.class, "Operation not permitted",
-                () -> { new File(DCIM_DIR, SUBTITLE_FILE_NAME).createNewFile(); });
+                () -> { new File(dcimDir, SUBTITLE_FILE_NAME).createNewFile(); });
 
-        assertCanCreateFile(new File(ALARMS_DIR, AUDIO_FILE_NAME));
-        assertCanCreateFile(new File(AUDIOBOOKS_DIR, AUDIO_FILE_NAME));
-        assertCanCreateFile(new File(DCIM_DIR, IMAGE_FILE_NAME));
-        assertCanCreateFile(new File(DCIM_DIR, VIDEO_FILE_NAME));
-        assertCanCreateFile(new File(DOCUMENTS_DIR, AUDIO_FILE_NAME));
-        assertCanCreateFile(new File(DOCUMENTS_DIR, IMAGE_FILE_NAME));
-        assertCanCreateFile(new File(DOCUMENTS_DIR, NONMEDIA_FILE_NAME));
-        assertCanCreateFile(new File(DOCUMENTS_DIR, VIDEO_FILE_NAME));
-        assertCanCreateFile(new File(DOWNLOAD_DIR, AUDIO_FILE_NAME));
-        assertCanCreateFile(new File(DOWNLOAD_DIR, IMAGE_FILE_NAME));
-        assertCanCreateFile(new File(DOWNLOAD_DIR, NONMEDIA_FILE_NAME));
-        assertCanCreateFile(new File(DOWNLOAD_DIR, VIDEO_FILE_NAME));
-        assertCanCreateFile(new File(MOVIES_DIR, VIDEO_FILE_NAME));
-        assertCanCreateFile(new File(MOVIES_DIR, SUBTITLE_FILE_NAME));
-        assertCanCreateFile(new File(MUSIC_DIR, AUDIO_FILE_NAME));
-        assertCanCreateFile(new File(MUSIC_DIR, PLAYLIST_FILE_NAME));
-        assertCanCreateFile(new File(NOTIFICATIONS_DIR, AUDIO_FILE_NAME));
-        assertCanCreateFile(new File(PICTURES_DIR, IMAGE_FILE_NAME));
-        assertCanCreateFile(new File(PICTURES_DIR, VIDEO_FILE_NAME));
-        assertCanCreateFile(new File(PODCASTS_DIR, AUDIO_FILE_NAME));
-        assertCanCreateFile(new File(RINGTONES_DIR, AUDIO_FILE_NAME));
+        assertCanCreateFile(new File(getAlarmsDir(), AUDIO_FILE_NAME));
+        assertCanCreateFile(new File(getAudiobooksDir(), AUDIO_FILE_NAME));
+        assertCanCreateFile(new File(dcimDir, IMAGE_FILE_NAME));
+        assertCanCreateFile(new File(dcimDir, VIDEO_FILE_NAME));
+        assertCanCreateFile(new File(documentsDir, AUDIO_FILE_NAME));
+        assertCanCreateFile(new File(documentsDir, IMAGE_FILE_NAME));
+        assertCanCreateFile(new File(documentsDir, NONMEDIA_FILE_NAME));
+        assertCanCreateFile(new File(documentsDir, VIDEO_FILE_NAME));
+        assertCanCreateFile(new File(downloadDir, AUDIO_FILE_NAME));
+        assertCanCreateFile(new File(downloadDir, IMAGE_FILE_NAME));
+        assertCanCreateFile(new File(downloadDir, NONMEDIA_FILE_NAME));
+        assertCanCreateFile(new File(downloadDir, VIDEO_FILE_NAME));
+        assertCanCreateFile(new File(moviesDir, VIDEO_FILE_NAME));
+        assertCanCreateFile(new File(moviesDir, SUBTITLE_FILE_NAME));
+        assertCanCreateFile(new File(musicDir, AUDIO_FILE_NAME));
+        assertCanCreateFile(new File(musicDir, PLAYLIST_FILE_NAME));
+        assertCanCreateFile(new File(getNotificationsDir(), AUDIO_FILE_NAME));
+        assertCanCreateFile(new File(picturesDir, IMAGE_FILE_NAME));
+        assertCanCreateFile(new File(picturesDir, VIDEO_FILE_NAME));
+        assertCanCreateFile(new File(getPodcastsDir(), AUDIO_FILE_NAME));
+        assertCanCreateFile(new File(getRingtonesDir(), AUDIO_FILE_NAME));
 
         // No file whatsoever can be created in the top level directory
         assertThrows(IOException.class, "Operation not permitted",
-                () -> { new File(EXTERNAL_STORAGE_DIR, NONMEDIA_FILE_NAME).createNewFile(); });
+                () -> { new File(getExternalStorageDir(), NONMEDIA_FILE_NAME).createNewFile(); });
         assertThrows(IOException.class, "Operation not permitted",
-                () -> { new File(EXTERNAL_STORAGE_DIR, AUDIO_FILE_NAME).createNewFile(); });
+                () -> { new File(getExternalStorageDir(), AUDIO_FILE_NAME).createNewFile(); });
         assertThrows(IOException.class, "Operation not permitted",
-                () -> { new File(EXTERNAL_STORAGE_DIR, IMAGE_FILE_NAME).createNewFile(); });
+                () -> { new File(getExternalStorageDir(), IMAGE_FILE_NAME).createNewFile(); });
         assertThrows(IOException.class, "Operation not permitted",
-                () -> { new File(EXTERNAL_STORAGE_DIR, VIDEO_FILE_NAME).createNewFile(); });
+                () -> { new File(getExternalStorageDir(), VIDEO_FILE_NAME).createNewFile(); });
     }
 
     /**
@@ -256,7 +259,7 @@
      */
     @Test
     public void testCreateFileInAppExternalDir() throws Exception {
-        final File file = new File(EXTERNAL_FILES_DIR, "text.txt");
+        final File file = new File(getExternalFilesDir(), "text.txt");
         try {
             assertThat(file.createNewFile()).isTrue();
             assertThat(file.delete()).isTrue();
@@ -283,7 +286,7 @@
     public void testCreateFileInOtherAppExternalDir() throws Exception {
         // Creating a file in a non existent package dir should return ENOENT, as expected
         final File nonexistentPackageFileDir = new File(
-                EXTERNAL_FILES_DIR.getPath().replace(THIS_PACKAGE_NAME, "no.such.package"));
+                getExternalFilesDir().getPath().replace(THIS_PACKAGE_NAME, "no.such.package"));
         final File file1 = new File(nonexistentPackageFileDir, NONMEDIA_FILE_NAME);
         assertThrows(
                 IOException.class, FILE_CREATION_ERROR_MESSAGE, () -> { file1.createNewFile(); });
@@ -292,7 +295,7 @@
         // leaking installed app names, and we know the following directory exists because shell
         // mkdirs it in test setup
         final File shellPackageFileDir = new File(
-                EXTERNAL_FILES_DIR.getPath().replace(THIS_PACKAGE_NAME, "com.android.shell"));
+                getExternalFilesDir().getPath().replace(THIS_PACKAGE_NAME, "com.android.shell"));
         final File file2 = new File(shellPackageFileDir, NONMEDIA_FILE_NAME);
         assertThrows(
                 IOException.class, FILE_CREATION_ERROR_MESSAGE, () -> { file1.createNewFile(); });
@@ -303,7 +306,7 @@
      */
     @Test
     public void testContributeMediaFile() throws Exception {
-        final File imageFile = new File(DCIM_DIR, IMAGE_FILE_NAME);
+        final File imageFile = new File(getDcimDir(), IMAGE_FILE_NAME);
 
         ContentResolver cr = getContentResolver();
         final String selection =
@@ -358,13 +361,14 @@
 
     @Test
     public void testCreateAndDeleteEmptyDir() throws Exception {
+        final File externalFilesDir = getExternalFilesDir();
         // Remove directory in order to create it again
-        EXTERNAL_FILES_DIR.delete();
+        externalFilesDir.delete();
 
         // Can create own external files dir
-        assertThat(EXTERNAL_FILES_DIR.mkdir()).isTrue();
+        assertThat(externalFilesDir.mkdir()).isTrue();
 
-        final File dir1 = new File(EXTERNAL_FILES_DIR, "random_dir");
+        final File dir1 = new File(externalFilesDir, "random_dir");
         // Can create dirs inside it
         assertThat(dir1.mkdir()).isTrue();
 
@@ -375,13 +379,13 @@
         // And can delete them all
         assertThat(dir2.delete()).isTrue();
         assertThat(dir1.delete()).isTrue();
-        assertThat(EXTERNAL_FILES_DIR.delete()).isTrue();
+        assertThat(externalFilesDir.delete()).isTrue();
 
         // Can't create external dir for other apps
         final File nonexistentPackageFileDir = new File(
-                EXTERNAL_FILES_DIR.getPath().replace(THIS_PACKAGE_NAME, "no.such.package"));
+                externalFilesDir.getPath().replace(THIS_PACKAGE_NAME, "no.such.package"));
         final File shellPackageFileDir = new File(
-                EXTERNAL_FILES_DIR.getPath().replace(THIS_PACKAGE_NAME, "com.android.shell"));
+                externalFilesDir.getPath().replace(THIS_PACKAGE_NAME, "com.android.shell"));
 
         assertThat(nonexistentPackageFileDir.mkdir()).isFalse();
         assertThat(shellPackageFileDir.mkdir()).isFalse();
@@ -389,8 +393,8 @@
 
     @Test
     public void testCantAccessOtherAppsContents() throws Exception {
-        final File mediaFile = new File(PICTURES_DIR, IMAGE_FILE_NAME);
-        final File nonMediaFile = new File(DOWNLOAD_DIR, NONMEDIA_FILE_NAME);
+        final File mediaFile = new File(getPicturesDir(), IMAGE_FILE_NAME);
+        final File nonMediaFile = new File(getDownloadDir(), NONMEDIA_FILE_NAME);
         try {
             installApp(TEST_APP_A);
 
@@ -415,7 +419,7 @@
 
     @Test
     public void testCantDeleteOtherAppsContents() throws Exception {
-        final File dirInDownload = new File(DOWNLOAD_DIR, TEST_DIRECTORY_NAME);
+        final File dirInDownload = new File(getDownloadDir(), TEST_DIRECTORY_NAME);
         final File mediaFile = new File(dirInDownload, IMAGE_FILE_NAME);
         final File nonMediaFile = new File(dirInDownload, NONMEDIA_FILE_NAME);
         try {
@@ -470,27 +474,26 @@
     public void testOpendirRestrictions() throws Exception {
         // Opening a non existent package directory should fail, as expected
         final File nonexistentPackageFileDir = new File(
-                EXTERNAL_FILES_DIR.getPath().replace(THIS_PACKAGE_NAME, "no.such.package"));
+                getExternalFilesDir().getPath().replace(THIS_PACKAGE_NAME, "no.such.package"));
         assertThat(nonexistentPackageFileDir.list()).isNull();
 
         // Opening another package's external directory should fail as well, even if it exists
         final File shellPackageFileDir = new File(
-                EXTERNAL_FILES_DIR.getPath().replace(THIS_PACKAGE_NAME, "com.android.shell"));
+                getExternalFilesDir().getPath().replace(THIS_PACKAGE_NAME, "com.android.shell"));
         assertThat(shellPackageFileDir.list()).isNull();
 
         // We can open our own external files directory
-        final String[] filesList = EXTERNAL_FILES_DIR.list();
+        final String[] filesList = getExternalFilesDir().list();
         assertThat(filesList).isNotNull();
-        assertThat(filesList).isEmpty();
 
         // We can open any public directory in external storage
-        assertThat(DCIM_DIR.list()).isNotNull();
-        assertThat(DOWNLOAD_DIR.list()).isNotNull();
-        assertThat(MOVIES_DIR.list()).isNotNull();
-        assertThat(MUSIC_DIR.list()).isNotNull();
+        assertThat(getDcimDir().list()).isNotNull();
+        assertThat(getDownloadDir().list()).isNotNull();
+        assertThat(getMoviesDir().list()).isNotNull();
+        assertThat(getMusicDir().list()).isNotNull();
 
         // We can open the root directory of external storage
-        final String[] topLevelDirs = EXTERNAL_STORAGE_DIR.list();
+        final String[] topLevelDirs = getExternalStorageDir().list();
         assertThat(topLevelDirs).isNotNull();
         // TODO(b/145287327): This check fails on a device with no visible files.
         // This can be fixed if we display default directories.
@@ -499,7 +502,7 @@
 
     @Test
     public void testLowLevelFileIO() throws Exception {
-        String filePath = new File(DOWNLOAD_DIR, NONMEDIA_FILE_NAME).toString();
+        String filePath = new File(getDownloadDir(), NONMEDIA_FILE_NAME).toString();
         try {
             int createFlags = O_CREAT | O_RDWR;
             int createExclFlags = createFlags | O_EXCL;
@@ -544,7 +547,8 @@
      */
     @Test
     public void testListDirectoriesWithMediaFiles() throws Exception {
-        final File dir = new File(DCIM_DIR, TEST_DIRECTORY_NAME);
+        final File dcimDir = getDcimDir();
+        final File dir = new File(dcimDir, TEST_DIRECTORY_NAME);
         final File videoFile = new File(dir, VIDEO_FILE_NAME);
         final String videoFileName = videoFile.getName();
         try {
@@ -556,14 +560,14 @@
             installApp(TEST_APP_A);
             assertThat(createFileAs(TEST_APP_A, videoFile.getPath())).isTrue();
             // TEST_APP_A should see TEST_DIRECTORY in DCIM and new file in TEST_DIRECTORY.
-            assertThat(listAs(TEST_APP_A, DCIM_DIR.getPath())).contains(TEST_DIRECTORY_NAME);
+            assertThat(listAs(TEST_APP_A, dcimDir.getPath())).contains(TEST_DIRECTORY_NAME);
             assertThat(listAs(TEST_APP_A, dir.getPath())).containsExactly(videoFileName);
 
             // Install TEST_APP_B with storage permission.
             installAppWithStoragePermissions(TEST_APP_B);
             // TEST_APP_B with storage permission should see TEST_DIRECTORY in DCIM and new file
             // in TEST_DIRECTORY.
-            assertThat(listAs(TEST_APP_B, DCIM_DIR.getPath())).contains(TEST_DIRECTORY_NAME);
+            assertThat(listAs(TEST_APP_B, dcimDir.getPath())).contains(TEST_DIRECTORY_NAME);
             assertThat(listAs(TEST_APP_B, dir.getPath())).containsExactly(videoFileName);
 
             // Revoke storage permission for TEST_APP_B
@@ -571,7 +575,7 @@
                     TEST_APP_B.getPackageName(), Manifest.permission.READ_EXTERNAL_STORAGE);
             // TEST_APP_B without storage permission should see TEST_DIRECTORY in DCIM and should
             // not see new file in new TEST_DIRECTORY.
-            assertThat(listAs(TEST_APP_B, DCIM_DIR.getPath())).contains(TEST_DIRECTORY_NAME);
+            assertThat(listAs(TEST_APP_B, dcimDir.getPath())).contains(TEST_DIRECTORY_NAME);
             assertThat(listAs(TEST_APP_B, dir.getPath())).doesNotContain(videoFileName);
         } finally {
             uninstallAppNoThrow(TEST_APP_B);
@@ -586,7 +590,8 @@
      */
     @Test
     public void testListDirectoriesWithNonMediaFiles() throws Exception {
-        final File dir = new File(DOWNLOAD_DIR, TEST_DIRECTORY_NAME);
+        final File downloadDir = getDownloadDir();
+        final File dir = new File(downloadDir, TEST_DIRECTORY_NAME);
         final File pdfFile = new File(dir, NONMEDIA_FILE_NAME);
         final String pdfFileName = pdfFile.getName();
         try {
@@ -598,16 +603,16 @@
             installApp(TEST_APP_A);
             assertThat(createFileAs(TEST_APP_A, pdfFile.getPath())).isTrue();
 
-            // TEST_APP_A should see TEST_DIRECTORY in DOWNLOAD_DIR and new non media file in
+            // TEST_APP_A should see TEST_DIRECTORY in downloadDir and new non media file in
             // TEST_DIRECTORY.
-            assertThat(listAs(TEST_APP_A, DOWNLOAD_DIR.getPath())).contains(TEST_DIRECTORY_NAME);
+            assertThat(listAs(TEST_APP_A, downloadDir.getPath())).contains(TEST_DIRECTORY_NAME);
             assertThat(listAs(TEST_APP_A, dir.getPath())).containsExactly(pdfFileName);
 
             // Install TEST_APP_B with storage permission.
             installAppWithStoragePermissions(TEST_APP_B);
-            // TEST_APP_B with storage permission should see TEST_DIRECTORY in DOWNLOAD_DIR
+            // TEST_APP_B with storage permission should see TEST_DIRECTORY in downloadDir
             // and should not see new non media file in TEST_DIRECTORY.
-            assertThat(listAs(TEST_APP_B, DOWNLOAD_DIR.getPath())).contains(TEST_DIRECTORY_NAME);
+            assertThat(listAs(TEST_APP_B, downloadDir.getPath())).contains(TEST_DIRECTORY_NAME);
             assertThat(listAs(TEST_APP_B, dir.getPath())).doesNotContain(pdfFileName);
         } finally {
             uninstallAppNoThrow(TEST_APP_B);
@@ -623,7 +628,7 @@
     @Test
     public void testListFilesFromExternalFilesDirectory() throws Exception {
         final String packageName = THIS_PACKAGE_NAME;
-        final File videoFile = new File(EXTERNAL_FILES_DIR, NONMEDIA_FILE_NAME);
+        final File videoFile = new File(getExternalFilesDir(), NONMEDIA_FILE_NAME);
 
         try {
             // Create a file in app's external files directory
@@ -638,8 +643,9 @@
             // TEST_APP_A should not see other app's external files directory.
             installAppWithStoragePermissions(TEST_APP_A);
 
-            assertThrows(IOException.class, () -> listAs(TEST_APP_A, ANDROID_DATA_DIR.getPath()));
-            assertThrows(IOException.class, () -> listAs(TEST_APP_A, EXTERNAL_FILES_DIR.getPath()));
+            assertThrows(IOException.class, () -> listAs(TEST_APP_A, getAndroidDataDir().getPath()));
+            assertThrows(IOException.class,
+                    () -> listAs(TEST_APP_A, getExternalFilesDir().getPath()));
         } finally {
             videoFile.delete();
             uninstallAppNoThrow(TEST_APP_A);
@@ -651,7 +657,7 @@
      */
     @Test
     public void testListFilesFromExternalMediaDirectory() throws Exception {
-        final File videoFile = new File(EXTERNAL_MEDIA_DIR, VIDEO_FILE_NAME);
+        final File videoFile = new File(getExternalMediaDir(), VIDEO_FILE_NAME);
 
         try {
             // Create a file in app's external media directory
@@ -666,9 +672,11 @@
             // Install TEST_APP_A with READ_EXTERNAL_STORAGE permission.
             // TEST_APP_A with storage permission should see other app's external media directory.
             installAppWithStoragePermissions(TEST_APP_A);
-            // Apps with READ_EXTERNAL_STORAGE can list files in other app's external media directory.
-            assertThat(listAs(TEST_APP_A, ANDROID_MEDIA_DIR.getPath())).contains(THIS_PACKAGE_NAME);
-            assertThat(listAs(TEST_APP_A, EXTERNAL_MEDIA_DIR.getPath()))
+            // Apps with READ_EXTERNAL_STORAGE can list files in other app's external media
+            // directory.
+            assertThat(listAs(TEST_APP_A, getAndroidMediaDir().getPath()))
+                    .contains(THIS_PACKAGE_NAME);
+            assertThat(listAs(TEST_APP_A, getExternalMediaDir().getPath()))
                     .containsExactly(videoFile.getName());
         } finally {
             videoFile.delete();
@@ -681,8 +689,8 @@
      */
     @Test
     public void testListUnsupportedFileType() throws Exception {
-        final File pdfFile = new File(DCIM_DIR, NONMEDIA_FILE_NAME);
-        final File videoFile = new File(MUSIC_DIR, VIDEO_FILE_NAME);
+        final File pdfFile = new File(getDcimDir(), NONMEDIA_FILE_NAME);
+        final File videoFile = new File(getMusicDir(), VIDEO_FILE_NAME);
         try {
             // TEST_APP_A with storage permission should not see pdf file in DCIM
             executeShellCommand("touch " + pdfFile.getAbsolutePath());
@@ -690,13 +698,14 @@
             assertThat(MediaStore.scanFile(getContentResolver(), pdfFile)).isNotNull();
 
             installAppWithStoragePermissions(TEST_APP_A);
-            assertThat(listAs(TEST_APP_A, DCIM_DIR.getPath())).doesNotContain(NONMEDIA_FILE_NAME);
+            assertThat(listAs(TEST_APP_A, getDcimDir().getPath()))
+                    .doesNotContain(NONMEDIA_FILE_NAME);
 
             executeShellCommand("touch " + videoFile.getAbsolutePath());
             // We don't insert files to db for files created by shell.
             assertThat(MediaStore.scanFile(getContentResolver(), videoFile)).isNotNull();
             // TEST_APP_A with storage permission should see video file in Music directory.
-            assertThat(listAs(TEST_APP_A, MUSIC_DIR.getPath())).contains(VIDEO_FILE_NAME);
+            assertThat(listAs(TEST_APP_A, getMusicDir().getPath())).contains(VIDEO_FILE_NAME);
         } finally {
             executeShellCommand("rm " + pdfFile.getAbsolutePath());
             executeShellCommand("rm " + videoFile.getAbsolutePath());
@@ -706,7 +715,7 @@
 
     @Test
     public void testMetaDataRedaction() throws Exception {
-        File jpgFile = new File(PICTURES_DIR, "img_metadata.jpg");
+        File jpgFile = new File(getPicturesDir(), "img_metadata.jpg");
         try {
             if (jpgFile.exists()) {
                 assertThat(jpgFile.delete()).isTrue();
@@ -742,7 +751,7 @@
     @Test
     public void testOpenFilePathFirstWriteContentResolver() throws Exception {
         String displayName = "open_file_path_write_content_resolver.jpg";
-        File file = new File(DCIM_DIR, displayName);
+        File file = new File(getDcimDir(), displayName);
 
         try {
             assertThat(file.createNewFile()).isTrue();
@@ -761,7 +770,7 @@
     @Test
     public void testOpenContentResolverFirstWriteContentResolver() throws Exception {
         String displayName = "open_content_resolver_write_content_resolver.jpg";
-        File file = new File(DCIM_DIR, displayName);
+        File file = new File(getDcimDir(), displayName);
 
         try {
             assertThat(file.createNewFile()).isTrue();
@@ -780,7 +789,7 @@
     @Test
     public void testOpenFilePathFirstWriteFilePath() throws Exception {
         String displayName = "open_file_path_write_file_path.jpg";
-        File file = new File(DCIM_DIR, displayName);
+        File file = new File(getDcimDir(), displayName);
 
         try {
             assertThat(file.createNewFile()).isTrue();
@@ -799,7 +808,7 @@
     @Test
     public void testOpenContentResolverFirstWriteFilePath() throws Exception {
         String displayName = "open_content_resolver_write_file_path.jpg";
-        File file = new File(DCIM_DIR, displayName);
+        File file = new File(getDcimDir(), displayName);
 
         try {
             assertThat(file.createNewFile()).isTrue();
@@ -818,7 +827,7 @@
     @Test
     public void testOpenContentResolverWriteOnly() throws Exception {
         String displayName = "open_content_resolver_write_only.jpg";
-        File file = new File(DCIM_DIR, displayName);
+        File file = new File(getDcimDir(), displayName);
 
         try {
             assertThat(file.createNewFile()).isTrue();
@@ -839,7 +848,7 @@
     @Test
     public void testOpenContentResolverDup() throws Exception {
         String displayName = "open_content_resolver_dup.jpg";
-        File file = new File(DCIM_DIR, displayName);
+        File file = new File(getDcimDir(), displayName);
 
         try {
             file.delete();
@@ -865,7 +874,7 @@
     @Test
     public void testOpenContentResolverClose() throws Exception {
         String displayName = "open_content_resolver_close.jpg";
-        File file = new File(DCIM_DIR, displayName);
+        File file = new File(getDcimDir(), displayName);
 
         try {
             byte[] readBuffer = new byte[10];
@@ -897,7 +906,7 @@
     @Test
     public void testContentResolverDelete() throws Exception {
         String displayName = "content_resolver_delete.jpg";
-        File file = new File(DCIM_DIR, displayName);
+        File file = new File(getDcimDir(), displayName);
 
         try {
             assertThat(file.createNewFile()).isTrue();
@@ -915,8 +924,8 @@
     public void testContentResolverUpdate() throws Exception {
         String oldDisplayName = "content_resolver_update_old.jpg";
         String newDisplayName = "content_resolver_update_new.jpg";
-        File oldFile = new File(DCIM_DIR, oldDisplayName);
-        File newFile = new File(DCIM_DIR, newDisplayName);
+        File oldFile = new File(getDcimDir(), oldDisplayName);
+        File newFile = new File(getDcimDir(), newDisplayName);
 
         try {
             assertThat(oldFile.createNewFile()).isTrue();
@@ -936,24 +945,24 @@
 
     @Test
     public void testCreateLowerCaseDeleteUpperCase() throws Exception {
-        File upperCase = new File(DOWNLOAD_DIR, "CREATE_LOWER_DELETE_UPPER");
-        File lowerCase = new File(DOWNLOAD_DIR, "create_lower_delete_upper");
+        File upperCase = new File(getDownloadDir(), "CREATE_LOWER_DELETE_UPPER");
+        File lowerCase = new File(getDownloadDir(), "create_lower_delete_upper");
 
         createDeleteCreate(lowerCase, upperCase);
     }
 
     @Test
     public void testCreateUpperCaseDeleteLowerCase() throws Exception {
-        File upperCase = new File(DOWNLOAD_DIR, "CREATE_UPPER_DELETE_LOWER");
-        File lowerCase = new File(DOWNLOAD_DIR, "create_upper_delete_lower");
+        File upperCase = new File(getDownloadDir(), "CREATE_UPPER_DELETE_LOWER");
+        File lowerCase = new File(getDownloadDir(), "create_upper_delete_lower");
 
         createDeleteCreate(upperCase, lowerCase);
     }
 
     @Test
     public void testCreateMixedCaseDeleteDifferentMixedCase() throws Exception {
-        File mixedCase1 = new File(DOWNLOAD_DIR, "CrEaTe_MiXeD_dElEtE_mIxEd");
-        File mixedCase2 = new File(DOWNLOAD_DIR, "cReAtE_mIxEd_DeLeTe_MiXeD");
+        File mixedCase1 = new File(getDownloadDir(), "CrEaTe_MiXeD_dElEtE_mIxEd");
+        File mixedCase2 = new File(getDownloadDir(), "cReAtE_mIxEd_DeLeTe_MiXeD");
 
         createDeleteCreate(mixedCase1, mixedCase2);
     }
@@ -976,39 +985,39 @@
 
     @Test
     public void testReadStorageInvalidation() throws Exception {
-        testAppOpInvalidation(TEST_APP_C, new File(DCIM_DIR, "read_storage.jpg"),
+        testAppOpInvalidation(TEST_APP_C, new File(getDcimDir(), "read_storage.jpg"),
                 Manifest.permission.READ_EXTERNAL_STORAGE,
                 AppOpsManager.OPSTR_READ_EXTERNAL_STORAGE, /* forWrite */ false);
     }
 
     @Test
     public void testWriteStorageInvalidation() throws Exception {
-        testAppOpInvalidation(TEST_APP_C_LEGACY, new File(DCIM_DIR, "write_storage.jpg"),
+        testAppOpInvalidation(TEST_APP_C_LEGACY, new File(getDcimDir(), "write_storage.jpg"),
                 Manifest.permission.WRITE_EXTERNAL_STORAGE,
                 AppOpsManager.OPSTR_WRITE_EXTERNAL_STORAGE, /* forWrite */ true);
     }
 
     @Test
     public void testManageStorageInvalidation() throws Exception {
-        testAppOpInvalidation(TEST_APP_C, new File(DOWNLOAD_DIR, "manage_storage.pdf"),
+        testAppOpInvalidation(TEST_APP_C, new File(getDownloadDir(), "manage_storage.pdf"),
                 /* permission */ null, OPSTR_MANAGE_EXTERNAL_STORAGE, /* forWrite */ true);
     }
 
     @Test
     public void testWriteImagesInvalidation() throws Exception {
-        testAppOpInvalidation(TEST_APP_C, new File(DCIM_DIR, "write_images.jpg"),
+        testAppOpInvalidation(TEST_APP_C, new File(getDcimDir(), "write_images.jpg"),
                 /* permission */ null, AppOpsManager.OPSTR_WRITE_MEDIA_IMAGES, /* forWrite */ true);
     }
 
     @Test
     public void testWriteVideoInvalidation() throws Exception {
-        testAppOpInvalidation(TEST_APP_C, new File(DCIM_DIR, "write_video.mp4"),
+        testAppOpInvalidation(TEST_APP_C, new File(getDcimDir(), "write_video.mp4"),
                 /* permission */ null, AppOpsManager.OPSTR_WRITE_MEDIA_VIDEO, /* forWrite */ true);
     }
 
     @Test
     public void testAccessMediaLocationInvalidation() throws Exception {
-        File imgFile = new File(DCIM_DIR, "access_media_location.jpg");
+        File imgFile = new File(getDcimDir(), "access_media_location.jpg");
 
         try {
             // Setup image with sensitive data on external storage
@@ -1050,7 +1059,7 @@
 
     @Test
     public void testAppUpdateInvalidation() throws Exception {
-        File file = new File(DCIM_DIR, "app_update.jpg");
+        File file = new File(getDcimDir(), "app_update.jpg");
         try {
             assertThat(file.createNewFile()).isTrue();
 
@@ -1078,7 +1087,7 @@
 
     @Test
     public void testAppReinstallInvalidation() throws Exception {
-        File file = new File(DCIM_DIR, "app_reinstall.jpg");
+        File file = new File(getDcimDir(), "app_reinstall.jpg");
 
         try {
             assertThat(file.createNewFile()).isTrue();
@@ -1142,9 +1151,9 @@
 
     @Test
     public void testSystemGalleryAppHasFullAccessToImages() throws Exception {
-        final File otherAppImageFile = new File(DCIM_DIR, "other_" + IMAGE_FILE_NAME);
-        final File topLevelImageFile = new File(EXTERNAL_STORAGE_DIR, IMAGE_FILE_NAME);
-        final File imageInAnObviouslyWrongPlace = new File(MUSIC_DIR, IMAGE_FILE_NAME);
+        final File otherAppImageFile = new File(getDcimDir(), "other_" + IMAGE_FILE_NAME);
+        final File topLevelImageFile = new File(getExternalStorageDir(), IMAGE_FILE_NAME);
+        final File imageInAnObviouslyWrongPlace = new File(getMusicDir(), IMAGE_FILE_NAME);
 
         try {
             installApp(TEST_APP_A);
@@ -1182,9 +1191,9 @@
 
     @Test
     public void testSystemGalleryAppHasNoFullAccessToAudio() throws Exception {
-        final File otherAppAudioFile = new File(MUSIC_DIR, "other_" + AUDIO_FILE_NAME);
-        final File topLevelAudioFile = new File(EXTERNAL_STORAGE_DIR, AUDIO_FILE_NAME);
-        final File audioInAnObviouslyWrongPlace = new File(PICTURES_DIR, AUDIO_FILE_NAME);
+        final File otherAppAudioFile = new File(getMusicDir(), "other_" + AUDIO_FILE_NAME);
+        final File topLevelAudioFile = new File(getExternalStorageDir(), AUDIO_FILE_NAME);
+        final File audioInAnObviouslyWrongPlace = new File(getPicturesDir(), AUDIO_FILE_NAME);
 
         try {
             installApp(TEST_APP_A);
@@ -1217,11 +1226,11 @@
 
     @Test
     public void testSystemGalleryCanRenameImagesAndVideos() throws Exception {
-        final File otherAppVideoFile = new File(DCIM_DIR, "other_" + VIDEO_FILE_NAME);
-        final File imageFile = new File(PICTURES_DIR, IMAGE_FILE_NAME);
-        final File videoFile = new File(PICTURES_DIR, VIDEO_FILE_NAME);
-        final File topLevelVideoFile = new File(EXTERNAL_STORAGE_DIR, VIDEO_FILE_NAME);
-        final File musicFile = new File(MUSIC_DIR, AUDIO_FILE_NAME);
+        final File otherAppVideoFile = new File(getDcimDir(), "other_" + VIDEO_FILE_NAME);
+        final File imageFile = new File(getPicturesDir(), IMAGE_FILE_NAME);
+        final File videoFile = new File(getPicturesDir(), VIDEO_FILE_NAME);
+        final File topLevelVideoFile = new File(getExternalStorageDir(), VIDEO_FILE_NAME);
+        final File musicFile = new File(getMusicDir(), AUDIO_FILE_NAME);
         try {
             installApp(TEST_APP_A);
             allowAppOpsToUid(Process.myUid(), SYSTEM_GALERY_APPOPS);
@@ -1266,19 +1275,20 @@
      */
     @Test
     public void testRenameFile() throws Exception {
-        final File nonMediaDir = new File(DOWNLOAD_DIR, TEST_DIRECTORY_NAME);
-        final File pdfFile1 = new File(DOWNLOAD_DIR, NONMEDIA_FILE_NAME);
+        final File downloadDir = getDownloadDir();
+        final File nonMediaDir = new File(downloadDir, TEST_DIRECTORY_NAME);
+        final File pdfFile1 = new File(downloadDir, NONMEDIA_FILE_NAME);
         final File pdfFile2 = new File(nonMediaDir, NONMEDIA_FILE_NAME);
-        final File videoFile1 = new File(DCIM_DIR, VIDEO_FILE_NAME);
-        final File videoFile2 = new File(MOVIES_DIR, VIDEO_FILE_NAME);
-        final File videoFile3 = new File(DOWNLOAD_DIR, VIDEO_FILE_NAME);
+        final File videoFile1 = new File(getDcimDir(), VIDEO_FILE_NAME);
+        final File videoFile2 = new File(getMoviesDir(), VIDEO_FILE_NAME);
+        final File videoFile3 = new File(downloadDir, VIDEO_FILE_NAME);
 
         try {
             // Renaming non media file to media directory is not allowed.
             assertThat(pdfFile1.createNewFile()).isTrue();
-            assertCantRenameFile(pdfFile1, new File(DCIM_DIR, NONMEDIA_FILE_NAME));
-            assertCantRenameFile(pdfFile1, new File(MUSIC_DIR, NONMEDIA_FILE_NAME));
-            assertCantRenameFile(pdfFile1, new File(MOVIES_DIR, NONMEDIA_FILE_NAME));
+            assertCantRenameFile(pdfFile1, new File(getDcimDir(), NONMEDIA_FILE_NAME));
+            assertCantRenameFile(pdfFile1, new File(getMusicDir(), NONMEDIA_FILE_NAME));
+            assertCantRenameFile(pdfFile1, new File(getMoviesDir(), NONMEDIA_FILE_NAME));
 
             // Renaming non media files to non media directories is allowed.
             if (!nonMediaDir.exists()) {
@@ -1307,13 +1317,13 @@
      */
     @Test
     public void testRenameFileType() throws Exception {
-        final File pdfFile = new File(DOWNLOAD_DIR, NONMEDIA_FILE_NAME);
-        final File videoFile = new File(DCIM_DIR, VIDEO_FILE_NAME);
+        final File pdfFile = new File(getDownloadDir(), NONMEDIA_FILE_NAME);
+        final File videoFile = new File(getDcimDir(), VIDEO_FILE_NAME);
         try {
             assertThat(pdfFile.createNewFile()).isTrue();
             assertThat(videoFile.exists()).isFalse();
             // Moving pdfFile to DCIM directory is not allowed.
-            assertCantRenameFile(pdfFile, new File(DCIM_DIR, NONMEDIA_FILE_NAME));
+            assertCantRenameFile(pdfFile, new File(getDcimDir(), NONMEDIA_FILE_NAME));
             // However, moving pdfFile to DCIM directory with changing the mime type to video is
             // allowed.
             assertCanRenameFile(pdfFile, videoFile);
@@ -1332,8 +1342,8 @@
      */
     @Test
     public void testRenameAndReplaceFile() throws Exception {
-        final File videoFile1 = new File(DCIM_DIR, VIDEO_FILE_NAME);
-        final File videoFile2 = new File(MOVIES_DIR, VIDEO_FILE_NAME);
+        final File videoFile1 = new File(getDcimDir(), VIDEO_FILE_NAME);
+        final File videoFile2 = new File(getMoviesDir(), VIDEO_FILE_NAME);
         final ContentResolver cr = getContentResolver();
         try {
             assertThat(videoFile1.createNewFile()).isTrue();
@@ -1360,8 +1370,8 @@
      */
     @Test
     public void testRenameFileNotOwned() throws Exception {
-        final File videoFile1 = new File(DCIM_DIR, VIDEO_FILE_NAME);
-        final File videoFile2 = new File(MOVIES_DIR, VIDEO_FILE_NAME);
+        final File videoFile1 = new File(getDcimDir(), VIDEO_FILE_NAME);
+        final File videoFile2 = new File(getMoviesDir(), VIDEO_FILE_NAME);
         try {
             installApp(TEST_APP_A);
             assertThat(createFileAs(TEST_APP_A, videoFile1.getAbsolutePath())).isTrue();
@@ -1385,16 +1395,18 @@
      */
     @Test
     public void testRenameDirectory() throws Exception {
+        final File dcimDir = getDcimDir();
+        final File downloadDir = getDownloadDir();
         final String nonMediaDirectoryName = TEST_DIRECTORY_NAME + "NonMedia";
-        final File nonMediaDirectory = new File(DOWNLOAD_DIR, nonMediaDirectoryName);
+        final File nonMediaDirectory = new File(downloadDir, nonMediaDirectoryName);
         final File pdfFile = new File(nonMediaDirectory, NONMEDIA_FILE_NAME);
 
         final String mediaDirectoryName = TEST_DIRECTORY_NAME + "Media";
-        final File mediaDirectory1 = new File(DCIM_DIR, mediaDirectoryName);
+        final File mediaDirectory1 = new File(dcimDir, mediaDirectoryName);
         final File videoFile1 = new File(mediaDirectory1, VIDEO_FILE_NAME);
-        final File mediaDirectory2 = new File(DOWNLOAD_DIR, mediaDirectoryName);
+        final File mediaDirectory2 = new File(downloadDir, mediaDirectoryName);
         final File videoFile2 = new File(mediaDirectory2, VIDEO_FILE_NAME);
-        final File mediaDirectory3 = new File(MOVIES_DIR, TEST_DIRECTORY_NAME);
+        final File mediaDirectory3 = new File(getMoviesDir(), TEST_DIRECTORY_NAME);
         final File videoFile3 = new File(mediaDirectory3, VIDEO_FILE_NAME);
         final File mediaDirectory4 = new File(mediaDirectory3, mediaDirectoryName);
 
@@ -1404,7 +1416,7 @@
             }
             assertThat(pdfFile.createNewFile()).isTrue();
             // Move directory with pdf file to DCIM directory is not allowed.
-            assertThat(nonMediaDirectory.renameTo(new File(DCIM_DIR, nonMediaDirectoryName)))
+            assertThat(nonMediaDirectory.renameTo(new File(dcimDir, nonMediaDirectoryName)))
                     .isFalse();
 
             if (!mediaDirectory1.exists()) {
@@ -1412,9 +1424,9 @@
             }
             assertThat(videoFile1.createNewFile()).isTrue();
             // Renaming to and from default directories is not allowed.
-            assertThat(mediaDirectory1.renameTo(DCIM_DIR)).isFalse();
+            assertThat(mediaDirectory1.renameTo(dcimDir)).isFalse();
             // Moving top level default directories is not allowed.
-            assertCantRenameDirectory(DOWNLOAD_DIR, new File(DCIM_DIR, TEST_DIRECTORY_NAME), null);
+            assertCantRenameDirectory(downloadDir, new File(dcimDir, TEST_DIRECTORY_NAME), null);
 
             // Moving media directory to Download directory is allowed.
             assertCanRenameDirectory(mediaDirectory1, mediaDirectory2, new File[] {videoFile1},
@@ -1457,8 +1469,8 @@
     @Test
     public void testRenameDirectoryNotOwned() throws Exception {
         final String mediaDirectoryName = TEST_DIRECTORY_NAME + "Media";
-        File mediaDirectory1 = new File(DCIM_DIR, mediaDirectoryName);
-        File mediaDirectory2 = new File(MOVIES_DIR, mediaDirectoryName);
+        File mediaDirectory1 = new File(getDcimDir(), mediaDirectoryName);
+        File mediaDirectory2 = new File(getMoviesDir(), mediaDirectoryName);
         File videoFile = new File(mediaDirectory1, VIDEO_FILE_NAME);
 
         try {
@@ -1486,8 +1498,8 @@
     @Test
     public void testRenameEmptyDirectory() throws Exception {
         final String emptyDirectoryName = TEST_DIRECTORY_NAME + "Media";
-        File emptyDirectoryOldPath = new File(DCIM_DIR, emptyDirectoryName);
-        File emptyDirectoryNewPath = new File(MOVIES_DIR, TEST_DIRECTORY_NAME);
+        File emptyDirectoryOldPath = new File(getDcimDir(), emptyDirectoryName);
+        File emptyDirectoryNewPath = new File(getMoviesDir(), TEST_DIRECTORY_NAME);
         try {
             if (emptyDirectoryOldPath.exists()) {
                 executeShellCommand("rm -r " + emptyDirectoryOldPath.getPath());
@@ -1502,9 +1514,9 @@
 
     @Test
     public void testManageExternalStorageCanCreateFilesAnywhere() throws Exception {
-        final File topLevelPdf = new File(EXTERNAL_STORAGE_DIR, NONMEDIA_FILE_NAME);
-        final File musicFileInMovies = new File(MOVIES_DIR, AUDIO_FILE_NAME);
-        final File imageFileInDcim = new File(DCIM_DIR, IMAGE_FILE_NAME);
+        final File topLevelPdf = new File(getExternalStorageDir(), NONMEDIA_FILE_NAME);
+        final File musicFileInMovies = new File(getMoviesDir(), AUDIO_FILE_NAME);
+        final File imageFileInDcim = new File(getDcimDir(), IMAGE_FILE_NAME);
         try {
             allowAppOpsToUid(Process.myUid(), OPSTR_MANAGE_EXTERNAL_STORAGE);
             // Nothing special about this, anyone can create an image file in DCIM
@@ -1524,7 +1536,7 @@
      */
     @Test
     public void testCanCreateHiddenFile() throws Exception {
-        final File hiddenImageFile = new File(DOWNLOAD_DIR, ".hiddenFile" + IMAGE_FILE_NAME);
+        final File hiddenImageFile = new File(getDownloadDir(), ".hiddenFile" + IMAGE_FILE_NAME);
         try {
             assertThat(hiddenImageFile.createNewFile()).isTrue();
             // Write to hidden file is allowed.
@@ -1535,7 +1547,7 @@
 
             assertNotMediaTypeImage(hiddenImageFile);
 
-            assertDirectoryContains(DOWNLOAD_DIR, hiddenImageFile);
+            assertDirectoryContains(getDownloadDir(), hiddenImageFile);
             assertThat(getFileRowIdFromDatabase(hiddenImageFile)).isNotEqualTo(-1);
 
             // We can delete hidden file
@@ -1552,9 +1564,9 @@
     @Test
     public void testCanRenameHiddenFile() throws Exception {
         final String hiddenFileName = ".hidden" + IMAGE_FILE_NAME;
-        final File hiddenImageFile1 = new File(DCIM_DIR, hiddenFileName);
-        final File hiddenImageFile2 = new File(DOWNLOAD_DIR, hiddenFileName);
-        final File imageFile = new File(DOWNLOAD_DIR, IMAGE_FILE_NAME);
+        final File hiddenImageFile1 = new File(getDcimDir(), hiddenFileName);
+        final File hiddenImageFile2 = new File(getDownloadDir(), hiddenFileName);
+        final File imageFile = new File(getDownloadDir(), IMAGE_FILE_NAME);
         try {
             assertThat(hiddenImageFile1.createNewFile()).isTrue();
             assertCanRenameFile(hiddenImageFile1, hiddenImageFile2);
@@ -1579,9 +1591,9 @@
      */
     @Test
     public void testHiddenDirectory() throws Exception {
-        final File hiddenDir = new File(DOWNLOAD_DIR, ".hidden" + TEST_DIRECTORY_NAME);
+        final File hiddenDir = new File(getDownloadDir(), ".hidden" + TEST_DIRECTORY_NAME);
         final File hiddenImageFile = new File(hiddenDir, IMAGE_FILE_NAME);
-        final File nonHiddenDir = new File(DOWNLOAD_DIR, TEST_DIRECTORY_NAME);
+        final File nonHiddenDir = new File(getDownloadDir(), TEST_DIRECTORY_NAME);
         final File imageFile = new File(nonHiddenDir, IMAGE_FILE_NAME);
         try {
             if (!hiddenDir.exists()) {
@@ -1612,7 +1624,7 @@
      */
     @Test
     public void testHiddenDirectory_nomedia() throws Exception {
-        final File directoryNoMedia = new File(DOWNLOAD_DIR, "nomedia" + TEST_DIRECTORY_NAME);
+        final File directoryNoMedia = new File(getDownloadDir(), "nomedia" + TEST_DIRECTORY_NAME);
         final File noMediaFile = new File(directoryNoMedia, ".nomedia");
         final File imageFile = new File(directoryNoMedia, IMAGE_FILE_NAME);
         final File videoFile = new File(directoryNoMedia, VIDEO_FILE_NAME);
@@ -1651,17 +1663,18 @@
      */
     @Test
     public void testListHiddenFile() throws Exception {
+        final File dcimDir = getDcimDir();
         final String hiddenImageFileName = ".hidden" + IMAGE_FILE_NAME;
-        final File hiddenImageFile = new File(DCIM_DIR, hiddenImageFileName);
+        final File hiddenImageFile = new File(dcimDir, hiddenImageFileName);
         try {
             assertThat(hiddenImageFile.createNewFile()).isTrue();
             assertNotMediaTypeImage(hiddenImageFile);
 
-            assertDirectoryContains(DCIM_DIR, hiddenImageFile);
+            assertDirectoryContains(dcimDir, hiddenImageFile);
 
             installApp(TEST_APP_A, true);
             // TestApp with read permissions can't see the hidden image file created by other app
-            assertThat(listAs(TEST_APP_A, DCIM_DIR.getAbsolutePath()))
+            assertThat(listAs(TEST_APP_A, dcimDir.getAbsolutePath()))
                     .doesNotContain(hiddenImageFileName);
 
             final int testAppUid =
@@ -1669,7 +1682,7 @@
             // FileManager can see the hidden image file created by other app
             try {
                 allowAppOpsToUid(testAppUid, OPSTR_MANAGE_EXTERNAL_STORAGE);
-                assertThat(listAs(TEST_APP_A, DCIM_DIR.getAbsolutePath()))
+                assertThat(listAs(TEST_APP_A, dcimDir.getAbsolutePath()))
                         .contains(hiddenImageFileName);
             } finally {
                 denyAppOpsToUid(testAppUid, OPSTR_MANAGE_EXTERNAL_STORAGE);
@@ -1678,7 +1691,7 @@
             // Gallery can not see the hidden image file created by other app
             try {
                 allowAppOpsToUid(testAppUid, SYSTEM_GALERY_APPOPS);
-                assertThat(listAs(TEST_APP_A, DCIM_DIR.getAbsolutePath()))
+                assertThat(listAs(TEST_APP_A, dcimDir.getAbsolutePath()))
                         .doesNotContain(hiddenImageFileName);
             } finally {
                 denyAppOpsToUid(testAppUid, SYSTEM_GALERY_APPOPS);
@@ -1691,9 +1704,9 @@
 
     @Test
     public void testManageExternalStorageCanDeleteOtherAppsContents() throws Exception {
-        final File otherAppPdf = new File(DOWNLOAD_DIR, "other" + NONMEDIA_FILE_NAME);
-        final File otherAppImage = new File(DCIM_DIR, "other" + IMAGE_FILE_NAME);
-        final File otherAppMusic = new File(MUSIC_DIR, "other" + AUDIO_FILE_NAME);
+        final File otherAppPdf = new File(getDownloadDir(), "other" + NONMEDIA_FILE_NAME);
+        final File otherAppImage = new File(getDcimDir(), "other" + IMAGE_FILE_NAME);
+        final File otherAppMusic = new File(getMusicDir(), "other" + AUDIO_FILE_NAME);
         try {
             installApp(TEST_APP_A);
 
@@ -1725,10 +1738,11 @@
     public void testAccess_file() throws Exception {
         pollForPermission(Manifest.permission.READ_EXTERNAL_STORAGE, /*granted*/ true);
 
-        final File otherAppPdf = new File(DOWNLOAD_DIR, "other-" + NONMEDIA_FILE_NAME);
-        final File otherAppImage = new File(DCIM_DIR, "other-" + IMAGE_FILE_NAME);
-        final File myAppPdf = new File(DOWNLOAD_DIR, "my-" + NONMEDIA_FILE_NAME);
-        final File doesntExistPdf = new File(DOWNLOAD_DIR, "nada-" + NONMEDIA_FILE_NAME);
+        final File downloadDir = getDownloadDir();
+        final File otherAppPdf = new File(downloadDir, "other-" + NONMEDIA_FILE_NAME);
+        final File otherAppImage = new File(getDcimDir(), "other-" + IMAGE_FILE_NAME);
+        final File myAppPdf = new File(downloadDir, "my-" + NONMEDIA_FILE_NAME);
+        final File doesntExistPdf = new File(downloadDir, "nada-" + NONMEDIA_FILE_NAME);
 
         try {
             installApp(TEST_APP_A);
@@ -1761,7 +1775,7 @@
             installApp(TEST_APP_A);
 
             // Let app A create a file in its data dir
-            final File otherAppExternalDataDir = new File(EXTERNAL_FILES_DIR.getPath().replace(
+            final File otherAppExternalDataDir = new File(getExternalFilesDir().getPath().replace(
                     THIS_PACKAGE_NAME, TEST_APP_A.getPackageName()));
             final File otherAppExternalDataSubDir = new File(otherAppExternalDataDir, "subdir");
             final File otherAppExternalDataFile = new File(otherAppExternalDataSubDir, "abc.jpg");
@@ -1786,13 +1800,13 @@
             //
             //            // We can read and write our own app dir, but app A cannot.
             //            assertThat(canReadAndWriteAs(TEST_APP_A,
-            //                    EXTERNAL_FILES_DIR.getAbsolutePath())).isFalse();
-            assertAccess(EXTERNAL_FILES_DIR, true, true, true);
+            //                    getExternalFilesDir().getAbsolutePath())).isFalse();
+            assertAccess(getExternalFilesDir(), true, true, true);
 
-            assertDirectoryAccess(DCIM_DIR, /* exists */ true);
-            assertDirectoryAccess(EXTERNAL_STORAGE_DIR, true);
-            assertDirectoryAccess(new File(EXTERNAL_STORAGE_DIR, "Android"), true);
-            assertDirectoryAccess(new File(EXTERNAL_STORAGE_DIR, "doesnt/exist"), false);
+            assertDirectoryAccess(getDcimDir(), /* exists */ true);
+            assertDirectoryAccess(getExternalStorageDir(), true);
+            assertDirectoryAccess(new File(getExternalStorageDir(), "Android"), true);
+            assertDirectoryAccess(new File(getExternalStorageDir(), "doesnt/exist"), false);
         } finally {
             uninstallApp(TEST_APP_A); // Uninstalling deletes external app dirs
         }
@@ -1800,11 +1814,11 @@
 
     @Test
     public void testManageExternalStorageCanRenameOtherAppsContents() throws Exception {
-        final File otherAppPdf = new File(DOWNLOAD_DIR, "other" + NONMEDIA_FILE_NAME);
-        final File pdf = new File(DOWNLOAD_DIR, NONMEDIA_FILE_NAME);
-        final File pdfInObviouslyWrongPlace = new File(PICTURES_DIR, NONMEDIA_FILE_NAME);
-        final File topLevelPdf = new File(EXTERNAL_STORAGE_DIR, NONMEDIA_FILE_NAME);
-        final File musicFile = new File(MUSIC_DIR, AUDIO_FILE_NAME);
+        final File otherAppPdf = new File(getDownloadDir(), "other" + NONMEDIA_FILE_NAME);
+        final File pdf = new File(getDownloadDir(), NONMEDIA_FILE_NAME);
+        final File pdfInObviouslyWrongPlace = new File(getPicturesDir(), NONMEDIA_FILE_NAME);
+        final File topLevelPdf = new File(getExternalStorageDir(), NONMEDIA_FILE_NAME);
+        final File musicFile = new File(getMusicDir(), AUDIO_FILE_NAME);
         try {
             installApp(TEST_APP_A);
 
@@ -1847,24 +1861,26 @@
 
     @Test
     public void testCanCreateDefaultDirectory() throws Exception {
+        final File podcastsDir = getPodcastsDir();
         try {
-            if (PODCASTS_DIR.exists()) {
+            if (podcastsDir.exists()) {
                 // Apps can't delete top level directories, not even default directories, so we let
                 // shell do the deed for us.
-                executeShellCommand("rm -r " + PODCASTS_DIR);
+                executeShellCommand("rm -r " + podcastsDir);
             }
-            assertThat(PODCASTS_DIR.mkdir()).isTrue();
+            assertThat(podcastsDir.mkdir()).isTrue();
         } finally {
-            executeShellCommand("mkdir " + PODCASTS_DIR);
+            executeShellCommand("mkdir " + podcastsDir);
         }
     }
 
     @Test
     public void testManageExternalStorageReaddir() throws Exception {
-        final File otherAppPdf = new File(DOWNLOAD_DIR, "other" + NONMEDIA_FILE_NAME);
-        final File otherAppImg = new File(DCIM_DIR, "other" + IMAGE_FILE_NAME);
-        final File otherAppMusic = new File(MUSIC_DIR, "other" + AUDIO_FILE_NAME);
-        final File otherTopLevelFile = new File(EXTERNAL_STORAGE_DIR, "other" + NONMEDIA_FILE_NAME);
+        final File otherAppPdf = new File(getDownloadDir(), "other" + NONMEDIA_FILE_NAME);
+        final File otherAppImg = new File(getDcimDir(), "other" + IMAGE_FILE_NAME);
+        final File otherAppMusic = new File(getMusicDir(), "other" + AUDIO_FILE_NAME);
+        final File otherTopLevelFile = new File(getExternalStorageDir(),
+                "other" + NONMEDIA_FILE_NAME);
         try {
             installApp(TEST_APP_A);
             assertCreateFilesAs(TEST_APP_A, otherAppImg, otherAppMusic, otherAppPdf);
@@ -1877,10 +1893,10 @@
             assertDirectoryContains(otherAppImg.getParentFile(), otherAppImg);
             assertDirectoryContains(otherAppMusic.getParentFile(), otherAppMusic);
             // We can list top level files
-            assertDirectoryContains(EXTERNAL_STORAGE_DIR, otherTopLevelFile);
+            assertDirectoryContains(getExternalStorageDir(), otherTopLevelFile);
 
             // We can also list all top level directories
-            assertDirectoryContains(EXTERNAL_STORAGE_DIR, DEFAULT_TOP_LEVEL_DIRS);
+            assertDirectoryContains(getExternalStorageDir(), getDefaultTopLevelDirs());
         } finally {
             denyAppOpsToUid(Process.myUid(), OPSTR_MANAGE_EXTERNAL_STORAGE);
             executeShellCommand("rm " + otherTopLevelFile);
@@ -1891,10 +1907,10 @@
 
     @Test
     public void testManageExternalStorageQueryOtherAppsFile() throws Exception {
-        final File otherAppPdf = new File(DOWNLOAD_DIR, "other" + NONMEDIA_FILE_NAME);
-        final File otherAppImg = new File(DCIM_DIR, "other" + IMAGE_FILE_NAME);
-        final File otherAppMusic = new File(MUSIC_DIR, "other" + AUDIO_FILE_NAME);
-        final File otherHiddenFile = new File(PICTURES_DIR, ".otherHiddenFile.jpg");
+        final File otherAppPdf = new File(getDownloadDir(), "other" + NONMEDIA_FILE_NAME);
+        final File otherAppImg = new File(getDcimDir(), "other" + IMAGE_FILE_NAME);
+        final File otherAppMusic = new File(getMusicDir(), "other" + AUDIO_FILE_NAME);
+        final File otherHiddenFile = new File(getPicturesDir(), ".otherHiddenFile.jpg");
         try {
             installApp(TEST_APP_A);
             assertCreateFilesAs(
@@ -1917,10 +1933,10 @@
 
     @Test
     public void testQueryOtherAppsFiles() throws Exception {
-        final File otherAppPdf = new File(DOWNLOAD_DIR, "other" + NONMEDIA_FILE_NAME);
-        final File otherAppImg = new File(DCIM_DIR, "other" + IMAGE_FILE_NAME);
-        final File otherAppMusic = new File(MUSIC_DIR, "other" + AUDIO_FILE_NAME);
-        final File otherHiddenFile = new File(PICTURES_DIR, ".otherHiddenFile.jpg");
+        final File otherAppPdf = new File(getDownloadDir(), "other" + NONMEDIA_FILE_NAME);
+        final File otherAppImg = new File(getDcimDir(), "other" + IMAGE_FILE_NAME);
+        final File otherAppMusic = new File(getMusicDir(), "other" + AUDIO_FILE_NAME);
+        final File otherHiddenFile = new File(getPicturesDir(), ".otherHiddenFile.jpg");
         try {
             installApp(TEST_APP_A);
             assertCreateFilesAs(
@@ -1940,10 +1956,10 @@
 
     @Test
     public void testSystemGalleryQueryOtherAppsFiles() throws Exception {
-        final File otherAppPdf = new File(DOWNLOAD_DIR, "other" + NONMEDIA_FILE_NAME);
-        final File otherAppImg = new File(DCIM_DIR, "other" + IMAGE_FILE_NAME);
-        final File otherAppMusic = new File(MUSIC_DIR, "other" + AUDIO_FILE_NAME);
-        final File otherHiddenFile = new File(PICTURES_DIR, ".otherHiddenFile.jpg");
+        final File otherAppPdf = new File(getDownloadDir(), "other" + NONMEDIA_FILE_NAME);
+        final File otherAppImg = new File(getDcimDir(), "other" + IMAGE_FILE_NAME);
+        final File otherAppMusic = new File(getMusicDir(), "other" + AUDIO_FILE_NAME);
+        final File otherHiddenFile = new File(getPicturesDir(), ".otherHiddenFile.jpg");
         try {
             installApp(TEST_APP_A);
             assertCreateFilesAs(
@@ -1972,9 +1988,9 @@
      */
     @Test
     public void testSystemGalleryCanRenameImageAndVideoDirs() throws Exception {
-        final File dirInDcim = new File(DCIM_DIR, TEST_DIRECTORY_NAME);
-        final File dirInPictures = new File(PICTURES_DIR, TEST_DIRECTORY_NAME);
-        final File dirInPodcasts = new File(PODCASTS_DIR, TEST_DIRECTORY_NAME);
+        final File dirInDcim = new File(getDcimDir(), TEST_DIRECTORY_NAME);
+        final File dirInPictures = new File(getPicturesDir(), TEST_DIRECTORY_NAME);
+        final File dirInPodcasts = new File(getPodcastsDir(), TEST_DIRECTORY_NAME);
         final File otherAppImageFile1 = new File(dirInDcim, "other_" + IMAGE_FILE_NAME);
         final File otherAppVideoFile1 = new File(dirInDcim, "other_" + VIDEO_FILE_NAME);
         final File otherAppPdfFile1 = new File(dirInDcim, "other_" + NONMEDIA_FILE_NAME);
@@ -2021,7 +2037,7 @@
      */
     @Test
     public void testCreateCanRestoreDeletedRowId() throws Exception {
-        final File imageFile = new File(DCIM_DIR, IMAGE_FILE_NAME);
+        final File imageFile = new File(getDcimDir(), IMAGE_FILE_NAME);
         final ContentResolver cr = getContentResolver();
 
         try {
@@ -2057,8 +2073,8 @@
      */
     @Test
     public void testRenameCanRestoreDeletedRowId() throws Exception {
-        final File imageFile = new File(DCIM_DIR, IMAGE_FILE_NAME);
-        final File temporaryFile = new File(DOWNLOAD_DIR, IMAGE_FILE_NAME + "_.tmp");
+        final File imageFile = new File(getDcimDir(), IMAGE_FILE_NAME);
+        final File temporaryFile = new File(getDownloadDir(), IMAGE_FILE_NAME + "_.tmp");
         final ContentResolver cr = getContentResolver();
 
         try {
@@ -2083,8 +2099,8 @@
 
     @Test
     public void testCantCreateOrRenameFileWithInvalidName() throws Exception {
-        File invalidFile = new File(DOWNLOAD_DIR, "<>");
-        File validFile = new File(DOWNLOAD_DIR, NONMEDIA_FILE_NAME);
+        File invalidFile = new File(getDownloadDir(), "<>");
+        File validFile = new File(getDownloadDir(), NONMEDIA_FILE_NAME);
         try {
             assertThrows(IOException.class, "Operation not permitted",
                     () -> { invalidFile.createNewFile(); });
@@ -2210,8 +2226,8 @@
 
     private static void assertDirectoryAccess(File dir, boolean exists) throws Exception {
         // This util does not handle app data directories.
-        assumeFalse(dir.getAbsolutePath().startsWith(ANDROID_DIR.getAbsolutePath())
-                && !dir.equals(ANDROID_DIR));
+        assumeFalse(dir.getAbsolutePath().startsWith(getAndroidDir().getAbsolutePath())
+                && !dir.equals(getAndroidDir()));
         assertThat(dir.isDirectory()).isEqualTo(exists);
         // For non-app data directories, exists => canRead() and canWrite().
         assertAccess(dir, exists, exists, exists);