OC-2333: Unit test for UploadFile
authormasensio <masensio@solidgear.es>
Tue, 17 Dec 2013 14:16:15 +0000 (15:16 +0100)
committermasensio <masensio@solidgear.es>
Tue, 17 Dec 2013 14:16:15 +0000 (15:16 +0100)
oc_framework-test-project/AndroidManifest.xml
oc_framework-test-project/assets/fileToUpload.png [new file with mode: 0644]
oc_framework-test-project/assets/fileToUploadChunks.MP4 [new file with mode: 0644]
oc_framework-test-project/oc_framework-test-test/src/com/owncloud/android/oc_framework_test_project/test/UploadFileTest.java [new file with mode: 0644]
oc_framework-test-project/src/com/owncloud/android/oc_framework_test_project/TestActivity.java

index c913bf0..2d296ed 100644 (file)
@@ -9,7 +9,9 @@
     <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
     <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
     <uses-permission android:name="android.permission.INTERNET"/>
-        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
+    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
     
     <uses-sdk
         android:minSdkVersion="8"
diff --git a/oc_framework-test-project/assets/fileToUpload.png b/oc_framework-test-project/assets/fileToUpload.png
new file mode 100644 (file)
index 0000000..915ec22
Binary files /dev/null and b/oc_framework-test-project/assets/fileToUpload.png differ
diff --git a/oc_framework-test-project/assets/fileToUploadChunks.MP4 b/oc_framework-test-project/assets/fileToUploadChunks.MP4
new file mode 100644 (file)
index 0000000..2422025
Binary files /dev/null and b/oc_framework-test-project/assets/fileToUploadChunks.MP4 differ
diff --git a/oc_framework-test-project/oc_framework-test-test/src/com/owncloud/android/oc_framework_test_project/test/UploadFileTest.java b/oc_framework-test-project/oc_framework-test-test/src/com/owncloud/android/oc_framework_test_project/test/UploadFileTest.java
new file mode 100644 (file)
index 0000000..6aabf5b
--- /dev/null
@@ -0,0 +1,147 @@
+/* ownCloud Android client application
+ *   Copyright (C) 2012-2013 ownCloud Inc.
+ *
+ *   This program is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License version 2,
+ *   as published by the Free Software Foundation.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+package com.owncloud.android.oc_framework_test_project.test;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import android.content.res.AssetManager;
+import android.os.Environment;
+import android.test.ActivityInstrumentationTestCase2;
+import android.util.Log;
+
+import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
+import com.owncloud.android.oc_framework.operations.RemoteOperationResult.ResultCode;
+import com.owncloud.android.oc_framework_test_project.TestActivity;
+
+/**
+ * Class to test Update File Operation
+ * @author masensio
+ *
+ */
+
+public class UploadFileTest extends ActivityInstrumentationTestCase2<TestActivity> {
+
+       /* Files to upload. These files must exists on the device */    
+       private final String mFileToUpload = "fileToUpload.png";
+       private final String mMimeType = "image/png";
+       
+       private final String mFileToUploadWithChunks = "fileToUploadChunks.MP4";
+       private final String mMimeTypeWithChunks = "video/mp4";
+       
+       private final String mStoragePath = "/owncloud/tmp/uploadTest";
+       private String mPath;
+       
+       private String mCurrentDate;
+       
+       private TestActivity mActivity;
+       
+       public UploadFileTest() {
+           super(TestActivity.class);
+          
+       }
+       
+       @Override
+         protected void setUp() throws Exception {
+           super.setUp();
+           setActivityInitialTouchMode(false);
+           mActivity = getActivity();
+           
+           SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
+           mCurrentDate = sdf.format(new Date());
+           
+           File sdCard = Environment.getExternalStorageDirectory();
+        mPath =  sdCard.getAbsolutePath() + "/" + mStoragePath + mCurrentDate;
+        
+               //mActivity.createFolder(mPath, true);
+        
+           copyAssets();
+       }
+
+       /**
+        * Copy Files to ulpload to SdCard
+        */
+       private void copyAssets() {
+               AssetManager assetManager = getActivity().getAssets();
+               String[] files = { mFileToUpload, mFileToUploadWithChunks }; 
+           
+           // Folder with contents
+        File folder = new File(mPath);
+        folder.mkdirs();
+        
+        
+           for(String filename : files) {
+               InputStream in = null;
+               OutputStream out = null;
+               try {
+                 in = assetManager.open(filename);
+                 File outFile = new File(folder, filename);
+                 out = new FileOutputStream(outFile);
+                 copyFile(in, out);
+                 in.close();
+                 in = null;
+                 out.flush();
+                 out.close();
+                 out = null;
+               } catch(IOException e) {
+                   Log.e("tag", "Failed to copy asset file: " + filename, e);
+               }       
+           }
+       }
+       
+       private void copyFile(InputStream in, OutputStream out) throws IOException {
+           byte[] buffer = new byte[1024];
+           int read;
+           while((read = in.read(buffer)) != -1){
+             out.write(buffer, 0, read);
+           }
+       }
+       
+       
+       /**
+        * Test Upload File without chunks
+        */
+       public void testUploadFile() {
+
+               String storagePath = mPath + "/" + mFileToUpload;
+               //String remotePath = "/uploadTest" + mCurrentDate + "/" + mFileToUpload;
+               String remotePath = "/" + mFileToUpload;
+               
+               RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeType);
+               assertTrue(result.isSuccess());
+       }
+       
+       /**
+        * Test Upload File with chunks
+        */
+       public void testUploadFileWithChunks() {
+
+               String storagePath = mPath + "/" + mFileToUploadWithChunks;
+               //String remotePath = "/uploadTest" + mCurrentDate + "/" +mFileToUploadWithChunks;
+               String remotePath = "/" + mFileToUploadWithChunks;
+               
+               RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeTypeWithChunks);
+               assertTrue(result.isSuccess());
+       }
+       
+}
index db38ea5..f259ec4 100644 (file)
@@ -1,5 +1,7 @@
 package com.owncloud.android.oc_framework_test_project;
 
+import java.io.File;
+
 import com.owncloud.android.oc_framework.network.webdav.OwnCloudClientFactory;
 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
@@ -7,9 +9,11 @@ import com.owncloud.android.oc_framework.operations.remote.CreateRemoteFolderOpe
 import com.owncloud.android.oc_framework.operations.remote.ReadRemoteFolderOperation;
 import com.owncloud.android.oc_framework.operations.remote.RemoveRemoteFileOperation;
 import com.owncloud.android.oc_framework.operations.remote.RenameRemoteFileOperation;
+import com.owncloud.android.oc_framework.operations.remote.UploadRemoteFileOperation;
 
 import android.net.Uri;
 import android.os.Bundle;
+import android.os.Environment;
 import android.app.Activity;
 import android.view.Menu;
 
@@ -105,4 +109,18 @@ public class TestActivity extends Activity {
                return result;
        }
        
+       /** Access to the library method to Upload a File 
+        * @param storagePath
+        * @param remotePath
+        * @param mimeType
+        * 
+        * @return
+        */
+       public RemoteOperationResult uploadFile(String storagePath, String remotePath, String mimeType) {
+               
+               UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(storagePath, remotePath, mimeType);
+               RemoteOperationResult result = uploadOperation.execute(mClient);
+               
+               return result;
+       }
 }