Browse Source

Fix missing update of storage path, so that files/folders were only locally changed, but update not reflected in UI

add IT test

update search lists after renaming

extract string to variable

Remove local/storageManager files after each test run

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
tobiasKaminsky 2 years ago
parent
commit
82fcf2e3a0

+ 13 - 0
app/src/androidTest/java/com/owncloud/android/AbstractIT.java

@@ -44,6 +44,7 @@ import com.owncloud.android.utils.FileStorageUtils;
 import junit.framework.TestCase;
 
 import org.apache.commons.io.FileUtils;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Rule;
@@ -199,6 +200,12 @@ public abstract class AbstractIT {
         androidx.test.espresso.accessibility.AccessibilityChecks.enable().setRunChecksFromRootView(true);
     }
 
+    @After
+    public void after() {
+        fileDataStorageManager.removeLocalFiles(user, fileDataStorageManager);
+        fileDataStorageManager.deleteAllFiles();
+    }
+
     protected FileDataStorageManager getStorageManager() {
         return fileDataStorageManager;
     }
@@ -326,6 +333,12 @@ public abstract class AbstractIT {
         return getStorageManager().getFileByDecryptedRemotePath(remotePath);
     }
 
+    public void uploadFile(File file, String remotePath) {
+        OCUpload ocUpload = new OCUpload(file.getAbsolutePath(), remotePath, account.name);
+
+        uploadOCUpload(ocUpload);
+    }
+
     public void uploadOCUpload(OCUpload ocUpload) {
         ConnectivityService connectivityServiceMock = new ConnectivityService() {
             @Override

+ 5 - 3
app/src/androidTest/java/com/owncloud/android/AbstractOnServerIT.java

@@ -99,7 +99,7 @@ public abstract class AbstractOnServerIT extends AbstractIT {
 
             waitForServer(client, baseUrl);
 
-            deleteAllFiles(); // makes sure that no file/folder is in root
+            deleteAllFilesOnServer(); // makes sure that no file/folder is in root
 
         } catch (OperationCanceledException e) {
             e.printStackTrace();
@@ -114,10 +114,12 @@ public abstract class AbstractOnServerIT extends AbstractIT {
 
     @After
     public void after() {
-        deleteAllFiles();
+        deleteAllFilesOnServer();
+
+        super.after();
     }
 
-    public static void deleteAllFiles() {
+    public static void deleteAllFilesOnServer() {
         RemoteOperationResult result = new ReadFolderRemoteOperation("/").execute(client);
         assertTrue(result.getLogMessage(), result.isSuccess());
 

+ 66 - 2
app/src/androidTest/java/com/owncloud/android/FileIT.java

@@ -4,15 +4,21 @@ import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.operations.CreateFolderOperation;
 import com.owncloud.android.operations.RemoveFileOperation;
+import com.owncloud.android.operations.RenameFileOperation;
+import com.owncloud.android.operations.SynchronizeFolderOperation;
 import com.owncloud.android.operations.common.SyncOperation;
 
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
+import java.io.File;
+import java.io.IOException;
+
 import androidx.test.ext.junit.runners.AndroidJUnit4;
 
 import static junit.framework.TestCase.assertTrue;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
 
 /**
@@ -38,12 +44,14 @@ public class FileIT extends AbstractOnServerIT {
         assertTrue(file.isFolder());
 
         // cleanup
-        new RemoveFileOperation(file, false, user, false, targetContext, getStorageManager()).execute(client);
+        assertTrue(new RemoveFileOperation(file, false, user, false, targetContext, getStorageManager())
+                       .execute(client)
+                       .isSuccess());
     }
 
     @Test
     public void testCreateNonExistingSubFolder() {
-        String path = "/testFolder/1/2/3/4/5/";
+        String path = "/subFolder/1/2/3/4/5/";
         // folder does not exist yet
         assertNull(getStorageManager().getFileByPath(path));
 
@@ -77,4 +85,60 @@ public class FileIT extends AbstractOnServerIT {
         getStorageManager().deleteAllFiles();
         assertEquals(0, getStorageManager().getAllFiles().size());
     }
+
+    @Test
+    public void testRenameFolder() throws IOException {
+        String folderPath = "/testRenameFolder/";
+
+        // create folder
+        createFolder(folderPath);
+
+        // upload file inside it
+        uploadFile(getDummyFile("nonEmpty.txt"), folderPath + "text.txt");
+
+        // sync folder
+        assertTrue(new SynchronizeFolderOperation(targetContext,
+                                                  folderPath,
+                                                  user,
+                                                  System.currentTimeMillis(),
+                                                  fileDataStorageManager)
+                       .execute(targetContext)
+                       .isSuccess());
+
+        // check if file exists
+        String storagePath1 = fileDataStorageManager.getFileByDecryptedRemotePath(folderPath).getStoragePath();
+        assertTrue(new File(storagePath1).exists());
+
+        String storagePath2 = fileDataStorageManager
+            .getFileByDecryptedRemotePath(folderPath + "text.txt")
+            .getStoragePath();
+        assertTrue(new File(storagePath2).exists());
+
+        shortSleep();
+
+        // Rename
+        assertTrue(
+            new RenameFileOperation(folderPath, "test123", fileDataStorageManager)
+                .execute(targetContext)
+                .isSuccess()
+                  );
+
+        // after rename check new location
+        assertTrue(
+            new File(fileDataStorageManager.getFileByDecryptedRemotePath("/test123/").getStoragePath())
+                .exists()
+                  );
+        assertTrue(
+            new File(fileDataStorageManager.getFileByDecryptedRemotePath("/test123/text.txt").getStoragePath())
+                .exists()
+                  );
+
+        // old files do no exist
+        assertNull(fileDataStorageManager.getFileByDecryptedRemotePath(folderPath));
+        assertNull(fileDataStorageManager.getFileByDecryptedRemotePath(folderPath + "text.txt"));
+
+        // local files also do not exist
+        assertFalse(new File(storagePath1).exists());
+        assertFalse(new File(storagePath2).exists());
+    }
 }

+ 3 - 1
app/src/androidTest/java/com/owncloud/android/ui/fragment/FileDetailSharingFragmentIT.kt

@@ -835,8 +835,10 @@ class FileDetailSharingFragmentIT : AbstractIT() {
     }
 
     @After
-    fun after() {
+    override fun after() {
         activity.storageManager.cleanShares()
         activity.finish()
+
+        super.after()
     }
 }

+ 0 - 6
app/src/androidTest/java/com/owncloud/android/ui/fragment/OCFileListFragmentStaticServerIT.kt

@@ -31,7 +31,6 @@ import com.owncloud.android.lib.resources.shares.ShareType
 import com.owncloud.android.lib.resources.shares.ShareeUser
 import com.owncloud.android.utils.MimeType
 import com.owncloud.android.utils.ScreenshotTest
-import org.junit.After
 import org.junit.Assert
 import org.junit.Rule
 import org.junit.Test
@@ -266,11 +265,6 @@ class OCFileListFragmentStaticServerIT : AbstractIT() {
         screenshot(sut)
     }
 
-    @After
-    fun after() {
-        fileDataStorageManager.deleteAllFiles()
-    }
-
     @Test
     @ScreenshotTest
     @Suppress("MagicNumber")

+ 2 - 10
app/src/main/java/com/nextcloud/client/jobs/AccountRemovalWork.kt

@@ -53,10 +53,8 @@ import com.owncloud.android.ui.activity.ContactsPreferenceActivity
 import com.owncloud.android.ui.activity.ManageAccountsActivity
 import com.owncloud.android.ui.events.AccountRemovedEvent
 import com.owncloud.android.utils.EncryptionUtils
-import com.owncloud.android.utils.FileStorageUtils
 import com.owncloud.android.utils.PushUtils
 import org.greenrobot.eventbus.EventBus
-import java.io.File
 
 /**
  * Removes account and all local files
@@ -125,7 +123,8 @@ class AccountRemovalWork(
         }
 
         // remove all files
-        removeFiles(user, storageManager)
+        storageManager.removeLocalFiles(user, storageManager)
+
         // delete all database entries
         storageManager.deleteAllFiles()
 
@@ -201,13 +200,6 @@ class AccountRemovalWork(
         }
     }
 
-    private fun removeFiles(user: User, storageManager: FileDataStorageManager) {
-        val tempDir = File(FileStorageUtils.getTemporalPath(user.accountName))
-        val saveDir = File(FileStorageUtils.getSavePath(user.accountName))
-        FileStorageUtils.deleteRecursively(tempDir, storageManager)
-        FileStorageUtils.deleteRecursively(saveDir, storageManager)
-    }
-
     private fun createClient(user: User): Optional<OwnCloudClient> {
         @Suppress("TooGenericExceptionCaught") // needs migration to newer api to get rid of exceptions
         return try {

+ 15 - 1
app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java

@@ -701,7 +701,14 @@ public class FileDataStorageManager {
                     contentValues.put(
                         ProviderTableMeta.FILE_PATH,
                         targetPath + childFile.getRemotePath().substring(lengthOfOldPath)
-                    );
+                                     );
+
+                    if (!childFile.isEncrypted()) {
+                        contentValues.put(
+                            ProviderTableMeta.FILE_PATH_DECRYPTED,
+                            targetPath + childFile.getRemotePath().substring(lengthOfOldPath)
+                                         );
+                    }
 
                     if (childFile.getStoragePath() != null && childFile.getStoragePath().startsWith(defaultSavePath)) {
                         // update link to downloaded content - but local move is not done here!
@@ -2310,6 +2317,13 @@ public class FileDataStorageManager {
         }
     }
 
+    public void removeLocalFiles(User user, FileDataStorageManager storageManager) {
+        File tempDir = new File(FileStorageUtils.getTemporalPath(user.getAccountName()));
+        File saveDir = new File(FileStorageUtils.getSavePath(user.getAccountName()));
+        FileStorageUtils.deleteRecursively(tempDir, storageManager);
+        FileStorageUtils.deleteRecursively(saveDir, storageManager);
+    }
+
     public List<OCFile> getAllFiles() {
         String selection = ProviderTableMeta.FILE_ACCOUNT_OWNER + "= ? ";
         String[] selectionArgs = new String[]{user.getAccountName()};

+ 4 - 0
app/src/main/java/com/owncloud/android/datamodel/OCFile.java

@@ -406,6 +406,10 @@ public class OCFile implements Parcelable, Comparable<OCFile>, ServerFileInterfa
             localPath = null;
         } else {
             localPath = storage_path.replaceAll("//", "/");
+
+            if (isFolder() && !localPath.endsWith("/")) {
+                localPath = localPath + "/";
+            }
         }
         localUri = null;
         exposedFileUri = null;

+ 1 - 1
app/src/main/java/com/owncloud/android/operations/RenameFileOperation.java

@@ -175,7 +175,7 @@ public class RenameFileOperation extends SyncOperation {
         String tmpFolderName = FileStorageUtils.getTemporalPath("");
         File testFile = new File(tmpFolderName + newName);
         File tmpFolder = testFile.getParentFile();
-        if (! tmpFolder.mkdirs()) {
+        if (!tmpFolder.exists() && !tmpFolder.mkdirs()) {
             Log_OC.e(TAG, "Unable to create parent folder " + tmpFolder.getAbsolutePath());
         }
         if (!tmpFolder.isDirectory()) {

+ 4 - 1
app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java

@@ -1331,7 +1331,7 @@ public class OCFileListFragment extends ExtendedListFragment implements
                     storageManager,
                     onlyOnDevice,
                     mLimitToMimeType
-                );
+                                      );
 
                 OCFile previousDirectory = mFile;
                 mFile = directory;
@@ -1348,6 +1348,9 @@ public class OCFileListFragment extends ExtendedListFragment implements
                     getRecyclerView().scrollToPosition(0);
                 }
             }
+        } else if (isSearchEventSet(searchEvent)) {
+            handleSearchEvent(searchEvent);
+            mRefreshListLayout.setRefreshing(false);
         }
     }