浏览代码

Implemented tests for FolderPickerActivity

Kilian Périsset 5 年之前
父节点
当前提交
389090d3e8

+ 74 - 0
src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityTest.java

@@ -0,0 +1,74 @@
+package com.owncloud.android.ui.activity;
+
+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 FolderPickerActivityTest {
+    @Rule
+    public ActivityTestRule<FolderPickerActivity> activityRule =
+        new ActivityTestRule<>(FolderPickerActivity.class);
+
+    @Test
+    public void getFile_NoDifferenceTest() {
+        // 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_isNotRootFolderTest() {
+        // 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_isRootFolderTest() {
+        // 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);
+    }
+}

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

@@ -331,22 +331,22 @@ public class FolderPickerActivity extends FileActivity implements FileFragment.C
     }
 
     protected OCFile getCurrentFolder() {
-        OCFile currentStateFile = getFile();
-        OCFile finalFolder;
+        OCFile currentFile = getFile();
+        OCFile finalFolder = null;
 
         // If the file is null, take the root folder to avoid any error in functions depending on this one
-        if (currentStateFile == null) {
-            finalFolder = getStorageManager().getFileByPath(OCFile.ROOT_PATH);
-        } else {
-            if (currentStateFile.isFolder()) {
-                finalFolder = currentStateFile;
-            } else {
-                String parentPath = currentStateFile
+        if (currentFile != null) {
+            if (currentFile.isFolder()) {
+                finalFolder = currentFile;
+            } else if(currentFile.getRemotePath() != null) {
+                String parentPath = currentFile
                         .getRemotePath()
-                        .substring(0, currentStateFile.getRemotePath()
-                        .lastIndexOf(currentStateFile.getFileName()));
+                        .substring(0, currentFile.getRemotePath()
+                        .lastIndexOf(currentFile.getFileName()));
                 finalFolder = getStorageManager().getFileByPath(parentPath);
             }
+        } else {
+            finalFolder = getStorageManager().getFileByPath(OCFile.ROOT_PATH);
         }
         return finalFolder;
     }