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

Jwen/unfollow #335

Merged
merged 27 commits into from
May 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0053336
extract setUpRecycleView in SearchUserActivity.kt
jiabaow May 21, 2022
dffb899
extract listener inn onCreate() in SearchUserActivity.kt
jiabaow May 21, 2022
b2184f3
fix typo in SearchUserActivity.kt
jiabaow May 21, 2022
a2d0b53
show following icon in profile
jiabaow May 21, 2022
e93b473
change to ADD btn when click on unfollow btn in profile
jiabaow May 21, 2022
f7a65fc
check isFollowing in map
jiabaow May 21, 2022
6bece81
add followBtnListener in ProfileFollowingAdapter.kt
jiabaow May 21, 2022
6adeae2
add unfollowBtnListener in UserProfileAdapter.kt
jiabaow May 21, 2022
a90b19f
update ProfileFollowingAdapterTest.kt
jiabaow May 21, 2022
c2bb109
extract changeImageToBtn()
jiabaow May 21, 2022
66eb99d
extract follow unfollow in DataGetter.kt
jiabaow May 21, 2022
9ea5e1d
test unfollowBtnClick wip
jiabaow May 21, 2022
c6c46b5
test unfollowBtnClick finish
jiabaow May 21, 2022
6feb7d3
rename changeBtnToImage and rewrite documentation for changeImageToBtn
jiabaow May 21, 2022
cc2df69
refactor changeBtnToImage and changeImageToBtn
jiabaow May 21, 2022
23780cc
rename to switchViewsVisibility
jiabaow May 21, 2022
7f4599e
delete changeImageToBtn
jiabaow May 21, 2022
39670ee
delete changeBtnToImage in AdapterHelper.kt
jiabaow May 21, 2022
0e1db85
refactor changeBtnToImage
jiabaow May 21, 2022
136a1a0
remove empty line in PublicProfileActivity.kt
jiabaow May 21, 2022
20ffd63
merge main
jiabaow May 22, 2022
877d308
address Margaux's comment
jiabaow May 22, 2022
ef83d88
address Maxime's comment
jiabaow May 22, 2022
fe65d83
add space and delete empty line in SearchUserFragmentTest.kt
jiabaow May 22, 2022
1be3220
add documentation in AdapterHelper.kt
jiabaow May 22, 2022
aaea119
refactor ScoreBoardFragment.kt and genre_for_scoreboard.xml
jiabaow May 22, 2022
1edb23f
rename genre btns in scoreboard
jiabaow May 22, 2022
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 @@ -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() }

jiabaow marked this conversation as resolved.
Show resolved Hide resolved
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
jiabaow marked this conversation as resolved.
Show resolved Hide resolved
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