From 749a09db6362acd56a03c95bb3409e104038c44c Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Sun, 12 Feb 2017 00:08:59 -0800 Subject: [PATCH] Convert dismissable_test.dart to ListView (#8078) This test was a bit tricky to convert because it subtly relied upon the lazy evaluation of an Iterable. The onDismissed from Dismissable happens during the animation phase of the pipeline. Previously, the ScrollableList had already been built for that frame but had not evaluated its Iterable yet. When we got to the layout phase, ScrollableList evaluated its Iterable and saw the updated version of dismissedItems. A straightforward conversion to ListView calls toList() when building the ListView, but that evaluates the iterable when buildTest() is called, which is before the calls to pump and therefore before the animation phase, meaning the Iterable sees the old value of dismissedItems. This patch fixes the test to use the normal setState pattern to signal that state upon which the build depends has changed. Now, the onDismissed callback happens during the animation phase and the StatefulBuilder is marked as dirty via setState, which causes it to rebuild the ListView and re-evaluate the Iterable, seeing the updated version of dismissedItems. This change also lets us replace the gratuious use of pumpWidget with pump now that we use setState rather than pumpWidget to trigger a rebuild. --- .../test/widgets/dismissable_test.dart | 88 +++++++++---------- 1 file changed, 43 insertions(+), 45 deletions(-) diff --git a/packages/flutter/test/widgets/dismissable_test.dart b/packages/flutter/test/widgets/dismissable_test.dart index 0a75c139e2..6135f16244 100644 --- a/packages/flutter/test/widgets/dismissable_test.dart +++ b/packages/flutter/test/widgets/dismissable_test.dart @@ -13,48 +13,46 @@ DismissDirection reportedDismissDirection; List dismissedItems = []; Widget background; -void handleOnResize(int item) { - expect(dismissedItems.contains(item), isFalse); -} - -void handleOnDismissed(DismissDirection direction, int item) { - reportedDismissDirection = direction; - expect(dismissedItems.contains(item), isFalse); - dismissedItems.add(item); -} - Widget buildTest({ double startToEndThreshold }) { - Widget buildDismissableItem(int item) { - return new Dismissable( - key: new ValueKey(item), - direction: dismissDirection, - onDismissed: (DismissDirection direction) { - handleOnDismissed(direction, item); - }, - onResize: () { - handleOnResize(item); - }, - background: background, - dismissThresholds: startToEndThreshold == null - ? {} - : {DismissDirection.startToEnd: startToEndThreshold}, - child: new Container( - width: itemExtent, - height: itemExtent, - child: new Text(item.toString()) - ), - ); - } + return new StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + Widget buildDismissableItem(int item) { + return new Dismissable( + key: new ValueKey(item), + direction: dismissDirection, + onDismissed: (DismissDirection direction) { + setState(() { + reportedDismissDirection = direction; + expect(dismissedItems.contains(item), isFalse); + dismissedItems.add(item); + }); + }, + onResize: () { + expect(dismissedItems.contains(item), isFalse); + }, + background: background, + dismissThresholds: startToEndThreshold == null + ? {} + : {DismissDirection.startToEnd: startToEndThreshold}, + child: new Container( + width: itemExtent, + height: itemExtent, + child: new Text(item.toString()), + ), + ); + } - return new Container( - padding: const EdgeInsets.all(10.0), - child: new ScrollableList( - scrollDirection: scrollDirection, - itemExtent: itemExtent, - children: [0, 1, 2, 3, 4] - .where((int i) => !dismissedItems.contains(i)) - .map(buildDismissableItem), - ) + return new Container( + padding: const EdgeInsets.all(10.0), + child: new ListView( + scrollDirection: scrollDirection, + itemExtent: itemExtent, + children: [0, 1, 2, 3, 4] + .where((int i) => !dismissedItems.contains(i)) + .map(buildDismissableItem).toList(), + ), + ); + }, ); } @@ -106,11 +104,11 @@ Future dismissItem(WidgetTester tester, int item, { DismissDirection gestu await dismissElement(tester, itemFinder, gestureDirection: gestureDirection); - await tester.pumpWidget(buildTest()); // start the slide - await tester.pumpWidget(buildTest(), const Duration(seconds: 1)); // finish the slide and start shrinking... - await tester.pumpWidget(buildTest()); // first frame of shrinking animation - await tester.pumpWidget(buildTest(), const Duration(seconds: 1)); // finish the shrinking and call the callback... - await tester.pumpWidget(buildTest()); // rebuild after the callback removes the entry + await tester.pump(); // start the slide + await tester.pump(const Duration(seconds: 1)); // finish the slide and start shrinking... + await tester.pump(); // first frame of shrinking animation + await tester.pump(const Duration(seconds: 1)); // finish the shrinking and call the callback... + await tester.pump(); // rebuild after the callback removes the entry } class Test1215DismissableWidget extends StatelessWidget {