Jelajahi Sumber

add tests

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
Andy Scherzinger 4 tahun lalu
induk
melakukan
2fe3369210

+ 74 - 0
src/androidTest/java/com/owncloud/android/utils/FileUtilTest.kt

@@ -0,0 +1,74 @@
+/*
+ * Nextcloud Android client application
+ *
+ * @author Andy Scherzinger
+ * Copyright (C) 2020 Andy Scherzinger
+ *
+ * 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 <https://www.gnu.org/licenses/>.
+ */
+package com.owncloud.android.utils
+
+import com.owncloud.android.AbstractIT
+import org.junit.Assert
+import org.junit.Test
+import java.io.File
+
+class FileUtilTest : AbstractIT() {
+    @Test
+    fun assertNullInput() {
+        Assert.assertEquals("", FileUtil.getFilenameFromPathString(null))
+    }
+
+    @Test
+    fun assertEmptyInput() {
+        Assert.assertEquals("", FileUtil.getFilenameFromPathString(""))
+    }
+
+    @Test
+    fun assertFileInput() {
+        val file = getDummyFile("empty.txt")
+        Assert.assertEquals("empty.txt", FileUtil.getFilenameFromPathString(file.absolutePath))
+    }
+
+    @Test
+    fun assertSlashInput() {
+        val tempPath = File(FileStorageUtils.getTemporalPath(account.name) + File.pathSeparator + "folder")
+        if (!tempPath.exists()) {
+            Assert.assertTrue(tempPath.mkdirs())
+        }
+        Assert.assertEquals("", FileUtil.getFilenameFromPathString(tempPath.absolutePath))
+    }
+
+    @Test
+    fun assertDotFileInput() {
+        val file = getDummyFile(".dotfile.ext")
+        Assert.assertEquals(".dotfile.ext", FileUtil.getFilenameFromPathString(file.absolutePath))
+    }
+
+    @Test
+    fun assertFolderInput() {
+        val tempPath = File(FileStorageUtils.getTemporalPath(account.name))
+        if (!tempPath.exists()) {
+            Assert.assertTrue(tempPath.mkdirs())
+        }
+
+        Assert.assertEquals("", FileUtil.getFilenameFromPathString(tempPath.absolutePath))
+    }
+
+    @Test
+    fun assertNoFileExtensionInput() {
+        val file = getDummyFile("file")
+        Assert.assertEquals("file", FileUtil.getFilenameFromPathString(file.absolutePath))
+    }
+}

+ 191 - 0
src/androidTest/java/com/owncloud/android/utils/SyncedFolderUtilsTest.kt

@@ -0,0 +1,191 @@
+/*
+ * Nextcloud Android client application
+ *
+ * @author Andy Scherzinger
+ * Copyright (C) 2020 Andy Scherzinger
+ *
+ * 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 <https://www.gnu.org/licenses/>.
+ */
+
+package com.owncloud.android.utils
+
+import com.owncloud.android.AbstractIT
+import com.owncloud.android.datamodel.MediaFolder
+import com.owncloud.android.datamodel.MediaFolderType
+import com.owncloud.android.datamodel.SyncedFolder
+import org.junit.AfterClass
+import org.junit.Assert
+import org.junit.BeforeClass
+import org.junit.Test
+import java.util.Arrays
+
+class SyncedFolderUtilsTest : AbstractIT() {
+    @Test
+    fun assertCoverFilenameUnqualified() {
+        Assert.assertFalse(SyncedFolderUtils.isFileNameQualifiedForMediaDetection(COVER))
+        Assert.assertFalse(SyncedFolderUtils.isFileNameQualifiedForMediaDetection("cover.JPG"))
+        Assert.assertFalse(SyncedFolderUtils.isFileNameQualifiedForMediaDetection("cover.jpeg"))
+        Assert.assertFalse(SyncedFolderUtils.isFileNameQualifiedForMediaDetection("cover.JPEG"))
+        Assert.assertFalse(SyncedFolderUtils.isFileNameQualifiedForMediaDetection("COVER.jpg"))
+        Assert.assertFalse(SyncedFolderUtils.isFileNameQualifiedForMediaDetection(FOLDER))
+        Assert.assertFalse(SyncedFolderUtils.isFileNameQualifiedForMediaDetection("Folder.jpeg"))
+        Assert.assertFalse(SyncedFolderUtils.isFileNameQualifiedForMediaDetection("FOLDER.jpg"))
+    }
+
+    @Test
+    fun assertImageFilenameQualified() {
+        Assert.assertTrue(SyncedFolderUtils.isFileNameQualifiedForMediaDetection("image.jpg"))
+        Assert.assertTrue(SyncedFolderUtils.isFileNameQualifiedForMediaDetection("screenshot.JPG"))
+        Assert.assertTrue(SyncedFolderUtils.isFileNameQualifiedForMediaDetection(IMAGE_JPEG))
+        Assert.assertTrue(SyncedFolderUtils.isFileNameQualifiedForMediaDetection("image.JPEG"))
+        Assert.assertTrue(SyncedFolderUtils.isFileNameQualifiedForMediaDetection("SCREENSHOT.jpg"))
+        Assert.assertTrue(SyncedFolderUtils.isFileNameQualifiedForMediaDetection(SELFIE))
+        Assert.assertTrue(SyncedFolderUtils.isFileNameQualifiedForMediaDetection("screenshot.PNG"))
+    }
+
+    @Test
+    fun assertMediaFolderNullSafe() {
+        val folder: MediaFolder? = null
+        Assert.assertFalse(SyncedFolderUtils.isQualifyingMediaFolder(folder))
+    }
+
+    @Test
+    fun assertMediaFolderCustomQualified() {
+        val folder = MediaFolder()
+        folder.type = MediaFolderType.CUSTOM
+        Assert.assertTrue(SyncedFolderUtils.isQualifyingMediaFolder(folder))
+    }
+
+    @Test
+    fun assertMediaFolderVideoUnqualified() {
+        val folder = MediaFolder()
+        folder.type = MediaFolderType.VIDEO
+        folder.numberOfFiles = 0L
+        Assert.assertFalse(SyncedFolderUtils.isQualifyingMediaFolder(folder))
+    }
+
+    @Test
+    fun assertMediaFolderVideoQualified() {
+        val folder = MediaFolder()
+        folder.type = MediaFolderType.VIDEO
+        folder.numberOfFiles = 20L
+        Assert.assertTrue(SyncedFolderUtils.isQualifyingMediaFolder(folder))
+    }
+
+    @Test
+    fun assertMediaFolderImagesQualified() {
+        val folder = MediaFolder()
+        folder.type = MediaFolderType.IMAGE
+        folder.numberOfFiles = 4L
+        folder.filePaths = Arrays.asList(
+            getDummyFile(SELFIE).absolutePath,
+            getDummyFile(SCREENSHOT).absolutePath,
+            getDummyFile(IMAGE_JPEG).absolutePath,
+            getDummyFile(IMAGE_BITMAP).absolutePath
+        )
+        Assert.assertTrue(SyncedFolderUtils.isQualifyingMediaFolder(folder))
+    }
+
+    @Test
+    fun assertMediaFolderImagesEmptyUnqualified() {
+        val folder = MediaFolder()
+        folder.type = MediaFolderType.IMAGE
+        folder.numberOfFiles = 0L
+        Assert.assertFalse(SyncedFolderUtils.isQualifyingMediaFolder(folder))
+    }
+
+    @Test
+    fun assertMediaFolderImagesNoImagesUnqualified() {
+        val folder = MediaFolder()
+        folder.type = MediaFolderType.IMAGE
+        folder.numberOfFiles = 3L
+        folder.filePaths = Arrays.asList(
+            getDummyFile(SONG_ZERO).absolutePath,
+            getDummyFile(SONG_ONE).absolutePath,
+            getDummyFile(SONG_TWO).absolutePath
+        )
+        Assert.assertFalse(SyncedFolderUtils.isQualifyingMediaFolder(folder))
+    }
+
+    @Test
+    fun assertMediaFolderImagesMusicAlbumWithCoverArtUnqualified() {
+        val folder = MediaFolder()
+        folder.type = MediaFolderType.IMAGE
+        folder.numberOfFiles = 3L
+        folder.filePaths = Arrays.asList(
+            getDummyFile(COVER).absolutePath,
+            getDummyFile(SONG_ONE).absolutePath,
+            getDummyFile(SONG_TWO).absolutePath
+        )
+        Assert.assertFalse(SyncedFolderUtils.isQualifyingMediaFolder(folder))
+    }
+
+    @Test
+    fun assertMediaFolderImagesMusicAlbumWithFolderArtUnqualified() {
+        val folder = MediaFolder()
+        folder.type = MediaFolderType.IMAGE
+        folder.numberOfFiles = 3L
+        folder.filePaths = Arrays.asList(
+            getDummyFile(FOLDER).absolutePath,
+            getDummyFile(SONG_ONE).absolutePath,
+            getDummyFile(SONG_TWO).absolutePath
+        )
+        Assert.assertFalse(SyncedFolderUtils.isQualifyingMediaFolder(folder))
+    }
+
+    @Test
+    fun assertSyncedFolderNullSafe() {
+        val folder: SyncedFolder? = null
+        Assert.assertFalse(SyncedFolderUtils.isQualifyingMediaFolder(folder))
+    }
+
+    companion object {
+        private const val SELFIE = "selfie.png"
+        private const val SCREENSHOT = "screenshot.JPG"
+        private const val IMAGE_JPEG = "image.jpeg"
+        private const val IMAGE_BITMAP = "image.bmp"
+        private const val SONG_ZERO = "song0.mp3"
+        private const val SONG_ONE = "song1.mp3"
+        private const val SONG_TWO = "song2.mp3"
+        private const val FOLDER = "folder.JPG"
+        private const val COVER = "cover.jpg"
+        private const val ITERATION = 100
+
+        @BeforeClass
+        fun setUp() {
+            createFile(SELFIE, ITERATION)
+            createFile(SCREENSHOT, ITERATION)
+            createFile(IMAGE_JPEG, ITERATION)
+            createFile(IMAGE_BITMAP, ITERATION)
+            createFile(SONG_ZERO, ITERATION)
+            createFile(SONG_ONE, ITERATION)
+            createFile(SONG_TWO, ITERATION)
+            createFile(FOLDER, ITERATION)
+            createFile(COVER, ITERATION)
+        }
+
+        @AfterClass
+        fun tearDown() {
+            getDummyFile(SELFIE).delete()
+            getDummyFile(SCREENSHOT).delete()
+            getDummyFile(IMAGE_JPEG).delete()
+            getDummyFile(IMAGE_BITMAP).delete()
+            getDummyFile(SONG_ZERO).delete()
+            getDummyFile(SONG_ONE).delete()
+            getDummyFile(SONG_TWO).delete()
+            getDummyFile(FOLDER).delete()
+            getDummyFile(COVER).delete()
+        }
+    }
+}

+ 8 - 1
src/main/java/com/owncloud/android/utils/FileUtil.java

@@ -32,7 +32,14 @@ public final class FileUtil {
         // utility class -> private constructor
     }
 
-    public static @NonNull String getFilenameFromPathString(@Nullable String filePath) {
+    /**
+     * returns the file name of a given path.
+     *
+     * @param filePath (absolute) file path
+     * @return the filename including its file extension, <code>empty String</code> for invalid input values
+     */
+    public static @NonNull
+    String getFilenameFromPathString(@Nullable String filePath) {
         if (filePath != null && filePath.length() > EMPTY_LENGTH) {
             File file = new File(filePath);
             if (file.isFile()) {