Эх сурвалжийг харах

Merge pull request #6107 from nextcloud/fixUpload

Fix upload and wrote tests
Tobias Kaminsky 4 жил өмнө
parent
commit
f0da2f41e1

+ 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;
             }
         };

+ 199 - 39
src/androidTest/java/com/owncloud/android/UploadIT.java

@@ -21,35 +21,41 @@
  */
 package com.owncloud.android;
 
-import android.content.ContentResolver;
-
-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.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.Before;
+import org.junit.After;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
 import androidx.test.ext.junit.runners.AndroidJUnit4;
 
+import static junit.framework.TestCase.assertFalse;
+import static junit.framework.TestCase.assertTrue;
+
 /**
  * Tests related to file uploads
  */
 
 @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());
 
     private ConnectivityService connectivityServiceMock = new ConnectivityService() {
         @Override
@@ -81,72 +87,226 @@ public class UploadIT extends AbstractIT {
         }
     };
 
-    @Before
-    public void setUp() {
-        final ContentResolver contentResolver = targetContext.getContentResolver();
-        final UserAccountManager accountManager = UserAccountManagerImpl.fromContext(targetContext);
-        storageManager = new UploadsStorageManager(accountManager, contentResolver);
+    @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(new OCFile("/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(new OCFile("/testUpload/"),
-                                false,
-                                account,
-                                false,
-                                targetContext)
-            .execute(client, getStorageManager());
+    public RemoteOperationResult testUpload(OCUpload ocUpload) {
+        UploadFileOperation newUpload = new UploadFileOperation(
+            uploadsStorageManager,
+            connectivityServiceMock,
+            powerManagementServiceMock,
+            account,
+            null,
+            ocUpload,
+            FileUploader.NameCollisionPolicy.DEFAULT,
+            FileUploader.LOCAL_BEHAVIOUR_COPY,
+            targetContext,
+            false,
+            false
+        );
+        newUpload.addRenameUploadListener(() -> {
+            // dummy
+        });
+
+        newUpload.setRemoteFolderToBeCreated();
+
+        return newUpload.execute(client, getStorageManager());
     }
 
     @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);
+    }
+    @Test
+    public void testUploadOnChargingOnlyButNotCharging() {
+        OCUpload ocUpload = new OCUpload(FileStorageUtils.getSavePath(account.name) + "/empty.txt",
+                                         FOLDER + "notCharging.txt", account.name);
+        ocUpload.setWhileChargingOnly(true);
+
+        UploadFileOperation newUpload = new UploadFileOperation(
+            uploadsStorageManager,
+            connectivityServiceMock,
+            powerManagementServiceMock,
+            account,
+            null,
+            ocUpload,
+            FileUploader.NameCollisionPolicy.DEFAULT,
+            FileUploader.LOCAL_BEHAVIOUR_COPY,
+            targetContext,
+            false,
+            true
+        );
+        newUpload.setRemoteFolderToBeCreated();
+        newUpload.addRenameUploadListener(() -> {
+            // dummy
+        });
+
+        RemoteOperationResult result = newUpload.execute(client, getStorageManager());
+        assertFalse(result.toString(), result.isSuccess());
+    }
+
+    @Test
+    public void testUploadOnChargingOnlyAndCharging() {
+        PowerManagementService powerManagementServiceMock = new PowerManagementService() {
+            @Override
+            public boolean isPowerSavingEnabled() {
+                return false;
+            }
+
+            @Override
+            public boolean isPowerSavingExclusionAvailable() {
+                return false;
+            }
+
+            @NotNull
+            @Override
+            public BatteryStatus getBattery() {
+                return new BatteryStatus(true, 100);
+            }
+        };
+
+        OCUpload ocUpload = new OCUpload(FileStorageUtils.getSavePath(account.name) + "/empty.txt",
+                                         FOLDER + "charging.txt", account.name);
+        ocUpload.setWhileChargingOnly(true);
+
+        UploadFileOperation newUpload = new UploadFileOperation(
+            uploadsStorageManager,
+            connectivityServiceMock,
+            powerManagementServiceMock,
+            account,
+            null,
+            ocUpload,
+            FileUploader.NameCollisionPolicy.DEFAULT,
+            FileUploader.LOCAL_BEHAVIOUR_COPY,
+            targetContext,
+            false,
+            true
+        );
+        newUpload.setRemoteFolderToBeCreated();
+        newUpload.addRenameUploadListener(() -> {
+            // dummy
+        });
+
+        RemoteOperationResult result = newUpload.execute(client, getStorageManager());
+        assertTrue(result.toString(), result.isSuccess());
+    }
+
+    @Test
+    public void testUploadOnWifiOnlyButNoWifi() {
+        ConnectivityService connectivityServiceMock = new ConnectivityService() {
+            @Override
+            public boolean isInternetWalled() {
+                return false;
+            }
+
+            @Override
+            public Connectivity getConnectivity() {
+                return new Connectivity(true, false, false, true);
+            }
+        };
+        OCUpload ocUpload = new OCUpload(FileStorageUtils.getSavePath(account.name) + "/empty.txt",
+                                         FOLDER + "noWifi.txt", account.name);
+        ocUpload.setUseWifiOnly(true);
+
+        UploadFileOperation newUpload = new UploadFileOperation(
+            uploadsStorageManager,
+            connectivityServiceMock,
+            powerManagementServiceMock,
+            account,
+            null,
+            ocUpload,
+            FileUploader.NameCollisionPolicy.DEFAULT,
+            FileUploader.LOCAL_BEHAVIOUR_COPY,
+            targetContext,
+            true,
+            false
+        );
+        newUpload.setRemoteFolderToBeCreated();
+        newUpload.addRenameUploadListener(() -> {
+            // dummy
+        });
+
+        RemoteOperationResult result = newUpload.execute(client, getStorageManager());
+        assertFalse(result.toString(), result.isSuccess());
+    }
+
+    @Test
+    public void testUploadOnWifiOnlyAndWifi() {
+        OCUpload ocUpload = new OCUpload(FileStorageUtils.getSavePath(account.name) + "/empty.txt",
+                                         FOLDER + "wifi.txt", account.name);
+        ocUpload.setWhileChargingOnly(true);
+
+        UploadFileOperation newUpload = new UploadFileOperation(
+            uploadsStorageManager,
+            connectivityServiceMock,
+            powerManagementServiceMock,
+            account,
+            null,
+            ocUpload,
+            FileUploader.NameCollisionPolicy.DEFAULT,
+            FileUploader.LOCAL_BEHAVIOUR_COPY,
+            targetContext,
+            true,
+            false
+        );
+        newUpload.setRemoteFolderToBeCreated();
+        newUpload.addRenameUploadListener(() -> {
+            // dummy
+        });
+
+        RemoteOperationResult result = newUpload.execute(client, getStorageManager());
+        assertTrue(result.toString(), result.isSuccess());
 
         // cleanup
-        new RemoveFileOperation(new OCFile("/testUpload/"),
+        new RemoveFileOperation(getStorageManager().getFileByDecryptedRemotePath(FOLDER),
                                 false,
                                 account,
                                 false,

+ 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());
 

+ 2 - 2
src/main/java/com/owncloud/android/operations/UploadFileOperation.java

@@ -679,14 +679,14 @@ public class UploadFileOperation extends SyncOperation {
 
         // check that connectivity conditions are met and delays the upload otherwise
         Connectivity connectivity = connectivityService.getConnectivity();
-        if (mOnWifiOnly && connectivity.isWifi()) {
+        if (mOnWifiOnly && !connectivity.isWifi()) {
             Log_OC.d(TAG, "Upload delayed until WiFi is available: " + getRemotePath());
             remoteOperationResult = new RemoteOperationResult(ResultCode.DELAYED_FOR_WIFI);
         }
 
         // check if charging conditions are met and delays the upload otherwise
         final BatteryStatus battery = powerManagementService.getBattery();
-        if (mWhileChargingOnly && battery.isCharging()) {
+        if (mWhileChargingOnly && !battery.isCharging()) {
             Log_OC.d(TAG, "Upload delayed until the device is charging: " + getRemotePath());
             remoteOperationResult =  new RemoteOperationResult(ResultCode.DELAYED_FOR_CHARGING);
         }