summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Handler/Keyboard.html
blob: 4a72c9229fe88ecf90cbe0ec1f26a586f0023b56 (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
<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    function test_Handler_Keyboard_initialize(t) {
        t.plan(3);
        var control = new OpenLayers.Control();
        control.id = Math.random();
        var callbacks = {foo: "bar"};
        var options = {bar: "foo"};
        
        var oldInit = OpenLayers.Handler.prototype.initialize;
        
        OpenLayers.Handler.prototype.initialize = function(con, call, opt) {
            t.eq(con.id, control.id,
                 "constructor calls parent with the correct control");
            t.eq(call, callbacks,
                 "constructor calls parent with the correct callbacks");
            t.eq(opt, options,
                 "constructor calls parent with the correct options");
        }
        var handler = new OpenLayers.Handler.Keyboard(control, callbacks,
                                                      options);

        OpenLayers.Handler.prototype.initialize = oldInit;
    }

    function test_Handler_Keyboard_destroy(t) {
        t.plan(3);
        var control = new OpenLayers.Control();        
        var handler = new OpenLayers.Handler.Keyboard(control);
        var old = OpenLayers.Handler.prototype.destroy;
        t.ok(handler.eventListener != null,
             "eventListener is not null before destroy");
        OpenLayers.Handler.prototype.destroy = function() {
            t.ok(true, "destroy calls destroy on correct parent");
        };
        handler.destroy();
        t.ok(handler.eventListener == null,
             "eventListeners is null after destroy");
        OpenLayers.Handler.prototype.destroy = old;
    }

    function test_Handler_Keyboard_activate(t) {
        t.plan(15);

        var log;
        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control();
        map.addControl(control);
        var handler = new OpenLayers.Handler.Keyboard(control);

        // mock OpenLayers.Event.observe
        var old = OpenLayers.Event.stopObserving;
        OpenLayers.Event.observe = function(obj, type, method) {
            log[type] = obj;
            var validType = OpenLayers.Util.indexOf(["keydown", "keyup"], type) != -1;
            t.ok(validType, "activate calls observe for " + type);
            t.ok(method == handler.eventListener,
                 "activate calls observing with correct method");
        };

        handler.active = true;
        var activated = handler.activate();
        t.ok(!activated,
             "activate returns false if the handler was already active");

        log = {};
        handler.active = false;
        handler.observeElement = map.div;
        activated = handler.activate();
        t.ok(log['keydown'] == map.div,
             "activate calls observing for keydown with correct object");
        t.ok(log['keyup'] == map.div,
             "activate calls observing for keyup with correct object");
        t.ok(activated,
             "activate returns true if the handler was not already active");

        log = {};
        handler.active = false;
        handler.observeElement = null;
        activated = handler.activate();
        t.ok(log['keydown'] == document,
             "activate calls observing for keydown with correct object");
        t.ok(log['keyup'] == document,
             "activate calls observing for keyup with correct object");
        t.ok(activated,
             "activate returns true if the handler was not already active");

        OpenLayers.Event.observe = old;
        map.destroy();
    }

    function test_Handler_Keyboard_deactivate(t) {
        t.plan(15);

        var log;
        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control();
        map.addControl(control);
        var handler = new OpenLayers.Handler.Keyboard(control);

        // mock OpenLayers.Event.stopObserving
        var old = OpenLayers.Event.stopObserving;
        OpenLayers.Event.stopObserving = function(obj, type, method) {
            log[type] = obj;
            var validType = OpenLayers.Util.indexOf(["keydown", "keyup"], type) != -1;
            t.ok(validType, "deactivate calls stopObserving for " + type);
            t.ok(method == handler.eventListener,
                 "deactivate calls stopObserving with correct method");
        };

        handler.active = false;
        var deactivated = handler.deactivate();
        t.ok(!deactivated,
             "deactivate returns false if the handler was not already active");

        log = {};
        handler.active = true;
        handler.observeElement = map.div;
        deactivated = handler.deactivate();
        t.ok(log['keydown'] == map.div,
             "deactivate calls stopObserving for keydown with correct object");
        t.ok(log['keyup'] == map.div,
             "deactivate calls stopObserving for keyup with correct object");
        t.ok(deactivated,
             "deactivate returns true if the handler was active already");

        log = {};
        handler.active = true;
        handler.observeElement = document;
        deactivated = handler.deactivate();
        t.ok(log['keydown'] == document,
             "deactivate calls stopObserving for keydown with correct object");
        t.ok(log['keyup'] == document,
             "deactivate calls stopObserving for keyup with correct object");
        t.ok(deactivated,
             "deactivate returns true if the handler was active already");

        OpenLayers.Event.stopObserving = old;
        map.destroy();
    }


  </script>
</head>
<body>
    <div id="map" style="width: 300px; height: 150px;"/>
</body>
</html>