aspect_ratio_test.dart 1.69 KB
Newer Older
1 2 3 4 5 6 7 8
// 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';

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

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

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