This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an IdlingResource for the progress bar to make tests timing-indep…
…endent.
- Loading branch information
1 parent
c776458
commit 0be587a
Showing
5 changed files
with
131 additions
and
8 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
53 changes: 53 additions & 0 deletions
53
app/src/androidTest/java/me/vickychijwani/spectre/testing/ActivityStateIdlingResource.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,53 @@ | ||
package me.vickychijwani.spectre.testing | ||
|
||
import android.app.Activity | ||
import android.os.Handler | ||
import android.support.annotation.NonNull | ||
import android.support.test.espresso.IdlingResource | ||
import java.lang.ref.WeakReference | ||
|
||
/** | ||
* [IdlingResource] that is idle when the given [Activity] passes the given predicate. | ||
* | ||
* Adapted from https://gist.github.com/vaughandroid/e2fda716c7cf6853fa79 | ||
*/ | ||
open class ActivityStateIdlingResource(@NonNull activity: Activity, | ||
private val mIdlePredicate: (Activity) -> Boolean) | ||
: IdlingResource { | ||
|
||
companion object { | ||
private val IDLE_POLL_DELAY_MILLIS = 100L | ||
} | ||
|
||
/* Hold a weak reference, so we don't leak memory even if the resource isn't unregistered. */ | ||
private val mActivity: WeakReference<Activity> = WeakReference(activity) | ||
private val mName: String = "IdlingResource for state of ${activity::class.java.simpleName} " + | ||
"(@${System.identityHashCode(activity)})" | ||
|
||
private var mResourceCallback: IdlingResource.ResourceCallback? = null | ||
|
||
override fun getName(): String { | ||
return mName | ||
} | ||
|
||
override fun isIdleNow(): Boolean { | ||
val activity = mActivity.get() | ||
val isIdle = if (activity != null) mIdlePredicate(activity) else true | ||
if (isIdle) { | ||
if (mResourceCallback != null) { | ||
mResourceCallback!!.onTransitionToIdle() | ||
} | ||
} else { | ||
/* Force a re-check of the idle state in a little while. If isIdleNow() returns false, | ||
* Espresso only polls it every few seconds which can slow down our tests. */ | ||
Handler().postDelayed({ isIdleNow }, IDLE_POLL_DELAY_MILLIS) | ||
} | ||
|
||
return isIdle | ||
} | ||
|
||
override fun registerIdleTransitionCallback(resourceCallback: IdlingResource.ResourceCallback) { | ||
mResourceCallback = resourceCallback | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
app/src/androidTest/java/me/vickychijwani/spectre/testing/ViewNotVisibleIdlingResource.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,20 @@ | ||
package me.vickychijwani.spectre.testing | ||
|
||
import android.app.Activity | ||
import android.support.annotation.IdRes | ||
import android.support.annotation.NonNull | ||
import android.support.test.espresso.IdlingResource | ||
import android.view.View | ||
|
||
/** | ||
* [IdlingResource] which is idle when a [View] with the given ID is NOT [View.VISIBLE] | ||
* in the given activity. | ||
*/ | ||
class ViewNotVisibleIdlingResource(@NonNull activity: Activity, | ||
@IdRes private val mViewId: Int) | ||
: ActivityStateIdlingResource(activity, | ||
{ act -> | ||
val view: View? = act.findViewById(mViewId) | ||
view == null || view.visibility != View.VISIBLE | ||
} | ||
) |
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