From fb84577c7b50901f7b6964675f741104f0946a3e Mon Sep 17 00:00:00 2001 From: Rajat Talesra Date: Mon, 5 Jul 2021 22:24:08 +0530 Subject: [PATCH] Fix #3410: Merged Switch click with TextViews in ProfileEditActivity (#3409) * Merged switch click with TextViews in Profile Edit * Added tests * Added more tests Co-authored-by: Rajat Talesra --- .../profile/ProfileEditFragmentPresenter.kt | 34 +++--- .../res/layout-land/profile_edit_fragment.xml | 104 +++++++++--------- .../main/res/layout/profile_edit_fragment.xml | 104 +++++++++--------- .../profile/ProfileEditActivityTest.kt | 101 +++++++++++++++++ 4 files changed, 224 insertions(+), 119 deletions(-) diff --git a/app/src/main/java/org/oppia/android/app/settings/profile/ProfileEditFragmentPresenter.kt b/app/src/main/java/org/oppia/android/app/settings/profile/ProfileEditFragmentPresenter.kt index a42597d1d13..bbba90e6a1e 100644 --- a/app/src/main/java/org/oppia/android/app/settings/profile/ProfileEditFragmentPresenter.kt +++ b/app/src/main/java/org/oppia/android/app/settings/profile/ProfileEditFragmentPresenter.kt @@ -85,24 +85,24 @@ class ProfileEditFragmentPresenter @Inject constructor( } ) - binding.profileEditAllowDownloadSwitch.setOnCheckedChangeListener { compoundButton, checked -> - if (compoundButton.isPressed) { - profileManagementController.updateAllowDownloadAccess( - ProfileId.newBuilder().setInternalId(internalProfileId).build(), - checked - ).toLiveData().observe( - activity, - Observer { - if (it.isFailure()) { - oppiaLogger.e( - "ProfileEditActivityPresenter", - "Failed to updated allow download access", - it.getErrorOrNull()!! - ) - } + binding.profileEditAllowDownloadContainer.setOnClickListener { + binding.profileEditAllowDownloadSwitch.isChecked = + !binding.profileEditAllowDownloadSwitch.isChecked + profileManagementController.updateAllowDownloadAccess( + ProfileId.newBuilder().setInternalId(internalProfileId).build(), + binding.profileEditAllowDownloadSwitch.isChecked + ).toLiveData().observe( + activity, + Observer { + if (it.isFailure()) { + oppiaLogger.e( + "ProfileEditActivityPresenter", + "Failed to updated allow download access", + it.getErrorOrNull()!! + ) } - ) - } + } + ) } return binding.root } diff --git a/app/src/main/res/layout-land/profile_edit_fragment.xml b/app/src/main/res/layout-land/profile_edit_fragment.xml index d74d664d776..0625ee0fce8 100644 --- a/app/src/main/res/layout-land/profile_edit_fragment.xml +++ b/app/src/main/res/layout-land/profile_edit_fragment.xml @@ -135,61 +135,63 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/profile_reset_button" /> - - - - - - - + app:layout_constraintTop_toBottomOf="@id/view4"> + + + + + + + + app:layout_constraintTop_toBottomOf="@+id/profile_edit_allow_download_container" /> - - - - - - - + app:layout_constraintTop_toBottomOf="@id/view4"> + + + + + + + + app:layout_constraintTop_toBottomOf="@+id/profile_edit_allow_download_container" /> ( + ProfileEditActivity.createProfileEditActivity( + context = context, + profileId = 4 + ) + ).use { + testCoroutineDispatchers.runCurrent() + onView(withId(R.id.profile_edit_allow_download_switch)).check(matches(isChecked())) + onView(withId(R.id.profile_edit_allow_download_container)).perform(click()) + onView(withId(R.id.profile_edit_allow_download_switch)).check(matches(not(isChecked()))) + } + } + + @Test + fun testProfileEdit_startWithUserHasDownloadAccess_switchIsNotClickable() { + profileManagementController.addProfile( + name = "James", + pin = "123", + avatarImagePath = null, + allowDownloadAccess = true, + colorRgb = -10710042, + isAdmin = false + ).toLiveData() + launch( + ProfileEditActivity.createProfileEditActivity( + context = context, + profileId = 4 + ) + ).use { + testCoroutineDispatchers.runCurrent() + onView(withId(R.id.profile_edit_allow_download_switch)).check(matches(not(isClickable()))) + } + } + + @Test + fun testProfileEdit_startWithUserHasDownloadAccess_switchContainerIsFocusable() { + profileManagementController.addProfile( + name = "James", + pin = "123", + avatarImagePath = null, + allowDownloadAccess = true, + colorRgb = -10710042, + isAdmin = false + ).toLiveData() + launch( + ProfileEditActivity.createProfileEditActivity( + context = context, + profileId = 4 + ) + ).use { + testCoroutineDispatchers.runCurrent() + onView(withId(R.id.profile_edit_allow_download_container)).check(matches(isFocusable())) + } + } + + @Test + fun testProfileEdit_startWithUserHasDownloadAccess_switchContainerIsDisplayed() { + profileManagementController.addProfile( + name = "James", + pin = "123", + avatarImagePath = null, + allowDownloadAccess = true, + colorRgb = -10710042, + isAdmin = false + ).toLiveData() + launch( + ProfileEditActivity.createProfileEditActivity( + context = context, + profileId = 4 + ) + ).use { + testCoroutineDispatchers.runCurrent() + onView(withId(R.id.profile_edit_allow_download_container)).check(matches(isDisplayed())) + } + } + + @Test + fun testProfileEdit_startWithUserDoesNotHaveDownloadAccess_switchContainerIsNotDisplayed() { + launch( + ProfileEditActivity.createProfileEditActivity( + context = context, + profileId = 0 + ) + ).use { + testCoroutineDispatchers.runCurrent() + onView(withId(R.id.profile_edit_allow_download_container)).check(matches(not(isDisplayed()))) + } + } + // TODO(#59): Figure out a way to reuse modules instead of needing to re-declare them. @Singleton @Component(