From: masensio Date: Fri, 15 Nov 2013 12:43:45 +0000 (+0100) Subject: OC-1991: Create Unit Tests for Rename File and Folder X-Git-Tag: oc-android-1.5.5~123^2~7 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/af0b050668d5953cca872b13ee735dceba13f6ea?ds=inline;hp=-c OC-1991: Create Unit Tests for Rename File and Folder --- af0b050668d5953cca872b13ee735dceba13f6ea 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 index 00000000..119d160f --- /dev/null +++ b/oc_framework-test-project/oc_framework-test-test/src/com/owncloud/android/oc_framework_test_project/test/RenameFileTest.java @@ -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 { + + /* 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()); + } + +} diff --git a/oc_framework-test-project/src/com/owncloud/android/oc_framework_test_project/TestActivity.java b/oc_framework-test-project/src/com/owncloud/android/oc_framework_test_project/TestActivity.java index ec3f5d53..7df78bd4 100644 --- a/oc_framework-test-project/src/com/owncloud/android/oc_framework_test_project/TestActivity.java +++ b/oc_framework-test-project/src/com/owncloud/android/oc_framework_test_project/TestActivity.java @@ -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; + } } diff --git a/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/RenameRemoteFileOperation.java b/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/RenameRemoteFileOperation.java index 05a58e27..cecf07db 100644 --- a/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/RenameRemoteFileOperation.java +++ b/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/RenameRemoteFileOperation.java @@ -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 "/" */