diff --git a/app/src/test/java/org/mozilla/fenix/ext/ActivityTest.kt b/app/src/test/java/org/mozilla/fenix/ext/ActivityTest.kt new file mode 100644 index 000000000..7b9e9f289 --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/ext/ActivityTest.kt @@ -0,0 +1,44 @@ +package org.mozilla.fenix.ext + +import kotlinx.coroutines.ObsoleteCoroutinesApi +import org.mozilla.fenix.TestApplication +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.Assert.assertTrue +import org.junit.Assert.assertEquals +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import android.app.Activity +import android.view.View +import android.view.WindowManager +import org.robolectric.Shadows.shadowOf + +@ObsoleteCoroutinesApi +@RunWith(RobolectricTestRunner::class) +@Config(application = TestApplication::class) + +class ActivityTest { + + @Test + fun testEnterImmersiveMode() { + val activity = Robolectric.buildActivity(Activity::class.java).create().get() + val window = activity.getWindow() + + // Turn off Keep Screen on Flag if it is on + if (shadowOf(window).getFlag(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)) window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) + + // Make sure that System UI flags are not set before the test + val flags = arrayOf(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION, View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN, View.SYSTEM_UI_FLAG_HIDE_NAVIGATION, View.SYSTEM_UI_FLAG_FULLSCREEN, View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) + if (flags.any { f -> (window.getDecorView().getSystemUiVisibility() and f) == f }) { + window.getDecorView().setSystemUiVisibility(0) + } + + // Run + activity.enterToImmersiveMode() + + // Test + for (f in flags) assertEquals(f, window.decorView.systemUiVisibility and f) + assertTrue(shadowOf(window).getFlag(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)) + } +} diff --git a/app/src/test/java/org/mozilla/fenix/ext/BrowserIconsTest.kt b/app/src/test/java/org/mozilla/fenix/ext/BrowserIconsTest.kt new file mode 100644 index 000000000..06e3cbe46 --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/ext/BrowserIconsTest.kt @@ -0,0 +1,31 @@ +package org.mozilla.fenix.ext + +import mozilla.components.support.test.robolectric.testContext +import android.widget.ImageView +import kotlinx.coroutines.ObsoleteCoroutinesApi +import io.mockk.verify +import io.mockk.spyk +import mozilla.components.browser.icons.IconRequest +import mozilla.components.lib.fetch.httpurlconnection.HttpURLConnectionClient +import org.junit.Test +import org.mozilla.fenix.TestApplication +import mozilla.components.browser.icons.BrowserIcons +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import org.junit.runner.RunWith + +@ObsoleteCoroutinesApi +@RunWith(RobolectricTestRunner::class) +@Config(application = TestApplication::class) + +class BrowserIconsTest { + @Test + fun loadIntoViewTest() { + val imageView = spyk(ImageView(testContext)) + val icons = spyk(BrowserIcons(testContext, httpClient = HttpURLConnectionClient())) + val myUrl = "https://mozilla.com" + val request = spyk(IconRequest(url = myUrl)) + icons.loadIntoView(imageView, myUrl) + verify { icons.loadIntoView(imageView, request) } + } +} diff --git a/app/src/test/java/org/mozilla/fenix/ext/DrawableTest.kt b/app/src/test/java/org/mozilla/fenix/ext/DrawableTest.kt new file mode 100644 index 000000000..12a7bf1bb --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/ext/DrawableTest.kt @@ -0,0 +1,43 @@ +package org.mozilla.fenix.ext + +import kotlinx.coroutines.ObsoleteCoroutinesApi +import org.mozilla.fenix.TestApplication +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import android.graphics.drawable.Drawable +import android.graphics.Rect +import android.graphics.Canvas +import android.graphics.ColorFilter + +@ObsoleteCoroutinesApi +@RunWith(RobolectricTestRunner::class) +@Config(application = TestApplication::class) + +class DrawableTest { + @Test + fun testSetBounds() { + val drawable = TestDrawable() + assertFalse(drawable.boundsChanged) + val size = 10 + drawable.setBounds(size) + assertTrue(drawable.boundsChanged) + val returnRec = drawable.copyBounds() + assertTrue(returnRec.contains(0, 0, -10, 10)) + } + + private class TestDrawable() : Drawable() { + var boundsChanged: Boolean = false + override fun getOpacity(): Int { return 0 } + override fun draw(canvas: Canvas) {} + override fun setAlpha(alpha: Int) {} + override fun setColorFilter(cf: ColorFilter) {} + protected override fun onBoundsChange(bounds: Rect) { + boundsChanged = true + super.onBoundsChange(bounds) + } + } +} diff --git a/app/src/test/java/org/mozilla/fenix/ext/FragmentTest.kt b/app/src/test/java/org/mozilla/fenix/ext/FragmentTest.kt new file mode 100644 index 000000000..178fbf49f --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/ext/FragmentTest.kt @@ -0,0 +1,88 @@ +package org.mozilla.fenix.ext + +import kotlinx.coroutines.ObsoleteCoroutinesApi +import mozilla.components.support.test.robolectric.testContext +import org.mozilla.fenix.TestApplication +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import io.mockk.mockk +import io.mockk.spyk +import io.mockk.mockkStatic +import io.mockk.verify +import io.mockk.every +import io.mockk.Runs +import io.mockk.just +import io.mockk.confirmVerified +import androidx.fragment.app.Fragment +import androidx.navigation.NavDirections +import androidx.navigation.NavOptions +import androidx.navigation.fragment.NavHostFragment.findNavController +import androidx.navigation.fragment.NavHostFragment +import androidx.navigation.NavDestination +import androidx.navigation.NavController +import androidx.navigation.Navigator.Extras + +@ObsoleteCoroutinesApi +@RunWith(RobolectricTestRunner::class) +@Config(application = TestApplication::class) + +class FragmentTest { + + val navDirections: NavDirections = mockk(relaxed = true) + val mockDestination = spyk(NavDestination("hi")) + val mockExtras: Extras = mockk(relaxed = true) + val mockId = 4 + val navController = spyk(NavController(testContext)) + val mockFragment: Fragment = mockk(relaxed = true) + val mockOptions: NavOptions = mockk(relaxed = true) + + @Test + fun `Test nav fun with ID and directions`() { + mockkStatic(NavHostFragment::class) + every { (NavHostFragment.findNavController(mockFragment)) } returns navController + every { (NavHostFragment.findNavController(mockFragment).getCurrentDestination()) } returns mockDestination + every { (NavHostFragment.findNavController(mockFragment).navigate(navDirections)) } just Runs + every { (mockDestination.getId()) } returns mockId + every { (navController.getCurrentDestination()) } returns mockDestination + every { (NavHostFragment.findNavController(mockFragment).getCurrentDestination()?.getId()) } answers { (mockDestination.getId()) } + + mockFragment.nav(mockId, navDirections) + verify { (NavHostFragment.findNavController(mockFragment).getCurrentDestination()) } + verify { (NavHostFragment.findNavController(mockFragment).navigate(navDirections)) } + confirmVerified(mockFragment) + } + + @Test + fun `Test nav fun with ID, directions, and extras`() { + mockkStatic(NavHostFragment::class) + every { (NavHostFragment.findNavController(mockFragment)) } returns navController + every { (NavHostFragment.findNavController(mockFragment).getCurrentDestination()) } returns mockDestination + every { (NavHostFragment.findNavController(mockFragment).navigate(navDirections, mockExtras)) } just Runs + every { (mockDestination.getId()) } returns mockId + every { (navController.getCurrentDestination()) } returns mockDestination + every { (NavHostFragment.findNavController(mockFragment).getCurrentDestination()?.getId()) } answers { (mockDestination.getId()) } + + mockFragment.nav(mockId, navDirections, mockExtras) + verify { (NavHostFragment.findNavController(mockFragment).getCurrentDestination()) } + verify { (NavHostFragment.findNavController(mockFragment).navigate(navDirections, mockExtras)) } + confirmVerified(mockFragment) + } + + @Test + fun `Test nav fun with ID, directions, and options`() { + mockkStatic(NavHostFragment::class) + every { (NavHostFragment.findNavController(mockFragment)) } returns navController + every { (NavHostFragment.findNavController(mockFragment).getCurrentDestination()) } returns mockDestination + every { (NavHostFragment.findNavController(mockFragment).navigate(navDirections, mockOptions)) } just Runs + every { (mockDestination.getId()) } returns mockId + every { (navController.getCurrentDestination()) } returns mockDestination + every { (NavHostFragment.findNavController(mockFragment).getCurrentDestination()?.getId()) } answers { (mockDestination.getId()) } + + mockFragment.nav(mockId, navDirections, mockOptions) + verify { (NavHostFragment.findNavController(mockFragment).getCurrentDestination()) } + verify { (NavHostFragment.findNavController(mockFragment).navigate(navDirections, mockOptions)) } + confirmVerified(mockFragment) + } +} diff --git a/app/src/test/java/org/mozilla/fenix/ext/LogTest.kt b/app/src/test/java/org/mozilla/fenix/ext/LogTest.kt new file mode 100644 index 000000000..96b951a5c --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/ext/LogTest.kt @@ -0,0 +1,49 @@ +package org.mozilla.fenix.ext + +import kotlinx.coroutines.ObsoleteCoroutinesApi +import org.mozilla.fenix.TestApplication +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import io.mockk.mockk +import io.mockk.mockkStatic +import io.mockk.verify +import android.util.Log + +@ObsoleteCoroutinesApi +@RunWith(RobolectricTestRunner::class) +@Config(application = TestApplication::class) + +class LogTest { + + @Test + fun `Test log debug function`() { + mockkStatic(Log::class) + logDebug("hi", "hi") + verify { (Log.d("hi", "hi")) } + } + + @Test + fun `Test log warn function with tag and message args`() { + mockkStatic(Log::class) + logWarn("hi", "hi") + verify { (Log.w("hi", "hi")) } + } + + @Test + fun `Test log warn function with tag, message, and exception args`() { + mockkStatic(Log::class) + val mockThrowable: Throwable = mockk(relaxed = true) + logWarn("hi", "hi", mockThrowable) + verify { (Log.w("hi", "hi", mockThrowable)) } + } + + @Test + fun `Test log error function with tag, message, and exception args`() { + mockkStatic(Log::class) + val mockThrowable: Throwable = mockk(relaxed = true) + logErr("hi", "hi", mockThrowable) + verify { (Log.e("hi", "hi", mockThrowable)) } + } +} diff --git a/app/src/test/java/org/mozilla/fenix/ext/NavControllerTest.kt b/app/src/test/java/org/mozilla/fenix/ext/NavControllerTest.kt new file mode 100644 index 000000000..92cb24dbd --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/ext/NavControllerTest.kt @@ -0,0 +1,101 @@ +package org.mozilla.fenix.ext + +import kotlinx.coroutines.ObsoleteCoroutinesApi +import org.mozilla.fenix.TestApplication +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import io.mockk.mockk +import io.mockk.verify +import io.mockk.every +import io.mockk.mockkClass +import android.os.Bundle +import androidx.navigation.NavController +import androidx.navigation.NavDirections +import androidx.navigation.NavOptions +import org.mozilla.fenix.BuildConfig +import androidx.navigation.NavDestination +import androidx.navigation.Navigator.Extras + +@ObsoleteCoroutinesApi +@RunWith(RobolectricTestRunner::class) +@Config(application = TestApplication::class) + +class NavControllerTest() { + + val navController: NavController = mockk(relaxed = true) + val navDirections = mockk(relaxed = true) + val mockDestination: NavDestination = mockk(relaxed = true) + val mockExtras: Extras = mockk(relaxed = true) + val mockOptions: NavOptions = mockk(relaxed = true) + val mockBundle: Bundle = mockk(relaxed = true) + val mockBuildConfig = mockkClass(BuildConfig::class) + + @Test + fun `Nav with id and directions args`() { + every { (navController.getCurrentDestination()) } returns mockDestination + every { (mockDestination.getId()) } returns 4 + navController.nav(4, navDirections) + verify { (navController.getCurrentDestination()) } + verify { (navController.navigate(navDirections)) } + } + + @Test + fun `Nav with id, directions, and extras args`() { + every { (navController.getCurrentDestination()) } returns mockDestination + every { (mockDestination.getId()) } returns 4 + navController.nav(4, navDirections, mockExtras) + verify { (navController.getCurrentDestination()) } + verify { (navController.navigate(navDirections, mockExtras)) } + } + + @Test + fun `Nav with id, directions, and options args`() { + every { (navController.getCurrentDestination()) } returns mockDestination + every { (mockDestination.getId()) } returns 4 + navController.nav(4, navDirections, mockOptions) + verify { (navController.getCurrentDestination()) } + verify { (navController.navigate(navDirections, mockOptions)) } + } + + @Test + fun `Nav with id, destId, bundle, options, and extras args`() { + every { (navController.getCurrentDestination()) } returns mockDestination + every { (mockDestination.getId()) } returns 4 + navController.nav(4, 5, mockBundle, mockOptions, mockExtras) + verify { (navController.getCurrentDestination()) } + verify { (navController.navigate(5, mockBundle, mockOptions, mockExtras)) } + } + + @Test + fun `Test error response for id exception in-block`() { + every { (navController.getCurrentDestination()) } returns mockDestination + every { (mockDestination.getId()) } returns 4 + navController.nav(7, navDirections) + verify { (recordIdException(mockDestination.getId(), 7)) } + } + + // TO-DO Not Working + /* @Test + fun `Test record id exception fun`() { + mockkStatic(String::class) + val actual = 7 + var expected = 4 + + class MySentry() { + public fun capture(myString: String) { + println(myString) } + } + + class MyBuildConfig() { + public val SENTRY_TOKEN = "Mozilla" + } + + //val Sentry = MySentry() + val BuildConfig = MyBuildConfig() + every {BuildConfig.SENTRY_TOKEN.isNullOrEmpty()} returns false + recordIdException(actual, expected) + //verify { Sentry.capture("Fragment id $actual did not match expected $expected")} + }*/ +} diff --git a/app/src/test/java/org/mozilla/fenix/ext/StringTest.kt b/app/src/test/java/org/mozilla/fenix/ext/StringTest.kt new file mode 100644 index 000000000..7b5623b81 --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/ext/StringTest.kt @@ -0,0 +1,46 @@ +package org.mozilla.fenix.ext + +import kotlinx.coroutines.ObsoleteCoroutinesApi +import mozilla.components.support.test.robolectric.testContext +import org.mozilla.fenix.TestApplication +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.Assert.assertEquals +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@ObsoleteCoroutinesApi +@RunWith(RobolectricTestRunner::class) +@Config(application = TestApplication::class) + +class StringTest { + + @Test + fun `Replace`() { + val chars = mapOf("Mozilla Corporation" to "moco", "Mozilla Foundation" to "mofo") + var sentence = "Mozilla Corporation and Mozilla Foundation are committed to the future of the internet" + val new = sentence.replace(chars) + assertEquals(new, "moco and mofo are committed to the future of the internet") + } + + @Test + fun `Get Host From Url`() { + val urlTest = "http://www.example.com:1080/docs/resource1.html" + var new = urlTest.getHostFromUrl() + assertEquals(new, "www.example.com") + } + + @Test + fun `Url To Trimmed Host`() { + val urlTest = "http://www.example.com:1080/docs/resource1.html" + var new = urlTest.urlToTrimmedHost(testContext) + assertEquals(new, "example") + } + + @Test + fun `Simplified Url`() { + val urlTest = "https://www.amazon.com" + var new = urlTest.simplifiedUrl() + assertEquals(new, "amazon.com") + } +}