Skip to content

Commit 9928db6

Browse files
authored
Merge pull request #103 from Nikitae57/scrollTo-supports-only-scrollView
Make ScrollViewActions support horizontal and nested scroll views
2 parents 4ec839b + 3ded376 commit 9928db6

File tree

12 files changed

+322
-9
lines changed

12 files changed

+322
-9
lines changed

kakao/src/main/kotlin/io/github/kakaocup/kakao/scroll/ScrollViewActions.kt

+40-9
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
package io.github.kakaocup.kakao.scroll
44

55
import android.view.View
6+
import android.widget.HorizontalScrollView
67
import android.widget.ScrollView
8+
import androidx.core.widget.NestedScrollView
79
import androidx.test.espresso.UiController
810
import androidx.test.espresso.ViewAction
911
import androidx.test.espresso.matcher.ViewMatchers
12+
import androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom
1013
import io.github.kakaocup.kakao.common.actions.ScrollableActions
1114
import io.github.kakaocup.kakao.common.actions.SwipeableActions
1215
import org.hamcrest.Matchers
16+
import org.hamcrest.Matchers.anyOf
1317

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

2630
override fun getConstraints() =
27-
Matchers.allOf(ViewMatchers.isAssignableFrom(ScrollView::class.java), ViewMatchers.isDisplayed())
31+
Matchers.allOf(
32+
anyOf(
33+
isAssignableFrom(ScrollView::class.java),
34+
isAssignableFrom(NestedScrollView::class.java),
35+
isAssignableFrom(HorizontalScrollView::class.java),
36+
),
37+
ViewMatchers.isDisplayed()
38+
)
2839

2940
override fun perform(uiController: UiController?, view: View?) {
30-
if (view is ScrollView) {
31-
view.fullScroll(View.FOCUS_UP)
41+
when (view) {
42+
is ScrollView -> view.fullScroll(View.FOCUS_UP)
43+
is NestedScrollView -> view.fullScroll(View.FOCUS_UP)
44+
is HorizontalScrollView -> view.fullScroll(View.FOCUS_LEFT)
3245
}
3346
}
3447
})
@@ -39,11 +52,20 @@ interface ScrollViewActions : ScrollableActions, SwipeableActions {
3952
override fun getDescription() = "Scroll ScrollView to end"
4053

4154
override fun getConstraints() =
42-
Matchers.allOf(ViewMatchers.isAssignableFrom(ScrollView::class.java), ViewMatchers.isDisplayed())
55+
Matchers.allOf(
56+
anyOf(
57+
isAssignableFrom(ScrollView::class.java),
58+
isAssignableFrom(NestedScrollView::class.java),
59+
isAssignableFrom(HorizontalScrollView::class.java),
60+
),
61+
ViewMatchers.isDisplayed()
62+
)
4363

4464
override fun perform(uiController: UiController?, view: View?) {
45-
if (view is ScrollView) {
46-
view.fullScroll(View.FOCUS_DOWN)
65+
when (view) {
66+
is ScrollView -> view.fullScroll(View.FOCUS_DOWN)
67+
is NestedScrollView -> view.fullScroll(View.FOCUS_DOWN)
68+
is HorizontalScrollView -> view.fullScroll(View.FOCUS_RIGHT)
4769
}
4870
}
4971
})
@@ -54,11 +76,20 @@ interface ScrollViewActions : ScrollableActions, SwipeableActions {
5476
override fun getDescription() = "Scroll ScrollView to $position Y position"
5577

5678
override fun getConstraints() =
57-
Matchers.allOf(ViewMatchers.isAssignableFrom(ScrollView::class.java), ViewMatchers.isDisplayed())
79+
Matchers.allOf(
80+
anyOf(
81+
isAssignableFrom(ScrollView::class.java),
82+
isAssignableFrom(NestedScrollView::class.java),
83+
isAssignableFrom(HorizontalScrollView::class.java),
84+
),
85+
ViewMatchers.isDisplayed()
86+
)
5887

5988
override fun perform(uiController: UiController?, view: View?) {
60-
if (view is ScrollView) {
61-
view.scrollTo(0, position)
89+
when (view) {
90+
is ScrollView -> view.scrollTo(0, position)
91+
is NestedScrollView -> view.scrollTo(0, position)
92+
is HorizontalScrollView -> view.scrollTo(position, 0)
6293
}
6394
}
6495
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.github.kakaocup.sample
2+
3+
import androidx.test.ext.junit.rules.ActivityScenarioRule
4+
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
5+
import io.github.kakaocup.kakao.screen.Screen.Companion.onScreen
6+
import io.github.kakaocup.sample.screen.ScrollScreen
7+
import org.junit.Rule
8+
import org.junit.Test
9+
import org.junit.runner.RunWith
10+
11+
12+
@RunWith(AndroidJUnit4ClassRunner::class)
13+
class HorizontalScrollTest {
14+
@Rule
15+
@JvmField
16+
val rule = ActivityScenarioRule(HorizontalScrollActivity::class.java)
17+
18+
@Test
19+
fun test() {
20+
onScreen<ScrollScreen> {
21+
checkStart()
22+
scroll { scrollToEnd() }
23+
checkEnd()
24+
25+
scroll { scrollToStart() }
26+
checkStart()
27+
28+
scroll { scrollTo(5000) }
29+
checkEnd()
30+
}
31+
}
32+
}
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.github.kakaocup.sample
2+
3+
import androidx.test.ext.junit.rules.ActivityScenarioRule
4+
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
5+
import io.github.kakaocup.kakao.screen.Screen.Companion.onScreen
6+
import io.github.kakaocup.sample.screen.ScrollScreen
7+
import org.junit.Rule
8+
import org.junit.Test
9+
import org.junit.runner.RunWith
10+
11+
12+
@RunWith(AndroidJUnit4ClassRunner::class)
13+
class NestedScrollTest {
14+
@Rule
15+
@JvmField
16+
val rule = ActivityScenarioRule(NestedScrollActivity::class.java)
17+
18+
@Test
19+
fun test() {
20+
onScreen<ScrollScreen> {
21+
checkStart()
22+
scroll { scrollToEnd() }
23+
checkEnd()
24+
25+
scroll { scrollToStart() }
26+
checkStart()
27+
28+
scroll { scrollTo(5000) }
29+
checkEnd()
30+
}
31+
}
32+
}
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.github.kakaocup.sample
2+
3+
import androidx.test.ext.junit.rules.ActivityScenarioRule
4+
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
5+
import io.github.kakaocup.kakao.screen.Screen.Companion.onScreen
6+
import io.github.kakaocup.sample.screen.ScrollScreen
7+
import org.junit.Rule
8+
import org.junit.Test
9+
import org.junit.runner.RunWith
10+
11+
12+
@RunWith(AndroidJUnit4ClassRunner::class)
13+
class ScrollTest {
14+
@Rule
15+
@JvmField
16+
val rule = ActivityScenarioRule(ScrollActivity::class.java)
17+
18+
@Test
19+
fun test() {
20+
onScreen<ScrollScreen> {
21+
checkStart()
22+
scroll { scrollToEnd() }
23+
checkEnd()
24+
25+
scroll { scrollToStart() }
26+
checkStart()
27+
28+
scroll { scrollTo(5000) }
29+
checkEnd()
30+
}
31+
}
32+
}
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.github.kakaocup.sample.screen
2+
3+
import io.github.kakaocup.kakao.screen.Screen
4+
import io.github.kakaocup.kakao.scroll.KScrollView
5+
import io.github.kakaocup.kakao.text.KTextView
6+
import io.github.kakaocup.sample.R
7+
8+
9+
class ScrollScreen : Screen<ScrollScreen>() {
10+
val firstElement = KTextView { withId(R.id.first_element) }
11+
val secondElement = KTextView { withId(R.id.second_element) }
12+
val scroll = KScrollView { withId(R.id.scroll) }
13+
14+
fun checkStart() {
15+
idle(1000)
16+
firstElement {
17+
isDisplayed()
18+
}
19+
secondElement {
20+
isNotDisplayed()
21+
}
22+
}
23+
24+
fun checkEnd() {
25+
idle(1000)
26+
firstElement {
27+
isNotDisplayed()
28+
}
29+
secondElement {
30+
isDisplayed()
31+
}
32+
}
33+
}

sample/src/main/AndroidManifest.xml

+12
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@
3939
android:name="io.github.kakaocup.sample.NestedRecyclerActivity"
4040
android:label="Nested Recycler Activity"
4141
android:theme="@style/AppTheme" />
42+
<activity
43+
android:name="io.github.kakaocup.sample.NestedScrollActivity"
44+
android:label="Nested scroll activity"
45+
android:theme="@style/AppTheme" />
46+
<activity
47+
android:name="io.github.kakaocup.sample.HorizontalScrollActivity"
48+
android:label="Horizontal scroll activity"
49+
android:theme="@style/AppTheme" />
50+
<activity
51+
android:name="io.github.kakaocup.sample.ScrollActivity"
52+
android:label="Scroll activity"
53+
android:theme="@style/AppTheme" />
4254
<activity
4355
android:name="io.github.kakaocup.sample.WebActivity"
4456
android:label="Web Activity"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.github.kakaocup.sample
2+
3+
import android.os.Bundle
4+
import androidx.appcompat.app.AppCompatActivity
5+
6+
class HorizontalScrollActivity : AppCompatActivity() {
7+
8+
override fun onCreate(savedInstanceState: Bundle?) {
9+
super.onCreate(savedInstanceState)
10+
setContentView(R.layout.activity_horizontal_scroll)
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.github.kakaocup.sample
2+
3+
import android.os.Bundle
4+
import androidx.appcompat.app.AppCompatActivity
5+
6+
class NestedScrollActivity : AppCompatActivity() {
7+
8+
override fun onCreate(savedInstanceState: Bundle?) {
9+
super.onCreate(savedInstanceState)
10+
setContentView(R.layout.activity_nested_scroll)
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.github.kakaocup.sample
2+
3+
import android.os.Bundle
4+
import androidx.appcompat.app.AppCompatActivity
5+
6+
class ScrollActivity : AppCompatActivity() {
7+
8+
override fun onCreate(savedInstanceState: Bundle?) {
9+
super.onCreate(savedInstanceState)
10+
setContentView(R.layout.activity_nested_scroll)
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical">
6+
7+
<HorizontalScrollView
8+
android:id="@+id/scroll"
9+
android:layout_width="match_parent"
10+
android:layout_height="wrap_content">
11+
12+
<LinearLayout
13+
android:layout_width="wrap_content"
14+
android:layout_height="wrap_content"
15+
android:orientation="horizontal">
16+
17+
<TextView
18+
android:id="@+id/first_element"
19+
android:layout_width="wrap_content"
20+
android:layout_height="wrap_content"
21+
android:text="123" />
22+
23+
<View
24+
android:layout_width="1000dp"
25+
android:layout_height="wrap_content" />
26+
27+
<TextView
28+
android:id="@+id/second_element"
29+
android:layout_width="wrap_content"
30+
android:layout_height="wrap_content"
31+
android:text="321" />
32+
</LinearLayout>
33+
</HorizontalScrollView>
34+
</LinearLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical">
6+
7+
<androidx.core.widget.NestedScrollView
8+
android:id="@+id/scroll"
9+
android:layout_width="match_parent"
10+
android:layout_height="wrap_content">
11+
12+
<LinearLayout
13+
android:layout_width="match_parent"
14+
android:layout_height="wrap_content"
15+
android:orientation="vertical">
16+
17+
<TextView
18+
android:id="@+id/first_element"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
android:text="123" />
22+
23+
<View
24+
android:layout_width="wrap_content"
25+
android:layout_height="1000dp" />
26+
27+
<TextView
28+
android:id="@+id/second_element"
29+
android:layout_width="match_parent"
30+
android:layout_height="wrap_content"
31+
android:text="321" />
32+
</LinearLayout>
33+
</androidx.core.widget.NestedScrollView>
34+
</LinearLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical">
6+
7+
<ScrollView
8+
android:id="@+id/scroll"
9+
android:layout_width="match_parent"
10+
android:layout_height="wrap_content">
11+
12+
<LinearLayout
13+
android:layout_width="match_parent"
14+
android:layout_height="wrap_content"
15+
android:orientation="vertical">
16+
17+
<TextView
18+
android:id="@+id/first_element"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
android:text="123" />
22+
23+
<View
24+
android:layout_width="wrap_content"
25+
android:layout_height="1000dp" />
26+
27+
<TextView
28+
android:id="@+id/second_element"
29+
android:layout_width="match_parent"
30+
android:layout_height="wrap_content"
31+
android:text="321" />
32+
</LinearLayout>
33+
</ScrollView>
34+
</LinearLayout>

0 commit comments

Comments
 (0)