3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
7 function test_Collection_constructor (t) {
11 coll = new OpenLayers.Geometry.Collection();
12 t.ok( coll instanceof OpenLayers.Geometry.Collection, "new OpenLayers.Geometry.Collection returns coll object" );
13 t.eq( coll.CLASS_NAME, "OpenLayers.Geometry.Collection", "coll.CLASS_NAME is set correctly");
14 t.eq( coll.components.length, 0, "coll.components is set correctly");
16 OpenLayers.Geometry.Collection.prototype._addComponents =
17 OpenLayers.Geometry.Collection.prototype.addComponents;
18 OpenLayers.Geometry.Collection.prototype.addComponents =
19 function(comps) { g_addcomponents = comps; };
22 g_addcomponents = null;
24 coll = new OpenLayers.Geometry.Collection(components);
25 t.ok(g_addcomponents, components, "addcomponents called on non-null param")
27 OpenLayers.Geometry.Collection.prototype.addComponents =
28 OpenLayers.Geometry.Collection.prototype._addComponents;
31 function test_Collection_addComponents (t) {
34 coll = new OpenLayers.Geometry.Collection();
37 coll.addComponents(null);
38 t.ok(true, "doesn't break on add null components");
40 OpenLayers.Geometry.Collection.prototype._addComponent =
41 OpenLayers.Geometry.Collection.prototype.addComponent;
43 OpenLayers.Geometry.Collection.prototype.addComponent =
44 function(comp) { g_addComp = comp; g_added++};
51 coll.addComponents(component);
52 t.eq(g_added, 1, "added once");
53 t.eq(g_addComp, component, "added component");
60 coll.addComponents([component1, component2]);
61 t.eq(g_added, 2, "added twice");
62 t.eq(g_addComp, component2, "added component");
64 OpenLayers.Geometry.Collection.prototype.addComponent =
65 OpenLayers.Geometry.Collection.prototype._addComponent;
69 coll.addComponents(new OpenLayers.Geometry.Point(0,0));
70 coll.addComponents(new OpenLayers.Geometry.Point(10,10));
71 t.eq( coll.components.length, 2, "added two components to collection" );
72 bounds = coll.getBounds();
73 t.eq( bounds.left, 0, "left bound is 0" );
74 t.eq( bounds.bottom, 0, "bottom bound is 0" );
75 t.eq( bounds.right, 10, "right bound is 10" );
76 t.eq( bounds.top, 10, "top bound is 10" );
79 function test_Collection_clone (t) {
81 coll = new OpenLayers.Geometry.Collection();
82 coll.addComponents(new OpenLayers.Geometry.Point(0,0));
83 coll.addComponents(new OpenLayers.Geometry.Point(10,10));
85 t.ok( coll2 instanceof OpenLayers.Geometry.Collection, "coll.clone() returns collection object" );
86 t.eq( coll2.components.length, 2, "coll2.components.length is set correctly");
87 t.ok( coll2.components[0] instanceof OpenLayers.Geometry.Point,
88 "coll2.components.length is set correctly");
91 function test_Collection_removeComponents (t) {
93 coll = new OpenLayers.Geometry.Collection();
94 point = new OpenLayers.Geometry.Point(0,0);
95 coll.addComponents(point);
96 coll.addComponents(new OpenLayers.Geometry.Point(10,10));
97 coll.removeComponents(coll.components[0]);
98 t.eq( coll.components.length, 1, "coll.components.length is smaller after removeComponent" );
99 t.ok( coll.bounds == null, "bounds are nullified after call to remove (to trigger recalc on getBounds()");
100 bounds = coll.getBounds();
101 t.eq( bounds.left, 10, "left bound is 10 after removeComponent" );
102 t.eq( bounds.bottom, 10, "bottom bound is 10 after removeComponent" );
104 coll = new OpenLayers.Geometry.Collection();
105 for(var i=0; i<5; ++i) {
107 new OpenLayers.Geometry.Point(Math.random(), Math.random())
110 coll.removeComponents(coll.components);
111 t.eq(coll.components.length, 0,
112 "remove components even works with multiple components");
116 function test_Collection_calculateBounds(t) {
119 var coll = new OpenLayers.Geometry.Collection();
120 coll.calculateBounds();
121 t.eq(coll.bounds, null, "null components list gives null bounds on calculation()");
123 var p1 = new OpenLayers.Geometry.Point(10,20);
124 var p2 = new OpenLayers.Geometry.Point(30,40);
126 var components = [p1, p2];
127 coll = new OpenLayers.Geometry.Collection(components);
129 coll.calculateBounds();
131 t.eq(coll.bounds.left, 10, "good left bounds");
132 t.eq(coll.bounds.bottom, 20, "good bottom bounds");
133 t.eq(coll.bounds.right, 30, "good right bounds");
134 t.eq(coll.bounds.top, 40, "good top bounds");
136 var newPoint = new OpenLayers.Geometry.Point(60,70);
137 coll.addComponent(newPoint);
138 coll.calculateBounds();
140 t.eq(coll.bounds.left, 10, "good left bounds");
141 t.eq(coll.bounds.bottom, 20, "good bottom bounds");
142 t.eq(coll.bounds.right, 60, "good right bounds");
143 t.eq(coll.bounds.top, 70, "good top bounds");
146 function test_Collection_equals(t) {
148 var geom = new OpenLayers.Geometry.Collection();
149 t.ok(!geom.equals(), "collection.equals() returns false for undefined");
152 function test_Collection_addComponent(t) {
155 var coll = new OpenLayers.Geometry.Collection();
158 coll.addComponent(null);
159 t.ok(!coll.addComponent(null),
160 "addComponent returns false for bad component")
163 var component = new OpenLayers.Geometry.Point(3,4);
164 t.ok(coll.addComponent(component),
165 "addComponent returns true for good component");
166 t.ok(coll.bounds == null, "bounds cache correctly cleared");
168 var foundComponent = false;
169 for(var i=0; i< coll.components.length; i++) {
170 if (coll.components[i].equals(component)) {
171 foundComponent = true;
174 t.ok(foundComponent, "component added to internal array");
176 // restricted components
177 coll.componentTypes = ["OpenLayers.Geometry.Point",
178 "OpenLayers.Geometry.LineString"];
179 var point1 = new OpenLayers.Geometry.Point(0,0);
180 var point2 = new OpenLayers.Geometry.Point(1,1);
181 var line = new OpenLayers.Geometry.LineString([point1, point2]);
182 var multipoint = new OpenLayers.Geometry.MultiPoint([point1, point2]);
184 t.ok(coll.addComponent(point1),
185 "addComponent returns true for 1st geometry type in componentTypes");
186 t.ok(OpenLayers.Util.indexOf(coll.components, point1) > -1,
187 "addComponent adds 1st restricted type to components array");
188 t.ok(coll.addComponent(line),
189 "addComponent returns true for 2nd geometry type in componentTypes");
190 t.ok(OpenLayers.Util.indexOf(coll.components, point1) > -1,
191 "addComponent adds 2nd restricted type to components array");
192 t.ok(!coll.addComponent(multipoint),
193 "addComponent returns false for geometry type not in componentTypes");
194 t.ok(OpenLayers.Util.indexOf(coll.components, multipoint) == -1,
195 "addComponent doesn't add restricted type to component array");
199 function test_collection_getLength(t) {
203 var coll = new OpenLayers.Geometry.Collection();
204 t.eq( coll.getLength(), 0, "null coll has 0 getlength");
208 { 'getLength': function() { return 50; } },
209 { 'getLength': function() { return 15; } }
211 t.eq( coll.getLength(), 65, "coll with valid components correctly sums getlength");
214 function test_collection_getArea(t) {
218 var coll = new OpenLayers.Geometry.Collection();
219 t.eq( coll.getArea(), 0, "null coll has 0 getArea");
223 { 'getArea': function() { return 50; } },
224 { 'getArea': function() { return 15; } }
226 t.eq( coll.getArea(), 65, "coll with valid components correctly sums getArea");
229 function test_transform(t) {
231 var p1 = new OpenLayers.Geometry.Point(0,0);
233 var p2 = new OpenLayers.Geometry.Point(1,1);
235 var line = new OpenLayers.Geometry.LineString([p1, p2]);
236 var multipoint = new OpenLayers.Geometry.MultiPoint([p1, p2]);
237 var coll = new OpenLayers.Geometry.Collection([
238 p1, p2, line, multipoint
242 var wgs84 = new OpenLayers.Projection("EPSG:4326");
243 var sm = new OpenLayers.Projection("EPSG:900913");
244 coll.transform(wgs84, sm);
246 t.eq(coll.bounds, null, "coll bounds cleared");
247 t.eq(p1.bounds, null, "p1 component bounds cleared");
248 t.eq(p2.bounds, null, "p2 component bounds cleared");
249 t.eq(line.bounds, null, "line component bounds cleared");
250 t.eq(multipoint.bounds, null, "multipoint component bounds cleared");
254 function test_Collection_destroy(t) {
256 coll = new OpenLayers.Geometry.Collection();
257 coll.components = {};
261 t.ok(coll.components == null, "components array cleared");