Merge "Get "ForFuse" methods some test coverage." into rvc-dev
diff --git a/tests/Android.bp b/tests/Android.bp
index 16cc202..e74d9bb 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -39,6 +39,7 @@
         "androidx.test.rules",
         "guava",
         "mockito-target",
+        "truth-prebuilt",
     ],
 
     certificate: "media",
diff --git a/tests/src/com/android/providers/media/MediaProviderForFuseTest.java b/tests/src/com/android/providers/media/MediaProviderForFuseTest.java
new file mode 100644
index 0000000..127cb35
--- /dev/null
+++ b/tests/src/com/android/providers/media/MediaProviderForFuseTest.java
@@ -0,0 +1,135 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.providers.media;
+
+import android.Manifest;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.pm.PackageManager;
+import android.os.Environment;
+import android.provider.MediaStore;
+
+import androidx.test.InstrumentationRegistry;
+import androidx.test.runner.AndroidJUnit4;
+
+import com.android.providers.media.scan.MediaScannerTest.IsolatedContext;
+
+import com.google.common.truth.Truth;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.io.File;
+import java.util.Arrays;
+
+/**
+ * This class is purely here to convince internal code coverage tools that
+ * {@code FuseDaemonHostTest} is actually covering all of these methods; the
+ * current coverage infrastructure doesn't support host tests yet.
+ */
+@RunWith(AndroidJUnit4.class)
+public class MediaProviderForFuseTest {
+
+    private static Context sIsolatedContext;
+    private static ContentResolver sIsolatedResolver;
+    private static MediaProvider sMediaProvider;
+
+    private static int sTestUid;
+    private static File sTestDir;
+
+    @BeforeClass
+    public static void setUp() throws Exception {
+        InstrumentationRegistry.getInstrumentation().getUiAutomation().adoptShellPermissionIdentity(
+                Manifest.permission.LOG_COMPAT_CHANGE,
+                Manifest.permission.READ_COMPAT_CHANGE_CONFIG,
+                Manifest.permission.UPDATE_APP_OPS_STATS);
+
+        final Context context = InstrumentationRegistry.getTargetContext();
+        sIsolatedContext = new IsolatedContext(context, "modern");
+        sIsolatedResolver = sIsolatedContext.getContentResolver();
+        sMediaProvider = (MediaProvider) sIsolatedResolver
+                .acquireContentProviderClient(MediaStore.AUTHORITY).getLocalContentProvider();
+
+        // Use a random app without any permissions
+        sTestUid = context.getPackageManager().getPackageUid("com.android.egg",
+                PackageManager.MATCH_ALL);
+        sTestDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
+    }
+
+    @AfterClass
+    public static void tearDown() throws Exception {
+        InstrumentationRegistry.getInstrumentation()
+                .getUiAutomation().dropShellPermissionIdentity();
+    }
+
+    @Test
+    public void testTypical() throws Exception {
+        final File file = new File(sTestDir, "test" + System.nanoTime());
+
+        // We can create our file
+        Truth.assertThat(sMediaProvider.insertFileIfNecessaryForFuse(
+                file.getPath(), sTestUid)).isEqualTo(0);
+        Truth.assertThat(Arrays.asList(sMediaProvider.getFilesInDirectoryForFuse(
+                sTestDir.getPath(), sTestUid))).contains(file.getName());
+
+        // Touch on disk so we can rename below
+        file.createNewFile();
+
+        // We can write our file
+        Truth.assertThat(sMediaProvider.isOpenAllowedForFuse(
+                file.getPath(), sTestUid, true)).isEqualTo(0);
+
+        // We should have no redaction
+        Truth.assertThat(sMediaProvider.getRedactionRangesForFuse(
+                file.getPath(), sTestUid)).isEqualTo(new long[0]);
+
+        // We can rename our file
+        final File renamed = new File(sTestDir, "renamed" + System.nanoTime());
+        Truth.assertThat(sMediaProvider.renameForFuse(
+                file.getPath(), renamed.getPath(), sTestUid)).isEqualTo(0);
+        Truth.assertThat(Arrays.asList(sMediaProvider.getFilesInDirectoryForFuse(
+                sTestDir.getPath(), sTestUid))).doesNotContain(file.getName());
+        Truth.assertThat(Arrays.asList(sMediaProvider.getFilesInDirectoryForFuse(
+                sTestDir.getPath(), sTestUid))).contains(renamed.getName());
+
+        // And we can delete it
+        Truth.assertThat(sMediaProvider.deleteFileForFuse(
+                renamed.getPath(), sTestUid)).isEqualTo(0);
+        Truth.assertThat(Arrays.asList(sMediaProvider.getFilesInDirectoryForFuse(
+                sTestDir.getPath(), sTestUid))).doesNotContain(renamed.getName());
+    }
+
+    @Test
+    public void test_scanFileForFuse() throws Exception {
+        final File file = File.createTempFile("test", ".jpg", sTestDir);
+        sMediaProvider.scanFileForFuse(file.getPath());
+    }
+
+    @Test
+    public void test_isOpendirAllowedForFuse() throws Exception {
+        Truth.assertThat(sMediaProvider.isOpendirAllowedForFuse(
+                sTestDir.getPath(), sTestUid)).isEqualTo(0);
+    }
+
+    @Test
+    public void test_isDirectoryCreationOrDeletionAllowedForFuse() throws Exception {
+        Truth.assertThat(sMediaProvider.isDirectoryCreationOrDeletionAllowedForFuse(
+                sTestDir.getPath(), sTestUid, true)).isEqualTo(0);
+    }
+}