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

import 'package:flutter/widgets.dart';
6
import 'package:flutter_test/flutter_test.dart';
7

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

void main() {
28
  testWidgets('Aspect ratio control test', (WidgetTester tester) async {
29 30
    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)));
31 32
  });

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