]> dev.renevier.net Git - syp.git/blob - openlayers/tests/Feature/Vector.html
initial commit
[syp.git] / openlayers / tests / Feature / Vector.html
1 <html>
2 <head>
3   <script src="../../lib/OpenLayers.js"></script>
4   <script type="text/javascript">
5     var map; 
6     var feature; 
7     
8     function test_Feature_Vector_constructor(t) {
9         t.plan(3);
10         
11         var geometry = new OpenLayers.Geometry();
12         geometry.id = Math.random();
13         var style = {foo: "bar"};
14         var attributes = {bar: "foo"};
15
16         feature = new OpenLayers.Feature.Vector(geometry, attributes, style);
17
18         t.ok(feature instanceof OpenLayers.Feature.Vector,
19              "new OpenLayers.Feature.Vector returns Feature.Vector object" );
20         t.eq(feature.attributes, attributes,
21              "attributes property set properly" );
22         t.eq(feature.geometry.id, geometry.id,
23              "geometry.property set properly" );
24     }
25     
26     function test_Feature_onScreen(t) {
27         t.plan(6);
28         var line = new OpenLayers.Geometry.LineString([
29             new OpenLayers.Geometry.Point(0, 0),
30             new OpenLayers.Geometry.Point(10, 20)
31         ]);
32         var feature = new OpenLayers.Feature.Vector(line);
33         feature.layer = {
34             map: {
35                 getExtent: function() {
36                     return new OpenLayers.Bounds(5, 5, 10, 10);
37                 }
38             }
39         };
40         t.eq(feature.onScreen(), true,
41              "intersecting feature returns true for intersection");
42         t.eq(feature.onScreen(true), true,
43              "intersecting feature returns true for bounds only");
44         
45         // move the line so only the bounds intersects
46         line.move(0, 5);
47         t.eq(feature.onScreen(), false,
48              "bounds-only feature returns false for intersection");
49         t.eq(feature.onScreen(true), true,
50              "bounds-only feature returns true for bounds only");
51
52         // move the line so bounds does not intersect
53         line.move(0, 10);
54         t.eq(feature.onScreen(), false,
55              "off-screen feature returns false for intersection");
56         t.eq(feature.onScreen(true), false,
57              "off-screen feature returns false for bounds only");
58         
59     }
60     
61     function test_Feature_Vector_clone(t) {
62         t.plan(6);
63
64         var geometry = new OpenLayers.Geometry.Point(Math.random(),
65                                                      Math.random());
66         var style = {foo: "bar"};
67         var attributes = {bar: "foo"};
68
69         feature = new OpenLayers.Feature.Vector(geometry, attributes, style);
70         var clone = feature.clone();
71
72         t.ok(clone instanceof OpenLayers.Feature.Vector,
73              "new OpenLayers.Feature.Vector returns Feature.Vector object");
74         t.eq(clone.attributes, attributes,
75              "attributes property set properly");
76         t.eq(clone.style, style,
77              "style property set properly");
78         t.eq(clone.geometry.x, geometry.x,
79              "geometry.x property set properly");
80         t.eq(clone.geometry.y, geometry.y,
81              "geometry.y property set properly");
82
83         feature = new OpenLayers.Feature.Vector();
84         clone = feature.clone();
85         t.ok(clone instanceof OpenLayers.Feature.Vector,
86              "clone can clone geometry-less features");
87     }
88         
89     function test_Feature_Vector_move(t) {
90         t.plan(3);
91
92         var oldx = 26;
93         var oldy = 14;
94         var newx = 6;
95         var newy = 4;
96         var res = 10;
97
98         var geometry = new OpenLayers.Geometry.Point(oldx,
99                                                      oldy);
100
101         var drawn = false;
102
103         feature = new OpenLayers.Feature.Vector(geometry);
104
105         feature.layer = {
106             getViewPortPxFromLonLat : function(lonlat){
107                 return new OpenLayers.Pixel(lonlat.lon,lonlat.lat);
108             },
109             map: {
110                 getResolution: function(){
111                     return res;
112                 }
113             },
114             drawFeature: function(){
115                 drawn = true;
116             }
117         }
118
119         var pixel = new OpenLayers.Pixel(newx,newy)
120
121         feature.move(pixel);
122
123         geometry = feature.geometry;
124
125         t.ok(drawn, "The feature is redrawn after the move");
126         t.eq(geometry.x, res * (newx - oldx) + oldx, "New geometry has proper x coordinate");
127         t.eq(geometry.y, res * (oldy - newy) + oldy, "New geometry has proper y coordinate");
128     }
129         
130
131   </script>
132 </head>
133 <body>
134   <div id="map" style="width: 500px; height: 300px;"></div>
135 </body>
136 </html>