Browse Source

Fix upload and wrote tests

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
tobiasKaminsky 4 years ago
parent
commit
1a4be987d9

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

@@ -10,11 +10,12 @@ import android.content.Context;
 import android.net.Uri;
 import android.os.Bundle;
 
-import com.evernote.android.job.JobRequest;
 import com.facebook.testing.screenshot.Screenshot;
 import com.nextcloud.client.account.UserAccountManager;
 import com.nextcloud.client.account.UserAccountManagerImpl;
+import com.nextcloud.client.device.BatteryStatus;
 import com.nextcloud.client.device.PowerManagementService;
+import com.nextcloud.client.network.Connectivity;
 import com.nextcloud.client.network.ConnectivityService;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
@@ -38,6 +39,7 @@ import junit.framework.TestCase;
 import org.apache.commons.httpclient.HttpStatus;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.io.FileUtils;
+import org.jetbrains.annotations.NotNull;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.BeforeClass;
@@ -74,6 +76,9 @@ public abstract class AbstractIT {
 
     private Activity currentActivity;
 
+    protected FileDataStorageManager fileDataStorageManager =
+        new FileDataStorageManager(account, targetContext.getContentResolver());
+
     @BeforeClass
     public static void beforeAll() {
         try {
@@ -152,7 +157,7 @@ public abstract class AbstractIT {
 
 
     protected FileDataStorageManager getStorageManager() {
-        return new FileDataStorageManager(account, targetContext.getContentResolver());
+        return fileDataStorageManager;
     }
 
     protected Account[] getAllAccounts() {
@@ -272,29 +277,25 @@ public abstract class AbstractIT {
             }
 
             @Override
-            public boolean isOnlineWithWifi() {
-                return true;
-            }
-
-            @Override
-            public JobRequest.NetworkType getActiveNetworkType() {
-                return JobRequest.NetworkType.ANY;
+            public Connectivity getConnectivity() {
+                return Connectivity.CONNECTED_WIFI;
             }
         };
 
         PowerManagementService powerManagementServiceMock = new PowerManagementService() {
+            @NotNull
             @Override
-            public boolean isPowerSavingEnabled() {
-                return false;
+            public BatteryStatus getBattery() {
+                return new BatteryStatus();
             }
 
             @Override
-            public boolean isPowerSavingExclusionAvailable() {
+            public boolean isPowerSavingEnabled() {
                 return false;
             }
 
             @Override
-            public boolean isBatteryCharging() {
+            public boolean isPowerSavingExclusionAvailable() {
                 return false;
             }
         };

+ 47 - 44
src/androidTest/java/com/owncloud/android/UploadIT.java

@@ -27,12 +27,16 @@ import com.nextcloud.client.device.PowerManagementService;
 import com.nextcloud.client.network.Connectivity;
 import com.nextcloud.client.network.ConnectivityService;
 import com.owncloud.android.datamodel.UploadsStorageManager;
-import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.db.OCUpload;
+import com.owncloud.android.files.services.FileUploader;
+import com.owncloud.android.lib.common.operations.RemoteOperationResult;
+import com.owncloud.android.operations.RefreshFolderOperation;
 import com.owncloud.android.operations.RemoveFileOperation;
+import com.owncloud.android.operations.UploadFileOperation;
 import com.owncloud.android.utils.FileStorageUtils;
 
 import org.jetbrains.annotations.NotNull;
+import org.junit.After;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
@@ -47,8 +51,9 @@ import static junit.framework.TestCase.assertTrue;
 
 @RunWith(AndroidJUnit4.class)
 public class UploadIT extends AbstractIT {
+    private static final String FOLDER = "/testUpload/";
 
-    private UploadsStorageManager storageManager =
+    private UploadsStorageManager uploadsStorageManager =
         new UploadsStorageManager(UserAccountManagerImpl.fromContext(targetContext),
                                   targetContext.getContentResolver());
 
@@ -82,51 +87,57 @@ public class UploadIT extends AbstractIT {
         }
     };
 
+    @After
+    public void after() {
+        RemoteOperationResult result = new RefreshFolderOperation(getStorageManager().getFileByPath("/"),
+                                                                  System.currentTimeMillis() / 1000L,
+                                                                  false,
+                                                                  true,
+                                                                  getStorageManager(),
+                                                                  account,
+                                                                  targetContext)
+            .execute(client);
+
+        // cleanup only if folder exists
+        if (result.isSuccess() && getStorageManager().getFileByDecryptedRemotePath(FOLDER) != null) {
+            new RemoveFileOperation(getStorageManager().getFileByDecryptedRemotePath(FOLDER),
+                                    false,
+                                    account,
+                                    false,
+                                    targetContext)
+                .execute(client, getStorageManager());
+        }
+    }
+
     @Test
     public void testEmptyUpload() {
         OCUpload ocUpload = new OCUpload(FileStorageUtils.getSavePath(account.name) + "/empty.txt",
-                                         "/testUpload/empty.txt",
+                                         FOLDER + "empty.txt",
                                          account.name);
 
         uploadOCUpload(ocUpload);
-
-        // cleanup
-        new RemoveFileOperation(new OCFile("/testUpload/"),
-                                false,
-                                account,
-                                false,
-                                targetContext)
-            .execute(client, getStorageManager());
     }
 
     @Test
     public void testNonEmptyUpload() {
         OCUpload ocUpload = new OCUpload(FileStorageUtils.getSavePath(account.name) + "/nonEmpty.txt",
-                                         "/testUpload/nonEmpty.txt",
+                                         FOLDER + "nonEmpty.txt",
                                          account.name);
 
         uploadOCUpload(ocUpload);
-
-        // cleanup
-        new RemoveFileOperation("/testUpload/", false, account, false, targetContext)
-            .execute(client, getStorageManager());
     }
 
     @Test
     public void testChunkedUpload() {
         OCUpload ocUpload = new OCUpload(FileStorageUtils.getSavePath(account.name) + "/chunkedFile.txt",
-                                         "/testUpload/chunkedFile.txt", account.name);
+                                         FOLDER + "chunkedFile.txt", account.name);
 
         uploadOCUpload(ocUpload);
-
-        // cleanup
-        new RemoveFileOperation("/testUpload/", false, account, false, targetContext)
-            .execute(client, getStorageManager());
     }
 
     public RemoteOperationResult testUpload(OCUpload ocUpload) {
         UploadFileOperation newUpload = new UploadFileOperation(
-            storageManager,
+            uploadsStorageManager,
             connectivityServiceMock,
             powerManagementServiceMock,
             account,
@@ -150,26 +161,18 @@ public class UploadIT extends AbstractIT {
     @Test
     public void testUploadInNonExistingFolder() {
         OCUpload ocUpload = new OCUpload(FileStorageUtils.getSavePath(account.name) + "/empty.txt",
-                                         "/testUpload/2/3/4/1.txt", account.name);
+                                         FOLDER + "2/3/4/1.txt", account.name);
 
         uploadOCUpload(ocUpload);
-
-        // cleanup
-        new RemoveFileOperation(new OCFile("/testUpload/"),
-                                false,
-                                account,
-                                false,
-                                targetContext)
-            .execute(client, getStorageManager());
     }
     @Test
     public void testUploadOnChargingOnlyButNotCharging() {
         OCUpload ocUpload = new OCUpload(FileStorageUtils.getSavePath(account.name) + "/empty.txt",
-                                         "/testUpload/notCharging.txt", account.name);
+                                         FOLDER + "notCharging.txt", account.name);
         ocUpload.setWhileChargingOnly(true);
 
         UploadFileOperation newUpload = new UploadFileOperation(
-            storageManager,
+            uploadsStorageManager,
             connectivityServiceMock,
             powerManagementServiceMock,
             account,
@@ -211,11 +214,11 @@ public class UploadIT extends AbstractIT {
         };
 
         OCUpload ocUpload = new OCUpload(FileStorageUtils.getSavePath(account.name) + "/empty.txt",
-                                         "/testUpload/charging.txt", account.name);
+                                         FOLDER + "charging.txt", account.name);
         ocUpload.setWhileChargingOnly(true);
 
         UploadFileOperation newUpload = new UploadFileOperation(
-            storageManager,
+            uploadsStorageManager,
             connectivityServiceMock,
             powerManagementServiceMock,
             account,
@@ -234,10 +237,6 @@ public class UploadIT extends AbstractIT {
 
         RemoteOperationResult result = newUpload.execute(client, getStorageManager());
         assertTrue(result.toString(), result.isSuccess());
-
-        // cleanup
-        new RemoveFileOperation("/testUpload/", false, account, false, targetContext)
-            .execute(client, getStorageManager());
     }
 
     @Test
@@ -254,11 +253,11 @@ public class UploadIT extends AbstractIT {
             }
         };
         OCUpload ocUpload = new OCUpload(FileStorageUtils.getSavePath(account.name) + "/empty.txt",
-                                         "/testUpload/noWifi.txt", account.name);
+                                         FOLDER + "noWifi.txt", account.name);
         ocUpload.setUseWifiOnly(true);
 
         UploadFileOperation newUpload = new UploadFileOperation(
-            storageManager,
+            uploadsStorageManager,
             connectivityServiceMock,
             powerManagementServiceMock,
             account,
@@ -282,11 +281,11 @@ public class UploadIT extends AbstractIT {
     @Test
     public void testUploadOnWifiOnlyAndWifi() {
         OCUpload ocUpload = new OCUpload(FileStorageUtils.getSavePath(account.name) + "/empty.txt",
-                                         "/testUpload/wifi.txt", account.name);
+                                         FOLDER + "wifi.txt", account.name);
         ocUpload.setWhileChargingOnly(true);
 
         UploadFileOperation newUpload = new UploadFileOperation(
-            storageManager,
+            uploadsStorageManager,
             connectivityServiceMock,
             powerManagementServiceMock,
             account,
@@ -307,7 +306,11 @@ public class UploadIT extends AbstractIT {
         assertTrue(result.toString(), result.isSuccess());
 
         // cleanup
-        new RemoveFileOperation("/testUpload/", false, account, false, targetContext)
+        new RemoveFileOperation(getStorageManager().getFileByDecryptedRemotePath(FOLDER),
+                                false,
+                                account,
+                                false,
+                                targetContext)
             .execute(client, getStorageManager());
     }
 }

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

@@ -44,7 +44,7 @@ public class FolderPickerActivityIT {
         // Arrange
         FolderPickerActivity targetActivity = activityRule.getActivity();
         OCFile origin = new OCFile("/test/file.test");
-        origin.setEncryptedRemotePath("/remotePath/test");
+        origin.setRemotePath("/remotePath/test");
 
         // Act
         targetActivity.setFile(origin);
@@ -60,7 +60,7 @@ public class FolderPickerActivityIT {
         FolderPickerActivity targetActivity = activityRule.getActivity();
         OCFile origin = new OCFile("/test/");
         origin.setFileId(1);
-        origin.setEncryptedRemotePath("/test/");
+        origin.setRemotePath("/test/");
         origin.setStoragePath("/test/");
         origin.setFolder();
 
@@ -78,7 +78,7 @@ public class FolderPickerActivityIT {
         FolderPickerActivity targetActivity = activityRule.getActivity();
         OCFile origin = new OCFile("/");
         origin.setFileId(1);
-        origin.setEncryptedRemotePath("/");
+        origin.setRemotePath("/");
         origin.setStoragePath("/");
         origin.setFolder();
 
@@ -109,7 +109,7 @@ public class FolderPickerActivityIT {
         // Arrange
         FolderPickerActivity targetActivity = activityRule.getActivity();
         OCFile origin = new OCFile("/test/file.test");
-        origin.setEncryptedRemotePath("/test/file.test");
+        origin.setRemotePath("/test/file.test");
 
         OCFile target = new OCFile("/test/");
 

+ 11 - 0
src/main/java/com/owncloud/android/operations/CreateFolderOperation.java

@@ -83,6 +83,17 @@ public class CreateFolderOperation extends SyncOperation implements OnRemoteOper
 
         OCFile parent = getStorageManager().getFileByDecryptedRemotePath(remoteParentPath);
 
+        String tempRemoteParentPath = remoteParentPath;
+        while (parent == null) {
+            tempRemoteParentPath = new File(tempRemoteParentPath).getParent();
+
+            if (!tempRemoteParentPath.endsWith(OCFile.PATH_SEPARATOR)) {
+                tempRemoteParentPath = tempRemoteParentPath + OCFile.PATH_SEPARATOR;
+            }
+
+            parent = getStorageManager().getFileByDecryptedRemotePath(tempRemoteParentPath);
+        }
+
         // check if any parent is encrypted
         boolean encryptedAncestor = FileStorageUtils.checkEncryptionStatus(parent, getStorageManager());