Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix part of #1868: Fixed few TopicRevisionFragmentTest for Robolectric #2074

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class RecyclerViewMatcher {
fun hasGridItemCount(spanCount: Int, position: Int): ViewAssertion {
return RecyclerViewGridItemCountAssertion(spanCount, position)
}

fun hasGridColumnCount(expectedColumnCount: Int): ViewAssertion {
return GridLayoutManagerColumnCountAssertion(expectedColumnCount)
}
}

private class RecyclerViewItemCountAssertion(private val count: Int) : ViewAssertion {
Expand Down Expand Up @@ -102,4 +106,30 @@ class RecyclerViewMatcher {
assertThat("RecyclerViewGrid span count", spanCount, equalTo(count))
}
}

private class GridLayoutManagerColumnCountAssertion(expectedColumnCount: Int) : ViewAssertion {
private var expectedColumnCount: Int = 0

init {
this.expectedColumnCount = expectedColumnCount
}

override fun check(view: View, noViewFoundException: NoMatchingViewException?) {
if (noViewFoundException != null) {
throw noViewFoundException
}
val recyclerView = view as RecyclerView
if (recyclerView.layoutManager is GridLayoutManager) {
val gridLayoutManager = recyclerView.layoutManager as GridLayoutManager
val spanCount = gridLayoutManager.spanCount
if (spanCount != expectedColumnCount) {
val errorMessage =
("expected column count " + expectedColumnCount + " but was " + spanCount)
anandwana001 marked this conversation as resolved.
Show resolved Hide resolved
throw AssertionError(errorMessage)
}
} else {
throw IllegalStateException("no grid layout manager")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
package org.oppia.android.app.topic.revision

import android.app.Application
import android.view.View
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ActivityScenario.launch
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.NoMatchingViewException
import androidx.test.espresso.ViewAssertion
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.RecyclerViewActions
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.Intents.intended
import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent
import androidx.test.espresso.matcher.ViewMatchers.hasDescendant
import androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA
import androidx.test.espresso.matcher.ViewMatchers.isRoot
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.rule.ActivityTestRule
import com.google.firebase.FirebaseApp
import dagger.Component
import org.hamcrest.Matchers.allOf
import org.junit.After
import org.junit.Before
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.oppia.android.R
Expand All @@ -37,9 +39,12 @@ import org.oppia.android.app.application.ApplicationModule
import org.oppia.android.app.application.ApplicationStartupListenerModule
import org.oppia.android.app.player.state.hintsandsolution.HintsAndSolutionConfigModule
import org.oppia.android.app.recyclerview.RecyclerViewMatcher.Companion.atPosition
import org.oppia.android.app.recyclerview.RecyclerViewMatcher.Companion.atPositionOnView
import org.oppia.android.app.recyclerview.RecyclerViewMatcher.Companion.hasGridColumnCount
import org.oppia.android.app.shim.ViewBindingShimModule
import org.oppia.android.app.topic.TopicActivity
import org.oppia.android.app.topic.TopicTab
import org.oppia.android.app.topic.revisioncard.RevisionCardActivity
import org.oppia.android.app.utility.EspressoTestsMatchers.withDrawable
import org.oppia.android.app.utility.OrientationChangeAction.Companion.orientationLandscape
import org.oppia.android.domain.classify.InteractionsModule
Expand All @@ -61,6 +66,7 @@ import org.oppia.android.domain.question.QuestionModule
import org.oppia.android.domain.topic.FRACTIONS_TOPIC_ID
import org.oppia.android.domain.topic.PrimeTopicAssetsControllerModule
import org.oppia.android.testing.TestAccessibilityModule
import org.oppia.android.testing.TestCoroutineDispatchers
import org.oppia.android.testing.TestDispatcherModule
import org.oppia.android.testing.TestLogReportingModule
import org.oppia.android.util.caching.testing.CachingTestModule
Expand All @@ -72,6 +78,7 @@ import org.oppia.android.util.parser.HtmlParserEntityTypeModule
import org.oppia.android.util.parser.ImageParsingModule
import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode
import javax.inject.Inject
import javax.inject.Singleton

/** Tests for [TopicRevisionFragment]. */
Expand All @@ -82,33 +89,45 @@ import javax.inject.Singleton
qualifiers = "port-xxhdpi"
)
class TopicRevisionFragmentTest {

@Inject
lateinit var context: Context

@Inject
lateinit var testCoroutineDispatchers: TestCoroutineDispatchers

private val subtopicThumbnail = R.drawable.topic_fractions_01
private val internalProfileId = 0

@get:Rule
var topicActivityTestRule: ActivityTestRule<TopicActivity> = ActivityTestRule(
TopicActivity::class.java,
/* initialTouchMode= */ true,
/* launchActivity= */ false
)

@Before
fun setUp() {
FirebaseApp.initializeApp(ApplicationProvider.getApplicationContext())
Intents.init()
setUpTestApplicationComponent()
FirebaseApp.initializeApp(context)
testCoroutineDispatchers.registerIdlingResource()
}

@After
fun tearDown() {
testCoroutineDispatchers.unregisterIdlingResource()
Intents.release()
}

private fun setUpTestApplicationComponent() {
ApplicationProvider.getApplicationContext<TestApplication>().inject(this)
}

@Test
// TODO(#973): Fix TopicRevisionFragmentTest
@Ignore
fun testTopicRevisionFragment_loadFragment_displayReviewTopics_isSuccessful() {
fun testTopicRevisionFragment_loadFragment_displayRevisionTopics_isSuccessful() {
launchTopicActivityIntent(internalProfileId, FRACTIONS_TOPIC_ID).use {
testCoroutineDispatchers.runCurrent()
onView(
allOf(
withText(TopicTab.getTabForPosition(3).name),
isDescendantOfA(withId(R.id.topic_tabs_container))
)
).perform(click())
testCoroutineDispatchers.runCurrent()
onView(atPosition(R.id.revision_recycler_view, 0))
.check(matches(hasDescendant(withId(R.id.subtopic_title))))
}
Expand All @@ -117,63 +136,41 @@ class TopicRevisionFragmentTest {
@Test
// TODO(#973): Fix TopicRevisionFragmentTest
@Ignore
fun testTopicRevisionFragment_loadFragment_selectReviewTopics_opensReviewActivity() {
topicActivityTestRule.launchActivity(
TopicActivity.createTopicActivityIntent(
ApplicationProvider.getApplicationContext(),
internalProfileId,
FRACTIONS_TOPIC_ID
)
)
onView(
allOf(
withText(TopicTab.getTabForPosition(3).name),
isDescendantOfA(withId(R.id.topic_tabs_container))
)
).perform(click())
onView(atPosition(R.id.revision_recycler_view, 0)).perform(click())
}

@Test
// TODO(#973): Fix TopicRevisionFragmentTest
@Ignore
fun testTopicRevisionFragment_loadFragment_selectReviewTopics_reviewCardDisplaysCorrectExplanation() { // ktlint-disable max-line-length
fun testTopicRevisionFragment_loadFragment_selectRevisionTopics_opensRevisionCardActivity() {
launchTopicActivityIntent(internalProfileId, FRACTIONS_TOPIC_ID).use {
testCoroutineDispatchers.runCurrent()
onView(
allOf(
withText(TopicTab.getTabForPosition(3).name),
isDescendantOfA(withId(R.id.topic_tabs_container))
)
).perform(click())
onView(atPosition(R.id.revision_recycler_view, 1)).perform(click())
onView(withId(R.id.revision_card_explanation_text))
.check(
matches(
withText(
"Description of subtopic is here."
)
)
testCoroutineDispatchers.runCurrent()
onView(withId(R.id.revision_recycler_view)).perform(
RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(
0
)
)
onView(atPosition(R.id.revision_recycler_view, 0)).perform(click())
intended(hasComponent(RevisionCardActivity::class.java.name))
}
}

@Test
// TODO(#973): Fix TopicRevisionFragmentTest
@Ignore
fun testTopicRevisionFragment_loadFragment_checkTopicThumbnail_isCorrect() {
launchTopicActivityIntent(internalProfileId, FRACTIONS_TOPIC_ID).use {
testCoroutineDispatchers.runCurrent()
onView(
allOf(
withText(TopicTab.getTabForPosition(3).name),
isDescendantOfA(withId(R.id.topic_tabs_container))
)
).perform(click())
onView(withId(R.id.revision_recycler_view)).check(
testCoroutineDispatchers.runCurrent()
onView(atPositionOnView(R.id.revision_recycler_view, 0, R.id.subtopic_image_view)).check(
matches(
hasDescendant(
withDrawable(
subtopicThumbnail
)
withDrawable(
subtopicThumbnail
)
)
)
Expand All @@ -191,48 +188,46 @@ class TopicRevisionFragmentTest {
).perform(click())
onView(withId(R.id.revision_recycler_view))
.check(
GridLayoutManagerColumnCountAssertion(
2
hasGridColumnCount(
expectedColumnCount = 2
)
)
}
}

@Test
// TODO(#973): Fix TopicRevisionFragmentTest
@Ignore
fun testTopicPracticeFragment_loadFragment_configurationChange_reviewSubtopicsAreDisplayed() {
fun testTopicPracticeFragment_loadFragment_configurationChange_revisionSubtopicsAreDisplayed() {
launchTopicActivityIntent(internalProfileId, FRACTIONS_TOPIC_ID).use {
testCoroutineDispatchers.runCurrent()
onView(isRoot()).perform(orientationLandscape())
onView(
allOf(
withText(TopicTab.getTabForPosition(3).name),
isDescendantOfA(withId(R.id.topic_tabs_container))
)
).perform(click())
testCoroutineDispatchers.runCurrent()
onView(atPosition(R.id.revision_recycler_view, 0))
.check(matches(hasDescendant(withId(R.id.subtopic_title))))
}
}

@Test
// TODO(#973): Fix TopicRevisionFragmentTest
@Ignore
fun testTopicRevisionFragment_loadFragment_configurationChange_checkTopicThumbnail_isCorrect() {
launchTopicActivityIntent(internalProfileId, FRACTIONS_TOPIC_ID).use {
testCoroutineDispatchers.runCurrent()
onView(isRoot()).perform(orientationLandscape())
onView(
allOf(
withText(TopicTab.getTabForPosition(3).name),
isDescendantOfA(withId(R.id.topic_tabs_container))
)
).perform(click())
onView(withId(R.id.revision_recycler_view)).check(
testCoroutineDispatchers.runCurrent()
onView(atPositionOnView(R.id.revision_recycler_view, 0, R.id.subtopic_image_view)).check(
matches(
hasDescendant(
withDrawable(
subtopicThumbnail
)
withDrawable(
subtopicThumbnail
)
)
)
Expand All @@ -244,6 +239,7 @@ class TopicRevisionFragmentTest {
@Ignore
fun testTopicRevisionFragment_loadFragment_configurationChange_checkSpanCount_isThree() {
launchTopicActivityIntent(internalProfileId, FRACTIONS_TOPIC_ID).use {
testCoroutineDispatchers.runCurrent()
onView(isRoot()).perform(orientationLandscape())
onView(
allOf(
Expand All @@ -253,50 +249,24 @@ class TopicRevisionFragmentTest {
).perform(click())
onView(withId(R.id.revision_recycler_view))
.check(
GridLayoutManagerColumnCountAssertion(
3
hasGridColumnCount(
expectedColumnCount = 3
)
)
}
}

private fun createTopicActivityIntent(internalProfileId: Int, topicId: String): Intent {
return TopicActivity.createTopicActivityIntent(
ApplicationProvider.getApplicationContext(), internalProfileId, topicId
)
}

private fun launchTopicActivityIntent(
internalProfileId: Int,
topicId: String
): ActivityScenario<TopicActivity> {
val intent =
TopicActivity.createTopicActivityIntent(
ApplicationProvider.getApplicationContext(),
internalProfileId,
topicId
)
return ActivityScenario.launch(intent)
}

class GridLayoutManagerColumnCountAssertion(expectedColumnCount: Int) : ViewAssertion {
private var expectedColumnCount: Int = 0

init {
this.expectedColumnCount = expectedColumnCount
}

override fun check(view: View, noViewFoundException: NoMatchingViewException?) {
if (noViewFoundException != null) {
throw noViewFoundException
}
val recyclerView = view as RecyclerView
if (recyclerView.layoutManager is GridLayoutManager) {
val gridLayoutManager = recyclerView.layoutManager as GridLayoutManager
val spanCount = gridLayoutManager.spanCount
if (spanCount != expectedColumnCount) {
val errorMessage =
("expected column count " + expectedColumnCount + " but was " + spanCount)
throw AssertionError(errorMessage)
}
} else {
throw IllegalStateException("no grid layout manager")
}
}
return launch(createTopicActivityIntent(internalProfileId, topicId))
}

// TODO(#59): Figure out a way to reuse modules instead of needing to re-declare them.
Expand Down