flutter/packages/flutter/test/widget/aspect_ratio_test.dart
Adam Barth 6a54e12240 AspectRatio should attempt to fit its height
If there's not enough height for the given aspect ratio, we now try to see if
using the height to infer the width will fit the given constraints. The
algorithm we use is similar to the one we use for RenderImage.

Fixes #2620
2016-03-11 15:25:06 -08:00

58 lines
1.7 KiB
Dart

// 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_test/flutter_test.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:test/test.dart';
Size _getSize(WidgetTester tester, BoxConstraints constraints, double aspectRatio) {
Key childKey = new UniqueKey();
tester.pumpWidget(
new Center(
child: new ConstrainedBox(
constraints: constraints,
child: new AspectRatio(
aspectRatio: aspectRatio,
child: new Container(
key: childKey
)
)
)
)
);
RenderBox box = tester.findElementByKey(childKey).renderObject;
return box.size;
}
void main() {
test('Aspect ratio control test', () {
testWidgets((WidgetTester tester) {
expect(_getSize(tester, new BoxConstraints.loose(new Size(500.0, 500.0)), 2.0), equals(new Size(500.0, 250.0)));
expect(_getSize(tester, new BoxConstraints.loose(new Size(500.0, 500.0)), 0.5), equals(new Size(250.0, 500.0)));
});
});
test('Aspect ratio infinite width', () {
testWidgets((WidgetTester tester) {
Key childKey = new UniqueKey();
tester.pumpWidget(
new Center(
child: new Viewport(
scrollDirection: Axis.horizontal,
child: new AspectRatio(
aspectRatio: 2.0,
child: new Container(
key: childKey
)
)
)
)
);
RenderBox box = tester.findElementByKey(childKey).renderObject;
expect(box.size, equals(new Size(1200.0, 600.0)));
});
});
}