From c36ec76002e246e6d07a3a5d9bb8628ff868902e Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Tue, 15 May 2018 14:23:44 -0700 Subject: [PATCH] Reduce odds of race in scrollUntilVisible (#17594) In FlutterDriver.scrollUntilVisible, pause momentarily after we kick off the waitFor() call, in order to give that call a chance to complete before we start scrolling if the element is on-screen. --- .../flutter_driver/lib/src/driver/driver.dart | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/packages/flutter_driver/lib/src/driver/driver.dart b/packages/flutter_driver/lib/src/driver/driver.dart index 57d7890142..754ba7692a 100644 --- a/packages/flutter_driver/lib/src/driver/driver.dart +++ b/packages/flutter_driver/lib/src/driver/driver.dart @@ -478,21 +478,16 @@ class FlutterDriver { assert(dxScroll != 0.0 || dyScroll != 0.0); assert(timeout != null); - // If the item is already visible then we're done. + // Kick off an (unawaited) waitFor that will complete when the item we're + // looking for finally scrolls onscreen. We add an initial pause to give it + // the chance to complete if the item is already onscreen; if not, scroll + // repeatedly until we either find the item or time out. bool isVisible = false; - try { - await waitFor(item, timeout: const Duration(milliseconds: 100)); - isVisible = true; - } on DriverError { - // Assume that that waitFor timed out because the item isn't visible. - } - - if (!isVisible) { - waitFor(item, timeout: timeout).then((Null _) { isVisible = true; }); - while (!isVisible) { - await scroll(scrollable, dxScroll, dyScroll, const Duration(milliseconds: 100)); - await new Future.delayed(const Duration(milliseconds: 500)); - } + waitFor(item, timeout: timeout).then((Null _) { isVisible = true; }); + await new Future.delayed(const Duration(milliseconds: 500)); + while (!isVisible) { + await scroll(scrollable, dxScroll, dyScroll, const Duration(milliseconds: 100)); + await new Future.delayed(const Duration(milliseconds: 500)); } return scrollIntoView(item, alignment: alignment);