Skip to content

Instantly share code, notes, and snippets.

@ashley-figueira
Created December 2, 2020 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashley-figueira/952f8d173fe8573b8f09e67b25cb4a2b to your computer and use it in GitHub Desktop.
Save ashley-figueira/952f8d173fe8573b8f09e67b25cb4a2b to your computer and use it in GitHub Desktop.
@ExperimentalCoroutinesApi
@UninstallModules(
UrlProviderModule::class,
CacheControlModule::class
)
@HiltAndroidTest
@RunWith(AndroidJUnit4::class)
class BaseFragmentTest {
@get:Rule val hiltRule = HiltAndroidRule(this)
val navController = TestNavHostController(ApplicationProvider.getApplicationContext())
protected lateinit var mockWebServer: MockWebServer
@Inject lateinit var okHttp: OkHttpClient
@Before
open fun setUp() {
hiltRule.inject()
mockWebServer = MockWebServer()
mockWebServer.dispatcher = MockServerDispatcher().RequestDispatcher()
mockWebServer.start(8080)
IdlingRegistry.getInstance().register(OkHttp3IdlingResource.create("okhttp", okHttp))
}
@After
open fun tearDown() {
mockWebServer.shutdown()
}
@Test
fun givenArticleIsClickedThenNavigateToDetailsScreen() {
//Launch fragment
launchNewsListFragment()
//Click on first article
onView(withId(R.id.recyclerView)).perform(actionOnItemAtPosition<RecyclerView.ViewHolder>(1, click()))
//Check that it navigates to Detail screen
assertThat(navController.currentDestination?.id).isEqualTo(R.id.newsDetailFragment)
}
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)
}
}
}
}
@Module
@InstallIn(ApplicationComponent::class)
class FakeBaseUrlModule {
@Provides
@Singleton
fun provideUrl(): String = "http://localhost:8080/"
}
}
inline fun <reified T : Fragment> launchFragmentInHiltContainer(
fragmentArgs: Bundle? = null,
@StyleRes themeResId: Int = R.style.Theme,
fragmentFactory: FragmentFactory? = null,
crossinline action: Fragment.() -> Unit = {}
) {
val startActivityIntent = Intent.makeMainActivity(
ComponentName(
ApplicationProvider.getApplicationContext(),
HiltTestActivity::class.java
)
).putExtra(EmptyFragmentActivity.THEME_EXTRAS_BUNDLE_KEY, themeResId)
ActivityScenario.launch<HiltTestActivity>(startActivityIntent).onActivity { activity ->
fragmentFactory?.let {
activity.supportFragmentManager.fragmentFactory = it
}
val fragment: Fragment = activity.supportFragmentManager.fragmentFactory.instantiate(
Preconditions.checkNotNull(T::class.java.classLoader),
T::class.java.name
)
fragment.arguments = fragmentArgs
activity.supportFragmentManager
.beginTransaction()
.add(android.R.id.content, fragment, "")
.commitNow()
fragment.action()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment