From 9070e6a16b57a809b1141e96925735d77e00add2 Mon Sep 17 00:00:00 2001 From: Sean Mac Gillicuddy Date: Fri, 7 Jun 2019 11:00:45 +0100 Subject: [PATCH] #1193 fix or migrate unit tests --- .../kiwixmobile/downloader/ChunkUtilsTest.kt | 32 +-- .../entity/MetaLinkNetworkEntityTest.java | 2 +- .../{BookUtilsTest.java => BookUtilsTest.kt} | 47 ++-- .../kiwixmobile/utils/NetworkUtilsTest.java | 207 --------------- .../kiwixmobile/utils/NetworkUtilsTest.kt | 239 ++++++++++++++++++ .../kiwixmobile/utils/files/FileUtilsTest.kt | 2 +- .../library_view/LibraryUtilsTest.java | 3 +- 7 files changed, 277 insertions(+), 255 deletions(-) rename app/src/test/java/org/kiwix/kiwixmobile/utils/{BookUtilsTest.java => BookUtilsTest.kt} (62%) delete mode 100644 app/src/test/java/org/kiwix/kiwixmobile/utils/NetworkUtilsTest.java create mode 100644 app/src/test/java/org/kiwix/kiwixmobile/utils/NetworkUtilsTest.kt diff --git a/app/src/test/java/org/kiwix/kiwixmobile/downloader/ChunkUtilsTest.kt b/app/src/test/java/org/kiwix/kiwixmobile/downloader/ChunkUtilsTest.kt index 7417af464..ef3f3be99 100644 --- a/app/src/test/java/org/kiwix/kiwixmobile/downloader/ChunkUtilsTest.kt +++ b/app/src/test/java/org/kiwix/kiwixmobile/downloader/ChunkUtilsTest.kt @@ -20,9 +20,9 @@ package org.kiwix.kiwixmobile.downloader import io.mockk.every import io.mockk.mockkStatic +import org.assertj.core.api.Java6Assertions.assertThat import org.junit.Assert.assertEquals -import org.junit.Before -import org.junit.Test +import org.junit.jupiter.api.Test import org.kiwix.kiwixmobile.utils.StorageUtils class ChunkUtilsTest { @@ -30,8 +30,7 @@ class ChunkUtilsTest { private val URL = "http://mirror.netcologne.de/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim" - @Before - fun executeBefore() { + init { mockkStatic("org.kiwix.kiwixmobile.utils.StorageUtils") every { StorageUtils.getFileNameFromUrl(URL) }.returns("TestFileName") every { StorageUtils.getFileNameFromUrl("TestURL") }.returns("TestFileName.xml") @@ -59,7 +58,7 @@ class ChunkUtilsTest { ) assertEquals( "verify that the file name is correctly assigned in case of a single file", - "TestFileName.part", listReturned[0].fileName + "TestFileName.part.part", listReturned[0].fileName ) assertEquals( "verify that the same URL is passed on to the chunk", URL, @@ -103,19 +102,14 @@ class ChunkUtilsTest { ) // test assignment of file names - var test = true val ALPHABET = "abcdefghijklmnopqrstuvwxyz" for (i in listReturned.indices) { - if (listReturned[i] - .fileName - .substring( - listReturned[i].fileName.length - 11 - ) != ".zim" + ALPHABET[i / 26] + ALPHABET[i % 26] + ".part" - ) { - test = false - } + val extension = listReturned[i] + .fileName + .substringAfter('.') + val expectedExtension = "zim" + ALPHABET[i / 26] + ALPHABET[i % 26] + ".part.part" + assertThat(extension).isEqualTo(expectedExtension) } - assertEquals("verify that the file name endings are correctly assigned", true, test) // When the file size is less than CHUNK_SIZE size = ChunkUtils.CHUNK_SIZE - (1024 * 1024).toLong() @@ -134,7 +128,7 @@ class ChunkUtilsTest { ) assertEquals( "verify that the file name is correctly assigned in case of a single file", - "TestFileName.part", listReturned[0].fileName + "TestFileName.part.part", listReturned[0].fileName ) assertEquals( "verify that the same URL is passed on to the chunk", URL, @@ -146,18 +140,18 @@ class ChunkUtilsTest { listReturned = ChunkUtils.getChunks("TestURL", size, 0) assertEquals( "verify that previous extension in the filename (if any) is removed in case of files having 1 chunk", - "TestFileName.xml.part", listReturned[0].fileName + "TestFileName.xml.part.part", listReturned[0].fileName ) size = ChunkUtils.CHUNK_SIZE * 2.toLong() listReturned = ChunkUtils.getChunks("TestURL", size, 0) assertEquals( "verify that previous extension in the filename (if any) is removed in case of files having more than 1 chunk", - "TestFileName.zimaa.part", listReturned[0].fileName + "TestFileName.zimaa.part.part", listReturned[0].fileName ) assertEquals( "verify that previous extension in the filename (if any) is removed in case of files having more than 1 chunk", - "TestFileName.zimab.part", listReturned[1].fileName + "TestFileName.zimab.part.part", listReturned[1].fileName ) } } diff --git a/app/src/test/java/org/kiwix/kiwixmobile/library/entity/MetaLinkNetworkEntityTest.java b/app/src/test/java/org/kiwix/kiwixmobile/library/entity/MetaLinkNetworkEntityTest.java index b3cc5a0bb..75e3c5bce 100644 --- a/app/src/test/java/org/kiwix/kiwixmobile/library/entity/MetaLinkNetworkEntityTest.java +++ b/app/src/test/java/org/kiwix/kiwixmobile/library/entity/MetaLinkNetworkEntityTest.java @@ -22,7 +22,7 @@ import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; diff --git a/app/src/test/java/org/kiwix/kiwixmobile/utils/BookUtilsTest.java b/app/src/test/java/org/kiwix/kiwixmobile/utils/BookUtilsTest.kt similarity index 62% rename from app/src/test/java/org/kiwix/kiwixmobile/utils/BookUtilsTest.java rename to app/src/test/java/org/kiwix/kiwixmobile/utils/BookUtilsTest.kt index 461ad6f1c..20344f0bc 100644 --- a/app/src/test/java/org/kiwix/kiwixmobile/utils/BookUtilsTest.java +++ b/app/src/test/java/org/kiwix/kiwixmobile/utils/BookUtilsTest.kt @@ -16,42 +16,37 @@ * along with this program. If not, see . */ -package org.kiwix.kiwixmobile.utils; +package org.kiwix.kiwixmobile.utils -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import io.mockk.every +import io.mockk.mockk +import org.junit.Assert.assertEquals +import org.junit.jupiter.api.Test -import static org.junit.Assert.assertEquals; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.when; - -@RunWith(MockitoJUnitRunner.class) -public class BookUtilsTest { - @Mock private LanguageUtils.LanguageContainer container; +class BookUtilsTest { + private val container: LanguageUtils.LanguageContainer = mockk() //Test that the language returned for the given language code is correct @Test - public void testLanguageFromCode() { - BookUtils t = new BookUtils(container); + fun testLanguageFromCode() { + val t = BookUtils(container) //testing trivial cases - assertEquals("null is passed", "", t.getLanguage(null)); - assertEquals("empty string passed", "", t.getLanguage("")); - assertEquals("code length more than 3", "", t.getLanguage("english")); + assertEquals("null is passed", "", t.getLanguage(null)) + assertEquals("empty string passed", "", t.getLanguage("")) + assertEquals("code length more than 3", "", t.getLanguage("english")) //testing the hashmap created inside the BookUtils class - assertEquals("code length equals 3 (English)", "English", t.getLanguage("eng")); - assertEquals("code length equals 3 (Hindi)", "Hindi", t.getLanguage("hin")); - assertEquals("code length equals 3 (French)", "French", t.getLanguage("fra")); - assertEquals("code length equals 3 (Akan)", "Akan", t.getLanguage("aka")); - assertEquals("code length equals 3 (Burmese)", "Burmese", t.getLanguage("mya")); - assertEquals("code length equals 3 (Catalan)", "Catalan", t.getLanguage("cat")); + assertEquals("code length equals 3 (English)", "English", t.getLanguage("eng")) + assertEquals("code length equals 3 (Hindi)", "Hindi", t.getLanguage("hin")) + assertEquals("code length equals 3 (French)", "French", t.getLanguage("fra")) + assertEquals("code length equals 3 (Akan)", "Akan", t.getLanguage("aka")) + assertEquals("code length equals 3 (Burmese)", "Burmese", t.getLanguage("mya")) + assertEquals("code length equals 3 (Catalan)", "Catalan", t.getLanguage("cat")) //this case uses the result from the container nested class inside LanguageUtils. It will be tested in LanguageUtilsTest - when(container.findLanguageName(anyString())).thenReturn(container); - when(container.getLanguageName()).thenReturn("English"); - assertEquals("code length equals 2 (dummy)", "English", t.getLanguage("en")); + every { container.findLanguageName(any()) } returns container + every { container.languageName} returns "English" + assertEquals("code length equals 2 (dummy)", "English", t.getLanguage("en")) } } diff --git a/app/src/test/java/org/kiwix/kiwixmobile/utils/NetworkUtilsTest.java b/app/src/test/java/org/kiwix/kiwixmobile/utils/NetworkUtilsTest.java deleted file mode 100644 index 0f6a9d4d0..000000000 --- a/app/src/test/java/org/kiwix/kiwixmobile/utils/NetworkUtilsTest.java +++ /dev/null @@ -1,207 +0,0 @@ -/* - * Kiwix Android - * Copyright (C) 2018 Kiwix - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.kiwix.kiwixmobile.utils; - -import android.content.Context; -import android.net.ConnectivityManager; -import android.net.NetworkInfo; -import android.os.Build; -import android.util.Log; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.kiwix.kiwixmobile.R; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@RunWith(MockitoJUnitRunner.class) -public class NetworkUtilsTest { - - @Mock private Context context; - @Mock private ConnectivityManager connectivity; - @Mock private NetworkInfo networkInfo; - @Mock private NetworkInfo networkInfo1; - @Mock private NetworkInfo networkInfo2; - - @Test - public void testNetworkAvailability() { - NetworkInfo[] networkInfos = { networkInfo1, networkInfo2 }; - when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivity); - when(connectivity.getAllNetworkInfo()).thenReturn(networkInfos); - - //one network is connected - when(networkInfo1.getState()).thenReturn(NetworkInfo.State.CONNECTED); - when(networkInfo2.getState()).thenReturn(NetworkInfo.State.DISCONNECTING); - assertEquals(true, NetworkUtils.isNetworkAvailable(context)); - - when(networkInfo1.getState()).thenReturn(NetworkInfo.State.DISCONNECTING); - when(networkInfo2.getState()).thenReturn(NetworkInfo.State.CONNECTING); - assertEquals(false, NetworkUtils.isNetworkAvailable(context)); - - //no network is available - when(networkInfo1.getState()).thenReturn(NetworkInfo.State.DISCONNECTED); - when(networkInfo2.getState()).thenReturn(NetworkInfo.State.DISCONNECTED); - assertEquals(false, NetworkUtils.isNetworkAvailable(context)); - } - - @Test - public void test_isNetworkConnectionOK() { - when(networkInfo2.getState()).thenReturn(NetworkInfo.State.CONNECTING); - assertFalse(NetworkUtils.isNetworkConnectionOK(networkInfo2)); - - when(networkInfo2.getState()).thenReturn(NetworkInfo.State.CONNECTED); - assertTrue(NetworkUtils.isNetworkConnectionOK(networkInfo2)); - } - - @Test - public void testWifiAvailability() { - when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivity); - when(connectivity.getActiveNetworkInfo()).thenReturn(networkInfo); - - //SDK >= 23 - try { - SetSDKVersion(Build.VERSION.class.getField("SDK_INT"), 23); - } catch (Exception e) { - Log.d("NetworkUtilsTest", "Unable to set Build SDK Version"); - } - - //on Mobile Data - when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_MOBILE); - assertEquals(false, NetworkUtils.isWiFi(context)); - //verify that the correct methods are used according to the build SDK version - verify(connectivity).getActiveNetworkInfo(); - verify(networkInfo).getType(); - verify(connectivity, never()).getNetworkInfo(ConnectivityManager.TYPE_WIFI); - - //on WIFI connected - when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI); - when(networkInfo.isConnected()).thenReturn(Boolean.TRUE); - assertEquals(true, NetworkUtils.isWiFi(context)); - verify(connectivity, times(2)).getActiveNetworkInfo(); - verify(connectivity, never()).getNetworkInfo(ConnectivityManager.TYPE_WIFI); - - //on WIFI disconnected - when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI); - when(networkInfo.isConnected()).thenReturn(Boolean.FALSE); - assertEquals(false, NetworkUtils.isWiFi(context)); - verify(connectivity, times(3)).getActiveNetworkInfo(); - verify(connectivity, never()).getNetworkInfo(ConnectivityManager.TYPE_WIFI); - - //SDK < 23 - try { - SetSDKVersion(Build.VERSION.class.getField("SDK_INT"), 22); - } catch (Exception e) { - Log.d("NetworkUtilsTest", "Unable to set Build SDK Version"); - } - - //WIFI connected - when(connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI)).thenReturn(networkInfo); - when(networkInfo.isConnected()).thenReturn(true); - assertEquals(true, NetworkUtils.isWiFi(context)); - verify(connectivity, times(1)).getNetworkInfo(ConnectivityManager.TYPE_WIFI); - - //WIFI disconnected - when(connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI)).thenReturn(networkInfo); - when(networkInfo.isConnected()).thenReturn(false); - assertEquals(false, NetworkUtils.isWiFi(context)); - verify(connectivity, times(2)).getNetworkInfo(ConnectivityManager.TYPE_WIFI); - } - - @Test - public void testFilenameFromUrl() { - // TODO: find a way to assert regex matching via JUnit and rewrite the test - - String defaultUUIDRegex = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"; - Pattern pattern = Pattern.compile(defaultUUIDRegex); - - // URL is an Empty String - Matcher matcher = pattern.matcher(NetworkUtils.getFileNameFromUrl("")); - if (!matcher.matches()) { - assertEquals("filename doesn't match UUID regex (for empty string URL)", 0, 1); - } - - // URL contains no '?' character but has '/' characters - assertEquals("File Name from URL (no '?' character)", "q=kiwix+android", - NetworkUtils.getFileNameFromUrl("https://github.com/search/q=kiwix+android")); - // and ends with a '/' character - matcher = pattern.matcher( - NetworkUtils.getFileNameFromUrl("https://github.com/search/q=kiwix+android/")); - if (!matcher.matches()) { - assertEquals("filename doesn't match UUID regex (for no '?' and '/' in end)", 0, 1); - } - - // Empty string between last '?' and preceding '/' - matcher = pattern.matcher( - NetworkUtils.getFileNameFromUrl("https://github.com/search/?q=kiwix+android")); - if (!matcher.matches()) { - assertEquals("filename doesn't match UUID regex (for consecutive '/?')", 0, 1); - } - - // Standard case - // Here the Method should return the substring between the first '?' character and the nearest '/' character preceeding it - assertEquals("File Name from URL standard case", "search", NetworkUtils.getFileNameFromUrl( - "https://www.google.com/search?source=hp&ei=zs4LW6W1E5n6rQH65Z-wDQ&q=kiwix+android&oq=kiwix+android&gs_l=psy-ab.3...2590.6259.0.6504.14.12.0.0.0.0.263.485.2-2.2.0....0...1c.1.64.psy-ab..12.2.485.0..0j35i39k1.0.WSlGY7hWzTo")); - assertEquals("File Name from URL standard case", "Special:Search", - NetworkUtils.getFileNameFromUrl( - "https://en.wikipedia.org/wiki/Special:Search?search=kiwix+android&go=Go&searchToken=3zrcxw8fltdcij99zvoh5c6wy")); - assertEquals("File Name from URL standard case", "search", - NetworkUtils.getFileNameFromUrl("https://github.com/search?q=kiwix+android")); - } - - @Test - public void testParsedURL() { - when(context.getString(R.string.zim_nopic)).thenReturn("No Pictures"); - when(context.getString(R.string.zim_novid)).thenReturn("No Videos"); - when(context.getString(R.string.zim_simple)).thenReturn("Simple"); - - assertEquals("URL Parsing on empty string", "", NetworkUtils.parseURL(context, "")); - - //Using the standard Kiwix Download URLs - assertEquals("URL Parsing", "No Pictures", NetworkUtils.parseURL(context, - "http://ftpmirror.your.org/pub/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim")); - assertEquals("URL Parsing", "No Videos", NetworkUtils.parseURL(context, - "http://www.mirrorservice.org/sites/download.kiwix.org/zim/wikipedia/wikipedia_af_all_novid_2016-05.zim")); - assertEquals("URL Parsing", "Simple", NetworkUtils.parseURL(context, - "http://download.wikimedia.org/kiwix/zim/wikipedia/wikipedia_af_all_simple_2016-05.zim")); - assertEquals("URL Parsing", "No Pictures", NetworkUtils.parseURL(context, - "http://mirror.netcologne.de/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim")); - assertEquals("URL Parsing", "Simple", NetworkUtils.parseURL(context, - "http://mirror3.kiwix.org/zim/wikipedia/wikipedia_af_all_simple_2016-05.zim")); - } - - //Sets the Build SDK version - private static void SetSDKVersion(Field field, Object newValue) throws Exception { - field.setAccessible(true); - Field modifiersField = Field.class.getDeclaredField("modifiers"); - modifiersField.setAccessible(true); - modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); - field.set(null, newValue); - } -} diff --git a/app/src/test/java/org/kiwix/kiwixmobile/utils/NetworkUtilsTest.kt b/app/src/test/java/org/kiwix/kiwixmobile/utils/NetworkUtilsTest.kt new file mode 100644 index 000000000..d58f42ba6 --- /dev/null +++ b/app/src/test/java/org/kiwix/kiwixmobile/utils/NetworkUtilsTest.kt @@ -0,0 +1,239 @@ +/* + * Kiwix Android + * Copyright (C) 2018 Kiwix + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.kiwix.kiwixmobile.utils + +import android.content.Context +import android.net.ConnectivityManager +import android.net.NetworkInfo +import android.os.Build +import android.util.Log +import io.mockk.every +import io.mockk.mockk +import io.mockk.verify +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.jupiter.api.Test +import org.kiwix.kiwixmobile.R +import java.lang.reflect.Field +import java.lang.reflect.Modifier +import java.util.regex.Pattern + +class NetworkUtilsTest { + + private val context: Context = mockk() + private val connectivity: ConnectivityManager = mockk() + private val networkInfo: NetworkInfo = mockk() + private val networkInfo1: NetworkInfo = mockk() + private val networkInfo2: NetworkInfo = mockk() + + @Test + fun testNetworkAvailability() { + val networkInfos = arrayOf(networkInfo1, networkInfo2) + every { context.getSystemService(Context.CONNECTIVITY_SERVICE) } returns connectivity + every { connectivity.allNetworkInfo } returns networkInfos + + //one network is connected + every { (networkInfo1.state) } returns NetworkInfo.State.CONNECTED + every { (networkInfo2.state) } returns NetworkInfo.State.DISCONNECTING + assertEquals(true, NetworkUtils.isNetworkAvailable(context)) + + every { (networkInfo1.state) } returns NetworkInfo.State.DISCONNECTING + every { (networkInfo2.state) } returns NetworkInfo.State.CONNECTING + assertEquals(false, NetworkUtils.isNetworkAvailable(context)) + + //no network is available + every { (networkInfo1.state) } returns NetworkInfo.State.DISCONNECTED + every { (networkInfo2.state) } returns NetworkInfo.State.DISCONNECTED + assertEquals(false, NetworkUtils.isNetworkAvailable(context)) + } + + @Test + fun test_isNetworkConnectionOK() { + every { (networkInfo2.state) } returns NetworkInfo.State.CONNECTING + assertFalse(NetworkUtils.isNetworkConnectionOK(networkInfo2)) + + every { (networkInfo2.state) } returns NetworkInfo.State.CONNECTED + assertTrue(NetworkUtils.isNetworkConnectionOK(networkInfo2)) + } + + @Test + fun testWifiAvailability() { + every { (context.getSystemService(Context.CONNECTIVITY_SERVICE)) } returns connectivity + every { (connectivity.activeNetworkInfo) } returns networkInfo + + //SDK >= 23 + try { + SetSDKVersion(Build.VERSION::class.java.getField("SDK_INT"), 23) + } catch (e: Exception) { + Log.d("NetworkUtilsTest", "Unable to set Build SDK Version") + } + + //on Mobile Data + every { (networkInfo.type) } returns ConnectivityManager.TYPE_MOBILE + assertEquals(false, NetworkUtils.isWiFi(context)) + //verify that the correct methods are used according to the build SDK version + verify { + connectivity.activeNetworkInfo + networkInfo.type + } + verify(exactly = 0) { connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI) } + + //on WIFI connected + every { (networkInfo.type) } returns ConnectivityManager.TYPE_WIFI + every { (networkInfo.isConnected) } returns java.lang.Boolean.TRUE + assertEquals(true, NetworkUtils.isWiFi(context)) + verify(exactly = 2) { connectivity.activeNetworkInfo } + verify(exactly = 0) { connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI) } + + //on WIFI disconnected + every { (networkInfo.type) } returns ConnectivityManager.TYPE_WIFI + every { (networkInfo.isConnected) } returns java.lang.Boolean.FALSE + assertEquals(false, NetworkUtils.isWiFi(context)) + verify(exactly = 3) { connectivity.activeNetworkInfo } + verify(exactly = 0) { connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI) } + + //SDK < 23 + try { + SetSDKVersion(Build.VERSION::class.java.getField("SDK_INT"), 22) + } catch (e: Exception) { + Log.d("NetworkUtilsTest", "Unable to set Build SDK Version") + } + + //WIFI connected + every { (connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI)) } returns networkInfo + every { (networkInfo.isConnected) } returns true + assertEquals(true, NetworkUtils.isWiFi(context)) + verify { connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI) } + + //WIFI disconnected + every { (connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI)) } returns networkInfo + every { (networkInfo.isConnected) } returns false + assertEquals(false, NetworkUtils.isWiFi(context)) + verify(exactly = 2) { connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI) } + } + + @Test + fun testFilenameFromUrl() { + // TODO: find a way to assert regex matching via JUnit and rewrite the test + + val defaultUUIDRegex = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" + val pattern = Pattern.compile(defaultUUIDRegex) + + // URL is an Empty String + var matcher = pattern.matcher(NetworkUtils.getFileNameFromUrl("")) + if (!matcher.matches()) { + assertEquals("filename doesn't match UUID regex (for empty string URL)", 0, 1) + } + + // URL contains no '?' character but has '/' characters + assertEquals( + "File Name from URL (no '?' character)", "q=kiwix+android", + NetworkUtils.getFileNameFromUrl("https://github.com/search/q=kiwix+android") + ) + // and ends with a '/' character + matcher = pattern.matcher( + NetworkUtils.getFileNameFromUrl("https://github.com/search/q=kiwix+android/") + ) + if (!matcher.matches()) { + assertEquals("filename doesn't match UUID regex (for no '?' and '/' in end)", 0, 1) + } + + // Empty string between last '?' and preceding '/' + matcher = pattern.matcher( + NetworkUtils.getFileNameFromUrl("https://github.com/search/?q=kiwix+android") + ) + if (!matcher.matches()) { + assertEquals("filename doesn't match UUID regex (for consecutive '/?')", 0, 1) + } + + // Standard case + // Here the Method should return the substring between the first '?' character and the nearest '/' character preceeding it + assertEquals( + "File Name from URL standard case", "search", NetworkUtils.getFileNameFromUrl( + "https://www.google.com/search?source=hp&ei=zs4LW6W1E5n6rQH65Z-wDQ&q=kiwix+android&oq=kiwix+android&gs_l=psy-ab.3...2590.6259.0.6504.14.12.0.0.0.0.263.485.2-2.2.0....0...1c.1.64.psy-ab..12.2.485.0..0j35i39k1.0.WSlGY7hWzTo" + ) + ) + assertEquals( + "File Name from URL standard case", "Special:Search", + NetworkUtils.getFileNameFromUrl( + "https://en.wikipedia.org/wiki/Special:Search?search=kiwix+android&go=Go&searchToken=3zrcxw8fltdcij99zvoh5c6wy" + ) + ) + assertEquals( + "File Name from URL standard case", "search", + NetworkUtils.getFileNameFromUrl("https://github.com/search?q=kiwix+android") + ) + } + + @Test + fun testParsedURL() { + every { (context.getString(R.string.zim_nopic)) } returns "No Pictures" + every { (context.getString(R.string.zim_novid)) } returns "No Videos" + every { (context.getString(R.string.zim_simple)) } returns "Simple" + + assertEquals("URL Parsing on empty string", "", NetworkUtils.parseURL(context, "")) + + //Using the standard Kiwix Download URLs + assertEquals( + "URL Parsing", "No Pictures", NetworkUtils.parseURL( + context, + "http://ftpmirror.your.org/pub/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim" + ) + ) + assertEquals( + "URL Parsing", "No Videos", NetworkUtils.parseURL( + context, + "http://www.mirrorservice.org/sites/download.kiwix.org/zim/wikipedia/wikipedia_af_all_novid_2016-05.zim" + ) + ) + assertEquals( + "URL Parsing", "Simple", NetworkUtils.parseURL( + context, + "http://download.wikimedia.org/kiwix/zim/wikipedia/wikipedia_af_all_simple_2016-05.zim" + ) + ) + assertEquals( + "URL Parsing", "No Pictures", NetworkUtils.parseURL( + context, + "http://mirror.netcologne.de/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim" + ) + ) + assertEquals( + "URL Parsing", "Simple", NetworkUtils.parseURL( + context, + "http://mirror3.kiwix.org/zim/wikipedia/wikipedia_af_all_simple_2016-05.zim" + ) + ) + } + + //Sets the Build SDK version + @Throws(Exception::class) + private fun SetSDKVersion( + field: Field, + newValue: Any + ) { + field.isAccessible = true + val modifiersField = Field::class.java.getDeclaredField("modifiers") + modifiersField.isAccessible = true + modifiersField.setInt(field, field.modifiers and Modifier.FINAL.inv()) + field.set(null, newValue) + } +} + diff --git a/app/src/test/java/org/kiwix/kiwixmobile/utils/files/FileUtilsTest.kt b/app/src/test/java/org/kiwix/kiwixmobile/utils/files/FileUtilsTest.kt index e97f2d0df..1e024c6db 100644 --- a/app/src/test/java/org/kiwix/kiwixmobile/utils/files/FileUtilsTest.kt +++ b/app/src/test/java/org/kiwix/kiwixmobile/utils/files/FileUtilsTest.kt @@ -75,7 +75,7 @@ class FileUtilsTest { assertThat(files.size).isEqualTo(1) .withFailMessage("Only a single book is returned in case the file has extension $extension") if (fileExists) { - assertThat(testBook.file.toString()).isEqualTo(files[0].path) + assertThat(testBook.file).isEqualTo(files[0]) .withFailMessage("The filename retained as such") } else { assertThat(testBook.file.toString() + ".part").isEqualTo(files[0].path) diff --git a/app/src/test/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryUtilsTest.java b/app/src/test/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryUtilsTest.java index ca6ba41a1..8014a9e28 100644 --- a/app/src/test/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryUtilsTest.java +++ b/app/src/test/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryUtilsTest.java @@ -18,7 +18,8 @@ package org.kiwix.kiwixmobile.zim_manager.library_view; -import org.junit.Test; + +import org.junit.jupiter.api.Test; import static org.junit.Assert.assertEquals;