Merge "Create default dirs before running FuseDaemonHostTest" into rvc-dev
diff --git a/tests/jni/FuseDaemonTest/host/src/com/android/tests/fused/host/FuseDaemonHostTest.java b/tests/jni/FuseDaemonTest/host/src/com/android/tests/fused/host/FuseDaemonHostTest.java
index 9d8a1a0..79cac4f 100644
--- a/tests/jni/FuseDaemonTest/host/src/com/android/tests/fused/host/FuseDaemonHostTest.java
+++ b/tests/jni/FuseDaemonTest/host/src/com/android/tests/fused/host/FuseDaemonHostTest.java
@@ -33,6 +33,9 @@
  */
 @RunWith(DeviceJUnit4ClassRunner.class)
 public class FuseDaemonHostTest extends BaseHostJUnit4Test {
+
+    private boolean isExternalStorageSetup = false;
+
     /**
      * Runs the given phase of FilePathAccessTest by calling into the device.
      * Throws an exception if the test phase fails.
@@ -47,8 +50,16 @@
         return getDevice().executeShellCommand(cmd);
     }
 
+    private void setupExternalStorage() throws Exception {
+        if (!isExternalStorageSetup) {
+            runDeviceTest("setupExternalStorage");
+            isExternalStorageSetup = true;
+        }
+    }
+
     @Before
     public void setup() throws Exception {
+        setupExternalStorage();
         executeShellCommand("mkdir /sdcard/Android/data/com.android.shell -m 2770");
         executeShellCommand("mkdir /sdcard/Android/data/com.android.shell/files -m 2770");
     }
diff --git a/tests/jni/FuseDaemonTest/libs/FuseDaemonTestLib/src/com/android/tests/fused/lib/TestUtils.java b/tests/jni/FuseDaemonTest/libs/FuseDaemonTestLib/src/com/android/tests/fused/lib/TestUtils.java
index 97e39d4..db60c18 100644
--- a/tests/jni/FuseDaemonTest/libs/FuseDaemonTestLib/src/com/android/tests/fused/lib/TestUtils.java
+++ b/tests/jni/FuseDaemonTest/libs/FuseDaemonTestLib/src/com/android/tests/fused/lib/TestUtils.java
@@ -90,10 +90,53 @@
     public static final byte[] BYTES_DATA1 = STR_DATA1.getBytes();
     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 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 ANDROID_DATA_DIR = new File(EXTERNAL_STORAGE_DIR, "Android/data");
+    public static final File ANDROID_MEDIA_DIR = new File(EXTERNAL_STORAGE_DIR, "Android/media");
+
     private static final long POLLING_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(10);
     private static final long POLLING_SLEEP_MILLIS = 100;
 
     /**
+     * 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
+     * assumptions about their existence.
+     */
+    public static void setupDefaultDirectories() {
+        for (File dir : new File [] { ALARMS_DIR, AUDIOBOOKS_DIR, DCIM_DIR,
+                DOCUMENTS_DIR, DOWNLOAD_DIR, MUSIC_DIR, MOVIES_DIR, NOTIFICATIONS_DIR,
+                PICTURES_DIR, PODCASTS_DIR, RINGTONES_DIR}) {
+            dir.mkdir();
+        }
+    }
+
+    /**
      * Grants {@link Manifest.permission#GRANT_RUNTIME_PERMISSIONS} to the given package.
      */
     public static void grantPermission(String packageName, String permission) {
diff --git a/tests/jni/FuseDaemonTest/src/com/android/tests/fused/FilePathAccessTest.java b/tests/jni/FuseDaemonTest/src/com/android/tests/fused/FilePathAccessTest.java
index b2a8941..80df24f 100644
--- a/tests/jni/FuseDaemonTest/src/com/android/tests/fused/FilePathAccessTest.java
+++ b/tests/jni/FuseDaemonTest/src/com/android/tests/fused/FilePathAccessTest.java
@@ -56,10 +56,24 @@
 import static com.android.tests.fused.lib.TestUtils.openWithMediaProvider;
 import static com.android.tests.fused.lib.TestUtils.readExifMetadataFromTestApp;
 import static com.android.tests.fused.lib.TestUtils.revokePermission;
+import static com.android.tests.fused.lib.TestUtils.setupDefaultDirectories;
 import static com.android.tests.fused.lib.TestUtils.uninstallApp;
 import static com.android.tests.fused.lib.TestUtils.uninstallAppNoThrow;
 import static com.android.tests.fused.lib.TestUtils.updateDisplayNameWithMediaProvider;
 import static com.android.tests.fused.lib.TestUtils.pollForExternalStorageState;
+import static com.android.tests.fused.lib.TestUtils.ALARMS_DIR;
+import static com.android.tests.fused.lib.TestUtils.AUDIOBOOKS_DIR;
+import static com.android.tests.fused.lib.TestUtils.DCIM_DIR;
+import static com.android.tests.fused.lib.TestUtils.DOCUMENTS_DIR;
+import static com.android.tests.fused.lib.TestUtils.DOWNLOAD_DIR;
+import static com.android.tests.fused.lib.TestUtils.MUSIC_DIR;
+import static com.android.tests.fused.lib.TestUtils.MOVIES_DIR;
+import static com.android.tests.fused.lib.TestUtils.NOTIFICATIONS_DIR;
+import static com.android.tests.fused.lib.TestUtils.PICTURES_DIR;
+import static com.android.tests.fused.lib.TestUtils.PODCASTS_DIR;
+import static com.android.tests.fused.lib.TestUtils.RINGTONES_DIR;
+import static com.android.tests.fused.lib.TestUtils.ANDROID_DATA_DIR;
+import static com.android.tests.fused.lib.TestUtils.ANDROID_MEDIA_DIR;
 
 import static com.google.common.truth.Truth.assertThat;
 
@@ -110,24 +124,6 @@
 
     static final File EXTERNAL_STORAGE_DIR = Environment.getExternalStorageDirectory();
 
-    // Default top-level directories
-    static final File ALARMS_DIR = new File(EXTERNAL_STORAGE_DIR, Environment.DIRECTORY_ALARMS);
-    static final File AUDIOBOOKS_DIR = new File(EXTERNAL_STORAGE_DIR,
-            Environment.DIRECTORY_AUDIOBOOKS);
-    static final File DCIM_DIR = new File(EXTERNAL_STORAGE_DIR, Environment.DIRECTORY_DCIM);
-    static final File DOCUMENTS_DIR = new File(EXTERNAL_STORAGE_DIR,
-            Environment.DIRECTORY_DOCUMENTS);
-    static final File DOWNLOAD_DIR = new File(EXTERNAL_STORAGE_DIR,
-            Environment.DIRECTORY_DOWNLOADS);
-    static final File MUSIC_DIR = new File(EXTERNAL_STORAGE_DIR, Environment.DIRECTORY_MUSIC);
-    static final File MOVIES_DIR = new File(EXTERNAL_STORAGE_DIR, Environment.DIRECTORY_MOVIES);
-    static final File NOTIFICATIONS_DIR = new File(EXTERNAL_STORAGE_DIR, Environment.DIRECTORY_NOTIFICATIONS);
-    static final File PICTURES_DIR = new File(EXTERNAL_STORAGE_DIR, Environment.DIRECTORY_PICTURES);
-    static final File PODCASTS_DIR = new File(EXTERNAL_STORAGE_DIR, Environment.DIRECTORY_PODCASTS);
-    static final File RINGTONES_DIR = new File(EXTERNAL_STORAGE_DIR, Environment.DIRECTORY_RINGTONES);
-
-    static final File ANDROID_DATA_DIR = new File(EXTERNAL_STORAGE_DIR, "Android/data");
-    static final File ANDROID_MEDIA_DIR = new File(EXTERNAL_STORAGE_DIR, "Android/media");
     static final String TEST_DIRECTORY_NAME = "FilePathAccessTestDirectory";
 
     static final File EXTERNAL_FILES_DIR = getContext().getExternalFilesDir(null);
@@ -154,7 +150,7 @@
             permissionToOp(Manifest.permission.MANAGE_EXTERNAL_STORAGE);
 
     @Before
-    public void setUp() throws Exception {
+    public void setup() throws Exception {
         // skips all test cases if FUSE is not active.
         assumeTrue(getBoolean("persist.sys.fuse", false));
 
@@ -163,6 +159,14 @@
     }
 
     /**
+     * This method needs to be called once before running the whole test.
+     */
+    @Test
+    public void setupExternalStorage() {
+        setupDefaultDirectories();
+    }
+
+    /**
      * Test that we enforce certain media types can only be created in certain directories.
      */
     @Test