Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ScrollViewActions support horizontal and nested scroll views #103

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -3,13 +3,17 @@
package io.github.kakaocup.kakao.scroll

import android.view.View
import android.widget.HorizontalScrollView
import android.widget.ScrollView
import androidx.core.widget.NestedScrollView
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom
import io.github.kakaocup.kakao.common.actions.ScrollableActions
import io.github.kakaocup.kakao.common.actions.SwipeableActions
import org.hamcrest.Matchers
import org.hamcrest.Matchers.anyOf

/**
* Provides ScrollableActions implementation for ScrollView
Expand All @@ -24,11 +28,20 @@ interface ScrollViewActions : ScrollableActions, SwipeableActions {
override fun getDescription() = "Scroll ScrollView to start"

override fun getConstraints() =
Matchers.allOf(ViewMatchers.isAssignableFrom(ScrollView::class.java), ViewMatchers.isDisplayed())
Matchers.allOf(
anyOf(
isAssignableFrom(ScrollView::class.java),
isAssignableFrom(NestedScrollView::class.java),
isAssignableFrom(HorizontalScrollView::class.java),
),
ViewMatchers.isDisplayed()
)

override fun perform(uiController: UiController?, view: View?) {
if (view is ScrollView) {
view.fullScroll(View.FOCUS_UP)
when (view) {
is ScrollView -> view.fullScroll(View.FOCUS_UP)
is NestedScrollView -> view.fullScroll(View.FOCUS_UP)
is HorizontalScrollView -> view.fullScroll(View.FOCUS_LEFT)
}
}
})
Expand All @@ -39,11 +52,20 @@ interface ScrollViewActions : ScrollableActions, SwipeableActions {
override fun getDescription() = "Scroll ScrollView to end"

override fun getConstraints() =
Matchers.allOf(ViewMatchers.isAssignableFrom(ScrollView::class.java), ViewMatchers.isDisplayed())
Matchers.allOf(
anyOf(
isAssignableFrom(ScrollView::class.java),
isAssignableFrom(NestedScrollView::class.java),
isAssignableFrom(HorizontalScrollView::class.java),
),
ViewMatchers.isDisplayed()
)

override fun perform(uiController: UiController?, view: View?) {
if (view is ScrollView) {
view.fullScroll(View.FOCUS_DOWN)
when (view) {
is ScrollView -> view.fullScroll(View.FOCUS_DOWN)
is NestedScrollView -> view.fullScroll(View.FOCUS_DOWN)
is HorizontalScrollView -> view.fullScroll(View.FOCUS_RIGHT)
}
}
})
Expand All @@ -54,11 +76,20 @@ interface ScrollViewActions : ScrollableActions, SwipeableActions {
override fun getDescription() = "Scroll ScrollView to $position Y position"

override fun getConstraints() =
Matchers.allOf(ViewMatchers.isAssignableFrom(ScrollView::class.java), ViewMatchers.isDisplayed())
Matchers.allOf(
anyOf(
isAssignableFrom(ScrollView::class.java),
isAssignableFrom(NestedScrollView::class.java),
isAssignableFrom(HorizontalScrollView::class.java),
),
ViewMatchers.isDisplayed()
)

override fun perform(uiController: UiController?, view: View?) {
if (view is ScrollView) {
view.scrollTo(0, position)
when (view) {
is ScrollView -> view.scrollTo(0, position)
is NestedScrollView -> view.scrollTo(0, position)
is HorizontalScrollView -> view.scrollTo(position, 0)
}
}
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.github.kakaocup.sample

import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import io.github.kakaocup.kakao.screen.Screen.Companion.onScreen
import io.github.kakaocup.sample.screen.ScrollScreen
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith


@RunWith(AndroidJUnit4ClassRunner::class)
class HorizontalScrollTest {
@Rule
@JvmField
val rule = ActivityScenarioRule(HorizontalScrollActivity::class.java)

@Test
fun test() {
onScreen<ScrollScreen> {
checkStart()
scroll { scrollToEnd() }
checkEnd()

scroll { scrollToStart() }
checkStart()

scroll { scrollTo(5000) }
checkEnd()
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.github.kakaocup.sample

import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import io.github.kakaocup.kakao.screen.Screen.Companion.onScreen
import io.github.kakaocup.sample.screen.ScrollScreen
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith


@RunWith(AndroidJUnit4ClassRunner::class)
class NestedScrollTest {
@Rule
@JvmField
val rule = ActivityScenarioRule(NestedScrollActivity::class.java)

@Test
fun test() {
onScreen<ScrollScreen> {
checkStart()
scroll { scrollToEnd() }
checkEnd()

scroll { scrollToStart() }
checkStart()

scroll { scrollTo(5000) }
checkEnd()
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.github.kakaocup.sample

import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import io.github.kakaocup.kakao.screen.Screen.Companion.onScreen
import io.github.kakaocup.sample.screen.ScrollScreen
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith


@RunWith(AndroidJUnit4ClassRunner::class)
class ScrollTest {
@Rule
@JvmField
val rule = ActivityScenarioRule(ScrollActivity::class.java)

@Test
fun test() {
onScreen<ScrollScreen> {
checkStart()
scroll { scrollToEnd() }
checkEnd()

scroll { scrollToStart() }
checkStart()

scroll { scrollTo(5000) }
checkEnd()
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.github.kakaocup.sample.screen

import io.github.kakaocup.kakao.screen.Screen
import io.github.kakaocup.kakao.scroll.KScrollView
import io.github.kakaocup.kakao.text.KTextView
import io.github.kakaocup.sample.R


class ScrollScreen : Screen<ScrollScreen>() {
val firstElement = KTextView { withId(R.id.first_element) }
val secondElement = KTextView { withId(R.id.second_element) }
val scroll = KScrollView { withId(R.id.scroll) }

fun checkStart() {
idle(1000)
firstElement {
isDisplayed()
}
secondElement {
isNotDisplayed()
}
}

fun checkEnd() {
idle(1000)
firstElement {
isNotDisplayed()
}
secondElement {
isDisplayed()
}
}
}
12 changes: 12 additions & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@
android:name="io.github.kakaocup.sample.NestedRecyclerActivity"
android:label="Nested Recycler Activity"
android:theme="@style/AppTheme" />
<activity
android:name="io.github.kakaocup.sample.NestedScrollActivity"
android:label="Nested scroll activity"
android:theme="@style/AppTheme" />
<activity
android:name="io.github.kakaocup.sample.HorizontalScrollActivity"
android:label="Horizontal scroll activity"
android:theme="@style/AppTheme" />
<activity
android:name="io.github.kakaocup.sample.ScrollActivity"
android:label="Scroll activity"
android:theme="@style/AppTheme" />
<activity
android:name="io.github.kakaocup.sample.WebActivity"
android:label="Web Activity"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.kakaocup.sample

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class HorizontalScrollActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_horizontal_scroll)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.kakaocup.sample

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class NestedScrollActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_nested_scroll)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.kakaocup.sample

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class ScrollActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_nested_scroll)
}
}
34 changes: 34 additions & 0 deletions sample/src/main/res/layout/activity_horizontal_scroll.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<HorizontalScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/first_element"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123" />

<View
android:layout_width="1000dp"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/second_element"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="321" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
34 changes: 34 additions & 0 deletions sample/src/main/res/layout/activity_nested_scroll.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<androidx.core.widget.NestedScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/first_element"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123" />

<View
android:layout_width="wrap_content"
android:layout_height="1000dp" />

<TextView
android:id="@+id/second_element"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="321" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
34 changes: 34 additions & 0 deletions sample/src/main/res/layout/activity_scroll.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/first_element"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123" />

<View
android:layout_width="wrap_content"
android:layout_height="1000dp" />

<TextView
android:id="@+id/second_element"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="321" />
</LinearLayout>
</ScrollView>
</LinearLayout>