align_test.dart 4.37 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 12
      Align(
        child: Container(),
13
        alignment: const Alignment(0.50, 0.50),
14
      ),
15
    );
Adam Barth's avatar
Adam Barth committed
16

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

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

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

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

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

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

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

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

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

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

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

105
    final Size size = alignKey.currentContext!.size!;
106 107
    expect(size.width, equals(800.0));
    expect(size.height, equals(10.0));
108
  });
109 110 111 112 113 114 115 116

  testWidgets('Align widthFactor', (WidgetTester tester) async {
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.center,
117
          children: const <Widget>[
118 119
            Align(
              widthFactor: 0.5,
120
              child: SizedBox(
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
                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,
140
          children: const <Widget>[
141 142 143
            Align(
              alignment: Alignment.center,
              heightFactor: 0.5,
144
              child: SizedBox(
145 146 147 148 149 150 151 152 153 154 155
                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
156
}