-
-
Notifications
You must be signed in to change notification settings - Fork 643
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5166 from wikimedia/espresso-test-articles
Espresso test articles
- Loading branch information
Showing
37 changed files
with
1,678 additions
and
66 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
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
34 changes: 34 additions & 0 deletions
34
app/src/androidTest/java/org/wikipedia/base/DrawableMatcher.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,34 @@ | ||
package org.wikipedia.base | ||
|
||
import android.content.res.Resources | ||
import android.view.View | ||
import android.widget.ImageView | ||
import androidx.annotation.DrawableRes | ||
import androidx.test.espresso.matcher.BoundedMatcher | ||
import org.hamcrest.Description | ||
|
||
object DrawableMatcher { | ||
fun withDrawableId(@DrawableRes expectedId: Int) = | ||
object : BoundedMatcher<View, ImageView>(ImageView::class.java) { | ||
override fun describeTo(description: Description) { | ||
description.appendText("with drawable from resource id: $expectedId") | ||
} | ||
|
||
override fun matchesSafely(imageView: ImageView): Boolean { | ||
if (expectedId < 0) return false | ||
|
||
val context = imageView.context | ||
val resources = imageView.resources | ||
|
||
try { | ||
val expectedName = resources.getResourceEntryName(expectedId) | ||
val expectedType = resources.getResourceTypeName(expectedId) | ||
|
||
val foundId = resources.getIdentifier(expectedName, expectedType, context.packageName) | ||
return foundId == expectedId | ||
} catch (e: Resources.NotFoundException) { | ||
return false | ||
} | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
app/src/androidTest/java/org/wikipedia/base/ExecuteJavascriptAction.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,51 @@ | ||
package org.wikipedia.base | ||
|
||
import android.view.View | ||
import android.webkit.ValueCallback | ||
import android.webkit.WebView | ||
import androidx.test.espresso.PerformException | ||
import androidx.test.espresso.UiController | ||
import androidx.test.espresso.ViewAction | ||
import androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom | ||
import androidx.test.espresso.util.HumanReadables | ||
import org.hamcrest.Matcher | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
class ExecuteJavascriptAction(private val script: String) : ViewAction, ValueCallback<String> { | ||
private val evaluateFinished = AtomicBoolean(false) | ||
|
||
override fun getConstraints(): Matcher<View> { | ||
return isAssignableFrom(WebView::class.java) | ||
} | ||
|
||
override fun getDescription(): String { | ||
return "Execute Javascript Action" | ||
} | ||
|
||
override fun perform(uiController: UiController, view: View) { | ||
uiController.loopMainThreadUntilIdle() | ||
|
||
val webView = view as WebView | ||
val exception = PerformException.Builder() | ||
.withActionDescription(this.description) | ||
.withViewDescription(HumanReadables.describe(view)) | ||
|
||
webView.evaluateJavascript(script, this) | ||
|
||
val maxTime = System.currentTimeMillis() + 5000 | ||
while (!evaluateFinished.get()) { | ||
if (System.currentTimeMillis() > maxTime) { | ||
throw exception | ||
.withCause(RuntimeException("Evaluating Javascript timed out.")) | ||
.build() | ||
} | ||
uiController.loopMainThreadForAtLeast(50) | ||
} | ||
|
||
uiController.loopMainThreadForAtLeast(500) | ||
} | ||
|
||
override fun onReceiveValue(value: String?) { | ||
evaluateFinished.set(true) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
app/src/androidTest/java/org/wikipedia/base/GifMatchers.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,27 @@ | ||
package org.wikipedia.base | ||
|
||
import android.view.View | ||
import android.widget.ImageView | ||
import androidx.test.espresso.matcher.BoundedMatcher | ||
import com.bumptech.glide.load.resource.gif.GifDrawable | ||
import org.hamcrest.Description | ||
import org.hamcrest.Matcher | ||
|
||
object GifMatchers { | ||
fun hasGifDrawable(): Matcher<View> { | ||
return object : BoundedMatcher<View, ImageView>(ImageView::class.java) { | ||
override fun describeTo(description: Description) { | ||
description.appendText("has gif drawable") | ||
} | ||
|
||
override fun matchesSafely(imageView: ImageView): Boolean { | ||
val drawable = imageView.drawable | ||
|
||
return when (drawable) { | ||
is GifDrawable -> true | ||
else -> false | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.