fitted_box_test.dart 3.45 KB
Newer Older
Adam Barth's avatar
Adam Barth committed
1 2 3 4 5 6 7 8 9 10
// 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';

void main() {
  testWidgets('Can size according to aspect ratio', (WidgetTester tester) async {
11 12
    final Key outside = new UniqueKey();
    final Key inside = new UniqueKey();
Adam Barth's avatar
Adam Barth committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

    await tester.pumpWidget(
      new Center(
        child: new Container(
          width: 200.0,
          child: new FittedBox(
            key: outside,
            child: new Container(
              key: inside,
              width: 100.0,
              height: 50.0,
            )
          )
        )
      )
    );

30
    final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
Adam Barth's avatar
Adam Barth committed
31 32 33
    expect(outsideBox.size.width, 200.0);
    expect(outsideBox.size.height, 100.0);

34
    final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside));
Adam Barth's avatar
Adam Barth committed
35 36 37
    expect(insideBox.size.width, 100.0);
    expect(insideBox.size.height, 50.0);

38 39
    final Point insidePoint = insideBox.localToGlobal(const Point(100.0, 50.0));
    final Point outsidePoint = outsideBox.localToGlobal(const Point(200.0, 100.0));
Adam Barth's avatar
Adam Barth committed
40

41
    expect(outsidePoint, equals(const Point(500.0, 350.0)));
Adam Barth's avatar
Adam Barth committed
42 43 44 45
    expect(insidePoint, equals(outsidePoint));
  });

  testWidgets('Can contain child', (WidgetTester tester) async {
46 47
    final Key outside = new UniqueKey();
    final Key inside = new UniqueKey();
Adam Barth's avatar
Adam Barth committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

    await tester.pumpWidget(
      new Center(
        child: new Container(
          width: 200.0,
          height: 200.0,
          child: new FittedBox(
            key: outside,
            child: new Container(
              key: inside,
              width: 100.0,
              height: 50.0,
            )
          )
        )
      )
    );

66
    final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
Adam Barth's avatar
Adam Barth committed
67 68 69
    expect(outsideBox.size.width, 200.0);
    expect(outsideBox.size.height, 200.0);

70
    final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside));
Adam Barth's avatar
Adam Barth committed
71 72 73
    expect(insideBox.size.width, 100.0);
    expect(insideBox.size.height, 50.0);

74 75
    final Point insidePoint = insideBox.localToGlobal(const Point(100.0, 0.0));
    final Point outsidePoint = outsideBox.localToGlobal(const Point(200.0, 50.0));
Adam Barth's avatar
Adam Barth committed
76 77 78 79 80

    expect(insidePoint, equals(outsidePoint));
  });

  testWidgets('Child can conver', (WidgetTester tester) async {
81 82
    final Key outside = new UniqueKey();
    final Key inside = new UniqueKey();
Adam Barth's avatar
Adam Barth committed
83 84 85 86 87 88 89 90

    await tester.pumpWidget(
      new Center(
        child: new Container(
          width: 200.0,
          height: 200.0,
          child: new FittedBox(
            key: outside,
91
            fit: BoxFit.cover,
Adam Barth's avatar
Adam Barth committed
92 93 94 95 96 97 98 99 100 101
            child: new Container(
              key: inside,
              width: 100.0,
              height: 50.0,
            )
          )
        )
      )
    );

102
    final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
Adam Barth's avatar
Adam Barth committed
103 104 105
    expect(outsideBox.size.width, 200.0);
    expect(outsideBox.size.height, 200.0);

106
    final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside));
Adam Barth's avatar
Adam Barth committed
107 108 109
    expect(insideBox.size.width, 100.0);
    expect(insideBox.size.height, 50.0);

110 111
    final Point insidePoint = insideBox.localToGlobal(const Point(50.0, 25.0));
    final Point outsidePoint = outsideBox.localToGlobal(const Point(100.0, 100.0));
Adam Barth's avatar
Adam Barth committed
112 113 114 115

    expect(insidePoint, equals(outsidePoint));
  });
}