Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
Merge pull request #335 from MaximeZmt/jwen/unfollow
Browse files Browse the repository at this point in the history
Jwen/unfollow
  • Loading branch information
jiabaow authored May 22, 2022
2 parents aae09bb + 1edb23f commit 6afa49a
Show file tree
Hide file tree
Showing 15 changed files with 334 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -162,7 +171,7 @@ class MyProfileActivityTest {
}

@Test
fun friendsRecycleViewClickTest() {
fun friendsRecycleViewGoToProfileTest() {
val friendsMap: Map<String, Boolean> = mapOf(
"friend1" to true, "friend2" to true
)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ch.sdp.vibester.fragment

import androidx.recyclerview.widget.RecyclerView
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
Expand Down Expand Up @@ -46,19 +45,19 @@ class ScoreBoardFragmentTest {
@Test
fun genreShouldDisappearAfterSelected() {
onView(withId(R.id.genrePerScoreboard)).check(matches(isDisplayed()))
onView(withId(R.id.rockButton)).perform(click())
onView(withId(R.id.scoreboard_rockButton)).perform(click())
onView(withId(R.id.genrePerScoreboard)).check(matches(not(isDisplayed())))
}

@Test
fun rockBtnShouldSetUpRecycleView() {
onView(withId(R.id.rockButton)).perform(click())
onView(withId(R.id.scoreboard_rockButton)).perform(click())
onView(withId(R.id.scoreboard_content_scrolling)).check(matches(isDisplayed()))
}

@Test
fun clickOnItemShouldGoesToProfileAndDisplaysScores() {
onView(withId(R.id.rockButton)).perform(click())
onView(withId(R.id.scoreboard_rockButton)).perform(click())

onView((withId(R.id.recycler_view)))
.perform(
Expand All @@ -73,26 +72,26 @@ class ScoreBoardFragmentTest {

@Test
fun topBtnClick() {
onView(withId(R.id.topTracksButton)).perform(click())
onView(withId(R.id.scoreboard_topTracksButton)).perform(click())
}

@Test
fun kpopBtnClick() {
onView(withId(R.id.kpopButton)).perform(click())
onView(withId(R.id.scoreboard_kpopButton)).perform(click())
}

@Test
fun billieEilishButtonClick() {
onView(withId(R.id.billieEilishButton)).perform(click())
onView(withId(R.id.scoreboard_billieEilishButton)).perform(click())
}

@Test
fun imagineDragonsButtonClick() {
onView(withId(R.id.imagDragonsButton)).perform(click())
onView(withId(R.id.scoreboard_imagDragonsButton)).perform(click())
}

@Test
fun btsButtonClick() {
onView(withId(R.id.btsButton)).perform(click())
onView(withId(R.id.scoreboard_btsButton)).perform(click())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class SearchUserFragmentTest {
val mockUser3 = User("mockUser3", uid = "mockUser3uid")
val mockUser = User("mockUser", uid = "mockUseruid", following = mapOf(Pair(mockUser2.uid,true), Pair(mockUser3.uid,true)))

val mockUIDs = arrayListOf<String>("mockUser1uid","mockUser2uid","mockUser3uid")
val mockUsers = arrayListOf<User>(mockUser1, mockUser2, mockUser3)
val mockUIDs = arrayListOf("mockUser1uid","mockUser2uid","mockUser3uid")
val mockUsers = arrayListOf(mockUser1, mockUser2, mockUser3)

every { mockUsersRepo.searchByField(any(), any(), any(), any()) } answers {
thirdArg<(ArrayList<User>) -> Unit>().invoke(mockUsers)
Expand All @@ -61,11 +61,12 @@ class SearchUserFragmentTest {
secondArg<(User) -> Unit>().invoke(mockUser)
}

every { mockUsersRepo.getCurrentUser() } answers {createMockUser()}

every {mockUsersRepo.updateSubFieldInt(any(), any(), any(), any(),any())} answers {}
every {mockUsersRepo.setSubFieldValue(any(), any(), any(), any())} answers {}
every { mockUsersRepo.getCurrentUser() } answers { createMockUser() }

every { mockUsersRepo.updateSubFieldInt(any(), any(), any(), any(),any()) } answers {}
every { mockUsersRepo.setSubFieldValue(any(), any(), any(), any()) } answers {}
every { mockUsersRepo.setFollowing(any(), any()) } answers {}
every { mockUsersRepo.setUnfollow(any(), any()) } answers {}
}

@BindValue @JvmField
Expand Down Expand Up @@ -156,16 +157,37 @@ class SearchUserFragmentTest {
fun checkAddBtnClick(){
checkRecyclerSubViews(R.id.searchList, 0, withEffectiveVisibility(Visibility.VISIBLE), R.id.addFollowingBtn)
checkRecyclerSubViews(R.id.searchList, 0, withEffectiveVisibility(Visibility.INVISIBLE), R.id.addedFollowingIcon)

onView(withId(R.id.searchList))
.perform(
RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(
0,
clickOnViewChild(R.id.addFollowingBtn))
)

checkRecyclerSubViews(R.id.searchList, 0, withEffectiveVisibility(Visibility.INVISIBLE), R.id.addFollowingBtn)
checkRecyclerSubViews(R.id.searchList, 0, withEffectiveVisibility(Visibility.VISIBLE), R.id.addedFollowingIcon)
}

@Test
fun checkUnfollowBtnClick() {
//follow
onView(withId(R.id.searchList)).perform(
RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(
0, clickOnViewChild(R.id.addFollowingBtn)
)
)
//unfollow
onView(withId(R.id.searchList)).perform(
RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(
0, clickOnViewChild(R.id.addedFollowingIcon)
)
)

checkRecyclerSubViews(R.id.searchList, 0, withEffectiveVisibility(Visibility.VISIBLE), R.id.addFollowingBtn)
checkRecyclerSubViews(R.id.searchList, 0, withEffectiveVisibility(Visibility.INVISIBLE), R.id.addedFollowingIcon)
}

/**
* Custom function to handle button clicks inside recycleView
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package ch.sdp.vibester.user

import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.matcher.ViewMatchers.assertThat
import ch.sdp.vibester.auth.FireBaseAuthenticator
import ch.sdp.vibester.database.DataGetter
import org.hamcrest.CoreMatchers.equalTo
import org.junit.Test

Expand All @@ -13,7 +15,7 @@ class ProfileFollowingAdapterTest {
val friends: MutableList<User> = arrayListOf()
friends.addAll(listOf(friend1, friend2))
val profileFollowingViewHolder: RecyclerView.Adapter<ProfileFollowingAdapter.ProfileFollowingViewHolder> =
ProfileFollowingAdapter(friends, null)
ProfileFollowingAdapter(friends, DataGetter(), FireBaseAuthenticator(), null)
assertThat(profileFollowingViewHolder.itemCount, equalTo(friends.size))
}

Expand All @@ -24,7 +26,7 @@ class ProfileFollowingAdapterTest {
val friends: MutableList<User> = arrayListOf()
friends.addAll(listOf(friend1, friend2))
val profileFollowingViewHolder: RecyclerView.Adapter<ProfileFollowingAdapter.ProfileFollowingViewHolder> =
ProfileFollowingAdapter(friends, null)
ProfileFollowingAdapter(friends, DataGetter(), FireBaseAuthenticator(), null)
val defaultType = 0
assertThat(
profileFollowingViewHolder.getItemViewType(0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ open class ProfileActivity : AppCompatActivity(), OnItemClickListener {
val imageRequestCode = 100

private var followings: MutableList<User> ? = null
private var profileFriendsAdapter: ProfileFollowingAdapter?= null
private var profileFollowingAdapter: ProfileFollowingAdapter?= null

override fun onCreate(savedInstanceState: Bundle?) {
requestWindowFeature(Window.FEATURE_NO_TITLE)
Expand All @@ -70,14 +70,14 @@ open class ProfileActivity : AppCompatActivity(), OnItemClickListener {
private fun setupRecycleViewForFriends() {
findViewById<RecyclerView>(R.id.profile_followingList).apply {
layoutManager = LinearLayoutManager(context)
adapter = followings?.let { ProfileFollowingAdapter(it, this@ProfileActivity) }
adapter = followings?.let { ProfileFollowingAdapter(it, dataGetter, authenticator,this@ProfileActivity) }
setHasFixedSize(true)
}
}

private fun showFriendsPosition(friends: MutableList<User>?) {
profileFriendsAdapter = ProfileFollowingAdapter(friends!!, this)
findViewById<RecyclerView>(R.id.profile_followingList)!!.adapter = profileFriendsAdapter
profileFollowingAdapter = ProfileFollowingAdapter(friends!!, dataGetter, authenticator, this)
findViewById<RecyclerView>(R.id.profile_followingList)!!.adapter = profileFollowingAdapter
}


Expand Down Expand Up @@ -193,7 +193,7 @@ open class ProfileActivity : AppCompatActivity(), OnItemClickListener {
*
*/
private fun loadFollowing(followingMap: Map<String, Boolean>) {
followingMap.forEach { (userId, _) -> dataGetter.getUserData(userId, this::addFollowing)}
followingMap.forEach { (userId, isFollowing) -> if (isFollowing) dataGetter.getUserData(userId, this::addFollowing) }
showFriendsPosition(followings)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class PublicProfileActivity : ProfileActivity() {
}
}


override fun queryDatabase() {
dataGetter.getUserData(userId, this::setupProfile)
}
Expand Down
Loading

0 comments on commit 6afa49a

Please sign in to comment.