From 9356c19dce6e77d51af981fd27cb00dac93afd5b Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Tue, 22 Mar 2016 22:47:16 -0700 Subject: [PATCH] Handle NaNs in constraints, just in case. --- packages/flutter/lib/src/rendering/box.dart | 23 +++++++++++ .../test/rendering/constraints_test.dart | 40 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/packages/flutter/lib/src/rendering/box.dart b/packages/flutter/lib/src/rendering/box.dart index dde63c8ad66..dfda2df0bf5 100644 --- a/packages/flutter/lib/src/rendering/box.dart +++ b/packages/flutter/lib/src/rendering/box.dart @@ -305,6 +305,29 @@ class BoxConstraints extends Constraints { @override bool get debugAssertIsNormalized { assert(() { + if (minWidth.isNaN || maxWidth.isNaN || minHeight.isNaN || maxHeight.isNaN) { + List affectedFieldsList = []; + if (minWidth.isNaN) + affectedFieldsList.add('minWidth'); + if (maxWidth.isNaN) + affectedFieldsList.add('maxWidth'); + if (minHeight.isNaN) + affectedFieldsList.add('minHeight'); + if (maxHeight.isNaN) + affectedFieldsList.add('maxHeight'); + assert(affectedFieldsList.length > 0); + if (affectedFieldsList.length > 1) + affectedFieldsList.add('and ${affectedFieldsList.removeLast()}'); + String whichFields = ''; + if (affectedFieldsList.length > 2) { + whichFields = affectedFieldsList.join(', '); + } else if (affectedFieldsList.length == 2) { + whichFields = affectedFieldsList.join(' '); + } else { + whichFields = affectedFieldsList.single; + } + throw new FlutterError('BoxConstraints has ${affectedFieldsList.length == 1 ? 'a NaN value' : 'NaN values' } in $whichFields.\n$this'); + } if (minWidth < 0.0 && minHeight < 0.0) throw new FlutterError('BoxConstraints has both a negative minimum width and a negative minimum height.\n$this'); if (minWidth < 0.0) diff --git a/packages/flutter/test/rendering/constraints_test.dart b/packages/flutter/test/rendering/constraints_test.dart index f94779a43aa..98d8d9b9b7d 100644 --- a/packages/flutter/test/rendering/constraints_test.dart +++ b/packages/flutter/test/rendering/constraints_test.dart @@ -30,4 +30,44 @@ void main() { expect(leaf.size.width, equals(400.0)); expect(leaf.size.height, equals(100.0)); }); + + test("BoxConstraints with NaN", () { + String result; + + result = 'no exception'; + try { + BoxConstraints constraints = new BoxConstraints(minWidth: double.NAN, maxWidth: double.NAN, minHeight: 2.0, maxHeight: double.NAN); + assert(constraints.debugAssertIsNormalized); + } on FlutterError catch (e) { + result = '$e'; + } + expect(result, equals( + 'BoxConstraints has NaN values in minWidth, maxWidth, and maxHeight.\n' + 'BoxConstraints(NaN<=w<=NaN, 2.0<=h<=NaN; NOT NORMALIZED)' + )); + + result = 'no exception'; + try { + BoxConstraints constraints = new BoxConstraints(minHeight: double.NAN); + assert(constraints.debugAssertIsNormalized); + } on FlutterError catch (e) { + result = '$e'; + } + expect(result, equals( + 'BoxConstraints has a NaN value in minHeight.\n' + 'BoxConstraints(0.0<=w<=Infinity, NaN<=h<=Infinity; NOT NORMALIZED)' + )); + + result = 'no exception'; + try { + BoxConstraints constraints = new BoxConstraints(minHeight: double.NAN, maxWidth: 0.0/0.0); + assert(constraints.debugAssertIsNormalized); + } on FlutterError catch (e) { + result = '$e'; + } + expect(result, equals( + 'BoxConstraints has NaN values in maxWidth and minHeight.\n' + 'BoxConstraints(0.0<=w<=NaN, NaN<=h<=Infinity; NOT NORMALIZED)' + )); + }); }