[fenix] Add method to test fragments in roboletric (https://github.com/mozilla-mobile/fenix/pull/12261)
parent
61ae2b2fb8
commit
c3391dd74d
@ -0,0 +1,20 @@
|
||||
/* 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
|
||||
|
||||
import androidx.appcompat.app.ActionBar
|
||||
|
||||
/**
|
||||
* Interface for the main activity in a single-activity architecture.
|
||||
* All fragments will be displayed inside this activity.
|
||||
*/
|
||||
interface NavHostActivity {
|
||||
|
||||
/**
|
||||
* Returns the support action bar, inflating it if necessary.
|
||||
* Everyone should call this instead of supportActionBar.
|
||||
*/
|
||||
fun getSupportActionBarAndInflateIfNecessary(): ActionBar
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/* 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
|
||||
|
||||
import androidx.appcompat.app.ActionBar
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import io.mockk.mockk
|
||||
import org.robolectric.Robolectric
|
||||
|
||||
class MockNavHostActivity : AppCompatActivity(), NavHostActivity {
|
||||
|
||||
private val mockActionBar = mockk<ActionBar>(relaxed = true)
|
||||
|
||||
override fun getSupportActionBarAndInflateIfNecessary() = mockActionBar
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up an added [Fragment] to a [FragmentActivity] that has been initialized to a resumed state.
|
||||
*
|
||||
* Variant of [mozilla.components.support.test.robolectric.createAddedTestFragment] that uses
|
||||
* a custom Fenix activity to hold the fragment.
|
||||
*
|
||||
* @param fragmentTag the name that will be used to tag the fragment inside the [FragmentManager].
|
||||
* @param fragmentFactory a lambda function that returns a Fragment that will be added to the Activity.
|
||||
*
|
||||
* @return The same [Fragment] that was returned from [fragmentFactory] after it got added to the
|
||||
* Activity.
|
||||
*/
|
||||
inline fun <T : Fragment> createAddedTestFragmentInNavHostActivity(
|
||||
fragmentTag: String = "test",
|
||||
fragmentFactory: () -> T
|
||||
): T {
|
||||
val activity = Robolectric.buildActivity(MockNavHostActivity::class.java)
|
||||
.create()
|
||||
.start()
|
||||
.resume()
|
||||
.get()
|
||||
|
||||
return fragmentFactory().also {
|
||||
activity.supportFragmentManager.beginTransaction()
|
||||
.add(it, fragmentTag)
|
||||
.commitNow()
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
/* 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.addons
|
||||
|
||||
import android.content.Intent.ACTION_VIEW
|
||||
import androidx.core.net.toUri
|
||||
import kotlinx.android.synthetic.main.fragment_add_on_permissions.*
|
||||
import mozilla.components.feature.addons.Addon
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mozilla.fenix.createAddedTestFragmentInNavHostActivity
|
||||
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
|
||||
@RunWith(FenixRobolectricTestRunner::class)
|
||||
class AddonPermissionsDetailsFragmentTest {
|
||||
|
||||
private val addon = Addon(
|
||||
id = "",
|
||||
translatableName = mapOf(
|
||||
Addon.DEFAULT_LOCALE to "Some blank addon"
|
||||
)
|
||||
)
|
||||
|
||||
@Test
|
||||
fun `trigger view intent when learn more is clicked`() {
|
||||
val fragment = createAddedTestFragmentInNavHostActivity {
|
||||
AddonPermissionsDetailsFragment().apply {
|
||||
arguments = AddonPermissionsDetailsFragmentArgs(addon).toBundle()
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals("Some blank addon", fragment.activity?.title)
|
||||
|
||||
fragment.learn_more_label.performClick()
|
||||
|
||||
val intent = shadowOf(fragment.activity).peekNextStartedActivity()
|
||||
assertEquals(ACTION_VIEW, intent.action)
|
||||
assertEquals(
|
||||
"https://support.mozilla.org/kb/permission-request-messages-firefox-extensions".toUri(),
|
||||
intent.data
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue