Fix upload and wrote tests

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
This commit is contained in:
tobiasKaminsky 2020-05-20 16:43:59 +02:00
parent 89c9838442
commit 1a4be987d9
No known key found for this signature in database
GPG key ID: 0E00D4D47D0C5AF7
4 changed files with 77 additions and 62 deletions

View file

@ -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);

View file

@ -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());
}
}

View file

@ -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/");

View file

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