67103718e468e9eed7100eac56e766d0368dfa9f
[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_test_project.TestActivity;
35
36 /**
37 * Class to test Update File Operation
38 * @author masensio
39 *
40 */
41
42 public class UploadFileTest extends ActivityInstrumentationTestCase2<TestActivity> {
43
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";
47
48 private final String mFileToUploadWithChunks = "fileToUploadChunks.MP4";
49 private final String mMimeTypeWithChunks = "video/mp4";
50
51 private final String mFileNotFound = "fileNotFound.png";
52
53 private final String mStoragePath = "/owncloud/tmp/uploadTest";
54 private String mPath;
55
56 private String mCurrentDate;
57
58 private TestActivity mActivity;
59
60 public UploadFileTest() {
61 super(TestActivity.class);
62
63 }
64
65 @Override
66 protected void setUp() throws Exception {
67 super.setUp();
68 setActivityInitialTouchMode(false);
69 mActivity = getActivity();
70
71 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
72 mCurrentDate = sdf.format(new Date());
73
74 File sdCard = Environment.getExternalStorageDirectory();
75 mPath = sdCard.getAbsolutePath() + "/" + mStoragePath + mCurrentDate;
76
77 //mActivity.createFolder(mPath, true);
78
79 copyAssets();
80 }
81
82 /**
83 * Copy Files to ulpload to SdCard
84 */
85 private void copyAssets() {
86 AssetManager assetManager = getActivity().getAssets();
87 String[] files = { mFileToUpload, mFileToUploadWithChunks };
88
89 // Folder with contents
90 File folder = new File(mPath);
91 folder.mkdirs();
92
93
94 for(String filename : files) {
95 InputStream in = null;
96 OutputStream out = null;
97 try {
98 in = assetManager.open(filename);
99 File outFile = new File(folder, filename);
100 out = new FileOutputStream(outFile);
101 copyFile(in, out);
102 in.close();
103 in = null;
104 out.flush();
105 out.close();
106 out = null;
107 } catch(IOException e) {
108 Log.e("tag", "Failed to copy asset file: " + filename, e);
109 }
110 }
111 }
112
113 private void copyFile(InputStream in, OutputStream out) throws IOException {
114 byte[] buffer = new byte[1024];
115 int read;
116 while((read = in.read(buffer)) != -1){
117 out.write(buffer, 0, read);
118 }
119 }
120
121
122 /**
123 * Test Upload File without chunks
124 */
125 public void testUploadFile() {
126
127 String storagePath = mPath + "/" + mFileToUpload;
128 //String remotePath = "/uploadTest" + mCurrentDate + "/" + mFileToUpload;
129 String remotePath = "/" + mFileToUpload;
130
131 RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeType);
132 assertTrue(result.isSuccess());
133 }
134
135 /**
136 * Test Upload File with chunks
137 */
138 public void testUploadFileWithChunks() {
139
140 String storagePath = mPath + "/" + mFileToUploadWithChunks;
141 //String remotePath = "/uploadTest" + mCurrentDate + "/" +mFileToUploadWithChunks;
142 String remotePath = "/" + mFileToUploadWithChunks;
143
144 RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeTypeWithChunks);
145 assertTrue(result.isSuccess());
146 }
147
148 /**
149 * Test Upload Not Found File
150 */
151 public void testUploadFileNotFound() {
152
153 String storagePath = mPath + "/" + mFileNotFound;
154 //String remotePath = "/uploadTest" + mCurrentDate + "/" + mFileToUpload;
155 String remotePath = "/" + mFileNotFound;
156
157 RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeType);
158 assertFalse(result.isSuccess());
159 }
160
161 }