From 21f22ed3bae713db9bdd1260dfc5f16c5fae888c Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Thu, 9 Aug 2018 18:23:36 -0700 Subject: [PATCH] Visibility widget (#20365) * Visibility widget This attempts to address the confusion around how to hide a widget subtree. * Apply review comments * More clarifications --- .../flutter/lib/src/rendering/proxy_box.dart | 42 ++- packages/flutter/lib/src/widgets/basic.dart | 66 +++- .../flutter/lib/src/widgets/transitions.dart | 3 +- .../flutter/lib/src/widgets/visibility.dart | 245 ++++++++++++++ packages/flutter/lib/widgets.dart | 1 + .../test/rendering/recording_canvas.dart | 7 + .../flutter/test/widgets/opacity_test.dart | 150 +++++++++ .../test/widgets/semantics_tester.dart | 38 ++- .../flutter/test/widgets/sized_box_test.dart | 23 ++ .../flutter/test/widgets/visibility_test.dart | 313 ++++++++++++++++++ 10 files changed, 853 insertions(+), 35 deletions(-) create mode 100644 packages/flutter/lib/src/widgets/visibility.dart create mode 100644 packages/flutter/test/widgets/opacity_test.dart create mode 100644 packages/flutter/test/widgets/visibility_test.dart diff --git a/packages/flutter/lib/src/rendering/proxy_box.dart b/packages/flutter/lib/src/rendering/proxy_box.dart index a142d9763b..1505013843 100644 --- a/packages/flutter/lib/src/rendering/proxy_box.dart +++ b/packages/flutter/lib/src/rendering/proxy_box.dart @@ -717,16 +717,23 @@ class RenderOpacity extends RenderProxyBox { /// Creates a partially transparent render object. /// /// The [opacity] argument must be between 0.0 and 1.0, inclusive. - RenderOpacity({ double opacity = 1.0, RenderBox child }) - : assert(opacity != null), - assert(opacity >= 0.0 && opacity <= 1.0), - _opacity = opacity, - _alpha = _getAlphaFromOpacity(opacity), - super(child); + RenderOpacity({ + double opacity = 1.0, + bool alwaysIncludeSemantics = false, + RenderBox child, + }) : assert(opacity != null), + assert(opacity >= 0.0 && opacity <= 1.0), + assert(alwaysIncludeSemantics != null), + _opacity = opacity, + _alwaysIncludeSemantics = alwaysIncludeSemantics, + _alpha = _getAlphaFromOpacity(opacity), + super(child); @override bool get alwaysNeedsCompositing => child != null && (_alpha != 0 && _alpha != 255); + int _alpha; + /// The fraction to scale the child's alpha value. /// /// An opacity of 1.0 is fully opaque. An opacity of 0.0 is fully transparent @@ -755,13 +762,26 @@ class RenderOpacity extends RenderProxyBox { markNeedsSemanticsUpdate(); } - int _alpha; + /// Whether child semantics are included regardless of the opacity. + /// + /// If false, semantics are excluded when [opacity] is 0.0. + /// + /// Defaults to false. + bool get alwaysIncludeSemantics => _alwaysIncludeSemantics; + bool _alwaysIncludeSemantics; + set alwaysIncludeSemantics(bool value) { + if (value == _alwaysIncludeSemantics) + return; + _alwaysIncludeSemantics = value; + markNeedsSemanticsUpdate(); + } @override void paint(PaintingContext context, Offset offset) { if (child != null) { - if (_alpha == 0) + if (_alpha == 0) { return; + } if (_alpha == 255) { context.paintChild(child, offset); return; @@ -773,7 +793,7 @@ class RenderOpacity extends RenderProxyBox { @override void visitChildrenForSemantics(RenderObjectVisitor visitor) { - if (child != null && _alpha != 0) + if (child != null && (_alpha != 0 || alwaysIncludeSemantics)) visitor(child); } @@ -781,6 +801,7 @@ class RenderOpacity extends RenderProxyBox { void debugFillProperties(DiagnosticPropertiesBuilder properties) { super.debugFillProperties(properties); properties.add(new DoubleProperty('opacity', opacity)); + properties.add(new FlagProperty('alwaysIncludeSemantics', value: alwaysIncludeSemantics, ifTrue: 'alwaysIncludeSemantics')); } } @@ -832,6 +853,8 @@ class RenderAnimatedOpacity extends RenderProxyBox { /// Whether child semantics are included regardless of the opacity. /// + /// If false, semantics are excluded when [opacity] is 0.0. + /// /// Defaults to false. bool get alwaysIncludeSemantics => _alwaysIncludeSemantics; bool _alwaysIncludeSemantics; @@ -893,6 +916,7 @@ class RenderAnimatedOpacity extends RenderProxyBox { void debugFillProperties(DiagnosticPropertiesBuilder properties) { super.debugFillProperties(properties); properties.add(new DiagnosticsProperty>('opacity', opacity)); + properties.add(new FlagProperty('alwaysIncludeSemantics', value: alwaysIncludeSemantics, ifTrue: 'alwaysIncludeSemantics')); } } diff --git a/packages/flutter/lib/src/widgets/basic.dart b/packages/flutter/lib/src/widgets/basic.dart index 4967718188..ab8da3e3a8 100644 --- a/packages/flutter/lib/src/widgets/basic.dart +++ b/packages/flutter/lib/src/widgets/basic.dart @@ -145,15 +145,15 @@ class Directionality extends InheritedWidget { /// /// ## Opacity Animation /// -/// [Opacity] animations should be built using [AnimatedOpacity] rather than -/// manually rebuilding the [Opacity] widget. -/// /// Animating an [Opacity] widget directly causes the widget (and possibly its /// subtree) to rebuild each frame, which is not very efficient. Consider using /// an [AnimatedOpacity] instead. /// /// See also: /// +/// * [Visibility], which can hide a child more efficiently (albeit less +/// subtly, because it is either visible or hidden, rather than allowing +/// fractional opacity values). /// * [ShaderMask], which can apply more elaborate effects to its child. /// * [Transform], which applies an arbitrary transform to its child widget at /// paint time. @@ -169,8 +169,10 @@ class Opacity extends SingleChildRenderObjectWidget { const Opacity({ Key key, @required this.opacity, + this.alwaysIncludeSemantics = false, Widget child, }) : assert(opacity != null && opacity >= 0.0 && opacity <= 1.0), + assert(alwaysIncludeSemantics != null), super(key: key, child: child); /// The fraction to scale the child's alpha value. @@ -185,18 +187,36 @@ class Opacity extends SingleChildRenderObjectWidget { /// expensive. final double opacity; + /// Whether the semantic information of the children is always included. + /// + /// Defaults to false. + /// + /// When true, regardless of the opacity settings the child semantic + /// information is exposed as if the widget were fully visible. This is + /// useful in cases where labels may be hidden during animations that + /// would otherwise contribute relevant semantics. + final bool alwaysIncludeSemantics; + @override - RenderOpacity createRenderObject(BuildContext context) => new RenderOpacity(opacity: opacity); + RenderOpacity createRenderObject(BuildContext context) { + return new RenderOpacity( + opacity: opacity, + alwaysIncludeSemantics: alwaysIncludeSemantics, + ); + } @override void updateRenderObject(BuildContext context, RenderOpacity renderObject) { - renderObject.opacity = opacity; + renderObject + ..opacity = opacity + ..alwaysIncludeSemantics = alwaysIncludeSemantics; } @override void debugFillProperties(DiagnosticPropertiesBuilder properties) { super.debugFillProperties(properties); properties.add(new DoubleProperty('opacity', opacity)); + properties.add(new FlagProperty('alwaysIncludeSemantics', value: alwaysIncludeSemantics, ifTrue: 'alwaysIncludeSemantics')); } } @@ -1710,6 +1730,12 @@ class SizedBox extends SingleChildRenderObjectWidget { height = double.infinity, super(key: key, child: child); + /// Creates a box that will become as small as its parent allows. + const SizedBox.shrink({ Key key, Widget child }) + : width = 0.0, + height = 0.0, + super(key: key, child: child); + /// Creates a box with the specified size. SizedBox.fromSize({ Key key, Widget child, Size size }) : width = size?.width, @@ -1740,17 +1766,27 @@ class SizedBox extends SingleChildRenderObjectWidget { @override String toStringShort() { - final String type = (width == double.infinity && height == double.infinity) ? - '$runtimeType.expand' : '$runtimeType'; + String type; + if (width == double.infinity && height == double.infinity) { + type = '$runtimeType.expand'; + } else if (width == 0.0 && height == 0.0) { + type = '$runtimeType.shrink'; + } else { + type = '$runtimeType'; + } return key == null ? '$type' : '$type-$key'; } @override void debugFillProperties(DiagnosticPropertiesBuilder properties) { super.debugFillProperties(properties); - final DiagnosticLevel level = (width == double.infinity && height == double.infinity) - ? DiagnosticLevel.hidden - : DiagnosticLevel.info; + DiagnosticLevel level; + if ((width == double.infinity && height == double.infinity) || + (width == 0.0 && height == 0.0)) { + level = DiagnosticLevel.hidden; + } else { + level = DiagnosticLevel.info; + } properties.add(new DoubleProperty('width', width, defaultValue: null, level: level)); properties.add(new DoubleProperty('height', height, defaultValue: null, level: level)); } @@ -2237,9 +2273,9 @@ class SizedOverflowBox extends SingleChildRenderObjectWidget { } } -/// A widget that lays the child out as if it was in the tree, but without painting anything, -/// without making the child available for hit testing, and without taking any -/// room in the parent. +/// A widget that lays the child out as if it was in the tree, but without +/// painting anything, without making the child available for hit testing, and +/// without taking any room in the parent. /// /// Animations continue to run in offstage children, and therefore use battery /// and CPU time, regardless of whether the animations end up being visible. @@ -2251,8 +2287,10 @@ class SizedOverflowBox extends SingleChildRenderObjectWidget { /// /// See also: /// -/// * The [catalog of layout widgets](https://flutter.io/widgets/layout/). +/// * [Visibility], which can hide a child more efficiently (albeit less +/// subtly). /// * [TickerMode], which can be used to disable animations in a subtree. +/// * The [catalog of layout widgets](https://flutter.io/widgets/layout/). class Offstage extends SingleChildRenderObjectWidget { /// Creates a widget that visually hides its child. const Offstage({ Key key, this.offstage = true, Widget child }) diff --git a/packages/flutter/lib/src/widgets/transitions.dart b/packages/flutter/lib/src/widgets/transitions.dart index 7a6d64c85f..047322846c 100644 --- a/packages/flutter/lib/src/widgets/transitions.dart +++ b/packages/flutter/lib/src/widgets/transitions.dart @@ -341,8 +341,8 @@ class FadeTransition extends SingleChildRenderObjectWidget { const FadeTransition({ Key key, @required this.opacity, - Widget child, this.alwaysIncludeSemantics = false, + Widget child, }) : super(key: key, child: child); /// The animation that controls the opacity of the child. @@ -382,6 +382,7 @@ class FadeTransition extends SingleChildRenderObjectWidget { void debugFillProperties(DiagnosticPropertiesBuilder properties) { super.debugFillProperties(properties); properties.add(new DiagnosticsProperty>('opacity', opacity)); + properties.add(new FlagProperty('alwaysIncludeSemantics', value: alwaysIncludeSemantics, ifTrue: 'alwaysIncludeSemantics')); } } diff --git a/packages/flutter/lib/src/widgets/visibility.dart b/packages/flutter/lib/src/widgets/visibility.dart new file mode 100644 index 0000000000..34b6622bc0 --- /dev/null +++ b/packages/flutter/lib/src/widgets/visibility.dart @@ -0,0 +1,245 @@ +// Copyright 2018 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/foundation.dart'; + +import 'basic.dart'; +import 'framework.dart'; +import 'ticker_provider.dart'; + +/// Whether to show or hide a child. +/// +/// By default, the [visible] property controls whether the [child] is included +/// in the subtree or not; when it is not [visible], the [replacement] child +/// (typically a zero-sized box) is included instead. +/// +/// A variety of flags can be used to tweak exactly how the child is hidden. +/// (Changing the flags dynamically is discouraged, as it can cause the [child] +/// subtree to be rebuilt, with any state in the subtree being discarded. +/// Typically, only the [visible] flag is changed dynamically.) +/// +/// These widgets provide some of the facets of this one: +/// +/// * [Opacity], which can stop its child from being painted. +/// * [Offstage], which can stop its child from being laid out or painted. +/// * [TickerMode], which can stop its child from being animated. +/// * [ExcludeSemantics], which can hide the child from accessibility tools. +/// * [IgnorePointer], which can disable touch interactions with the child. +/// +/// Using this widget is not necessary to hide children. The simplest way to +/// hide a child is just to not include it, or, if a child _must_ be given (e.g. +/// because the parent is a [StatelessWidget]) then to use [SizedBox.shrink] +/// instead of the child that would otherwise be included. +/// +/// See also: +/// +/// * [AnimatedSwitcher], which can fade from one child to the next as the +/// subtree changes. +/// * [AnimatedCrossFade], which can fade between two specific children. +class Visibility extends StatelessWidget { + /// Control whether the given [child] is [visible]. + /// + /// The [child] and [replacement] arguments must not be null. + /// + /// The boolean arguments must not be null. + /// + /// The [maintainSemantics] and [maintainInteractivity] arguments can only be + /// set if [maintainSize] is set. + /// + /// The [maintainSize] argument can only be set if [maintainAnimation] is set. + /// + /// The [maintainAnimation] argument can only be set if [maintainState] is set. + const Visibility({ + Key key, + @required this.child, + this.replacement = const SizedBox.shrink(), + this.visible = true, + this.maintainState = false, + this.maintainAnimation = false, + this.maintainSize = false, + this.maintainSemantics = false, + this.maintainInteractivity = false, + }) : assert(child != null), + assert(replacement != null), + assert(visible != null), + assert(maintainState != null), + assert(maintainAnimation != null), + assert(maintainSize != null), + assert(maintainState == true || maintainAnimation == false, 'Cannot maintain animations if the state is not also maintained.'), + assert(maintainAnimation == true || maintainSize == false, 'Cannot maintain size if animations are not maintained.'), + assert(maintainSize == true || maintainSemantics == false, 'Cannot maintain semantics if size is not maintained.'), + assert(maintainSize == true || maintainInteractivity == false, 'Cannot maintain interactivity if size is not maintained.'), + super(key: key); + + /// The widget to show or hide, as controlled by [visible]. + /// + /// {@macro flutter.widgets.child} + final Widget child; + + /// The widget to use when the child is not [visible], assuming that none of + /// the `maintain` flags (in particular, [maintainState]) are set. + /// + /// The normal behavior is to replace the widget with a zero by zero box + /// ([SizedBox.shrink]). + /// + /// See also: + /// + /// * [AnimatedCrossFade], which can animate between two children. + final Widget replacement; + + /// Switches between showing the [child] or hiding it. + /// + /// The `maintain` flags should be set to the same values regardless of the + /// state of the [visible] property, otherwise they will not operate correctly + /// (specifically, the state will be lost regardless of the state of + /// [maintainState] whenever any of the `maintain` flags are changed, since + /// doing so will result in a subtree shape change). + /// + /// Unless [maintainState] is set, the [child] subtree will be disposed + /// (removed from the tree) while hidden. + final bool visible; + + /// Whether to maintain the [State] objects of the [child] subtree when it is + /// not [visible]. + /// + /// Keeping the state of the subtree is potentially expensive (because it + /// means all the objects are still in memory; their resources are not + /// released). It should only be maintained if it cannot be recreated on + /// demand. One example of when the state would be maintained is if the child + /// subtree contains a [Navigator], since that widget maintains elaborate + /// state that cannot be recreated on the fly. + /// + /// If this property is true, an [Offstage] widget is used to hide the child + /// instead of replacing it with [replacement]. + /// + /// If this property is false, then [maintainAnimation] must also be false. + /// + /// Dynamically changing this value may cause the current state of the + /// subtree to be lost (and a new instance of the subtree, with new [State] + /// objects, to be immediately created if [visible] is true). + final bool maintainState; + + /// Whether to maintain animations within the [child] subtree when it is + /// not [visible]. + /// + /// To set this, [maintainState] must also be set. + /// + /// Keeping animations active when the widget is not visible is even more + /// expensive than only maintaining the state. + /// + /// One example when this might be useful is if the subtree is animating its + /// layout in time with an [AnimationController], and the result of that + /// layout is being used to influence some other logic. If this flag is false, + /// then any [AnimationController]s hosted inside the [child] subtree will be + /// muted while the [visible] flag is false. + /// + /// If this property is true, no [TickerMode] widget is used. + /// + /// If this property is false, then [maintainSize] must also be false. + /// + /// Dynamically changing this value may cause the current state of the + /// subtree to be lost (and a new instance of the subtree, with new [State] + /// objects, to be immediately created if [visible] is true). + final bool maintainAnimation; + + /// Whether to maintain space for where the widget would have been. + /// + /// To set this, [maintainAnimation] must also be set. + /// + /// Maintaining the size when the widget is not [visible] is not notably more + /// expensive than just keeping animations running without maintaining the + /// size, and may in some circumstances be slightly cheaper if the subtree is + /// simple and the [visible] property is frequently toggled, since it avoids + /// triggering a layout change when the [visible] property is toggled. If the + /// [child] subtree is not trivial then it is significantly cheaper to not + /// even keep the state (see [maintainState]). + /// + /// If this property is true, [Opacity] is used instead of [Offstage]. + /// + /// If this property is false, then [maintainSemantics] and + /// [maintainInteractivity] must also be false. + /// + /// Dynamically changing this value may cause the current state of the + /// subtree to be lost (and a new instance of the subtree, with new [State] + /// objects, to be immediately created if [visible] is true). + /// + /// See also: + /// + /// * [AnimatedOpacity] and [FadeTransition], which apply animations to the + /// opacity for a more subtle effect. + final bool maintainSize; + + /// Whether to maintain the semantics for the widget when it is hidden (e.g. + /// for accessibility). + /// + /// To set this, [maintainSize] must also be set. + /// + /// By default, with [maintainSemantics] set to false, the [child] is not + /// visible to accessibility tools when it is hidden from the user. If this + /// flag is set to true, then accessibility tools will report the widget as if + /// it was present. + /// + /// Dynamically changing this value may cause the current state of the + /// subtree to be lost (and a new instance of the subtree, with new [State] + /// objects, to be immediately created if [visible] is true). + final bool maintainSemantics; + + /// Whether to allow the widget to be interactive when hidden. + /// + /// To set this, [maintainSize] must also be set. + /// + /// By default, with [maintainInteractivity] set to false, touch events cannot + /// reach the [child] when it is hidden from the user. If this flag is set to + /// true, then touch events will nonetheless be passed through. + /// + /// Dynamically changing this value may cause the current state of the + /// subtree to be lost (and a new instance of the subtree, with new [State] + /// objects, to be immediately created if [visible] is true). + final bool maintainInteractivity; + + @override + Widget build(BuildContext context) { + if (maintainSize) { + Widget result = child; + if (!maintainInteractivity) { + result = new IgnorePointer( + child: child, + ignoring: !visible, + ignoringSemantics: !visible && !maintainSemantics, + ); + } + return new Opacity( + opacity: visible ? 1.0 : 0.0, + alwaysIncludeSemantics: maintainSemantics, + child: result, + ); + } + assert(!maintainInteractivity); + assert(!maintainSemantics); + assert(!maintainSize); + if (maintainState) { + Widget result = child; + if (!maintainAnimation) + result = new TickerMode(child: child, enabled: visible); + return new Offstage( + child: result, + offstage: !visible, + ); + } + assert(!maintainAnimation); + assert(!maintainState); + return visible ? child : replacement; + } + + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties.add(new FlagProperty('visible', value: visible, ifFalse: 'hidden', ifTrue: 'visible')); + properties.add(new FlagProperty('maintainState', value: maintainState, ifFalse: 'maintainState')); + properties.add(new FlagProperty('maintainAnimation', value: maintainAnimation, ifFalse: 'maintainAnimation')); + properties.add(new FlagProperty('maintainSize', value: maintainSize, ifFalse: 'maintainSize')); + properties.add(new FlagProperty('maintainSemantics', value: maintainSemantics, ifFalse: 'maintainSemantics')); + properties.add(new FlagProperty('maintainInteractivity', value: maintainInteractivity, ifFalse: 'maintainInteractivity')); + } +} diff --git a/packages/flutter/lib/widgets.dart b/packages/flutter/lib/widgets.dart index 156cfc6bc9..3ec5fe3c98 100644 --- a/packages/flutter/lib/widgets.dart +++ b/packages/flutter/lib/widgets.dart @@ -99,5 +99,6 @@ export 'src/widgets/title.dart'; export 'src/widgets/transitions.dart'; export 'src/widgets/unique_widget.dart'; export 'src/widgets/viewport.dart'; +export 'src/widgets/visibility.dart'; export 'src/widgets/widget_inspector.dart'; export 'src/widgets/will_pop_scope.dart'; diff --git a/packages/flutter/test/rendering/recording_canvas.dart b/packages/flutter/test/rendering/recording_canvas.dart index 6eb6b7006b..edc0eee5bf 100644 --- a/packages/flutter/test/rendering/recording_canvas.dart +++ b/packages/flutter/test/rendering/recording_canvas.dart @@ -126,6 +126,13 @@ class TestRecordingPaintingContext extends ClipContext implements PaintingContex canvas.restore(); } + @override + void pushOpacity(Offset offset, int alpha, PaintingContextCallback painter) { + canvas.saveLayer(null, null); // TODO(ianh): Expose the alpha somewhere. + painter(this, offset); + canvas.restore(); + } + @override void pushLayer(Layer childLayer, PaintingContextCallback painter, Offset offset, {Rect childPaintBounds}) { painter(this, offset); diff --git a/packages/flutter/test/widgets/opacity_test.dart b/packages/flutter/test/widgets/opacity_test.dart new file mode 100644 index 0000000000..ace2f96926 --- /dev/null +++ b/packages/flutter/test/widgets/opacity_test.dart @@ -0,0 +1,150 @@ +// Copyright 2018 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/widgets.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import '../rendering/mock_canvas.dart'; +import 'semantics_tester.dart'; + +void main() { + testWidgets('Opacity', (WidgetTester tester) async { + final SemanticsTester semantics = new SemanticsTester(tester); + + // Opacity 1.0: Semantics and painting + await tester.pumpWidget( + const Opacity( + child: Text('a', textDirection: TextDirection.rtl), + opacity: 1.0, + ), + ); + expect(semantics, hasSemantics( + new TestSemantics.root( + children: [ + new TestSemantics.rootChild( + id: 1, + rect: Rect.fromLTRB(0.0, 0.0, 800.0, 600.0), + label: 'a', + textDirection: TextDirection.rtl, + ) + ], + ), + )); + expect(find.byType(Opacity), paints..paragraph()); + + // Opacity 0.0: Nothing + await tester.pumpWidget( + const Opacity( + child: Text('a', textDirection: TextDirection.rtl), + opacity: 0.0, + ), + ); + expect(semantics, hasSemantics( + new TestSemantics.root(), + )); + expect(find.byType(Opacity), paintsNothing); + + // Opacity 0.0 with semantics: Just semantics + await tester.pumpWidget( + const Opacity( + child: Text('a', textDirection: TextDirection.rtl), + opacity: 0.0, + alwaysIncludeSemantics: true, + ), + ); + expect(semantics, hasSemantics( + new TestSemantics.root( + children: [ + new TestSemantics.rootChild( + id: 1, + rect: Rect.fromLTRB(0.0, 0.0, 800.0, 600.0), + label: 'a', + textDirection: TextDirection.rtl, + ) + ], + ), + )); + expect(find.byType(Opacity), paintsNothing); + + // Opacity 0.0 without semantics: Nothing + await tester.pumpWidget( + const Opacity( + child: Text('a', textDirection: TextDirection.rtl), + opacity: 0.0, + alwaysIncludeSemantics: false, + ), + ); + expect(semantics, hasSemantics( + new TestSemantics.root(), + )); + expect(find.byType(Opacity), paintsNothing); + + // Opacity 0.1: Semantics and painting + await tester.pumpWidget( + const Opacity( + child: Text('a', textDirection: TextDirection.rtl), + opacity: 0.1, + ), + ); + expect(semantics, hasSemantics( + new TestSemantics.root( + children: [ + new TestSemantics.rootChild( + id: 1, + rect: Rect.fromLTRB(0.0, 0.0, 800.0, 600.0), + label: 'a', + textDirection: TextDirection.rtl, + ) + ], + ), + )); + expect(find.byType(Opacity), paints..paragraph()); + + // Opacity 0.1 without semantics: Still has semantics and painting + await tester.pumpWidget( + const Opacity( + child: Text('a', textDirection: TextDirection.rtl), + opacity: 0.1, + alwaysIncludeSemantics: false, + ), + ); + expect(semantics, hasSemantics( + new TestSemantics.root( + children: [ + new TestSemantics.rootChild( + id: 1, + rect: Rect.fromLTRB(0.0, 0.0, 800.0, 600.0), + label: 'a', + textDirection: TextDirection.rtl, + ) + ], + ), + )); + expect(find.byType(Opacity), paints..paragraph()); + + // Opacity 0.1 with semantics: Semantics and painting + await tester.pumpWidget( + const Opacity( + child: Text('a', textDirection: TextDirection.rtl), + opacity: 0.1, + alwaysIncludeSemantics: true, + ), + ); + expect(semantics, hasSemantics( + new TestSemantics.root( + children: [ + new TestSemantics.rootChild( + id: 1, + rect: Rect.fromLTRB(0.0, 0.0, 800.0, 600.0), + label: 'a', + textDirection: TextDirection.rtl, + ) + ], + ), + )); + expect(find.byType(Opacity), paints..paragraph()); + + semantics.dispose(); + }); +} diff --git a/packages/flutter/test/widgets/semantics_tester.dart b/packages/flutter/test/widgets/semantics_tester.dart index 4a3c276b52..84af7eb2d3 100644 --- a/packages/flutter/test/widgets/semantics_tester.dart +++ b/packages/flutter/test/widgets/semantics_tester.dart @@ -225,8 +225,6 @@ class TestSemantics { DebugSemanticsDumpOrder childOrder = DebugSemanticsDumpOrder.inverseHitTest, } ) { - final SemanticsData nodeData = node.getSemanticsData(); - bool fail(String message) { matchState[TestSemantics] = '$message'; return false; @@ -237,6 +235,8 @@ class TestSemantics { if (!ignoreId && id != node.id) return fail('expected node id $id but found id ${node.id}.'); + final SemanticsData nodeData = node.getSemanticsData(); + final int flagsBitmask = flags is int ? flags : flags.fold(0, (int bitmask, SemanticsFlag flag) => bitmask | flag.index); @@ -372,7 +372,7 @@ class SemanticsTester { } @override - String toString() => 'SemanticsTester for ${tester.binding.pipelineOwner.semanticsOwner.rootSemanticsNode}'; + String toString() => 'SemanticsTester for ${tester.binding.pipelineOwner.semanticsOwner?.rootSemanticsNode}'; /// Returns all semantics nodes in the current semantics tree whose properties /// match the non-null arguments. @@ -487,7 +487,7 @@ class SemanticsTester { /// over-test. Prefer breaking your widgets into smaller widgets and test them /// individually. String generateTestSemanticsExpressionForCurrentSemanticsTree(DebugSemanticsDumpOrder childOrder) { - final SemanticsNode node = tester.binding.pipelineOwner.semanticsOwner.rootSemanticsNode; + final SemanticsNode node = tester.binding.pipelineOwner.semanticsOwner?.rootSemanticsNode; return _generateSemanticsTestForNode(node, 0, childOrder); } @@ -520,6 +520,8 @@ class SemanticsTester { /// Recursively generates [TestSemantics] code for [node] and its children, /// indenting the expression by `indentAmount`. static String _generateSemanticsTestForNode(SemanticsNode node, int indentAmount, DebugSemanticsDumpOrder childOrder) { + if (node == null) + return 'null'; final String indent = ' ' * indentAmount; final StringBuffer buf = new StringBuffer(); final SemanticsData nodeData = node.getSemanticsData(); @@ -590,7 +592,7 @@ class _HasSemantics extends Matcher { @override bool matches(covariant SemanticsTester item, Map matchState) { final bool doesMatch = _semantics._matches( - item.tester.binding.pipelineOwner.semanticsOwner.rootSemanticsNode, + item.tester.binding.pipelineOwner.semanticsOwner?.rootSemanticsNode, matchState, ignoreTransform: ignoreTransform, ignoreRect: ignoreRect, @@ -600,6 +602,9 @@ class _HasSemantics extends Matcher { if (!doesMatch) { matchState['would-match'] = item.generateTestSemanticsExpressionForCurrentSemanticsTree(childOrder); } + if (item.tester.binding.pipelineOwner.semanticsOwner == null) { + matchState['additional-notes'] = '(Check that the SemanticsTester has not been disposed early.)'; + } return doesMatch; } @@ -608,14 +613,25 @@ class _HasSemantics extends Matcher { return description.add('semantics node matching:\n$_semantics'); } + String _indent(String text) { + return text.toString().trimRight().split('\n').map((String line) => ' $line').join('\n'); + } + @override Description describeMismatch(dynamic item, Description mismatchDescription, Map matchState, bool verbose) { - return mismatchDescription - .add('${matchState[TestSemantics]}\n') - .add('Current SemanticsNode tree:\n') - .add(RendererBinding.instance?.renderView?.debugSemantics?.toStringDeep(childOrder: childOrder)) - .add('The semantics tree would have matched the following configuration:\n') - .add(matchState['would-match']); + Description result = mismatchDescription + .add('${matchState[TestSemantics]}\n') + .add('Current SemanticsNode tree:\n') + .add(_indent(RendererBinding.instance?.renderView?.debugSemantics?.toStringDeep(childOrder: childOrder))) + .add('\n') + .add('The semantics tree would have matched the following configuration:\n') + .add(_indent(matchState['would-match'])); + if (matchState.containsKey('additional-notes')) { + result = result + .add('\n') + .add(matchState['additional-notes']); + } + return result; } } diff --git a/packages/flutter/test/widgets/sized_box_test.dart b/packages/flutter/test/widgets/sized_box_test.dart index 5f808493cd..ae80796c78 100644 --- a/packages/flutter/test/widgets/sized_box_test.dart +++ b/packages/flutter/test/widgets/sized_box_test.dart @@ -30,6 +30,10 @@ void main() { const SizedBox f = SizedBox.expand(); expect(f.width, double.infinity); expect(f.height, double.infinity); + + const SizedBox g = SizedBox.shrink(); + expect(g.width, 0.0); + expect(g.height, 0.0); }); testWidgets('SizedBox - no child', (WidgetTester tester) async { @@ -95,6 +99,15 @@ void main() { ) ); expect(patient.currentContext.size, equals(const Size(800.0, 600.0))); + + await tester.pumpWidget( + new Center( + child: new SizedBox.shrink( + key: patient, + ) + ) + ); + expect(patient.currentContext.size, equals(const Size(0.0, 0.0))); }); testWidgets('SizedBox - container child', (WidgetTester tester) async { @@ -166,5 +179,15 @@ void main() { ) ); expect(patient.currentContext.size, equals(const Size(800.0, 600.0))); + + await tester.pumpWidget( + new Center( + child: new SizedBox.shrink( + key: patient, + child: new Container(), + ) + ) + ); + expect(patient.currentContext.size, equals(const Size(0.0, 0.0))); }); } diff --git a/packages/flutter/test/widgets/visibility_test.dart b/packages/flutter/test/widgets/visibility_test.dart new file mode 100644 index 0000000000..be6a544967 --- /dev/null +++ b/packages/flutter/test/widgets/visibility_test.dart @@ -0,0 +1,313 @@ +// Copyright 2018 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/widgets.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import '../rendering/mock_canvas.dart'; +import 'semantics_tester.dart'; + +class TestState extends StatefulWidget { + const TestState({ Key key, this.child, this.log }) : super(key: key); + final Widget child; + final List log; + @override + State createState() => new _TestStateState(); +} + +class _TestStateState extends State { + @override + void initState() { + super.initState(); + widget.log.add('created new state'); + } + @override + Widget build(BuildContext context) { + return widget.child; + } +} + +void main() { + testWidgets('Visibility', (WidgetTester tester) async { + final SemanticsTester semantics = new SemanticsTester(tester); + final List log = []; + + final Widget testChild = new GestureDetector( + onTap: () { log.add('tap'); }, + child: new Builder( + builder: (BuildContext context) { + final bool animating = TickerMode.of(context); + return new TestState( + log: log, + child: new Text('a $animating', textDirection: TextDirection.rtl), + ); + }, + ), + ); + + final Matcher expectedSemanticsWhenPresent = hasSemantics( + new TestSemantics.root( + children: [ + new TestSemantics.rootChild( + label: 'a true', + textDirection: TextDirection.rtl, + actions: [SemanticsAction.tap], + ) + ], + ), + ignoreId: true, + ignoreRect: true, + ignoreTransform: true, + ); + + final Matcher expectedSemanticsWhenAbsent = hasSemantics(new TestSemantics.root()); + + // We now run a sequence of pumpWidget calls one after the other. In + // addition to verifying that the right behaviour is seen in each case, this + // also verifies that the widget can dynamically change from state to state. + + await tester.pumpWidget(new Visibility(child: testChild)); + expect(find.byType(Text, skipOffstage: false), findsOneWidget); + expect(find.text('a true', skipOffstage: false), findsOneWidget); + expect(find.byType(Visibility), paints..paragraph()); + expect(tester.getSize(find.byType(Visibility)), const Size(800.0, 600.0)); + expect(semantics, expectedSemanticsWhenPresent); + expect(log, ['created new state']); + await tester.tap(find.byType(Visibility)); + expect(log, ['created new state', 'tap']); + log.clear(); + + await tester.pumpWidget(new Visibility(child: testChild, visible: false)); + expect(find.byType(Text, skipOffstage: false), findsNothing); + expect(find.byType(Visibility), paintsNothing); + expect(tester.getSize(find.byType(Visibility)), const Size(800.0, 600.0)); + expect(semantics, expectedSemanticsWhenAbsent); + expect(log, []); + await tester.tap(find.byType(Visibility)); + expect(log, []); + log.clear(); + + await tester.pumpWidget(new Center(child: new Visibility(child: testChild, visible: false))); + expect(find.byType(Text, skipOffstage: false), findsNothing); + expect(find.byType(Placeholder), findsNothing); + expect(find.byType(Visibility), paintsNothing); + expect(tester.getSize(find.byType(Visibility)), Size.zero); + expect(semantics, expectedSemanticsWhenAbsent); + expect(log, []); + await tester.tap(find.byType(Visibility)); + expect(log, []); + log.clear(); + + await tester.pumpWidget(new Center(child: new Visibility(child: testChild, replacement: const Placeholder(), visible: false))); + expect(find.byType(Text, skipOffstage: false), findsNothing); + expect(find.byType(Placeholder), findsOneWidget); + expect(find.byType(Visibility), paints..path()); + expect(tester.getSize(find.byType(Visibility)), const Size(800.0, 600.0)); + expect(semantics, expectedSemanticsWhenAbsent); + expect(log, []); + await tester.tap(find.byType(Visibility)); + expect(log, []); + log.clear(); + + await tester.pumpWidget(new Center(child: new Visibility(child: testChild, replacement: const Placeholder(), visible: true))); + expect(find.byType(Text, skipOffstage: false), findsOneWidget); + expect(find.text('a true', skipOffstage: false), findsOneWidget); + expect(find.byType(Placeholder), findsNothing); + expect(find.byType(Visibility), paints..paragraph()); + expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0)); + expect(semantics, expectedSemanticsWhenPresent); + expect(log, ['created new state']); + await tester.tap(find.byType(Visibility)); + expect(log, ['created new state', 'tap']); + log.clear(); + + await tester.pumpWidget(new Center(child: new Visibility(child: testChild, visible: true, maintainState: true, maintainAnimation: true, maintainSize: true, maintainInteractivity: true, maintainSemantics: true))); + expect(find.byType(Text, skipOffstage: false), findsOneWidget); + expect(find.text('a true', skipOffstage: false), findsOneWidget); + expect(find.byType(Placeholder), findsNothing); + expect(find.byType(Visibility), paints..paragraph()); + expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0)); + expect(semantics, expectedSemanticsWhenPresent); + expect(log, ['created new state']); + await tester.tap(find.byType(Visibility)); + expect(log, ['created new state', 'tap']); + log.clear(); + + await tester.pumpWidget(new Center(child: new Visibility(child: testChild, visible: false, maintainState: true, maintainAnimation: true, maintainSize: true, maintainInteractivity: true, maintainSemantics: true))); + expect(find.byType(Text, skipOffstage: false), findsOneWidget); + expect(find.text('a true', skipOffstage: false), findsOneWidget); + expect(find.byType(Placeholder), findsNothing); + expect(find.byType(Visibility), paintsNothing); + expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0)); + expect(semantics, expectedSemanticsWhenPresent); + expect(log, []); + await tester.tap(find.byType(Visibility)); + expect(log, ['tap']); + log.clear(); + + await tester.pumpWidget(new Center(child: new Visibility(child: testChild, visible: false, maintainState: true, maintainAnimation: true, maintainSize: true, maintainInteractivity: true))); + expect(find.byType(Text, skipOffstage: false), findsOneWidget); + expect(find.text('a true', skipOffstage: false), findsOneWidget); + expect(find.byType(Placeholder), findsNothing); + expect(find.byType(Visibility), paintsNothing); + expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0)); + expect(semantics, expectedSemanticsWhenAbsent); + expect(log, []); + await tester.tap(find.byType(Visibility)); + expect(log, ['tap']); + log.clear(); + + await tester.pumpWidget(new Center(child: new Visibility(child: testChild, visible: false, maintainState: true, maintainAnimation: true, maintainSize: true, maintainSemantics: true))); + expect(find.byType(Text, skipOffstage: false), findsOneWidget); + expect(find.text('a true', skipOffstage: false), findsOneWidget); + expect(find.byType(Placeholder), findsNothing); + expect(find.byType(Visibility), paintsNothing); + expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0)); + expect(semantics, expectedSemanticsWhenPresent); + expect(log, ['created new state']); + await tester.tap(find.byType(Visibility)); + expect(log, ['created new state']); + log.clear(); + + await tester.pumpWidget(new Center(child: new Visibility(child: testChild, visible: false, maintainState: true, maintainAnimation: true, maintainSize: true))); + expect(find.byType(Text, skipOffstage: false), findsOneWidget); + expect(find.text('a true', skipOffstage: false), findsOneWidget); + expect(find.byType(Placeholder), findsNothing); + expect(find.byType(Visibility), paintsNothing); + expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0)); + expect(semantics, expectedSemanticsWhenAbsent); + expect(log, []); + await tester.tap(find.byType(Visibility)); + expect(log, []); + log.clear(); + + await tester.pumpWidget(new Center(child: new Visibility(child: testChild, visible: false, maintainState: true, maintainAnimation: true))); + expect(find.byType(Text, skipOffstage: false), findsOneWidget); + expect(find.byType(Text, skipOffstage: true), findsNothing); + expect(find.text('a true', skipOffstage: false), findsOneWidget); + expect(find.byType(Placeholder), findsNothing); + expect(find.byType(Visibility), paintsNothing); + expect(tester.getSize(find.byType(Visibility)), Size.zero); + expect(semantics, expectedSemanticsWhenAbsent); + expect(log, ['created new state']); + await tester.tap(find.byType(Visibility)); + expect(log, ['created new state']); + log.clear(); + + await tester.pumpWidget(new Center(child: new Visibility(child: testChild, visible: false, maintainState: true))); + expect(find.byType(Text, skipOffstage: false), findsOneWidget); + expect(find.byType(Text, skipOffstage: true), findsNothing); + expect(find.text('a false', skipOffstage: false), findsOneWidget); + expect(find.byType(Placeholder), findsNothing); + expect(find.byType(Visibility), paintsNothing); + expect(tester.getSize(find.byType(Visibility)), Size.zero); + expect(semantics, expectedSemanticsWhenAbsent); + expect(log, ['created new state']); + await tester.tap(find.byType(Visibility)); + expect(log, ['created new state']); + log.clear(); + + // Now we toggle the visibility off and on a few times to make sure that works. + + await tester.pumpWidget(new Center(child: new Visibility(child: testChild, visible: true, maintainState: true))); + expect(find.byType(Text), findsOneWidget); + expect(find.text('a true', skipOffstage: false), findsOneWidget); + expect(find.byType(Placeholder), findsNothing); + expect(find.byType(Visibility), paints..paragraph()); + expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0)); + expect(semantics, expectedSemanticsWhenPresent); + expect(log, []); + await tester.tap(find.byType(Visibility)); + expect(log, ['tap']); + log.clear(); + + await tester.pumpWidget(new Center(child: new Visibility(child: testChild, visible: false, maintainState: true))); + expect(find.byType(Text, skipOffstage: false), findsOneWidget); + expect(find.byType(Text, skipOffstage: true), findsNothing); + expect(find.text('a false', skipOffstage: false), findsOneWidget); + expect(find.byType(Placeholder), findsNothing); + expect(find.byType(Visibility), paintsNothing); + expect(tester.getSize(find.byType(Visibility)), Size.zero); + expect(semantics, expectedSemanticsWhenAbsent); + expect(log, []); + await tester.tap(find.byType(Visibility)); + expect(log, []); + log.clear(); + + await tester.pumpWidget(new Center(child: new Visibility(child: testChild, visible: true, maintainState: true))); + expect(find.byType(Text), findsOneWidget); + expect(find.text('a true', skipOffstage: false), findsOneWidget); + expect(find.byType(Placeholder), findsNothing); + expect(find.byType(Visibility), paints..paragraph()); + expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0)); + expect(semantics, expectedSemanticsWhenPresent); + expect(log, []); + await tester.tap(find.byType(Visibility)); + expect(log, ['tap']); + log.clear(); + + await tester.pumpWidget(new Center(child: new Visibility(child: testChild, visible: false, maintainState: true))); + expect(find.byType(Text, skipOffstage: false), findsOneWidget); + expect(find.byType(Text, skipOffstage: true), findsNothing); + expect(find.text('a false', skipOffstage: false), findsOneWidget); + expect(find.byType(Placeholder), findsNothing); + expect(find.byType(Visibility), paintsNothing); + expect(tester.getSize(find.byType(Visibility)), Size.zero); + expect(semantics, expectedSemanticsWhenAbsent); + expect(log, []); + await tester.tap(find.byType(Visibility)); + expect(log, []); + log.clear(); + + // Same but without maintainState. + + await tester.pumpWidget(new Center(child: new Visibility(child: testChild, visible: false))); + expect(find.byType(Text, skipOffstage: false), findsNothing); + expect(find.byType(Placeholder), findsNothing); + expect(find.byType(Visibility), paintsNothing); + expect(tester.getSize(find.byType(Visibility)), Size.zero); + expect(semantics, expectedSemanticsWhenAbsent); + expect(log, []); + await tester.tap(find.byType(Visibility)); + expect(log, []); + log.clear(); + + await tester.pumpWidget(new Center(child: new Visibility(child: testChild, visible: true))); + expect(find.byType(Text), findsOneWidget); + expect(find.text('a true', skipOffstage: false), findsOneWidget); + expect(find.byType(Placeholder), findsNothing); + expect(find.byType(Visibility), paints..paragraph()); + expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0)); + expect(semantics, expectedSemanticsWhenPresent); + expect(log, ['created new state']); + await tester.tap(find.byType(Visibility)); + expect(log, ['created new state', 'tap']); + log.clear(); + + await tester.pumpWidget(new Center(child: new Visibility(child: testChild, visible: false))); + expect(find.byType(Text, skipOffstage: false), findsNothing); + expect(find.byType(Placeholder), findsNothing); + expect(find.byType(Visibility), paintsNothing); + expect(tester.getSize(find.byType(Visibility)), Size.zero); + expect(semantics, expectedSemanticsWhenAbsent); + expect(log, []); + await tester.tap(find.byType(Visibility)); + expect(log, []); + log.clear(); + + await tester.pumpWidget(new Center(child: new Visibility(child: testChild, visible: true))); + expect(find.byType(Text), findsOneWidget); + expect(find.text('a true', skipOffstage: false), findsOneWidget); + expect(find.byType(Placeholder), findsNothing); + expect(find.byType(Visibility), paints..paragraph()); + expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0)); + expect(semantics, expectedSemanticsWhenPresent); + expect(log, ['created new state']); + await tester.tap(find.byType(Visibility)); + expect(log, ['created new state', 'tap']); + log.clear(); + + semantics.dispose(); + }); +}