OpenLayers OpenLayers

root/branches/openlayers/2.5/tests/test_Feature.html

Revision 4302, 7.5 kB (checked in by tschaub, 1 year ago)

Deprecating all prototype extensions. This puts all OpenLayers functionality in the OpenLayers namespace. If you are using any of the Function, String, or Number prototype extensions, start using the functional equivalents in the OpenLayers namespace - the prototype extensions will be gone in 3.0 (closes #712).

  • Property svn:eol-style set to native
Line 
1 <html>
2 <head>
3   <script src="../lib/OpenLayers.js"></script>
4   <script type="text/javascript">
5     var isMozilla = (navigator.userAgent.indexOf("compatible") == -1);
6     var map;
7     var feature, layer;
8    
9     function test_01_Feature_constructor (t) {
10         t.plan( 7 );
11
12         var layer = {};
13         var lonlat = new OpenLayers.LonLat(2,1);
14         var iconURL = 'http://boston.openguides.org/features/ORANGE.png';
15         var iconSize = new OpenLayers.Size(12, 17);
16         var data =  { iconURL: iconURL,
17                       iconSize: iconSize
18                     };
19        
20         feature = new OpenLayers.Feature(layer, lonlat, data);
21
22         t.ok( feature instanceof OpenLayers.Feature, "new OpenLayers.Feature returns Feature object" );
23         t.eq( feature.layer, layer, "feature.layer set correctly" );
24         t.ok(OpenLayers.String.startsWith(feature.id, "OpenLayers.Feature_"),
25              "feature.id set correctly");
26         t.ok( feature.lonlat.equals(lonlat), "feature.lonlat set correctly" );
27         t.eq( feature.data.iconURL, iconURL, "feature.data.iconURL set correctly" );
28         t.ok( feature.data.iconSize.equals(iconSize), "feature.data.iconSize set correctly" );
29         t.ok( feature.popupClass == OpenLayers.Popup.AnchoredBubble, "default popupClass is AnchoredBubble");
30     }
31    
32     function test_02_Feature_createPopup (t) {
33         t.plan(1);
34         var layer = {};
35         var lonlat = new OpenLayers.LonLat(2,1);
36         var iconURL = 'http://boston.openguides.org/features/ORANGE.png';
37         var iconSize = new OpenLayers.Size(12, 17);
38         var data =  { iconURL: iconURL,
39                       iconSize: iconSize,
40                       'overflow':'auto'
41                     };
42        
43         feature = new OpenLayers.Feature(layer, lonlat, data);
44         popup = feature.createPopup();
45         t.eq(popup.contentDiv.style.overflow, "auto", 'overflow on popup is correct');
46     }   
47     function test_02_Feature_createMarker (t) {
48         t.plan(1);
49         t.ok(true);
50 /*
51
52         t.plan( 11 );
53         feature = new OpenLayers.Feature("myfeature", new OpenLayers.LonLat(2,1),
54                {
55                 iconURL:'http://boston.openguides.org/features/ORANGE.png',
56                 iconW: 12,
57                 iconH: 17
58                });
59         layer = new OpenLayers.Layer.Markers('Marker Layer');
60         t.ok( feature instanceof OpenLayers.Feature, "new OpenLayers.Feature returns Feature object" );
61         t.ok( layer instanceof OpenLayers.Layer.Markers, "Layer is a marker layer" );
62         feature.createMarker(layer);
63         
64         t.ok( feature.marker instanceof OpenLayers.Marker,
65               "createMarker sets a marker property to a marker" );
66         t.ok( layer.markers[0] === feature.marker,
67               "First marker in layer is the feature marker" );
68         
69         t.ok( feature.marker.lonlat instanceof OpenLayers.LonLat,
70               "createMarker sets a marker lontlat property to a lonlat" );
71         t.ok( layer.markers[0].lonlat === feature.lonlat,
72               "First marker in the layer matches feature lonlat" );
73         
74         t.ok( feature.marker.icon instanceof OpenLayers.Icon,
75               "createMarker sets a marker icon property to an icon" );
76         
77         t.eq( feature.marker.icon.url,
78               "http://boston.openguides.org/features/ORANGE.png",
79               "createMarker sets marker url correctly" );
80       
81         var map = new OpenLayers.Map('map');
82         map.addLayer(layer);
83         map.setCenter(new OpenLayers.LonLat(0,0),0);
84         t.ok( map.layers[0] == layer,
85               "Marker layer added to map okay." );
86         if (!isMozilla)
87             t.ok( true, "skipping element test outside of Mozilla");
88         else
89             t.ok( map.layers[0].div.firstChild instanceof HTMLImageElement,
90                   "layer div firstChild is an image" );
91         t.eq( map.layers[0].div.firstChild.src,
92               "http://boston.openguides.org/features/ORANGE.png",
93               "Layer div img contains correct url" );
94 */
95     }
96    
97     function test_03_Feature_onScreen(t) {
98         t.plan( 2 );
99
100         var map = new OpenLayers.Map("map");
101
102         var url = "http://octo.metacarta.com/cgi-bin/mapserv";
103         var wms = new OpenLayers.Layer.WMS(name, url);
104
105         map.addLayer(wms);
106
107         var layer = new OpenLayers.Layer("foo");
108         map.addLayer(layer);
109        
110         map.zoomToExtent(new OpenLayers.Bounds(-50,-50,50,50));
111
112         //onscreen feature
113         var feature1 = new OpenLayers.Feature(layer,
114                                               new OpenLayers.LonLat(0,0));
115         t.ok( feature1.onScreen(), "feature knows it's onscreen" );
116
117         //onscreen feature
118         var feature2 = new OpenLayers.Feature(layer,
119                                               new OpenLayers.LonLat(100,100));
120         t.ok( !feature2.onScreen(), "feature knows it's offscreen" );
121     }
122
123     function test_04_Feature_createPopup(t) {
124         t.plan(17);
125
126     //no lonlat       
127         var f = {
128             'popup': null
129         };
130        
131         var ret = OpenLayers.Feature.prototype.createPopup.apply(f, []);
132         t.ok((ret == f.popup) && (f.popup == null), "if no 'lonlat' set on feature, still returns reference to this.popup (though it is null)");
133
134
135
136         f.popupClass = OpenLayers.Class({
137             initialize: function(id, lonlat, size, contentHTML, anchor, closeBox) {
138                 t.eq(id, "Campion_popup", "correctly generates new popup id from feature's id");
139                 t.eq(lonlat, f.lonlat, "correctly passes feature's lonlat to popup constructor");
140                 t.eq(size, f.data.popupSize, "correctly passes feature's data.popupSize to popup constructor");
141                 t.eq(contentHTML, f.data.popupContentHTML, "correctly passes feature's data.popupContentHTML to popup constructor");
142                 t.eq(anchor, g_ExpectedAnchor, "passes correct anchor to popup constructor");
143                 t.eq(closeBox, g_CloseBox, "correctly relays closeBox argument to popup constructor");
144             }
145         });
146        
147
148     //valid lonlat but no anchor   
149         f.popup = null;
150        
151         f.id = "Campion";
152         f.lonlat = {};
153         f.data = {
154             'popupSize': {},
155             'popupContentHTML': {}
156         };
157         g_ExpectedAnchor = null;
158         g_CloseBox = {};
159
160         ret = OpenLayers.Feature.prototype.createPopup.apply(f, [g_CloseBox]);
161        
162         t.ok((ret == f.popup) && (f.popup != null), "a valid popup has been set and returned")
163         t.ok( f.popup.feature == f, "popup's 'feature' property correctly set");
164
165    
166     //valid lonlat with anchor   
167
168         f.marker = {
169             'icon': {}
170         };
171         g_ExpectedAnchor = f.marker.icon;
172         ret = OpenLayers.Feature.prototype.createPopup.apply(f, [g_CloseBox]);
173         t.ok((ret == f.popup) && (f.popup != null), "a valid popup has been set and returned")
174         t.ok( f.popup.feature == f, "popup's 'feature' property correctly set");
175     }
176
177     function test_04_Feature_destroyPopup(t) {
178         t.plan(2);
179
180         var f = {
181             'popup': {
182                 'feature': {},
183                 'destroy': function() {
184                     t.ok(true, "default destroyPopup() calls popup.destroy");
185                 }
186             }
187         };
188        
189         OpenLayers.Feature.prototype.destroyPopup.apply(f, []);
190         t.ok(f.popup.feature == null, "popup's 'feature' property nullified on destroy");
191     }
192
193   </script>
194 </head>
195 <body>
196   <div id="map" style="width: 500px; height: 300px;"></div>
197 </body>
198 </html>
Note: See TracBrowser for help on using the browser.