From fa2b11b7685d989d1e28e62f65ff1f80d890a9d1 Mon Sep 17 00:00:00 2001 From: Alireza Tofighi Date: Fri, 21 May 2021 20:21:58 +0430 Subject: [PATCH] Move ContentSettingsFragment.isValidPath to helpers and add unit test for it. --- .../settings/ContentSettingsFragment.java | 13 ++------ .../schabi/newpipe/util/FilePathUtils.java | 22 +++++++++++++ .../newpipe/util/FilePathHelperTest.java | 32 +++++++++++++++++++ 3 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/util/FilePathUtils.java create mode 100644 app/src/test/java/org/schabi/newpipe/util/FilePathHelperTest.java diff --git a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java index e4850d7fa..3464f1081 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java @@ -26,6 +26,7 @@ import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.localization.ContentCountry; import org.schabi.newpipe.extractor.localization.Localization; import org.schabi.newpipe.util.FilePickerActivityHelper; +import org.schabi.newpipe.util.FilePathUtils; import org.schabi.newpipe.util.ZipHelper; import java.io.File; @@ -67,7 +68,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment { .putExtra(FilePickerActivityHelper.EXTRA_MODE, FilePickerActivityHelper.MODE_FILE); final String path = defaultPreferences.getString(importExportDataPathKey, ""); - if (isValidPath(path)) { + if (FilePathUtils.isValidDirectoryPath(path)) { i.putExtra(FilePickerActivityHelper.EXTRA_START_PATH, path); } startActivityForResult(i, REQUEST_IMPORT_PATH); @@ -82,7 +83,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment { .putExtra(FilePickerActivityHelper.EXTRA_MODE, FilePickerActivityHelper.MODE_DIR); final String path = defaultPreferences.getString(importExportDataPathKey, ""); - if (isValidPath(path)) { + if (FilePathUtils.isValidDirectoryPath(path)) { i.putExtra(FilePickerActivityHelper.EXTRA_START_PATH, path); } startActivityForResult(i, REQUEST_EXPORT_PATH); @@ -254,14 +255,6 @@ public class ContentSettingsFragment extends BasePreferenceFragment { } } - private boolean isValidPath(final String path) { - if (path == null || path.isEmpty()) { - return false; - } - final File file = new File(path); - return file.exists() && file.isDirectory(); - } - private void setImportExportDataPath(final File file) { final String directoryPath; if (!file.isDirectory()) { diff --git a/app/src/main/java/org/schabi/newpipe/util/FilePathUtils.java b/app/src/main/java/org/schabi/newpipe/util/FilePathUtils.java new file mode 100644 index 000000000..4162e563a --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/util/FilePathUtils.java @@ -0,0 +1,22 @@ +package org.schabi.newpipe.util; + +import java.io.File; + +public final class FilePathUtils { + private FilePathUtils() { } + + + /** + * Check that the path is a valid directory path and it exists. + * + * @param path full path of directory, + * @return is path valid or not + */ + public static boolean isValidDirectoryPath(final String path) { + if (path == null || path.isEmpty()) { + return false; + } + final File file = new File(path); + return file.exists() && file.isDirectory(); + } +} diff --git a/app/src/test/java/org/schabi/newpipe/util/FilePathHelperTest.java b/app/src/test/java/org/schabi/newpipe/util/FilePathHelperTest.java new file mode 100644 index 000000000..7cdc5f855 --- /dev/null +++ b/app/src/test/java/org/schabi/newpipe/util/FilePathHelperTest.java @@ -0,0 +1,32 @@ +package org.schabi.newpipe.util; + +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class FilePathHelperTest { + @Test + public void testIsValidDirectoryPath() throws IOException { + // path that exists + final File dir1 = Files.createTempDirectory("dir1").toFile(); + assertTrue(FilePathUtils.isValidDirectoryPath(dir1.getAbsolutePath())); + + // a directory in above path that exists + final File subDir = Files.createDirectory(dir1.toPath().resolve("subdir")).toFile(); + assertTrue(FilePathUtils.isValidDirectoryPath(subDir.getAbsolutePath())); + + // a directory in above path that doesn't exist + assertFalse(FilePathUtils.isValidDirectoryPath(dir1.toPath().resolve("not-exists-subdir"). + toFile().getAbsolutePath())); + + // file is not a valid direcotry path + final File tempFile = Files.createFile(dir1.toPath().resolve("simple_file")).toFile(); + assertFalse(FilePathUtils.isValidDirectoryPath(tempFile.getAbsolutePath())); + } + +}