1// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'package:flutter/material.dart';
6import 'package:flutter_test/flutter_test.dart';
7
8void main() {
9 testWidgets('Material3 - Divider control test', (WidgetTester tester) async {
10 await tester.pumpWidget(const MaterialApp(home: Center(child: Divider())));
11 final RenderBox box = tester.firstRenderObject(find.byType(Divider));
12 expect(box.size.height, 16.0);
13 final Container container = tester.widget(find.byType(Container));
14 final BoxDecoration decoration = container.decoration! as BoxDecoration;
15 expect(decoration.border!.bottom.width, 1.0);
16 });
17
18 testWidgets('Material2 - Divider control test', (WidgetTester tester) async {
19 await tester.pumpWidget(
20 MaterialApp(
21 theme: ThemeData(useMaterial3: false),
22 home: const Center(child: Divider()),
23 ),
24 );
25 final RenderBox box = tester.firstRenderObject(find.byType(Divider));
26 expect(box.size.height, 16.0);
27 final Container container = tester.widget(find.byType(Container));
28 final BoxDecoration decoration = container.decoration! as BoxDecoration;
29 expect(decoration.border!.bottom.width, 0.0);
30 });
31
32 testWidgets('Divider custom thickness', (WidgetTester tester) async {
33 await tester.pumpWidget(
34 const Directionality(
35 textDirection: TextDirection.ltr,
36 child: Center(child: Divider(thickness: 5.0)),
37 ),
38 );
39 final Container container = tester.widget(find.byType(Container));
40 final BoxDecoration decoration = container.decoration! as BoxDecoration;
41 expect(decoration.border!.bottom.width, 5.0);
42 });
43
44 testWidgets('Divider custom radius', (WidgetTester tester) async {
45 await tester.pumpWidget(
46 Directionality(
47 textDirection: TextDirection.ltr,
48 child: Center(child: Divider(radius: BorderRadius.circular(5))),
49 ),
50 );
51 final Container container = tester.widget(find.byType(Container));
52 final BoxDecoration decoration = container.decoration! as BoxDecoration;
53 final BorderRadius borderRadius = decoration.borderRadius! as BorderRadius;
54 expect(borderRadius.bottomLeft, const Radius.circular(5));
55 expect(borderRadius.bottomRight, const Radius.circular(5));
56 expect(borderRadius.topLeft, const Radius.circular(5));
57 expect(borderRadius.topRight, const Radius.circular(5));
58 });
59
60 testWidgets('Horizontal divider custom indentation', (WidgetTester tester) async {
61 const double customIndent = 10.0;
62 Rect dividerRect;
63 Rect lineRect;
64
65 await tester.pumpWidget(
66 const Directionality(
67 textDirection: TextDirection.ltr,
68 child: Center(child: Divider(indent: customIndent)),
69 ),
70 );
71 // The divider line is drawn with a DecoratedBox with a border
72 dividerRect = tester.getRect(find.byType(Divider));
73 lineRect = tester.getRect(find.byType(DecoratedBox));
74 expect(lineRect.left, dividerRect.left + customIndent);
75 expect(lineRect.right, dividerRect.right);
76
77 await tester.pumpWidget(
78 const Directionality(
79 textDirection: TextDirection.ltr,
80 child: Center(child: Divider(endIndent: customIndent)),
81 ),
82 );
83 dividerRect = tester.getRect(find.byType(Divider));
84 lineRect = tester.getRect(find.byType(DecoratedBox));
85 expect(lineRect.left, dividerRect.left);
86 expect(lineRect.right, dividerRect.right - customIndent);
87
88 await tester.pumpWidget(
89 const Directionality(
90 textDirection: TextDirection.ltr,
91 child: Center(
92 child: Divider(indent: customIndent, endIndent: customIndent),
93 ),
94 ),
95 );
96 dividerRect = tester.getRect(find.byType(Divider));
97 lineRect = tester.getRect(find.byType(DecoratedBox));
98 expect(lineRect.left, dividerRect.left + customIndent);
99 expect(lineRect.right, dividerRect.right - customIndent);
100 });
101
102 testWidgets('Material3 - Vertical Divider Test', (WidgetTester tester) async {
103 await tester.pumpWidget(const MaterialApp(home: Center(child: VerticalDivider())));
104 final RenderBox box = tester.firstRenderObject(find.byType(VerticalDivider));
105 expect(box.size.width, 16.0);
106 final Container container = tester.widget(find.byType(Container));
107 final BoxDecoration decoration = container.decoration! as BoxDecoration;
108 final Border border = decoration.border! as Border;
109 expect(border.left.width, 1.0);
110 });
111
112 testWidgets('Material2 - Vertical Divider Test', (WidgetTester tester) async {
113 await tester.pumpWidget(
114 MaterialApp(
115 theme: ThemeData(useMaterial3: false),
116 home: const Center(child: VerticalDivider()),
117 ),
118 );
119 final RenderBox box = tester.firstRenderObject(find.byType(VerticalDivider));
120 expect(box.size.width, 16.0);
121 final Container container = tester.widget(find.byType(Container));
122 final BoxDecoration decoration = container.decoration! as BoxDecoration;
123 final Border border = decoration.border! as Border;
124 expect(border.left.width, 0.0);
125 });
126
127 testWidgets('Divider custom thickness', (WidgetTester tester) async {
128 await tester.pumpWidget(
129 const Directionality(
130 textDirection: TextDirection.ltr,
131 child: Center(child: VerticalDivider(thickness: 5.0)),
132 ),
133 );
134 final Container container = tester.widget(find.byType(Container));
135 final BoxDecoration decoration = container.decoration! as BoxDecoration;
136 final Border border = decoration.border! as Border;
137 expect(border.left.width, 5.0);
138 });
139
140 testWidgets('Vertical Divider Test 2', (WidgetTester tester) async {
141 await tester.pumpWidget(
142 MaterialApp(
143 theme: ThemeData(useMaterial3: false),
144 home: const Material(
145 child: SizedBox(
146 height: 24.0,
147 child: Row(children: <Widget>[Text('Hey.'), VerticalDivider()]),
148 ),
149 ),
150 ),
151 );
152 final RenderBox box = tester.firstRenderObject(find.byType(VerticalDivider));
153 final RenderBox containerBox = tester.firstRenderObject(find.byType(Container).last);
154
155 expect(box.size.width, 16.0);
156 expect(containerBox.size.height, 600.0);
157 expect(find.byType(VerticalDivider), paints..path(strokeWidth: 0.0));
158 });
159
160 testWidgets('Vertical divider custom indentation', (WidgetTester tester) async {
161 const double customIndent = 10.0;
162 Rect dividerRect;
163 Rect lineRect;
164
165 await tester.pumpWidget(
166 const Directionality(
167 textDirection: TextDirection.ltr,
168 child: Center(child: VerticalDivider(indent: customIndent)),
169 ),
170 );
171 // The divider line is drawn with a DecoratedBox with a border
172 dividerRect = tester.getRect(find.byType(VerticalDivider));
173 lineRect = tester.getRect(find.byType(DecoratedBox));
174 expect(lineRect.top, dividerRect.top + customIndent);
175 expect(lineRect.bottom, dividerRect.bottom);
176
177 await tester.pumpWidget(
178 const Directionality(
179 textDirection: TextDirection.ltr,
180 child: Center(child: VerticalDivider(endIndent: customIndent)),
181 ),
182 );
183 dividerRect = tester.getRect(find.byType(VerticalDivider));
184 lineRect = tester.getRect(find.byType(DecoratedBox));
185 expect(lineRect.top, dividerRect.top);
186 expect(lineRect.bottom, dividerRect.bottom - customIndent);
187
188 await tester.pumpWidget(
189 const Directionality(
190 textDirection: TextDirection.ltr,
191 child: Center(
192 child: VerticalDivider(indent: customIndent, endIndent: customIndent),
193 ),
194 ),
195 );
196 dividerRect = tester.getRect(find.byType(VerticalDivider));
197 lineRect = tester.getRect(find.byType(DecoratedBox));
198 expect(lineRect.top, dividerRect.top + customIndent);
199 expect(lineRect.bottom, dividerRect.bottom - customIndent);
200 });
201
202 testWidgets('VerticalDivider custom radius', (WidgetTester tester) async {
203 await tester.pumpWidget(
204 Directionality(
205 textDirection: TextDirection.ltr,
206 child: Center(child: VerticalDivider(radius: BorderRadius.circular(5))),
207 ),
208 );
209 final Container container = tester.widget(find.byType(Container));
210 final BoxDecoration decoration = container.decoration! as BoxDecoration;
211 final BorderRadius borderRadius = decoration.borderRadius! as BorderRadius;
212 expect(borderRadius.bottomLeft, const Radius.circular(5));
213 expect(borderRadius.bottomRight, const Radius.circular(5));
214 expect(borderRadius.topLeft, const Radius.circular(5));
215 expect(borderRadius.topRight, const Radius.circular(5));
216 });
217
218 // Regression test for https://github.com/flutter/flutter/issues/39533
219 testWidgets('createBorderSide does not throw exception with null context', (
220 WidgetTester tester,
221 ) async {
222 // Passing a null context used to throw an exception but no longer does.
223 expect(() => Divider.createBorderSide(null), isNot(throwsAssertionError));
224 expect(() => Divider.createBorderSide(null), isNot(throwsNoSuchMethodError));
225 });
226}
227