OC-1991: Create Unit Tests for Rename File and Folder
authormasensio <masensio@solidgear.es>
Fri, 15 Nov 2013 12:43:45 +0000 (13:43 +0100)
committermasensio <masensio@solidgear.es>
Fri, 15 Nov 2013 12:43:45 +0000 (13:43 +0100)
oc_framework-test-project/oc_framework-test-test/src/com/owncloud/android/oc_framework_test_project/test/RenameFileTest.java [new file with mode: 0644]
oc_framework-test-project/src/com/owncloud/android/oc_framework_test_project/TestActivity.java
oc_framework/src/com/owncloud/android/oc_framework/operations/remote/RenameRemoteFileOperation.java

diff --git a/oc_framework-test-project/oc_framework-test-test/src/com/owncloud/android/oc_framework_test_project/test/RenameFileTest.java b/oc_framework-test-project/oc_framework-test-test/src/com/owncloud/android/oc_framework_test_project/test/RenameFileTest.java
new file mode 100644 (file)
index 0000000..119d160
--- /dev/null
@@ -0,0 +1,91 @@
+package com.owncloud.android.oc_framework_test_project.test;
+
+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;
+
+import android.test.ActivityInstrumentationTestCase2;
+
+public class RenameFileTest extends ActivityInstrumentationTestCase2<TestActivity> {
+
+       /* Folder data to rename. This folder must exist on the account */
+       private final String mOldFolderName = "folderToRename";
+       private final String mOldFolderPath = "/folderToRename/";
+       private final String mNewFolderName = "renamedFolder"; 
+       private final String mNewFolderPath = "/renameFolder/";
+       private final String mNewFolderNameInvalidChars = "renamedFolder:";
+       private final String mNewFolderPathInvalidChars = "/renamedFolder:/";
+       
+       /* File data to rename. This file must exist on the account */
+       private final String mOldFileName = "fileToRename.png";
+       private final String mOldFilePath = "/fileToRename.png";
+       private final String mNewFileName = "renamedFile.png";
+       private final String mNewFilePath = "/renamedFile.png";
+       private final String mNewFileNameInvalidChars = "renamedFile:.png";
+       private final String mNewFilePathInvalidChars = "/renamedFile:.png";
+       
+       
+       private TestActivity mActivity;
+       
+       public RenameFileTest() {
+           super(TestActivity.class);
+          
+       }
+       
+       @Override
+         protected void setUp() throws Exception {
+           super.setUp();
+           setActivityInitialTouchMode(false);
+           mActivity = getActivity();
+       }
+       
+       /**
+        * Test Rename Folder
+        */
+       public void testRenameFolder() {
+
+               RemoteOperationResult result = mActivity.renameFile(mOldFolderName, mOldFolderPath, mNewFolderName, mNewFolderPath);
+               assertTrue(result.isSuccess());
+       }
+       
+       /**
+        * Test Rename Folder with forbidden characters
+        */
+       public void testRenameFolderForbiddenChars() {
+
+               RemoteOperationResult result = mActivity.renameFile(mOldFolderName, mOldFolderPath, 
+                               mNewFolderNameInvalidChars, mNewFolderPathInvalidChars);
+               assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
+       }
+       
+       /**
+        * Test Rename File
+        */
+       public void testRenameFile() {
+               RemoteOperationResult result = mActivity.renameFile(mOldFileName, mOldFilePath, mNewFileName, mNewFilePath);
+               assertTrue(result.isSuccess());
+       }
+       
+       
+       /**
+        * Test Rename Folder with forbidden characters
+        */
+       public void testRenameFileForbiddenChars() {
+               RemoteOperationResult result = mActivity.renameFile(mOldFileName, mOldFilePath, 
+                               mNewFileNameInvalidChars, mNewFilePathInvalidChars);
+               assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
+       }
+       
+       
+       /**
+        * Restore initial conditions
+        */
+       public void testRestoreInitialConditions() {
+               RemoteOperationResult result = mActivity.renameFile(mNewFolderName, mNewFolderPath, mOldFolderName, mOldFolderPath);
+               assertTrue(result.isSuccess());
+               
+               result = mActivity.renameFile(mNewFileName, mNewFilePath, mOldFileName, mOldFilePath);
+               assertTrue(result.isSuccess());
+       }
+       
+}
index ec3f5d5..7df78bd 100644 (file)
@@ -7,6 +7,7 @@ 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;
 import com.owncloud.android.oc_framework.operations.remote.CreateRemoteFolderOperation;
+import com.owncloud.android.oc_framework.operations.remote.RenameRemoteFileOperation;
 
 import android.os.Bundle;
 import android.accounts.Account;
@@ -26,6 +27,13 @@ public class TestActivity extends Activity {
        
        private static final String TAG = "TestActivity";
        
+       // This account must exists on the simulator / device
+       private static final String mAccountHost = "beta.owncloud.com";
+       private static final String mAccountUser = "testandroid";
+       private static final String mAccountName = mAccountUser + "@"+ mAccountHost;
+       private static final String mAccountPass = "testandroid";
+       private static final String mAccountType = "owncloud";  
+       
        private Account mAccount = null;
        private WebdavClient mClient;
        
@@ -33,27 +41,20 @@ public class TestActivity extends Activity {
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_test);
-               
-               // This account must exists on the simulator / device
-               String accountHost = "beta.owncloud.com";
-               String accountUser = "masensio";
-               String accountName = accountUser + "@"+ accountHost;
-               String accountPass = "masensio";
-               String accountType = "owncloud";        
 
                AccountManager am = AccountManager.get(this);
                
-               Account[] ocAccounts = am.getAccountsByType(accountType);
+               Account[] ocAccounts = am.getAccountsByType(mAccountType);
         for (Account ac : ocAccounts) {
-           if (ac.name.equals(accountName)) {
+           if (ac.name.equals(mAccountName)) {
                   mAccount = ac;
                   break;
             }
         }
 
 //        if (mAccount == null) {
-//                     mAccount = new Account(accountName, accountType);       
-//                     am.addAccountExplicitly(mAccount, accountPass, null);
+//                     mAccount = new Account(accountName, mAccountType);      
+//                     am.addAccountExplicitly(mAccount, mAccountPass, null);
 //             am.setUserData(mAccount, "oc_version",    "5.0.14");
 //             am.setUserData(mAccount, "oc_base_url",   "http://beta.owncloud.com/owncloud");
 //        } else {
@@ -91,6 +92,7 @@ public class TestActivity extends Activity {
         * @param folderName
         * @param remotePath
         * @param createFullPath
+        * 
         * @return
         */
        public RemoteOperationResult createFolder(String folderName, String remotePath, boolean createFullPath) {
@@ -100,4 +102,22 @@ public class TestActivity extends Activity {
                
                return result;
        }
+       
+       /**
+        * Access to the library method to Rename a File or Folder
+        * @param oldName                       Old name of the file.
+     * @param oldRemotePath            Old remote path of the file. For folders it starts and ends by "/"
+     * @param newName                  New name to set as the name of file.
+     * @param newRemotePath            New remote path to move the file, for folders it starts and ends by "/"
+     * 
+     * @return
+     */
+
+       public RemoteOperationResult renameFile(String oldName, String oldRemotePath, String newName, String newRemotePath) {
+               
+               RenameRemoteFileOperation renameOperation = new RenameRemoteFileOperation(oldName, oldRemotePath, newName, newRemotePath);
+               RemoteOperationResult result = renameOperation.execute(mClient);
+               
+               return result;
+       }
 }
index 05a58e2..cecf07d 100644 (file)
@@ -35,7 +35,7 @@ public class RenameRemoteFileOperation extends RemoteOperation {
      * Constructor
      * 
      * @param oldName                  Old name of the file.
-     * @param oldRemotePath            Old remote path of the file.
+     * @param oldRemotePath            Old remote path of the file. For folders it starts and ends by "/"
      * @param newName                  New name to set as the name of file.
      * @param newRemotePath            New remote path to move the file, for folders it starts and ends by "/"
      */