divider_test.dart 7.9 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
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
8

9
void main() {
10
  testWidgetsWithLeakTracking('Material3 - Divider control test', (WidgetTester tester) async {
11 12 13 14 15 16 17 18 19 20 21 22 23
    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);
  });

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

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

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

    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
58
        child: Center(child: Divider(indent: customIndent)),
59 60 61 62 63 64 65 66 67 68 69
      ),
    );
    // 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,
70
        child: Center(child: Divider(endIndent: customIndent)),
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
      ),
    );
    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);
  });

95
  testWidgetsWithLeakTracking('Material3 - Vertical Divider Test', (WidgetTester tester) async {
96 97 98 99 100 101 102 103 104 105 106 107 108 109
    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);
  });

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

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

138
  testWidgetsWithLeakTracking('Vertical Divider Test 2', (WidgetTester tester) async {
jslavitz's avatar
jslavitz committed
139
    await tester.pumpWidget(
140 141 142
      MaterialApp(
        theme: ThemeData(useMaterial3: false),
        home: const Material(
143
          child: SizedBox(
jslavitz's avatar
jslavitz committed
144 145
            height: 24.0,
            child: Row(
146
              children: <Widget>[
jslavitz's avatar
jslavitz committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
                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));
  });
162

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

    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
171
        child: Center(child: VerticalDivider(indent: customIndent)),
172 173 174 175 176 177 178 179 180 181 182
      ),
    );
    // 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,
183
        child: Center(child: VerticalDivider(endIndent: customIndent)),
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
      ),
    );
    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);
  });
207 208

  // Regression test for https://github.com/flutter/flutter/issues/39533
209
  testWidgetsWithLeakTracking('createBorderSide does not throw exception with null context', (WidgetTester tester) async {
210 211 212 213
    // 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));
  });
214
}