From b4c3970319aa7d8c52dfef7c056b370154729175 Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Wed, 11 Nov 2015 08:49:50 -0800 Subject: [PATCH] Add a unit test for persistent bottom sheets --- .../lib/src/material/bottom_sheet.dart | 4 +- packages/unit/test/engine/mock_events.dart | 20 +++++---- .../unit/test/widget/bottom_sheet_test.dart | 41 ++++++++++++++++++- packages/unit/test/widget/widget_tester.dart | 21 ++++++++++ 4 files changed, 76 insertions(+), 10 deletions(-) diff --git a/packages/flutter/lib/src/material/bottom_sheet.dart b/packages/flutter/lib/src/material/bottom_sheet.dart index 135354202a..be59077bb5 100644 --- a/packages/flutter/lib/src/material/bottom_sheet.dart +++ b/packages/flutter/lib/src/material/bottom_sheet.dart @@ -216,5 +216,7 @@ Future showBottomSheet({ BuildContext context, GlobalKey place _BottomSheetRoute route = new _BottomSheetRoute(child: child, completer: completer); placeholderKey.currentState.child = new _PersistentBottomSheet(route: route); Navigator.of(context).pushEphemeral(route); - return completer.future; + return completer.future.then((_) { + placeholderKey.currentState.child = null; + }); } diff --git a/packages/unit/test/engine/mock_events.dart b/packages/unit/test/engine/mock_events.dart index ffb8befab9..7aa0e87c61 100644 --- a/packages/unit/test/engine/mock_events.dart +++ b/packages/unit/test/engine/mock_events.dart @@ -11,7 +11,7 @@ class TestPointer { bool isDown = false; ui.Point location; - PointerInputEvent down([ui.Point newLocation = ui.Point.origin ]) { + PointerInputEvent down([ui.Point newLocation = ui.Point.origin, double timeStamp = 0.0 ]) { assert(!isDown); isDown = true; location = newLocation; @@ -19,11 +19,12 @@ class TestPointer { type: 'pointerdown', pointer: pointer, x: location.x, - y: location.y + y: location.y, + timeStamp: timeStamp ); } - PointerInputEvent move([ui.Point newLocation = ui.Point.origin ]) { + PointerInputEvent move([ui.Point newLocation = ui.Point.origin, double timeStamp = 0.0]) { assert(isDown); ui.Offset delta = newLocation - location; location = newLocation; @@ -33,29 +34,32 @@ class TestPointer { x: newLocation.x, y: newLocation.y, dx: delta.dx, - dy: delta.dy + dy: delta.dy, + timeStamp: timeStamp ); } - PointerInputEvent up() { + PointerInputEvent up([ double timeStamp = 0.0 ]) { assert(isDown); isDown = false; return new PointerInputEvent( type: 'pointerup', pointer: pointer, x: location.x, - y: location.y + y: location.y, + timeStamp: timeStamp ); } - PointerInputEvent cancel() { + PointerInputEvent cancel([ double timeStamp = 0.0 ]) { assert(isDown); isDown = false; return new PointerInputEvent( type: 'pointercancel', pointer: pointer, x: location.x, - y: location.y + y: location.y, + timeStamp: timeStamp ); } diff --git a/packages/unit/test/widget/bottom_sheet_test.dart b/packages/unit/test/widget/bottom_sheet_test.dart index aedeb20b0b..3f277ff1a6 100644 --- a/packages/unit/test/widget/bottom_sheet_test.dart +++ b/packages/unit/test/widget/bottom_sheet_test.dart @@ -5,7 +5,7 @@ import 'package:test/test.dart'; import 'widget_tester.dart'; void main() { - test('Verify that a tap dismisses the BottomSheet', () { + test('Verify that a tap dismisses a modal BottomSheet', () { testWidgets((WidgetTester tester) { BuildContext context; tester.pumpWidget(new MaterialApp( @@ -46,4 +46,43 @@ void main() { }); }); + test('Verify that a downwards fling dismisses a persistent BottomSheet', () { + testWidgets((WidgetTester tester) { + GlobalKey _bottomSheetPlaceholderKey = new GlobalKey(); + BuildContext context; + tester.pumpWidget(new MaterialApp( + routes: { + '/': (RouteArguments args) { + context = args.context; + return new Scaffold( + bottomSheet: new Placeholder(key: _bottomSheetPlaceholderKey), + body: new Center(child: new Text('body')) + ); + } + } + )); + + tester.pump(); + expect(tester.findText('BottomSheet'), isNull); + + showBottomSheet( + context: context, + child: new Container(child: new Text('BottomSheet'), margin: new EdgeDims.all(40.0)), + placeholderKey: _bottomSheetPlaceholderKey + ); + + expect(_bottomSheetPlaceholderKey.currentState.child, isNotNull); + tester.pump(); // bottom sheet show animation starts + tester.pump(new Duration(seconds: 1)); // animation done + expect(tester.findText('BottomSheet'), isNotNull); + + tester.fling(tester.findText('BottomSheet'), const Offset(0.0, 20.0), 1000.0); + tester.pump(); // bottom sheet dismiss animation starts + tester.pump(new Duration(seconds: 1)); // animation done + tester.pump(new Duration(seconds: 2)); // rebuild frame without the bottom sheet + expect(tester.findText('BottomSheet'), isNull); + expect(_bottomSheetPlaceholderKey.currentState.child, isNull); + }); + }); + } diff --git a/packages/unit/test/widget/widget_tester.dart b/packages/unit/test/widget/widget_tester.dart index ac0a965460..6fff24d70a 100644 --- a/packages/unit/test/widget/widget_tester.dart +++ b/packages/unit/test/widget/widget_tester.dart @@ -144,6 +144,27 @@ class WidgetTester { _dispatchEvent(p.up(), result); } + void fling(Element element, Offset offset, velocity, { int pointer: 1 }) { + flingFrom(getCenter(element), offset, velocity, pointer: pointer); + } + + void flingFrom(Point startLocation, Offset offset, double velocity, { int pointer: 1 }) { + assert(offset.distance > 0.0); + assert(velocity != 0.0); // velocity is pixels/second + final TestPointer p = new TestPointer(pointer); + final HitTestResult result = _hitTest(startLocation); + final kMoveCount = 50; // Needs to be >= kHistorySize, see _LeastSquaresVelocityTrackerStrategy + final double timeStampDelta = 1000.0 * offset.distance / (kMoveCount * velocity); + double timeStamp = 0.0; + _dispatchEvent(p.down(startLocation, timeStamp), result); + for(int i = 0; i < kMoveCount; i++) { + final Point location = startLocation + Offset.lerp(Offset.zero, offset, i / kMoveCount); + _dispatchEvent(p.move(location, timeStamp), result); + timeStamp += timeStampDelta; + } + _dispatchEvent(p.up(timeStamp), result); + } + void scroll(Element element, Offset offset, { int pointer: 1 }) { scrollAt(getCenter(element), offset, pointer: pointer); }