Skip to content

Instantly share code, notes, and snippets.

@BenHenning
Last active April 2, 2020 07:29
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 BenHenning/02983a9bd8bcf4dd47c83fca9ec19a28 to your computer and use it in GitHub Desktop.
Save BenHenning/02983a9bd8bcf4dd47c83fca9ec19a28 to your computer and use it in GitHub Desktop.
package org.oppia.app.testing
import android.content.Context
import android.content.res.Configuration
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.test.core.app.ActivityScenario.launch
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.intent.Intents
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.oppia.app.profile.ProfileChooserFragment
import org.robolectric.annotation.Config
private const val TAG_PROFILE_CHOOSER_FRAGMENT_RECYCLER_VIEW = "profile_recycler_view"
/**
* Tests for ensuring [ProfileChooserFragment] uses the correct column count for profiles based on display density.
* Reference document :https://developer.android.com/reference/androidx/test/core/app/ActivityScenario
* */
@RunWith(AndroidJUnit4::class)
@Config(manifest = Config.NONE)
class ProfileChooserSpanTest {
@Before
@ExperimentalCoroutinesApi
fun setUp() {
Intents.init()
ApplicationProvider.getApplicationContext<Context>().resources.configuration.orientation =
Configuration.ORIENTATION_LANDSCAPE
}
@get:Rule
var activityRule = ActivityScenarioRule(ProfileChooserFragmentTestActivity::class.java)
@After
fun tearDown() {
Intents.release()
}
private fun getProfileRecyclerViewGridLayoutManager(activity: ProfileChooserFragmentTestActivity): GridLayoutManager {
return getProfileRecyclerView(activity).layoutManager as GridLayoutManager
}
private fun getProfileRecyclerView(activity: ProfileChooserFragmentTestActivity): RecyclerView {
return getProfileChooserFragment(activity).view?.findViewWithTag<View>(TAG_PROFILE_CHOOSER_FRAGMENT_RECYCLER_VIEW)!! as RecyclerView
}
@Test
@Config(qualifiers = "land-ldpi")
fun testProfileChooserFragmentRecyclerView_landscape_ldpi_hasCorrectSpanCount() {
launch(ProfileChooserFragmentTestActivity::class.java).use { scenario ->
scenario.onActivity { activity ->
assertThat(getProfileRecyclerViewGridLayoutManager(activity).spanCount).isEqualTo(3)
}
}
}
// Other tests need to be updated to match the above.
@Test
@Config(qualifiers = "land-mdpi")
fun testProfileChooserFragmentRecyclerView_landscape_mdpi_hasCorrectSpanCount() {
launch(ProfileChooserFragmentTestActivity::class.java).use {
assertThat(getProfileRecyclerViewGridLayoutManager(activity!!).spanCount).isEqualTo(3)
}
}
@Test
@Config(qualifiers = "land-hdpi")
fun testProfileChooserFragmentRecyclerView_landscape_hdpi_hasCorrectSpanCount() {
launch(ProfileChooserFragmentTestActivity::class.java).use {
assertThat(getProfileRecyclerViewGridLayoutManager(activity!!).spanCount).isEqualTo(4)
}
}
@Test
@Config(qualifiers = "land-xhdpi")
fun testProfileChooserFragmentRecyclerView_landscape_xhdpi_hasCorrectSpanCount() {
launch(ProfileChooserFragmentTestActivity::class.java).use {
assertThat(getProfileRecyclerViewGridLayoutManager(activity!!).spanCount).isEqualTo(5)
}
}
@Test
@Config(qualifiers = "land-xxhdpi")
fun testProfileChooserFragmentRecyclerView_landscape_xxhdpi_hasCorrectSpanCount() {
launch(ProfileChooserFragmentTestActivity::class.java).use {
assertThat(getProfileRecyclerViewGridLayoutManager(activity!!).spanCount).isEqualTo(5)
}
}
@Test
@Config(qualifiers = "land-xxxhdpi")
fun testProfileChooserFragmentRecyclerView_landscape_xxxhdpi_hasCorrectSpanCount() {
launch(ProfileChooserFragmentTestActivity::class.java).use {
assertThat(getProfileRecyclerViewGridLayoutManager(activity!!).spanCount).isEqualTo(5)
}
}
fun getProfileChooserFragment(activity: ProfileChooserFragmentTestActivity): ProfileChooserFragment {
return activity!!.supportFragmentManager.findFragmentByTag(ProfileChooserFragmentTestActivity.TAG_PROFILE_CHOOSER_FRAGMENT) as ProfileChooserFragment
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment