From 99a9bbbe7c454fd629d9477ac1c69c6e816f7f31 Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Thu, 8 Sep 2016 12:14:24 -0700 Subject: [PATCH] Add a test for 5630 (#5768) This issue got fixed by a recent check-in, but I had a test specifically for this incarnation so we should probably keep it just in case. Closes https://github.com/flutter/flutter/issues/5630 --- .../layout_builder_and_parent_data_test.dart | 3 +- .../widget/remember_scroll_position_test.dart | 3 +- .../test/widget/scrollable_dispose_test.dart | 3 +- .../state_setting_in_scrollables_test.dart | 97 +++++++++++++++++++ .../flutter/test/widget/test_widgets.dart | 3 +- packages/flutter_test/lib/src/finders.dart | 36 +++++++ 6 files changed, 137 insertions(+), 8 deletions(-) create mode 100644 packages/flutter/test/widget/state_setting_in_scrollables_test.dart diff --git a/packages/flutter/test/widget/layout_builder_and_parent_data_test.dart b/packages/flutter/test/widget/layout_builder_and_parent_data_test.dart index 2fabba5909..fce354f5ad 100644 --- a/packages/flutter/test/widget/layout_builder_and_parent_data_test.dart +++ b/packages/flutter/test/widget/layout_builder_and_parent_data_test.dart @@ -66,8 +66,7 @@ void main() { }) )); frame += 1; - SizeChangerState state = tester.state(find.byType(SizeChanger)); - state.trigger(); + tester.state/**/(find.byType(SizeChanger)).trigger(); await tester.pump(); }); } diff --git a/packages/flutter/test/widget/remember_scroll_position_test.dart b/packages/flutter/test/widget/remember_scroll_position_test.dart index b63d6b38e0..73bcb53647 100644 --- a/packages/flutter/test/widget/remember_scroll_position_test.dart +++ b/packages/flutter/test/widget/remember_scroll_position_test.dart @@ -57,8 +57,7 @@ Future performTest(WidgetTester tester, bool maintainState) async { expect(find.text('10'), findsNothing); expect(find.text('100'), findsNothing); - ScrollableState targetState = tester.state(find.byType(Scrollable)); - targetState.scrollTo(1000.0); + tester.state/**/(find.byType(Scrollable)).scrollTo(1000.0); await tester.pump(new Duration(seconds: 1)); // we're 600 pixels high, each item is 100 pixels high, scroll position is diff --git a/packages/flutter/test/widget/scrollable_dispose_test.dart b/packages/flutter/test/widget/scrollable_dispose_test.dart index 4148ea2f0a..a9d457d470 100644 --- a/packages/flutter/test/widget/scrollable_dispose_test.dart +++ b/packages/flutter/test/widget/scrollable_dispose_test.dart @@ -20,8 +20,7 @@ void main() { await tester.fling(find.byType(Scrollable), new Offset(0.0, -200.0), 1000.0); await tester.pump(); - FlipWidgetState flipWidget = tester.state(find.byType(FlipWidget)); - flipWidget.flip(); + tester.state/**/(find.byType(FlipWidget)).flip(); await tester.pump(new Duration(hours: 5)); }); } diff --git a/packages/flutter/test/widget/state_setting_in_scrollables_test.dart b/packages/flutter/test/widget/state_setting_in_scrollables_test.dart new file mode 100644 index 0000000000..65c17c0b45 --- /dev/null +++ b/packages/flutter/test/widget/state_setting_in_scrollables_test.dart @@ -0,0 +1,97 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter_test/flutter_test.dart' hide TypeMatcher; +import 'package:flutter/widgets.dart'; + +class Foo extends StatefulWidget { + @override + FooState createState() => new FooState(); +} + +class FooState extends State { + final GlobalKey blockKey = new GlobalKey(); + GlobalKey scrollableKey = new GlobalKey(); + + @override + Widget build(BuildContext context) { + return new LayoutBuilder( + builder: (BuildContext context, BoxConstraints constraints) { + return new ScrollConfiguration( + delegate: new FooScrollConfiguration(), + child: new Block( + scrollableKey: scrollableKey, + children: [ + new GestureDetector( + onTap: () { + setState(() {}); + scrollableKey.currentState.scrollBy(200.0, duration: const Duration(milliseconds: 500)); + }, + child: new DecoratedBox( + decoration: new BoxDecoration(backgroundColor: const Color(0)), + child: new SizedBox( + height: 200.0, + ), + ) + ), + new DecoratedBox( + decoration: new BoxDecoration(backgroundColor: const Color(0)), + child: new SizedBox( + height: 200.0, + ), + ), + new DecoratedBox( + decoration: new BoxDecoration(backgroundColor: const Color(0)), + child: new SizedBox( + height: 200.0, + ), + ), + new DecoratedBox( + decoration: new BoxDecoration(backgroundColor: const Color(0)), + child: new SizedBox( + height: 200.0, + ), + ), + new DecoratedBox( + decoration: new BoxDecoration(backgroundColor: const Color(0)), + child: new SizedBox( + height: 200.0, + ), + ), + new DecoratedBox( + decoration: new BoxDecoration(backgroundColor: const Color(0)), + child: new SizedBox( + height: 200.0, + ), + ), + ], + ) + ); + } + ); + } +} + +class FooScrollConfiguration extends ScrollConfigurationDelegate { + @override + TargetPlatform get platform => defaultTargetPlatform; + + @override + ExtentScrollBehavior createScrollBehavior() => + new OverscrollWhenScrollableBehavior(platform: platform); + + @override + bool updateShouldNotify(FooScrollConfiguration old) => true; +} + +void main() { + testWidgets('https://github.com/flutter/flutter/issues/5630', (WidgetTester tester) async { + await tester.pumpWidget(new Foo()); + expect(tester.state/**/(find.byType(Scrollable)).scrollOffset, 0.0); + await tester.tap(find.byType(GestureDetector).first); + await tester.pump(); + await tester.pump(const Duration(seconds: 1)); + expect(tester.state/**/(find.byType(Scrollable)).scrollOffset, 200.0); + }); +} diff --git a/packages/flutter/test/widget/test_widgets.dart b/packages/flutter/test/widget/test_widgets.dart index cbcc5bb37a..91ba96b22f 100644 --- a/packages/flutter/test/widget/test_widgets.dart +++ b/packages/flutter/test/widget/test_widgets.dart @@ -54,6 +54,5 @@ class FlipWidgetState extends State { } void flipStatefulWidget(WidgetTester tester) { - FlipWidgetState state = tester.state(find.byType(FlipWidget)); - state.flip(); + tester.state/**/(find.byType(FlipWidget)).flip(); } diff --git a/packages/flutter_test/lib/src/finders.dart b/packages/flutter_test/lib/src/finders.dart index 590342e38a..aa80002020 100644 --- a/packages/flutter_test/lib/src/finders.dart +++ b/packages/flutter_test/lib/src/finders.dart @@ -205,6 +205,14 @@ abstract class Finder { return false; } + /// Returns a variant of this finder that only matches the first element + /// matched by this finder. + Finder get first => new _FirstFinder(this); + + /// Returns a variant of this finder that only matches the last element + /// matched by this finder. + Finder get last => new _LastFinder(this); + @override String toString() { final String additional = skipOffstage ? ' (ignoring offstage widgets)' : ''; @@ -220,6 +228,34 @@ abstract class Finder { } } +class _FirstFinder extends Finder { + _FirstFinder(this.parent); + + final Finder parent; + + @override + String get description => '${parent.description} (ignoring all but first)'; + + @override + Iterable apply(Iterable candidates) sync* { + yield parent.apply(candidates).first; + } +} + +class _LastFinder extends Finder { + _LastFinder(this.parent); + + final Finder parent; + + @override + String get description => '${parent.description} (ignoring all but last)'; + + @override + Iterable apply(Iterable candidates) sync* { + yield parent.apply(candidates).last; + } +} + /// Searches a widget tree and returns nodes that match a particular /// pattern. abstract class MatchFinder extends Finder {