Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
Add tests for ContextKt (#11824)
Browse files Browse the repository at this point in the history
Migrated from an old branch by Kate

Co-authored-by: Kate Glazko <[email protected]>
  • Loading branch information
NotWoods and kglazko authored Jun 22, 2020
1 parent c8d36dd commit 18cc4c9
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 19 deletions.
4 changes: 0 additions & 4 deletions app/src/main/java/org/mozilla/fenix/ext/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import android.view.ContextThemeWrapper
import android.view.View
import android.view.ViewGroup
import androidx.annotation.StringRes
import androidx.fragment.app.FragmentActivity
import mozilla.components.browser.search.SearchEngineManager
import mozilla.components.support.locale.LocaleManager
import org.mozilla.fenix.BuildConfig
Expand Down Expand Up @@ -50,9 +49,6 @@ val Context.searchEngineManager: SearchEngineManager
fun Context.asActivity() = (this as? ContextThemeWrapper)?.baseContext as? Activity
?: this as? Activity

fun Context.asFragmentActivity() = (this as? ContextThemeWrapper)?.baseContext as? FragmentActivity
?: this as? FragmentActivity

fun Context.getPreferenceKey(@StringRes resourceId: Int): String =
resources.getString(resourceId)

Expand Down
138 changes: 123 additions & 15 deletions app/src/test/java/org/mozilla/fenix/ext/ContextTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,165 @@

package org.mozilla.fenix.ext

import android.app.Activity
import android.content.Context
import android.view.ContextThemeWrapper
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.test.core.app.ApplicationProvider
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.mockkStatic
import io.mockk.unmockkObject
import mozilla.components.support.locale.LocaleManager
import mozilla.components.support.locale.LocaleManager.getSystemDefault
import mozilla.components.support.test.robolectric.testContext
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.FenixApplication
import org.mozilla.fenix.R
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import java.lang.String.format
import java.util.Locale

@RunWith(FenixRobolectricTestRunner::class)
class ContextTest {
private lateinit var context: Context

private lateinit var mockContext: Context
private val selectedLocale = Locale("ro", "RO")
private val appName = "Firefox Preview"
private val correctlyFormattedString = "Incearca noul %1s"
private val incorrectlyFormattedString = "Incearca noul %1&amp;s"
private val englishString = "Try the new %1s"

private val mockId: Int = 11

@Before
fun setup() {
mockkStatic("org.mozilla.fenix.settings.advanced.LocaleManagerExtensionKt")
mockkObject(LocaleManager)
context = mockk(relaxed = true)
context.resources.configuration.setLocale(selectedLocale)

every { LocaleManager.getCurrentLocale(context) } returns selectedLocale
mockContext = mockk(relaxed = true)
mockContext.resources.configuration.setLocale(selectedLocale)

every { LocaleManager.getCurrentLocale(mockContext) } returns selectedLocale
}

@After
fun teardown() {
unmockkObject(LocaleManager)
}

@Test
fun `getStringWithArgSafe returns selected locale for correct formatted string`() {
every { context.getString(mockId) } returns correctlyFormattedString
val correctlyFormattedString = "Incearca noul %1s"
every { mockContext.getString(mockId) } returns correctlyFormattedString

val result = context.getStringWithArgSafe(mockId, appName)
val result = mockContext.getStringWithArgSafe(mockId, appName)

assertEquals("Incearca noul Firefox Preview", result)
}

@Test
fun `getStringWithArgSafe returns English locale for incorrect formatted string`() {

val englishString = "Try the new %1s"
val incorrectlyFormattedString = "Incearca noul %1&amp;s"
every { getSystemDefault() } returns Locale("en")
every { context.getString(mockId) } returns incorrectlyFormattedString
every { format(context.getString(mockId), appName) } returns format(englishString, appName)
every { mockContext.getString(mockId) } returns incorrectlyFormattedString
every { format(mockContext.getString(mockId), appName) } returns format(englishString, appName)

val result = context.getStringWithArgSafe(mockId, appName)
val result = mockContext.getStringWithArgSafe(mockId, appName)

assertEquals("Try the new Firefox Preview", result)
}

@Test
fun `GIVEN context WHEN seeking application of context THEN send back application context`() {
val expectedAppValue = ApplicationProvider.getApplicationContext<FenixApplication>()
assertEquals(expectedAppValue, testContext.application)
}

@Test
fun `GIVEN context WHEN requiring components THEN send back application components`() {
val expectedComponentsValue = ApplicationProvider.getApplicationContext<FenixApplication>().components
assertEquals(expectedComponentsValue, testContext.components)
}

@Test
fun `GIVEN context WHEN getting metrics controller THEN send back metrics`() {
val expectedMetricsValue = ApplicationProvider.getApplicationContext<FenixApplication>().components.analytics.metrics
assertEquals(expectedMetricsValue, testContext.metrics)
}

@Test
fun `GIVEN activity context WHEN make it an activity THEN return activity`() {
val mockActivity = mockk<Activity> {
every { baseContext } returns null
}
val mockContext: Context = mockActivity
assertEquals(mockActivity, mockContext.asActivity())
}

@Test
fun `GIVEN theme wrapper context WHEN make it an activity THEN return base`() {
val mockActivity = mockk<Activity>()
val mockThemeWrapper = mockk<ContextThemeWrapper> {
every { baseContext } returns mockActivity
}
val mockContext: Context = mockThemeWrapper
assertEquals(mockActivity, mockContext.asActivity())
}

@Test
fun `GIVEN theme wrapper context without activity base context WHEN make it an activity THEN return null`() {
val mockThemeWrapper = mockk<ContextThemeWrapper> {
every { baseContext } returns mockk<FenixApplication>()
}
val mockContext: Context = mockThemeWrapper
assertNull(mockContext.asActivity())
}

@Test
fun `GIVEN activity context WHEN get root view THEN return content view`() {
val rootView = mockk<ViewGroup>()
val mockActivity = mockk<Activity> {
every { baseContext } returns null
every { window } returns mockk {
every { decorView } returns mockk {
every { findViewById<View>(android.R.id.content) } returns rootView
}
}
}
assertEquals(rootView, mockActivity.getRootView())
}

@Test
fun `GIVEN activity context without window WHEN get root view THEN return content view`() {
val mockActivity = mockk<Activity> {
every { baseContext } returns null
every { window } returns null
}
assertNull(mockActivity.getRootView())
}

@Test
fun `GIVEN activity context without valid content view WHEN get root view THEN return content view`() {
val mockActivity = mockk<Activity> {
every { baseContext } returns null
every { window } returns mockk {
every { decorView } returns mockk {
every { findViewById<View>(android.R.id.content) } returns mockk<TextView>()
}
}
}
assertNull(mockActivity.getRootView())
}

@Test
fun `GIVEN context WHEN given a preference key THEN send back the right string`() {
val comparisonStr = testContext.getString(R.string.private_browsing_common_myths)
val actualStr = testContext.getPreferenceKey(R.string.private_browsing_common_myths)
assertEquals(comparisonStr, actualStr)
}
}

0 comments on commit 18cc4c9

Please sign in to comment.