aspect_ratio_test.dart 1.71 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7 8 9 10
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

11
Future<Size> _getSize(WidgetTester tester, BoxConstraints constraints, double aspectRatio) async {
12
  final Key childKey = UniqueKey();
13
  await tester.pumpWidget(
14 15
    Center(
      child: ConstrainedBox(
16
        constraints: constraints,
17
        child: AspectRatio(
18
          aspectRatio: aspectRatio,
19
          child: Container(
20
            key: childKey
21 22 23
          ),
        ),
      ),
24
    ),
25
  );
26
  final RenderBox box = tester.renderObject(find.byKey(childKey));
27 28 29 30
  return box.size;
}

void main() {
31
  testWidgets('Aspect ratio control test', (WidgetTester tester) async {
32 33
    expect(await _getSize(tester, BoxConstraints.loose(const Size(500.0, 500.0)), 2.0), equals(const Size(500.0, 250.0)));
    expect(await _getSize(tester, BoxConstraints.loose(const Size(500.0, 500.0)), 0.5), equals(const Size(250.0, 500.0)));
34 35
  });

36
  testWidgets('Aspect ratio infinite width', (WidgetTester tester) async {
37 38
    final Key childKey = UniqueKey();
    await tester.pumpWidget(Directionality(
39
      textDirection: TextDirection.ltr,
40 41
      child: Center(
        child: SingleChildScrollView(
42
          scrollDirection: Axis.horizontal,
43
          child: AspectRatio(
44
            aspectRatio: 2.0,
45
            child: Container(
46
              key: childKey
47 48 49 50
            ),
          ),
        ),
      ),
51
    ));
52
    final RenderBox box = tester.renderObject(find.byKey(childKey));
53
    expect(box.size, equals(const Size(1200.0, 600.0)));
54 55
  });
}