Skip to content

Commit

Permalink
Expand test
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesa2 committed Sep 6, 2023
1 parent 33bee07 commit a48ce71
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 3 deletions.
42 changes: 42 additions & 0 deletions demo/src/androidTest/java/info/hannes/slidingup/demo/Matcher.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package info.hannes.slidingup.demo

import android.util.Log
import android.view.View
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.matcher.BoundedMatcher
import androidx.test.espresso.matcher.ViewMatchers
import com.sothree.slidinguppanel.PanelState
import com.sothree.slidinguppanel.SlidingUpPanelLayout
import org.hamcrest.Description
import org.hamcrest.Matcher

fun withValue(expectedValue: PanelState): Matcher<View?> {
return object : BoundedMatcher<View?, SlidingUpPanelLayout>(SlidingUpPanelLayout::class.java) {
override fun describeTo(description: Description) {
description.appendText("expected: $expectedValue")
}

override fun matchesSafely(slider: SlidingUpPanelLayout): Boolean {
Log.d("slider", slider.panelState.toString())
return slider.panelState == expectedValue
}
}
}

fun setValue(value: PanelState): ViewAction {
return object : ViewAction {
override fun getDescription(): String {
return "Set Slider value to $value"
}

override fun getConstraints(): Matcher<View> {
return ViewMatchers.isAssignableFrom(SlidingUpPanelLayout::class.java)
}

override fun perform(uiController: UiController?, view: View) {
val seekBar = view as SlidingUpPanelLayout
seekBar.panelState = value
}
}
}
61 changes: 58 additions & 3 deletions demo/src/androidTest/java/info/hannes/slidingup/demo/StartTest.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
package info.hannes.slidingup.demo

import android.util.Log
import android.view.View
import androidx.test.espresso.assertion.ViewAssertions.matches
import com.sothree.slidinguppanel.demo.R
import androidx.test.core.graphics.writeToTestStorage
import androidx.test.espresso.Espresso
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.matcher.BoundedMatcher
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.screenshot.captureToBitmap
import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.sothree.slidinguppanel.PanelState
import com.sothree.slidinguppanel.SlidingUpPanelLayout
import com.sothree.slidinguppanel.demo.DemoActivity
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestName
Expand All @@ -15,6 +27,8 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class StartTest {

private val WAIT_SLIDER = 600L

@get:Rule
val activityScenarioRule = activityScenarioRule<DemoActivity>()

Expand All @@ -23,9 +37,50 @@ class StartTest {

@Test
fun smokeTestSimplyStart() {
Thread.sleep(1000)
Espresso.onView(ViewMatchers.isRoot())
onView(ViewMatchers.isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
}

@Test
fun testExpand() {
onView(ViewMatchers.isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-1")
onView(withId(R.id.sliding_layout)).check(matches(withValue(PanelState.COLLAPSED)))

onView(withId(R.id.sliding_layout)).perform(setValue(PanelState.EXPANDED))

Thread.sleep(WAIT_SLIDER)
onView(withId(R.id.sliding_layout)).check(matches(withValue(PanelState.EXPANDED)))
onView(ViewMatchers.isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-2")

onView(withId(R.id.sliding_layout)).perform(setValue(PanelState.COLLAPSED))
Thread.sleep(WAIT_SLIDER)
onView(withId(R.id.sliding_layout)).check(matches(withValue(PanelState.COLLAPSED)))
onView(ViewMatchers.isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-3")
}

@Test
fun testAnchorWithoutSetAnchored() {
onView(withId(R.id.sliding_layout)).check(matches(withValue(PanelState.COLLAPSED)))

onView(withId(R.id.sliding_layout)).perform(setValue(PanelState.EXPANDED))

Thread.sleep(WAIT_SLIDER)
onView(withId(R.id.sliding_layout)).check(matches(withValue(PanelState.EXPANDED)))

// without state anchored, a state ANCHORED should be ignored
onView(withId(R.id.sliding_layout)).perform(setValue(PanelState.ANCHORED))
Thread.sleep(WAIT_SLIDER)
// should be still EXPANDED
onView(withId(R.id.sliding_layout)).check(matches(withValue(PanelState.EXPANDED)))
onView(ViewMatchers.isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-2")
}
}

0 comments on commit a48ce71

Please sign in to comment.