ContextTest.kt 6.11 KB
Newer Older
1 2 3 4 5 6
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.fenix.ext

Tiger Oakes's avatar
Tiger Oakes committed
7
import android.app.Activity
8
import android.content.Context
Tiger Oakes's avatar
Tiger Oakes committed
9 10 11 12 13
import android.view.ContextThemeWrapper
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.test.core.app.ApplicationProvider
14 15 16
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkObject
Tiger Oakes's avatar
Tiger Oakes committed
17
import io.mockk.unmockkObject
18 19
import mozilla.components.support.locale.LocaleManager
import mozilla.components.support.locale.LocaleManager.getSystemDefault
Tiger Oakes's avatar
Tiger Oakes committed
20 21
import mozilla.components.support.test.robolectric.testContext
import org.junit.After
22
import org.junit.Assert.assertEquals
Tiger Oakes's avatar
Tiger Oakes committed
23
import org.junit.Assert.assertNull
24 25
import org.junit.Before
import org.junit.Test
Tiger Oakes's avatar
Tiger Oakes committed
26 27 28 29
import org.junit.runner.RunWith
import org.mozilla.fenix.FenixApplication
import org.mozilla.fenix.R
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
30 31 32
import java.lang.String.format
import java.util.Locale

Tiger Oakes's avatar
Tiger Oakes committed
33
@RunWith(FenixRobolectricTestRunner::class)
34
class ContextTest {
Tiger Oakes's avatar
Tiger Oakes committed
35 36

    private lateinit var mockContext: Context
37
    private val selectedLocale = Locale("ro", "RO")
Matthew Finkel's avatar
Matthew Finkel committed
38
    private val appName = "Tor Browser Dev"
39 40 41 42 43 44 45

    private val mockId: Int = 11

    @Before
    fun setup() {
        mockkObject(LocaleManager)

Tiger Oakes's avatar
Tiger Oakes committed
46 47 48 49 50 51 52 53 54
        mockContext = mockk(relaxed = true)
        mockContext.resources.configuration.setLocale(selectedLocale)

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

    @After
    fun teardown() {
        unmockkObject(LocaleManager)
55 56 57 58
    }

    @Test
    fun `getStringWithArgSafe returns selected locale for correct formatted string`() {
Tiger Oakes's avatar
Tiger Oakes committed
59 60
        val correctlyFormattedString = "Incearca noul %1s"
        every { mockContext.getString(mockId) } returns correctlyFormattedString
61

Tiger Oakes's avatar
Tiger Oakes committed
62
        val result = mockContext.getStringWithArgSafe(mockId, appName)
63

Matthew Finkel's avatar
Matthew Finkel committed
64
        assertEquals("Incearca noul Tor Browser Dev", result)
65 66 67 68
    }

    @Test
    fun `getStringWithArgSafe returns English locale for incorrect formatted string`() {
Tiger Oakes's avatar
Tiger Oakes committed
69 70
        val englishString = "Try the new %1s"
        val incorrectlyFormattedString = "Incearca noul %1&s"
71
        every { getSystemDefault() } returns Locale("en")
Tiger Oakes's avatar
Tiger Oakes committed
72 73
        every { mockContext.getString(mockId) } returns incorrectlyFormattedString
        every { format(mockContext.getString(mockId), appName) } returns format(englishString, appName)
74

Tiger Oakes's avatar
Tiger Oakes committed
75
        val result = mockContext.getStringWithArgSafe(mockId, appName)
76

Matthew Finkel's avatar
Matthew Finkel committed
77
        assertEquals("Try the new Tor Browser Dev", result)
78
    }
Tiger Oakes's avatar
Tiger Oakes committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

    @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)
    }
168
}