-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
158 additions
and
3 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
27 changes: 27 additions & 0 deletions
27
library/src/main/java/com/qautomatron/kopi/library/matcher/ChildViewCountMatcher.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 com.qautomatron.kopi.library.matcher | ||
|
||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.test.espresso.matcher.BoundedMatcher | ||
import org.hamcrest.Description | ||
import org.hamcrest.Matcher | ||
import org.hamcrest.TypeSafeMatcher | ||
|
||
fun withChildViewCount(count: Int, childMatcher: Matcher<View?>): Matcher<View?> { | ||
return object : BoundedMatcher<View?, ViewGroup>(ViewGroup::class.java) { | ||
override fun matchesSafely(viewGroup: ViewGroup): Boolean { | ||
var matchCount = 0 | ||
for (i in 0 until viewGroup.childCount) { | ||
if (childMatcher.matches(viewGroup.getChildAt(i))) { | ||
matchCount++ | ||
} | ||
} | ||
return matchCount == count | ||
} | ||
|
||
override fun describeTo(description: Description) { | ||
description.appendText("ViewGroup with child-count=$count and") | ||
childMatcher.describeTo(description) | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
library/src/main/java/com/qautomatron/kopi/library/matcher/IsNotMovingMatcher.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,36 @@ | ||
package com.qautomatron.kopi.library.matcher | ||
|
||
import android.os.SystemClock | ||
import android.view.View | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.runBlocking | ||
import org.hamcrest.Description | ||
import org.hamcrest.TypeSafeMatcher | ||
|
||
class IsNotMovingMatcher(private val timeoutInMills: Int?) : TypeSafeMatcher<View>() { | ||
|
||
private val defaultTimeoutInMills: Int = 1000 | ||
private val pollingIntervalInMills: Int = 250 | ||
|
||
override fun matchesSafely(view: View): Boolean { | ||
val endTime = SystemClock.elapsedRealtime() + (timeoutInMills ?: defaultTimeoutInMills) | ||
val oldLocation = IntArray(2) | ||
val newLocation = IntArray(2) | ||
|
||
do { | ||
view.getLocationOnScreen(oldLocation) | ||
runBlocking { delay(pollingIntervalInMills.toLong()) } | ||
view.getLocationOnScreen(newLocation) | ||
|
||
if (newLocation.contentEquals(oldLocation)) { | ||
return true | ||
} | ||
} while (SystemClock.elapsedRealtime() < endTime) | ||
|
||
return false | ||
} | ||
|
||
override fun describeTo(description: Description) { | ||
description.appendText("which is stop moving before $timeoutInMills") | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
library/src/main/java/com/qautomatron/kopi/library/matcher/NthChildOfMatcher.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,21 @@ | ||
package com.qautomatron.kopi.library.matcher | ||
|
||
import android.view.View | ||
import android.view.ViewGroup | ||
import org.hamcrest.Description | ||
import org.hamcrest.Matcher | ||
import org.hamcrest.TypeSafeMatcher | ||
|
||
class NthChildOfMatcher(val parentMatcher: Matcher<View>, val childPosition: Int) : TypeSafeMatcher<View>() { | ||
override fun describeTo(description: Description) { | ||
description.appendText("with $childPosition child view of type parentMatcher") | ||
} | ||
|
||
override fun matchesSafely(view: View): Boolean { | ||
if (view.parent !is ViewGroup) { | ||
return parentMatcher.matches(view.parent) | ||
} | ||
val group = view.parent as ViewGroup | ||
return parentMatcher.matches(view.parent) && group.getChildAt(childPosition) == view | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
library/src/main/java/com/qautomatron/kopi/library/matcher/ToastMatcher.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,32 @@ | ||
package com.qautomatron.kopi.library.matcher | ||
|
||
import android.view.WindowManager | ||
import androidx.test.espresso.Root | ||
import org.hamcrest.Description | ||
import org.hamcrest.Matcher | ||
import org.hamcrest.TypeSafeMatcher | ||
|
||
class ToastMatcher : TypeSafeMatcher<Root>() { | ||
|
||
override fun describeTo(description: Description) { | ||
description.appendText("is toast") | ||
} | ||
|
||
public override fun matchesSafely(root: Root): Boolean { | ||
val type = root.windowLayoutParams.get().type | ||
if (type == WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY) { | ||
val windowToken = root.decorView.windowToken | ||
val appToken = root.decorView.applicationWindowToken | ||
if (windowToken === appToken) { | ||
// windowToken == appToken means this window isn't contained by any other windows. | ||
// if it was a window for an activity, it would have TYPE_BASE_APPLICATION. | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
fun isToast(): Matcher<Root> { | ||
return ToastMatcher() | ||
} | ||
} |
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