OpenLayers OpenLayers

root/trunk/openlayers/tests/Geometry.html

Revision 6724, 8.5 kB (checked in by euzuro, 8 months ago)

fix up tests, remove _01_. (Closes #1387)

  • Property svn:eol-style set to native
Line 
1 <html>
2 <head>
3   <script src="../lib/OpenLayers.js"></script>
4   <script src="data/geos_wkt_intersects.js"></script>
5   <script type="text/javascript">
6     var map;
7
8     function test_Geometry_constructor (t) {
9         t.plan( 2 );
10        
11         var g = new OpenLayers.Geometry();
12        
13         t.eq(g.CLASS_NAME, "OpenLayers.Geometry", "correct CLASS_NAME")
14         t.ok(OpenLayers.String.startsWith(g.id, "OpenLayers.Geometry_"),
15              "id correctly set");
16     }
17
18
19     function test_Geometry_clone(t) {
20         t.plan(2);
21         var geometry = new OpenLayers.Geometry();
22         var clone = geometry.clone();
23
24         t.eq(clone.CLASS_NAME, "OpenLayers.Geometry", "correct CLASS_NAME")
25         t.ok(OpenLayers.String.startsWith(clone.id, "OpenLayers.Geometry_"),
26              "id correctly set");
27     }
28
29     function test_Geometry_setBounds(t) {
30         t.plan( 2 );
31        
32         var g = new OpenLayers.Geometry();
33        
34       //null object
35         g.setBounds(null);
36         t.ok(g.bounds == null, "setbounds with null value does not crash or set bounds");
37      
38       //no classname object
39         g_clone = {};
40         var object = {
41             'clone': function() { return g_clone; }
42         };
43         g.setBounds(object);
44         t.ok(g.bounds == g_clone, "setbounds with valid object sets bounds, calls clone");
45     }
46
47     function test_Geometry_extendBounds(t) {
48         t.plan(9);
49        
50         OpenLayers.Bounds.prototype._extend =
51             OpenLayers.Bounds.prototype.extend;
52         OpenLayers.Bounds.prototype.extend = function(b) {
53             g_extendBounds = b;
54         };
55        
56         var g = new OpenLayers.Geometry();
57
58       //this.bounds null (calculateBounds(), setBounds() called)
59         g.setBounds = function(b) { g_setBounds = b; };
60         g.calculateBounds = function() { g_calculateBounds = {}; };
61         var object = {};
62         g_setBounds = null;
63         g_calculateBounds = null;
64         g_extendBounds = null;
65         g.extendBounds(object);
66         t.ok(g_calculateBounds != null, "calculateBounds() called when this.bounds is null");
67         t.ok(g_setBounds == object, "setBounds() called when this.bounds is null and calculateBounds() is null too");       
68         t.ok(g_extendBounds != object, "this.bounds.extend() not called when this.bounds is null and calculateBounds() is null too");
69        
70       //this.bounds null (calculateBounds() sets this.bounds:
71       //   -  setBounds() not called
72       //   -  this.bounds.extend() called
73         g_calcBounds = new OpenLayers.Bounds(1,2,3,4);
74         g.calculateBounds = function() {
75             g_calculateBounds = {};
76             this.bounds = g_calcBounds;
77         };
78         var object = {};
79
80         g_setBounds = null;
81         g_calculateBounds = null;
82         g_extendBounds = null;
83         g.extendBounds(object);
84         t.ok(g_calculateBounds != null, "calculateBounds() called when this.bounds is null");
85         t.ok(g_setBounds == null, "setBounds() not called when this.bounds is null and calculateBounds() sets this.bounds");       
86         t.ok(g_extendBounds == object, "this.bounds.extend() called when this.bounds is null and calculateBounds() sets this.bounds");       
87
88        
89       //this.bounds non-null thus extend()
90       //   -  setBounds() not called
91       //   -  this.bounds.extend() called
92         g_setBounds = null;
93         g_calculateBounds = null;
94         g_extendBounds = null;
95         g.extendBounds(object);
96         t.ok(g_calculateBounds == null, "calculateBounds() not called when this.bounds is non null");
97         t.ok(g_setBounds == null, "setBounds() not called when this.bounds is nonnull");       
98         t.ok(g_extendBounds == object, "this.bounds.extend() called when this.bounds is non-null");       
99
100         OpenLayers.Bounds.prototype.extend =
101             OpenLayers.Bounds.prototype._extend;
102
103
104     }
105
106     function test_Geometry_getBounds(t) {
107         t.plan(1);
108        
109         var g = new OpenLayers.Geometry();
110        
111         var testBounds = new OpenLayers.Bounds(1,2,3,4);
112         g.bounds = testBounds.clone();
113        
114         t.ok(g.getBounds().equals(testBounds), "getBounds works");
115     }
116    
117     function test_Geometry_atPoint(t) {
118         t.plan(6);
119            
120         var g = new OpenLayers.Geometry();
121    
122         var lonlat = null;
123         var lon = 5;
124         var lat = 10;
125    
126       //null lonlat
127         g.bounds = new OpenLayers.Bounds();
128
129         var atPoint = g.atPoint(lonlat, lon, lat);
130         t.ok(!atPoint, "null lonlat")
131
132       //null this.bounds
133         g.bounds = null;
134         lonlat = new OpenLayers.LonLat(1,2);     
135      
136         atPoint = g.atPoint(lonlat, lon, lat);
137         t.ok(!atPoint, "null this.bounds")
138
139     //toleranceLon/toleranceLat
140
141       //default toleranceLon/toleranceLat
142         OpenLayers.Bounds.prototype._containsLonLat = OpenLayers.Bounds.prototype.containsLonLat;
143         g_Return = {};
144         OpenLayers.Bounds.prototype.containsLonLat = function(ll) {
145             g_bounds = this;
146             return g_Return;
147          }
148
149         var testBounds = new OpenLayers.Bounds(10,20,30,40);
150         g.bounds = testBounds.clone();
151         lonlat = new OpenLayers.LonLat(20,30);
152      
153         g_bounds = null;
154         atPoint = g.atPoint(lonlat);
155         t.ok(g_bounds.equals(testBounds), "default toleranceLon/Lat are 0");
156         t.ok(atPoint == g_Return, "default toleranceLon/Lat returns correctly");
157
158       //real toleranceLon/toleranceLat
159         var testBounds = new OpenLayers.Bounds(10,20,30,40);
160         g.bounds = testBounds.clone();
161         lonlat = new OpenLayers.LonLat(20,30);
162      
163         g_bounds = null;
164         atPoint = g.atPoint(lonlat, lon, lat);
165         testBounds.left -= lon;
166         testBounds.bottom -= lat;
167         testBounds.right += lon;
168         testBounds.top += lat;
169         t.ok(g_bounds.equals(testBounds), "real toleranceLon/Lat are 0");
170         t.ok(atPoint == g_Return, "real toleranceLon/Lat returns correctly");
171
172         OpenLayers.Bounds.prototype.containsLonLat = OpenLayers.Bounds.prototype._containsLonLat;
173    
174     }
175    
176     function test_Geometry_getLength(t) {
177         t.plan(1);
178        
179         var g = new OpenLayers.Geometry();
180        
181         t.eq(g.getLength(), 0, "getLength is 0");
182     }   
183
184     function test_Geometry_getArea(t) {
185         t.plan(1);
186        
187         var g = new OpenLayers.Geometry();
188        
189         t.eq(g.getArea(), 0, "getArea is 0");
190     }
191    
192     function test_Geometry_clearBounds(t) {
193         t.plan(2);
194        
195         var g = new OpenLayers.Geometry();
196         g.parent = new OpenLayers.Geometry();
197
198         g.bounds = "foo";
199         g.parent.bounds = "bar";
200        
201         g.clearBounds();
202         t.ok(g.bounds == null, "bounds is correctly cleared");
203         t.ok(g.parent.bounds == null, "parent geometry bounds is correctly cleared");
204     }
205
206     function test_Geometry_destroy(t) {
207         t.plan( 2 );
208
209         var g = new OpenLayers.Geometry();
210         g.bounds = new OpenLayers.Bounds();
211
212         g_style_destroy = null;
213         g.destroy();
214        
215         t.eq(g.id, null, "id nullified");
216
217         t.eq(g.bounds, null, "bounds nullified");
218
219     }
220
221     function test_Geometry_intersects_geos_wkt(t) {
222         var wkt = new OpenLayers.Format.WKT();
223         var failures = [];
224         var intersect12, intersect21, msg;
225         for (var i = 0; i < geos_test_data.length; i++) {
226             var testcase = geos_test_data[i];
227             f1 = wkt.read(testcase['wkt1']);
228             f2 = wkt.read(testcase['wkt2']);
229             intersect12 = f1.geometry.intersects(f2.geometry);
230             intersect21 = f2.geometry.intersects(f1.geometry);
231             if(intersect12 != testcase.result) {
232                 msg = "f1 should " + (testcase.result ? "" : "not ") +
233                       "intersect f2: f1 = '" + testcase['wkt1'] + "' " +
234                       "f2 = '" + testcase['wkt2'] + "'";
235                 failures.push(msg);
236             }
237             if(intersect21 != testcase.result) {
238                 msg = "f2 should " + (testcase.result ? "" : "not ") +
239                       "intersect f1: f1 = '" + testcase['wkt1'] + "' " +
240                       "f2 = '" + testcase['wkt2'] + "'";
241                 failures.push(msg);
242             }
243         }
244         if(failures.length == 0) {
245             t.plan(1);
246             t.ok(true, "all " + geos_test_data.length + " geos tests pass");
247         } else {
248             t.plan(failures.length);
249             for(var f=0; f<failures.length; ++f) {
250                 t.fail(failures[f]);
251             }
252         }
253     }   
254
255   </script>
256 </head>
257 <body>
258     <div id="map" style="width: 1024px; height: 512px;"/>
259 </body>
260 </html>
Note: See TracBrowser for help on using the browser.