-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce generic assertAnyView function (#198)
* Extract ugly try/catch code to a magicAssertOnView function and use it I also created some infix extension functions to make it even easier to write. It extends any Matcher<View>, String or Int (resource). I added a `not` operator to Matcher<View>. I documented the behaviour the best I could. I replaced the in-site code with this function, and applied it to other methods of the BaristaVisibilityAssertions that were not doing any magic. * Rename and improve error message of BaristaArgumentTypeException * Apply magicAssert to EnabledAssertions * Apply magicAsset on CheckedAssertions * Apply magicAssert to BackgroundAssertions * Apply magicAssert to FocusedAssertions * Apply magicAssert to HintAssertions * Apply magicAssert to ImageViewAssertions * Extract assertion attempts to methods * Remove operator for matchers * Simplify magicAssert api * Rename to assertAny
- Loading branch information
Showing
10 changed files
with
126 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 8 additions & 7 deletions
15
library/src/main/java/com/schibsted/spain/barista/assertion/BaristaCheckedAssertions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,30 @@ | ||
package com.schibsted.spain.barista.assertion | ||
|
||
import android.support.test.espresso.Espresso.onView | ||
import android.support.test.espresso.assertion.ViewAssertions.matches | ||
import android.support.test.espresso.matcher.ViewMatchers.* | ||
import android.support.test.espresso.matcher.ViewMatchers.isChecked | ||
import android.support.test.espresso.matcher.ViewMatchers.withText | ||
import com.schibsted.spain.barista.internal.assertAny | ||
import com.schibsted.spain.barista.internal.util.resourceMatcher | ||
import org.hamcrest.Matchers.not | ||
|
||
object BaristaCheckedAssertions { | ||
|
||
@JvmStatic | ||
fun assertChecked(resId: Int) { | ||
onView(resId.resourceMatcher()).check(matches(isChecked())) | ||
resId.resourceMatcher().assertAny(isChecked()) | ||
} | ||
|
||
@JvmStatic | ||
fun assertChecked(text: String) { | ||
onView(withText(text)).check(matches(isChecked())) | ||
withText(text).assertAny(isChecked()) | ||
} | ||
|
||
@JvmStatic | ||
fun assertUnchecked(resId: Int) { | ||
onView(resId.resourceMatcher()).check(matches(isNotChecked())) | ||
resId.resourceMatcher().assertAny(not(isChecked())) | ||
} | ||
|
||
@JvmStatic | ||
fun assertUnchecked(text: String) { | ||
onView(withText(text)).check(matches(isNotChecked())) | ||
withText(text).assertAny(not(isChecked())) | ||
} | ||
} |
13 changes: 6 additions & 7 deletions
13
library/src/main/java/com/schibsted/spain/barista/assertion/BaristaEnabledAssertions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,30 @@ | ||
package com.schibsted.spain.barista.assertion | ||
|
||
import android.support.test.espresso.Espresso.onView | ||
import android.support.test.espresso.assertion.ViewAssertions.matches | ||
import android.support.test.espresso.matcher.ViewMatchers.isEnabled | ||
import android.support.test.espresso.matcher.ViewMatchers.withText | ||
import com.schibsted.spain.barista.internal.assertAny | ||
import com.schibsted.spain.barista.internal.util.resourceMatcher | ||
import org.hamcrest.core.IsNot.not | ||
import org.hamcrest.Matchers.not | ||
|
||
object BaristaEnabledAssertions { | ||
|
||
@JvmStatic | ||
fun assertEnabled(resId: Int) { | ||
onView(resId.resourceMatcher()).check(matches(isEnabled())) | ||
resId.resourceMatcher().assertAny(isEnabled()) | ||
} | ||
|
||
@JvmStatic | ||
fun assertEnabled(text: String) { | ||
onView(withText(text)).check(matches(isEnabled())) | ||
withText(text).assertAny(isEnabled()) | ||
} | ||
|
||
@JvmStatic | ||
fun assertDisabled(resId: Int) { | ||
onView(resId.resourceMatcher()).check(matches(not(isEnabled()))) | ||
resId.resourceMatcher().assertAny(not(isEnabled())) | ||
} | ||
|
||
@JvmStatic | ||
fun assertDisabled(text: String) { | ||
onView(withText(text)).check(matches(not(isEnabled()))) | ||
withText(text).assertAny(not(isEnabled())) | ||
} | ||
} |
15 changes: 7 additions & 8 deletions
15
library/src/main/java/com/schibsted/spain/barista/assertion/BaristaFocusedAssertions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,31 @@ | ||
package com.schibsted.spain.barista.assertion | ||
|
||
import android.support.annotation.IdRes | ||
import android.support.test.espresso.Espresso.onView | ||
import android.support.test.espresso.assertion.ViewAssertions.matches | ||
import android.support.test.espresso.matcher.ViewMatchers.hasFocus | ||
import android.support.test.espresso.matcher.ViewMatchers.withText | ||
import com.schibsted.spain.barista.internal.assertAny | ||
import com.schibsted.spain.barista.internal.util.resourceMatcher | ||
import org.hamcrest.core.IsNot.not | ||
import org.hamcrest.Matchers.not | ||
|
||
object BaristaFocusedAssertions { | ||
|
||
@JvmStatic | ||
fun assertFocused(@IdRes resId: Int) { | ||
onView(resId.resourceMatcher()).check(matches(hasFocus())) | ||
fun assertFocused(resId: Int) { | ||
resId.resourceMatcher().assertAny(hasFocus()) | ||
} | ||
|
||
@JvmStatic | ||
fun assertNotFocused(@IdRes resId: Int) { | ||
onView(resId.resourceMatcher()).check(matches(not(hasFocus()))) | ||
resId.resourceMatcher().assertAny(not(hasFocus())) | ||
} | ||
|
||
@JvmStatic | ||
fun assertFocused(text: String) { | ||
onView(withText(text)).check(matches(hasFocus())) | ||
withText(text).assertAny(hasFocus()) | ||
} | ||
|
||
@JvmStatic | ||
fun assertNotFocused(text: String) { | ||
onView(withText(text)).check(matches(not(hasFocus()))) | ||
withText(text).assertAny(not(hasFocus())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
library/src/main/java/com/schibsted/spain/barista/internal/AssertAny.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.schibsted.spain.barista.internal | ||
|
||
import android.support.test.espresso.Espresso.onView | ||
import android.support.test.espresso.assertion.ViewAssertions | ||
import android.view.View | ||
import com.schibsted.spain.barista.internal.failurehandler.SpyFailureHandler | ||
import com.schibsted.spain.barista.internal.failurehandler.description | ||
import com.schibsted.spain.barista.internal.matcher.HelperMatchers.firstViewOf | ||
import org.hamcrest.Matcher | ||
import org.hamcrest.Matchers.allOf | ||
|
||
/** | ||
* Extension function alias for [assertAnyView] | ||
*/ | ||
fun Matcher<View>.assertAny(condition: Matcher<View>) { | ||
assertAnyView(viewMatcher = this, condition = condition) | ||
} | ||
|
||
/** | ||
* Performs an assertion of a [condition] on a view described by [viewMatcher]. | ||
* | ||
* Attempts to assert using multiple scenarios for the [viewMatcher]: | ||
* 1. Just one view matches the [viewMatcher]. | ||
* 2. Multiple views match the [viewMatcher]: will pass if at least one of them matches the [condition]. | ||
*/ | ||
fun assertAnyView(viewMatcher: Matcher<View>, condition: Matcher<View>) { | ||
val spyFailureHandler = SpyFailureHandler() | ||
try { | ||
tryToAssert(viewMatcher, condition, spyFailureHandler) | ||
} catch (firstError: RuntimeException) { | ||
try { | ||
tryToAssertFirstView(viewMatcher, condition, spyFailureHandler) | ||
} catch (secondError: RuntimeException) { | ||
spyFailureHandler.resendFirstError("View ${viewMatcher.description()} wasn't displayed on the screen") | ||
} | ||
} | ||
} | ||
|
||
private fun tryToAssertFirstView(viewMatcher: Matcher<View>, condition: Matcher<View>, spyFailureHandler: SpyFailureHandler) { | ||
onView(firstViewOf(allOf(viewMatcher, condition))) | ||
.withFailureHandler(spyFailureHandler) | ||
.check(ViewAssertions.matches(condition)) | ||
} | ||
|
||
private fun tryToAssert(viewMatcher: Matcher<View>, condition: Matcher<View>, spyFailureHandler: SpyFailureHandler) { | ||
onView(viewMatcher) | ||
.withFailureHandler(spyFailureHandler) | ||
.check(ViewAssertions.matches(condition)) | ||
} |
Oops, something went wrong.