]> dev.renevier.net Git - syp.git/blob - openlayers/tests/Handler/Keyboard.html
initial commit
[syp.git] / openlayers / tests / Handler / Keyboard.html
1 <html>
2 <head>
3   <script src="../../lib/OpenLayers.js"></script>
4   <script type="text/javascript">
5     function test_Handler_Keyboard_initialize(t) {
6         t.plan(3);
7         var control = new OpenLayers.Control();
8         control.id = Math.random();
9         var callbacks = {foo: "bar"};
10         var options = {bar: "foo"};
11         
12         var oldInit = OpenLayers.Handler.prototype.initialize;
13         
14         OpenLayers.Handler.prototype.initialize = function(con, call, opt) {
15             t.eq(con.id, control.id,
16                  "constructor calls parent with the correct control");
17             t.eq(call, callbacks,
18                  "constructor calls parent with the correct callbacks");
19             t.eq(opt, options,
20                  "constructor calls parent with the correct options");
21         }
22         var handler = new OpenLayers.Handler.Keyboard(control, callbacks,
23                                                       options);
24
25         OpenLayers.Handler.prototype.initialize = oldInit;
26     }
27
28     function test_Handler_Keyboard_destroy(t) {
29         t.plan(3);
30         var control = new OpenLayers.Control();        
31         var handler = new OpenLayers.Handler.Keyboard(control);
32         var old = OpenLayers.Handler.prototype.destroy;
33         t.ok(handler.eventListener != null,
34              "eventListener is not null before destroy");
35         OpenLayers.Handler.prototype.destroy = function() {
36             t.ok(true, "destroy calls destroy on correct parent");
37         };
38         handler.destroy();
39         t.ok(handler.eventListener == null,
40              "eventListeners is null after destroy");
41         OpenLayers.Handler.prototype.destroy = old;
42     }
43
44     function test_Handler_Keyboard_activate(t) {
45         t.plan(8);
46         var map = new OpenLayers.Map('map');
47         var control = new OpenLayers.Control();
48         map.addControl(control);
49         var handler = new OpenLayers.Handler.Keyboard(control);
50         handler.active = true;
51         var activated = handler.activate();
52         t.ok(!activated,
53              "activate returns false if the handler was already active");
54         handler.active = false;
55         handler.dragging = true;
56         var old = OpenLayers.Event.stopObserving;
57         var types = ["keydown", "keyup"];
58         OpenLayers.Event.observe = function(obj, type, method) {
59             t.ok(obj == document,
60                  "activate calls observing with correct object");
61             var validType = (OpenLayers.Util.indexOf(types, type) != -1);
62             t.ok(validType, "activate calls observe for " + type);
63             t.ok(method == handler.eventListener,
64                  "activate calls observing with correct method");
65         };
66         activated = handler.activate();
67         t.ok(activated,
68              "activate returns true if the handler was not already active");
69         OpenLayers.Event.observe = old;
70         
71     }
72
73     function test_Handler_Drag_deactivate(t) {
74         t.plan(8);
75         var map = new OpenLayers.Map('map');
76         var control = new OpenLayers.Control();
77         map.addControl(control);
78         var handler = new OpenLayers.Handler.Keyboard(control);
79         handler.active = false;
80         var deactivated = handler.deactivate();
81         t.ok(!deactivated,
82              "deactivate returns false if the handler was not already active");
83         handler.active = true;
84         var old = OpenLayers.Event.stopObserving;
85         var types = ["keydown", "keyup"];
86         OpenLayers.Event.stopObserving = function(obj, type, method) {
87             t.ok(obj == document,
88                  "deactivate calls stopObserving with correct object");
89             var validType = (OpenLayers.Util.indexOf(types, type) != -1);
90             t.ok(validType, "deactivate calls stopObserving for " + type);
91             t.ok(method == handler.eventListener,
92                  "deactivate calls stopObserving with correct method");
93         };
94         deactivated = handler.deactivate();
95         t.ok(deactivated,
96              "deactivate returns true if the handler was active already");
97         OpenLayers.Event.stopObserving = old;
98     }
99
100
101   </script>
102 </head>
103 <body>
104     <div id="map" style="width: 300px; height: 150px;"/>
105 </body>
106 </html>