mozilla-mobile / fenix

⚠️ Fenix (Firefox for Android) moved to a new repository. It is now developed and maintained as part of: https://github.com/mozilla-mobile/firefox-android
https://github.com/mozilla-mobile/firefox-android
Mozilla Public License 2.0
6.47k stars 1.27k forks source link

[Bug]: Cannot run automated test on compose lazy list: java.lang.IllegalArgumentException: Failed requirement. at androidx.compose.ui.node.MeasureAndLayoutDelegate.measureAndLayout(MeasureAndLayoutDelegate.kt:190 #27879

Closed sv-ohorvath closed 1 year ago

sv-ohorvath commented 2 years ago

Steps to reproduce

More and more tests require scrolling compose lists as the Compose migration is advancing on Fenix, so I'm marking this as S1 to get some attention because it's blocking us from writing new UI tests.

Note: This is a copy of https://github.com/mozilla-mobile/focus-android/issues/7282, documenting my findings in testing Compose lazy lists. Not manually reproducible, this is affecting UI tests only, but needs an engineer's attention

While trying to refactor Focus' englishSystemLocaleTest() Recently visited search groups from Fenix, to test with Compose test rule, I hit this error trying to scroll the languages list:

java.lang.IllegalArgumentException: Failed requirement.
at androidx.compose.ui.node.MeasureAndLayoutDelegate.measureAndLayout(MeasureAndLayoutDelegate.kt:190)
at androidx.compose.ui.platform.AndroidComposeView.measureAndLayout(AndroidComposeView.android.kt:662)
at androidx.compose.ui.node.Owner$DefaultImpls.measureAndLayout$default(Owner.kt:182)
at androidx.compose.ui.node.LayoutNode.forceRemeasure(LayoutNode.kt:1339)
at androidx.compose.foundation.lazy.layout.LazyLayoutState.remeasure(LazyLayoutState.kt:54)
at androidx.compose.foundation.lazy.LazyListState.snapToItemIndexInternal$foundation_release(LazyListState.kt:202)
at androidx.compose.foundation.lazy.LazyListState$scrollToItem$2.invokeSuspend(LazyListState.kt:194)
at androidx.compose.foundation.lazy.LazyListState$scrollToItem$2.invoke(Unknown Source:8)
at androidx.compose.foundation.lazy.LazyListState$scrollToItem$2.invoke(Unknown Source:4)

or

java.lang.IllegalArgumentException: Can't scroll to index 3, it is out of bounds [0, 3)
at androidx.compose.foundation.lazy.LazySemanticsKt$lazyListSemantics$1$scrollToIndexAction$1.invoke(LazySemantics.kt:106)
at androidx.compose.foundation.lazy.LazySemanticsKt$lazyListSemantics$1$scrollToIndexAction$1.invoke(LazySemantics.kt:104)

Example: https://github.com/mozilla-mobile/fenix/issues/26976

While investigating I've tried a solution found here https://dmytroshuba.com/blog/jetpack-compose-ui-testing/#h-lateinit-property-remeasurement-has-not-been-initialized-in-lazylist* to replace scrollToItem(index) with animateScrollToItem(index) which should work the same way. The test passed now until I hit another crash which didn't seem so straightforward to fix: java.lang.IllegalStateException: measure() may not be called multiple times on the same Measurable. Current state InMeasureBlock. Parent state Measuring. Test log: https://console.firebase.google.com/u/1/project/moz-focus-android/testlab/histories/bh.2189b040bbce6d5a/matrices/5356848690213833442/executions/bs.c8e057ae5d7d1fc4/testcases/1/test-cases

Meanwhile, some of the UI tests are now written using UiAutomator and not scrolling the list (it will scroll only partially, not all the way down), and is blocking writing tests for more compose lists, like the Recently visited list, the Pocket stories list, the search suggestions list, etc.

Expected behaviour

-

Actual behaviour

-

Device name

Pixel

Android version

9, 11

Firefox release type

Firefox Nightly

Firefox version

108

Device logs

No response

Additional information

No response

┆Issue is synchronized with this Jira Task

Mugurell commented 2 years ago

Can talk about the usecases you're interested in.

sv-ohorvath commented 1 year ago

not reproducing anymore

allanconda-mercari commented 1 year ago

@sv-ohorvath Can you share how you fixed the issue?

sv-ohorvath commented 1 year ago

@aconda-mercari I didn't do anything, I think the problem might have been fixed by updating the Compose version.