#3980 Hitting "My Rank" button from a list position below your rank on leaderboard takes you to the person ranked below you (#3990)

* Refactored scrollToUserRank to account for users scrolling from below their rank

* Refactored my change to use a similar method

* Added comments to change

* Removed leftover cruft

* Reverted package updates

* Added tests for the LeaderBoard UI
This commit is contained in:
Rebecca Rothschild 2020-10-28 07:21:58 -07:00 committed by GitHub
parent 39221f5365
commit ee3e52d0c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 100 additions and 1 deletions

View file

@ -0,0 +1,91 @@
package fr.free.nrw.commons
import android.app.Activity
import android.app.Instrumentation.ActivityResult
import android.view.View
import androidx.test.espresso.Espresso
import androidx.test.espresso.PerformException
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.contrib.DrawerActions
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.Intents.intending
import androidx.test.espresso.intent.matcher.IntentMatchers.isInternal
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.rule.ActivityTestRule
import androidx.test.runner.AndroidJUnit4
import fr.free.nrw.commons.auth.LoginActivity
import androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import com.google.android.material.tabs.TabLayout
import org.hamcrest.CoreMatchers.allOf
import org.hamcrest.CoreMatchers.not
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class LeaderboardActivityTest {
@get:Rule
var activityRule = ActivityTestRule(LoginActivity::class.java)
@Before
fun setup() {
try {
Intents.init()
} catch (ex: IllegalStateException) {
}
UITestHelper.skipWelcome()
intending(not(isInternal())).respondWith(ActivityResult(Activity.RESULT_OK, null))
}
@Test
fun testScrollToRankFromAbove() {
Espresso.onView(ViewMatchers.withId(R.id.drawer_layout)).perform(DrawerActions.open())
Espresso.onView(ViewMatchers.withId(R.id.user_icon)).perform(ViewActions.click())
Espresso.onView(ViewMatchers.withId(R.id.tab_layout)).perform(ViewActions.click())
Espresso.onView(ViewMatchers.withId(R.id.tab_layout)).perform(selectTabAtPosition(1))
UITestHelper.sleep(10000)
Espresso.onView(ViewMatchers.withId(R.id.scroll)).perform(ViewActions.click())
}
@Test
fun testScrollToRankFromBelow() {
Espresso.onView(ViewMatchers.withId(R.id.drawer_layout)).perform(DrawerActions.open())
Espresso.onView(ViewMatchers.withId(R.id.user_icon)).perform(ViewActions.click())
Espresso.onView(ViewMatchers.withId(R.id.tab_layout)).perform(ViewActions.click())
Espresso.onView(ViewMatchers.withId(R.id.tab_layout)).perform(selectTabAtPosition(1))
UITestHelper.sleep(10000)
Espresso.onView(ViewMatchers.withId(R.id.leaderboard_list)).perform(ViewActions.swipeUp())
Espresso.onView(ViewMatchers.withId(R.id.leaderboard_list)).perform(ViewActions.swipeUp())
Espresso.onView(ViewMatchers.withId(R.id.scroll)).perform(ViewActions.click())
}
private fun selectTabAtPosition(tabIndex: Int): ViewAction {
return object : ViewAction {
override fun getDescription() = "with tab at index $tabIndex"
override fun getConstraints() = allOf(isDisplayed(), isAssignableFrom(TabLayout::class.java))
override fun perform(uiController: UiController, view: View) {
val tabLayout = view as TabLayout
val tabAtIndex: TabLayout.Tab = tabLayout.getTabAt(tabIndex)
?: throw PerformException.Builder()
.withCause(Throwable("No tab at index $tabIndex"))
.build()
tabAtIndex.select()
}
}
}
}

View file

@ -173,10 +173,18 @@ public class LeaderboardFragment extends CommonsDaggerSupportFragment {
/**
* Performs Auto Scroll to the User's Rank
* We use userRank+1 to load one extra user and prevent overlapping of my rank button
* If you are viewing the leaderboard below userRank, it scrolls to the user rank at the top
*/
private void scrollToUserRank() {
if (Objects.requireNonNull(leaderboardListRecyclerView.getAdapter()).getItemCount() > userRank + 1) {
leaderboardListRecyclerView.smoothScrollToPosition(userRank + 1);
int currPosition = ((LinearLayoutManager) leaderboardListRecyclerView.getLayoutManager())
.findFirstCompletelyVisibleItemPosition();
// if you are below your rank, scroll to userRank
if (currPosition > userRank) {
leaderboardListRecyclerView.smoothScrollToPosition(userRank);
} else {
leaderboardListRecyclerView.smoothScrollToPosition(userRank + 1);
}
} else {
if (viewModel != null) {
viewModel.refresh(duration, category, userRank + 1, 0);