Skip to content

Commit

Permalink
Fix #5485 Create means for verifying Fragment Arguments (#5522)
Browse files Browse the repository at this point in the history
<!-- READ ME FIRST: Please fill in the explanation section below and
check off every point from the Essential Checklist! -->
## Explanation

   Feature Request part1 #5485
   Added test for 10 fragments arguments and saveInstanceState.
 

## Essential Checklist
<!-- Please tick the relevant boxes by putting an "x" in them. -->
- [x] The PR title and explanation each start with "Fix #bugnum: " (If
this PR fixes part of an issue, prefix the title with "Fix part of
#bugnum: ...".)
- [x] Any changes to
[scripts/assets](https://github.com/oppia/oppia-android/tree/develop/scripts/assets)
files have their rationale included in the PR explanation.
- [x] The PR follows the [style
guide](https://github.com/oppia/oppia-android/wiki/Coding-style-guide).
- [x] The PR does not contain any unnecessary code changes from Android
Studio
([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#undo-unnecessary-changes)).
- [x] The PR is made from a branch that's **not** called "develop" and
is up-to-date with "develop".
- [x] The PR is **assigned** to the appropriate reviewers
([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#clarification-regarding-assignees-and-reviewers-section)).

## For UI-specific PRs only
<!-- Delete these section if this PR does not include UI-related
changes. -->
If your PR includes UI-related changes, then:
- Add screenshots for portrait/landscape for both a tablet & phone of
the before & after UI changes
- For the screenshots above, include both English and pseudo-localized
(RTL) screenshots (see [RTL
guide](https://github.com/oppia/oppia-android/wiki/RTL-Guidelines))
- Add a video showing the full UX flow with a screen reader enabled (see
[accessibility
guide](https://github.com/oppia/oppia-android/wiki/Accessibility-A11y-Guide))
- For PRs introducing new UI elements or color changes, both light and
dark mode screenshots must be included
- Add a screenshot demonstrating that you ran affected Espresso tests
locally & that they're passing
  • Loading branch information
subhajitxyz authored Sep 5, 2024
1 parent a1fbe0c commit 16082aa
Show file tree
Hide file tree
Showing 12 changed files with 431 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class AppLanguageFragment : InjectableFragment(), AppLanguageRadioButtonListener
}
}

private fun Bundle.retrieveLanguageFromArguments(): OppiaLanguage {
/** Returns the [OppiaLanguage] stored in the fragment's arguments. */
fun Bundle.retrieveLanguageFromArguments(): OppiaLanguage {
return getProto(
FRAGMENT_ARGUMENTS_KEY, AppLanguageFragmentArguments.getDefaultInstance()
).oppiaLanguage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ class AudioLanguageFragment : InjectableFragment(), AudioLanguageRadioButtonList
}
}

private fun Bundle.retrieveLanguageFromArguments(): AudioLanguage {
/** Returns the [AudioLanguage] stored in the fragment's arguments. */
fun Bundle.retrieveLanguageFromArguments(): AudioLanguage {
return getProto(
FRAGMENT_ARGUMENTS_KEY, AudioLanguageFragmentArguments.getDefaultInstance()
).audioLanguage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ import org.oppia.android.app.application.ApplicationInjectorProvider
import org.oppia.android.app.application.ApplicationModule
import org.oppia.android.app.application.ApplicationStartupListenerModule
import org.oppia.android.app.application.testing.TestingBuildFlavorModule
import org.oppia.android.app.devoptions.markchapterscompleted.MarkChaptersCompletedFragment
import org.oppia.android.app.devoptions.markchapterscompleted.testing.MarkChaptersCompletedTestActivity
import org.oppia.android.app.model.ChapterPlayState
import org.oppia.android.app.model.MarkChaptersCompletedFragmentArguments
import org.oppia.android.app.model.ProfileId
import org.oppia.android.app.player.state.itemviewmodel.SplitScreenInteractionModule
import org.oppia.android.app.recyclerview.RecyclerViewMatcher.Companion.atPositionOnView
Expand Down Expand Up @@ -94,6 +96,7 @@ import org.oppia.android.testing.time.FakeOppiaClockModule
import org.oppia.android.util.accessibility.AccessibilityTestModule
import org.oppia.android.util.caching.AssetModule
import org.oppia.android.util.caching.testing.CachingTestModule
import org.oppia.android.util.extensions.getProto
import org.oppia.android.util.gcsresource.GcsResourceModule
import org.oppia.android.util.locale.LocaleProdModule
import org.oppia.android.util.logging.EventLoggingConfigurationModule
Expand Down Expand Up @@ -901,6 +904,70 @@ class MarkChaptersCompletedFragmentTest {
}
}

@Test
fun testFragment_fragmentLoaded_verifyCorrectArgumentsPassed() {
launchMarkChaptersCompletedFragmentTestActivity(
internalProfileId, showConfirmationNotice = true
).use { scenario ->
testCoroutineDispatchers.runCurrent()
scenario.onActivity { activity ->

val fragment = activity.supportFragmentManager
.findFragmentById(R.id.mark_chapters_completed_container) as MarkChaptersCompletedFragment
val arguments =
checkNotNull(fragment.arguments) {
"Expected arguments to be passed to MarkChaptersCompletedFragment"
}
val args = arguments.getProto(
"MarkChaptersCompletedFragment.arguments",
MarkChaptersCompletedFragmentArguments.getDefaultInstance()
)
val receivedInternalProfileId = args?.internalProfileId
val receivedShowConfirmationNotice = args?.showConfirmationNotice

assertThat(receivedInternalProfileId).isEqualTo(internalProfileId)
assertThat(receivedShowConfirmationNotice).isEqualTo(true)
}
}
}

@Test
fun testFragment_saveInstanceState_verifyCorrectStateRestored() {
launchMarkChaptersCompletedFragmentTestActivity(
internalProfileId, showConfirmationNotice = true
).use { scenario ->
testCoroutineDispatchers.runCurrent()
onView(withId(R.id.mark_chapters_completed_all_check_box_container)).perform(click())
var actualSelectedExplorationIds = ArrayList<String>()
var actualSelectedExplorationTitles = ArrayList<String>()

scenario.onActivity { activity ->
var fragment = activity.supportFragmentManager
.findFragmentById(R.id.mark_chapters_completed_container) as MarkChaptersCompletedFragment

actualSelectedExplorationIds =
fragment.markChaptersCompletedFragmentPresenter.serializableSelectedExplorationIds
actualSelectedExplorationTitles =
fragment.markChaptersCompletedFragmentPresenter.serializableSelectedExplorationTitles
}

scenario.recreate()

scenario.onActivity { activity ->
val newFragment = activity.supportFragmentManager
.findFragmentById(R.id.mark_chapters_completed_container) as MarkChaptersCompletedFragment

val receivedSelectedExplorationIds =
newFragment.markChaptersCompletedFragmentPresenter.serializableSelectedExplorationIds
val receivedSelectedExplorationTitles =
newFragment.markChaptersCompletedFragmentPresenter.serializableSelectedExplorationTitles

assertThat(receivedSelectedExplorationIds).isEqualTo(actualSelectedExplorationIds)
assertThat(receivedSelectedExplorationTitles).isEqualTo(actualSelectedExplorationTitles)
}
}
}

private fun launchMarkChaptersCompletedFragmentTestActivity(
internalProfileId: Int,
showConfirmationNotice: Boolean = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import org.oppia.android.app.application.ApplicationInjectorProvider
import org.oppia.android.app.application.ApplicationModule
import org.oppia.android.app.application.ApplicationStartupListenerModule
import org.oppia.android.app.application.testing.TestingBuildFlavorModule
import org.oppia.android.app.devoptions.markstoriescompleted.MarkStoriesCompletedFragment
import org.oppia.android.app.devoptions.markstoriescompleted.testing.MarkStoriesCompletedTestActivity
import org.oppia.android.app.model.ProfileId
import org.oppia.android.app.player.state.itemviewmodel.SplitScreenInteractionModule
Expand Down Expand Up @@ -99,6 +100,7 @@ import org.oppia.android.util.networking.NetworkConnectionUtilDebugModule
import org.oppia.android.util.parser.html.HtmlParserEntityTypeModule
import org.oppia.android.util.parser.image.GlideImageLoaderModule
import org.oppia.android.util.parser.image.ImageParsingModule
import org.oppia.android.util.profile.CurrentUserProfileIdIntentDecorator.extractCurrentUserProfileId
import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode
import javax.inject.Inject
Expand Down Expand Up @@ -469,6 +471,58 @@ class MarkStoriesCompletedFragmentTest {
}
}

@Test
fun testFragment_fragmentLoaded_verifyCorrectArgumentsPassed() {
launch<MarkStoriesCompletedTestActivity>(
createMarkStoriesCompletedTestActivityIntent(internalProfileId)
).use { scenario ->
testCoroutineDispatchers.runCurrent()
scenario.onActivity { activity ->

val fragment = activity.supportFragmentManager
.findFragmentById(R.id.mark_stories_completed_container) as MarkStoriesCompletedFragment

val arguments =
checkNotNull(fragment.arguments) {
"Expected arguments to be passed to MarkStoriesCompletedFragment"
}
val profileId = arguments.extractCurrentUserProfileId()
val receivedInternalProfileId = profileId.internalId

assertThat(receivedInternalProfileId).isEqualTo(internalProfileId)
}
}
}

@Test
fun testFragment_saveInstanceState_verifyCorrectStateRestored() {
launch<MarkStoriesCompletedTestActivity>(
createMarkStoriesCompletedTestActivityIntent(internalProfileId)
).use { scenario ->
testCoroutineDispatchers.runCurrent()
onView(withId(R.id.mark_stories_completed_all_check_box_container)).perform(click())
var actualSelectedStoryIdList = ArrayList<String>()

scenario.onActivity { activity ->
var fragment = activity.supportFragmentManager
.findFragmentById(R.id.mark_stories_completed_container) as MarkStoriesCompletedFragment
actualSelectedStoryIdList =
fragment.markStoriesCompletedFragmentPresenter.selectedStoryIdList
}

scenario.recreate()

scenario.onActivity { activity ->
val newFragment = activity.supportFragmentManager
.findFragmentById(R.id.mark_stories_completed_container) as MarkStoriesCompletedFragment
val receivedSelectedStoryIdList =
newFragment.markStoriesCompletedFragmentPresenter.selectedStoryIdList

assertThat(receivedSelectedStoryIdList).isEqualTo(actualSelectedStoryIdList)
}
}
}

private fun createMarkStoriesCompletedTestActivityIntent(internalProfileId: Int): Intent {
return MarkStoriesCompletedTestActivity.createMarkStoriesCompletedTestIntent(
context, internalProfileId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import org.oppia.android.app.application.ApplicationInjectorProvider
import org.oppia.android.app.application.ApplicationModule
import org.oppia.android.app.application.ApplicationStartupListenerModule
import org.oppia.android.app.application.testing.TestingBuildFlavorModule
import org.oppia.android.app.devoptions.marktopicscompleted.MarkTopicsCompletedFragment
import org.oppia.android.app.devoptions.marktopicscompleted.testing.MarkTopicsCompletedTestActivity
import org.oppia.android.app.model.ProfileId
import org.oppia.android.app.player.state.itemviewmodel.SplitScreenInteractionModule
Expand Down Expand Up @@ -99,6 +100,7 @@ import org.oppia.android.util.networking.NetworkConnectionUtilDebugModule
import org.oppia.android.util.parser.html.HtmlParserEntityTypeModule
import org.oppia.android.util.parser.image.GlideImageLoaderModule
import org.oppia.android.util.parser.image.ImageParsingModule
import org.oppia.android.util.profile.CurrentUserProfileIdIntentDecorator.extractCurrentUserProfileId
import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode
import javax.inject.Inject
Expand Down Expand Up @@ -450,6 +452,56 @@ class MarkTopicsCompletedFragmentTest {
}
}

@Test
fun testFragment_fragmentLoaded_verifyCorrectArgumentsPassed() {
launch<MarkTopicsCompletedTestActivity>(
createMarkTopicsCompletedTestActivityIntent(internalProfileId)
).use { scenario ->
testCoroutineDispatchers.runCurrent()
scenario.onActivity { activity ->

var fragment = activity.supportFragmentManager
.findFragmentById(R.id.mark_topics_completed_container) as MarkTopicsCompletedFragment
val arguments =
checkNotNull(fragment.arguments) {
"Expected arguments to be passed to MarkTopicsCompletedFragment"
}
val receivedProfileId = arguments.extractCurrentUserProfileId()

assertThat(receivedProfileId).isEqualTo(profileId)
}
}
}

@Test
fun testFragment_saveInstanceState_verifyCorrectStateRestored() {
launch<MarkTopicsCompletedTestActivity>(
createMarkTopicsCompletedTestActivityIntent(internalProfileId)
).use { scenario ->
testCoroutineDispatchers.runCurrent()
onView(withId(R.id.mark_topics_completed_all_check_box_container)).perform(click())
var actualSelectedTopicsList = ArrayList<String>()

scenario.onActivity { activity ->
var fragment = activity.supportFragmentManager
.findFragmentById(R.id.mark_topics_completed_container) as MarkTopicsCompletedFragment
actualSelectedTopicsList =
fragment.markTopicsCompletedFragmentPresenter.selectedTopicIdList
}

scenario.recreate()

scenario.onActivity { activity ->
val newFragment = activity.supportFragmentManager
.findFragmentById(R.id.mark_topics_completed_container) as MarkTopicsCompletedFragment
val restoredTopicIdList =
newFragment.markTopicsCompletedFragmentPresenter.selectedTopicIdList

assertThat(restoredTopicIdList).isEqualTo(actualSelectedTopicsList)
}
}
}

private fun createMarkTopicsCompletedTestActivityIntent(internalProfileId: Int): Intent {
return MarkTopicsCompletedTestActivity.createMarkTopicsCompletedTestIntent(
context, internalProfileId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.oppia.android.app.help

import android.app.Application
import android.content.Intent
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.drawerlayout.widget.DrawerLayout
import androidx.recyclerview.widget.RecyclerView
Expand Down Expand Up @@ -29,6 +30,7 @@ import androidx.test.espresso.matcher.ViewMatchers.withEffectiveVisibility
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import dagger.Component
import org.hamcrest.Matchers.equalTo
import org.junit.After
Expand All @@ -50,6 +52,7 @@ import org.oppia.android.app.devoptions.DeveloperOptionsModule
import org.oppia.android.app.devoptions.DeveloperOptionsStarterModule
import org.oppia.android.app.help.faq.FAQListActivity
import org.oppia.android.app.help.thirdparty.ThirdPartyDependencyListActivity
import org.oppia.android.app.model.HelpFragmentArguments
import org.oppia.android.app.model.PoliciesActivityParams
import org.oppia.android.app.model.PolicyPage
import org.oppia.android.app.model.ProfileId
Expand Down Expand Up @@ -104,6 +107,7 @@ import org.oppia.android.testing.time.FakeOppiaClockModule
import org.oppia.android.util.accessibility.AccessibilityTestModule
import org.oppia.android.util.caching.AssetModule
import org.oppia.android.util.caching.testing.CachingTestModule
import org.oppia.android.util.extensions.getProto
import org.oppia.android.util.gcsresource.GcsResourceModule
import org.oppia.android.util.locale.LocaleProdModule
import org.oppia.android.util.logging.EventLoggingConfigurationModule
Expand Down Expand Up @@ -1374,6 +1378,34 @@ class HelpFragmentTest {
}
}

@Test
fun testFragment_fragmentLoaded_verifyCorrectArgumentsPassed() {
launch<HelpActivity>(
createHelpActivityIntent(
internalProfileId = 0,
isFromNavigationDrawer = true
)
).use { scenario ->
testCoroutineDispatchers.runCurrent()
scenario.onActivity { activity ->

var fragment = activity.supportFragmentManager
.findFragmentById(R.id.help_fragment_placeholder) as HelpFragment
val isMultipane =
activity.findViewById<FrameLayout>(R.id.multipane_options_container) != null

val arguments = checkNotNull(fragment.arguments) {
"Expected arguments to be passed to HelpFragment"
}
val args =
arguments.getProto("HelpFragment.arguments", HelpFragmentArguments.getDefaultInstance())
val receivedIsMultipane = args.isMultipane

assertThat(receivedIsMultipane).isEqualTo(isMultipane)
}
}
}

private fun ActivityScenario<HelpActivity>.openNavigationDrawer() {
onView(withContentDescription(R.string.drawer_open_content_description))
.check(matches(isCompletelyDisplayed()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import org.oppia.android.app.application.testing.TestingBuildFlavorModule
import org.oppia.android.app.devoptions.DeveloperOptionsModule
import org.oppia.android.app.devoptions.DeveloperOptionsStarterModule
import org.oppia.android.app.model.OppiaLanguage
import org.oppia.android.app.options.AppLanguageFragment.Companion.retrieveLanguageFromArguments
import org.oppia.android.app.player.state.itemviewmodel.SplitScreenInteractionModule
import org.oppia.android.app.recyclerview.RecyclerViewMatcher.Companion.atPositionOnView
import org.oppia.android.app.shim.ViewBindingShimModule
Expand Down Expand Up @@ -95,6 +96,7 @@ import org.oppia.android.util.networking.NetworkConnectionUtilDebugModule
import org.oppia.android.util.parser.html.HtmlParserEntityTypeModule
import org.oppia.android.util.parser.image.GlideImageLoaderModule
import org.oppia.android.util.parser.image.ImageParsingModule
import org.oppia.android.util.profile.CurrentUserProfileIdIntentDecorator.extractCurrentUserProfileId
import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode
import javax.inject.Inject
Expand Down Expand Up @@ -215,6 +217,50 @@ class AppLanguageFragmentTest {
}
}

@Test
fun testFragment_fragmentLoaded_verifyCorrectArgumentsPassed() {
launch<AppLanguageActivity>(createAppLanguageActivityIntent(OppiaLanguage.ENGLISH))
.use { scenario ->
testCoroutineDispatchers.runCurrent()
scenario.onActivity { activity ->

val appLanguageFragment = activity.supportFragmentManager
.findFragmentById(R.id.app_language_fragment_container) as AppLanguageFragment
val recievedLanguage = appLanguageFragment.arguments?.retrieveLanguageFromArguments()
val receivedProfileId =
appLanguageFragment.arguments?.extractCurrentUserProfileId()?.internalId

assertThat(recievedLanguage).isEqualTo(OppiaLanguage.ENGLISH)
assertThat(receivedProfileId).isEqualTo(internalProfileId)
}
}
}

@Test
fun testFragment_saveInstanceState_verifyCorrectStateRestored() {
launch<AppLanguageActivity>(createAppLanguageActivityIntent(OppiaLanguage.ENGLISH))
.use { scenario ->
testCoroutineDispatchers.runCurrent()

scenario.onActivity { activity ->
var appLanguageFragment = activity.supportFragmentManager
.findFragmentById(R.id.app_language_fragment_container) as AppLanguageFragment
appLanguageFragment.appLanguageFragmentPresenter.onLanguageSelected(OppiaLanguage.ARABIC)
}

scenario.recreate()

scenario.onActivity { activity ->
val newAppLanguageFragment = activity.supportFragmentManager
.findFragmentById(R.id.app_language_fragment_container) as AppLanguageFragment
val restoredLanguage =
newAppLanguageFragment.appLanguageFragmentPresenter.getLanguageSelected()

assertThat(restoredLanguage).isEqualTo(OppiaLanguage.ARABIC)
}
}
}

private fun verifyKiswahiliIsSelected(appLanguageActivity: AppLanguageActivity?) {
checkSelectedLanguage(index = KISWAHILI_BUTTON_INDEX, expectedLanguageName = "Kiswahili")
assertThat(appLanguageActivity?.appLanguageActivityPresenter?.getLanguageSelected()?.name)
Expand Down
Loading

0 comments on commit 16082aa

Please sign in to comment.