1 /* ownCloud Android Library is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 package com.owncloud.android.lib.test_project.test;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.OutputStream;
32 import java.text.SimpleDateFormat;
33 import java.util.Date;
35 import android.content.res.AssetManager;
36 import android.os.Environment;
37 import android.test.ActivityInstrumentationTestCase2;
38 import android.util.Log;
40 import com.owncloud.android.lib.operations.common.RemoteOperationResult;
41 import com.owncloud.android.lib.test_project.TestActivity;
44 * Class to test Update File Operation
49 public class UploadFileTest extends ActivityInstrumentationTestCase2<TestActivity> {
51 /* Files to upload. These files must exists on the device */
52 private final String mFileToUpload = "fileToUpload.png";
53 private final String mMimeType = "image/png";
55 private final String mFileToUploadWithChunks = "fileToUploadChunks.MP4";
56 private final String mMimeTypeWithChunks = "video/mp4";
58 private final String mFileNotFound = "fileNotFound.png";
60 private final String mStoragePath = "/owncloud/tmp/uploadTest";
63 private String mCurrentDate;
65 private TestActivity mActivity;
67 public UploadFileTest() {
68 super(TestActivity.class);
73 protected void setUp() throws Exception {
75 setActivityInitialTouchMode(false);
76 mActivity = getActivity();
78 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
79 mCurrentDate = sdf.format(new Date());
81 File sdCard = Environment.getExternalStorageDirectory();
82 mPath = sdCard.getAbsolutePath() + "/" + mStoragePath + mCurrentDate;
84 //mActivity.createFolder(mPath, true);
90 * Copy Files to ulpload to SdCard
92 private void copyAssets() {
93 AssetManager assetManager = getActivity().getAssets();
94 String[] files = { mFileToUpload, mFileToUploadWithChunks };
96 // Folder with contents
97 File folder = new File(mPath);
101 for(String filename : files) {
102 InputStream in = null;
103 OutputStream out = null;
105 in = assetManager.open(filename);
106 File outFile = new File(folder, filename);
107 out = new FileOutputStream(outFile);
114 } catch(IOException e) {
115 Log.e("tag", "Failed to copy asset file: " + filename, e);
120 private void copyFile(InputStream in, OutputStream out) throws IOException {
121 byte[] buffer = new byte[1024];
123 while((read = in.read(buffer)) != -1){
124 out.write(buffer, 0, read);
130 * Test Upload File without chunks
132 public void testUploadFile() {
134 String storagePath = mPath + "/" + mFileToUpload;
135 //String remotePath = "/uploadTest" + mCurrentDate + "/" + mFileToUpload;
136 String remotePath = "/" + mFileToUpload;
138 RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeType);
139 assertTrue(result.isSuccess());
143 * Test Upload File with chunks
145 public void testUploadFileWithChunks() {
147 String storagePath = mPath + "/" + mFileToUploadWithChunks;
148 //String remotePath = "/uploadTest" + mCurrentDate + "/" +mFileToUploadWithChunks;
149 String remotePath = "/" + mFileToUploadWithChunks;
151 RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeTypeWithChunks);
152 assertTrue(result.isSuccess());
156 * Test Upload Not Found File
158 public void testUploadFileNotFound() {
160 String storagePath = mPath + "/" + mFileNotFound;
161 //String remotePath = "/uploadTest" + mCurrentDate + "/" + mFileToUpload;
162 String remotePath = "/" + mFileNotFound;
164 RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeType);
165 assertFalse(result.isSuccess());