From 0d001d61f2b69c6e690afd2f46d45b32e4600da4 Mon Sep 17 00:00:00 2001 From: Gouri Panda Date: Sat, 14 May 2022 14:48:00 +0530 Subject: [PATCH] added test cases --- .../kiwixmobile/core/utils/files/FileUtils.kt | 7 +++- .../core/utils/files/FileUtilsTest.kt | 40 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt index b4a35593f..bf1efefaf 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt @@ -291,7 +291,12 @@ object FileUtils { return null } - private fun getDecodedFileName(url: String?, src: String?): String = + /** + * Returns the file name from the url or src. In url it gets the file name from the last '/' and + * if it contains '.'. If the url is null then it'll get the file name from the last '/'. + * If the url and src doesn't exist it returns the empty string. + */ + fun getDecodedFileName(url: String?, src: String?): String = url?.substringAfterLast("/", "") ?.takeIf { it.contains(".") } ?: src?.substringAfterLast("/", "") diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/utils/files/FileUtilsTest.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/utils/files/FileUtilsTest.kt index 4058b9ee5..298c738c2 100644 --- a/core/src/test/java/org/kiwix/kiwixmobile/core/utils/files/FileUtilsTest.kt +++ b/core/src/test/java/org/kiwix/kiwixmobile/core/utils/files/FileUtilsTest.kt @@ -88,4 +88,44 @@ class FileUtilsTest { every { mockFile.path } returns "$fileName$extension" every { mockFile.exists() } returns fileExists } + + @Test + fun `test decode file name`() { + val fileName = + FileUtils.getDecodedFileName( + url = "https://kiwix.org/contributors/contributors_list.pdf", + src = null + ) + assertThat(fileName).isEqualTo("contributors_list.pdf") + } + + @Test + fun `test file name if extension doesn't exist`() { + val fileName = FileUtils.getDecodedFileName(url = "https://kiwix.org/contributors/", src = null) + assertThat(fileName).isEqualTo("") + } + + @Test + fun `test file name if the url and src doesn't exist`() { + val fileName = FileUtils.getDecodedFileName(url = null, src = null) + assertThat(fileName).isEqualTo("") + } + + @Test + fun `test file name if only file name exist`() { + val fileName = FileUtils.getDecodedFileName(src = "android_tutorials.pdf", url = null) + assertThat(fileName).isEqualTo("") + } + + @Test + fun `test file name if url doesn't exist`() { + val fileName = FileUtils.getDecodedFileName(url = null, src = "/html/images/test.png") + assertThat(fileName).isEqualTo("test.png") + } + + @Test + fun `test file name if url and src's extension doesn't exist`() { + val fileName = FileUtils.getDecodedFileName(url = null, src = "/html/images/") + assertThat(fileName).isEqualTo("") + } }