diff --git a/app/src/main/java/com/amaze/filemanager/filesystem/files/FileUtils.java b/app/src/main/java/com/amaze/filemanager/filesystem/files/FileUtils.java index 6b6a6bec65..80c63b8496 100644 --- a/app/src/main/java/com/amaze/filemanager/filesystem/files/FileUtils.java +++ b/app/src/main/java/com/amaze/filemanager/filesystem/files/FileUtils.java @@ -586,27 +586,55 @@ public static String[] getFolderNamesInPath(String path) { * ["smb://user;workgroup:passw0rd@12.3.4", "smb://user;workgroup:passw0rd@12.3.4/user", "smb://user;workgroup:passw0rd@12.3.4/user/Documents", "smb://user;workgroup:passw0rd@12.3.4/user/Documents/flare.doc"] * * - * @param path + * @param pathParam * @return string array of incremental path segments */ - public static String[] getPathsInPath(String path) { + public static String[] getPathsInPath(String pathParam) { + String path = pathParam; + path = path.trim(); + if (path.isEmpty()) { + return new String[0]; + } + if (path.equals("/")) { + return new String[] {"/"}; + } if (path.endsWith("/")) { path = path.substring(0, path.length() - 1); } path = path.trim(); - ArrayList paths = new ArrayList<>(); @Nullable String urlPrefix = null; @Nullable Pair splitUri = splitUri(path); if (splitUri != null) { urlPrefix = splitUri.first; path = splitUri.second; + + if (path == null) { + return new String[] {urlPrefix}; + } } if (!path.startsWith("/")) { path = "/" + path; } + ArrayList paths = buildPaths(path, urlPrefix); + + paths.add(urlPrefix != null ? urlPrefix : "/"); + Collections.reverse(paths); + + return paths.toArray(new String[0]); + } + + /** + * Splits a given path to URI prefix (if exists) and path. + * + * @param pathParam + * @return string array of incremental path segments + */ + public static ArrayList buildPaths(String pathParam, String urlPrefix) { + ArrayList paths = new ArrayList<>(); + String path = pathParam; while (path.length() > 0) { if (urlPrefix != null) { paths.add(urlPrefix + path); @@ -619,15 +647,7 @@ public static String[] getPathsInPath(String path) { break; } } - - if (urlPrefix != null) { - paths.add(urlPrefix); - } else { - paths.add("/"); - } - Collections.reverse(paths); - - return paths.toArray(new String[0]); + return paths; } /** diff --git a/app/src/test/java/com/amaze/filemanager/filesystem/files/FileUtilsTest.kt b/app/src/test/java/com/amaze/filemanager/filesystem/files/FileUtilsTest.kt index a75c54e152..26171046aa 100644 --- a/app/src/test/java/com/amaze/filemanager/filesystem/files/FileUtilsTest.kt +++ b/app/src/test/java/com/amaze/filemanager/filesystem/files/FileUtilsTest.kt @@ -41,6 +41,75 @@ import java.util.TimeZone @Config(sdk = [LOLLIPOP, P, Build.VERSION_CODES.R]) @Suppress("TooManyFunctions", "StringLiteralDuplication") class FileUtilsTest { + /** + * Test FileUtils.getPathsInPath() with empty + * + * @see FileUtils.getPathsInPath + */ + @Test + fun testGetPathsInPathForEmpty() { + getPathsInPath("").run { + assertEquals(0, size) + assertArrayEquals( + arrayOf(), + this, + ) + } + } + + /** + * Test FileUtils.getPathsInPath() with just whitespace + * + * @see FileUtils.getPathsInPath + */ + @Test + fun testGetPathsInPathForWhitespace() { + getPathsInPath(" ").run { + assertEquals(0, size) + assertArrayEquals( + arrayOf(), + this, + ) + } + } + + /** + * Test FileUtils.getPathsInPath() with single slash + * + * @see FileUtils.getPathsInPath + */ + @Test + fun testGetPathsInPathForSingleSlash() { + getPathsInPath("/").run { + assertEquals(1, size) + assertArrayEquals( + arrayOf( + "/", + ), + this, + ) + } + } + + /** + * Test FileUtils.getPathsInPath() for folder with slash at end + * + * @see FileUtils.getPathsInPath + */ + @Test + fun testGetPathsInPathForSingleFolderWithSlashAtEnd() { + getPathsInPath("/dir/").run { + assertEquals(2, size) + assertArrayEquals( + arrayOf( + "/", + "/dir", + ), + this, + ) + } + } + /** * Test FileUtils.getPathsInPath() for directory *