From 13bfa7353eac78699b286445c3f6928974087f4d Mon Sep 17 00:00:00 2001 From: liyuqian Date: Thu, 9 Aug 2018 16:38:08 -0700 Subject: [PATCH] Set default clipBehavior to Clip.none and update tests (#20205) This fixes https://github.com/flutter/flutter/issues/18057 --- packages/flutter/lib/src/material/button.dart | 19 ++- .../flutter/lib/src/material/material.dart | 22 +-- .../lib/src/material/outline_button.dart | 13 +- .../lib/src/material/raised_button.dart | 16 ++- .../flutter/lib/src/painting/basic_types.dart | 1 + packages/flutter/lib/src/rendering/layer.dart | 9 +- .../flutter/lib/src/rendering/object.dart | 1 - .../flutter/lib/src/rendering/proxy_box.dart | 30 ++-- packages/flutter/lib/src/widgets/basic.dart | 5 +- .../lib/src/widgets/implicit_animations.dart | 4 +- .../flutter/test/material/buttons_test.dart | 31 ++++ .../flutter/test/material/material_test.dart | 28 +++- .../test/material/outline_button_test.dart | 24 ++++ .../test/rendering/recording_canvas.dart | 2 - packages/flutter/test/widgets/clip_test.dart | 134 +++++++++++++++++- packages/flutter_test/lib/src/matchers.dart | 68 ++++++--- 16 files changed, 336 insertions(+), 71 deletions(-) diff --git a/packages/flutter/lib/src/material/button.dart b/packages/flutter/lib/src/material/button.dart index bd415d1bcd..31a86bc418 100644 --- a/packages/flutter/lib/src/material/button.dart +++ b/packages/flutter/lib/src/material/button.dart @@ -29,8 +29,8 @@ import 'theme_data.dart'; class RawMaterialButton extends StatefulWidget { /// Create a button based on [Semantics], [Material], and [InkWell] widgets. /// - /// The [shape], [elevation], [padding], and [constraints] arguments - /// must not be null. + /// The [shape], [elevation], [padding], [constraints], and [clipBehavior] + /// arguments must not be null. const RawMaterialButton({ Key key, @required this.onPressed, @@ -46,6 +46,7 @@ class RawMaterialButton extends StatefulWidget { this.constraints = const BoxConstraints(minWidth: 88.0, minHeight: 36.0), this.shape = const RoundedRectangleBorder(), this.animationDuration = kThemeChangeDuration, + this.clipBehavior = Clip.none, MaterialTapTargetSize materialTapTargetSize, this.child, }) : this.materialTapTargetSize = materialTapTargetSize ?? MaterialTapTargetSize.padded, @@ -56,6 +57,7 @@ class RawMaterialButton extends StatefulWidget { assert(padding != null), assert(constraints != null), assert(animationDuration != null), + assert(clipBehavior != null), super(key: key); /// Called when the button is tapped or otherwise activated. @@ -148,6 +150,9 @@ class RawMaterialButton extends StatefulWidget { /// * [MaterialTapTargetSize], for a description of how this affects tap targets. final MaterialTapTargetSize materialTapTargetSize; + /// {@macro flutter.widgets.Clip} + final Clip clipBehavior; + @override _RawMaterialButtonState createState() => new _RawMaterialButtonState(); } @@ -177,6 +182,7 @@ class _RawMaterialButtonState extends State { color: widget.fillColor, type: widget.fillColor == null ? MaterialType.transparency : MaterialType.button, animationDuration: widget.animationDuration, + clipBehavior: widget.clipBehavior, child: new InkWell( onHighlightChanged: _handleHighlightChanged, splashColor: widget.splashColor, @@ -245,6 +251,8 @@ class MaterialButton extends StatelessWidget { /// Rather than creating a material button directly, consider using /// [FlatButton] or [RaisedButton]. To create a custom Material button /// consider using [RawMaterialButton]. + /// + /// The [clipBehavior] argument must not be null. const MaterialButton({ Key key, this.colorBrightness, @@ -259,9 +267,10 @@ class MaterialButton extends StatelessWidget { this.height, this.padding, this.materialTapTargetSize, + this.clipBehavior = Clip.none, @required this.onPressed, this.child - }) : super(key: key); + }) : assert(clipBehavior != null), super(key: key); /// The theme brightness to use for this button. /// @@ -373,6 +382,9 @@ class MaterialButton extends StatelessWidget { /// * [MaterialTapTargetSize], for a description of how this affects tap targets. final MaterialTapTargetSize materialTapTargetSize; + /// {@macro flutter.widgets.Clip} + final Clip clipBehavior; + /// Whether the button is enabled or disabled. Buttons are disabled by default. To /// enable a button, set its [onPressed] property to a non-null value. bool get enabled => onPressed != null; @@ -433,6 +445,7 @@ class MaterialButton extends StatelessWidget { shape: buttonTheme.shape, child: child, materialTapTargetSize: materialTapTargetSize ?? theme.materialTapTargetSize, + clipBehavior: clipBehavior, ); } diff --git a/packages/flutter/lib/src/material/material.dart b/packages/flutter/lib/src/material/material.dart index 4e73e77976..dc06f8e9ec 100644 --- a/packages/flutter/lib/src/material/material.dart +++ b/packages/flutter/lib/src/material/material.dart @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:ui' show Clip, defaultClipBehavior; // ignore: deprecated_member_use - import 'package:flutter/foundation.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/widgets.dart'; @@ -174,7 +172,7 @@ class Material extends StatefulWidget { this.textStyle, this.borderRadius, this.shape, - this.clipBehavior = defaultClipBehavior, // ignore: deprecated_member_use + this.clipBehavior = Clip.none, this.animationDuration = kThemeChangeDuration, this.child, }) : assert(type != null), @@ -370,14 +368,16 @@ class _MaterialState extends State with TickerProviderStateMixin { } static Widget _transparentInterior({ShapeBorder shape, Clip clipBehavior, Widget contents}) { + final _ShapeBorderPaint child = new _ShapeBorderPaint( + child: contents, + shape: shape, + ); + if (clipBehavior == Clip.none) { + return child; + } return new ClipPath( - child: new _ShapeBorderPaint( - child: contents, - shape: shape, - ), - clipper: new ShapeBorderClipper( - shape: shape, - ), + child: child, + clipper: new ShapeBorderClipper(shape: shape), clipBehavior: clipBehavior, ); } @@ -598,7 +598,7 @@ class _MaterialInterior extends ImplicitlyAnimatedWidget { Key key, @required this.child, @required this.shape, - this.clipBehavior = defaultClipBehavior, // ignore: deprecated_member_use + this.clipBehavior = Clip.none, @required this.elevation, @required this.color, @required this.shadowColor, diff --git a/packages/flutter/lib/src/material/outline_button.dart b/packages/flutter/lib/src/material/outline_button.dart index b9b08d583a..d57a66f9df 100644 --- a/packages/flutter/lib/src/material/outline_button.dart +++ b/packages/flutter/lib/src/material/outline_button.dart @@ -51,7 +51,7 @@ const Duration _kElevationDuration = Duration(milliseconds: 75); class OutlineButton extends StatefulWidget { /// Create a filled button. /// - /// The [highlightElevation], and [borderWidth] + /// The [highlightElevation], [borderWidth], and [clipBehavior] /// arguments must not be null. const OutlineButton({ Key key, @@ -68,8 +68,10 @@ class OutlineButton extends StatefulWidget { this.highlightedBorderColor, this.padding, this.shape, + this.clipBehavior = Clip.none, this.child, }) : assert(highlightElevation != null && highlightElevation >= 0.0), + assert(clipBehavior != null), super(key: key); /// Create an outline button from a pair of widgets that serve as the button's @@ -78,7 +80,8 @@ class OutlineButton extends StatefulWidget { /// The icon and label are arranged in a row and padded by 12 logical pixels /// at the start, and 16 at the end, with an 8 pixel gap in between. /// - /// The [highlightElevation], [icon], and [label] must not be null. + /// The [highlightElevation], [icon], [label], and [clipBehavior] must not be + /// null. OutlineButton.icon({ Key key, @required this.onPressed, @@ -93,11 +96,13 @@ class OutlineButton extends StatefulWidget { this.disabledBorderColor, this.highlightedBorderColor, this.shape, + this.clipBehavior = Clip.none, @required Widget icon, @required Widget label, }) : assert(highlightElevation != null && highlightElevation >= 0.0), assert(icon != null), assert(label != null), + assert(clipBehavior != null), padding = const EdgeInsetsDirectional.only(start: 12.0, end: 16.0), child = new Row( mainAxisSize: MainAxisSize.min, @@ -223,6 +228,9 @@ class OutlineButton extends StatefulWidget { /// shape as well. final ShapeBorder shape; + /// {@macro flutter.widgets.Clip} + final Clip clipBehavior; + /// The button's label. /// /// Often a [Text] widget in all caps. @@ -415,6 +423,7 @@ class _OutlineButtonState extends State with SingleTickerProvider shape: widget.shape ?? buttonTheme.shape, side: _getOutline(theme, buttonTheme), ), + clipBehavior: widget.clipBehavior, animationDuration: _kElevationDuration, child: widget.child, ); diff --git a/packages/flutter/lib/src/material/raised_button.dart b/packages/flutter/lib/src/material/raised_button.dart index f53c9915ab..7b06c05183 100644 --- a/packages/flutter/lib/src/material/raised_button.dart +++ b/packages/flutter/lib/src/material/raised_button.dart @@ -44,8 +44,8 @@ import 'theme_data.dart'; class RaisedButton extends StatelessWidget { /// Create a filled button. /// - /// The [elevation], [highlightElevation], and [disabledElevation] - /// arguments must not be null. + /// The [elevation], [highlightElevation], [disabledElevation], and + /// [clipBehavior] arguments must not be null. const RaisedButton({ Key key, @required this.onPressed, @@ -63,6 +63,7 @@ class RaisedButton extends StatelessWidget { this.disabledElevation = 0.0, this.padding, this.shape, + this.clipBehavior = Clip.none, this.materialTapTargetSize, this.animationDuration = kThemeChangeDuration, this.child, @@ -70,6 +71,7 @@ class RaisedButton extends StatelessWidget { assert(highlightElevation != null), assert(disabledElevation != null), assert(animationDuration != null), + assert(clipBehavior != null), super(key: key); /// Create a filled button from a pair of widgets that serve as the button's @@ -78,8 +80,8 @@ class RaisedButton extends StatelessWidget { /// The icon and label are arranged in a row and padded by 12 logical pixels /// at the start, and 16 at the end, with an 8 pixel gap in between. /// - /// The [elevation], [highlightElevation], [disabledElevation], [icon], and - /// [label] arguments must not be null. + /// The [elevation], [highlightElevation], [disabledElevation], [icon], + /// [label], and [clipBehavior] arguments must not be null. RaisedButton.icon({ Key key, @required this.onPressed, @@ -96,6 +98,7 @@ class RaisedButton extends StatelessWidget { this.highlightElevation = 8.0, this.disabledElevation = 0.0, this.shape, + this.clipBehavior = Clip.none, this.materialTapTargetSize, this.animationDuration = kThemeChangeDuration, @required Widget icon, @@ -106,6 +109,7 @@ class RaisedButton extends StatelessWidget { assert(icon != null), assert(label != null), assert(animationDuration != null), + assert(clipBehavior != null), padding = const EdgeInsetsDirectional.only(start: 12.0, end: 16.0), child = new Row( mainAxisSize: MainAxisSize.min, @@ -287,6 +291,9 @@ class RaisedButton extends StatelessWidget { /// shape as well. final ShapeBorder shape; + /// {@macro flutter.widgets.Clip} + final Clip clipBehavior; + /// Defines the duration of animated changes for [shape] and [elevation]. /// /// The default value is [kThemeChangeDuration]. @@ -390,6 +397,7 @@ class RaisedButton extends StatelessWidget { padding: padding ?? buttonTheme.padding, constraints: buttonTheme.constraints, shape: shape ?? buttonTheme.shape, + clipBehavior: clipBehavior, animationDuration: animationDuration, child: child, materialTapTargetSize: materialTapTargetSize ?? theme.materialTapTargetSize, diff --git a/packages/flutter/lib/src/painting/basic_types.dart b/packages/flutter/lib/src/painting/basic_types.dart index 5a4361a2cb..b3ca604e7f 100644 --- a/packages/flutter/lib/src/painting/basic_types.dart +++ b/packages/flutter/lib/src/painting/basic_types.dart @@ -8,6 +8,7 @@ export 'dart:ui' show BlendMode, BlurStyle, Canvas, + Clip, Color, ColorFilter, FilterQuality, diff --git a/packages/flutter/lib/src/rendering/layer.dart b/packages/flutter/lib/src/rendering/layer.dart index c62ae7aac7..f2db334aac 100644 --- a/packages/flutter/lib/src/rendering/layer.dart +++ b/packages/flutter/lib/src/rendering/layer.dart @@ -5,7 +5,6 @@ import 'dart:async'; import 'dart:collection'; import 'dart:ui' as ui show Image, ImageFilter, Picture, Scene, SceneBuilder; -import 'dart:ui' show Clip, Offset, defaultClipBehavior; // ignore: deprecated_member_use import 'package:flutter/foundation.dart'; import 'package:flutter/painting.dart'; @@ -626,7 +625,11 @@ class ClipRectLayer extends ContainerLayer { /// (as described at [Layer]). Rect clipRect; - /// {@macro flutter.clipper.clipBehavior} + /// {@template flutter.clipper.clipBehavior} + /// Controls how to clip (default to [Clip.antiAlias]). + /// + /// [Clip.none] is not allowed here. + /// {@endtemplate} Clip get clipBehavior => _clipBehavior; Clip _clipBehavior; set clipBehavior(Clip value) { @@ -967,7 +970,7 @@ class PhysicalModelLayer extends ContainerLayer { /// The [clipPath], [elevation], and [color] arguments must not be null. PhysicalModelLayer({ @required this.clipPath, - this.clipBehavior = defaultClipBehavior, // ignore: deprecated_member_use + this.clipBehavior = Clip.none, @required this.elevation, @required this.color, @required this.shadowColor, diff --git a/packages/flutter/lib/src/rendering/object.dart b/packages/flutter/lib/src/rendering/object.dart index f6faa698c4..eca523b80f 100644 --- a/packages/flutter/lib/src/rendering/object.dart +++ b/packages/flutter/lib/src/rendering/object.dart @@ -4,7 +4,6 @@ import 'dart:developer'; import 'dart:ui' as ui show PictureRecorder; -import 'dart:ui' show Clip; import 'package:flutter/animation.dart'; import 'package:flutter/foundation.dart'; diff --git a/packages/flutter/lib/src/rendering/proxy_box.dart b/packages/flutter/lib/src/rendering/proxy_box.dart index bc02349a94..a142d9763b 100644 --- a/packages/flutter/lib/src/rendering/proxy_box.dart +++ b/packages/flutter/lib/src/rendering/proxy_box.dart @@ -5,7 +5,6 @@ import 'dart:async'; import 'dart:ui' as ui show ImageFilter, Gradient, Image; -import 'dart:ui' show Clip, defaultClipBehavior; // ignore: deprecated_member_use import 'package:flutter/animation.dart'; import 'package:flutter/foundation.dart'; @@ -1118,8 +1117,8 @@ abstract class _RenderCustomClip extends RenderProxyBox { _RenderCustomClip({ RenderBox child, CustomClipper clipper, - this.clipBehavior = defaultClipBehavior, // ignore: deprecated_member_use - }) : _clipper = clipper, assert(clipBehavior != null), assert(clipBehavior != Clip.none), super(child); + this.clipBehavior = Clip.antiAlias, + }) : _clipper = clipper, assert(clipBehavior != null), super(child); /// If non-null, determines which clip to use on the child. CustomClipper get clipper => _clipper; @@ -1162,11 +1161,6 @@ abstract class _RenderCustomClip extends RenderProxyBox { T get _defaultClip; T _clip; - /// {@template flutter.clipper.clipBehavior} - /// Controls how to clip (default to [Clip.antiAlias]). - /// - /// [Clip.none] is not allowed here. - /// {@endtemplate} final Clip clipBehavior; @override @@ -1227,6 +1221,8 @@ class RenderClipRect extends _RenderCustomClip { /// /// If [clipper] is null, the clip will match the layout size and position of /// the child. + /// + /// The [clipBehavior] cannot be [Clip.none]. RenderClipRect({ RenderBox child, CustomClipper clipper, @@ -1280,12 +1276,14 @@ class RenderClipRRect extends _RenderCustomClip { /// right-angled corners. /// /// If [clipper] is non-null, then [borderRadius] is ignored. + /// + /// The [clipBehavior] cannot be [Clip.none]. RenderClipRRect({ RenderBox child, BorderRadius borderRadius = BorderRadius.zero, CustomClipper clipper, Clip clipBehavior = Clip.antiAlias, - }) : _borderRadius = borderRadius, super(child: child, clipper: clipper, clipBehavior: clipBehavior) { + }) : assert(clipBehavior != Clip.none), _borderRadius = borderRadius, super(child: child, clipper: clipper, clipBehavior: clipBehavior) { assert(_borderRadius != null || clipper != null); } @@ -1350,11 +1348,13 @@ class RenderClipOval extends _RenderCustomClip { /// /// If [clipper] is null, the oval will be inscribed into the layout size and /// position of the child. + /// + /// The [clipBehavior] cannot be [Clip.none]. RenderClipOval({ RenderBox child, CustomClipper clipper, Clip clipBehavior = Clip.antiAlias, - }) : super(child: child, clipper: clipper, clipBehavior: clipBehavior); + }) : assert(clipBehavior != Clip.none), super(child: child, clipper: clipper, clipBehavior: clipBehavior); Rect _cachedRect; Path _cachedPath; @@ -1423,11 +1423,13 @@ class RenderClipPath extends _RenderCustomClip { /// size and location of the child. However, rather than use this default, /// consider using a [RenderClipRect], which can achieve the same effect more /// efficiently. + /// + /// The [clipBehavior] cannot be [Clip.none]. RenderClipPath({ RenderBox child, CustomClipper clipper, Clip clipBehavior = Clip.antiAlias, - }) : super(child: child, clipper: clipper, clipBehavior: clipBehavior); + }) : assert(clipBehavior != Clip.none), super(child: child, clipper: clipper, clipBehavior: clipBehavior); @override Path get _defaultClip => new Path()..addRect(Offset.zero & size); @@ -1475,7 +1477,7 @@ abstract class _RenderPhysicalModelBase extends _RenderCustomClip { @required double elevation, @required Color color, @required Color shadowColor, - Clip clipBehavior = defaultClipBehavior, // ignore: deprecated_member_use + Clip clipBehavior = Clip.none, CustomClipper clipper, }) : assert(elevation != null), assert(color != null), @@ -1554,7 +1556,7 @@ class RenderPhysicalModel extends _RenderPhysicalModelBase { RenderPhysicalModel({ RenderBox child, BoxShape shape = BoxShape.rectangle, - Clip clipBehavior = defaultClipBehavior, // ignore: deprecated_member_use + Clip clipBehavior = Clip.none, BorderRadius borderRadius, double elevation = 0.0, @required Color color, @@ -1713,7 +1715,7 @@ class RenderPhysicalShape extends _RenderPhysicalModelBase { RenderPhysicalShape({ RenderBox child, @required CustomClipper clipper, - Clip clipBehavior = defaultClipBehavior, // ignore: deprecated_member_use + Clip clipBehavior = Clip.none, double elevation = 0.0, @required Color color, Color shadowColor = const Color(0xFF000000), diff --git a/packages/flutter/lib/src/widgets/basic.dart b/packages/flutter/lib/src/widgets/basic.dart index 7dbb7a9ee8..4967718188 100644 --- a/packages/flutter/lib/src/widgets/basic.dart +++ b/packages/flutter/lib/src/widgets/basic.dart @@ -3,7 +3,6 @@ // found in the LICENSE file. import 'dart:ui' as ui show Image, ImageFilter; -import 'dart:ui' show Clip, defaultClipBehavior; // ignore: deprecated_member_use import 'package:flutter/foundation.dart'; import 'package:flutter/rendering.dart'; @@ -692,7 +691,7 @@ class PhysicalModel extends SingleChildRenderObjectWidget { const PhysicalModel({ Key key, this.shape = BoxShape.rectangle, - this.clipBehavior = defaultClipBehavior, // ignore: deprecated_member_use + this.clipBehavior = Clip.none, this.borderRadius, this.elevation = 0.0, @required this.color, @@ -780,7 +779,7 @@ class PhysicalShape extends SingleChildRenderObjectWidget { const PhysicalShape({ Key key, @required this.clipper, - this.clipBehavior = defaultClipBehavior, // ignore: deprecated_member_use + this.clipBehavior = Clip.none, this.elevation = 0.0, @required this.color, this.shadowColor = const Color(0xFF000000), diff --git a/packages/flutter/lib/src/widgets/implicit_animations.dart b/packages/flutter/lib/src/widgets/implicit_animations.dart index 8fc79f11e9..67eb0fc831 100644 --- a/packages/flutter/lib/src/widgets/implicit_animations.dart +++ b/packages/flutter/lib/src/widgets/implicit_animations.dart @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:ui' show Clip, defaultClipBehavior; // ignore: deprecated_member_use - import 'package:flutter/animation.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/rendering.dart'; @@ -1183,7 +1181,7 @@ class AnimatedPhysicalModel extends ImplicitlyAnimatedWidget { Key key, @required this.child, @required this.shape, - this.clipBehavior = defaultClipBehavior, // ignore: deprecated_member_use + this.clipBehavior = Clip.none, this.borderRadius = BorderRadius.zero, @required this.elevation, @required this.color, diff --git a/packages/flutter/test/material/buttons_test.dart b/packages/flutter/test/material/buttons_test.dart index 10043988b4..053f5250d8 100644 --- a/packages/flutter/test/material/buttons_test.dart +++ b/packages/flutter/test/material/buttons_test.dart @@ -179,6 +179,7 @@ void main() { splashColor: directSplashColor, highlightColor: directHighlightColor, onPressed: () { /* to make sure the button is enabled */ }, + clipBehavior: Clip.antiAlias, ), ), ); @@ -224,6 +225,7 @@ void main() { child: new Center( child: new MaterialButton( onPressed: () { /* to make sure the button is enabled */ }, + clipBehavior: Clip.antiAlias, ), ), ); @@ -280,6 +282,35 @@ void main() { await gesture.up(); }); + testWidgets('MaterialButton has no clip by default', (WidgetTester tester) async { + final GlobalKey buttonKey = new GlobalKey(); + final Widget buttonWidget = new Material( + child: new Center( + child: new MaterialButton( + key: buttonKey, + onPressed: () { /* to make sure the button is enabled */ }, + ), + ), + ); + + await tester.pumpWidget( + new Directionality( + textDirection: TextDirection.ltr, + child: new Theme( + data: new ThemeData( + materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, + ), + child: buttonWidget, + ), + ), + ); + + expect( + tester.renderObject(find.byKey(buttonKey)), + paintsExactlyCountTimes(#clipPath, 0) + ); + }); + testWidgets('Disabled MaterialButton has same semantic size as enabled and exposes disabled semantics', (WidgetTester tester) async { final SemanticsTester semantics = new SemanticsTester(tester); diff --git a/packages/flutter/test/material/material_test.dart b/packages/flutter/test/material/material_test.dart index 22e01c5731..a656f052c3 100644 --- a/packages/flutter/test/material/material_test.dart +++ b/packages/flutter/test/material/material_test.dart @@ -171,27 +171,42 @@ void main() { }); group('Transparency clipping', () { - testWidgets('clips to bounding rect by default', (WidgetTester tester) async { + testWidgets('No clip by default', (WidgetTester tester) async { + final GlobalKey materialKey = new GlobalKey(); + await tester.pumpWidget( + new Material( + key: materialKey, + type: MaterialType.transparency, + child: const SizedBox(width: 100.0, height: 100.0), + ) + ); + + expect(find.byKey(materialKey), hasNoImmediateClip); + }); + + testWidgets('clips to bounding rect by default given Clip.antiAlias', (WidgetTester tester) async { final GlobalKey materialKey = new GlobalKey(); await tester.pumpWidget( new Material( key: materialKey, type: MaterialType.transparency, - child: const SizedBox(width: 100.0, height: 100.0) + child: const SizedBox(width: 100.0, height: 100.0), + clipBehavior: Clip.antiAlias, ) ); expect(find.byKey(materialKey), clipsWithBoundingRect); }); - testWidgets('clips to rounded rect when borderRadius provided', (WidgetTester tester) async { + testWidgets('clips to rounded rect when borderRadius provided given Clip.antiAlias', (WidgetTester tester) async { final GlobalKey materialKey = new GlobalKey(); await tester.pumpWidget( new Material( key: materialKey, type: MaterialType.transparency, borderRadius: const BorderRadius.all(Radius.circular(10.0)), - child: const SizedBox(width: 100.0, height: 100.0) + child: const SizedBox(width: 100.0, height: 100.0), + clipBehavior: Clip.antiAlias, ) ); @@ -203,14 +218,15 @@ void main() { ); }); - testWidgets('clips to shape when provided', (WidgetTester tester) async { + testWidgets('clips to shape when provided given Clip.antiAlias', (WidgetTester tester) async { final GlobalKey materialKey = new GlobalKey(); await tester.pumpWidget( new Material( key: materialKey, type: MaterialType.transparency, shape: const StadiumBorder(), - child: const SizedBox(width: 100.0, height: 100.0) + child: const SizedBox(width: 100.0, height: 100.0), + clipBehavior: Clip.antiAlias, ) ); diff --git a/packages/flutter/test/material/outline_button_test.dart b/packages/flutter/test/material/outline_button_test.dart index 562068275b..89ad0ae3ed 100644 --- a/packages/flutter/test/material/outline_button_test.dart +++ b/packages/flutter/test/material/outline_button_test.dart @@ -60,6 +60,7 @@ void main() { alignment: Alignment.topLeft, child: OutlineButton( shape: const RoundedRectangleBorder(), // default border radius is 0 + clipBehavior: Clip.antiAlias, color: fillColor, highlightedBorderColor: highlightedBorderColor, disabledBorderColor: disabledBorderColor, @@ -135,6 +136,29 @@ void main() { ..path(color: borderColor, strokeWidth: borderWidth)); }); + testWidgets('OutlineButton has no clip by default', (WidgetTester tester) async { + final GlobalKey buttonKey = new GlobalKey(); + await tester.pumpWidget( + new Directionality( + textDirection: TextDirection.ltr, + child: new Material( + child: new Center( + child: new OutlineButton( + key: buttonKey, + onPressed: () { }, + child: const Text('ABC'), + ), + ), + ), + ), + ); + + expect( + tester.renderObject(find.byKey(buttonKey)), + paintsExactlyCountTimes(#clipPath, 0) + ); + }); + testWidgets('OutlineButton contributes semantics', (WidgetTester tester) async { final SemanticsTester semantics = new SemanticsTester(tester); await tester.pumpWidget( diff --git a/packages/flutter/test/rendering/recording_canvas.dart b/packages/flutter/test/rendering/recording_canvas.dart index 16879e913b..6eb6b7006b 100644 --- a/packages/flutter/test/rendering/recording_canvas.dart +++ b/packages/flutter/test/rendering/recording_canvas.dart @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:ui' show Clip; - import 'package:flutter/foundation.dart'; import 'package:flutter/rendering.dart'; diff --git a/packages/flutter/test/widgets/clip_test.dart b/packages/flutter/test/widgets/clip_test.dart index 1beb365945..6ed6a5237e 100644 --- a/packages/flutter/test/widgets/clip_test.dart +++ b/packages/flutter/test/widgets/clip_test.dart @@ -431,7 +431,70 @@ void main() { ); }); - testWidgets('PhysicalModel painting', (WidgetTester tester) async { + Center genPhysicalModel(Clip clipBehavior) { + return new Center( + child: new RepaintBoundary( + child: new Container( + color: Colors.white, + child: new Padding( + padding: const EdgeInsets.all(100.0), + child: new SizedBox( + height: 100.0, + width: 100.0, + child: new Transform.rotate( + angle: 1.0, // radians + child: new PhysicalModel( + borderRadius: new BorderRadius.circular(20.0), + color: Colors.red, + clipBehavior: clipBehavior, + child: new Container( + color: Colors.white, + child: new RepaintBoundary( + child: new Center( + child: new Container( + color: Colors.black, + height: 10.0, + width: 10.0, + ), + ), + ), + ), + ), + ), + ), + ), + ), + ), + ); + } + + testWidgets('PhysicalModel painting with Clip.antiAlias', (WidgetTester tester) async { + await tester.pumpWidget(genPhysicalModel(Clip.antiAlias)); + await expectLater( + find.byType(RepaintBoundary).first, + matchesGoldenFile('clip.PhysicalModel.antiAlias.png'), + ); + }); + + testWidgets('PhysicalModel painting with Clip.hardEdge', (WidgetTester tester) async { + await tester.pumpWidget(genPhysicalModel(Clip.hardEdge)); + await expectLater( + find.byType(RepaintBoundary).first, + matchesGoldenFile('clip.PhysicalModel.hardEdge.png'), + ); + }); + + // There will be bleeding edges on the rect edges, but there shouldn't be any bleeding edges on the + // round corners. + testWidgets('PhysicalModel painting with Clip.antiAliasWithSaveLayer', (WidgetTester tester) async { + await tester.pumpWidget(genPhysicalModel(Clip.antiAliasWithSaveLayer)); + await expectLater( + find.byType(RepaintBoundary).first, + matchesGoldenFile('clip.PhysicalModel.antiAliasWithSaveLayer.png'), + ); + }); + + testWidgets('Default PhysicalModel painting', (WidgetTester tester) async { await tester.pumpWidget( new Center( child: new RepaintBoundary( @@ -469,7 +532,72 @@ void main() { ); await expectLater( find.byType(RepaintBoundary).first, - matchesGoldenFile('clip.PhysicalModel.1.png'), + matchesGoldenFile('clip.PhysicalModel.default.png'), + ); + }); + + Center genPhysicalShape(Clip clipBehavior) { + return new Center( + child: new RepaintBoundary( + child: new Container( + color: Colors.white, + child: new Padding( + padding: const EdgeInsets.all(100.0), + child: new SizedBox( + height: 100.0, + width: 100.0, + child: new Transform.rotate( + angle: 1.0, // radians + child: new PhysicalShape( + clipper: new ShapeBorderClipper( + shape: new BeveledRectangleBorder( + borderRadius: new BorderRadius.circular(20.0), + ), + ), + clipBehavior: clipBehavior, + color: Colors.red, + child: new Container( + color: Colors.white, + child: new RepaintBoundary( + child: new Center( + child: new Container( + color: Colors.black, + height: 10.0, + width: 10.0, + ), + ), + ), + ), + ), + ), + ), + ), + ), + ), + ); + } + + testWidgets('PhysicalShape painting with Clip.antiAlias', (WidgetTester tester) async { + await tester.pumpWidget(genPhysicalShape(Clip.antiAlias)); + await expectLater( + find.byType(RepaintBoundary).first, + matchesGoldenFile('clip.PhysicalShape.antiAlias.png'), + ); + }); + + testWidgets('PhysicalShape painting with Clip.hardEdge', (WidgetTester tester) async { + await tester.pumpWidget(genPhysicalShape(Clip.hardEdge)); + await expectLater( + find.byType(RepaintBoundary).first, + matchesGoldenFile('clip.PhysicalShape.hardEdge.png'), + ); + }); + + testWidgets('PhysicalShape painting with Clip.antiAliasWithSaveLayer', (WidgetTester tester) async { + await tester.pumpWidget(genPhysicalShape(Clip.antiAliasWithSaveLayer)); + await expectLater( + find.byType(RepaintBoundary).first, + matchesGoldenFile('clip.PhysicalShape.antiAliasWithSaveLayer.png'), ); }); @@ -515,7 +643,7 @@ void main() { ); await expectLater( find.byType(RepaintBoundary).first, - matchesGoldenFile('clip.PhysicalShape.1.png'), + matchesGoldenFile('clip.PhysicalShape.default.png'), ); }); } diff --git a/packages/flutter_test/lib/src/matchers.dart b/packages/flutter_test/lib/src/matchers.dart index d489051be4..f88354ed10 100644 --- a/packages/flutter_test/lib/src/matchers.dart +++ b/packages/flutter_test/lib/src/matchers.dart @@ -1046,6 +1046,11 @@ class _IsMethodCall extends Matcher { /// [RenderClipPath]. const Matcher clipsWithBoundingRect = _ClipsWithBoundingRect(); +/// Asserts that a [Finder] locates a single object whose root RenderObject is +/// not a [RenderClipRect], [RenderClipRRect], [RenderClipOval], or +/// [RenderClipPath]. +const Matcher hasNoImmediateClip = _MatchAnythingExceptClip(); + /// Asserts that a [Finder] locates a single object whose root RenderObject /// is a [RenderClipRRect] with no clipper set, and border radius equals to /// [borderRadius], or an equivalent [RenderClipPath]. @@ -1104,7 +1109,53 @@ Matcher rendersOnPhysicalShape({ ); } -abstract class _MatchRenderObject extends Matcher { +abstract class _FailWithDescriptionMatcher extends Matcher { + const _FailWithDescriptionMatcher(); + + bool failWithDescription(Map matchState, String description) { + matchState['failure'] = description; + return false; + } + + @override + Description describeMismatch( + dynamic item, + Description mismatchDescription, + Map matchState, + bool verbose + ) { + return mismatchDescription.add(matchState['failure']); + } +} + +class _MatchAnythingExceptClip extends _FailWithDescriptionMatcher { + const _MatchAnythingExceptClip(); + + @override + bool matches(covariant Finder finder, Map matchState) { + final Iterable nodes = finder.evaluate(); + if (nodes.length != 1) + return failWithDescription(matchState, 'did not have a exactly one child element'); + final RenderObject renderObject = nodes.single.renderObject; + + switch (renderObject.runtimeType) { + case RenderClipPath: + case RenderClipOval: + case RenderClipRect: + case RenderClipRRect: + return failWithDescription(matchState, 'had a root render object of type: ${renderObject.runtimeType}'); + default: + return true; + } + } + + @override + Description describe(Description description) { + description.add('does not have a clip as an immediate child'); + } +} + +abstract class _MatchRenderObject extends _FailWithDescriptionMatcher { const _MatchRenderObject(); bool renderObjectMatchesT(Map matchState, T renderObject); @@ -1125,21 +1176,6 @@ abstract class _MatchRenderObject matchState, String description) { - matchState['failure'] = description; - return false; - } - - @override - Description describeMismatch( - dynamic item, - Description mismatchDescription, - Map matchState, - bool verbose - ) { - return mismatchDescription.add(matchState['failure']); - } } class _RendersOnPhysicalModel extends _MatchRenderObject {