diff --git a/examples/flutter_gallery/test/calculator/smoke_test.dart b/examples/flutter_gallery/test/calculator/smoke_test.dart index 85458e1413..0195c7c9ac 100644 --- a/examples/flutter_gallery/test/calculator/smoke_test.dart +++ b/examples/flutter_gallery/test/calculator/smoke_test.dart @@ -14,7 +14,7 @@ void main() { // We press the "1" and the "2" buttons and check that the display // reads "12". testWidgets('Flutter calculator app smoke test', (WidgetTester tester) async { - await tester.pumpWidget(const CalculatorDemo()); + await tester.pumpWidget(new MaterialApp(home: const CalculatorDemo())); final Finder oneButton = find.widgetWithText(InkResponse, '1'); expect(oneButton, findsOneWidget); diff --git a/packages/flutter/lib/src/material/app_bar.dart b/packages/flutter/lib/src/material/app_bar.dart index b8b6d2b8f1..73274c8a99 100644 --- a/packages/flutter/lib/src/material/app_bar.dart +++ b/packages/flutter/lib/src/material/app_bar.dart @@ -341,6 +341,7 @@ class _AppBarState extends State { @override Widget build(BuildContext context) { + assert(!widget.primary || debugCheckHasMediaQuery(context)); final ThemeData themeData = Theme.of(context); IconThemeData appBarIconTheme = widget.iconTheme ?? themeData.primaryIconTheme; @@ -906,6 +907,7 @@ class _SliverAppBarState extends State with TickerProviderStateMix @override Widget build(BuildContext context) { + assert(!widget.primary || debugCheckHasMediaQuery(context)); final double topPadding = widget.primary ? MediaQuery.of(context).padding.top : 0.0; final double collapsedHeight = (widget.pinned && widget.floating && widget.bottom != null) ? widget.bottom.preferredSize.height + topPadding : null; diff --git a/packages/flutter/lib/src/material/drawer_header.dart b/packages/flutter/lib/src/material/drawer_header.dart index ce36fe0c71..b94f68eafa 100644 --- a/packages/flutter/lib/src/material/drawer_header.dart +++ b/packages/flutter/lib/src/material/drawer_header.dart @@ -72,6 +72,7 @@ class DrawerHeader extends StatelessWidget { @override Widget build(BuildContext context) { assert(debugCheckHasMaterial(context)); + assert(debugCheckHasMediaQuery(context)); final ThemeData theme = Theme.of(context); final double statusBarHeight = MediaQuery.of(context).padding.top; return new Container( diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index 0b0fd9c9ae..8e1c41b6eb 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -769,6 +769,7 @@ class ScaffoldState extends State with TickerProviderStateMixin { @override Widget build(BuildContext context) { + assert(debugCheckHasMediaQuery(context)); EdgeInsets padding = MediaQuery.of(context).padding; final ThemeData themeData = Theme.of(context); if (!widget.resizeToAvoidBottomPadding) diff --git a/packages/flutter/lib/src/material/text_selection.dart b/packages/flutter/lib/src/material/text_selection.dart index bed81d5bf5..3fa615dd07 100644 --- a/packages/flutter/lib/src/material/text_selection.dart +++ b/packages/flutter/lib/src/material/text_selection.dart @@ -151,6 +151,7 @@ class _MaterialTextSelectionControls extends TextSelectionControls { @override Widget buildToolbar( BuildContext context, Offset position, TextSelectionDelegate delegate) { + assert(debugCheckHasMediaQuery(context)); final Size screenSize = MediaQuery.of(context).size; return new ConstrainedBox( constraints: new BoxConstraints.loose(screenSize), diff --git a/packages/flutter/lib/src/widgets/debug.dart b/packages/flutter/lib/src/widgets/debug.dart index 63d3a55301..7d47dba202 100644 --- a/packages/flutter/lib/src/widgets/debug.dart +++ b/packages/flutter/lib/src/widgets/debug.dart @@ -5,7 +5,9 @@ import 'dart:collection'; import 'package:flutter/foundation.dart'; + import 'framework.dart'; +import 'media_query.dart'; import 'table.dart'; // Any changes to this file should be reflected in the debugAssertAllWidgetVarsUnset() @@ -153,6 +155,39 @@ bool debugCheckHasTable(BuildContext context) { return true; } +/// Asserts that the given context has a [MediaQuery] ancestor. +/// +/// Used by various widgets to make sure that they are only used in an +/// appropriate context. +/// +/// To invoke this function, use the following pattern, typically in the +/// relevant Widget's [build] method: +/// +/// ```dart +/// assert(debugCheckHasMediaQuery(context)); +/// ``` +/// +/// Does nothing if asserts are disabled. Always returns true. +bool debugCheckHasMediaQuery(BuildContext context) { + assert(() { + if (context.widget is! MediaQuery && context.ancestorWidgetOfExactType(MediaQuery) == null) { + final Element element = context; + throw new FlutterError( + 'No MediaQuery widget found.\n' + '${context.widget.runtimeType} widgets require a MediaQuery widget ancestor.\n' + 'The specific widget that could not find a MediaQuery ancestor was:\n' + ' ${context.widget}\n' + 'The ownership chain for the affected widget is:\n' + ' ${element.debugGetCreatorChain(10)}\n' + 'Typically, the MediaQuery widget is introduced by the MaterialApp or ' + 'WidgetsApp widget at the top of your application widget tree.' + ); + } + return true; + }); + return true; +} + /// Asserts that the `built` widget is not null. /// /// Used when the given `widget` calls a builder function to check that the diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index d79bc7aa00..50387d1590 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -463,7 +463,7 @@ class EditableTextState extends State implements TextInputClient { showCursor: _showCursor, maxLines: widget.maxLines, selectionColor: widget.selectionColor, - textScaleFactor: widget.textScaleFactor ?? MediaQuery.of(context).textScaleFactor, + textScaleFactor: widget.textScaleFactor ?? MediaQuery.of(context, nullOk: true)?.textScaleFactor ?? 1.0, textAlign: widget.textAlign, obscureText: widget.obscureText, offset: offset, diff --git a/packages/flutter/lib/src/widgets/image.dart b/packages/flutter/lib/src/widgets/image.dart index b0a0b136d3..aa258609f4 100644 --- a/packages/flutter/lib/src/widgets/image.dart +++ b/packages/flutter/lib/src/widgets/image.dart @@ -27,10 +27,10 @@ export 'package:flutter/services.dart' show ImageConfiguration createLocalImageConfiguration(BuildContext context, { Size size }) { return new ImageConfiguration( bundle: DefaultAssetBundle.of(context), - devicePixelRatio: MediaQuery.of(context).devicePixelRatio, + devicePixelRatio: MediaQuery.of(context, nullOk: true)?.devicePixelRatio ?? 1.0, // TODO(ianh): provide the locale size: size, - platform: Platform.operatingSystem + platform: Platform.operatingSystem, ); } diff --git a/packages/flutter/lib/src/widgets/media_query.dart b/packages/flutter/lib/src/widgets/media_query.dart index 2d20dfaf12..5977c48237 100644 --- a/packages/flutter/lib/src/widgets/media_query.dart +++ b/packages/flutter/lib/src/widgets/media_query.dart @@ -26,6 +26,10 @@ enum Orientation { /// To obtain the current [MediaQueryData] for a given [BuildContext], use the /// [MediaQuery.of] function. For example, to obtain the size of the current /// window, use `MediaQuery.of(context).size`. +/// +/// If no [MediaQuery] is in scope then the [MediaQuery.of] method will throw an +/// exception, unless the `nullOk` argument is set to true, in which case it +/// returns null. @immutable class MediaQueryData { /// Creates data for a media query with explicit values. @@ -40,6 +44,11 @@ class MediaQueryData { }); /// Creates data for a media query based on the given window. + /// + /// If you use this, you should ensure that you also register for + /// notifications so that you can update your [MediaQueryData] when the + /// window's metrics change. For example, see + /// [WidgetsBindingObserver.didChangeMetrics] or [ui.window.onMetricsChanged]. MediaQueryData.fromWindow(ui.Window window) : size = window.physicalSize / window.devicePixelRatio, devicePixelRatio = window.devicePixelRatio, @@ -117,6 +126,16 @@ class MediaQueryData { /// Querying the current media using [MediaQuery.of] will cause your widget to /// rebuild automatically whenever the [MediaQueryData] changes (e.g., if the /// user rotates their device). +/// +/// If no [MediaQuery] is in scope then the [MediaQuery.of] method will throw an +/// exception, unless the `nullOk` argument is set to true, in which case it +/// returns null. +/// +/// See also: +/// +/// * [WidgetsApp] and [MaterialApp], which introduce a [MediaQuery] and keep +/// it up to date with the current screen metrics as they change. +/// * [MediaQueryData], the data structure that represents the metrics. class MediaQuery extends InheritedWidget { /// Creates a widget that provides [MediaQueryData] to its descendants. /// @@ -135,7 +154,8 @@ class MediaQuery extends InheritedWidget { /// height of the current window. final MediaQueryData data; - /// The data from the closest instance of this class that encloses the given context. + /// The data from the closest instance of this class that encloses the given + /// context. /// /// You can use this function to query the size an orientation of the screen. /// When that information changes, your widget will be scheduled to be rebuilt, @@ -146,9 +166,27 @@ class MediaQuery extends InheritedWidget { /// ```dart /// MediaQueryData media = MediaQuery.of(context); /// ``` - static MediaQueryData of(BuildContext context) { + /// + /// If there is no [MediaQuery] in scope, then this will throw an exception. + /// To return null if there is no [MediaQuery], then pass `nullOk: true`. + /// + /// If you use this from a widget (e.g. in its build function), consider + /// calling [debugCheckHasMediaQuery]. + static MediaQueryData of(BuildContext context, { bool nullOk: false }) { final MediaQuery query = context.inheritFromWidgetOfExactType(MediaQuery); - return query?.data ?? new MediaQueryData.fromWindow(ui.window); + if (query != null) + return query.data; + if (nullOk) + return null; + throw new FlutterError( + 'MediaQuery.of() called with a context that does not contain a MediaQuery.\n' + 'No MediaQuery ancestor could be found starting from the context that was passed ' + 'to MediaQuery.of(). This can happen because you do not have a WidgetsApp or ' + 'MaterialApp widget (those widgets introduce a MediaQuery), or it can happen ' + 'if the context you use comes from a widget above those widgets.\n' + 'The context used was:\n' + ' $context' + ); } @override diff --git a/packages/flutter/lib/src/widgets/text.dart b/packages/flutter/lib/src/widgets/text.dart index c0dbc0ac97..8b3f097d75 100644 --- a/packages/flutter/lib/src/widgets/text.dart +++ b/packages/flutter/lib/src/widgets/text.dart @@ -203,7 +203,7 @@ class Text extends StatelessWidget { textAlign: textAlign ?? defaultTextStyle.textAlign, softWrap: softWrap ?? defaultTextStyle.softWrap, overflow: overflow ?? defaultTextStyle.overflow, - textScaleFactor: textScaleFactor ?? MediaQuery.of(context).textScaleFactor, + textScaleFactor: textScaleFactor ?? MediaQuery.of(context, nullOk: true)?.textScaleFactor ?? 1.0, maxLines: maxLines ?? defaultTextStyle.maxLines, text: new TextSpan( style: effectiveTextStyle, diff --git a/packages/flutter/test/material/about_test.dart b/packages/flutter/test/material/about_test.dart index 940a1ef6ac..f2b9fe0529 100644 --- a/packages/flutter/test/material/about_test.dart +++ b/packages/flutter/test/material/about_test.dart @@ -89,9 +89,13 @@ void main() { ]); }); - await tester.pumpWidget(const Center( - child: const LicensePage() - )); + await tester.pumpWidget( + new MaterialApp( + home: const Center( + child: const LicensePage() + ), + ), + ); expect(licenseFuture, isNotNull); await licenseFuture; diff --git a/packages/flutter/test/material/app_bar_test.dart b/packages/flutter/test/material/app_bar_test.dart index e8e7aba6a6..e95cd037a6 100644 --- a/packages/flutter/test/material/app_bar_test.dart +++ b/packages/flutter/test/material/app_bar_test.dart @@ -8,29 +8,32 @@ import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; Widget buildSliverAppBarApp({ bool floating, bool pinned, double expandedHeight, bool snap: false }) { - return new Scaffold( - body: new DefaultTabController( - length: 3, - child: new CustomScrollView( - primary: true, - slivers: [ - new SliverAppBar( - title: const Text('AppBar Title'), - floating: floating, - pinned: pinned, - expandedHeight: expandedHeight, - snap: snap, - bottom: new TabBar( - tabs: ['A','B','C'].map((String t) => new Tab(text: 'TAB $t')).toList(), + return new MediaQuery( + data: const MediaQueryData(), + child: new Scaffold( + body: new DefaultTabController( + length: 3, + child: new CustomScrollView( + primary: true, + slivers: [ + new SliverAppBar( + title: const Text('AppBar Title'), + floating: floating, + pinned: pinned, + expandedHeight: expandedHeight, + snap: snap, + bottom: new TabBar( + tabs: ['A','B','C'].map((String t) => new Tab(text: 'TAB $t')).toList(), + ), ), - ), - new SliverToBoxAdapter( - child: new Container( - height: 1200.0, - color: Colors.orange[400], + new SliverToBoxAdapter( + child: new Container( + height: 1200.0, + color: Colors.orange[400], + ), ), - ), - ], + ], + ), ), ), ); @@ -246,12 +249,14 @@ void main() { testWidgets('AppBar with no Scaffold', (WidgetTester tester) async { await tester.pumpWidget( - new SizedBox( - height: kToolbarHeight, - child: new AppBar( - leading: const Text('L'), - title: const Text('No Scaffold'), - actions: [const Text('A1'), const Text('A2')], + new MaterialApp( + home: new SizedBox( + height: kToolbarHeight, + child: new AppBar( + leading: const Text('L'), + title: const Text('No Scaffold'), + actions: [const Text('A1'), const Text('A2')], + ), ), ), ); @@ -265,13 +270,15 @@ void main() { testWidgets('AppBar render at zero size', (WidgetTester tester) async { await tester.pumpWidget( - new Center( - child: new Container( - height: 0.0, - width: 0.0, - child: new Scaffold( - appBar: new AppBar( - title: const Text('X'), + new MaterialApp( + home: new Center( + child: new Container( + height: 0.0, + width: 0.0, + child: new Scaffold( + appBar: new AppBar( + title: const Text('X'), + ), ), ), ), diff --git a/packages/flutter/test/material/bottom_navigation_bar_test.dart b/packages/flutter/test/material/bottom_navigation_bar_test.dart index 283cb974e1..0feb895525 100644 --- a/packages/flutter/test/material/bottom_navigation_bar_test.dart +++ b/packages/flutter/test/material/bottom_navigation_bar_test.dart @@ -10,21 +10,23 @@ void main() { int mutatedIndex; await tester.pumpWidget( - new Scaffold( - bottomNavigationBar: new BottomNavigationBar( - items: [ - new BottomNavigationBarItem( - icon: const Icon(Icons.ac_unit), - title: const Text('AC') - ), - new BottomNavigationBarItem( - icon: const Icon(Icons.access_alarm), - title: const Text('Alarm') - ) - ], - onTap: (int index) { - mutatedIndex = index; - } + new MaterialApp( + home: new Scaffold( + bottomNavigationBar: new BottomNavigationBar( + items: [ + new BottomNavigationBarItem( + icon: const Icon(Icons.ac_unit), + title: const Text('AC') + ), + new BottomNavigationBarItem( + icon: const Icon(Icons.access_alarm), + title: const Text('Alarm') + ) + ], + onTap: (int index) { + mutatedIndex = index; + } + ) ) ) ); @@ -36,18 +38,20 @@ void main() { testWidgets('BottomNavigationBar content test', (WidgetTester tester) async { await tester.pumpWidget( - new Scaffold( - bottomNavigationBar: new BottomNavigationBar( - items: [ - new BottomNavigationBarItem( - icon: const Icon(Icons.ac_unit), - title: const Text('AC') - ), - new BottomNavigationBarItem( - icon: const Icon(Icons.access_alarm), - title: const Text('Alarm') - ) - ] + new MaterialApp( + home: new Scaffold( + bottomNavigationBar: new BottomNavigationBar( + items: [ + new BottomNavigationBarItem( + icon: const Icon(Icons.ac_unit), + title: const Text('AC') + ), + new BottomNavigationBarItem( + icon: const Icon(Icons.access_alarm), + title: const Text('Alarm') + ) + ] + ) ) ) ); @@ -60,19 +64,21 @@ void main() { testWidgets('BottomNavigationBar action size test', (WidgetTester tester) async { await tester.pumpWidget( - new Scaffold( - bottomNavigationBar: new BottomNavigationBar( - type: BottomNavigationBarType.shifting, - items: [ - new BottomNavigationBarItem( - icon: const Icon(Icons.ac_unit), - title: const Text('AC') - ), - new BottomNavigationBarItem( - icon: const Icon(Icons.access_alarm), - title: const Text('Alarm') - ) - ] + new MaterialApp( + home: new Scaffold( + bottomNavigationBar: new BottomNavigationBar( + type: BottomNavigationBarType.shifting, + items: [ + new BottomNavigationBarItem( + icon: const Icon(Icons.ac_unit), + title: const Text('AC') + ), + new BottomNavigationBarItem( + icon: const Icon(Icons.access_alarm), + title: const Text('Alarm') + ) + ] + ) ) ) ); @@ -83,20 +89,22 @@ void main() { expect(actions.elementAt(1).size.width, 105.6); await tester.pumpWidget( - new Scaffold( - bottomNavigationBar: new BottomNavigationBar( - currentIndex: 1, - type: BottomNavigationBarType.shifting, - items: [ - new BottomNavigationBarItem( - icon: const Icon(Icons.ac_unit), - title: const Text('AC') - ), - new BottomNavigationBarItem( - icon: const Icon(Icons.access_alarm), - title: const Text('Alarm') - ) - ] + new MaterialApp( + home: new Scaffold( + bottomNavigationBar: new BottomNavigationBar( + currentIndex: 1, + type: BottomNavigationBarType.shifting, + items: [ + new BottomNavigationBarItem( + icon: const Icon(Icons.ac_unit), + title: const Text('AC') + ), + new BottomNavigationBarItem( + icon: const Icon(Icons.access_alarm), + title: const Text('Alarm') + ) + ] + ) ) ) ); @@ -111,27 +119,29 @@ void main() { testWidgets('BottomNavigationBar multiple taps test', (WidgetTester tester) async { await tester.pumpWidget( - new Scaffold( - bottomNavigationBar: new BottomNavigationBar( - type: BottomNavigationBarType.shifting, - items: [ - new BottomNavigationBarItem( - icon: const Icon(Icons.ac_unit), - title: const Text('AC') - ), - new BottomNavigationBarItem( - icon: const Icon(Icons.access_alarm), - title: const Text('Alarm') - ), - new BottomNavigationBarItem( - icon: const Icon(Icons.access_time), - title: const Text('Time') - ), - new BottomNavigationBarItem( - icon: const Icon(Icons.add), - title: const Text('Add') - ) - ] + new MaterialApp( + home: new Scaffold( + bottomNavigationBar: new BottomNavigationBar( + type: BottomNavigationBarType.shifting, + items: [ + new BottomNavigationBarItem( + icon: const Icon(Icons.ac_unit), + title: const Text('AC') + ), + new BottomNavigationBarItem( + icon: const Icon(Icons.access_alarm), + title: const Text('Alarm') + ), + new BottomNavigationBarItem( + icon: const Icon(Icons.access_time), + title: const Text('Time') + ), + new BottomNavigationBarItem( + icon: const Icon(Icons.add), + title: const Text('Add') + ) + ] + ) ) ) ); @@ -167,35 +177,35 @@ void main() { testWidgets('BottomNavigationBar inherits shadowed app theme for shifting navbar', (WidgetTester tester) async { await tester.pumpWidget( - new MaterialApp( - theme: new ThemeData(brightness: Brightness.light), - home: new Theme( - data: new ThemeData(brightness: Brightness.dark), - child: new Scaffold( - bottomNavigationBar: new BottomNavigationBar( - type: BottomNavigationBarType.shifting, - items: [ - new BottomNavigationBarItem( - icon: const Icon(Icons.ac_unit), - title: const Text('AC') - ), - new BottomNavigationBarItem( - icon: const Icon(Icons.access_alarm), - title: const Text('Alarm') - ), - new BottomNavigationBarItem( - icon: const Icon(Icons.access_time), - title: const Text('Time') - ), - new BottomNavigationBarItem( - icon: const Icon(Icons.add), - title: const Text('Add') - ) - ] - ) + new MaterialApp( + theme: new ThemeData(brightness: Brightness.light), + home: new Theme( + data: new ThemeData(brightness: Brightness.dark), + child: new Scaffold( + bottomNavigationBar: new BottomNavigationBar( + type: BottomNavigationBarType.shifting, + items: [ + new BottomNavigationBarItem( + icon: const Icon(Icons.ac_unit), + title: const Text('AC') + ), + new BottomNavigationBarItem( + icon: const Icon(Icons.access_alarm), + title: const Text('Alarm') + ), + new BottomNavigationBarItem( + icon: const Icon(Icons.access_time), + title: const Text('Time') + ), + new BottomNavigationBarItem( + icon: const Icon(Icons.add), + title: const Text('Add') ) + ] ) + ) ) + ) ); await tester.tap(find.text('Alarm')); @@ -205,35 +215,35 @@ void main() { testWidgets('BottomNavigationBar inherits shadowed app theme for fixed navbar', (WidgetTester tester) async { await tester.pumpWidget( - new MaterialApp( - theme: new ThemeData(brightness: Brightness.light), - home: new Theme( - data: new ThemeData(brightness: Brightness.dark), - child: new Scaffold( - bottomNavigationBar: new BottomNavigationBar( - type: BottomNavigationBarType.fixed, - items: [ - new BottomNavigationBarItem( - icon: const Icon(Icons.ac_unit), - title: const Text('AC') - ), - new BottomNavigationBarItem( - icon: const Icon(Icons.access_alarm), - title: const Text('Alarm') - ), - new BottomNavigationBarItem( - icon: const Icon(Icons.access_time), - title: const Text('Time') - ), - new BottomNavigationBarItem( - icon: const Icon(Icons.add), - title: const Text('Add') - ) - ] - ) + new MaterialApp( + theme: new ThemeData(brightness: Brightness.light), + home: new Theme( + data: new ThemeData(brightness: Brightness.dark), + child: new Scaffold( + bottomNavigationBar: new BottomNavigationBar( + type: BottomNavigationBarType.fixed, + items: [ + new BottomNavigationBarItem( + icon: const Icon(Icons.ac_unit), + title: const Text('AC') + ), + new BottomNavigationBarItem( + icon: const Icon(Icons.access_alarm), + title: const Text('Alarm') + ), + new BottomNavigationBarItem( + icon: const Icon(Icons.access_time), + title: const Text('Time') + ), + new BottomNavigationBarItem( + icon: const Icon(Icons.add), + title: const Text('Add') ) + ] ) + ) ) + ) ); await tester.tap(find.text('Alarm')); @@ -244,27 +254,29 @@ void main() { testWidgets('BottomNavigationBar iconSize test', (WidgetTester tester) async { double builderIconSize; await tester.pumpWidget( - new Scaffold( - bottomNavigationBar: new BottomNavigationBar( - iconSize: 12.0, - items: [ - new BottomNavigationBarItem( - title: const Text('A'), - icon: const Icon(Icons.ac_unit), - ), - new BottomNavigationBarItem( - title: const Text('B'), - icon: new Builder( - builder: (BuildContext context) { - builderIconSize = IconTheme.of(context).size; - return new SizedBox( - width: builderIconSize, - height: builderIconSize, - ); - }, + new MaterialApp( + home: new Scaffold( + bottomNavigationBar: new BottomNavigationBar( + iconSize: 12.0, + items: [ + new BottomNavigationBarItem( + title: const Text('A'), + icon: const Icon(Icons.ac_unit), ), - ), - ], + new BottomNavigationBarItem( + title: const Text('B'), + icon: new Builder( + builder: (BuildContext context) { + builderIconSize = IconTheme.of(context).size; + return new SizedBox( + width: builderIconSize, + height: builderIconSize, + ); + }, + ), + ), + ], + ), ), ), ); diff --git a/packages/flutter/test/material/drawer_test.dart b/packages/flutter/test/material/drawer_test.dart index 4cfa68976f..eebac15071 100644 --- a/packages/flutter/test/material/drawer_test.dart +++ b/packages/flutter/test/material/drawer_test.dart @@ -10,24 +10,26 @@ void main() { final Key containerKey = const Key('container'); await tester.pumpWidget( - new Scaffold( - drawer: new Drawer( - child: new ListView( - children: [ - new DrawerHeader( - child: new Container( - key: containerKey, - child: const Text('header') - ) - ), - const ListTile( - leading: const Icon(Icons.archive), - title: const Text('Archive') - ) - ] - ) - ) - ) + new MaterialApp( + home: new Scaffold( + drawer: new Drawer( + child: new ListView( + children: [ + new DrawerHeader( + child: new Container( + key: containerKey, + child: const Text('header'), + ), + ), + const ListTile( + leading: const Icon(Icons.archive), + title: const Text('Archive'), + ), + ], + ), + ), + ), + ), ); expect(find.text('Archive'), findsNothing); diff --git a/packages/flutter/test/material/icon_button_test.dart b/packages/flutter/test/material/icon_button_test.dart index e4819e66b7..00a9152efa 100644 --- a/packages/flutter/test/material/icon_button_test.dart +++ b/packages/flutter/test/material/icon_button_test.dart @@ -172,15 +172,17 @@ void main() { testWidgets('IconButton AppBar size', (WidgetTester tester) async { await tester.pumpWidget( - new Scaffold( - appBar: new AppBar( - actions: [ - new IconButton( - padding: EdgeInsets.zero, - onPressed: mockOnPressedFunction, - icon: const Icon(Icons.ac_unit), - ), - ], + new MaterialApp( + home: new Scaffold( + appBar: new AppBar( + actions: [ + new IconButton( + padding: EdgeInsets.zero, + onPressed: mockOnPressedFunction, + icon: const Icon(Icons.ac_unit), + ), + ], + ), ), ), ); diff --git a/packages/flutter/test/material/mergeable_material_test.dart b/packages/flutter/test/material/mergeable_material_test.dart index 857c957e70..83c2c9c097 100644 --- a/packages/flutter/test/material/mergeable_material_test.dart +++ b/packages/flutter/test/material/mergeable_material_test.dart @@ -1,4 +1,4 @@ -// Copyright 2016 The Chromium Authors. All rights reserved. + // 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. @@ -64,11 +64,13 @@ BorderRadius getBorderRadius(WidgetTester tester, int index) { void main() { testWidgets('MergeableMaterial empty', (WidgetTester tester) async { await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: const MergeableMaterial() - ) - ) + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: const MergeableMaterial() + ), + ), + ), ); final RenderBox box = tester.renderObject(find.byType(MergeableMaterial)); @@ -77,42 +79,46 @@ void main() { testWidgets('MergeableMaterial update slice', (WidgetTester tester) async { await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] - ) - ) - ) + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ), + ), + ], + ), + ), + ), + ), ); RenderBox box = tester.renderObject(find.byType(MergeableMaterial)); expect(box.size.height, equals(100.0)); await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 200.0 - ) - ) - ] - ) - ) - ) + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 200.0 + ), + ), + ], + ), + ), + ), + ), ); box = tester.renderObject(find.byType(MergeableMaterial)); @@ -121,28 +127,30 @@ void main() { testWidgets('MergeableMaterial swap slices', (WidgetTester tester) async { await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ), - const MaterialSlice( - key: const ValueKey('B'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] - ) - ) - ) + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialSlice( + key: const ValueKey('B'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ), + ), + ], + ), + ), + ), + ), ); RenderBox box = tester.renderObject(find.byType(MergeableMaterial)); @@ -151,28 +159,30 @@ void main() { matches(getBorderRadius(tester, 0), RadiusType.Round, RadiusType.Round); await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('B'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ), - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] - ) - ) - ) + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('B'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ), + ), + ], + ), + ), + ), + ), ); box = tester.renderObject(find.byType(MergeableMaterial)); @@ -186,21 +196,23 @@ void main() { testWidgets('MergeableMaterial paints shadows', (WidgetTester tester) async { await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] - ) - ) - ) + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ), + ), + ], + ), + ), + ), + ), ); final BoxShadow boxShadow = kElevationToShadow[2][0]; @@ -215,31 +227,33 @@ void main() { testWidgets('MergeableMaterial merge gap', (WidgetTester tester) async { await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ), - const MaterialGap( - key: const ValueKey('x') - ), - const MaterialSlice( - key: const ValueKey('B'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] - ) - ) - ) + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialGap( + key: const ValueKey('x') + ), + const MaterialSlice( + key: const ValueKey('B'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ), + ), + ], + ), + ), + ), + ), ); final RenderBox box = tester.renderObject(find.byType(MergeableMaterial)); @@ -249,28 +263,30 @@ void main() { matches(getBorderRadius(tester, 1), RadiusType.Round, RadiusType.Round); await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ), - const MaterialSlice( - key: const ValueKey('B'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] - ) - ) - ) + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialSlice( + key: const ValueKey('B'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ), + ), + ], + ), + ), + ), + ), ); await tester.pump(const Duration(milliseconds: 100)); @@ -288,28 +304,30 @@ void main() { testWidgets('MergeableMaterial separate slices', (WidgetTester tester) async { await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ), - const MaterialSlice( - key: const ValueKey('B'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] - ) - ) - ) + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialSlice( + key: const ValueKey('B'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ), + ), + ], + ), + ), + ), + ), ); final RenderBox box = tester.renderObject(find.byType(MergeableMaterial)); @@ -318,31 +336,33 @@ void main() { matches(getBorderRadius(tester, 0), RadiusType.Round, RadiusType.Round); await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ), - const MaterialGap( - key: const ValueKey('x') - ), - const MaterialSlice( - key: const ValueKey('B'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] - ) - ) - ) + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialGap( + key: const ValueKey('x') + ), + const MaterialSlice( + key: const ValueKey('B'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ), + ), + ], + ), + ), + ), + ), ); await tester.pump(const Duration(milliseconds: 100)); @@ -360,25 +380,27 @@ void main() { testWidgets('MergeableMaterial separate merge seaparate', (WidgetTester tester) async { await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialSlice( + key: const ValueKey('B'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) ) - ), - const MaterialSlice( - key: const ValueKey('B'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] + ] + ) ) ) ) @@ -390,28 +412,30 @@ void main() { matches(getBorderRadius(tester, 0), RadiusType.Round, RadiusType.Round); await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialGap( + key: const ValueKey('x') + ), + const MaterialSlice( + key: const ValueKey('B'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) ) - ), - const MaterialGap( - key: const ValueKey('x') - ), - const MaterialSlice( - key: const ValueKey('B'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] + ] + ) ) ) ) @@ -430,25 +454,27 @@ void main() { matches(getBorderRadius(tester, 1), RadiusType.Round, RadiusType.Round); await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialSlice( + key: const ValueKey('B'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) ) - ), - const MaterialSlice( - key: const ValueKey('B'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] + ] + ) ) ) ) @@ -467,28 +493,30 @@ void main() { matches(getBorderRadius(tester, 1), RadiusType.Sharp, RadiusType.Round); await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialGap( + key: const ValueKey('x') + ), + const MaterialSlice( + key: const ValueKey('B'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) ) - ), - const MaterialGap( - key: const ValueKey('x') - ), - const MaterialSlice( - key: const ValueKey('B'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] + ] + ) ) ) ) @@ -509,25 +537,27 @@ void main() { testWidgets('MergeableMaterial insert slice', (WidgetTester tester) async { await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialSlice( + key: const ValueKey('C'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) ) - ), - const MaterialSlice( - key: const ValueKey('C'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] + ] + ) ) ) ) @@ -539,32 +569,34 @@ void main() { matches(getBorderRadius(tester, 0), RadiusType.Round, RadiusType.Round); await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialSlice( + key: const ValueKey('B'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialSlice( + key: const ValueKey('C'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) ) - ), - const MaterialSlice( - key: const ValueKey('B'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ), - const MaterialSlice( - key: const ValueKey('C'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] + ] + ) ) ) ) @@ -577,32 +609,34 @@ void main() { testWidgets('MergeableMaterial remove slice', (WidgetTester tester) async { await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialSlice( + key: const ValueKey('B'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialSlice( + key: const ValueKey('C'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) ) - ), - const MaterialSlice( - key: const ValueKey('B'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ), - const MaterialSlice( - key: const ValueKey('C'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] + ] + ) ) ) ) @@ -614,25 +648,27 @@ void main() { matches(getBorderRadius(tester, 0), RadiusType.Round, RadiusType.Round); await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialSlice( + key: const ValueKey('C'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) ) - ), - const MaterialSlice( - key: const ValueKey('C'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] + ] + ) ) ) ) @@ -646,25 +682,27 @@ void main() { testWidgets('MergeableMaterial insert chunk', (WidgetTester tester) async { await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialSlice( + key: const ValueKey('C'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) ) - ), - const MaterialSlice( - key: const ValueKey('C'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] + ] + ) ) ) ) @@ -676,38 +714,40 @@ void main() { matches(getBorderRadius(tester, 0), RadiusType.Round, RadiusType.Round); await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialGap( + key: const ValueKey('x') + ), + const MaterialSlice( + key: const ValueKey('B'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialGap( + key: const ValueKey('y') + ), + const MaterialSlice( + key: const ValueKey('C'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) ) - ), - const MaterialGap( - key: const ValueKey('x') - ), - const MaterialSlice( - key: const ValueKey('B'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ), - const MaterialGap( - key: const ValueKey('y') - ), - const MaterialSlice( - key: const ValueKey('C'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] + ] + ) ) ) ) @@ -730,38 +770,40 @@ void main() { testWidgets('MergeableMaterial remove chunk', (WidgetTester tester) async { await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialGap( + key: const ValueKey('x') + ), + const MaterialSlice( + key: const ValueKey('B'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialGap( + key: const ValueKey('y') + ), + const MaterialSlice( + key: const ValueKey('C'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) ) - ), - const MaterialGap( - key: const ValueKey('x') - ), - const MaterialSlice( - key: const ValueKey('B'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ), - const MaterialGap( - key: const ValueKey('y') - ), - const MaterialSlice( - key: const ValueKey('C'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] + ] + ) ) ) ) @@ -775,25 +817,27 @@ void main() { matches(getBorderRadius(tester, 2), RadiusType.Round, RadiusType.Round); await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialSlice( + key: const ValueKey('C'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) ) - ), - const MaterialSlice( - key: const ValueKey('C'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] + ] + ) ) ) ) @@ -814,28 +858,30 @@ void main() { testWidgets('MergeableMaterial replace gap with chunk', (WidgetTester tester) async { await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialGap( + key: const ValueKey('x') + ), + const MaterialSlice( + key: const ValueKey('C'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) ) - ), - const MaterialGap( - key: const ValueKey('x') - ), - const MaterialSlice( - key: const ValueKey('C'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] + ] + ) ) ) ) @@ -848,38 +894,40 @@ void main() { matches(getBorderRadius(tester, 1), RadiusType.Round, RadiusType.Round); await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialGap( + key: const ValueKey('y') + ), + const MaterialSlice( + key: const ValueKey('B'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialGap( + key: const ValueKey('z') + ), + const MaterialSlice( + key: const ValueKey('C'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) ) - ), - const MaterialGap( - key: const ValueKey('y') - ), - const MaterialSlice( - key: const ValueKey('B'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ), - const MaterialGap( - key: const ValueKey('z') - ), - const MaterialSlice( - key: const ValueKey('C'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] + ] + ) ) ) ) @@ -902,38 +950,40 @@ void main() { testWidgets('MergeableMaterial replace chunk with gap', (WidgetTester tester) async { await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialGap( + key: const ValueKey('x') + ), + const MaterialSlice( + key: const ValueKey('B'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialGap( + key: const ValueKey('y') + ), + const MaterialSlice( + key: const ValueKey('C'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) ) - ), - const MaterialGap( - key: const ValueKey('x') - ), - const MaterialSlice( - key: const ValueKey('B'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ), - const MaterialGap( - key: const ValueKey('y') - ), - const MaterialSlice( - key: const ValueKey('C'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] + ] + ) ) ) ) @@ -947,28 +997,30 @@ void main() { matches(getBorderRadius(tester, 2), RadiusType.Round, RadiusType.Round); await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialGap( + key: const ValueKey('z') + ), + const MaterialSlice( + key: const ValueKey('C'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) ) - ), - const MaterialGap( - key: const ValueKey('z') - ), - const MaterialSlice( - key: const ValueKey('C'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] + ] + ) ) ) ) @@ -1001,40 +1053,42 @@ void main() { testWidgets('MergeableMaterial dividers', (WidgetTester tester) async { await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - hasDividers: true, - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + hasDividers: true, + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialSlice( + key: const ValueKey('B'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialSlice( + key: const ValueKey('C'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialSlice( + key: const ValueKey('D'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) ) - ), - const MaterialSlice( - key: const ValueKey('B'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ), - const MaterialSlice( - key: const ValueKey('C'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ), - const MaterialSlice( - key: const ValueKey('D'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] + ] + ) ) ) ) @@ -1049,43 +1103,45 @@ void main() { expect(isDivider(boxes[offset + 3], true, false), isTrue); await tester.pumpWidget( - new Scaffold( - body: new SingleChildScrollView( - child: new MergeableMaterial( - hasDividers: true, - children: [ - const MaterialSlice( - key: const ValueKey('A'), - child: const SizedBox( - width: 100.0, - height: 100.0 + new MaterialApp( + home: new Scaffold( + body: new SingleChildScrollView( + child: new MergeableMaterial( + hasDividers: true, + children: [ + const MaterialSlice( + key: const ValueKey('A'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialSlice( + key: const ValueKey('B'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialGap( + key: const ValueKey('x') + ), + const MaterialSlice( + key: const ValueKey('C'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) + ), + const MaterialSlice( + key: const ValueKey('D'), + child: const SizedBox( + width: 100.0, + height: 100.0 + ) ) - ), - const MaterialSlice( - key: const ValueKey('B'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ), - const MaterialGap( - key: const ValueKey('x') - ), - const MaterialSlice( - key: const ValueKey('C'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ), - const MaterialSlice( - key: const ValueKey('D'), - child: const SizedBox( - width: 100.0, - height: 100.0 - ) - ) - ] + ] + ) ) ) ) diff --git a/packages/flutter/test/material/scaffold_test.dart b/packages/flutter/test/material/scaffold_test.dart index b089f51281..219abe6c43 100644 --- a/packages/flutter/test/material/scaffold_test.dart +++ b/packages/flutter/test/material/scaffold_test.dart @@ -11,9 +11,16 @@ void main() { final Key bodyKey = new UniqueKey(); await tester.pumpWidget(new Scaffold( appBar: new AppBar(title: const Text('Title')), - body: new Container(key: bodyKey) + body: new Container(key: bodyKey), )); + expect(tester.takeException(), isFlutterError); + await tester.pumpWidget(new MaterialApp( + home: new Scaffold( + appBar: new AppBar(title: const Text('Title')), + body: new Container(key: bodyKey), + ), + )); RenderBox bodyBox = tester.renderObject(find.byKey(bodyKey)); expect(bodyBox.size, equals(const Size(800.0, 544.0))); @@ -82,37 +89,37 @@ void main() { }); testWidgets('Floating action animation', (WidgetTester tester) async { - await tester.pumpWidget(const Scaffold( + await tester.pumpWidget(new MaterialApp(home: const Scaffold( floatingActionButton: const FloatingActionButton( key: const Key('one'), onPressed: null, child: const Text("1") ) - )); + ))); expect(tester.binding.transientCallbackCount, 0); - await tester.pumpWidget(const Scaffold( + await tester.pumpWidget(new MaterialApp(home: const Scaffold( floatingActionButton: const FloatingActionButton( key: const Key('two'), onPressed: null, child: const Text("2") ) - )); + ))); expect(tester.binding.transientCallbackCount, greaterThan(0)); await tester.pumpWidget(new Container()); expect(tester.binding.transientCallbackCount, 0); - await tester.pumpWidget(const Scaffold()); + await tester.pumpWidget(new MaterialApp(home: const Scaffold())); expect(tester.binding.transientCallbackCount, 0); - await tester.pumpWidget(const Scaffold( + await tester.pumpWidget(new MaterialApp(home: const Scaffold( floatingActionButton: const FloatingActionButton( key: const Key('one'), onPressed: null, child: const Text("1") ) - )); + ))); expect(tester.binding.transientCallbackCount, greaterThan(0)); }); @@ -367,7 +374,14 @@ void main() { testWidgets('body size with container', (WidgetTester tester) async { final Key testKey = new UniqueKey(); await tester.pumpWidget( - new Scaffold(body: new Container(key: testKey)) + new MediaQuery( + data: const MediaQueryData(), + child: new Scaffold( + body: new Container( + key: testKey, + ), + ), + ), ); expect(tester.element(find.byKey(testKey)).size, const Size(800.0, 600.0)); expect(tester.renderObject(find.byKey(testKey)).localToGlobal(Offset.zero), const Offset(0.0, 0.0)); @@ -376,7 +390,15 @@ void main() { testWidgets('body size with sized container', (WidgetTester tester) async { final Key testKey = new UniqueKey(); await tester.pumpWidget( - new Scaffold(body: new Container(key: testKey, height: 100.0)) + new MediaQuery( + data: const MediaQueryData(), + child: new Scaffold( + body: new Container( + key: testKey, + height: 100.0, + ), + ), + ), ); expect(tester.element(find.byKey(testKey)).size, const Size(800.0, 100.0)); expect(tester.renderObject(find.byKey(testKey)).localToGlobal(Offset.zero), const Offset(0.0, 0.0)); @@ -385,7 +407,16 @@ void main() { testWidgets('body size with centered container', (WidgetTester tester) async { final Key testKey = new UniqueKey(); await tester.pumpWidget( - new Scaffold(body: new Center(child: new Container(key: testKey))) + new MediaQuery( + data: const MediaQueryData(), + child: new Scaffold( + body: new Center( + child: new Container( + key: testKey, + ), + ), + ), + ), ); expect(tester.element(find.byKey(testKey)).size, const Size(800.0, 600.0)); expect(tester.renderObject(find.byKey(testKey)).localToGlobal(Offset.zero), const Offset(0.0, 0.0)); @@ -394,7 +425,16 @@ void main() { testWidgets('body size with button', (WidgetTester tester) async { final Key testKey = new UniqueKey(); await tester.pumpWidget( - new Scaffold(body: new FlatButton(key: testKey, onPressed: () { }, child: const Text(''))) + new MediaQuery( + data: const MediaQueryData(), + child: new Scaffold( + body: new FlatButton( + key: testKey, + onPressed: () { }, + child: const Text(''), + ), + ), + ), ); expect(tester.element(find.byKey(testKey)).size, const Size(88.0, 36.0)); expect(tester.renderObject(find.byKey(testKey)).localToGlobal(Offset.zero), const Offset(0.0, 0.0)); diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index f86d476785..0bc4270ed6 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -26,12 +26,15 @@ class MockClipboard { } Widget overlay(Widget child) { - return new Overlay( - initialEntries: [ - new OverlayEntry( - builder: (BuildContext context) => child, - ), - ], + return new MediaQuery( + data: const MediaQueryData(), + child: new Overlay( + initialEntries: [ + new OverlayEntry( + builder: (BuildContext context) => child, + ), + ], + ), ); } @@ -229,20 +232,23 @@ void main() { final TextEditingController controller = new TextEditingController(); Widget builder() { - return new Overlay( - initialEntries: [ - new OverlayEntry( - builder: (BuildContext context) { - return new Center( - child: new Material( - child: new TextField( - controller: controller, + return new MediaQuery( + data: const MediaQueryData(), + child: new Overlay( + initialEntries: [ + new OverlayEntry( + builder: (BuildContext context) { + return new Center( + child: new Material( + child: new TextField( + controller: controller, + ), ), - ), - ); - }, - ), - ], + ); + }, + ), + ], + ), ); } diff --git a/packages/flutter/test/material/user_accounts_drawer_header_test.dart b/packages/flutter/test/material/user_accounts_drawer_header_test.dart index 6dc560eae5..52cabdf321 100644 --- a/packages/flutter/test/material/user_accounts_drawer_header_test.dart +++ b/packages/flutter/test/material/user_accounts_drawer_header_test.dart @@ -13,31 +13,33 @@ void main() { final Key avatarD = const Key('D'); await tester.pumpWidget( - new Material( - child: new Center( - child: new UserAccountsDrawerHeader( - currentAccountPicture: new CircleAvatar( - key: avatarA, - child: const Text('A'), + new MaterialApp( + home: new Material( + child: new Center( + child: new UserAccountsDrawerHeader( + currentAccountPicture: new CircleAvatar( + key: avatarA, + child: const Text('A'), + ), + otherAccountsPictures: [ + const CircleAvatar( + child: const Text('B'), + ), + new CircleAvatar( + key: avatarC, + child: const Text('C'), + ), + new CircleAvatar( + key: avatarD, + child: const Text('D'), + ), + const CircleAvatar( + child: const Text('E'), + ) + ], + accountName: const Text("name"), + accountEmail: const Text("email"), ), - otherAccountsPictures: [ - const CircleAvatar( - child: const Text('B'), - ), - new CircleAvatar( - key: avatarC, - child: const Text('C'), - ), - new CircleAvatar( - key: avatarD, - child: const Text('D'), - ), - const CircleAvatar( - child: const Text('E'), - ) - ], - accountName: const Text("name"), - accountEmail: const Text("email"), ), ), ), @@ -87,15 +89,17 @@ void main() { VoidCallback onDetailsPressed, EdgeInsets margin, }) { - return new Material( - child: new Center( - child: new UserAccountsDrawerHeader( - currentAccountPicture: currentAccountPicture, - otherAccountsPictures: otherAccountsPictures, - accountName: accountName, - accountEmail: accountEmail, - onDetailsPressed: onDetailsPressed, - margin: margin, + return new MaterialApp( + home: new Material( + child: new Center( + child: new UserAccountsDrawerHeader( + currentAccountPicture: currentAccountPicture, + otherAccountsPictures: otherAccountsPictures, + accountName: accountName, + accountEmail: accountEmail, + onDetailsPressed: onDetailsPressed, + margin: margin, + ), ), ), ); diff --git a/packages/flutter/test/widgets/media_query_test.dart b/packages/flutter/test/widgets/media_query_test.dart index f64ab50357..3441a2377e 100644 --- a/packages/flutter/test/widgets/media_query_test.dart +++ b/packages/flutter/test/widgets/media_query_test.dart @@ -8,21 +8,40 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/widgets.dart'; void main() { - testWidgets('MediaQuery has a default', (WidgetTester tester) async { - Size size; - + testWidgets('MediaQuery does not have a default', (WidgetTester tester) async { + bool tested = false; await tester.pumpWidget( new Builder( builder: (BuildContext context) { - final MediaQueryData data = MediaQuery.of(context); - expect(data, hasOneLineDescription); - expect(data.hashCode, equals(data.copyWith().hashCode)); - size = data.size; + tested = true; + MediaQuery.of(context); // should throw return new Container(); } ) ); + expect(tested, isTrue); + expect(tester.takeException(), isFlutterError); + }); - expect(size, equals(ui.window.physicalSize / ui.window.devicePixelRatio)); + testWidgets('MediaQuery defaults to null', (WidgetTester tester) async { + bool tested = false; + await tester.pumpWidget( + new Builder( + builder: (BuildContext context) { + final MediaQueryData data = MediaQuery.of(context, nullOk: true); + expect(data, isNull); + tested = true; + return new Container(); + } + ) + ); + expect(tested, isTrue); + }); + + testWidgets('MediaQueryData is sane', (WidgetTester tester) async { + final MediaQueryData data = new MediaQueryData.fromWindow(ui.window); + expect(data, hasOneLineDescription); + expect(data.hashCode, equals(data.copyWith().hashCode)); + expect(data.size, equals(ui.window.physicalSize / ui.window.devicePixelRatio)); }); }