Skip to content

Commit

Permalink
Fix #2059: Fix Walkthrough Tests for Robolectric (#2060)
Browse files Browse the repository at this point in the history
* fix WalkthroughFinalFragmentTest for robolectric

* remove todo

* fix WalkthroughTopicListFragmentTest for robolectric

* Fix WalkthroughWelcomeFragmentTest for robolectric

* fix ktlint issue
  • Loading branch information
anandwana001 authored Nov 4, 2020
1 parent 29966c0 commit 93ec2c9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.oppia.android.app.walkthrough

import android.app.Application
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -21,7 +22,6 @@ import dagger.Component
import org.hamcrest.CoreMatchers.containsString
import org.junit.After
import org.junit.Before
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.oppia.android.R
Expand Down Expand Up @@ -55,6 +55,7 @@ import org.oppia.android.domain.oppialogger.loguploader.WorkManagerConfiguration
import org.oppia.android.domain.question.QuestionModule
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 @@ -66,6 +67,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 [WalkthroughFinalFragment]. */
Expand All @@ -77,26 +79,33 @@ import javax.inject.Singleton
)
class WalkthroughFinalFragmentTest {

@Inject
lateinit var context: Context

@Inject
lateinit var testCoroutineDispatchers: TestCoroutineDispatchers

@Before
fun setUp() {
Intents.init()
setUpTestApplicationComponent()
testCoroutineDispatchers.registerIdlingResource()
}

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

private fun createWalkthroughActivityIntent(profileId: Int): Intent {
return WalkthroughActivity.createWalkthroughActivityIntent(
ApplicationProvider.getApplicationContext(),
context,
profileId
)
}

@Test
// TODO(#973): Fix WalkthroughFinalFragmentTest
@Ignore
fun testWalkthroughWelcomeFragment_recyclerViewIndex1_topicSelected_topicTitleIsCorrect() {
launch<WalkthroughActivity>(createWalkthroughActivityIntent(0)).use {
onView(withId(R.id.walkthrough_welcome_next_button))
Expand All @@ -110,6 +119,7 @@ class WalkthroughFinalFragmentTest {
R.id.walkthrough_topic_name_text_view
)
).perform(click())
testCoroutineDispatchers.runCurrent()
onView(withId(R.id.walkthrough_final_topic_text_view)).check(
matches(
withText(containsString("First Test Topic"))
Expand All @@ -119,8 +129,6 @@ class WalkthroughFinalFragmentTest {
}

@Test
// TODO(#973): Fix WalkthroughFinalFragmentTest
@Ignore
fun testWalkthroughWelcomeFragment_recyclerViewIndex2_topicSelected_topicTitleIsCorrect() {
launch<WalkthroughActivity>(createWalkthroughActivityIntent(0)).use {
onView(withId(R.id.walkthrough_welcome_next_button))
Expand All @@ -134,6 +142,7 @@ class WalkthroughFinalFragmentTest {
R.id.walkthrough_topic_name_text_view
)
).perform(click())
testCoroutineDispatchers.runCurrent()
onView(withId(R.id.walkthrough_final_topic_text_view)).check(
matches(
withText(containsString("Second Test Topic"))
Expand All @@ -143,8 +152,6 @@ class WalkthroughFinalFragmentTest {
}

@Test
// TODO(#973): Fix WalkthroughFinalFragmentTest
@Ignore
fun testWalkthroughWelcomeFragment_recyclerViewIndex2_topicSelected_configurationChanged_topicTitleIsCorrect() { // ktlint-disable max-line-length
launch<WalkthroughActivity>(createWalkthroughActivityIntent(0)).use {
onView(withId(R.id.walkthrough_welcome_next_button))
Expand All @@ -158,12 +165,14 @@ class WalkthroughFinalFragmentTest {
R.id.walkthrough_topic_name_text_view
)
).perform(click())
testCoroutineDispatchers.runCurrent()
onView(withId(R.id.walkthrough_final_topic_text_view)).check(
matches(
withText(containsString("Second Test Topic"))
)
)
onView(isRoot()).perform(orientationLandscape())
testCoroutineDispatchers.runCurrent()
onView(withId(R.id.walkthrough_final_topic_text_view)).check(
matches(
withText(containsString("Second Test Topic"))
Expand Down Expand Up @@ -213,6 +222,10 @@ class WalkthroughFinalFragmentTest {
}
}

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

// TODO(#59): Figure out a way to reuse modules instead of needing to re-declare them.
// TODO(#1675): Add NetworkModule once data module is migrated off of Moshi.
@Singleton
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.oppia.android.app.walkthrough

import android.app.Application
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -20,7 +21,6 @@ import dagger.Component
import org.hamcrest.CoreMatchers.containsString
import org.junit.After
import org.junit.Before
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.oppia.android.R
Expand Down Expand Up @@ -53,6 +53,7 @@ import org.oppia.android.domain.oppialogger.loguploader.WorkManagerConfiguration
import org.oppia.android.domain.question.QuestionModule
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 @@ -64,6 +65,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 [WalkthroughTopicListFragment]. */
Expand All @@ -75,19 +77,28 @@ import javax.inject.Singleton
)
class WalkthroughTopicListFragmentTest {

@Inject
lateinit var context: Context

@Inject
lateinit var testCoroutineDispatchers: TestCoroutineDispatchers

@Before
fun setUp() {
Intents.init()
setUpTestApplicationComponent()
testCoroutineDispatchers.registerIdlingResource()
}

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

private fun createWalkthroughActivityIntent(profileId: Int): Intent {
return WalkthroughActivity.createWalkthroughActivityIntent(
ApplicationProvider.getApplicationContext(),
context,
profileId
)
}
Expand All @@ -111,8 +122,6 @@ class WalkthroughTopicListFragmentTest {
}

@Test
// TODO(#973): Fix WalkthroughTopicListFragmentTest
@Ignore
fun testWalkthroughWelcomeFragment_recyclerViewIndex1_topicCard_topicNameIsCorrect() {
launch<WalkthroughActivity>(createWalkthroughActivityIntent(0)).use {
onView(withId(R.id.walkthrough_welcome_next_button)).perform(scrollTo(), click())
Expand All @@ -121,6 +130,7 @@ class WalkthroughTopicListFragmentTest {
1
)
)
testCoroutineDispatchers.runCurrent()
onView(
atPositionOnView(
R.id.walkthrough_topic_recycler_view,
Expand All @@ -129,15 +139,13 @@ class WalkthroughTopicListFragmentTest {
)
).check(
matches(
withText(containsString("First Topic"))
withText(containsString("First Test Topic"))
)
)
}
}

@Test
// TODO(#973): Fix WalkthroughTopicListFragmentTest
@Ignore
fun testWalkthroughWelcomeFragment_recyclerViewIndex1_topicCard_configurationChanged_topicNameIsCorrect() { // ktlint-disable max-line-length
launch<WalkthroughActivity>(createWalkthroughActivityIntent(0)).use {
onView(withId(R.id.walkthrough_welcome_next_button)).perform(scrollTo(), click())
Expand All @@ -147,6 +155,7 @@ class WalkthroughTopicListFragmentTest {
)
)
onView(isRoot()).perform(orientationLandscape())
testCoroutineDispatchers.runCurrent()
onView(
atPositionOnView(
R.id.walkthrough_topic_recycler_view,
Expand All @@ -155,12 +164,16 @@ class WalkthroughTopicListFragmentTest {
)
).check(
matches(
withText(containsString("First Topic"))
withText(containsString("First Test Topic"))
)
)
}
}

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

// TODO(#59): Figure out a way to reuse modules instead of needing to re-declare them.
// TODO(#1675): Add NetworkModule once data module is migrated off of Moshi.
@Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import dagger.Component
import org.hamcrest.CoreMatchers.allOf
import org.junit.After
import org.junit.Before
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.oppia.android.R
Expand Down Expand Up @@ -53,6 +52,7 @@ import org.oppia.android.domain.oppialogger.loguploader.WorkManagerConfiguration
import org.oppia.android.domain.question.QuestionModule
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.testing.profile.ProfileTestHelper
Expand Down Expand Up @@ -80,6 +80,9 @@ class WalkthroughWelcomeFragmentTest {
@Inject
lateinit var profileTestHelper: ProfileTestHelper

@Inject
lateinit var testCoroutineDispatchers: TestCoroutineDispatchers

@Inject
lateinit var context: Context
private val internalProfileId = 0
Expand All @@ -89,13 +92,15 @@ class WalkthroughWelcomeFragmentTest {
fun setUp() {
Intents.init()
setUpTestApplicationComponent()
testCoroutineDispatchers.registerIdlingResource()
profileTestHelper.initializeProfiles()
profileId = ProfileId.newBuilder().setInternalId(internalProfileId).build()
FirebaseApp.initializeApp(context)
}

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

Expand All @@ -105,7 +110,7 @@ class WalkthroughWelcomeFragmentTest {

private fun createWalkthroughActivityIntent(profileId: Int): Intent {
return WalkthroughActivity.createWalkthroughActivityIntent(
ApplicationProvider.getApplicationContext(),
context,
profileId
)
}
Expand All @@ -123,10 +128,9 @@ class WalkthroughWelcomeFragmentTest {
}

@Test
// TODO(#973): Fix WalkthroughWelcomeFragmentTest
@Ignore
fun testWalkthroughWelcomeFragment_checkProfileName_isCorrect() {
launch<OnboardingActivity>(createWalkthroughActivityIntent(0)).use {
testCoroutineDispatchers.runCurrent()
onView(
allOf(
withId(R.id.walkthrough_welcome_title_text_view),
Expand All @@ -137,17 +141,17 @@ class WalkthroughWelcomeFragmentTest {
}

@Test
// TODO(#973): Fix WalkthroughWelcomeFragmentTest
@Ignore
fun testWalkthroughWelcomeFragment_checkProfileName_configurationChanged_isCorrect() {
launch<OnboardingActivity>(createWalkthroughActivityIntent(0)).use {
testCoroutineDispatchers.runCurrent()
onView(
allOf(
withId(R.id.walkthrough_welcome_title_text_view),
isCompletelyDisplayed()
)
).check(matches(withText("Welcome Admin!")))
onView(isRoot()).perform(orientationLandscape())
testCoroutineDispatchers.runCurrent()
onView(
allOf(
withId(R.id.walkthrough_welcome_title_text_view),
Expand Down

0 comments on commit 93ec2c9

Please sign in to comment.