| 1 |
<html> |
|---|
| 2 |
<head> |
|---|
| 3 |
<script src="../../lib/OpenLayers.js"></script> |
|---|
| 4 |
<script type="text/javascript"> |
|---|
| 5 |
var coll; |
|---|
| 6 |
|
|---|
| 7 |
function test_Collection_constructor (t) { |
|---|
| 8 |
t.plan( 4 ); |
|---|
| 9 |
|
|---|
| 10 |
//null param |
|---|
| 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"); |
|---|
| 15 |
|
|---|
| 16 |
OpenLayers.Geometry.Collection.prototype._addComponents = |
|---|
| 17 |
OpenLayers.Geometry.Collection.prototype.addComponents; |
|---|
| 18 |
OpenLayers.Geometry.Collection.prototype.addComponents = |
|---|
| 19 |
function(comps) { g_addcomponents = comps; }; |
|---|
| 20 |
|
|---|
| 21 |
//valid param |
|---|
| 22 |
g_addcomponents = null; |
|---|
| 23 |
var components = {}; |
|---|
| 24 |
coll = new OpenLayers.Geometry.Collection(components); |
|---|
| 25 |
t.ok(g_addcomponents, components, "addcomponents called on non-null param") |
|---|
| 26 |
|
|---|
| 27 |
OpenLayers.Geometry.Collection.prototype.addComponents = |
|---|
| 28 |
OpenLayers.Geometry.Collection.prototype._addComponents; |
|---|
| 29 |
} |
|---|
| 30 |
|
|---|
| 31 |
function test_Collection_addComponents (t) { |
|---|
| 32 |
t.plan( 10 ); |
|---|
| 33 |
|
|---|
| 34 |
coll = new OpenLayers.Geometry.Collection(); |
|---|
| 35 |
|
|---|
| 36 |
//null |
|---|
| 37 |
coll.addComponents(null); |
|---|
| 38 |
t.ok(true, "doesn't break on add null components"); |
|---|
| 39 |
|
|---|
| 40 |
OpenLayers.Geometry.Collection.prototype._addComponent = |
|---|
| 41 |
OpenLayers.Geometry.Collection.prototype.addComponent; |
|---|
| 42 |
|
|---|
| 43 |
OpenLayers.Geometry.Collection.prototype.addComponent = |
|---|
| 44 |
function(comp) { g_addComp = comp; g_added++}; |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
//nonarray argument |
|---|
| 48 |
var g_added = 0; |
|---|
| 49 |
var g_addComp = 0; |
|---|
| 50 |
var component = {}; |
|---|
| 51 |
coll.addComponents(component); |
|---|
| 52 |
t.eq(g_added, 1, "added once"); |
|---|
| 53 |
t.eq(g_addComp, component, "added component"); |
|---|
| 54 |
|
|---|
| 55 |
//array arg |
|---|
| 56 |
var g_added = 0; |
|---|
| 57 |
var g_addComp = 0; |
|---|
| 58 |
var component1 = {}; |
|---|
| 59 |
var component2 = {}; |
|---|
| 60 |
coll.addComponents([component1, component2]); |
|---|
| 61 |
t.eq(g_added, 2, "added twice"); |
|---|
| 62 |
t.eq(g_addComp, component2, "added component"); |
|---|
| 63 |
|
|---|
| 64 |
OpenLayers.Geometry.Collection.prototype.addComponent = |
|---|
| 65 |
OpenLayers.Geometry.Collection.prototype._addComponent; |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 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" ); |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
function test_Collection_clone (t) { |
|---|
| 80 |
t.plan( 3 ); |
|---|
| 81 |
coll = new OpenLayers.Geometry.Collection(); |
|---|
| 82 |
coll.addComponents(new OpenLayers.Geometry.Point(0,0)); |
|---|
| 83 |
coll.addComponents(new OpenLayers.Geometry.Point(10,10)); |
|---|
| 84 |
coll2 = coll.clone(); |
|---|
| 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"); |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
function test_Collection_removeComponents (t) { |
|---|
| 92 |
t.plan( 5 ); |
|---|
| 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" ); |
|---|
| 103 |
|
|---|
| 104 |
coll = new OpenLayers.Geometry.Collection(); |
|---|
| 105 |
for(var i=0; i<5; ++i) { |
|---|
| 106 |
coll.addComponents( |
|---|
| 107 |
new OpenLayers.Geometry.Point(Math.random(), Math.random()) |
|---|
| 108 |
); |
|---|
| 109 |
} |
|---|
| 110 |
coll.removeComponents(coll.components); |
|---|
| 111 |
t.eq(coll.components.length, 0, |
|---|
| 112 |
"remove components even works with multiple components"); |
|---|
| 113 |
|
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
function test_Collection_calculateBounds(t) { |
|---|
| 117 |
t.plan( 9 ); |
|---|
| 118 |
|
|---|
| 119 |
var coll = new OpenLayers.Geometry.Collection(); |
|---|
| 120 |
coll.calculateBounds(); |
|---|
| 121 |
t.eq(coll.bounds, null, "null components list gives null bounds on calculation()"); |
|---|
| 122 |
|
|---|
| 123 |
var p1 = new OpenLayers.Geometry.Point(10,20); |
|---|
| 124 |
var p2 = new OpenLayers.Geometry.Point(30,40); |
|---|
| 125 |
|
|---|
| 126 |
var components = [p1, p2]; |
|---|
| 127 |
coll = new OpenLayers.Geometry.Collection(components); |
|---|
| 128 |
|
|---|
| 129 |
coll.calculateBounds(); |
|---|
| 130 |
|
|---|
| 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"); |
|---|
| 135 |
|
|---|
| 136 |
var newPoint = new OpenLayers.Geometry.Point(60,70); |
|---|
| 137 |
coll.addComponent(newPoint); |
|---|
| 138 |
coll.calculateBounds(); |
|---|
| 139 |
|
|---|
| 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"); |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
function test_Collection_equals(t) { |
|---|
| 147 |
t.plan(1); |
|---|
| 148 |
var geom = new OpenLayers.Geometry.Collection(); |
|---|
| 149 |
t.ok(!geom.equals(), "collection.equals() returns false for undefined"); |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
function test_Collection_addComponent(t) { |
|---|
| 153 |
t.plan(10); |
|---|
| 154 |
|
|---|
| 155 |
var coll = new OpenLayers.Geometry.Collection(); |
|---|
| 156 |
|
|---|
| 157 |
//null |
|---|
| 158 |
coll.addComponent(null); |
|---|
| 159 |
t.ok(!coll.addComponent(null), |
|---|
| 160 |
"addComponent returns false for bad component") |
|---|
| 161 |
|
|---|
| 162 |
//good 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"); |
|---|
| 167 |
|
|---|
| 168 |
var foundComponent = false; |
|---|
| 169 |
for(var i=0; i< coll.components.length; i++) { |
|---|
| 170 |
if (coll.components[i].equals(component)) { |
|---|
| 171 |
foundComponent = true; |
|---|
| 172 |
} |
|---|
| 173 |
} |
|---|
| 174 |
t.ok(foundComponent, "component added to internal array"); |
|---|
| 175 |
|
|---|
| 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]); |
|---|
| 183 |
|
|---|
| 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"); |
|---|
| 196 |
|
|---|
| 197 |
} |
|---|
| 198 |
|
|---|
| 199 |
function test_collection_getLength(t) { |
|---|
| 200 |
t.plan(2); |
|---|
| 201 |
|
|---|
| 202 |
//null |
|---|
| 203 |
var coll = new OpenLayers.Geometry.Collection(); |
|---|
| 204 |
t.eq( coll.getLength(), 0, "null coll has 0 getlength"); |
|---|
| 205 |
|
|---|
| 206 |
//valid |
|---|
| 207 |
coll.components = [ |
|---|
| 208 |
{ 'getLength': function() { return 50; } }, |
|---|
| 209 |
{ 'getLength': function() { return 15; } } |
|---|
| 210 |
]; |
|---|
| 211 |
t.eq( coll.getLength(), 65, "coll with valid components correctly sums getlength"); |
|---|
| 212 |
} |
|---|
| 213 |
|
|---|
| 214 |
function test_collection_getArea(t) { |
|---|
| 215 |
t.plan(2); |
|---|
| 216 |
|
|---|
| 217 |
//null |
|---|
| 218 |
var coll = new OpenLayers.Geometry.Collection(); |
|---|
| 219 |
t.eq( coll.getArea(), 0, "null coll has 0 getArea"); |
|---|
| 220 |
|
|---|
| 221 |
//valid |
|---|
| 222 |
coll.components = [ |
|---|
| 223 |
{ 'getArea': function() { return 50; } }, |
|---|
| 224 |
{ 'getArea': function() { return 15; } } |
|---|
| 225 |
]; |
|---|
| 226 |
t.eq( coll.getArea(), 65, "coll with valid components correctly sums getArea"); |
|---|
| 227 |
} |
|---|
| 228 |
|
|---|
| 229 |
function test_transform(t) { |
|---|
| 230 |
t.plan(5); |
|---|
| 231 |
var p1 = new OpenLayers.Geometry.Point(0,0); |
|---|
| 232 |
p1.bounds = "foo"; |
|---|
| 233 |
var p2 = new OpenLayers.Geometry.Point(1,1); |
|---|
| 234 |
p2.bounds = "foo"; |
|---|
| 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 |
|---|
| 239 |
]); |
|---|
| 240 |
coll.bounds = "foo"; |
|---|
| 241 |
|
|---|
| 242 |
var wgs84 = new OpenLayers.Projection("EPSG:4326"); |
|---|
| 243 |
var sm = new OpenLayers.Projection("EPSG:900913"); |
|---|
| 244 |
coll.transform(wgs84, sm); |
|---|
| 245 |
|
|---|
| 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"); |
|---|
| 251 |
|
|---|
| 252 |
} |
|---|
| 253 |
|
|---|
| 254 |
function test_Collection_destroy(t) { |
|---|
| 255 |
t.plan( 1 ); |
|---|
| 256 |
coll = new OpenLayers.Geometry.Collection(); |
|---|
| 257 |
coll.components = {}; |
|---|
| 258 |
|
|---|
| 259 |
coll.destroy(); |
|---|
| 260 |
|
|---|
| 261 |
t.ok(coll.components == null, "components array cleared"); |
|---|
| 262 |
|
|---|
| 263 |
} |
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
</script> |
|---|
| 267 |
</head> |
|---|
| 268 |
<body> |
|---|
| 269 |
</body> |
|---|
| 270 |
</html> |
|---|