From c86517c0bbc2388c99b226e4b724850ffd087d04 Mon Sep 17 00:00:00 2001 From: wise86-android Date: Tue, 7 Jan 2020 21:13:02 +0100 Subject: [PATCH] [issue 7293] add test for rettangle box decoration with boarder radius (#47915) --- .../test/widgets/box_decoration_test.dart | 216 +++++++++++++++++- 1 file changed, 215 insertions(+), 1 deletion(-) diff --git a/packages/flutter/test/widgets/box_decoration_test.dart b/packages/flutter/test/widgets/box_decoration_test.dart index ef35ee912d..bf42da188a 100644 --- a/packages/flutter/test/widgets/box_decoration_test.dart +++ b/packages/flutter/test/widgets/box_decoration_test.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'dart:async'; +import 'dart:math' as math; import 'dart:typed_data'; import 'dart:ui' as ui show Image; @@ -342,4 +343,217 @@ Future main() async { }); -} + testWidgets('Can hit test on BoxDecoration border', (WidgetTester tester) async { + List itemsTapped; + const Key key = Key('Container with BoxDecoration'); + Widget buildFrame(Border border) { + itemsTapped = []; + return Center( + child: GestureDetector( + behavior: HitTestBehavior.deferToChild, + child: Container( + key: key, + width: 100.0, + height: 50.0, + decoration: BoxDecoration(border: border, shape: BoxShape.rectangle, borderRadius: BorderRadius.circular(20.0)), + ), + onTap: () { + itemsTapped.add(1); + }, + ), + ); + } + + await tester.pumpWidget(buildFrame(Border.all())); + + expect(itemsTapped, isEmpty); + + await tester.tapAt(const Offset(0.0, 0.0)); + expect(itemsTapped, isEmpty); + + await tester.tapAt(const Offset(350.0, 275.0)); + expect(itemsTapped, isEmpty); + + await tester.tapAt(const Offset(400.0, 300.0)); + expect(itemsTapped, [1]); + + await tester.tap(find.byKey(key)); + expect(itemsTapped, [1,1]); + }); + + testWidgets('BoxDecoration not tap outside rounded angles - Top Left', (WidgetTester tester) async { + const double height = 50.0; + const double width = 50.0; + const double radius = 12.3; + + List itemsTapped; + const Key key = Key('Container with BoxDecoration'); + Widget buildFrame(Border border) { + itemsTapped = []; + return Align( + alignment: Alignment.topLeft, + child:GestureDetector( + behavior: HitTestBehavior.deferToChild, + child: Container( + key: key, + width: width, + height: height, + decoration: BoxDecoration(border: border, shape: BoxShape.rectangle,borderRadius: BorderRadius.circular(radius)) + ), + onTap: () { + itemsTapped.add(1); + }, + ) + ); + } + + await tester.pumpWidget(buildFrame(Border.all())); + + expect(itemsTapped, isEmpty); + // x, y + const Offset topLeft = Offset(0.0, 0.0); + const Offset borderTopTangent = Offset(radius-1, 0.0); + const Offset borderLeftTangent = Offset(0.0,radius-1); + //the borderDiagonalOffset is the backslash line + //\\######@@@ + //#\\###@#### + //##\\@###### + //##@######## + //@########## + //@########## + const double borderDiagonalOffset = radius - radius * math.sqrt1_2; + const Offset fartherBorderRadiusPoint = Offset(borderDiagonalOffset,borderDiagonalOffset); + + await tester.tapAt(topLeft); + expect(itemsTapped, isEmpty,reason: 'top left tapped'); + + await tester.tapAt(borderTopTangent); + expect(itemsTapped, isEmpty,reason: 'border top tapped'); + + await tester.tapAt(borderLeftTangent); + expect(itemsTapped, isEmpty,reason: 'border left tapped'); + + await tester.tapAt(fartherBorderRadiusPoint); + expect(itemsTapped, isEmpty,reason: 'border center tapped'); + + await tester.tap(find.byKey(key)); + expect(itemsTapped, [1]); + + }); + + testWidgets('BoxDecoration tap inside rounded angles - Top Left', (WidgetTester tester) async { + const double height = 50.0; + const double width = 50.0; + const double radius = 12.3; + + List itemsTapped; + const Key key = Key('Container with BoxDecoration'); + Widget buildFrame(Border border) { + itemsTapped = []; + return Align( + alignment: Alignment.topLeft, + child:GestureDetector( + behavior: HitTestBehavior.deferToChild, + child: Container( + key: key, + width: width, + height: height, + decoration: BoxDecoration(border: border, shape: BoxShape.rectangle,borderRadius: BorderRadius.circular(radius)) + ), + onTap: () { + itemsTapped.add(1); + }, + ) + ); + } + + await tester.pumpWidget(buildFrame(Border.all())); + + expect(itemsTapped, isEmpty); + // x, y + const Offset borderTopTangent = Offset(radius, 0.0); + const Offset borderLeftTangent = Offset(0.0,radius); + const double borderDiagonalOffset = radius - radius * math.sqrt1_2; + const Offset fartherBorderRadiusPoint = Offset(borderDiagonalOffset+1,borderDiagonalOffset+1); + + await tester.tapAt(borderTopTangent); + expect(itemsTapped, [1],reason: 'border Top not tapped'); + + await tester.tapAt(borderLeftTangent); + expect(itemsTapped, [1,1],reason: 'border Left not tapped'); + + await tester.tapAt(fartherBorderRadiusPoint); + expect(itemsTapped, [1,1,1],reason: 'border center not tapped'); + + await tester.tap(find.byKey(key)); + expect(itemsTapped, [1,1,1,1]); + }); + + testWidgets('BoxDecoration rounded angles other corner works', (WidgetTester tester) async { + const double height = 50.0; + const double width = 50.0; + const double radius = 20; + + List itemsTapped; + const Key key = Key('Container with BoxDecoration'); + Widget buildFrame(Border border) { + itemsTapped = []; + return Align( + alignment: Alignment.topLeft, + child:GestureDetector( + behavior: HitTestBehavior.deferToChild, + child: Container( + key: key, + width: width, + height: height, + decoration: BoxDecoration(border: border, shape: BoxShape.rectangle,borderRadius: BorderRadius.circular(radius)) + ), + onTap: () { + itemsTapped.add(1); + }, + ) + ); + } + + await tester.pumpWidget(buildFrame(Border.all())); + + expect(itemsTapped, isEmpty); + + await tester.tap(find.byKey(key)); + expect(itemsTapped, [1]); + + // x, y + const Offset topRightOutside = Offset(width, 0.0); + const Offset topRightInside = Offset(width-radius, radius); + const Offset bottomRightOutside = Offset(width, height); + const Offset bottomRightInside = Offset(width-radius, height-radius); + const Offset bottomLeftOutside = Offset(0, height); + const Offset bottomLeftInside = Offset(radius, height-radius); + const Offset topLeftOutside = Offset(0, 0); + const Offset topLeftInside = Offset(radius, radius); + + await tester.tapAt(topRightInside); + expect(itemsTapped, [1,1],reason: 'top right not tapped'); + + await tester.tapAt(topRightOutside); + expect(itemsTapped, [1,1],reason: 'top right tapped'); + + await tester.tapAt(bottomRightInside); + expect(itemsTapped, [1,1,1],reason: 'bottom right not tapped'); + + await tester.tapAt(bottomRightOutside); + expect(itemsTapped, [1,1,1],reason: 'bottom right tapped'); + + await tester.tapAt(bottomLeftInside); + expect(itemsTapped, [1,1,1,1],reason: 'bottom left not tapped'); + + await tester.tapAt(bottomLeftOutside); + expect(itemsTapped, [1,1,1,1],reason: 'bottom left tapped'); + + await tester.tapAt(topLeftInside); + expect(itemsTapped, [1,1,1,1,1],reason: 'top left not tapped'); + + await tester.tapAt(topLeftOutside); + expect(itemsTapped, [1,1,1,1,1],reason: 'top left tapped'); + }); +} \ No newline at end of file