1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com.owncloud.android.oc_framework_test_project.test;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
28 import android.content.res.AssetManager;
29 import android.os.Environment;
30 import android.test.ActivityInstrumentationTestCase2;
31 import android.util.Log;
33 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
34 import com.owncloud.android.oc_framework.operations.RemoteOperationResult.ResultCode;
35 import com.owncloud.android.oc_framework_test_project.TestActivity;
38 * Class to test Update File Operation
43 public class UploadFileTest extends ActivityInstrumentationTestCase2<TestActivity> {
45 /* Files to upload. These files must exists on the device */
46 private final String mFileToUpload = "fileToUpload.png";
47 private final String mMimeType = "image/png";
49 private final String mFileToUploadWithChunks = "fileToUploadChunks.MP4";
50 private final String mMimeTypeWithChunks = "video/mp4";
52 private final String mStoragePath = "/owncloud/tmp/uploadTest";
55 private String mCurrentDate;
57 private TestActivity mActivity;
59 public UploadFileTest() {
60 super(TestActivity.class);
65 protected void setUp() throws Exception {
67 setActivityInitialTouchMode(false);
68 mActivity = getActivity();
70 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
71 mCurrentDate = sdf.format(new Date());
73 File sdCard = Environment.getExternalStorageDirectory();
74 mPath = sdCard.getAbsolutePath() + "/" + mStoragePath + mCurrentDate;
76 //mActivity.createFolder(mPath, true);
82 * Copy Files to ulpload to SdCard
84 private void copyAssets() {
85 AssetManager assetManager = getActivity().getAssets();
86 String[] files = { mFileToUpload, mFileToUploadWithChunks };
88 // Folder with contents
89 File folder = new File(mPath);
93 for(String filename : files) {
94 InputStream in = null;
95 OutputStream out = null;
97 in = assetManager.open(filename);
98 File outFile = new File(folder, filename);
99 out = new FileOutputStream(outFile);
106 } catch(IOException e) {
107 Log.e("tag", "Failed to copy asset file: " + filename, e);
112 private void copyFile(InputStream in, OutputStream out) throws IOException {
113 byte[] buffer = new byte[1024];
115 while((read = in.read(buffer)) != -1){
116 out.write(buffer, 0, read);
122 * Test Upload File without chunks
124 public void testUploadFile() {
126 String storagePath = mPath + "/" + mFileToUpload;
127 //String remotePath = "/uploadTest" + mCurrentDate + "/" + mFileToUpload;
128 String remotePath = "/" + mFileToUpload;
130 RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeType);
131 assertTrue(result.isSuccess());
135 * Test Upload File with chunks
137 public void testUploadFileWithChunks() {
139 String storagePath = mPath + "/" + mFileToUploadWithChunks;
140 //String remotePath = "/uploadTest" + mCurrentDate + "/" +mFileToUploadWithChunks;
141 String remotePath = "/" + mFileToUploadWithChunks;
143 RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeTypeWithChunks);
144 assertTrue(result.isSuccess());