Commit 924480fc authored by Ian Hickson's avatar Ian Hickson

Merge pull request #2843 from Hixie/nan-constraints

Handle NaNs in constraints, just in case.
parents 9eb4d15c 9356c19d
...@@ -305,6 +305,29 @@ class BoxConstraints extends Constraints { ...@@ -305,6 +305,29 @@ class BoxConstraints extends Constraints {
@override @override
bool get debugAssertIsNormalized { bool get debugAssertIsNormalized {
assert(() { assert(() {
if (minWidth.isNaN || maxWidth.isNaN || minHeight.isNaN || maxHeight.isNaN) {
List<String> affectedFieldsList = <String>[];
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) if (minWidth < 0.0 && minHeight < 0.0)
throw new FlutterError('BoxConstraints has both a negative minimum width and a negative minimum height.\n$this'); throw new FlutterError('BoxConstraints has both a negative minimum width and a negative minimum height.\n$this');
if (minWidth < 0.0) if (minWidth < 0.0)
......
...@@ -30,4 +30,44 @@ void main() { ...@@ -30,4 +30,44 @@ void main() {
expect(leaf.size.width, equals(400.0)); expect(leaf.size.width, equals(400.0));
expect(leaf.size.height, equals(100.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)'
));
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment