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

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
7

8
void main() {
9
  testWidgets('Material3 - Divider control test', (WidgetTester tester) async {
10 11 12 13 14 15 16 17 18 19 20 21 22
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(useMaterial3: true),
        home: const Center(child: Divider()),
      ),
    );
    final RenderBox box = tester.firstRenderObject(find.byType(Divider));
    expect(box.size.height, 16.0);
    final Container container = tester.widget(find.byType(Container));
    final BoxDecoration decoration = container.decoration! as BoxDecoration;
    expect(decoration.border!.bottom.width, 1.0);
  });

23
  testWidgets('Material2 - Divider control test', (WidgetTester tester) async {
24
    await tester.pumpWidget(
25 26
      MaterialApp(
        theme: ThemeData(useMaterial3: false),
27
        home: const Center(child: Divider()),
28 29
      ),
    );
30
    final RenderBox box = tester.firstRenderObject(find.byType(Divider));
31
    expect(box.size.height, 16.0);
32
    final Container container = tester.widget(find.byType(Container));
33 34
    final BoxDecoration decoration = container.decoration! as BoxDecoration;
    expect(decoration.border!.bottom.width, 0.0);
35 36
  });

37
  testWidgets('Divider custom thickness', (WidgetTester tester) async {
38 39 40
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
41
        child: Center(child: Divider(thickness: 5.0)),
42 43 44
      ),
    );
    final Container container = tester.widget(find.byType(Container));
45 46
    final BoxDecoration decoration = container.decoration! as BoxDecoration;
    expect(decoration.border!.bottom.width, 5.0);
47
  });
jslavitz's avatar
jslavitz committed
48

49
  testWidgets('Horizontal divider custom indentation', (WidgetTester tester) async {
50 51 52 53 54 55 56
    const double customIndent = 10.0;
    Rect dividerRect;
    Rect lineRect;

    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
57
        child: Center(child: Divider(indent: customIndent)),
58 59 60 61 62 63 64 65 66 67 68
      ),
    );
    // The divider line is drawn with a DecoratedBox with a border
    dividerRect = tester.getRect(find.byType(Divider));
    lineRect = tester.getRect(find.byType(DecoratedBox));
    expect(lineRect.left, dividerRect.left + customIndent);
    expect(lineRect.right, dividerRect.right);

    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
69
        child: Center(child: Divider(endIndent: customIndent)),
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
      ),
    );
    dividerRect = tester.getRect(find.byType(Divider));
    lineRect = tester.getRect(find.byType(DecoratedBox));
    expect(lineRect.left, dividerRect.left);
    expect(lineRect.right, dividerRect.right - customIndent);

    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: Center(
          child: Divider(
            indent: customIndent,
            endIndent: customIndent,
          ),
        ),
      ),
    );
    dividerRect = tester.getRect(find.byType(Divider));
    lineRect = tester.getRect(find.byType(DecoratedBox));
    expect(lineRect.left, dividerRect.left + customIndent);
    expect(lineRect.right, dividerRect.right - customIndent);
  });

94
  testWidgets('Material3 - Vertical Divider Test', (WidgetTester tester) async {
95 96 97 98 99 100 101 102 103 104 105 106 107 108
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(useMaterial3: true),
        home: const Center(child: VerticalDivider()),
      ),
    );
    final RenderBox box = tester.firstRenderObject(find.byType(VerticalDivider));
    expect(box.size.width, 16.0);
    final Container container = tester.widget(find.byType(Container));
    final BoxDecoration decoration = container.decoration! as BoxDecoration;
    final Border border = decoration.border! as Border;
    expect(border.left.width, 1.0);
  });

109
  testWidgets('Material2 - Vertical Divider Test', (WidgetTester tester) async {
jslavitz's avatar
jslavitz committed
110
    await tester.pumpWidget(
111 112
      MaterialApp(
        theme: ThemeData(useMaterial3: false),
113
        home: const Center(child: VerticalDivider()),
jslavitz's avatar
jslavitz committed
114 115 116 117
      ),
    );
    final RenderBox box = tester.firstRenderObject(find.byType(VerticalDivider));
    expect(box.size.width, 16.0);
118
    final Container container = tester.widget(find.byType(Container));
119 120
    final BoxDecoration decoration = container.decoration! as BoxDecoration;
    final Border border = decoration.border! as Border;
121 122 123
    expect(border.left.width, 0.0);
  });

124
  testWidgets('Divider custom thickness', (WidgetTester tester) async {
125 126 127
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
128
        child: Center(child: VerticalDivider(thickness: 5.0)),
129 130 131
      ),
    );
    final Container container = tester.widget(find.byType(Container));
132 133
    final BoxDecoration decoration = container.decoration! as BoxDecoration;
    final Border border = decoration.border! as Border;
134
    expect(border.left.width, 5.0);
jslavitz's avatar
jslavitz committed
135
  });
jslavitz's avatar
jslavitz committed
136

137
  testWidgets('Vertical Divider Test 2', (WidgetTester tester) async {
jslavitz's avatar
jslavitz committed
138
    await tester.pumpWidget(
139 140 141
      MaterialApp(
        theme: ThemeData(useMaterial3: false),
        home: const Material(
142
          child: SizedBox(
jslavitz's avatar
jslavitz committed
143 144
            height: 24.0,
            child: Row(
145
              children: <Widget>[
jslavitz's avatar
jslavitz committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
                Text('Hey.'),
                VerticalDivider(),
              ],
            ),
          ),
        ),
      ),
    );
    final RenderBox box = tester.firstRenderObject(find.byType(VerticalDivider));
    final RenderBox containerBox = tester.firstRenderObject(find.byType(Container).last);

    expect(box.size.width, 16.0);
    expect(containerBox.size.height, 600.0);
    expect(find.byType(VerticalDivider), paints..path(strokeWidth: 0.0));
  });
161

162
  testWidgets('Vertical divider custom indentation', (WidgetTester tester) async {
163 164 165 166 167 168 169
    const double customIndent = 10.0;
    Rect dividerRect;
    Rect lineRect;

    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
170
        child: Center(child: VerticalDivider(indent: customIndent)),
171 172 173 174 175 176 177 178 179 180 181
      ),
    );
    // The divider line is drawn with a DecoratedBox with a border
    dividerRect = tester.getRect(find.byType(VerticalDivider));
    lineRect = tester.getRect(find.byType(DecoratedBox));
    expect(lineRect.top, dividerRect.top + customIndent);
    expect(lineRect.bottom, dividerRect.bottom);

    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
182
        child: Center(child: VerticalDivider(endIndent: customIndent)),
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
      ),
    );
    dividerRect = tester.getRect(find.byType(VerticalDivider));
    lineRect = tester.getRect(find.byType(DecoratedBox));
    expect(lineRect.top, dividerRect.top);
    expect(lineRect.bottom, dividerRect.bottom - customIndent);

    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: Center(
          child: VerticalDivider(
            indent: customIndent,
            endIndent: customIndent,
          ),
        ),
      ),
    );
    dividerRect = tester.getRect(find.byType(VerticalDivider));
    lineRect = tester.getRect(find.byType(DecoratedBox));
    expect(lineRect.top, dividerRect.top + customIndent);
    expect(lineRect.bottom, dividerRect.bottom - customIndent);
  });
206 207

  // Regression test for https://github.com/flutter/flutter/issues/39533
208
  testWidgets('createBorderSide does not throw exception with null context', (WidgetTester tester) async {
209 210 211 212
    // Passing a null context used to throw an exception but no longer does.
    expect(() => Divider.createBorderSide(null), isNot(throwsAssertionError));
    expect(() => Divider.createBorderSide(null), isNot(throwsNoSuchMethodError));
  });
213
}