From 511a16612d11b94152a2fd2be549be33c0864f42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kilian=20Pe=CC=81risset?= Date: Thu, 13 Feb 2020 09:32:41 +0100 Subject: [PATCH 1/6] Fixed bug relative to null folder in FolderPicker --- .../ui/activity/FolderPickerActivity.java | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java b/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java index 72953d250ae2..9466dcff4768 100644 --- a/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java @@ -59,11 +59,8 @@ import com.owncloud.android.utils.DisplayUtils; import com.owncloud.android.utils.ErrorMessageAdapter; import com.owncloud.android.utils.ThemeUtils; - import java.util.ArrayList; - import javax.inject.Inject; - import androidx.appcompat.app.ActionBar; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; @@ -302,7 +299,8 @@ public boolean onOptionsItemSelected(MenuItem item) { boolean retval = true; switch (item.getItemId()) { case R.id.action_create_dir: { - CreateFolderDialogFragment dialog = CreateFolderDialogFragment.newInstance(getCurrentFolder()); + OCFile currentFolder = getCurrentFolder(); + CreateFolderDialogFragment dialog = CreateFolderDialogFragment.newInstance(currentFolder); dialog.show(getSupportFragmentManager(), CreateFolderDialogFragment.CREATE_FOLDER_FRAGMENT); break; } @@ -333,16 +331,24 @@ public boolean onOptionsItemSelected(MenuItem item) { } protected OCFile getCurrentFolder() { - OCFile file = getFile(); - if (file != null) { - if (file.isFolder()) { - return file; - } else if (getStorageManager() != null) { - String parentPath = file.getRemotePath().substring(0, file.getRemotePath().lastIndexOf(file.getFileName())); - return getStorageManager().getFileByPath(parentPath); + OCFile currentStateFile = getFile(); + OCFile finalFolder; + + // If the file is null, take the root folder to avoid any error in functions depending on this one + if (currentStateFile == null) { + finalFolder = getStorageManager().getFileByPath(OCFile.ROOT_PATH); + } else { + if (currentStateFile.isFolder()) { + finalFolder = currentStateFile; + } else { + String parentPath = currentStateFile + .getRemotePath() + .substring(0, currentStateFile.getRemotePath() + .lastIndexOf(currentStateFile.getFileName())); + finalFolder = getStorageManager().getFileByPath(parentPath); } } - return null; + return finalFolder; } public void refreshListOfFilesFragment(boolean fromSearch) { From 389090d3e89d2a080abc813d3d52f22254c9bf5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kilian=20Pe=CC=81risset?= Date: Thu, 13 Feb 2020 12:55:02 +0100 Subject: [PATCH 2/6] Implemented tests for FolderPickerActivity --- .../ui/activity/FolderPickerActivityTest.java | 74 +++++++++++++++++++ .../ui/activity/FolderPickerActivity.java | 22 +++--- 2 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityTest.java diff --git a/src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityTest.java b/src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityTest.java new file mode 100644 index 000000000000..22419b2b42a9 --- /dev/null +++ b/src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityTest.java @@ -0,0 +1,74 @@ +package com.owncloud.android.ui.activity; + +import com.owncloud.android.datamodel.OCFile; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.LargeTest; +import androidx.test.rule.ActivityTestRule; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class FolderPickerActivityTest { + @Rule + public ActivityTestRule activityRule = + new ActivityTestRule<>(FolderPickerActivity.class); + + @Test + public void getFile_NoDifferenceTest() { + // Arrange + FolderPickerActivity targetActivity = activityRule.getActivity(); + OCFile origin = new OCFile("/test/file.test"); + origin.setRemotePath("/remotePath/test"); + + // Act + targetActivity.setFile(origin); + OCFile target = targetActivity.getFile(); + + // Assert + Assert.assertEquals(origin, target); + } + + @Test + public void getParentFolder_isNotRootFolderTest() { + // Arrange + FolderPickerActivity targetActivity = activityRule.getActivity(); + + + OCFile origin = new OCFile("/test/"); + origin.setFileId(1); + origin.setRemotePath("/test/"); + origin.setStoragePath("/test/"); + origin.setFolder(); + + // Act + targetActivity.setFile(origin); + OCFile target = targetActivity.getCurrentFolder(); + + // Assert + Assert.assertEquals(origin, target); + } + + @Test + public void getParentFolder_isRootFolderTest() { + // Arrange + FolderPickerActivity targetActivity = activityRule.getActivity(); + + OCFile origin = new OCFile("/"); + origin.setFileId(1); + origin.setRemotePath("/"); + origin.setStoragePath("/"); + origin.setFolder(); + + // Act + targetActivity.setFile(origin); + OCFile target = targetActivity.getCurrentFolder(); + + // Assert + Assert.assertEquals(origin, target); + } +} diff --git a/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java b/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java index 9466dcff4768..d37030b242a7 100644 --- a/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java @@ -331,22 +331,22 @@ public boolean onOptionsItemSelected(MenuItem item) { } protected OCFile getCurrentFolder() { - OCFile currentStateFile = getFile(); - OCFile finalFolder; + OCFile currentFile = getFile(); + OCFile finalFolder = null; // If the file is null, take the root folder to avoid any error in functions depending on this one - if (currentStateFile == null) { - finalFolder = getStorageManager().getFileByPath(OCFile.ROOT_PATH); - } else { - if (currentStateFile.isFolder()) { - finalFolder = currentStateFile; - } else { - String parentPath = currentStateFile + if (currentFile != null) { + if (currentFile.isFolder()) { + finalFolder = currentFile; + } else if(currentFile.getRemotePath() != null) { + String parentPath = currentFile .getRemotePath() - .substring(0, currentStateFile.getRemotePath() - .lastIndexOf(currentStateFile.getFileName())); + .substring(0, currentFile.getRemotePath() + .lastIndexOf(currentFile.getFileName())); finalFolder = getStorageManager().getFileByPath(parentPath); } + } else { + finalFolder = getStorageManager().getFileByPath(OCFile.ROOT_PATH); } return finalFolder; } From c821f192d66628d33fe5f95fcba30c8f3f495b36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kilian=20Pe=CC=81risset?= Date: Thu, 13 Feb 2020 13:48:17 +0100 Subject: [PATCH 3/6] Formated the code (removed useless spaces) --- .../owncloud/android/ui/activity/FolderPickerActivityTest.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityTest.java b/src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityTest.java index 22419b2b42a9..62d0e5c11444 100644 --- a/src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityTest.java +++ b/src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityTest.java @@ -37,8 +37,6 @@ public void getFile_NoDifferenceTest() { public void getParentFolder_isNotRootFolderTest() { // Arrange FolderPickerActivity targetActivity = activityRule.getActivity(); - - OCFile origin = new OCFile("/test/"); origin.setFileId(1); origin.setRemotePath("/test/"); @@ -57,7 +55,6 @@ public void getParentFolder_isNotRootFolderTest() { public void getParentFolder_isRootFolderTest() { // Arrange FolderPickerActivity targetActivity = activityRule.getActivity(); - OCFile origin = new OCFile("/"); origin.setFileId(1); origin.setRemotePath("/"); From c59f9c0b913b5da2a5b5c1a9960c3542b63a13fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kilian=20Pe=CC=81risset?= Date: Fri, 14 Feb 2020 13:00:49 +0100 Subject: [PATCH 4/6] Formated the code + added header --- .../ui/activity/FolderPickerActivityTest.java | 27 ++++++++++++++++--- .../ui/activity/FolderPickerActivity.java | 18 ++++++------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityTest.java b/src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityTest.java index 62d0e5c11444..28052d07dd24 100644 --- a/src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityTest.java +++ b/src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityTest.java @@ -1,5 +1,26 @@ package com.owncloud.android.ui.activity; +/* + * Nextcloud Android client application + * + * @author Kilian Périsset + * Copyright (C) 2019 Kilian Périsset (Infomaniak Network SA) + * Copyright (C) 2019 Nextcloud GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + import com.owncloud.android.datamodel.OCFile; import org.junit.Assert; @@ -19,7 +40,7 @@ public class FolderPickerActivityTest { new ActivityTestRule<>(FolderPickerActivity.class); @Test - public void getFile_NoDifferenceTest() { + public void getActivityFile() { // Arrange FolderPickerActivity targetActivity = activityRule.getActivity(); OCFile origin = new OCFile("/test/file.test"); @@ -34,7 +55,7 @@ public void getFile_NoDifferenceTest() { } @Test - public void getParentFolder_isNotRootFolderTest() { + public void getParentFolder_isNotRootFolder() { // Arrange FolderPickerActivity targetActivity = activityRule.getActivity(); OCFile origin = new OCFile("/test/"); @@ -52,7 +73,7 @@ public void getParentFolder_isNotRootFolderTest() { } @Test - public void getParentFolder_isRootFolderTest() { + public void getParentFolder_isRootFolder() { // Arrange FolderPickerActivity targetActivity = activityRule.getActivity(); OCFile origin = new OCFile("/"); diff --git a/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java b/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java index d37030b242a7..66d0021d4331 100644 --- a/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java @@ -43,6 +43,7 @@ import com.nextcloud.client.preferences.AppPreferences; import com.google.android.material.button.MaterialButton; import com.owncloud.android.R; +import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -59,6 +60,8 @@ import com.owncloud.android.utils.DisplayUtils; import com.owncloud.android.utils.ErrorMessageAdapter; import com.owncloud.android.utils.ThemeUtils; + +import java.io.File; import java.util.ArrayList; import javax.inject.Inject; import androidx.appcompat.app.ActionBar; @@ -299,8 +302,7 @@ public boolean onOptionsItemSelected(MenuItem item) { boolean retval = true; switch (item.getItemId()) { case R.id.action_create_dir: { - OCFile currentFolder = getCurrentFolder(); - CreateFolderDialogFragment dialog = CreateFolderDialogFragment.newInstance(currentFolder); + CreateFolderDialogFragment dialog = CreateFolderDialogFragment.newInstance(getCurrentFolder()); dialog.show(getSupportFragmentManager(), CreateFolderDialogFragment.CREATE_FOLDER_FRAGMENT); break; } @@ -333,20 +335,18 @@ public boolean onOptionsItemSelected(MenuItem item) { protected OCFile getCurrentFolder() { OCFile currentFile = getFile(); OCFile finalFolder = null; + FileDataStorageManager storageManager = getStorageManager(); // If the file is null, take the root folder to avoid any error in functions depending on this one if (currentFile != null) { if (currentFile.isFolder()) { finalFolder = currentFile; - } else if(currentFile.getRemotePath() != null) { - String parentPath = currentFile - .getRemotePath() - .substring(0, currentFile.getRemotePath() - .lastIndexOf(currentFile.getFileName())); - finalFolder = getStorageManager().getFileByPath(parentPath); + } else if (currentFile.getRemotePath() != null) { + long parentId = currentFile.getParentId(); + finalFolder = storageManager.getFileById(parentId); } } else { - finalFolder = getStorageManager().getFileByPath(OCFile.ROOT_PATH); + finalFolder = storageManager.getFileByPath(OCFile.ROOT_PATH); } return finalFolder; } From 467104c4551caab9c768f9dcfcf47dc1dfa6671b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kilian=20Pe=CC=81risset?= Date: Mon, 17 Feb 2020 10:53:14 +0100 Subject: [PATCH 5/6] Replaced OCFile by File in getFolder() method --- .../ui/activity/FolderPickerActivity.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java b/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java index 66d0021d4331..27bc65f70b34 100644 --- a/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java @@ -39,9 +39,9 @@ import android.view.View; import android.view.View.OnClickListener; +import com.google.android.material.button.MaterialButton; import com.nextcloud.client.di.Injectable; import com.nextcloud.client.preferences.AppPreferences; -import com.google.android.material.button.MaterialButton; import com.owncloud.android.R; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; @@ -63,7 +63,9 @@ import java.io.File; import java.util.ArrayList; + import javax.inject.Inject; + import androidx.appcompat.app.ActionBar; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; @@ -195,8 +197,7 @@ protected void createFragments() { } /** - * Show a text message on screen view for notifying user if content is - * loading or folder is empty + * Show a text message on screen view for notifying user if content is loading or folder is empty */ private void setBackgroundText() { OCFileListFragment listFragment = getListOfFilesFragment(); @@ -250,7 +251,7 @@ public void startSyncFolderOperation(OCFile folder, boolean ignoreETag) { // perform folder synchronization RemoteOperation refreshFolderOperation = new RefreshFolderOperation(folder, currentSyncTime, false, - ignoreETag, getStorageManager(), getAccount(), getApplicationContext()); + ignoreETag, getStorageManager(), getAccount(), getApplicationContext()); refreshFolderOperation.execute(getAccount(), this, null, null); setIndeterminate(true); @@ -342,8 +343,8 @@ protected OCFile getCurrentFolder() { if (currentFile.isFolder()) { finalFolder = currentFile; } else if (currentFile.getRemotePath() != null) { - long parentId = currentFile.getParentId(); - finalFolder = storageManager.getFileById(parentId); + String parentPath = new File(currentFile.getRemotePath()).getParent(); + finalFolder = storageManager.getFileByPath(parentPath); } } else { finalFolder = storageManager.getFileByPath(OCFile.ROOT_PATH); @@ -449,8 +450,7 @@ public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationRe /** - * Updates the view associated to the activity after the finish of an operation trying - * to create a new folder. + * Updates the view associated to the activity after the finish of an operation trying to create a new folder. * * @param operation Creation operation performed. * @param result Result of the creation. @@ -502,7 +502,7 @@ public void onReceive(Context context, Intent intent) { if (currentDir == null) { // current folder was removed from the server DisplayUtils.showSnackMessage(getActivity(), R.string.sync_current_folder_was_removed, - getCurrentFolder().getFileName()); + getCurrentFolder().getFileName()); browseToRoot(); } else { if (currentFile == null && !getFile().isFolder()) { From a93313ca19ca97ef03bd3cee4a97314e95192e9a Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Mon, 17 Feb 2020 15:05:14 +0100 Subject: [PATCH 6/6] added tests Signed-off-by: tobiasKaminsky --- ...yTest.java => FolderPickerActivityIT.java} | 32 ++++++++++++++++++- .../ui/activity/FolderPickerActivity.java | 12 +++++-- 2 files changed, 40 insertions(+), 4 deletions(-) rename src/androidTest/java/com/owncloud/android/ui/activity/{FolderPickerActivityTest.java => FolderPickerActivityIT.java} (76%) diff --git a/src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityTest.java b/src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityIT.java similarity index 76% rename from src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityTest.java rename to src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityIT.java index 28052d07dd24..70f29d288465 100644 --- a/src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityTest.java +++ b/src/androidTest/java/com/owncloud/android/ui/activity/FolderPickerActivityIT.java @@ -34,7 +34,7 @@ @RunWith(AndroidJUnit4.class) @LargeTest -public class FolderPickerActivityTest { +public class FolderPickerActivityIT { @Rule public ActivityTestRule activityRule = new ActivityTestRule<>(FolderPickerActivity.class); @@ -89,4 +89,34 @@ public void getParentFolder_isRootFolder() { // Assert Assert.assertEquals(origin, target); } + + @Test + public void nullFile() { + // Arrange + FolderPickerActivity targetActivity = activityRule.getActivity(); + OCFile rootFolder = targetActivity.getStorageManager().getFileByPath(OCFile.ROOT_PATH); + + // Act + targetActivity.setFile(null); + OCFile target = targetActivity.getCurrentFolder(); + + // Assert + Assert.assertEquals(rootFolder, target); + } + + @Test + public void getParentFolder() { + // Arrange + FolderPickerActivity targetActivity = activityRule.getActivity(); + OCFile origin = new OCFile("/test/file.test"); + origin.setRemotePath("/test/file.test"); + + OCFile target = new OCFile("/test/"); + + // Act + targetActivity.setFile(origin); + + // Assert + Assert.assertEquals(origin, target); + } } diff --git a/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java b/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java index 27bc65f70b34..341231628afb 100644 --- a/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.java @@ -250,8 +250,13 @@ public void startSyncFolderOperation(OCFile folder, boolean ignoreETag) { mSyncInProgress = true; // perform folder synchronization - RemoteOperation refreshFolderOperation = new RefreshFolderOperation(folder, currentSyncTime, false, - ignoreETag, getStorageManager(), getAccount(), getApplicationContext()); + RemoteOperation refreshFolderOperation = new RefreshFolderOperation(folder, + currentSyncTime, + false, + ignoreETag, + getStorageManager(), + getAccount(), + getApplicationContext()); refreshFolderOperation.execute(getAccount(), this, null, null); setIndeterminate(true); @@ -501,7 +506,8 @@ public void onReceive(Context context, Intent intent) { if (currentDir == null) { // current folder was removed from the server - DisplayUtils.showSnackMessage(getActivity(), R.string.sync_current_folder_was_removed, + DisplayUtils.showSnackMessage(getActivity(), + R.string.sync_current_folder_was_removed, getCurrentFolder().getFileName()); browseToRoot(); } else {