6aabf5b0ae07d92e784d881b4a5f24aaff1b2668
[pub/Android/ownCloud.git] /
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17
18 package com.owncloud.android.oc_framework_test_project.test;
19
20 import java.io.File;
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;
27
28 import android.content.res.AssetManager;
29 import android.os.Environment;
30 import android.test.ActivityInstrumentationTestCase2;
31 import android.util.Log;
32
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;
36
37 /**
38 * Class to test Update File Operation
39 * @author masensio
40 *
41 */
42
43 public class UploadFileTest extends ActivityInstrumentationTestCase2<TestActivity> {
44
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";
48
49 private final String mFileToUploadWithChunks = "fileToUploadChunks.MP4";
50 private final String mMimeTypeWithChunks = "video/mp4";
51
52 private final String mStoragePath = "/owncloud/tmp/uploadTest";
53 private String mPath;
54
55 private String mCurrentDate;
56
57 private TestActivity mActivity;
58
59 public UploadFileTest() {
60 super(TestActivity.class);
61
62 }
63
64 @Override
65 protected void setUp() throws Exception {
66 super.setUp();
67 setActivityInitialTouchMode(false);
68 mActivity = getActivity();
69
70 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
71 mCurrentDate = sdf.format(new Date());
72
73 File sdCard = Environment.getExternalStorageDirectory();
74 mPath = sdCard.getAbsolutePath() + "/" + mStoragePath + mCurrentDate;
75
76 //mActivity.createFolder(mPath, true);
77
78 copyAssets();
79 }
80
81 /**
82 * Copy Files to ulpload to SdCard
83 */
84 private void copyAssets() {
85 AssetManager assetManager = getActivity().getAssets();
86 String[] files = { mFileToUpload, mFileToUploadWithChunks };
87
88 // Folder with contents
89 File folder = new File(mPath);
90 folder.mkdirs();
91
92
93 for(String filename : files) {
94 InputStream in = null;
95 OutputStream out = null;
96 try {
97 in = assetManager.open(filename);
98 File outFile = new File(folder, filename);
99 out = new FileOutputStream(outFile);
100 copyFile(in, out);
101 in.close();
102 in = null;
103 out.flush();
104 out.close();
105 out = null;
106 } catch(IOException e) {
107 Log.e("tag", "Failed to copy asset file: " + filename, e);
108 }
109 }
110 }
111
112 private void copyFile(InputStream in, OutputStream out) throws IOException {
113 byte[] buffer = new byte[1024];
114 int read;
115 while((read = in.read(buffer)) != -1){
116 out.write(buffer, 0, read);
117 }
118 }
119
120
121 /**
122 * Test Upload File without chunks
123 */
124 public void testUploadFile() {
125
126 String storagePath = mPath + "/" + mFileToUpload;
127 //String remotePath = "/uploadTest" + mCurrentDate + "/" + mFileToUpload;
128 String remotePath = "/" + mFileToUpload;
129
130 RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeType);
131 assertTrue(result.isSuccess());
132 }
133
134 /**
135 * Test Upload File with chunks
136 */
137 public void testUploadFileWithChunks() {
138
139 String storagePath = mPath + "/" + mFileToUploadWithChunks;
140 //String remotePath = "/uploadTest" + mCurrentDate + "/" +mFileToUploadWithChunks;
141 String remotePath = "/" + mFileToUploadWithChunks;
142
143 RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeTypeWithChunks);
144 assertTrue(result.isSuccess());
145 }
146
147 }