Testing Delayed Tasks in Robolectric

fragment.binding.filterBar.setText("laund")
assertEquals(2, adapter.itemCount)
fragment.binding.filterBar.setText("laund")
assertEquals(1, adapter.itemCount)
binding.filterBar.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}

override fun afterTextChanged(s: Editable?) {
view?.handler?.apply {
removeCallbacks(runnable)
postDelayed(runnable, 1000)
}
}

})
assertEquals(2, adapter.itemCount)
fragment.binding.filterBar.setText("laund")
ShadowLooper.runUiThreadTasksIncludingDelayedTasks()
assertEquals(1, adapter.itemCount)

--

--

--

Documenting my learnings on my journey as a software engineer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Understanding Streams in Flutter (Dart)

Understanding Koin Basics

Testing Dialogs in Robolectric

GloballyDynamic: Dynamic delivery during development

Avoid these mistakes in pubspec.yaml | Tips for Flutter beginners

Breaking the ice: Service Locator and Dependency Injection Which is What

isChecked vs. isSelected for Android CheckBoxes and RadioButtons

Complete learning path of Android

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Brian Terczynski

Brian Terczynski

Documenting my learnings on my journey as a software engineer.

More from Medium

Two mistakes when I use AutoValue

Transformations in encapsulated LiveData

Kotlin⚡️: Constructors | Erselan Khan

With Love: Bézier