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 #5485 Create means for verifying Fragment Arguments #5522

Merged
merged 21 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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 @@ -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
Loading