This repository has been archived by the owner on Jan 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #335 from MaximeZmt/jwen/unfollow
Jwen/unfollow
- Loading branch information
Showing
15 changed files
with
334 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,18 +2,22 @@ package ch.sdp.vibester.activity.profile | |
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.view.View | ||
|
||
import androidx.recyclerview.widget.RecyclerView | ||
import androidx.test.core.app.ActivityScenario | ||
import androidx.test.core.app.ApplicationProvider | ||
import androidx.test.espresso.Espresso.onView | ||
import androidx.test.espresso.UiController | ||
import androidx.test.espresso.ViewAction | ||
import androidx.test.espresso.action.ViewActions | ||
import androidx.test.espresso.action.ViewActions.click | ||
import androidx.test.espresso.action.ViewActions.scrollTo | ||
import androidx.test.espresso.assertion.ViewAssertions.matches | ||
import androidx.test.espresso.contrib.RecyclerViewActions | ||
import androidx.test.espresso.intent.Intents | ||
import androidx.test.espresso.intent.matcher.IntentMatchers | ||
import androidx.test.espresso.matcher.BoundedMatcher | ||
import androidx.test.espresso.matcher.ViewMatchers.* | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import ch.sdp.vibester.R | ||
|
@@ -29,6 +33,8 @@ import dagger.hilt.android.testing.HiltAndroidTest | |
import io.mockk.every | ||
import io.mockk.mockk | ||
import org.hamcrest.CoreMatchers.not | ||
import org.hamcrest.Description | ||
import org.hamcrest.Matcher | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Rule | ||
|
@@ -85,6 +91,9 @@ class MyProfileActivityTest { | |
every { mockUsersRepo.getCurrentUser() } answers { null } | ||
every { mockUsersRepo.setFieldValue(any(), any(), any()) } answers {} | ||
every { mockUsersRepo.setFieldValue(any(), any(), any()) } answers {} | ||
|
||
every { mockUsersRepo.setFollowing(any(), any()) } answers {} | ||
every { mockUsersRepo.setUnfollow(any(), any()) } answers {} | ||
} | ||
|
||
private fun createMockImageGetter() { | ||
|
@@ -162,7 +171,7 @@ class MyProfileActivityTest { | |
} | ||
|
||
@Test | ||
fun friendsRecycleViewClickTest() { | ||
fun friendsRecycleViewGoToProfileTest() { | ||
val friendsMap: Map<String, Boolean> = mapOf( | ||
"friend1" to true, "friend2" to true | ||
) | ||
|
@@ -224,9 +233,6 @@ class MyProfileActivityTest { | |
} | ||
|
||
|
||
|
||
//FIXME layout does not display some elements | ||
|
||
@Test | ||
fun clickBackToProfile() { | ||
val inputProfile = User("Lalisa Bon", R.string.test_profile_image.toString(), "[email protected]", 12, 8) | ||
|
@@ -362,4 +368,104 @@ class MyProfileActivityTest { | |
onView(withId(R.id.profile_image_CardView)).check(matches(isDisplayed())) | ||
} | ||
|
||
@Test | ||
fun checkUnfollowBtnClick() { | ||
val friendsMap: Map<String, Boolean> = mapOf( | ||
"friend1" to true, "friend2" to true | ||
) | ||
val inputProfile = User("Lalisa Bon", R.string.test_profile_image.toString(), "[email protected]", | ||
12, following = friendsMap) | ||
val ctx = ApplicationProvider.getApplicationContext() as Context | ||
val intent = Intent(ctx, MyProfileActivity::class.java) | ||
|
||
createMockDataGetter(inputProfile) | ||
createMockAuthenticator() | ||
createMockImageGetter() | ||
|
||
val scn: ActivityScenario<MyProfileActivity> = ActivityScenario.launch(intent) | ||
|
||
onView(withId(R.id.profile_following)).perform(click()) | ||
|
||
checkRecyclerSubViews(R.id.profile_followingList, 0, withEffectiveVisibility(Visibility.VISIBLE), R.id.profile_unfollowIcon) | ||
checkRecyclerSubViews(R.id.profile_followingList, 0, withEffectiveVisibility(Visibility.INVISIBLE), R.id.profile_followingBtn) | ||
|
||
onView(withId(R.id.profile_followingList)).perform( | ||
RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>( | ||
0, clickOnViewChild(R.id.profile_unfollowIcon) | ||
) | ||
) | ||
|
||
checkRecyclerSubViews(R.id.profile_followingList, 0, withEffectiveVisibility(Visibility.VISIBLE), R.id.profile_followingBtn) | ||
checkRecyclerSubViews(R.id.profile_followingList, 0, withEffectiveVisibility(Visibility.INVISIBLE), R.id.profile_unfollowIcon) | ||
|
||
} | ||
|
||
@Test | ||
fun checkFollowBtnClick() { | ||
val friendsMap: Map<String, Boolean> = mapOf( | ||
"friend1" to true, "friend2" to true | ||
) | ||
val inputProfile = User("Lalisa Bon", R.string.test_profile_image.toString(), "[email protected]", | ||
12, following = friendsMap) | ||
val ctx = ApplicationProvider.getApplicationContext() as Context | ||
val intent = Intent(ctx, MyProfileActivity::class.java) | ||
|
||
createMockDataGetter(inputProfile) | ||
createMockAuthenticator() | ||
createMockImageGetter() | ||
|
||
val scn: ActivityScenario<MyProfileActivity> = ActivityScenario.launch(intent) | ||
|
||
onView(withId(R.id.profile_following)).perform(click()) | ||
|
||
onView(withId(R.id.profile_followingList)).perform( | ||
RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>( | ||
0, clickOnViewChild(R.id.profile_unfollowIcon) | ||
) | ||
) | ||
onView(withId(R.id.profile_followingList)).perform( | ||
RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>( | ||
0, clickOnViewChild(R.id.profile_followingBtn) | ||
) | ||
) | ||
|
||
checkRecyclerSubViews(R.id.profile_followingList, 0, withEffectiveVisibility(Visibility.INVISIBLE), R.id.profile_followingBtn) | ||
checkRecyclerSubViews(R.id.profile_followingList, 0, withEffectiveVisibility(Visibility.VISIBLE), R.id.profile_unfollowIcon) | ||
|
||
} | ||
|
||
/** | ||
* Custom function to handle button clicks inside recycleView | ||
*/ | ||
private fun clickOnViewChild(viewId: Int) = object : ViewAction { | ||
override fun getConstraints() = null | ||
|
||
override fun getDescription() = "Click on a child view with specified id." | ||
|
||
override fun perform(uiController: UiController, view: View) = click().perform(uiController, view.findViewById(viewId)) | ||
} | ||
|
||
/** | ||
* Custom functions to match the item views inside Recycle View | ||
*/ | ||
private fun checkRecyclerSubViews(recyclerViewId: Int, position: Int, itemMatcher: Matcher<View?>, subViewId: Int) { | ||
onView(withId(recyclerViewId)).perform( | ||
RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(position)) | ||
.check(matches(atPositionOnView(position, itemMatcher, subViewId))) | ||
} | ||
|
||
private fun atPositionOnView(position: Int, itemMatcher: Matcher<View?>, targetViewId: Int): Matcher<View?> { | ||
return object : BoundedMatcher<View?, RecyclerView>(RecyclerView::class.java) { | ||
override fun describeTo(description: Description) { | ||
description.appendText("has view id $itemMatcher at position $position") | ||
} | ||
|
||
override fun matchesSafely(recyclerView: RecyclerView): Boolean { | ||
val viewHolder = recyclerView.findViewHolderForAdapterPosition(position) | ||
val targetView = viewHolder!!.itemView.findViewById<View>(targetViewId) | ||
return itemMatcher.matches(targetView) | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.