Skip to content

Instantly share code, notes, and snippets.

View ashley-figueira's full-sized avatar
🇵🇹

Ashley Figueira ashley-figueira

🇵🇹
View GitHub Profile
@ashley-figueira
ashley-figueira / screenshotrule.kt
Last active March 9, 2024 16:21
Screenshot Rule
class ScreenshotRule(
private val screenshoter: Paparazzi,
) : TestRule {
val layoutInflater: LayoutInflater
get() = paparazzi.layoutInflater
val context: Context
get() = paparazzi.context
@ExperimentalCoroutinesApi
@UninstallModules(
UrlProviderModule::class,
CacheControlModule::class
)
@HiltAndroidTest
@RunWith(AndroidJUnit4::class)
class BaseFragmentTest {
@get:Rule val hiltRule = HiltAndroidRule(this)
IdlingRegistry.getInstance().register(OkHttp3IdlingResource.create("okhttp", okHttp))
private fun launchNewsListFragment() {
launchFragmentInHiltContainer<NewsListFragment> {
navController.setGraph(R.navigation.main_nav_graph)
navController.setCurrentDestination(R.id.newsListFragment)
this.viewLifecycleOwnerLiveData.observeForever { viewLifecycleOwner ->
if (viewLifecycleOwner != null) {
// The fragment’s view has just been created
Navigation.setViewNavController(this.requireView(), navController)
}
}
launchFragmentInHiltContainer<FixturesFragment>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company.package">
<application>
<activity
android:name=".HiltTestActivity"
android:exported="false" />
</application>
@AndroidEntryPoint
class HiltTestActivity : AppCompatActivity()
inline fun <reified T : Fragment> launchFragmentInHiltContainer(
fragmentArgs: Bundle? = null,
@StyleRes themeResId: Int = R.style.SomeTheme,
fragmentFactory: FragmentFactory? = null,
crossinline action: Fragment.() -> Unit = {}
) {
val startActivityIntent = Intent.makeMainActivity(
ComponentName(
ApplicationProvider.getApplicationContext(),
HiltTestActivity::class.java
class MockServerDispatcher {
/**
* Return ok response from mock server
*/
internal inner class RequestDispatcher : Dispatcher() {
override fun dispatch(request: RecordedRequest): MockResponse {
return when (request.path) {
"/fixtures" -> MockResponse().setResponseCode(200).setBody(getJsonContent("fixtures_feed.json"))
"/news" -> MockResponse().setResponseCode(200).setBody(getJsonContent("news_response.json"))
else -> MockResponse().setResponseCode(400)
private fun getJsonContent(fileName: String): String {
return InputStreamReader(this.javaClass.classLoader!!.getResourceAsStream(fileName)).use { it.readText() }
}