spacer_test.dart 2.87 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 9

void main() {
  testWidgets('Spacer takes up space.', (WidgetTester tester) async {
10
    await tester.pumpWidget(Column(
11
      children: const <Widget>[
12 13 14
        SizedBox(width: 10.0, height: 10.0),
        Spacer(),
        SizedBox(width: 10.0, height: 10.0),
15 16 17
      ],
    ));
    final Rect spacerRect = tester.getRect(find.byType(Spacer));
18 19
    expect(spacerRect.size, const Size(0.0, 580.0));
    expect(spacerRect.topLeft, const Offset(400.0, 10.0));
20 21 22
  });

  testWidgets('Spacer takes up space proportional to flex.', (WidgetTester tester) async {
23
    const Spacer spacer1 = Spacer();
24
    const Spacer spacer2 = Spacer();
25 26
    const Spacer spacer3 = Spacer(flex: 2);
    const Spacer spacer4 = Spacer(flex: 4);
27
    await tester.pumpWidget(Row(
28 29
      textDirection: TextDirection.rtl,
      children: const <Widget>[
30
        SizedBox(width: 10.0, height: 10.0),
31
        spacer1,
32
        SizedBox(width: 10.0, height: 10.0),
33
        spacer2,
34
        SizedBox(width: 10.0, height: 10.0),
35
        spacer3,
36
        SizedBox(width: 10.0, height: 10.0),
37
        spacer4,
38
        SizedBox(width: 10.0, height: 10.0),
39 40 41 42 43 44
      ],
    ));
    final Rect spacer1Rect = tester.getRect(find.byType(Spacer).at(0));
    final Rect spacer2Rect = tester.getRect(find.byType(Spacer).at(1));
    final Rect spacer3Rect = tester.getRect(find.byType(Spacer).at(2));
    final Rect spacer4Rect = tester.getRect(find.byType(Spacer).at(3));
45
    expect(spacer1Rect.size.height, 0.0);
46 47 48 49
    expect(spacer1Rect.size.width, moreOrLessEquals(93.8, epsilon: 0.1));
    expect(spacer1Rect.left, moreOrLessEquals(696.3, epsilon: 0.1));
    expect(spacer2Rect.size.width, moreOrLessEquals(93.8, epsilon: 0.1));
    expect(spacer2Rect.left, moreOrLessEquals(592.5, epsilon: 0.1));
50
    expect(spacer3Rect.size.width, spacer2Rect.size.width * 2.0);
51
    expect(spacer3Rect.left, moreOrLessEquals(395.0, epsilon: 0.1));
52
    expect(spacer4Rect.size.width, spacer3Rect.size.width * 2.0);
53
    expect(spacer4Rect.left, moreOrLessEquals(10.0, epsilon: 0.1));
54
  });
55

56
  testWidgets('Spacer takes up space.', (WidgetTester tester) async {
57
    await tester.pumpWidget(UnconstrainedBox(
58
      constrainedAxis: Axis.vertical,
59
      child: Column(
60
        children: const <Widget>[
61 62 63
          SizedBox(width: 20.0, height: 10.0),
          Spacer(),
          SizedBox(width: 10.0, height: 10.0),
64 65 66 67 68 69 70
        ],
      ),
    ));
    final Rect spacerRect = tester.getRect(find.byType(Spacer));
    final Rect flexRect = tester.getRect(find.byType(Column));
    expect(spacerRect.size, const Size(0.0, 580.0));
    expect(spacerRect.topLeft, const Offset(400.0, 10.0));
Dan Field's avatar
Dan Field committed
71
    expect(flexRect, const Rect.fromLTWH(390.0, 0.0, 20.0, 600.0));
72
  });
73
}