• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2013 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:test/test.dart';
6import 'package:ui/ui.dart';
7
8void main() {
9  test('Should have no subpaths when created', () {
10    final Path path = Path();
11    expect(path.subpaths.length, 0);
12  });
13
14  test('LineTo should add command', () {
15    final Path path = Path();
16    path.moveTo(5.0, 10.0);
17    path.lineTo(20.0, 40.0);
18    path.lineTo(30.0, 50.0);
19    expect(path.subpaths.length, 1);
20    expect(path.subpaths[0].currentX, 30.0);
21    expect(path.subpaths[0].currentY, 50.0);
22  });
23
24  test('LineTo should add moveTo 0,0 when first call to Path API', () {
25    final Path path = Path();
26    path.lineTo(20.0, 40.0);
27    expect(path.subpaths.length, 1);
28    expect(path.subpaths[0].currentX, 20.0);
29    expect(path.subpaths[0].currentY, 40.0);
30  });
31
32  test('relativeLineTo should increments currentX', () {
33    final Path path = Path();
34    path.moveTo(5.0, 10.0);
35    path.lineTo(20.0, 40.0);
36    path.relativeLineTo(5.0, 5.0);
37    expect(path.subpaths.length, 1);
38    expect(path.subpaths[0].currentX, 25.0);
39    expect(path.subpaths[0].currentY, 45.0);
40  });
41
42  test('Should allow calling relativeLineTo before moveTo', () {
43    final Path path = Path();
44    path.relativeLineTo(5.0, 5.0);
45    path.moveTo(5.0, 10.0);
46    expect(path.subpaths.length, 2);
47    expect(path.subpaths[0].currentX, 5.0);
48    expect(path.subpaths[0].currentY, 5.0);
49    expect(path.subpaths[1].currentX, 5.0);
50    expect(path.subpaths[1].currentY, 10.0);
51  });
52
53  test('Should allow relativeLineTo after reset', () {
54    final Path path = Path();
55    final Path subPath = Path();
56    subPath.moveTo(50.0, 60.0);
57    subPath.lineTo(200.0, 200.0);
58    path.extendWithPath(subPath, Offset(0.0, 0.0));
59    path.reset();
60    path.relativeLineTo(5.0, 5.0);
61    expect(path.subpaths.length, 1);
62    expect(path.subpaths[0].currentX, 5.0);
63    expect(path.subpaths[0].currentY, 5.0);
64  });
65
66  test('Should detect rectangular path', () {
67    final Path path = Path();
68    path.addRect(Rect.fromLTWH(1.0, 2.0, 3.0, 4.0));
69    expect(path.webOnlyPathAsRect, Rect.fromLTWH(1.0, 2.0, 3.0, 4.0));
70  });
71
72  test('Should detect non rectangular path if empty', () {
73    final Path path = Path();
74    expect(path.webOnlyPathAsRect, null);
75  });
76
77  test('Should detect non rectangular path if there are multiple subpaths', () {
78    final Path path = Path();
79    path.addRect(Rect.fromLTWH(1.0, 2.0, 3.0, 4.0));
80    path.addRect(Rect.fromLTWH(5.0, 6.0, 7.0, 8.0));
81    expect(path.webOnlyPathAsRect, null);
82  });
83
84  test('Should detect rounded rectangular path', () {
85    final Path path = Path();
86    path.addRRect(RRect.fromRectAndRadius(
87        Rect.fromLTWH(1.0, 2.0, 3.0, 4.0), Radius.circular(2.0)));
88    expect(
89        path.webOnlyPathAsRoundedRect,
90        RRect.fromRectAndRadius(
91            Rect.fromLTWH(1.0, 2.0, 3.0, 4.0), Radius.circular(2.0)));
92  });
93
94  test('Should detect non rounded rectangular path if empty', () {
95    final Path path = new Path();
96    expect(path.webOnlyPathAsRoundedRect, null);
97  });
98
99  test('Should detect rectangular path is not round', () {
100    final Path path = new Path();
101    path.addRect(Rect.fromLTWH(1.0, 2.0, 3.0, 4.0));
102    expect(path.webOnlyPathAsRoundedRect, null);
103  });
104
105  test(
106      'Should detect non rounded  rectangular path if there are '
107      'multiple subpaths', () {
108    final Path path = new Path();
109    path.addRRect(RRect.fromRectAndRadius(
110        Rect.fromLTWH(1.0, 2.0, 3.0, 4.0), Radius.circular(2.0)));
111    path.addRRect(RRect.fromRectAndRadius(
112        Rect.fromLTWH(1.0, 2.0, 3.0, 4.0), Radius.circular(2.0)));
113    expect(path.webOnlyPathAsRoundedRect, null);
114  });
115
116  test('Should compute bounds as empty for empty and moveTo only path', () {
117    final Path emptyPath = new Path();
118    expect(emptyPath.getBounds(), Rect.zero);
119
120    final Path path = new Path();
121    path.moveTo(50, 60);
122    expect(path.getBounds(), Rect.fromLTRB(50, 60, 50, 60));
123  });
124
125  test('Should compute bounds for lines', () {
126    final Path path = new Path();
127    path.moveTo(25, 30);
128    path.lineTo(100, 200);
129    expect(path.getBounds(), Rect.fromLTRB(25, 30, 100, 200));
130
131    final Path path2 = new Path();
132    path2.moveTo(250, 300);
133    path2.lineTo(50, 60);
134    expect(path2.getBounds(), Rect.fromLTRB(50, 60, 250, 300));
135  });
136
137  // test('Should compute bounds for quadraticBezierTo', () {
138  //   final Path path1 = new Path();
139  //   path1.moveTo(285.2, 682.1);
140  //   path1.quadraticBezierTo(432.0, 431.4, 594.9, 681.2);
141  //   expect(
142  //       path1.getBounds(),
143  //       within<Rect>(
144  //           distance: 0.1, from: Rect.fromLTRB(285.2, 556.5, 594.9, 682.1)));
145
146  //   // Control point below start , end.
147  //   final Path path2 = new Path();
148  //   path2.moveTo(285.2, 682.1);
149  //   path2.quadraticBezierTo(447.4, 946.8, 594.9, 681.2);
150  //   expect(
151  //       path2.getBounds(),
152  //       within<Rect>(
153  //           distance: 0.1, from: Rect.fromLTRB(285.2, 681.2, 594.9, 814.2)));
154
155  //   // Control point to the right of end point.
156  //   final Path path3 = new Path();
157  //   path3.moveTo(468.3, 685.6);
158  //   path3.quadraticBezierTo(644.7, 555.2, 594.9, 681.2);
159  //   expect(
160  //       path3.getBounds(),
161  //       within<Rect>(
162  //           distance: 0.1, from: Rect.fromLTRB(468.3, 619.3, 605.9, 685.6)));
163  // });
164
165  // test('Should compute bounds for cubicTo', () {
166  //   final Path path1 = new Path();
167  //   path1.moveTo(220, 300);
168  //   path1.cubicTo(230, 120, 400, 125, 410, 280);
169  //   expect(
170  //       path1.getBounds(),
171  //       within<Rect>(
172  //           distance: 0.1, from: Rect.fromLTRB(220.0, 164.3, 410.0, 300.0)));
173
174  //   // control point 1 to the right of control point 2
175  //   final Path path2 = new Path();
176  //   path2.moveTo(220, 300);
177  //   path2.cubicTo(564.2, 13.7, 400.0, 125.0, 410.0, 280.0);
178  //   expect(
179  //       path2.getBounds(),
180  //       within<Rect>(
181  //           distance: 0.1, from: Rect.fromLTRB(220.0, 122.8, 440.5, 300.0)));
182
183  //   // control point 1 to the right of control point 2 inflection
184  //   final Path path3 = new Path();
185  //   path3.moveTo(220, 300);
186  //   path3.cubicTo(839.8, 67.9, 400.0, 125.0, 410.0, 280.0);
187  //   expect(
188  //       path3.getBounds(),
189  //       within<Rect>(
190  //           distance: 0.1, from: Rect.fromLTRB(220.0, 144.5, 552.1, 300.0)));
191
192  //   // control point 1 below and between start and end points
193  //   final Path path4 = new Path();
194  //   path4.moveTo(220.0, 300.0);
195  //   path4.cubicTo(354.8, 388.3, 400.0, 125.0, 410.0, 280.0);
196  //   expect(
197  //       path4.getBounds(),
198  //       within<Rect>(
199  //           distance: 0.1, from: Rect.fromLTRB(220.0, 230.0, 410.0, 318.6)));
200
201  //   // control points inverted below
202  //   final Path path5 = new Path();
203  //   path5.moveTo(220.0, 300.0);
204  //   path5.cubicTo(366.5, 487.3, 256.4, 489.9, 410.0, 280.0);
205  //   expect(
206  //       path5.getBounds(),
207  //       within<Rect>(
208  //           distance: 0.1, from: Rect.fromLTRB(220.0, 280.0, 410.0, 439.0)));
209
210  //   // control points inverted below wide
211  //   final Path path6 = new Path();
212  //   path6.moveTo(220.0, 300.0);
213  //   path6.cubicTo(496.1, 485.5, 121.4, 491.6, 410.0, 280.0);
214  //   expect(
215  //       path6.getBounds(),
216  //       within<Rect>(
217  //           distance: 0.1, from: Rect.fromLTRB(220.0, 280.0, 410.0, 439.0)));
218
219  //   // control point 2 and end point swapped
220  //   final Path path7 = new Path();
221  //   path7.moveTo(220.0, 300.0);
222  //   path7.cubicTo(230.0, 120.0, 394.5, 296.1, 382.3, 124.1);
223  //   expect(
224  //       path7.getBounds(),
225  //       within<Rect>(
226  //           distance: 0.1, from: Rect.fromLTRB(220.0, 124.1, 382.9, 300.0)));
227  // });
228}
229