Commit 1a61a76e authored by Adam Barth's avatar Adam Barth Committed by GitHub

Port scrollable_lazy_list_test.dart to ListView (#8025)

We call the builder in slightly different ways, but the new results
seem reasonable.
parent ca2c54ba
...@@ -2,15 +2,13 @@ ...@@ -2,15 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:async';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'test_widgets.dart'; import 'test_widgets.dart';
void main() { void main() {
testWidgets('HomogeneousViewport mount/dismount smoke test', (WidgetTester tester) async { testWidgets('ListView.builder mount/dismount smoke test', (WidgetTester tester) async {
List<int> callbackTracker = <int>[]; List<int> callbackTracker = <int>[];
// the root view is 800x600 in the test environment // the root view is 800x600 in the test environment
...@@ -18,22 +16,18 @@ void main() { ...@@ -18,22 +16,18 @@ void main() {
Widget builder() { Widget builder() {
return new FlipWidget( return new FlipWidget(
left: new ScrollableLazyList( left: new ListView.builder(
itemBuilder: (BuildContext context, int start, int count) { itemExtent: 100.0,
List<Widget> result = <Widget>[]; itemBuilder: (BuildContext context, int index) {
for (int index = start; index < start + count; index += 1) {
callbackTracker.add(index); callbackTracker.add(index);
result.add(new Container( return new Container(
key: new ValueKey<int>(index), key: new ValueKey<int>(index),
height: 100.0, height: 100.0,
child: new Text("$index") child: new Text('$index'),
)); );
}
return result;
}, },
itemExtent: 100.0
), ),
right: new Text('Not Today') right: new Text('Not Today'),
); );
} }
...@@ -56,166 +50,171 @@ void main() { ...@@ -56,166 +50,171 @@ void main() {
expect(callbackTracker, equals(<int>[0, 1, 2, 3, 4, 5])); expect(callbackTracker, equals(<int>[0, 1, 2, 3, 4, 5]));
}); });
testWidgets('HomogeneousViewport vertical', (WidgetTester tester) async { testWidgets('ListView.builder vertical', (WidgetTester tester) async {
List<int> callbackTracker = <int>[]; List<int> callbackTracker = <int>[];
// the root view is 800x600 in the test environment // the root view is 800x600 in the test environment
// so if our widget is 200 pixels tall, it should fit exactly 3 times. // so if our widget is 200 pixels tall, it should fit exactly 3 times.
// but if we are offset by 300 pixels, there will be 4, numbered 1-4. // but if we are offset by 300 pixels, there will be 4, numbered 1-4.
ItemListBuilder itemBuilder = (BuildContext context, int start, int count) { IndexedWidgetBuilder itemBuilder = (BuildContext context, int index) {
List<Widget> result = <Widget>[];
for (int index = start; index < start + count; index += 1) {
callbackTracker.add(index); callbackTracker.add(index);
result.add(new Container( return new Container(
key: new ValueKey<int>(index), key: new ValueKey<int>(index),
width: 500.0, // this should be ignored width: 500.0, // this should be ignored
height: 400.0, // should be overridden by itemExtent height: 400.0, // should be overridden by itemExtent
child: new Text("$index") child: new Text('$index')
)); );
}
return result;
}; };
FlipWidget testWidget = new FlipWidget( FlipWidget buildWidget() {
left: new ScrollableLazyList( return new FlipWidget(
itemBuilder: itemBuilder, left: new ListView.builder(
controller: new ScrollController(initialScrollOffset: 300.0),
itemExtent: 200.0, itemExtent: 200.0,
initialScrollOffset: 300.0 itemBuilder: itemBuilder,
), ),
right: new Text('Not Today') right: new Text('Not Today')
); );
Completer<Null> scrollTo(double newScrollOffset) {
Completer<Null> completer = new Completer<Null>();
final ScrollableState scrollable = tester.state(find.byType(Scrollable));
scrollable.scrollTo(newScrollOffset).whenComplete(completer.complete);
return completer;
} }
await tester.pumpWidget(testWidget); void jumpTo(double newScrollOffset) {
final Scrollable2State scrollable = tester.state(find.byType(Scrollable2));
scrollable.position.jumpTo(newScrollOffset);
}
expect(callbackTracker, equals(<int>[1, 2, 3, 4])); await tester.pumpWidget(buildWidget());
expect(callbackTracker, equals(<int>[1, 2, 3, 4]));
callbackTracker.clear(); callbackTracker.clear();
Completer<Null> completer = scrollTo(400.0); jumpTo(400.0);
expect(completer.isCompleted, isFalse);
// now only 3 should fit, numbered 2-4. // now only 3 should fit, numbered 2-4.
await tester.pumpWidget(testWidget); await tester.pumpWidget(buildWidget());
expect(callbackTracker, equals(<int>[1, 2, 3, 4]));
callbackTracker.clear();
await tester.pumpWidget(buildWidget());
expect(callbackTracker, equals(<int>[2, 3, 4])); expect(callbackTracker, equals(<int>[2, 3, 4]));
expect(completer.isCompleted, isTrue); callbackTracker.clear();
jumpTo(500.0);
// now 4 should fit, numbered 2-5.
await tester.pumpWidget(buildWidget());
expect(callbackTracker, equals(<int>[2, 3, 4, 5]));
callbackTracker.clear(); callbackTracker.clear();
}); });
testWidgets('HomogeneousViewport horizontal', (WidgetTester tester) async { testWidgets('ListView.builder horizontal', (WidgetTester tester) async {
List<int> callbackTracker = <int>[]; List<int> callbackTracker = <int>[];
// the root view is 800x600 in the test environment // the root view is 800x600 in the test environment
// so if our widget is 200 pixels wide, it should fit exactly 4 times. // so if our widget is 200 pixels wide, it should fit exactly 4 times.
// but if we are offset by 300 pixels, there will be 5, numbered 1-5. // but if we are offset by 300 pixels, there will be 5, numbered 1-5.
ItemListBuilder itemBuilder = (BuildContext context, int start, int count) { IndexedWidgetBuilder itemBuilder = (BuildContext context, int index) {
List<Widget> result = <Widget>[];
for (int index = start; index < start + count; index += 1) {
callbackTracker.add(index); callbackTracker.add(index);
result.add(new Container( return new Container(
key: new ValueKey<int>(index), key: new ValueKey<int>(index),
width: 400.0, // this should be overridden by itemExtent width: 400.0, // this should be overridden by itemExtent
height: 500.0, // this should be ignored height: 500.0, // this should be ignored
child: new Text("$index") child: new Text('$index'),
)); );
}
return result;
}; };
FlipWidget testWidget = new FlipWidget( FlipWidget buildWidget() {
left: new ScrollableLazyList( return new FlipWidget(
left: new ListView.builder(
controller: new ScrollController(initialScrollOffset: 300.0),
itemBuilder: itemBuilder, itemBuilder: itemBuilder,
itemExtent: 200.0, itemExtent: 200.0,
initialScrollOffset: 300.0,
scrollDirection: Axis.horizontal scrollDirection: Axis.horizontal
), ),
right: new Text('Not Today') right: new Text('Not Today')
); );
Completer<Null> scrollTo(double newScrollOffset) {
Completer<Null> completer = new Completer<Null>();
final ScrollableState scrollable = tester.state(find.byType(Scrollable));
scrollable.scrollTo(newScrollOffset).whenComplete(completer.complete);
return completer;
} }
await tester.pumpWidget(testWidget); void jumpTo(double newScrollOffset) {
final Scrollable2State scrollable = tester.state(find.byType(Scrollable2));
scrollable.position.jumpTo(newScrollOffset);
}
await tester.pumpWidget(buildWidget());
expect(callbackTracker, equals(<int>[1, 2, 3, 4, 5])); expect(callbackTracker, equals(<int>[1, 2, 3, 4, 5]));
callbackTracker.clear(); callbackTracker.clear();
Completer<Null> completer = scrollTo(400.0); jumpTo(400.0);
expect(completer.isCompleted, isFalse);
// now only 4 should fit, numbered 2-5. // now only 4 should fit, numbered 2-5.
await tester.pumpWidget(testWidget); await tester.pumpWidget(buildWidget());
expect(callbackTracker, equals(<int>[1, 2, 3, 4, 5]));
callbackTracker.clear();
await tester.pumpWidget(buildWidget());
expect(callbackTracker, equals(<int>[2, 3, 4, 5])); expect(callbackTracker, equals(<int>[2, 3, 4, 5]));
expect(completer.isCompleted, isTrue); callbackTracker.clear();
jumpTo(500.0);
// now only 5 should fit, numbered 2-6.
await tester.pumpWidget(buildWidget());
expect(callbackTracker, equals(<int>[2, 3, 4, 5, 6]));
callbackTracker.clear(); callbackTracker.clear();
}); });
testWidgets('ScrollableLazyList 10 items, 2-3 items visible', (WidgetTester tester) async { testWidgets('ListView.builder 10 items, 2-3 items visible', (WidgetTester tester) async {
List<int> callbackTracker = <int>[]; List<int> callbackTracker = <int>[];
// The root view is 800x600 in the test environment and our list // The root view is 800x600 in the test environment and our list
// items are 300 tall. Scrolling should cause two or three items // items are 300 tall. Scrolling should cause two or three items
// to be built. // to be built.
ItemListBuilder itemBuilder = (BuildContext context, int start, int count) { IndexedWidgetBuilder itemBuilder = (BuildContext context, int index) {
List<Widget> result = <Widget>[];
for (int index = start; index < start + count; index += 1) {
callbackTracker.add(index); callbackTracker.add(index);
result.add(new Text('$index', key: new ValueKey<int>(index))); return new Text('$index', key: new ValueKey<int>(index));
}
return result;
}; };
Widget testWidget = new ScrollableLazyList( Widget testWidget = new ListView.builder(
itemBuilder: itemBuilder, itemBuilder: itemBuilder,
itemExtent: 300.0, itemExtent: 300.0,
itemCount: 10 itemCount: 10,
); );
Completer<Null> scrollTo(double newScrollOffset) {
Completer<Null> completer = new Completer<Null>(); void jumpTo(double newScrollOffset) {
ScrollableState scrollable = tester.state(find.byType(Scrollable)); final Scrollable2State scrollable = tester.state(find.byType(Scrollable2));
scrollable.scrollTo(newScrollOffset).whenComplete(completer.complete); scrollable.position.jumpTo(newScrollOffset);
return completer;
} }
await tester.pumpWidget(testWidget); await tester.pumpWidget(testWidget);
expect(callbackTracker, equals(<int>[0, 1])); expect(callbackTracker, equals(<int>[0, 1]));
callbackTracker.clear(); callbackTracker.clear();
Completer<Null> completer = scrollTo(150.0); jumpTo(150.0);
expect(completer.isCompleted, isFalse); await tester.pump();
await tester.pumpWidget(testWidget);
expect(callbackTracker, equals(<int>[0, 1, 2])); expect(callbackTracker, equals(<int>[2]));
expect(completer.isCompleted, isTrue);
callbackTracker.clear(); callbackTracker.clear();
completer = scrollTo(600.0); jumpTo(600.0);
expect(completer.isCompleted, isFalse); await tester.pump();
await tester.pumpWidget(testWidget);
expect(callbackTracker, equals(<int>[2, 3])); expect(callbackTracker, equals(<int>[3]));
expect(completer.isCompleted, isTrue);
callbackTracker.clear(); callbackTracker.clear();
completer = scrollTo(750.0); jumpTo(750.0);
expect(completer.isCompleted, isFalse); await tester.pump();
await tester.pumpWidget(testWidget);
expect(callbackTracker, equals(<int>[2, 3, 4])); expect(callbackTracker, equals(<int>[4]));
expect(completer.isCompleted, isTrue);
callbackTracker.clear(); callbackTracker.clear();
}); });
......
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