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

5
import 'package:flutter/widgets.dart';
6
import 'package:flutter_test/flutter_test.dart';
Adam Barth's avatar
Adam Barth committed
7 8

void main() {
9 10
  testWidgets('Align smoke test', (WidgetTester tester) async {
    await tester.pumpWidget(
11
      Align(
12
        alignment: const Alignment(0.50, 0.50),
13
        child: Container(),
14
      ),
15
    );
Adam Barth's avatar
Adam Barth committed
16

17
    await tester.pumpWidget(
18
      Align(
19
        child: Container(),
20
      ),
21
    );
Ian Hickson's avatar
Ian Hickson committed
22 23 24

    await tester.pumpWidget(
      const Align(
25
        alignment: Alignment.topLeft,
Ian Hickson's avatar
Ian Hickson committed
26 27 28 29
      ),
    );
    await tester.pumpWidget(const Directionality(
      textDirection: TextDirection.rtl,
30
      child: Align(
31
        alignment: AlignmentDirectional.topStart,
Ian Hickson's avatar
Ian Hickson committed
32 33 34 35
      ),
    ));
    await tester.pumpWidget(
      const Align(
36
        alignment: Alignment.topLeft,
Ian Hickson's avatar
Ian Hickson committed
37 38
      ),
    );
Adam Barth's avatar
Adam Barth committed
39
  });
40

41
  testWidgets('Align control test (LTR)', (WidgetTester tester) async {
42
    await tester.pumpWidget(const Directionality(
43
      textDirection: TextDirection.ltr,
44
      child: Align(
45
        alignment: AlignmentDirectional.topStart,
46
        child: SizedBox(width: 100.0, height: 80.0),
47 48 49
      ),
    ));

50 51
    expect(tester.getTopLeft(find.byType(SizedBox)).dx, 0.0);
    expect(tester.getBottomRight(find.byType(SizedBox)).dx, 100.0);
52

53
    await tester.pumpWidget(const Directionality(
54
      textDirection: TextDirection.ltr,
55
      child: Align(
56
        alignment: Alignment.topLeft,
57
        child: SizedBox(width: 100.0, height: 80.0),
58 59 60
      ),
    ));

61 62
    expect(tester.getTopLeft(find.byType(SizedBox)).dx, 0.0);
    expect(tester.getBottomRight(find.byType(SizedBox)).dx, 100.0);
63 64 65
  });

  testWidgets('Align control test (RTL)', (WidgetTester tester) async {
66
    await tester.pumpWidget(const Directionality(
67
      textDirection: TextDirection.rtl,
68
      child: Align(
69
        alignment: AlignmentDirectional.topStart,
70
        child: SizedBox(width: 100.0, height: 80.0),
71 72 73
      ),
    ));

74 75
    expect(tester.getTopLeft(find.byType(SizedBox)).dx, 700.0);
    expect(tester.getBottomRight(find.byType(SizedBox)).dx, 800.0);
76

77
    await tester.pumpWidget(const Directionality(
78
      textDirection: TextDirection.ltr,
79
      child: Align(
80
        alignment: Alignment.topLeft,
81
        child: SizedBox(width: 100.0, height: 80.0),
82 83 84
      ),
    ));

85 86
    expect(tester.getTopLeft(find.byType(SizedBox)).dx, 0.0);
    expect(tester.getBottomRight(find.byType(SizedBox)).dx, 100.0);
87 88
  });

89
  testWidgets('Shrink wraps in finite space', (WidgetTester tester) async {
90
    final GlobalKey alignKey = GlobalKey();
91
    await tester.pumpWidget(
92 93
      SingleChildScrollView(
        child: Align(
94
          key: alignKey,
95
          child: const SizedBox(
96
            width: 10.0,
97
            height: 10.0,
98
          ),
99 100
        ),
      ),
101
    );
102

103
    final Size size = alignKey.currentContext!.size!;
104 105
    expect(size.width, equals(800.0));
    expect(size.height, equals(10.0));
106
  });
107 108 109

  testWidgets('Align widthFactor', (WidgetTester tester) async {
    await tester.pumpWidget(
110
      const Directionality(
111 112 113 114
        textDirection: TextDirection.ltr,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.center,
115
          children: <Widget>[
116 117
            Align(
              widthFactor: 0.5,
118
              child: SizedBox(
119 120 121 122 123 124 125 126 127 128 129 130 131 132
                height: 100.0,
                width: 100.0,
              ),
            ),
          ],
        ),
      ),
    );
    final RenderBox box = tester.renderObject<RenderBox>(find.byType(Align));
    expect(box.size.width, equals(50.0));
  });

  testWidgets('Align heightFactor', (WidgetTester tester) async {
    await tester.pumpWidget(
133
      const Directionality(
134 135 136 137
        textDirection: TextDirection.ltr,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start,
138
          children: <Widget>[
139 140
            Align(
              heightFactor: 0.5,
141
              child: SizedBox(
142 143 144 145 146 147 148 149 150 151 152
                height: 100.0,
                width: 100.0,
              ),
            ),
          ],
        ),
      ),
    );
    final RenderBox box = tester.renderObject<RenderBox>(find.byType(Align));
    expect(box.size.height, equals(50.0));
  });
Adam Barth's avatar
Adam Barth committed
153
}