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

Created ViewActionExecutor to reuse performAction() code #194

Merged
merged 7 commits into from
Jul 2, 2018
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
@@ -1,17 +1,14 @@
package com.schibsted.spain.barista.interaction

import android.support.annotation.IdRes
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.matcher.ViewMatchers.withId
import com.schibsted.spain.barista.interaction.BaristaScrollInteractions.safelyScrollTo
import com.schibsted.spain.barista.internal.matcher.DisplayedMatchers.displayedAnd
import com.schibsted.spain.barista.internal.performAction
import com.schibsted.spain.barista.internal.viewaction.AutoCompleteViewActions.replaceAutoComplete

object BaristaAutoCompleteTextViewInteractions {

@JvmStatic
fun writeToAutoComplete(@IdRes autoCompleteId: Int, text: String) {
safelyScrollTo(autoCompleteId)
onView(displayedAnd(withId(autoCompleteId))).perform(replaceAutoComplete(text))
withId(autoCompleteId).performAction(replaceAutoComplete(text))
}
}
Original file line number Diff line number Diff line change
@@ -1,97 +1,36 @@
package com.schibsted.spain.barista.interaction

import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.Espresso.pressBack
import android.support.test.espresso.ViewAction
import android.support.test.espresso.action.ViewActions.click
import android.support.test.espresso.action.ViewActions.longClick
import android.support.test.espresso.action.ViewActions.scrollTo
import android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom
import android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA
import android.support.test.espresso.matcher.ViewMatchers.isDisplayed
import android.support.test.espresso.matcher.ViewMatchers.withText
import android.support.v4.widget.NestedScrollView
import android.view.View
import android.widget.AbsListView
import android.widget.HorizontalScrollView
import android.widget.ScrollView
import com.schibsted.spain.barista.internal.failurehandler.SpyFailureHandler
import com.schibsted.spain.barista.internal.failurehandler.description
import com.schibsted.spain.barista.internal.matcher.DisplayedMatchers.displayedAnd
import com.schibsted.spain.barista.internal.performAction
import com.schibsted.spain.barista.internal.util.resourceMatcher
import com.schibsted.spain.barista.internal.viewaction.NestedEnabledScrollToAction.nestedScrollToAction
import org.hamcrest.Matcher
import org.hamcrest.Matchers.allOf
import org.hamcrest.Matchers.anyOf

object BaristaClickInteractions {

@JvmStatic
fun clickBack() {
pressBack()
}

@JvmStatic
fun clickOn(resId: Int) {
performClickTypeOnMatcher(resId.resourceMatcher(), click())
}

@JvmStatic
fun clickOn(text: String) {
performClickTypeOnMatcher(withText(text), click())
}

@JvmStatic
fun longClickOn(resId: Int) {
performClickTypeOnMatcher(resId.resourceMatcher(), longClick())
}

@JvmStatic
fun longClickOn(text: String) {
performClickTypeOnMatcher(withText(text), longClick())
}

private fun performClickTypeOnMatcher(viewMatcher: Matcher<View>, clickType: ViewAction) {
val spyHandler = SpyFailureHandler()
try {
try {
performOnDisplayedView(viewMatcher, clickType, spyHandler)
} catch (firstError: RuntimeException) {
try {
scrollAndPerformOnView(viewMatcher, clickType, spyHandler)
} catch (secondError: RuntimeException) {
scrollAndPerformOnDisplayedView(viewMatcher, clickType, spyHandler)
}
}
} catch (fatalError: RuntimeException) {
spyHandler.resendFirstError("Could not click on view ${viewMatcher.description()}")
}
}

private fun scrollAndPerformOnView(viewMatcher: Matcher<View>, clickType: ViewAction, handler: SpyFailureHandler) {
onView(viewMatcher).withFailureHandler(handler).perform(nestedScrollToAction(), clickType)
}

private fun scrollAndPerformOnDisplayedView(viewMatcher: Matcher<View>, clickType: ViewAction, failureHandler: SpyFailureHandler) {
onView(allOf(
viewMatcher,
isDescendantOfA(allOf(
isDisplayed(),
anyOf(
isAssignableFrom(ScrollView::class.java),
isAssignableFrom(HorizontalScrollView::class.java),
isAssignableFrom(AbsListView::class.java),
isAssignableFrom(NestedScrollView::class.java)
)
))
))
.withFailureHandler(failureHandler)
.perform(scrollTo(), clickType)
}

private fun performOnDisplayedView(viewMatcher: Matcher<View>, clickType: ViewAction, failureHandler: SpyFailureHandler) {
onView(displayedAnd(viewMatcher))
.withFailureHandler(failureHandler)
.perform(clickType)
}
}
@JvmStatic
fun clickBack() {
pressBack()
}

@JvmStatic
fun clickOn(resId: Int) {
resId.resourceMatcher().performAction(click())
}

@JvmStatic
fun clickOn(text: String) {
withText(text).performAction(click())
}

@JvmStatic
fun longClickOn(resId: Int) {
resId.resourceMatcher().performAction(longClick())
}

@JvmStatic
fun longClickOn(text: String) {
withText(text).performAction(longClick())
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.schibsted.spain.barista.interaction

import android.support.annotation.IdRes
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.contrib.DrawerActions.close
import android.support.test.espresso.contrib.DrawerActions.open
import android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom
import android.support.test.espresso.matcher.ViewMatchers.withId
import android.support.v4.view.GravityCompat
import android.support.v4.widget.DrawerLayout
import com.schibsted.spain.barista.internal.performAction

object BaristaDrawerInteractions {

Expand All @@ -29,14 +29,14 @@ object BaristaDrawerInteractions {
@JvmOverloads
fun openDrawerWithGravity(@IdRes drawerId: Int? = null, gravity: Int) {
val matcher = findDrawerMatcher(drawerId)
onView(matcher).perform(open(gravity))
matcher.performAction(open(gravity))
}

@JvmStatic
@JvmOverloads
fun closeDrawerWithGravity(@IdRes drawerId: Int? = null, gravity: Int) {
val matcher = findDrawerMatcher(drawerId)
onView(matcher).perform(close(gravity))
matcher.performAction(close(gravity))
}

private fun findDrawerMatcher(drawerLayoutId: Int?) = when (drawerLayoutId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.schibsted.spain.barista.interaction

import android.support.annotation.IdRes
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.action.ViewActions.replaceText
import android.support.test.espresso.matcher.ViewMatchers.withId
import com.schibsted.spain.barista.interaction.BaristaScrollInteractions.safelyScrollTo
import com.schibsted.spain.barista.internal.matcher.DisplayedMatchers.displayedAnd
import com.schibsted.spain.barista.internal.performAction

object BaristaEditTextInteractions {

@JvmStatic
fun writeTo(@IdRes editTextId: Int, text: String) {
safelyScrollTo(editTextId)
onView(displayedAnd(withId(editTextId))).perform(replaceText(text))
withId(editTextId).performAction(replaceText(text))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,14 @@ package com.schibsted.spain.barista.interaction

import android.support.annotation.IdRes
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.ViewAction
import android.support.test.espresso.action.ViewActions
import android.support.test.espresso.action.ViewActions.closeSoftKeyboard
import android.support.test.espresso.matcher.ViewMatchers
import android.support.test.espresso.matcher.ViewMatchers.hasFocus
import android.support.test.espresso.matcher.ViewMatchers.isRoot
import android.support.test.espresso.matcher.ViewMatchers.withId
import android.support.v4.widget.NestedScrollView
import android.view.View
import android.widget.EditText
import android.widget.HorizontalScrollView
import android.widget.ListView
import android.widget.ScrollView
import com.schibsted.spain.barista.internal.failurehandler.SpyFailureHandler
import com.schibsted.spain.barista.internal.failurehandler.description
import com.schibsted.spain.barista.internal.matcher.DisplayedMatchers
import com.schibsted.spain.barista.internal.viewaction.NestedEnabledScrollToAction
import org.hamcrest.Matcher
import org.hamcrest.Matchers
import com.schibsted.spain.barista.internal.performAction
import org.hamcrest.core.AllOf.allOf

object BaristaKeyboardInteractions {
Expand All @@ -34,58 +23,11 @@ object BaristaKeyboardInteractions {
@JvmOverloads
fun pressImeActionButton(@IdRes editTextId: Int? = null) {
val matcher = findEditText(editTextId)
performPressImeActionButton(matcher, ViewActions.pressImeActionButton())
matcher.performAction(ViewActions.pressImeActionButton())
}

private fun findEditText(editTextId: Int?) = when (editTextId) {
null -> allOf(ViewMatchers.isAssignableFrom(EditText::class.java), hasFocus())
else -> withId(editTextId)
}

private fun performPressImeActionButton(viewMatcher: Matcher<View>, clickType: ViewAction) {
val spyHandler = SpyFailureHandler()
try {
try {
performOnDisplayedView(viewMatcher, clickType, spyHandler)
} catch (firstError: RuntimeException) {
try {
scrollAndPerformOnView(viewMatcher, clickType, spyHandler)
} catch (secondError: RuntimeException) {
scrollAndPerformOnDisplayedView(viewMatcher, clickType, spyHandler)
}
}
} catch (fatalError: RuntimeException) {
spyHandler.resendFirstError("Could not click on view ${viewMatcher.description()}")
}
}

private fun scrollAndPerformOnView(viewMatcher: Matcher<View>, clickType: ViewAction, handler: SpyFailureHandler) {
onView(viewMatcher).withFailureHandler(handler).perform(NestedEnabledScrollToAction.nestedScrollToAction(),
clickType)
}

private fun scrollAndPerformOnDisplayedView(viewMatcher: Matcher<View>, clickType: ViewAction,
failureHandler: SpyFailureHandler) {
onView(Matchers.allOf(
viewMatcher,
ViewMatchers.isDescendantOfA(Matchers.allOf(
ViewMatchers.isDisplayed(),
Matchers.anyOf(
ViewMatchers.isAssignableFrom(ScrollView::class.java),
ViewMatchers.isAssignableFrom(HorizontalScrollView::class.java),
ViewMatchers.isAssignableFrom(ListView::class.java),
ViewMatchers.isAssignableFrom(NestedScrollView::class.java)
)
))
))
.withFailureHandler(failureHandler)
.perform(ViewActions.scrollTo(), clickType)
}

private fun performOnDisplayedView(viewMatcher: Matcher<View>, clickType: ViewAction,
failureHandler: SpyFailureHandler) {
onView(DisplayedMatchers.displayedAnd(viewMatcher))
.withFailureHandler(failureHandler)
.perform(clickType)
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
package com.schibsted.spain.barista.interaction

import android.support.annotation.IdRes
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.action.ViewActions.click
import android.support.test.espresso.matcher.ViewMatchers.*
import android.view.View
import com.schibsted.spain.barista.internal.matcher.DisplayedMatchers.displayedAnd
import com.schibsted.spain.barista.internal.matcher.HelperMatchers.atPosition
import com.schibsted.spain.barista.interaction.BaristaScrollInteractions.safelyScrollTo
import android.support.test.espresso.matcher.ViewMatchers
import android.support.test.espresso.matcher.ViewMatchers.withId
import android.support.test.espresso.matcher.ViewMatchers.withText
import com.schibsted.spain.barista.internal.matcher.HelperMatchers.withParentId
import com.schibsted.spain.barista.internal.performAction
import org.hamcrest.Matchers.allOf

object BaristaRadioButtonInteractions {

@JvmStatic
fun clickRadioButtonItem(@IdRes radioGroupId: Int, @IdRes itemToClickId: Int) {
safelyScrollTo(radioGroupId)
onView(allOf<View>(withParent(displayedAnd(withId(radioGroupId))), displayedAnd(withId(itemToClickId))))
.perform(click())
allOf(withParentId(radioGroupId), withId(itemToClickId))
.performAction(click())
}

@JvmStatic
fun clickRadioButtonItem(@IdRes radioGroupId: Int, text: String) {
safelyScrollTo(radioGroupId)
onView(allOf<View>(withParent(displayedAnd(withId(radioGroupId))), displayedAnd(withText(text))))
.perform(click())
allOf(withParentId(radioGroupId), withText(text))
.performAction(click())
}

@JvmStatic
fun clickRadioButtonPosition(@IdRes radioGroupId: Int, position: Int) {
safelyScrollTo(radioGroupId)
onView(atPosition<View>(position, withParent(displayedAnd(withId(radioGroupId)))))
.perform(click())
allOf(withParentId(radioGroupId), ViewMatchers.withParentIndex(position))
.performAction(click())
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
package com.schibsted.spain.barista.interaction

import android.support.annotation.IdRes
import android.support.test.espresso.Espresso
import android.support.test.espresso.matcher.ViewMatchers
import com.schibsted.spain.barista.interaction.BaristaScrollInteractions.safelyScrollTo
import android.support.test.espresso.matcher.ViewMatchers.withId
import com.schibsted.spain.barista.internal.performAction
import com.schibsted.spain.barista.internal.viewaction.SeekBarActions.setSeekBarProgressTo
import com.schibsted.spain.barista.internal.viewaction.SeekBarActions.setSeekBarProgressToMax
import com.schibsted.spain.barista.internal.viewaction.SeekBarActions.setSeekBarProgressToMin

object BaristaSeekBarInteractions {
@JvmStatic
fun setProgressTo(@IdRes seekBarId: Int, progress: Int) {
safelyScrollTo(seekBarId)
Espresso.onView(ViewMatchers.withId(seekBarId)).perform(setSeekBarProgressTo(progress))
withId(seekBarId).performAction(setSeekBarProgressTo(progress))
}

@JvmStatic
fun setProgressToMin(@IdRes seekBarId: Int) {
safelyScrollTo(seekBarId)
Espresso.onView(ViewMatchers.withId(seekBarId)).perform(setSeekBarProgressToMin())
withId(seekBarId).performAction(setSeekBarProgressToMin())
}

@JvmStatic
fun setProgressToMax(@IdRes seekBarId: Int) {
safelyScrollTo(seekBarId)
Espresso.onView(ViewMatchers.withId(seekBarId)).perform(setSeekBarProgressToMax())
withId(seekBarId).performAction(setSeekBarProgressToMax())
}
}
Loading