mozilla-mobile / android-components

⚠️ This project moved to a new repository. It is now developed and maintained at: https://github.com/mozilla-mobile/firefox-android
https://github.com/mozilla-mobile/firefox-android
Mozilla Public License 2.0
2.02k stars 474 forks source link

Add helper functions for tests to assert received LiveData values #1725

Closed mcomella closed 5 years ago

mcomella commented 5 years ago

LiveData is tricky to test because you need to attach observers in order to assert the values received: this means the code cannot be written in an intuitive, sequential fashion. For example, on FFES I wrote:

        viewModel.setIsVoiceViewEnabled(true)
        viewModel.setIsNavigationOverlayVisible(false)
        viewModel.isToolbarScrollEnabled.observeForever { assertFalse(it!!) }
        viewModel.setIsNavigationOverlayVisible(true)

(where ToolbarScrollEnabled is modified by changes in VoiceView and NavOverlay).

On FFTV, there's a test helper so that you can write tests like:

viewModel.isToolbarScrollEnabled.assertValues(false, false, false, false) {
        viewModel.setIsVoiceViewEnabled(true)
        viewModel.setIsNavigationOverlayVisible(false)
        viewModel.setIsNavigationOverlayVisible(true)
}

The test helpers are defined here – https://github.com/mozilla-mobile/firefox-tv/blob/master/app/src/test/java/org/mozilla/tv/firefox/helpers/ext/LiveDataTestHelper.kt – and have their own tests here: https://github.com/mozilla-mobile/firefox-tv/blob/master/app/src/test/java/org/mozilla/tv/firefox/helpers/ext/LiveDataTestHelperTest.kt (which annoying run in the same test suite as the application tests).

We should use these helpers or adapt some new helpers.

┆Issue is synchronized with this Jira Task

mcomella commented 5 years ago

afaik, all of our teams are moving towards Rx: I don't think this is necessary.