Browse Source

Merge pull request #5465 from nextcloud/fix/create-folder-crash-autosync

Fixed crash during folder creation (autosync)
Tobias Kaminsky 5 năm trước cách đây
mục cha
commit
c243453168

+ 122 - 0
src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityIT.java

@@ -0,0 +1,122 @@
+package com.owncloud.android.ui.activity;
+
+/*
+ * Nextcloud Android client application
+ *
+ * @author Kilian Périsset
+ * Copyright (C) 2019 Kilian Périsset (Infomaniak Network SA)
+ * Copyright (C) 2019 Nextcloud GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+import com.owncloud.android.datamodel.OCFile;
+
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import androidx.test.filters.LargeTest;
+import androidx.test.rule.ActivityTestRule;
+
+@RunWith(AndroidJUnit4.class)
+@LargeTest
+public class FolderPickerActivityIT {
+    @Rule
+    public ActivityTestRule<FolderPickerActivity> activityRule =
+        new ActivityTestRule<>(FolderPickerActivity.class);
+
+    @Test
+    public void getActivityFile() {
+        // Arrange
+        FolderPickerActivity targetActivity = activityRule.getActivity();
+        OCFile origin = new OCFile("/test/file.test");
+        origin.setRemotePath("/remotePath/test");
+
+        // Act
+        targetActivity.setFile(origin);
+        OCFile target = targetActivity.getFile();
+
+        // Assert
+        Assert.assertEquals(origin, target);
+    }
+
+    @Test
+    public void getParentFolder_isNotRootFolder() {
+        // Arrange
+        FolderPickerActivity targetActivity = activityRule.getActivity();
+        OCFile origin = new OCFile("/test/");
+        origin.setFileId(1);
+        origin.setRemotePath("/test/");
+        origin.setStoragePath("/test/");
+        origin.setFolder();
+
+        // Act
+        targetActivity.setFile(origin);
+        OCFile target = targetActivity.getCurrentFolder();
+
+        // Assert
+        Assert.assertEquals(origin, target);
+    }
+
+    @Test
+    public void getParentFolder_isRootFolder() {
+        // Arrange
+        FolderPickerActivity targetActivity = activityRule.getActivity();
+        OCFile origin = new OCFile("/");
+        origin.setFileId(1);
+        origin.setRemotePath("/");
+        origin.setStoragePath("/");
+        origin.setFolder();
+
+        // Act
+        targetActivity.setFile(origin);
+        OCFile target = targetActivity.getCurrentFolder();
+
+        // Assert
+        Assert.assertEquals(origin, target);
+    }
+
+    @Test
+    public void nullFile() {
+        // Arrange
+        FolderPickerActivity targetActivity = activityRule.getActivity();
+        OCFile rootFolder = targetActivity.getStorageManager().getFileByPath(OCFile.ROOT_PATH);
+
+        // Act
+        targetActivity.setFile(null);
+        OCFile target = targetActivity.getCurrentFolder();
+
+        // Assert
+        Assert.assertEquals(rootFolder, target);
+    }
+
+    @Test
+    public void getParentFolder() {
+        // Arrange
+        FolderPickerActivity targetActivity = activityRule.getActivity();
+        OCFile origin = new OCFile("/test/file.test");
+        origin.setRemotePath("/test/file.test");
+
+        OCFile target = new OCFile("/test/");
+
+        // Act
+        targetActivity.setFile(origin);
+
+        // Assert
+        Assert.assertEquals(origin, target);
+    }
+}

+ 29 - 17
src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java

@@ -39,10 +39,11 @@ import android.view.MenuItem;
 import android.view.View;
 import android.view.View.OnClickListener;
 
+import com.google.android.material.button.MaterialButton;
 import com.nextcloud.client.di.Injectable;
 import com.nextcloud.client.preferences.AppPreferences;
-import com.google.android.material.button.MaterialButton;
 import com.owncloud.android.R;
+import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.lib.common.operations.RemoteOperation;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
@@ -60,6 +61,7 @@ import com.owncloud.android.utils.DisplayUtils;
 import com.owncloud.android.utils.ErrorMessageAdapter;
 import com.owncloud.android.utils.ThemeUtils;
 
+import java.io.File;
 import java.util.ArrayList;
 
 import javax.inject.Inject;
@@ -195,8 +197,7 @@ public class FolderPickerActivity extends FileActivity implements FileFragment.C
     }
 
     /**
-     * Show a text message on screen view for notifying user if content is
-     * loading or folder is empty
+     * Show a text message on screen view for notifying user if content is loading or folder is empty
      */
     private void setBackgroundText() {
         OCFileListFragment listFragment = getListOfFilesFragment();
@@ -249,8 +250,13 @@ public class FolderPickerActivity extends FileActivity implements FileFragment.C
         mSyncInProgress = true;
 
         // perform folder synchronization
-        RemoteOperation refreshFolderOperation = new RefreshFolderOperation(folder, currentSyncTime, false,
-            ignoreETag, getStorageManager(), getAccount(), getApplicationContext());
+        RemoteOperation refreshFolderOperation = new RefreshFolderOperation(folder,
+                                                                            currentSyncTime,
+                                                                            false,
+                                                                            ignoreETag,
+                                                                            getStorageManager(),
+                                                                            getAccount(),
+                                                                            getApplicationContext());
 
         refreshFolderOperation.execute(getAccount(), this, null, null);
         setIndeterminate(true);
@@ -333,16 +339,22 @@ public class FolderPickerActivity extends FileActivity implements FileFragment.C
     }
 
     protected OCFile getCurrentFolder() {
-        OCFile file = getFile();
-        if (file != null) {
-            if (file.isFolder()) {
-                return file;
-            } else if (getStorageManager() != null) {
-                String parentPath = file.getRemotePath().substring(0, file.getRemotePath().lastIndexOf(file.getFileName()));
-                return getStorageManager().getFileByPath(parentPath);
+        OCFile currentFile = getFile();
+        OCFile finalFolder = null;
+        FileDataStorageManager storageManager = getStorageManager();
+
+        // If the file is null, take the root folder to avoid any error in functions depending on this one
+        if (currentFile != null) {
+            if (currentFile.isFolder()) {
+                finalFolder = currentFile;
+            } else if (currentFile.getRemotePath() != null) {
+                String parentPath = new File(currentFile.getRemotePath()).getParent();
+                finalFolder = storageManager.getFileByPath(parentPath);
             }
+        } else {
+            finalFolder = storageManager.getFileByPath(OCFile.ROOT_PATH);
         }
-        return null;
+        return finalFolder;
     }
 
     public void refreshListOfFilesFragment(boolean fromSearch) {
@@ -443,8 +455,7 @@ public class FolderPickerActivity extends FileActivity implements FileFragment.C
 
 
     /**
-     * Updates the view associated to the activity after the finish of an operation trying
-     * to create a new folder.
+     * Updates the view associated to the activity after the finish of an operation trying to create a new folder.
      *
      * @param operation Creation operation performed.
      * @param result    Result of the creation.
@@ -495,8 +506,9 @@ public class FolderPickerActivity extends FileActivity implements FileFragment.C
 
                         if (currentDir == null) {
                             // current folder was removed from the server
-                            DisplayUtils.showSnackMessage(getActivity(), R.string.sync_current_folder_was_removed,
-                                getCurrentFolder().getFileName());
+                            DisplayUtils.showSnackMessage(getActivity(),
+                                                          R.string.sync_current_folder_was_removed,
+                                                          getCurrentFolder().getFileName());
                             browseToRoot();
                         } else {
                             if (currentFile == null && !getFile().isFolder()) {