Add tests for renaming from SHELL

Renaming files/directories from SHELL will gp through MediaProvider and
we will be updating MediaStore database on rename. Added tests to verify
renaming from SHELL works correctly.

Bug: 159565500
Test: atest CtsScopedStorageHostTest#testReanmeFromShell
Change-Id: Ibba3a7a01eeaa56cbbaeed0bb6b8e31c0dd6939d
Merged-In: Ibba3a7a01eeaa56cbbaeed0bb6b8e31c0dd6939d
(cherry picked from commit 85422ebfac963aeea632376c0483101d60112cc2)
diff --git a/hostsidetests/scopedstorage/host/src/android/scopedstorage/cts/host/ScopedStorageHostTest.java b/hostsidetests/scopedstorage/host/src/android/scopedstorage/cts/host/ScopedStorageHostTest.java
index d169f4a..b20342c 100644
--- a/hostsidetests/scopedstorage/host/src/android/scopedstorage/cts/host/ScopedStorageHostTest.java
+++ b/hostsidetests/scopedstorage/host/src/android/scopedstorage/cts/host/ScopedStorageHostTest.java
@@ -20,6 +20,7 @@
 
 import android.platform.test.annotations.AppModeFull;
 
+import com.android.tradefed.device.ITestDevice;
 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
 import com.android.tradefed.testtype.junit4.DeviceTestRunOptions;
@@ -494,6 +495,22 @@
         runDeviceTest("testReadWriteFilesInOtherAppExternalDir");
     }
 
+    @Test
+    public void testRenameFromShell() throws Exception {
+        final ITestDevice device = getDevice();
+        final boolean isAdbRoot = device.isAdbRoot() ? true : false;
+        try {
+            if (isAdbRoot) {
+                device.disableAdbRoot();
+            }
+            runDeviceTest("testRenameFromShell");
+        } finally {
+            if (isAdbRoot) {
+                device.enableAdbRoot();
+            }
+        }
+    }
+
     private void grantPermissions(String... perms) throws Exception {
         for (String perm : perms) {
             executeShellCommand("pm grant android.scopedstorage.cts " + perm);
diff --git a/hostsidetests/scopedstorage/src/android/scopedstorage/cts/ScopedStorageTest.java b/hostsidetests/scopedstorage/src/android/scopedstorage/cts/ScopedStorageTest.java
index 9009169..26f279e 100644
--- a/hostsidetests/scopedstorage/src/android/scopedstorage/cts/ScopedStorageTest.java
+++ b/hostsidetests/scopedstorage/src/android/scopedstorage/cts/ScopedStorageTest.java
@@ -2730,6 +2730,46 @@
         }
     }
 
+    @Test
+    public void testRenameFromShell() throws Exception {
+        final File imageFile = new File(getPicturesDir(), IMAGE_FILE_NAME);
+        final File dir = new File(getMoviesDir(), TEST_DIRECTORY_NAME);
+        final File renamedDir = new File(getMusicDir(), TEST_DIRECTORY_NAME);
+        final File renamedImageFile = new File(dir, IMAGE_FILE_NAME);
+        final File imageFileInRenamedDir = new File(renamedDir, IMAGE_FILE_NAME);
+        try {
+            assertTrue(imageFile.createNewFile());
+            assertThat(getFileRowIdFromDatabase(imageFile)).isNotEqualTo(-1);
+            if (!dir.exists()) {
+                assertThat(dir.mkdir()).isTrue();
+            }
+
+            final String renameFileCommand = String.format("mv %s %s",
+                    imageFile.getAbsolutePath(), renamedImageFile.getAbsolutePath());
+            executeShellCommand(renameFileCommand);
+            assertFalse(imageFile.exists());
+            assertThat(getFileRowIdFromDatabase(imageFile)).isEqualTo(-1);
+            assertTrue(renamedImageFile.exists());
+            assertThat(getFileRowIdFromDatabase(renamedImageFile)).isNotEqualTo(-1);
+
+            final String renameDirectoryCommand = String.format("mv %s %s",
+                    dir.getAbsolutePath(), renamedDir.getAbsolutePath());
+            executeShellCommand(renameDirectoryCommand);
+            assertFalse(dir.exists());
+            assertFalse(renamedImageFile.exists());
+            assertThat(getFileRowIdFromDatabase(renamedImageFile)).isEqualTo(-1);
+            assertTrue(renamedDir.exists());
+            assertTrue(imageFileInRenamedDir.exists());
+            assertThat(getFileRowIdFromDatabase(imageFileInRenamedDir)).isNotEqualTo(-1);
+        } finally {
+            imageFile.delete();
+            renamedImageFile.delete();
+            imageFileInRenamedDir.delete();
+            dir.delete();
+            renamedDir.delete();
+        }
+    }
+
     /**
      * Checks restrictions for opening pending and trashed files by different apps. Assumes that
      * given {@code testApp} is already installed and has READ_EXTERNAL_STORAGE permission. This