| 1 |
<html> |
|---|
| 2 |
<head> |
|---|
| 3 |
<script src="../../lib/OpenLayers.js"></script> |
|---|
| 4 |
<script type="text/javascript"> |
|---|
| 5 |
var line; |
|---|
| 6 |
var components = [new OpenLayers.Geometry.Point(10,15), |
|---|
| 7 |
new OpenLayers.Geometry.Point(0,0)]; |
|---|
| 8 |
|
|---|
| 9 |
function test_LineString_constructor (t) { |
|---|
| 10 |
t.plan( 3 ); |
|---|
| 11 |
line = new OpenLayers.Geometry.LineString(); |
|---|
| 12 |
t.ok( line instanceof OpenLayers.Geometry.LineString, "new OpenLayers.Geometry.LineString returns line object" ); |
|---|
| 13 |
t.eq( line.CLASS_NAME, "OpenLayers.Geometry.LineString", "line.CLASS_NAME is set correctly"); |
|---|
| 14 |
t.eq( line.components, [], "line.components is set correctly"); |
|---|
| 15 |
} |
|---|
| 16 |
|
|---|
| 17 |
function test_LineString_constructor (t) { |
|---|
| 18 |
t.plan( 3 ); |
|---|
| 19 |
line = new OpenLayers.Geometry.LineString(components); |
|---|
| 20 |
t.ok( line instanceof OpenLayers.Geometry.LineString, "new OpenLayers.Geometry.LineString returns line object" ); |
|---|
| 21 |
t.eq( line.CLASS_NAME, "OpenLayers.Geometry.LineString", "line.CLASS_NAME is set correctly"); |
|---|
| 22 |
// TBD FIXME, recursion |
|---|
| 23 |
// t.eq( line.components, components, "line.components is set correctly"); |
|---|
| 24 |
t.eq( line.components.length, 2, "line.components.length is set correctly"); |
|---|
| 25 |
} |
|---|
| 26 |
|
|---|
| 27 |
function test_LineString_toString(t) { |
|---|
| 28 |
t.plan(1); |
|---|
| 29 |
|
|---|
| 30 |
line = new OpenLayers.Geometry.LineString(components); |
|---|
| 31 |
t.eq(line.toString(), |
|---|
| 32 |
"LINESTRING(10 15,0 0)", |
|---|
| 33 |
"toString() returns WKT"); |
|---|
| 34 |
} |
|---|
| 35 |
|
|---|
| 36 |
function test_LineString_removeComponent(t) { |
|---|
| 37 |
t.plan(2); |
|---|
| 38 |
|
|---|
| 39 |
OpenLayers.Geometry.Collection.prototype._removeComponent = |
|---|
| 40 |
OpenLayers.Geometry.Collection.prototype.removeComponent; |
|---|
| 41 |
OpenLayers.Geometry.Collection.prototype.removeComponent = |
|---|
| 42 |
function(point) { g_removeComponent = point; }; |
|---|
| 43 |
|
|---|
| 44 |
line = new OpenLayers.Geometry.LineString(components); |
|---|
| 45 |
|
|---|
| 46 |
g_removeComponent = null; |
|---|
| 47 |
line.removeComponent(components[0]); |
|---|
| 48 |
t.ok(g_removeComponent == null, "point not removed if only 2 points in components"); |
|---|
| 49 |
|
|---|
| 50 |
line.components.push(new OpenLayers.Geometry.Point(4,4)); |
|---|
| 51 |
line.removeComponent(components[0]); |
|---|
| 52 |
t.ok(g_removeComponent, components[0], "point removed if 3 points in components"); |
|---|
| 53 |
|
|---|
| 54 |
OpenLayers.Geometry.Collection.prototype.removeComponent = |
|---|
| 55 |
OpenLayers.Geometry.Collection.prototype._removeComponent; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
function test_LineString_move(t) { |
|---|
| 59 |
t.plan(4); |
|---|
| 60 |
|
|---|
| 61 |
var components = [new OpenLayers.Geometry.Point(10,15), |
|---|
| 62 |
new OpenLayers.Geometry.Point(0,0)]; |
|---|
| 63 |
var line = new OpenLayers.Geometry.LineString(components); |
|---|
| 64 |
|
|---|
| 65 |
var x0 = components[0].x; |
|---|
| 66 |
var y0 = components[0].y; |
|---|
| 67 |
var x1 = components[1].x; |
|---|
| 68 |
var y1 = components[1].y; |
|---|
| 69 |
|
|---|
| 70 |
var dx = 10 * Math.random(); |
|---|
| 71 |
var dy = 10 * Math.random(); |
|---|
| 72 |
line.move(dx, dy); |
|---|
| 73 |
|
|---|
| 74 |
t.eq(line.components[0].x, x0 + dx, "move() correctly modifies first x"); |
|---|
| 75 |
t.eq(line.components[0].y, y0 + dy, "move() correctly modifies first y"); |
|---|
| 76 |
t.eq(line.components[1].x, x1 + dx, "move() correctly modifies second x"); |
|---|
| 77 |
t.eq(line.components[1].y, y1 + dy, "move() correctly modifies second y"); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
function test_LineString_rotate(t) { |
|---|
| 81 |
t.plan(6); |
|---|
| 82 |
|
|---|
| 83 |
var components = [new OpenLayers.Geometry.Point(10,15), |
|---|
| 84 |
new OpenLayers.Geometry.Point(0,0)]; |
|---|
| 85 |
var geometry = new OpenLayers.Geometry.LineString(components); |
|---|
| 86 |
|
|---|
| 87 |
var originals = []; |
|---|
| 88 |
var comp; |
|---|
| 89 |
var angle = 2 * Math.PI * Math.random(); |
|---|
| 90 |
var origin = new OpenLayers.Geometry.Point(10 * Math.random(), |
|---|
| 91 |
10 * Math.random()); |
|---|
| 92 |
for(var i=0; i<geometry.components.length; ++i) { |
|---|
| 93 |
comp = geometry.components[i]; |
|---|
| 94 |
originals[i] = comp.rotate; |
|---|
| 95 |
comp.rotate = function(a, o) { |
|---|
| 96 |
t.ok(true, "rotate called for component " + i); |
|---|
| 97 |
t.ok(a == angle, "rotate called with correct angle"); |
|---|
| 98 |
t.ok(o == origin, "rotate called with correct origin"); |
|---|
| 99 |
} |
|---|
| 100 |
} |
|---|
| 101 |
geometry.rotate(angle, origin); |
|---|
| 102 |
|
|---|
| 103 |
// restore the original rotate defs |
|---|
| 104 |
for(var i=0; i<geometry.components.length; ++i) { |
|---|
| 105 |
comp.rotate = originals[i]; |
|---|
| 106 |
} |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
function test_LineString_resize(t) { |
|---|
| 110 |
t.plan(7); |
|---|
| 111 |
|
|---|
| 112 |
var tolerance = 1e-10; |
|---|
| 113 |
|
|---|
| 114 |
var components = [new OpenLayers.Geometry.Point(10 * Math.random(), |
|---|
| 115 |
10 * Math.random()), |
|---|
| 116 |
new OpenLayers.Geometry.Point(10 * Math.random(), |
|---|
| 117 |
10 * Math.random())]; |
|---|
| 118 |
var geometry = new OpenLayers.Geometry.LineString(components); |
|---|
| 119 |
|
|---|
| 120 |
var origin = new OpenLayers.Geometry.Point(10 * Math.random(), |
|---|
| 121 |
10 * Math.random()); |
|---|
| 122 |
|
|---|
| 123 |
var scale = 10 * Math.random(); |
|---|
| 124 |
|
|---|
| 125 |
var oldLength = geometry.getLength(); |
|---|
| 126 |
geometry.resize(scale, origin); |
|---|
| 127 |
var newLength = geometry.getLength(); |
|---|
| 128 |
t.ok((((newLength / oldLength) - scale) / scale) < tolerance, |
|---|
| 129 |
"resize correctly changes the length of a linestring") |
|---|
| 130 |
|
|---|
| 131 |
var originals = []; |
|---|
| 132 |
var comp; |
|---|
| 133 |
for(var i=0; i<geometry.components.length; ++i) { |
|---|
| 134 |
comp = geometry.components[i]; |
|---|
| 135 |
originals[i] = comp.resize; |
|---|
| 136 |
comp.resize = function(s, o) { |
|---|
| 137 |
t.ok(true, "resize called for component " + i); |
|---|
| 138 |
t.ok(s == scale, "resize called with correct scale"); |
|---|
| 139 |
t.ok(o == origin, "resize called with correct origin"); |
|---|
| 140 |
} |
|---|
| 141 |
} |
|---|
| 142 |
geometry.resize(scale, origin); |
|---|
| 143 |
|
|---|
| 144 |
// restore the original resize defs |
|---|
| 145 |
for(var i=0; i<geometry.components.length; ++i) { |
|---|
| 146 |
comp.resize = originals[i]; |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
function test_LineString_equals(t) { |
|---|
| 152 |
t.plan(3); |
|---|
| 153 |
|
|---|
| 154 |
var x0 = Math.random() * 100; |
|---|
| 155 |
var y0 = Math.random() * 100; |
|---|
| 156 |
var x1 = Math.random() * 100; |
|---|
| 157 |
var y1 = Math.random() * 100; |
|---|
| 158 |
var point0 = new OpenLayers.Geometry.Point(x0, y0); |
|---|
| 159 |
var point1 = new OpenLayers.Geometry.Point(x1, y1); |
|---|
| 160 |
var geometry = new OpenLayers.Geometry.LineString([point0, point1]); |
|---|
| 161 |
var equal = new OpenLayers.Geometry.LineString([point0, point1]); |
|---|
| 162 |
var offX = new OpenLayers.Geometry.LineString([ |
|---|
| 163 |
new OpenLayers.Geometry.Point(x0 + 1, y0), |
|---|
| 164 |
new OpenLayers.Geometry.Point(x1 + 1, y1)]); |
|---|
| 165 |
var offY = new OpenLayers.Geometry.LineString([ |
|---|
| 166 |
new OpenLayers.Geometry.Point(x0, y0 + 1), |
|---|
| 167 |
new OpenLayers.Geometry.Point(x1, y1 + 1)]); |
|---|
| 168 |
t.ok(geometry.equals(equal), |
|---|
| 169 |
"equals() returns true for a geometry with equivalent coordinates"); |
|---|
| 170 |
t.ok(!geometry.equals(offX), |
|---|
| 171 |
"equals() returns false for a geometry with offset x"); |
|---|
| 172 |
t.ok(!geometry.equals(offY), |
|---|
| 173 |
"equals() returns false for a geometry with offset y"); |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
function test_LineString_clone(t) { |
|---|
| 177 |
t.plan(2); |
|---|
| 178 |
|
|---|
| 179 |
var x0 = Math.random() * 100; |
|---|
| 180 |
var y0 = Math.random() * 100; |
|---|
| 181 |
var x1 = Math.random() * 100; |
|---|
| 182 |
var y1 = Math.random() * 100; |
|---|
| 183 |
var point0 = new OpenLayers.Geometry.Point(x0, y0); |
|---|
| 184 |
var point1 = new OpenLayers.Geometry.Point(x1, y1); |
|---|
| 185 |
var geometry = new OpenLayers.Geometry.LineString([point0, point1]); |
|---|
| 186 |
var clone = geometry.clone(); |
|---|
| 187 |
t.ok(clone instanceof OpenLayers.Geometry.LineString, |
|---|
| 188 |
"clone() creates an OpenLayers.Geometry.LineString"); |
|---|
| 189 |
t.ok(geometry.equals(clone), "clone has equivalent coordinates"); |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
</script> |
|---|
| 194 |
</head> |
|---|
| 195 |
<body> |
|---|
| 196 |
</body> |
|---|
| 197 |
</html> |
|---|