Add hostside tests for ContentProvider

In order to complete the testing of content provider
allow to write host-side tests.

Bug: 123529934
Test: atest TradefedContentProviderHostTest
Bug: 121327547
Change-Id: I22309f7c4f3de3c2b1428854728666a9c22d41f3
Merged-In: I22309f7c4f3de3c2b1428854728666a9c22d41f3
diff --git a/src/com/android/tradefed/device/NativeDevice.java b/src/com/android/tradefed/device/NativeDevice.java
index d35fc4a..8dcd201 100644
--- a/src/com/android/tradefed/device/NativeDevice.java
+++ b/src/com/android/tradefed/device/NativeDevice.java
@@ -3885,9 +3885,7 @@
         // Default implementation empty on purpose
     }
 
-    /**
-     * {@inheritDoc}
-     */
+    /** {@inheritDoc} */
     @Override
     public void postInvocationTearDown() {
         // Default implementation
diff --git a/util-apps/ContentProvider/hostsidetests/.classpath b/util-apps/ContentProvider/hostsidetests/.classpath
new file mode 100644
index 0000000..60e2e17
--- /dev/null
+++ b/util-apps/ContentProvider/hostsidetests/.classpath
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry combineaccessrules="false" kind="src" path="/ddmlib"/>
+	<classpathentry combineaccessrules="false" kind="src" path="/tradefederation"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/util-apps/ContentProvider/hostsidetests/.project b/util-apps/ContentProvider/hostsidetests/.project
new file mode 100644
index 0000000..07bfc9a
--- /dev/null
+++ b/util-apps/ContentProvider/hostsidetests/.project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>TradefedContentProviderHostTest</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>
diff --git a/util-apps/ContentProvider/hostsidetests/Android.mk b/util-apps/ContentProvider/hostsidetests/Android.mk
new file mode 100644
index 0000000..21f2915
--- /dev/null
+++ b/util-apps/ContentProvider/hostsidetests/Android.mk
@@ -0,0 +1,27 @@
+
+# Copyright (C) 2012 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.
+
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE :=TradefedContentProviderHostTest
+LOCAL_MODULE_TAGS := tests
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+LOCAL_SDK_VERSION := 24
+LOCAL_JAVA_LIBRARIES := tradefed
+LOCAL_STATIC_JAVA_LIBRARIES := junit objenesis
+LOCAL_COMPATIBILITY_SUITE := general-tests
+
+include $(BUILD_HOST_JAVA_LIBRARY)
diff --git a/util-apps/ContentProvider/hostsidetests/AndroidTest.xml b/util-apps/ContentProvider/hostsidetests/AndroidTest.xml
new file mode 100644
index 0000000..744b54d
--- /dev/null
+++ b/util-apps/ContentProvider/hostsidetests/AndroidTest.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2019 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.
+-->
+<configuration description="Config for Tradefed Content Provider host tests">
+    <option name="test-suite-tag" value="tradefed_content_provider" />
+    <test class="com.android.tradefed.testtype.HostTest">
+        <option name="class" value="com.android.tradefed.contentprovider.ContentProviderTest" />
+    </test>
+</configuration>
diff --git a/util-apps/ContentProvider/hostsidetests/src/com/android/tradefed/contentprovider/ContentProviderTest.java b/util-apps/ContentProvider/hostsidetests/src/com/android/tradefed/contentprovider/ContentProviderTest.java
new file mode 100644
index 0000000..e6fe49d
--- /dev/null
+++ b/util-apps/ContentProvider/hostsidetests/src/com/android/tradefed/contentprovider/ContentProviderTest.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2019 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.tradefed.contentprovider;
+
+import static org.junit.Assert.assertTrue;
+
+import com.android.tradefed.device.contentprovider.ContentProviderHandler;
+import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
+import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
+import com.android.tradefed.util.FileUtil;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.io.File;
+
+/** Host tests for the Tradefed Content Provider. */
+@RunWith(DeviceJUnit4ClassRunner.class)
+public class ContentProviderTest extends BaseHostJUnit4Test {
+
+    private ContentProviderHandler mHandler;
+
+    @Before
+    public void setUp() throws Exception {
+        mHandler = new ContentProviderHandler(getDevice());
+        assertTrue(mHandler.setUp());
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        if (mHandler != null) {
+            mHandler.tearDown();
+        }
+    }
+
+    @Test
+    public void testPushFile() throws Exception {
+        File tmpFile = FileUtil.createTempFile("tmpFileToPush", ".txt");
+        try {
+            boolean res = mHandler.pushFile(tmpFile, "/sdcard/" + tmpFile.getName());
+            assertTrue(res);
+            assertTrue(getDevice().doesFileExist("/sdcard/" + tmpFile.getName()));
+        } finally {
+            FileUtil.deleteFile(tmpFile);
+        }
+    }
+}
diff --git a/util-apps/ContentProvider/main/java/android/tradefed/contentprovider/ManagedFileContentProvider.java b/util-apps/ContentProvider/main/java/android/tradefed/contentprovider/ManagedFileContentProvider.java
index 57639f0..cb08e6f 100644
--- a/util-apps/ContentProvider/main/java/android/tradefed/contentprovider/ManagedFileContentProvider.java
+++ b/util-apps/ContentProvider/main/java/android/tradefed/contentprovider/ManagedFileContentProvider.java
@@ -156,10 +156,7 @@
 
     @Override
     public int delete(
-            @NonNull Uri uri,
-            @Nullable String selection,
-            @Nullable String[] selectionArgs) {
-        // TODO: Support deleting a file created via content write
+            @NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
         ContentValues values = mFileTracker.remove(uri);
         if (values == null) {
             return 0;
@@ -192,9 +189,8 @@
     }
 
     @Override
-    public ParcelFileDescriptor openFile(
-            @NonNull Uri uri, @NonNull String mode) throws FileNotFoundException {
-        // TODO: Track the file created via this callback (content write)
+    public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode)
+            throws FileNotFoundException {
         final File file = getFileForUri(uri);
         final int fileMode = modeToMode(mode);
 
@@ -202,6 +198,10 @@
             // If the file is being created, create all its parent directories that don't already
             // exist.
             file.getParentFile().mkdirs();
+            if (!mFileTracker.containsKey(uri)) {
+                // Track the file, if not already tracked.
+                mFileTracker.put(uri, new ContentValues());
+            }
         }
         return ParcelFileDescriptor.open(file, fileMode);
     }