From 8f708bfbc109a50d2836ccea6b0628ed771f049b Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Wed, 11 Jan 2017 18:38:03 -0800 Subject: [PATCH] Test AboutDrawerItem (#7453) Also, fix a few minor bugs found by the test. --- .../flutter/lib/src/foundation/licenses.dart | 2 +- packages/flutter/lib/src/material/about.dart | 2 + packages/flutter/lib/src/material/drawer.dart | 5 +- .../flutter/test/material/about_test.dart | 97 +++++++++++++++++++ packages/flutter_test/lib/src/binding.dart | 3 +- .../flutter_test/lib/src/widget_tester.dart | 9 +- 6 files changed, 106 insertions(+), 12 deletions(-) create mode 100644 packages/flutter/test/material/about_test.dart diff --git a/packages/flutter/lib/src/foundation/licenses.dart b/packages/flutter/lib/src/foundation/licenses.dart index 357a143b2c..64ba5b83b5 100644 --- a/packages/flutter/lib/src/foundation/licenses.dart +++ b/packages/flutter/lib/src/foundation/licenses.dart @@ -286,4 +286,4 @@ class LicenseRegistry { for (LicenseEntryCollector collector in _collectors) yield* collector(); } -} \ No newline at end of file +} diff --git a/packages/flutter/lib/src/material/about.dart b/packages/flutter/lib/src/material/about.dart index 2f72f82e9d..120034235e 100644 --- a/packages/flutter/lib/src/material/about.dart +++ b/packages/flutter/lib/src/material/about.dart @@ -379,6 +379,8 @@ class _LicensePageState extends State { Future _initLicenses() async { await for (LicenseEntry license in LicenseRegistry.licenses) { + if (!mounted) + return; setState(() { _licenses.add(new Padding( padding: const EdgeInsets.symmetric(vertical: 18.0), diff --git a/packages/flutter/lib/src/material/drawer.dart b/packages/flutter/lib/src/material/drawer.dart index 6968c596a0..4ca3d4cabb 100644 --- a/packages/flutter/lib/src/material/drawer.dart +++ b/packages/flutter/lib/src/material/drawer.dart @@ -133,10 +133,7 @@ class DrawerControllerState extends State with SingleTickerPro @override void dispose() { _historyEntry?.remove(); - _controller - ..removeListener(_animationChanged) - ..removeStatusListener(_animationStatusChanged) - ..stop(); + _controller.dispose(); super.dispose(); } diff --git a/packages/flutter/test/material/about_test.dart b/packages/flutter/test/material/about_test.dart new file mode 100644 index 0000000000..b8f2ba0616 --- /dev/null +++ b/packages/flutter/test/material/about_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 'dart:async'; + +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('AboutDrawerItem control test', (WidgetTester tester) async { + await tester.pumpWidget( + new MaterialApp( + title: 'Pirate app', + home: new Scaffold( + appBar: new AppBar( + title: new Text('Home'), + ), + drawer: new Drawer( + child: new Block( + children: [ + new AboutDrawerItem( + applicationVersion: '0.1.2', + applicationIcon: new FlutterLogo(), + applicationLegalese: 'I am the very model of a modern major general.', + aboutBoxChildren: [ + new Text('About box'), + ] + ), + ], + ), + ), + ), + ), + ); + + expect(find.text('About Pirate app'), findsNothing); + expect(find.text('0.1.2'), findsNothing); + expect(find.text('About box'), findsNothing); + + await tester.tap(find.byType(IconButton)); + await tester.pumpUntilNoTransientCallbacks(const Duration(milliseconds: 100)); + + expect(find.text('About Pirate app'), findsOneWidget); + expect(find.text('0.1.2'), findsNothing); + expect(find.text('About box'), findsNothing); + + await tester.tap(find.text('About Pirate app')); + await tester.pumpUntilNoTransientCallbacks(const Duration(milliseconds: 100)); + + expect(find.text('About Pirate app'), findsOneWidget); + expect(find.text('0.1.2'), findsOneWidget); + expect(find.text('About box'), findsOneWidget); + + LicenseRegistry.addLicense(() { + return new Stream.fromIterable([ + new LicenseEntryWithLineBreaks([ 'Pirate package '], 'Pirate license') + ]); + }); + + await tester.tap(find.text('VIEW LICENSES')); + await tester.pumpUntilNoTransientCallbacks(const Duration(milliseconds: 100)); + + expect(find.text('Pirate license'), findsOneWidget); + }); + + testWidgets('AboutDrawerItem control test', (WidgetTester tester) async { + List log = []; + + Future licenseFuture; + LicenseRegistry.addLicense(() { + log.add('license1'); + licenseFuture = tester.pumpWidget(new Container()); + return new Stream.fromIterable([]); + }); + + LicenseRegistry.addLicense(() { + log.add('license2'); + return new Stream.fromIterable([ + new LicenseEntryWithLineBreaks([ 'Another package '], 'Another license') + ]); + }); + + await tester.pumpWidget(new Center( + child: new LicensePage() + )); + + expect(licenseFuture, isNotNull); + await licenseFuture; + + // We should not hit an exception here. + await tester.idle(); + + expect(log, equals(['license1', 'license2'])); + }); +} diff --git a/packages/flutter_test/lib/src/binding.dart b/packages/flutter_test/lib/src/binding.dart index 2a880c897e..fedcd4f5ba 100644 --- a/packages/flutter_test/lib/src/binding.dart +++ b/packages/flutter_test/lib/src/binding.dart @@ -11,7 +11,6 @@ import 'package:flutter/gestures.dart'; import 'package:flutter/http.dart' as http; import 'package:flutter/rendering.dart'; import 'package:flutter/scheduler.dart'; -import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; import 'package:meta/meta.dart'; import 'package:quiver/testing/async.dart'; @@ -84,8 +83,8 @@ const Size _kDefaultTestViewportSize = const Size(800.0, 600.0); abstract class TestWidgetsFlutterBinding extends BindingBase with SchedulerBinding, GestureBinding, - ServicesBinding, RendererBinding, + // Services binding omitted to avoid dragging in the licenses code. WidgetsBinding { /// Creates and initializes the binding. This function is /// idempotent; calling it a second time will just return the diff --git a/packages/flutter_test/lib/src/widget_tester.dart b/packages/flutter_test/lib/src/widget_tester.dart index 5d528b9e84..52f355018c 100644 --- a/packages/flutter_test/lib/src/widget_tester.dart +++ b/packages/flutter_test/lib/src/widget_tester.dart @@ -8,7 +8,6 @@ import 'package:flutter/gestures.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/scheduler.dart'; import 'package:flutter/widgets.dart'; -import 'package:meta/meta.dart'; import 'package:test/test.dart' as test_package; import 'all_elements.dart'; @@ -213,10 +212,10 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker /// /// Alternatively, one can check that the return value from this function /// matches the expected number of pumps. - Future pumpUntilNoTransientCallbacks([ - @required Duration duration, - EnginePhase phase = EnginePhase.sendSemanticsTree - ]) { + Future pumpUntilNoTransientCallbacks( + Duration duration, [ + EnginePhase phase = EnginePhase.sendSemanticsTree + ]) { assert(duration != null); assert(duration > Duration.ZERO); int count = 0;