Commit 749a09db authored by Adam Barth's avatar Adam Barth Committed by GitHub

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.
parent 547f89d0
...@@ -13,26 +13,22 @@ DismissDirection reportedDismissDirection; ...@@ -13,26 +13,22 @@ DismissDirection reportedDismissDirection;
List<int> dismissedItems = <int>[]; List<int> dismissedItems = <int>[];
Widget background; 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 buildTest({ double startToEndThreshold }) {
return new StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
Widget buildDismissableItem(int item) { Widget buildDismissableItem(int item) {
return new Dismissable( return new Dismissable(
key: new ValueKey<int>(item), key: new ValueKey<int>(item),
direction: dismissDirection, direction: dismissDirection,
onDismissed: (DismissDirection direction) { onDismissed: (DismissDirection direction) {
handleOnDismissed(direction, item); setState(() {
reportedDismissDirection = direction;
expect(dismissedItems.contains(item), isFalse);
dismissedItems.add(item);
});
}, },
onResize: () { onResize: () {
handleOnResize(item); expect(dismissedItems.contains(item), isFalse);
}, },
background: background, background: background,
dismissThresholds: startToEndThreshold == null dismissThresholds: startToEndThreshold == null
...@@ -41,20 +37,22 @@ Widget buildTest({ double startToEndThreshold }) { ...@@ -41,20 +37,22 @@ Widget buildTest({ double startToEndThreshold }) {
child: new Container( child: new Container(
width: itemExtent, width: itemExtent,
height: itemExtent, height: itemExtent,
child: new Text(item.toString()) child: new Text(item.toString()),
), ),
); );
} }
return new Container( return new Container(
padding: const EdgeInsets.all(10.0), padding: const EdgeInsets.all(10.0),
child: new ScrollableList( child: new ListView(
scrollDirection: scrollDirection, scrollDirection: scrollDirection,
itemExtent: itemExtent, itemExtent: itemExtent,
children: <int>[0, 1, 2, 3, 4] children: <int>[0, 1, 2, 3, 4]
.where((int i) => !dismissedItems.contains(i)) .where((int i) => !dismissedItems.contains(i))
.map(buildDismissableItem), .map(buildDismissableItem).toList(),
) ),
);
},
); );
} }
...@@ -106,11 +104,11 @@ Future<Null> dismissItem(WidgetTester tester, int item, { DismissDirection gestu ...@@ -106,11 +104,11 @@ Future<Null> dismissItem(WidgetTester tester, int item, { DismissDirection gestu
await dismissElement(tester, itemFinder, gestureDirection: gestureDirection); await dismissElement(tester, itemFinder, gestureDirection: gestureDirection);
await tester.pumpWidget(buildTest()); // start the slide await tester.pump(); // start the slide
await tester.pumpWidget(buildTest(), const Duration(seconds: 1)); // finish the slide and start shrinking... await tester.pump(const Duration(seconds: 1)); // finish the slide and start shrinking...
await tester.pumpWidget(buildTest()); // first frame of shrinking animation await tester.pump(); // first frame of shrinking animation
await tester.pumpWidget(buildTest(), const Duration(seconds: 1)); // finish the shrinking and call the callback... await tester.pump(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(); // rebuild after the callback removes the entry
} }
class Test1215DismissableWidget extends StatelessWidget { class Test1215DismissableWidget extends StatelessWidget {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment