OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library rect_test; | |
6 | |
7 import 'dart:html'; | |
8 import '../../pkg/unittest/lib/unittest.dart'; | |
9 import '../../pkg/unittest/lib/html_config.dart'; | |
10 | |
11 main() { | |
12 useHtmlConfiguration(); | |
13 | |
14 Rect createRect(List<num> a) { | |
15 return a != null ? new Rect(a[0], a[1], a[2] - a[0], a[3] - a[1]) : null; | |
16 } | |
17 | |
18 test('construction', () { | |
19 var r0 = new Rect(10, 20, 30, 40); | |
20 expect(r0.toString(), '(10, 20, 30, 40)'); | |
21 expect(r0.right, 40); | |
22 expect(r0.bottom, 60); | |
23 | |
24 var r1 = new Rect.fromPoints(r0.topLeft, r0.bottomRight); | |
25 expect(r1, r0); | |
26 | |
27 var r2 = new Rect.fromPoints(r0.bottomRight, r0.topLeft); | |
28 expect(r2, r0); | |
29 }); | |
30 | |
31 test('intersection', () { | |
32 var tests = [ | |
33 [[10, 10, 20, 20], [15, 15, 25, 25], [15, 15, 20, 20]], | |
34 [[10, 10, 20, 20], [20, 0, 30, 10], [20, 10, 20, 10]], | |
35 [[0, 0, 1, 1], [10, 11, 12, 13], null], | |
36 [[11, 12, 98, 99], [22, 23, 34, 35], [22, 23, 34, 35]]]; | |
37 | |
38 for (var test in tests) { | |
39 var r0 = createRect(test[0]); | |
40 var r1 = createRect(test[1]); | |
41 var expected = createRect(test[2]); | |
42 | |
43 expect(r0.intersection(r1), expected); | |
44 expect(r1.intersection(r0), expected); | |
45 } | |
46 }); | |
47 | |
48 test('intersects', () { | |
49 var r0 = new Rect(10, 10, 20, 20); | |
50 var r1 = new Rect(15, 15, 25, 25); | |
51 var r2 = new Rect(0, 0, 1, 1); | |
52 | |
53 expect(r0.intersects(r1), isTrue); | |
54 expect(r1.intersects(r0), isTrue); | |
55 | |
56 expect(r0.intersects(r2), isFalse); | |
57 expect(r2.intersects(r0), isFalse); | |
58 }); | |
59 | |
60 test('union', () { | |
61 var tests = [ | |
62 [[10, 10, 20, 20], [15, 15, 25, 25], [10, 10, 25, 25]], | |
63 [[10, 10, 20, 20], [20, 0, 30, 10], [10, 0, 30, 20]], | |
64 [[0, 0, 1, 1], [10, 11, 12, 13], [0, 0, 12, 13]], | |
65 [[11, 12, 98, 99], [22, 23, 34, 35], [11, 12, 98, 99]]]; | |
66 | |
67 for (var test in tests) { | |
68 var r0 = createRect(test[0]); | |
69 var r1 = createRect(test[1]); | |
70 var expected = createRect(test[2]); | |
71 | |
72 expect(r0.union(r1), expected); | |
73 expect(r1.union(r0), expected); | |
74 } | |
75 }); | |
76 | |
77 test('containsRect', () { | |
78 var r = new Rect(-10, 0, 20, 10); | |
79 expect(r.containsRect(r), isTrue); | |
80 | |
81 expect(r.containsRect( | |
82 new Rect(double.NAN, double.NAN, double.NAN, double.NAN)), isFalse); | |
83 | |
84 var r2 = new Rect(0, 2, 5, 5); | |
85 expect(r.containsRect(r2), isTrue); | |
86 expect(r2.containsRect(r), isFalse); | |
87 | |
88 r2 = new Rect(-11, 2, 5, 5); | |
89 expect(r.containsRect(r2), isFalse); | |
90 r2 = new Rect(0, 2, 15, 5); | |
91 expect(r.containsRect(r2), isFalse); | |
92 r2 = new Rect(0, 2, 5, 10); | |
93 expect(r.containsRect(r2), isFalse); | |
94 r2 = new Rect(0, 0, 5, 10); | |
95 expect(r.containsRect(r2), isTrue); | |
96 }); | |
97 | |
98 test('containsPoint', () { | |
99 var r = new Rect(20, 40, 60, 80); | |
100 | |
101 // Test middle. | |
102 expect(r.containsPoint(new Point(50, 80)), isTrue); | |
103 | |
104 // Test edges. | |
105 expect(r.containsPoint(new Point(20, 40)), isTrue); | |
106 expect(r.containsPoint(new Point(50, 40)), isTrue); | |
107 expect(r.containsPoint(new Point(80, 40)), isTrue); | |
108 expect(r.containsPoint(new Point(80, 80)), isTrue); | |
109 expect(r.containsPoint(new Point(80, 120)), isTrue); | |
110 expect(r.containsPoint(new Point(50, 120)), isTrue); | |
111 expect(r.containsPoint(new Point(20, 120)), isTrue); | |
112 expect(r.containsPoint(new Point(20, 80)), isTrue); | |
113 | |
114 // Test outside. | |
115 expect(r.containsPoint(new Point(0, 0)), isFalse); | |
116 expect(r.containsPoint(new Point(50, 0)), isFalse); | |
117 expect(r.containsPoint(new Point(100, 0)), isFalse); | |
118 expect(r.containsPoint(new Point(100, 80)), isFalse); | |
119 expect(r.containsPoint(new Point(100, 160)), isFalse); | |
120 expect(r.containsPoint(new Point(50, 160)), isFalse); | |
121 expect(r.containsPoint(new Point(0, 160)), isFalse); | |
122 expect(r.containsPoint(new Point(0, 80)), isFalse); | |
123 }); | |
124 | |
125 test('ceil', () { | |
126 var rect = new Rect(11.4, 26.6, 17.8, 9.2); | |
127 expect(rect.ceil(), new Rect(12.0, 27.0, 18.0, 10.0)); | |
128 }); | |
129 | |
130 test('floor', () { | |
131 var rect = new Rect(11.4, 26.6, 17.8, 9.2); | |
132 expect(rect.floor(), new Rect(11.0, 26.0, 17.0, 9.0)); | |
133 }); | |
134 | |
135 test('round', () { | |
136 var rect = new Rect(11.4, 26.6, 17.8, 9.2); | |
137 expect(rect.round(), new Rect(11.0, 27.0, 18.0, 9.0)); | |
138 }); | |
139 | |
140 test('toInt', () { | |
141 var rect = new Rect(11.4, 26.6, 17.8, 9.2); | |
142 var b = rect.toInt(); | |
143 expect(b, new Rect(11, 26, 17, 9)); | |
144 | |
145 expect(b.left is int, isTrue); | |
146 expect(b.top is int, isTrue); | |
147 expect(b.width is int, isTrue); | |
148 expect(b.height is int, isTrue); | |
149 }); | |
150 | |
151 test('hashCode', () { | |
152 var a = new Rect(0, 1, 2, 3); | |
153 var b = new Rect(0, 1, 2, 3); | |
154 expect(a.hashCode, b.hashCode); | |
155 | |
156 var c = new Rect(1, 0, 2, 3); | |
157 expect(a.hashCode == c.hashCode, isFalse); | |
158 }); | |
159 } | |
OLD | NEW |