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 8a0768143..97cf7af65 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 class FileUtils { * ["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 class FileUtils { 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 a75c54e15..6e9cb5e9e 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 * @@ -186,6 +255,25 @@ class FileUtilsTest { } } + /** + * Test FileUtils.getPathsInPath() with an URI that is just a scheme. + * + * @see FileUtils.getPathsInPath + */ + @Test + fun testGetPathsInPathOnlyScheme() { + getPathsInPath("file:///").run { + assertEquals(2, size) + assertArrayEquals( + arrayOf( + "file://", + "file:///", + ), + this, + ) + } + } + /** * Test FileUtils.getPathsInPath() with SMB URI *