1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

Updated UploadFileTest to work with an empty OC server

This commit is contained in:
David A. Velasco 2014-04-15 15:19:10 +02:00
parent b3e3eb74f0
commit 45eecf1ff8
4 changed files with 54 additions and 84 deletions

View File

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View File

@ -77,6 +77,8 @@ public class TestActivity extends Activity {
private static final int BUFFER_SIZE = 1024;
public static final String ASSETS__TEXT_FILE_NAME = "textFile.txt";
public static final String ASSETS__IMAGE_FILE_NAME = "imageFile.png";
public static final String ASSETS__VIDEO_FILE_NAME = "videoFile.MP4";
//private Account mAccount = null;
private OwnCloudClient mClient;

View File

@ -25,48 +25,45 @@
package com.owncloud.android.lib.test_project.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import android.content.res.AssetManager;
import android.os.Environment;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.files.FileUtils;
import com.owncloud.android.lib.test_project.TestActivity;
/**
* Class to test Update File Operation
* @author masensio
* @author David A. Velasco
*
*/
public class UploadFileTest extends ActivityInstrumentationTestCase2<TestActivity> {
/* Files to upload. These files must exists on the device */
private final String mFileToUpload = "fileToUpload.png";
private final String mMimeType = "image/png";
private static final String LOG_TAG = UploadFileTest.class.getCanonicalName();
private final String mFileToUploadWithChunks = "fileToUploadChunks.MP4";
private final String mMimeTypeWithChunks = "video/mp4";
private static final String UPLOAD_PATH =
FileUtils.PATH_SEPARATOR + TestActivity.ASSETS__IMAGE_FILE_NAME;
private final String mFileNotFound = "fileNotFound.png";
private static final String CHUNKED_UPLOAD_PATH =
FileUtils.PATH_SEPARATOR + TestActivity.ASSETS__VIDEO_FILE_NAME;
private final String mStoragePath = "/owncloud/tmp/uploadTest";
private String mPath;
private static final String FILE_NOT_FOUND_PATH =
FileUtils.PATH_SEPARATOR + "fileNotFound.png";
private String mCurrentDate;
private TestActivity mActivity;
private File mFileToUpload, mFileToUploadWithChunks;
private List<String> mUploadedFilesPaths;
public UploadFileTest() {
super(TestActivity.class);
mUploadedFilesPaths = new ArrayList<String>();
}
@Override
@ -74,55 +71,10 @@ public class UploadFileTest extends ActivityInstrumentationTestCase2<TestActivit
super.setUp();
setActivityInitialTouchMode(false);
mActivity = getActivity();
mUploadedFilesPaths.clear();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
mCurrentDate = sdf.format(new Date());
File sdCard = Environment.getExternalStorageDirectory();
mPath = sdCard.getAbsolutePath() + "/" + mStoragePath + mCurrentDate;
//mActivity.createFolder(mPath, true);
copyAssets();
}
/**
* Copy Files to ulpload to SdCard
*/
private void copyAssets() {
AssetManager assetManager = getActivity().getAssets();
String[] files = { mFileToUpload, mFileToUploadWithChunks };
// Folder with contents
File folder = new File(mPath);
folder.mkdirs();
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
File outFile = new File(folder, filename);
out = new FileOutputStream(outFile);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
mFileToUpload = mActivity.extractAsset(TestActivity.ASSETS__IMAGE_FILE_NAME);
mFileToUploadWithChunks = mActivity.extractAsset(TestActivity.ASSETS__VIDEO_FILE_NAME);
}
@ -131,11 +83,12 @@ public class UploadFileTest extends ActivityInstrumentationTestCase2<TestActivit
*/
public void testUploadFile() {
String storagePath = mPath + "/" + mFileToUpload;
//String remotePath = "/uploadTest" + mCurrentDate + "/" + mFileToUpload;
String remotePath = "/" + mFileToUpload;
RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeType);
RemoteOperationResult result = mActivity.uploadFile(
mFileToUpload.getAbsolutePath(),
UPLOAD_PATH,
"image/png"
);
mUploadedFilesPaths.add(UPLOAD_PATH);
assertTrue(result.isSuccess());
}
@ -144,11 +97,12 @@ public class UploadFileTest extends ActivityInstrumentationTestCase2<TestActivit
*/
public void testUploadFileWithChunks() {
String storagePath = mPath + "/" + mFileToUploadWithChunks;
//String remotePath = "/uploadTest" + mCurrentDate + "/" +mFileToUploadWithChunks;
String remotePath = "/" + mFileToUploadWithChunks;
RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeTypeWithChunks);
RemoteOperationResult result = mActivity.uploadFile(
mFileToUploadWithChunks.getAbsolutePath(),
CHUNKED_UPLOAD_PATH,
"video/mp4"
);
mUploadedFilesPaths.add(CHUNKED_UPLOAD_PATH);
assertTrue(result.isSuccess());
}
@ -157,12 +111,26 @@ public class UploadFileTest extends ActivityInstrumentationTestCase2<TestActivit
*/
public void testUploadFileNotFound() {
String storagePath = mPath + "/" + mFileNotFound;
//String remotePath = "/uploadTest" + mCurrentDate + "/" + mFileToUpload;
String remotePath = "/" + mFileNotFound;
RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeType);
RemoteOperationResult result = mActivity.uploadFile(
FILE_NOT_FOUND_PATH,
FILE_NOT_FOUND_PATH,
"image/png"
);
assertFalse(result.isSuccess());
}
@Override
protected void tearDown() throws Exception {
Iterator<String> it = mUploadedFilesPaths.iterator();
RemoteOperationResult removeResult = null;
while (it.hasNext()) {
removeResult = mActivity.removeFile(it.next());
if (!removeResult.isSuccess()) {
Utils.logAndThrow(LOG_TAG, removeResult);
}
}
super.tearDown();
}
}