align_test.dart 4.6 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 6
// @dart = 2.8

Adam Barth's avatar
Adam Barth committed
7
import 'package:flutter_test/flutter_test.dart';
8
import 'package:flutter/rendering.dart';
9
import 'package:flutter/widgets.dart';
Adam Barth's avatar
Adam Barth committed
10 11

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

20
    await tester.pumpWidget(
21 22
      Align(
        child: Container(),
23
        alignment: const Alignment(0.0, 0.0),
24
      ),
25
    );
Ian Hickson's avatar
Ian Hickson committed
26 27 28

    await tester.pumpWidget(
      const Align(
29
        key: GlobalObjectKey<State<StatefulWidget>>(null),
30
        alignment: Alignment.topLeft,
Ian Hickson's avatar
Ian Hickson committed
31 32 33 34
      ),
    );
    await tester.pumpWidget(const Directionality(
      textDirection: TextDirection.rtl,
35
      child: Align(
36
        key: GlobalObjectKey<State<StatefulWidget>>(null),
37
        alignment: AlignmentDirectional.topStart,
Ian Hickson's avatar
Ian Hickson committed
38 39 40 41
      ),
    ));
    await tester.pumpWidget(
      const Align(
42
        key: GlobalObjectKey<State<StatefulWidget>>(null),
43
        alignment: Alignment.topLeft,
Ian Hickson's avatar
Ian Hickson committed
44 45
      ),
    );
Adam Barth's avatar
Adam Barth committed
46
  });
47

48
  testWidgets('Align control test (LTR)', (WidgetTester tester) async {
49
    await tester.pumpWidget(const Directionality(
50
      textDirection: TextDirection.ltr,
51
      child: Align(
52
        child: SizedBox(width: 100.0, height: 80.0),
53
        alignment: AlignmentDirectional.topStart,
54 55 56
      ),
    ));

57 58
    expect(tester.getTopLeft(find.byType(SizedBox)).dx, 0.0);
    expect(tester.getBottomRight(find.byType(SizedBox)).dx, 100.0);
59

60
    await tester.pumpWidget(const Directionality(
61
      textDirection: TextDirection.ltr,
62
      child: Align(
63
        child: SizedBox(width: 100.0, height: 80.0),
64
        alignment: Alignment.topLeft,
65 66 67
      ),
    ));

68 69
    expect(tester.getTopLeft(find.byType(SizedBox)).dx, 0.0);
    expect(tester.getBottomRight(find.byType(SizedBox)).dx, 100.0);
70 71 72
  });

  testWidgets('Align control test (RTL)', (WidgetTester tester) async {
73
    await tester.pumpWidget(const Directionality(
74
      textDirection: TextDirection.rtl,
75
      child: Align(
76
        child: SizedBox(width: 100.0, height: 80.0),
77
        alignment: AlignmentDirectional.topStart,
78 79 80
      ),
    ));

81 82
    expect(tester.getTopLeft(find.byType(SizedBox)).dx, 700.0);
    expect(tester.getBottomRight(find.byType(SizedBox)).dx, 800.0);
83

84
    await tester.pumpWidget(const Directionality(
85
      textDirection: TextDirection.ltr,
86
      child: Align(
87
        child: SizedBox(width: 100.0, height: 80.0),
88
        alignment: Alignment.topLeft,
89 90 91
      ),
    ));

92 93
    expect(tester.getTopLeft(find.byType(SizedBox)).dx, 0.0);
    expect(tester.getBottomRight(find.byType(SizedBox)).dx, 100.0);
94 95
  });

96
  testWidgets('Shrink wraps in finite space', (WidgetTester tester) async {
97
    final GlobalKey alignKey = GlobalKey();
98
    await tester.pumpWidget(
99 100
      SingleChildScrollView(
        child: Align(
101
          key: alignKey,
102
          child: const SizedBox(
103
            width: 10.0,
104
            height: 10.0,
105
          ),
106
          alignment: const Alignment(0.0, 0.0),
107 108
        ),
      ),
109
    );
110

111 112 113
    final Size size = alignKey.currentContext.size;
    expect(size.width, equals(800.0));
    expect(size.height, equals(10.0));
114
  });
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

  testWidgets('Align widthFactor', (WidgetTester tester) async {
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Align(
              widthFactor: 0.5,
              child: Container(
                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(
      Directionality(
        textDirection: TextDirection.ltr,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Align(
              alignment: Alignment.center,
              heightFactor: 0.5,
              child: Container(
                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
162
}