From 1a4be987d978136f03b6d1c2a02ec0bbac205932 Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Wed, 20 May 2020 16:43:59 +0200 Subject: [PATCH] Fix upload and wrote tests Signed-off-by: tobiasKaminsky --- .../java/com/owncloud/android/AbstractIT.java | 29 +++--- .../java/com/owncloud/android/UploadIT.java | 91 ++++++++++--------- .../ui/activity/FolderPickerActivityIT.java | 8 +- .../operations/CreateFolderOperation.java | 11 +++ 4 files changed, 77 insertions(+), 62 deletions(-) diff --git a/src/androidTest/java/com/owncloud/android/AbstractIT.java b/src/androidTest/java/com/owncloud/android/AbstractIT.java index de94442c34..89e3c83679 100644 --- a/src/androidTest/java/com/owncloud/android/AbstractIT.java +++ b/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,17 +277,18 @@ 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 BatteryStatus getBattery() { + return new BatteryStatus(); + } + @Override public boolean isPowerSavingEnabled() { return false; @@ -292,11 +298,6 @@ public abstract class AbstractIT { public boolean isPowerSavingExclusionAvailable() { return false; } - - @Override - public boolean isBatteryCharging() { - return false; - } }; UserAccountManager accountManager = UserAccountManagerImpl.fromContext(targetContext); diff --git a/src/androidTest/java/com/owncloud/android/UploadIT.java b/src/androidTest/java/com/owncloud/android/UploadIT.java index a2a6b1032a..46d9b794de 100644 --- a/src/androidTest/java/com/owncloud/android/UploadIT.java +++ b/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()); } } diff --git a/src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityIT.java b/src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityIT.java index 13379e19aa..70f29d2884 100644 --- a/src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityIT.java +++ b/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/"); diff --git a/src/main/java/com/owncloud/android/operations/CreateFolderOperation.java b/src/main/java/com/owncloud/android/operations/CreateFolderOperation.java index 83b8b9be60..4b79019585 100644 --- a/src/main/java/com/owncloud/android/operations/CreateFolderOperation.java +++ b/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());