From 14f42a6a48de680d5951054c33dde956ae0046d4 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Mon, 27 Jun 2016 17:38:53 -0700 Subject: [PATCH] Improve test coverage in rendering.dart (#4775) --- packages/flutter/lib/rendering.dart | 3 +- packages/flutter/lib/src/rendering/block.dart | 12 +- packages/flutter/lib/src/rendering/box.dart | 15 --- .../flutter/lib/src/rendering/tweens.dart | 21 ++++ .../test/rendering/box_constraints_test.dart | 106 ++++++++++++++++++ 5 files changed, 135 insertions(+), 22 deletions(-) create mode 100644 packages/flutter/lib/src/rendering/tweens.dart create mode 100644 packages/flutter/test/rendering/box_constraints_test.dart diff --git a/packages/flutter/lib/rendering.dart b/packages/flutter/lib/rendering.dart index d3ff0c94a1..5062dda29f 100644 --- a/packages/flutter/lib/rendering.dart +++ b/packages/flutter/lib/rendering.dart @@ -3,7 +3,7 @@ // found in the LICENSE file. /// The Flutter rendering tree. -/// +/// /// To use, import `package:flutter/rendering.dart`. /// /// The [RenderObject] hierarchy is used by the Flutter Widgets @@ -48,6 +48,7 @@ export 'src/rendering/semantics.dart'; export 'src/rendering/shifted_box.dart'; export 'src/rendering/stack.dart'; export 'src/rendering/table.dart'; +export 'src/rendering/tweens.dart'; export 'src/rendering/view.dart'; export 'src/rendering/viewport.dart'; diff --git a/packages/flutter/lib/src/rendering/block.dart b/packages/flutter/lib/src/rendering/block.dart index 7d1cf79b13..eb9fe40006 100644 --- a/packages/flutter/lib/src/rendering/block.dart +++ b/packages/flutter/lib/src/rendering/block.dart @@ -51,13 +51,13 @@ class RenderBlock extends RenderBox } BoxConstraints _getInnerConstraints(BoxConstraints constraints) { + assert(_mainAxis != null); switch (_mainAxis) { case Axis.horizontal: return new BoxConstraints.tightFor(height: constraints.maxHeight); case Axis.vertical: return new BoxConstraints.tightFor(width: constraints.maxWidth); } - assert(_mainAxis != null); return null; } @@ -66,13 +66,13 @@ class RenderBlock extends RenderBox if (child == null) return 0.0; BoxParentData parentData = child.parentData; + assert(mainAxis != null); switch (mainAxis) { case Axis.horizontal: return parentData.offset.dx + child.size.width; case Axis.vertical: return parentData.offset.dy + child.size.height; } - assert(mainAxis != null); return null; } @@ -183,49 +183,49 @@ class RenderBlock extends RenderBox @override double computeMinIntrinsicWidth(double height) { + assert(mainAxis != null); switch (mainAxis) { case Axis.horizontal: return _getIntrinsicMainAxis((RenderBox child) => child.getMinIntrinsicWidth(height)); case Axis.vertical: return _getIntrinsicCrossAxis((RenderBox child) => child.getMinIntrinsicWidth(height)); } - assert(mainAxis != null); return null; } @override double computeMaxIntrinsicWidth(double height) { + assert(mainAxis != null); switch (mainAxis) { case Axis.horizontal: return _getIntrinsicMainAxis((RenderBox child) => child.getMaxIntrinsicWidth(height)); case Axis.vertical: return _getIntrinsicCrossAxis((RenderBox child) => child.getMaxIntrinsicWidth(height)); } - assert(mainAxis != null); return null; } @override double computeMinIntrinsicHeight(double width) { + assert(mainAxis != null); switch (mainAxis) { case Axis.horizontal: return _getIntrinsicMainAxis((RenderBox child) => child.getMinIntrinsicHeight(width)); case Axis.vertical: return _getIntrinsicCrossAxis((RenderBox child) => child.getMinIntrinsicHeight(width)); } - assert(mainAxis != null); return null; } @override double computeMaxIntrinsicHeight(double width) { + assert(mainAxis != null); switch (mainAxis) { case Axis.horizontal: return _getIntrinsicMainAxis((RenderBox child) => child.getMaxIntrinsicHeight(width)); case Axis.vertical: return _getIntrinsicCrossAxis((RenderBox child) => child.getMaxIntrinsicHeight(width)); } - assert(mainAxis != null); return null; } diff --git a/packages/flutter/lib/src/rendering/box.dart b/packages/flutter/lib/src/rendering/box.dart index c1ebd9398d..268e1a034a 100644 --- a/packages/flutter/lib/src/rendering/box.dart +++ b/packages/flutter/lib/src/rendering/box.dart @@ -5,7 +5,6 @@ import 'dart:math' as math; import 'dart:ui' as ui show lerpDouble; -import 'package:flutter/animation.dart'; import 'package:flutter/gestures.dart'; import 'package:meta/meta.dart'; import 'package:vector_math/vector_math_64.dart'; @@ -1456,17 +1455,3 @@ abstract class RenderBoxContainerDefaultsMixin to be -/// appropriate for rectangles. -class FractionalOffsetTween extends Tween { - /// Creates a fractional offset tween. - /// - /// The [begin] and [end] arguments must not be null. - FractionalOffsetTween({ FractionalOffset begin, FractionalOffset end }) : super(begin: begin, end: end); - - @override - FractionalOffset lerp(double t) => FractionalOffset.lerp(begin, end, t); -} diff --git a/packages/flutter/lib/src/rendering/tweens.dart b/packages/flutter/lib/src/rendering/tweens.dart new file mode 100644 index 0000000000..3206c2b763 --- /dev/null +++ b/packages/flutter/lib/src/rendering/tweens.dart @@ -0,0 +1,21 @@ +// Copyright 2015 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/animation.dart'; +import 'package:flutter/painting.dart'; + +/// An interpolation between two fractional offsets. +/// +/// This class specializes the interpolation of Tween to be +/// appropriate for rectangles. +class FractionalOffsetTween extends Tween { + /// Creates a fractional offset tween. + /// + /// The [begin] and [end] arguments must not be null. + FractionalOffsetTween({ FractionalOffset begin, FractionalOffset end }) + : super(begin: begin, end: end); + + @override + FractionalOffset lerp(double t) => FractionalOffset.lerp(begin, end, t); +} diff --git a/packages/flutter/test/rendering/box_constraints_test.dart b/packages/flutter/test/rendering/box_constraints_test.dart new file mode 100644 index 0000000000..e44125745e --- /dev/null +++ b/packages/flutter/test/rendering/box_constraints_test.dart @@ -0,0 +1,106 @@ +// Copyright 2015 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/rendering.dart'; +import 'package:test/test.dart'; + +void main() { + test('BoxConstraints toString', () { + expect(const BoxConstraints.expand().toString(), contains('biggest')); + expect(new BoxConstraints().toString(), contains('unconstrained')); + expect(new BoxConstraints.tightFor(width: 50.0).toString(), contains('w=50')); + }); + + test('BoxConstraints copyWith', () { + BoxConstraints constraints = new BoxConstraints( + minWidth: 3.0, + maxWidth: 7.0, + minHeight: 11.0, + maxHeight: 17.0 + ); + BoxConstraints copy = constraints.copyWith(); + expect(copy, equals(constraints)); + copy = constraints.copyWith( + minWidth: 13.0, + maxWidth: 17.0, + minHeight: 111.0, + maxHeight: 117.0 + ); + expect(copy.minWidth, 13.0); + expect(copy.maxWidth, 17.0); + expect(copy.minHeight, 111.0); + expect(copy.maxHeight, 117.0); + expect(copy, isNot(equals(constraints))); + expect(copy.hashCode, isNot(equals(constraints.hashCode))); + }); + + test('BoxConstraints operators', () { + BoxConstraints constraints = new BoxConstraints( + minWidth: 3.0, + maxWidth: 7.0, + minHeight: 11.0, + maxHeight: 17.0 + ); + BoxConstraints copy = constraints * 2.0; + expect(copy.minWidth, 6.0); + expect(copy.maxWidth, 14.0); + expect(copy.minHeight, 22.0); + expect(copy.maxHeight, 34.0); + expect(copy / 2.0, equals(constraints)); + copy = constraints ~/ 2.0; + expect(copy.minWidth, 1.0); + expect(copy.maxWidth, 3.0); + expect(copy.minHeight, 5.0); + expect(copy.maxHeight, 8.0); + copy = constraints % 3.0; + expect(copy.minWidth, 0.0); + expect(copy.maxWidth, 1.0); + expect(copy.minHeight, 2.0); + expect(copy.maxHeight, 2.0); + }); + + test('BoxConstraints lerp', () { + expect(BoxConstraints.lerp(null, null, 0.5), isNull); + BoxConstraints constraints = new BoxConstraints( + minWidth: 3.0, + maxWidth: 7.0, + minHeight: 11.0, + maxHeight: 17.0 + ); + BoxConstraints copy = BoxConstraints.lerp(null, constraints, 0.5); + expect(copy.minWidth, 1.5); + expect(copy.maxWidth, 3.5); + expect(copy.minHeight, 5.5); + expect(copy.maxHeight, 8.5); + copy = BoxConstraints.lerp(constraints, null, 0.5); + expect(copy.minWidth, 1.5); + expect(copy.maxWidth, 3.5); + expect(copy.minHeight, 5.5); + expect(copy.maxHeight, 8.5); + copy = BoxConstraints.lerp(new BoxConstraints( + minWidth: 13.0, + maxWidth: 17.0, + minHeight: 111.0, + maxHeight: 117.0 + ), constraints, 0.2); + expect(copy.minWidth, 11.0); + expect(copy.maxWidth, 15.0); + expect(copy.minHeight, 91.0); + expect(copy.maxHeight, 97.0); + }); + + test('BoxConstraints normalize', () { + BoxConstraints constraints = new BoxConstraints( + minWidth: 3.0, + maxWidth: 2.0, + minHeight: 11.0, + maxHeight: 18.0 + ); + BoxConstraints copy = constraints.normalize(); + expect(copy.minWidth, 3.0); + expect(copy.maxWidth, 3.0); + expect(copy.minHeight, 11.0); + expect(copy.maxHeight, 18.0); + }); +}