// Copyright 2017 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_test/flutter_test.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter/rendering.dart'; void main() { group('PhysicalShape', () { testWidgets('properties', (WidgetTester tester) async { await tester.pumpWidget( const PhysicalShape( clipper: ShapeBorderClipper(shape: CircleBorder()), elevation: 2.0, color: Color(0xFF0000FF), shadowColor: Color(0xFF00FF00), ) ); final RenderPhysicalShape renderObject = tester.renderObject(find.byType(PhysicalShape)); expect(renderObject.clipper, const ShapeBorderClipper(shape: CircleBorder())); expect(renderObject.color, const Color(0xFF0000FF)); expect(renderObject.shadowColor, const Color(0xFF00FF00)); expect(renderObject.elevation, 2.0); }); testWidgets('hit test', (WidgetTester tester) async { await tester.pumpWidget( PhysicalShape( clipper: const ShapeBorderClipper(shape: CircleBorder()), elevation: 2.0, color: const Color(0xFF0000FF), shadowColor: const Color(0xFF00FF00), child: Container(color: const Color(0xFF0000FF)), ) ); final RenderPhysicalShape renderPhysicalShape = tester.renderObject(find.byType(PhysicalShape)); // The viewport is 800x600, the CircleBorder is centered and fits // the shortest edge, so we get a circle of radius 300, centered at // (400, 300). // // We test by sampling a few points around the left-most point of the // circle (100, 300). expect(tester.hitTestOnBinding(const Offset(99.0, 300.0)), doesNotHit(renderPhysicalShape)); expect(tester.hitTestOnBinding(const Offset(100.0, 300.0)), hits(renderPhysicalShape)); expect(tester.hitTestOnBinding(const Offset(100.0, 299.0)), doesNotHit(renderPhysicalShape)); expect(tester.hitTestOnBinding(const Offset(100.0, 301.0)), doesNotHit(renderPhysicalShape)); }); }); group('FractionalTranslation', () { testWidgets('hit test - entirely inside the bounding box', (WidgetTester tester) async { final GlobalKey key1 = GlobalKey(); bool _pointerDown = false; await tester.pumpWidget( Center( child: FractionalTranslation( translation: Offset.zero, transformHitTests: true, child: Listener( onPointerDown: (PointerDownEvent event) { _pointerDown = true; }, child: SizedBox( key: key1, width: 100.0, height: 100.0, child: Container( color: const Color(0xFF0000FF) ), ), ), ), ) ); expect(_pointerDown, isFalse); await tester.tap(find.byKey(key1)); expect(_pointerDown, isTrue); }); testWidgets('hit test - partially inside the bounding box', (WidgetTester tester) async { final GlobalKey key1 = GlobalKey(); bool _pointerDown = false; await tester.pumpWidget( Center( child: FractionalTranslation( translation: const Offset(0.5, 0.5), transformHitTests: true, child: Listener( onPointerDown: (PointerDownEvent event) { _pointerDown = true; }, child: SizedBox( key: key1, width: 100.0, height: 100.0, child: Container( color: const Color(0xFF0000FF) ), ), ), ), ) ); expect(_pointerDown, isFalse); await tester.tap(find.byKey(key1)); expect(_pointerDown, isTrue); }); testWidgets('hit test - completely outside the bounding box', (WidgetTester tester) async { final GlobalKey key1 = GlobalKey(); bool _pointerDown = false; await tester.pumpWidget( Center( child: FractionalTranslation( translation: const Offset(1.0, 1.0), transformHitTests: true, child: Listener( onPointerDown: (PointerDownEvent event) { _pointerDown = true; }, child: SizedBox( key: key1, width: 100.0, height: 100.0, child: Container( color: const Color(0xFF0000FF) ), ), ), ), ) ); expect(_pointerDown, isFalse); await tester.tap(find.byKey(key1)); expect(_pointerDown, isTrue); }); }); test('UnconstrainedBox toString', () { expect( const UnconstrainedBox(constrainedAxis: Axis.vertical,).toString(), equals('UnconstrainedBox(alignment: center, constrainedAxis: vertical)'), ); expect( const UnconstrainedBox(constrainedAxis: Axis.horizontal, textDirection: TextDirection.rtl, alignment: Alignment.topRight).toString(), equals('UnconstrainedBox(alignment: topRight, constrainedAxis: horizontal, textDirection: rtl)'), ); }); } HitsRenderBox hits(RenderBox renderBox) => HitsRenderBox(renderBox); class HitsRenderBox extends Matcher { const HitsRenderBox(this.renderBox); final RenderBox renderBox; @override Description describe(Description description) => description.add('hit test result contains ').addDescriptionOf(renderBox); @override bool matches(dynamic item, Map<dynamic, dynamic> matchState) { final HitTestResult hitTestResult = item; return hitTestResult.path.where( (HitTestEntry entry) => entry.target == renderBox ).isNotEmpty; } } DoesNotHitRenderBox doesNotHit(RenderBox renderBox) => DoesNotHitRenderBox(renderBox); class DoesNotHitRenderBox extends Matcher { const DoesNotHitRenderBox(this.renderBox); final RenderBox renderBox; @override Description describe(Description description) => description.add('hit test result doesn\'t contain ').addDescriptionOf(renderBox); @override bool matches(dynamic item, Map<dynamic, dynamic> matchState) { final HitTestResult hitTestResult = item; return hitTestResult.path.where( (HitTestEntry entry) => entry.target == renderBox ).isEmpty; } }