Issue#67 Extract ViewActions into Library

This commit is contained in:
Gergely Hegedus 2022-05-27 17:55:38 +03:00
parent a27f19302a
commit 99141c0f17
12 changed files with 46 additions and 40 deletions

View file

@ -10,14 +10,13 @@ import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId import androidx.test.espresso.matcher.ViewMatchers.withId
import org.fnives.test.showcase.R import org.fnives.test.showcase.R
import org.fnives.test.showcase.testutils.configuration.SnackbarVerificationHelper import org.fnives.test.showcase.android.testutil.intent.notIntended
import org.fnives.test.showcase.testutils.viewactions.notIntended import org.fnives.test.showcase.android.testutil.snackbar.SnackbarVerificationHelper.assertSnackBarIsNotShown
import org.fnives.test.showcase.android.testutil.snackbar.SnackbarVerificationHelper.assertSnackBarIsShownWithText
import org.fnives.test.showcase.ui.home.MainActivity import org.fnives.test.showcase.ui.home.MainActivity
import org.hamcrest.core.IsNot.not import org.hamcrest.core.IsNot.not
class RobolectricLoginRobot( class RobolectricLoginRobot {
private val snackbarVerificationHelper: SnackbarVerificationHelper = SnackbarVerificationHelper()
) {
fun setUsername(username: String): RobolectricLoginRobot = apply { fun setUsername(username: String): RobolectricLoginRobot = apply {
onView(withId(R.id.user_edit_text)) onView(withId(R.id.user_edit_text))
@ -55,11 +54,11 @@ class RobolectricLoginRobot(
} }
fun assertErrorIsShown(@StringRes stringResID: Int) = apply { fun assertErrorIsShown(@StringRes stringResID: Int) = apply {
snackbarVerificationHelper.assertIsShownWithText(stringResID) assertSnackBarIsShownWithText(stringResID)
} }
fun assertErrorIsNotShown() = apply { fun assertErrorIsNotShown() = apply {
snackbarVerificationHelper.assertIsNotShown() assertSnackBarIsNotShown()
} }
fun assertNavigatedToHome() = apply { fun assertNavigatedToHome() = apply {

View file

@ -19,11 +19,11 @@ import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withParent import androidx.test.espresso.matcher.ViewMatchers.withParent
import androidx.test.espresso.matcher.ViewMatchers.withText import androidx.test.espresso.matcher.ViewMatchers.withText
import org.fnives.test.showcase.R import org.fnives.test.showcase.R
import org.fnives.test.showcase.android.testutil.intent.notIntended
import org.fnives.test.showcase.android.testutil.viewaction.imageview.WithDrawable
import org.fnives.test.showcase.android.testutil.viewaction.swiperefresh.PullToRefresh
import org.fnives.test.showcase.model.content.Content import org.fnives.test.showcase.model.content.Content
import org.fnives.test.showcase.model.content.FavouriteContent import org.fnives.test.showcase.model.content.FavouriteContent
import org.fnives.test.showcase.testutils.viewactions.PullToRefresh
import org.fnives.test.showcase.testutils.viewactions.WithDrawable
import org.fnives.test.showcase.testutils.viewactions.notIntended
import org.fnives.test.showcase.ui.auth.AuthActivity import org.fnives.test.showcase.ui.auth.AuthActivity
import org.hamcrest.Matchers.allOf import org.hamcrest.Matchers.allOf

View file

@ -14,15 +14,14 @@ import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId import androidx.test.espresso.matcher.ViewMatchers.withId
import org.fnives.test.showcase.R import org.fnives.test.showcase.R
import org.fnives.test.showcase.testutils.configuration.SnackbarVerificationHelper import org.fnives.test.showcase.android.testutil.intent.notIntended
import org.fnives.test.showcase.testutils.viewactions.ReplaceProgressBarDrawableToStatic import org.fnives.test.showcase.android.testutil.snackbar.SnackbarVerificationHelper.assertSnackBarIsNotShown
import org.fnives.test.showcase.testutils.viewactions.notIntended import org.fnives.test.showcase.android.testutil.snackbar.SnackbarVerificationHelper.assertSnackBarIsShownWithText
import org.fnives.test.showcase.android.testutil.viewaction.progressbar.ReplaceProgressBarDrawableToStatic
import org.fnives.test.showcase.ui.home.MainActivity import org.fnives.test.showcase.ui.home.MainActivity
import org.hamcrest.core.IsNot.not import org.hamcrest.core.IsNot.not
class LoginRobot( class LoginRobot {
private val snackbarVerificationHelper: SnackbarVerificationHelper = SnackbarVerificationHelper()
) {
fun setupIntentResults() { fun setupIntentResults() {
Intents.intending(hasComponent(MainActivity::class.java.canonicalName)) Intents.intending(hasComponent(MainActivity::class.java.canonicalName))
@ -68,7 +67,7 @@ class LoginRobot(
} }
fun assertErrorIsShown(@StringRes stringResID: Int) = apply { fun assertErrorIsShown(@StringRes stringResID: Int) = apply {
snackbarVerificationHelper.assertIsShownWithText(stringResID) assertSnackBarIsShownWithText(stringResID)
} }
fun assertLoadingBeforeRequests() = apply { fun assertLoadingBeforeRequests() = apply {
@ -82,7 +81,7 @@ class LoginRobot(
} }
fun assertErrorIsNotShown() = apply { fun assertErrorIsNotShown() = apply {
snackbarVerificationHelper.assertIsNotShown() assertSnackBarIsNotShown()
} }
fun assertNavigatedToHome() = apply { fun assertNavigatedToHome() = apply {

View file

@ -8,14 +8,13 @@ import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.matcher.IntentMatchers import androidx.test.espresso.intent.matcher.IntentMatchers
import androidx.test.espresso.matcher.ViewMatchers import androidx.test.espresso.matcher.ViewMatchers
import org.fnives.test.showcase.R import org.fnives.test.showcase.R
import org.fnives.test.showcase.testutils.configuration.SnackbarVerificationHelper import org.fnives.test.showcase.android.testutil.snackbar.SnackbarVerificationHelper.assertSnackBarIsNotShown
import org.fnives.test.showcase.testutils.viewactions.notIntended import org.fnives.test.showcase.android.testutil.snackbar.SnackbarVerificationHelper.assertSnackBarIsShownWithText
import org.fnives.test.showcase.android.testutil.intent.notIntended
import org.fnives.test.showcase.ui.home.MainActivity import org.fnives.test.showcase.ui.home.MainActivity
import org.hamcrest.core.IsNot import org.hamcrest.core.IsNot
class CodeKataSharedRobotTest( class CodeKataSharedRobotTest {
private val snackbarVerificationHelper: SnackbarVerificationHelper = SnackbarVerificationHelper()
) {
fun setUsername(username: String): CodeKataSharedRobotTest = apply { fun setUsername(username: String): CodeKataSharedRobotTest = apply {
Espresso.onView(ViewMatchers.withId(R.id.user_edit_text)) Espresso.onView(ViewMatchers.withId(R.id.user_edit_text))
@ -53,11 +52,11 @@ class CodeKataSharedRobotTest(
} }
fun assertErrorIsShown(@StringRes stringResID: Int): CodeKataSharedRobotTest = apply { fun assertErrorIsShown(@StringRes stringResID: Int): CodeKataSharedRobotTest = apply {
snackbarVerificationHelper.assertIsShownWithText(stringResID) assertSnackBarIsShownWithText(stringResID)
} }
fun assertErrorIsNotShown(): CodeKataSharedRobotTest = apply { fun assertErrorIsNotShown(): CodeKataSharedRobotTest = apply {
snackbarVerificationHelper.assertIsNotShown() assertSnackBarIsNotShown()
} }
fun assertNavigatedToHome(): CodeKataSharedRobotTest = apply { fun assertNavigatedToHome(): CodeKataSharedRobotTest = apply {

View file

@ -5,7 +5,7 @@ import android.app.Instrumentation
import android.content.Intent import android.content.Intent
import androidx.test.espresso.intent.Intents import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.matcher.IntentMatchers import androidx.test.espresso.intent.matcher.IntentMatchers
import org.fnives.test.showcase.testutils.viewactions.notIntended import org.fnives.test.showcase.android.testutil.intent.notIntended
import org.fnives.test.showcase.ui.auth.AuthActivity import org.fnives.test.showcase.ui.auth.AuthActivity
import org.fnives.test.showcase.ui.home.MainActivity import org.fnives.test.showcase.ui.home.MainActivity

View file

@ -31,7 +31,14 @@ android {
dependencies { dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version"
implementation "androidx.test:core:$androidx_test_version" implementation "androidx.test:core:$androidx_test_version"
implementation"androidx.test.espresso:espresso-core:$espresso_version" implementation "androidx.test.espresso:espresso-core:$espresso_version"
implementation "androidx.test.espresso:espresso-intents:$espresso_version"
implementation "com.squareup.okhttp3:okhttp:$okhttp_version" implementation "com.squareup.okhttp3:okhttp:$okhttp_version"
implementation "com.google.android.material:material:$androidx_material_version"
implementation "androidx.swiperefreshlayout:swiperefreshlayout:$androidx_swiperefreshlayout_version"
implementation "androidx.core:core-ktx:$androidx_core_version"
} }

View file

@ -1,4 +1,4 @@
package org.fnives.test.showcase.testutils.viewactions package org.fnives.test.showcase.android.testutil.intent
import android.content.Intent import android.content.Intent
import androidx.test.espresso.intent.Intents.intended import androidx.test.espresso.intent.Intents.intended

View file

@ -1,5 +1,6 @@
package org.fnives.test.showcase.testutils.configuration package org.fnives.test.showcase.android.testutil.snackbar
import android.annotation.SuppressLint
import android.view.View import android.view.View
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.test.espresso.Espresso import androidx.test.espresso.Espresso
@ -8,15 +9,16 @@ import androidx.test.espresso.ViewAction
import androidx.test.espresso.action.ViewActions import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.assertion.ViewAssertions import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.matcher.ViewMatchers import androidx.test.espresso.matcher.ViewMatchers
import com.google.android.material.R import com.google.android.material.R as MaterialR
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import org.hamcrest.Matcher import org.hamcrest.Matcher
import org.hamcrest.Matchers import org.hamcrest.Matchers
class SnackbarVerificationHelper { object SnackbarVerificationHelper {
fun assertIsShownWithText(@StringRes stringResID: Int, doDismiss: Boolean = true) { @SuppressLint("RestrictedApi")
Espresso.onView(ViewMatchers.withId(R.id.snackbar_text)) fun assertSnackBarIsShownWithText(@StringRes stringResID: Int, doDismiss: Boolean = true) {
Espresso.onView(ViewMatchers.withId(MaterialR.id.snackbar_text))
.check(ViewAssertions.matches(ViewMatchers.withText(stringResID))) .check(ViewAssertions.matches(ViewMatchers.withText(stringResID)))
if (doDismiss) { if (doDismiss) {
Espresso.onView(ViewMatchers.isAssignableFrom(Snackbar.SnackbarLayout::class.java)).perform(ViewActions.swipeRight()) Espresso.onView(ViewMatchers.isAssignableFrom(Snackbar.SnackbarLayout::class.java)).perform(ViewActions.swipeRight())
@ -24,8 +26,8 @@ class SnackbarVerificationHelper {
} }
} }
fun assertIsNotShown() { fun assertSnackBarIsNotShown() {
Espresso.onView(ViewMatchers.withId(R.id.snackbar_text)).check(ViewAssertions.doesNotExist()) Espresso.onView(ViewMatchers.withId(MaterialR.id.snackbar_text)).check(ViewAssertions.doesNotExist())
} }
class LoopMainUntilSnackbarDismissed : ViewAction { class LoopMainUntilSnackbarDismissed : ViewAction {
@ -39,4 +41,4 @@ class SnackbarVerificationHelper {
} }
} }
} }
} }

View file

@ -1,4 +1,4 @@
package org.fnives.test.showcase.testutils.viewactions package org.fnives.test.showcase.android.testutil.viewaction.imageview
import android.content.res.ColorStateList import android.content.res.ColorStateList
import android.graphics.PorterDuff import android.graphics.PorterDuff
@ -26,7 +26,7 @@ class WithDrawable(
override fun matchesSafely(view: View): Boolean { override fun matchesSafely(view: View): Boolean {
val context = view.context val context = view.context
val tintColor = tint?.let { ContextCompat.getColor(view.context, it) } val tintColor = tint?.let { ContextCompat.getColor(view.context, it) }
val expectedBitmap = context.getDrawable(id)?.apply { val expectedBitmap = ContextCompat.getDrawable(context, id)?.apply {
if (tintColor != null) { if (tintColor != null) {
setTintList(ColorStateList.valueOf(tintColor)) setTintList(ColorStateList.valueOf(tintColor))
setTintMode(tintMode) setTintMode(tintMode)

View file

@ -1,4 +1,4 @@
package org.fnives.test.showcase.testutils.viewactions package org.fnives.test.showcase.android.testutil.viewaction.progressbar
import android.graphics.Color import android.graphics.Color
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable

View file

@ -1,4 +1,4 @@
package org.fnives.test.showcase.testutils.viewactions package org.fnives.test.showcase.android.testutil.viewaction.swiperefresh
import android.view.View import android.view.View
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import androidx.swiperefreshlayout.widget.SwipeRefreshLayout