mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-28 17:07:49 +00:00 
			
		
		
		
	Updated UploadFileTest to work with an empty OC server
This commit is contained in:
		
							parent
							
								
									b3e3eb74f0
								
							
						
					
					
						commit
						45eecf1ff8
					
				| Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 21 KiB | 
| @ -77,6 +77,8 @@ public class TestActivity extends Activity { | |||||||
| 	private static final int BUFFER_SIZE = 1024; | 	private static final int BUFFER_SIZE = 1024; | ||||||
| 	 | 	 | ||||||
| 	public static final String ASSETS__TEXT_FILE_NAME = "textFile.txt"; | 	public static final String ASSETS__TEXT_FILE_NAME = "textFile.txt"; | ||||||
|  | 	public static final String ASSETS__IMAGE_FILE_NAME = "imageFile.png"; | ||||||
|  | 	public static final String ASSETS__VIDEO_FILE_NAME = "videoFile.MP4"; | ||||||
| 	 | 	 | ||||||
| 	//private Account mAccount = null; | 	//private Account mAccount = null; | ||||||
| 	private OwnCloudClient mClient; | 	private OwnCloudClient mClient; | ||||||
|  | |||||||
| @ -25,48 +25,45 @@ | |||||||
| package com.owncloud.android.lib.test_project.test; | package com.owncloud.android.lib.test_project.test; | ||||||
| 
 | 
 | ||||||
| import java.io.File; | import java.io.File; | ||||||
| import java.io.FileOutputStream; | import java.util.ArrayList; | ||||||
| import java.io.IOException; | import java.util.Iterator; | ||||||
| import java.io.InputStream; | import java.util.List; | ||||||
| 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.test.ActivityInstrumentationTestCase2; | ||||||
| import android.util.Log; |  | ||||||
| 
 | 
 | ||||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult; | import com.owncloud.android.lib.common.operations.RemoteOperationResult; | ||||||
|  | import com.owncloud.android.lib.resources.files.FileUtils; | ||||||
| import com.owncloud.android.lib.test_project.TestActivity; | import com.owncloud.android.lib.test_project.TestActivity; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Class to test Update File Operation |  * Class to test Update File Operation | ||||||
|  * @author masensio |  * @author masensio | ||||||
|  |  * @author David A. Velasco | ||||||
|  * |  * | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| public class UploadFileTest extends ActivityInstrumentationTestCase2<TestActivity> { | public class UploadFileTest extends ActivityInstrumentationTestCase2<TestActivity> { | ||||||
| 
 | 
 | ||||||
| 	/* Files to upload. These files must exists on the device */	 | 	private static final String LOG_TAG = UploadFileTest.class.getCanonicalName(); | ||||||
| 	private final String mFileToUpload = "fileToUpload.png"; |  | ||||||
| 	private final String mMimeType = "image/png"; |  | ||||||
| 	 | 	 | ||||||
| 	private final String mFileToUploadWithChunks = "fileToUploadChunks.MP4"; | 	private static final String UPLOAD_PATH =  | ||||||
| 	private final String mMimeTypeWithChunks = "video/mp4"; | 			FileUtils.PATH_SEPARATOR + TestActivity.ASSETS__IMAGE_FILE_NAME; | ||||||
| 	 | 	 | ||||||
| 	private final String mFileNotFound = "fileNotFound.png"; | 	private static final String CHUNKED_UPLOAD_PATH =  | ||||||
| 	 | 			FileUtils.PATH_SEPARATOR + TestActivity.ASSETS__VIDEO_FILE_NAME; | ||||||
| 	private final String mStoragePath = "/owncloud/tmp/uploadTest"; |  | ||||||
| 	private String mPath; |  | ||||||
| 	 |  | ||||||
| 	private String mCurrentDate; |  | ||||||
| 	 | 	 | ||||||
|  | 	private static final String FILE_NOT_FOUND_PATH =  | ||||||
|  | 			FileUtils.PATH_SEPARATOR + "fileNotFound.png"; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| 	private TestActivity mActivity; | 	private TestActivity mActivity; | ||||||
|  | 	private File mFileToUpload, mFileToUploadWithChunks; | ||||||
|  | 	private List<String> mUploadedFilesPaths; | ||||||
|  | 	 | ||||||
| 	 | 	 | ||||||
| 	public UploadFileTest() { | 	public UploadFileTest() { | ||||||
| 	    super(TestActivity.class); | 	    super(TestActivity.class); | ||||||
| 	    | 		mUploadedFilesPaths = new ArrayList<String>(); | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	@Override | 	@Override | ||||||
| @ -74,68 +71,24 @@ public class UploadFileTest extends ActivityInstrumentationTestCase2<TestActivit | |||||||
| 	    super.setUp(); | 	    super.setUp(); | ||||||
| 	    setActivityInitialTouchMode(false); | 	    setActivityInitialTouchMode(false); | ||||||
| 	    mActivity = getActivity(); | 	    mActivity = getActivity(); | ||||||
|  | 	    mUploadedFilesPaths.clear(); | ||||||
| 	     | 	     | ||||||
| 	    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); | 		mFileToUpload = mActivity.extractAsset(TestActivity.ASSETS__IMAGE_FILE_NAME); | ||||||
| 	    mCurrentDate = sdf.format(new Date()); | 		mFileToUploadWithChunks = mActivity.extractAsset(TestActivity.ASSETS__VIDEO_FILE_NAME); | ||||||
| 	     |  | ||||||
| 	    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 | 	 * Test Upload File without chunks | ||||||
| 	 */ | 	 */ | ||||||
| 	public void testUploadFile() { | 	public void testUploadFile() { | ||||||
| 
 |  | ||||||
| 		String storagePath = mPath + "/" + mFileToUpload; |  | ||||||
| 		//String remotePath = "/uploadTest" + mCurrentDate + "/" + mFileToUpload; |  | ||||||
| 		String remotePath = "/" + mFileToUpload; |  | ||||||
| 		 | 		 | ||||||
| 		RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeType); | 		RemoteOperationResult result = mActivity.uploadFile( | ||||||
|  | 				mFileToUpload.getAbsolutePath(),  | ||||||
|  | 				UPLOAD_PATH,  | ||||||
|  | 				"image/png" | ||||||
|  | 				); | ||||||
|  | 	    mUploadedFilesPaths.add(UPLOAD_PATH); | ||||||
| 		assertTrue(result.isSuccess()); | 		assertTrue(result.isSuccess()); | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| @ -143,12 +96,13 @@ public class UploadFileTest extends ActivityInstrumentationTestCase2<TestActivit | |||||||
| 	 * Test Upload File with chunks | 	 * Test Upload File with chunks | ||||||
| 	 */ | 	 */ | ||||||
| 	public void testUploadFileWithChunks() { | 	public void testUploadFileWithChunks() { | ||||||
| 
 |  | ||||||
| 		String storagePath = mPath + "/" + mFileToUploadWithChunks; |  | ||||||
| 		//String remotePath = "/uploadTest" + mCurrentDate + "/" +mFileToUploadWithChunks; |  | ||||||
| 		String remotePath = "/" + mFileToUploadWithChunks; |  | ||||||
| 		 | 		 | ||||||
| 		RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeTypeWithChunks); | 		RemoteOperationResult result = mActivity.uploadFile( | ||||||
|  | 				mFileToUploadWithChunks.getAbsolutePath(), | ||||||
|  | 				CHUNKED_UPLOAD_PATH,  | ||||||
|  | 				"video/mp4" | ||||||
|  | 				); | ||||||
|  | 	    mUploadedFilesPaths.add(CHUNKED_UPLOAD_PATH); | ||||||
| 		assertTrue(result.isSuccess()); | 		assertTrue(result.isSuccess()); | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| @ -157,12 +111,26 @@ public class UploadFileTest extends ActivityInstrumentationTestCase2<TestActivit | |||||||
| 	 */ | 	 */ | ||||||
| 	public void testUploadFileNotFound() { | 	public void testUploadFileNotFound() { | ||||||
| 
 | 
 | ||||||
| 		String storagePath = mPath + "/" + mFileNotFound; | 		RemoteOperationResult result = mActivity.uploadFile( | ||||||
| 		//String remotePath = "/uploadTest" + mCurrentDate + "/" + mFileToUpload; | 				FILE_NOT_FOUND_PATH,  | ||||||
| 		String remotePath = "/" + mFileNotFound; | 				FILE_NOT_FOUND_PATH,  | ||||||
| 		 | 				"image/png" | ||||||
| 		RemoteOperationResult result = mActivity.uploadFile(storagePath, remotePath, mMimeType); | 				); | ||||||
| 		assertFalse(result.isSuccess()); | 		assertFalse(result.isSuccess()); | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
|  | 
 | ||||||
|  | 	@Override | ||||||
|  | 	protected void tearDown() throws Exception { | ||||||
|  | 		Iterator<String> it = mUploadedFilesPaths.iterator(); | ||||||
|  | 		RemoteOperationResult removeResult = null; | ||||||
|  | 		while (it.hasNext()) { | ||||||
|  | 			removeResult = mActivity.removeFile(it.next()); | ||||||
|  | 			if (!removeResult.isSuccess()) { | ||||||
|  | 				Utils.logAndThrow(LOG_TAG, removeResult); | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		super.tearDown(); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user