You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am currently working on automating my tests by using instrumented tests with Espresso. In some test cases I want to verify the title/text of an alert, but there seems to be an issue with the alert. It blocks my code and therefore also my assertion from running as long as the alert is showing. The assertion runs just after the alert hides again. In my project, I am testing 2 different Fragments, in one, the alert does not execute synchronously as long as i don’t register a IdlingResource. In the other one, even if i dont register any IdlingResource, it doesnt work. (also the case in the example code below).
The output i am getting in Logcat is:
2020-04-24 11:30:44.816 29889-29889/? I/ViewInteraction: Performing 'single click' action on view with id: com.example.a:id/loginButton
...
2020-04-24 11:30:45.628 29889-29889/com.example.a D/Alert: Show Alert called
2020-04-24 11:30:49.965 29889-29889/com.example.a D/Alert: Alert hiding
2020-04-24 11:30:50.105 29889-29889/com.example.a D/RootViewPicker: No matching root available - waiting: 10ms for one to appear.
Here is some code with the according dependencies to reproduce it: build.gradle (:app)
class MyViewModel : ViewModel() {
private val repo = SomeRepository()
var requestStatus = MutableLiveData<String>()
fun login() {
viewModelScope.launch {
repo.login()
requestStatus.postValue("DONE")
}
}
}
SomeRepository
class SomeRepository {
fun login() = notIdleWhile {
// network request
Thread.sleep(500L)
}
}
NotIdleWhile
inline fun <T> notIdleWhile(block: () -> T): T {
EspressoIdlingResource.increment()
val result = try {
block()
} catch (t: Throwable) {
EspressoIdlingResource.decrement()
throw t
}
EspressoIdlingResource.decrement()
return result
}
EspressoIdlingResource
object EspressoIdlingResource {
val countingIdlingResource = CountingIdlingResource("RESOURCE")
fun increment() {
countingIdlingResource.increment()
}
fun decrement() {
if (!countingIdlingResource.isIdleNow)
countingIdlingResource.decrement()
}
}
FragmentExtension
fun Fragment.showAlert(@StringRes title: Int, @StringRes message: Int) {
if (Alerter.isShowing)
return
try {
val alerter = Alerter.create(activity)
.setTitle(title)
.setText(message)
.enableInfiniteDuration(false)
.setBackgroundColorRes(R.color.colorPrimary)
.setOnHideListener(OnHideAlertListener { Log.d("Alert","Alert hiding") })
alerter.show()
} catch (ex: Exception) {
Log.e("Alert","Couldn't show the alert due to: ${ex.message}")
}
}
InstrumentedTest
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@get:Rule
val actRule = ActivityTestRule(MainActivity::class.java)
@Before
fun setUp() {
/*IdlingRegistry.getInstance().register(
EspressoIdlingResource.countingIdlingResource)*/
actRule.activity.supportFragmentManager.beginTransaction()
.replace(R.id.frag_container, MyFragment()).commit()
}
@After
fun tearDown() {
/*IdlingRegistry.getInstance().unregister(
EspressoIdlingResource.countingIdlingResource)*/
}
@Test
fun showsAlertAfterClick(){
onView(withId(R.id.loginButton)).perform(click())
onView(withId(com.tapadoo.alerter.R.id.tvText))
.inRoot(withDecorView(hasDescendant(withId(com.tapadoo.alerter.R.id.tvText))))
.check(matches(withText(R.string.message)))
}
}
The text was updated successfully, but these errors were encountered:
I am currently working on automating my tests by using instrumented tests with Espresso. In some test cases I want to verify the title/text of an alert, but there seems to be an issue with the alert. It blocks my code and therefore also my assertion from running as long as the alert is showing. The assertion runs just after the alert hides again. In my project, I am testing 2 different Fragments, in one, the alert does not execute synchronously as long as i don’t register a IdlingResource. In the other one, even if i dont register any IdlingResource, it doesnt work. (also the case in the example code below).
The output i am getting in Logcat is:
Here is some code with the according dependencies to reproduce it:
build.gradle (:app)
MainActivity
MyFragment
MyViewModel
SomeRepository
NotIdleWhile
EspressoIdlingResource
FragmentExtension
InstrumentedTest
The text was updated successfully, but these errors were encountered: