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_test_project.TestActivity;
37 * Class to test Update File Operation
42 public class UploadFileTest extends ActivityInstrumentationTestCase2<TestActivity> {
44 /* Files to upload. These files must exists on the device */
45 private final String mFileToUpload = "fileToUpload.png";
46 private final String mMimeType = "image/png";
48 private final String mFileToUploadWithChunks = "fileToUploadChunks.MP4";
49 private final String mMimeTypeWithChunks = "video/mp4";
51 private final String mFileNotFound = "fileNotFound.png";
53 private final String mStoragePath = "/owncloud/tmp/uploadTest";
56 private String mCurrentDate;
58 private TestActivity mActivity;
60 public UploadFileTest() {
61 super(TestActivity.class);
66 protected void setUp() throws Exception {
68 setActivityInitialTouchMode(false);
69 mActivity = getActivity();
71 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
72 mCurrentDate = sdf.format(new Date());
74 File sdCard = Environment.getExternalStorageDirectory();
75 mPath = sdCard.getAbsolutePath() + "/" + mStoragePath + mCurrentDate;
77 //mActivity.createFolder(mPath, true);
83 * Copy Files to ulpload to SdCard
85 private void copyAssets() {
86 AssetManager assetManager = getActivity().getAssets();
87 String[] files = { mFileToUpload, mFileToUploadWithChunks };
89 // Folder with contents
90 File folder = new File(mPath);
94 for(String filename : files) {
95 InputStream in = null;
96 OutputStream out = null;
98 in = assetManager.open(filename);
99 File outFile = new File(folder, filename);
100 out = new FileOutputStream(outFile);
107 } catch(IOException e) {
108 Log.e("tag", "Failed to copy asset file: " + filename, e);
113 private void copyFile(InputStream in, OutputStream out) throws IOException {
114 byte[] buffer = new byte[1024];
116 while((read = in.read(buffer)) != -1){
117 out.write(buffer, 0, read);
123 * Test Upload File without chunks
125 public void testUploadFile() {
127 String storagePath = mPath + "/" + mFileToUpload;
128 //String remotePath = "/uploadTest" + mCurrentDate + "/" + mFileToUpload;
129 String remotePath = "/" + mFileToUpload;
131 RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeType);
132 assertTrue(result.isSuccess());
136 * Test Upload File with chunks
138 public void testUploadFileWithChunks() {
140 String storagePath = mPath + "/" + mFileToUploadWithChunks;
141 //String remotePath = "/uploadTest" + mCurrentDate + "/" +mFileToUploadWithChunks;
142 String remotePath = "/" + mFileToUploadWithChunks;
144 RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeTypeWithChunks);
145 assertTrue(result.isSuccess());
149 * Test Upload Not Found File
151 public void testUploadFileNotFound() {
153 String storagePath = mPath + "/" + mFileNotFound;
154 //String remotePath = "/uploadTest" + mCurrentDate + "/" + mFileToUpload;
155 String remotePath = "/" + mFileNotFound;
157 RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeType);
158 assertFalse(result.isSuccess());