summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Control/Panel.html
blob: f02e643feee133bce1c6acda86776c28dc6e4310 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    function test_Control_Panel_constructor (t) {
        t.plan( 2 );
    
        control = new OpenLayers.Control.Panel();
        t.ok( control instanceof OpenLayers.Control.Panel, "new OpenLayers.Control returns object" );
        t.eq( control.displayClass,  "olControlPanel", "displayClass is correct" );
    }
    function test_Control_Panel_constructor2 (t) {
        t.plan(19);
        var map = new OpenLayers.Map('map');
        var toolControl = new OpenLayers.Control.ZoomBox();
        var AnotherToolControl = OpenLayers.Class(OpenLayers.Control, {
              CLASS_NAME: 'mbControl.TestTool',
              type: OpenLayers.Control.TYPE_TOOL
        });
        var anotherToolControl = new AnotherToolControl();
        var ToggleControl = OpenLayers.Class(OpenLayers.Control, {
              CLASS_NAME: 'mbControl.TestToggle',
              type: OpenLayers.Control.TYPE_TOGGLE
        });

        var toggleControl = new ToggleControl();
        var buttonControl = new OpenLayers.Control.Button({
            trigger: function () {
                t.ok(true, "trigger function of button is called.");
            }
        });

        var panel = new OpenLayers.Control.Panel(
            {defaultControl: anotherToolControl});
        t.ok(panel instanceof OpenLayers.Control.Panel,
              "new OpenLayers.Control.Panel returns object");
        panel.redraw = function(){
            panel.redrawsCount++;
            OpenLayers.Control.Panel.prototype.redraw.apply(this, arguments);
        };

        // To get length of events.listeners error-free
        var getListenerLength= function(events,key){
            if(!events) {
                return -2; // events is destroyed
            } else if(!events.listeners) {
                return -1; // events is destroyed
            } else if(!events.listeners[key]) {
                return 0; // no listener in event
            } else {
                return events.listeners[key].length;
            }
        };
        var toolEventListenerLength = getListenerLength(toolControl.events,"activate");
        panel.addControls([toolControl, anotherToolControl, toggleControl]);
        t.eq(panel.controls.length, 3,
              "added three controls to the panel");
        panel.addControls([buttonControl]);

        panel.redrawsCount = 0;
        map.addControl(panel);
        t.eq(getListenerLength(toolControl.events,"activate"), toolEventListenerLength+1,
            "toolControl additional listener for \"activate\" after adding Panel to the map.");
        t.ok((panel.redrawsCount > 0), "Redraw called on add panel to map " +
            panel.redrawsCount + " times.");
        t.ok((panel.active),"Panel is active after add panel to map.");

        panel.redrawsCount = 0;
        panel.addControls(new AnotherToolControl());
        t.ok((panel.redrawsCount > 0),
            "Redraw called on add control to panel after add panel to map " +
            panel.redrawsCount + " times.");

        panel.deactivate();
        panel.redrawsCount = 0;
        panel.activate();
        t.ok((panel.redrawsCount > 0),"Redraw called on activate panel " +
            panel.redrawsCount + " times.");

        panel.activateControl(toolControl);
        t.ok(toolControl.active && !anotherToolControl.active && !toggleControl.active && !buttonControl.active,
              "activated one tool control, the other one is inactive and the toggle & button controls also.");

        panel.activateControl(toggleControl);
        t.eq(toggleControl.panel_div.className,"mbControlTestToggleItemActive olButton",
            "className of icon div for toggle control is active.");
        t.ok(toolControl.active && !anotherToolControl.active && toggleControl.active,
              "activated the toggle control, which has no influence on the tool & togggle controls.");
        panel.activateControl(buttonControl);
        t.ok(toolControl.active && !anotherToolControl.active && toggleControl.active,
              "activateContol calling for button, which has no influence on the tool & togggle controls.");
        t.ok(!buttonControl.active,
              "activateContol calling for button, button remains inactive.");
        buttonControl.activate();
        t.ok(buttonControl.active && toolControl.active && !anotherToolControl.active && toggleControl.active,
              "activated the button control, which has no influence on the tool & togggle controls.");

        panel.activateControl(anotherToolControl);
        t.eq(anotherToolControl.panel_div.className,"mbControlTestToolItemActive olButton",
            "className of icon div for anotherToolControl is active.");
        t.eq(toolControl.panel_div.className,"olControlZoomBoxItemInactive olButton",
            "className of icon div for toolControl is inactive.");
        t.ok(!toolControl.active && anotherToolControl.active && toggleControl.active,
              "activated the other tool control, the first one is inactive and the toggle control still active.");
        t.ok(buttonControl.active,
              "activated the other tool control, the button control still active.");

        panel.destroy();
        t.eq(getListenerLength(toolControl.events,"activate"), toolEventListenerLength,
            "toolControl additional listeners removed after destroy Panel.");
        map.destroy();
    }
    function test_Control_Panel_titles (t) { 
        t.plan(2); 
        var panel = new OpenLayers.Control.Panel(); 
        var toolControl = new OpenLayers.Control.ZoomBox({ 
            title:"Zoom box: Selecting it you can zoom on an area by clicking and dragging." 
        }); 
        panel.addControls([toolControl]); 
        t.eq(panel.controls.length, 1, "added a control to the panel"); 
        t.eq(panel.controls[0].title, toolControl.panel_div.title, "the title is correctly set"); 
    } 
    
    function test_Control_Panel_getBy(t) {
        
        var panel = {
            getBy: OpenLayers.Control.Panel.prototype.getBy,
            getControlsBy: OpenLayers.Control.Panel.prototype.getControlsBy,
            controls: [
                {foo: "foo", id: Math.random()},
                {foo: "bar", id: Math.random()},
                {foo: "foobar", id: Math.random()},
                {foo: "foo bar", id: Math.random()},
                {foo: "foo", id: Math.random()}
            ]
        };

        var cases = [
            {
                got: panel.getControlsBy("foo", "foo"),
                expected: [panel.controls[0], panel.controls[4]],
                message: "(string literal) got two controls matching foo"
            }, {
                got: panel.getControlsBy("foo", "bar"),
                expected: [panel.controls[1]],
                message: "(string literal) got one control matching foo"
            }, {
                got: panel.getControlsBy("foo", "barfoo"),
                expected: [],
                message: "(string literal) got empty array for no foo match"
            }, {
                got: panel.getControlsBy("foo", /foo/),
                expected: [panel.controls[0], panel.controls[2], panel.controls[3], panel.controls[4]],
                message: "(regexp literal) got three controls containing string"
            }, {
                got: panel.getControlsBy("foo", /foo$/),
                expected: [panel.controls[0], panel.controls[4]],
                message: "(regexp literal) got three controls ending with string"
            }, {
                got: panel.getControlsBy("foo", /\s/),
                expected: [panel.controls[3]],
                message: "(regexp literal) got control containing space"
            }, {
                got: panel.getControlsBy("foo", new RegExp("BAR", "i")),
                expected: [panel.controls[1], panel.controls[2], panel.controls[3]],
                message: "(regexp object) got layers ignoring case"
            }, {
                got: panel.getControlsBy("foo", {test: function(str) {return str.length > 3;}}),
                expected: [panel.controls[2], panel.controls[3]],
                message: "(custom object) got controls with foo length greater than 3"
            }
        ];
        t.plan(cases.length);
        for(var i=0; i<cases.length; ++i) {
            t.eq(cases[i].got, cases[i].expected, cases[i].message);
        }


    }
    
    function test_Control_Panel_saveState (t) { 
        t.plan(11); 
        var map = new OpenLayers.Map('map');

        var defaultControl = new OpenLayers.Control();
        var panel = new OpenLayers.Control.Panel({
            defaultControl: defaultControl
        });
        panel.addControls([new OpenLayers.Control(), defaultControl]);
        map.addControl(panel);
        t.eq(defaultControl.active, true,
            "After panel activation default control is active.");    
        t.ok(panel.defaultControl,
            "defaultControl not nullified after initial panel activation");          
        // activate the 1st control
        panel.activateControl(panel.controls[0]);
        panel.deactivate();      
        t.ok(!panel.controls[0].active && !panel.controls[1].active,
            "No controls are active after panel deactivation.");
        panel.activate();   
        t.eq(panel.controls[0].active, false,
            "After panel reactivation first control is inactive.");              
        t.eq(panel.controls[1].active, true,
            "After panel reactivation default control is active again.");
        panel.destroy();

        defaultControl = new OpenLayers.Control();
        panel = new OpenLayers.Control.Panel({
            saveState: true,
            defaultControl: defaultControl
        });
        panel.addControls([new OpenLayers.Control(), defaultControl]);
        map.addControl(panel);
        t.eq(defaultControl.active, true,
            "After panel activation default control is active.");    
        t.eq(panel.defaultControl, null,
            "defaultControl nullified after initial panel activation");          
        // activate the 1st control, which will deactivate the 2nd
        panel.activateControl(panel.controls[0]);
        t.eq(panel.controls[1].active, false,
            "2nd control deactivated with activation of 1st");
        panel.deactivate();      
        t.ok(!panel.controls[0].active && !panel.controls[1].active,
            "No controls are active after panel deactivation.");
        panel.activate();   
        t.eq(panel.controls[0].active, true,
            "After panel reactivation first control is active.");
        t.eq(panel.controls[1].active, false,
            "After panel reactivation second control is inactive.");              
        panel.destroy();
        map.destroy();
    } 

    function test_Control_Panel_autoActivate (t) {
        t.plan(1);
        var map = new OpenLayers.Map('map');
        var controlNoDeactive = new OpenLayers.Control({autoActivate:true});
        var chkDeactivate = function () {
            t.ok(false, "Tool control autoActivate:true was deactivated unnecessarily");
        };
        controlNoDeactive.events.on({deactivate: chkDeactivate});
        var panel = new OpenLayers.Control.Panel();
        
        map.addControl(panel);
        panel.addControls([controlNoDeactive]);
        controlNoDeactive.events.un({deactivate: chkDeactivate});
        t.ok(!controlNoDeactive.active, "Tool control autoActivate:true is not active");
        
    }

    function test_Control_Panel_deactivate (t) {
        t.plan(2);
        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control();
        var panel = new OpenLayers.Control.Panel();        
        map.addControl(panel);
        panel.addControls([control]);        
        t.ok(panel.div.innerHTML != "", "Panel displayed after activate");        
        
        panel.deactivate();
        t.ok(panel.div.innerHTML == "", 
            "Panel is not displayed after deactivate without any active control");

        map.destroy();
    }

    function test_allowDepress (t) { 
        t.plan(2); 
        var map = new OpenLayers.Map('map');

        var panel = new OpenLayers.Control.Panel();
        panel.addControls([new OpenLayers.Control(),new OpenLayers.Control()]);
        map.addControl(panel);
        
        var control1 = panel.controls[1]
        
        panel.activateControl(control1);
        
        panel.allowDepress = false;
        panel.activateControl(control1);
        t.eq(control1.active, true,
            "control1 remains active after calling again activateControl when allowDepress = false");
        panel.allowDepress = true;
        panel.activateControl(control1);
        t.eq(control1.active, false,
            "control1 is inactive after calling again activateControl when allowDepress = true");

        // panel.deactivate();
        map.destroy();
    }

    function test_iconOn_iconOff(t) {
        t.plan(2);

        var map = new OpenLayers.Map('map');

        var panel = new OpenLayers.Control.Panel();
        var ctrl = new OpenLayers.Control({displayClass: 'ctrl'});
        panel.addControls([ctrl]);

        map.addControl(panel);

        // add arbitrary classes to the panel div - we want to test
        // than iconOn and iconOff do their jobs even when the panel
        // div has application-specific classes.

        ctrl.panel_div.className =
            'ctrlItemInactive fooItemActive fooItemInactive';

        panel.iconOn.call(ctrl);
        t.eq(ctrl.panel_div.className,
             'ctrlItemActive fooItemActive fooItemInactive',
             'iconOn behaves as expected');

        ctrl.panel_div.className =
            'ctrlItemActive fooItemActive fooItemInactive';

        panel.iconOff.call(ctrl);
        t.eq(ctrl.panel_div.className,
             'ctrlItemInactive fooItemActive fooItemInactive',
             'iconOff behaves as expected');

        map.destroy();
    }
    
    function test_buttonclick(t) {
        t.plan(4);
        var map = new OpenLayers.Map('map');
        var panel1 = new OpenLayers.Control.Panel();
        var div = document.createElement("div");
        var panel2 = new OpenLayers.Control.Panel({div: div});
        map.addControls([panel1, panel2]);
        
        t.ok(map.events.listeners.buttonclick, "buttonclick event registered on map's Events instance for panel inside map");
        t.ok(!panel1.events.element, "Panel inside map has no element on its Events instance");
        t.ok(panel2.events.listeners.buttonclick, "buttonclick event registered on panel's Events instance if outside map")
        t.ok(panel2.events.element === div, "Panel outside map has the panel's div as element on its Events instance");
        
    }
    
    function test_iconOniconOff (t) {
    	t.plan(6);
    	var map = new OpenLayers.Map("map"),
			navControl = new OpenLayers.Control.Navigation({autoActivate: true}),
			zbControl = new OpenLayers.Control.ZoomBox(),
    		panel = new OpenLayers.Control.Panel({defaultControl: navControl}),
    		navActiveClass, navInactiveClass, zbActiveClass, zbInactiveClass;
    	
    	panel.addControls([navControl, zbControl]);
    	map.addControl(panel);
    	
     	navControl.panel_div.className += " foo";
    	zbControl.panel_div.className = "bar " + zbControl.panel_div.className;
    	
    	t.eq(navControl.panel_div.className, "olControlNavigationItemActive olButton foo",
    		"defaultControl className is set to [displayClass]Active on panel instantiation");
    	t.eq(zbControl.panel_div.className, "bar olControlZoomBoxItemInactive olButton",
        	"non-defaultControl className is set to [displayClass]Inactive on panel instantiation");
    	
    	panel.activateControl(zbControl);
    	
    	t.eq(zbControl.panel_div.className, "bar olControlZoomBoxItemActive olButton",
    		"active control class name with preceding secondary class name is set to [displayClass]Active");
    	t.eq(navControl.panel_div.className, "olControlNavigationItemInactive olButton foo",
    		"inactive control class name with trailing secondary class name is set to [displayClass]Inactive");
    	
    	panel.activateControl(navControl);
    	
    	t.eq(navControl.panel_div.className, "olControlNavigationItemActive olButton foo",
        		"active control class name with trailing secondary class name is set to [displayClass]Active");
    	t.eq(zbControl.panel_div.className, "bar olControlZoomBoxItemInactive olButton",
        		"inactive control class name with preceding secondary class name is set to [displayClass]Inactive");
    	
    	map.destroy();
    }
    
  </script>
</head>
<body>
    <div id="map" style="width: 1024px; height: 512px;"/>
</body>
</html>