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
|
<html>
<head>
<script src="../OLLoader.js"></script>
<script type="text/javascript">
function test_constructor(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.Pinch(control, callbacks, options);
OpenLayers.Handler.prototype.initialize = oldInit;
}
function test_activate(t) {
t.plan(3);
var map = new OpenLayers.Map('map');
var control = new OpenLayers.Control();
map.addControl(control);
var handler = new OpenLayers.Handler.Pinch(control);
handler.active = true;
var activated = handler.activate();
t.ok(!activated,
"activate returns false if the handler was already active");
handler.active = false;
handler.pinching = true;
activated = handler.activate();
t.ok(activated,
"activate returns true if the handler was not already active");
t.ok(!handler.pinching,
"activate sets pinching to false");
}
function test_events(t) {
// each handled event should be activated twice when handler is
// activated, so:
// 27 = 4tests * 2*3events + 1tests * 3events
t.plan(27);
var map = new OpenLayers.Map('map');
var control = new OpenLayers.Control();
map.addControl(control);
var handler = new OpenLayers.Handler.Pinch(control);
// list below events that should be handled (events) and those
// that should not be handled (nonevents) by the handler
var events = ["touchend", "touchmove", "touchstart"];
var nonevents = ["mousedown", "mouseup", "mousemove", "mouseout",
"click", "dblclick", "resize", "focus", "blur"];
map.events.registerPriority = function(type, obj, func) {
// this is one of the mock handler methods
t.eq(OpenLayers.Util.indexOf(nonevents, type), -1,
"registered method is not one of the events " +
"that should not be handled: " + type);
t.ok(OpenLayers.Util.indexOf(events, type) > -1,
"activate calls registerPriority with browser event: " + type);
t.eq(typeof func, "function",
"activate calls registerPriority with a function");
t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Pinch",
"activate calls registerPriority with the handler");
};
handler.activate();
handler.deactivate();
// set browser event like properties on the handler
for(var i=0; i<events.length; ++i) {
setMethod(events[i]);
}
function setMethod(key) {
handler[key] = function() {return key;};
}
map.events.registerPriority = function(type, obj, func) {
var r = func();
if(typeof r == "string") {
t.eq(r, type,
"activate calls registerPriority with the correct method");
}
}
handler.activate();
}
function test_callbacks(t) {
t.plan(32);
var map = new OpenLayers.Map('map', {controls: [], fallThrough: true});
var control = new OpenLayers.Control();
map.addControl(control);
// set fake values for touches
var testEvents = {
start: {
type: 'touchstart',
touches: [{
clientX: 100,
clientY: 0
}, {
clientX: 0,
clientY: 0
}]
},
move: {
type: 'touchmove',
touches: [{
clientX: 100,
clientY: 0
}, {
clientX: 20,
clientY: 0
}]
},
done: {
type: 'touchend',
touches: []
}
};
// set callback methods
var customCb = OpenLayers.Function.False;
var cb = function(evt) {
var callback = evt.type.replace("touch", "").replace("end", "done");;
var tch = testEvents[callback].touches;
t.ok(evt.touches[0].clientX == tch[0].clientX &&
evt.touches[0].clientY == tch[0].clientY,
"touchstart sets first touch position correctly in evt");
t.ok(evt.touches[1].clientX == tch[1].clientX &&
evt.touches[1].clientY == tch[1].clientY,
"touchstart sets second touch position correctly in evt");
t.eq(handler.start.distance, 100, "start distance is " +
"always the same");
customCb.apply(this, arguments);
}
var callbacks = {
start: cb,
move: cb,
done: function () {
customCb.apply(this, arguments);
}
};
var handler = new OpenLayers.Handler.Pinch(control, callbacks);
handler.activate();
var old_isMultiTouch = OpenLayers.Event.isMultiTouch;
var old_stop = OpenLayers.Event.stop;
// test single touch
OpenLayers.Event.isMultiTouch = function() {
return false;
}
// no callbacks with tests expected (pinch not started)
map.events.handleBrowserEvent(testEvents.start);
// test 1, 2, 3
t.ok(!handler.started, "1) touchstart (singletouch) sets started to false");
t.eq(handler.start, null, "1) touchstart (singletouch) sets start to null");
t.eq(handler.last, null, "1) touchstart (singletouch) sets last to null");
handler.started = true;
handler.start = {
distance: 100,
delta: 0,
scale: 1
};
handler.last = {
distance: 150,
delta: 10,
scale: 1.5
};
// no callbacks with tests expected (multitouch pinch started, so ignores singletouch)
map.events.handleBrowserEvent(testEvents.start);
// test 4, 5, 6
t.ok(handler.started, "1) touchstart (singletouch) after pinch started is ignored");
t.ok(!!handler.start, "1) touchstart (singletouch) after pinch started is ignored");
t.ok(!!handler.last, "1) touchstart (singletouch) after pinch started is ignored");
OpenLayers.Event.stop = function(evt, allowDefault) {
if(allowDefault) {
t.fail(
"touchstart is prevented from falling to other elements");
}
}
OpenLayers.Event.isMultiTouch = function(evt) {
var res = old_isMultiTouch(evt);
t.ok(res, "fake event is a mutitouch touch event");
return res;
}
customCb = function(evt, pinchdata) {
t.eq(pinchdata.distance, 100, "2) calculated distance is correct");
t.eq(pinchdata.delta, 0, "2) calculated delta is correct");
t.eq(pinchdata.scale, 1, "2) calculated scale is correct");
}
// test 7, 8, 9, 10, 11, 12, 13
map.events.handleBrowserEvent(testEvents.start);
// test 14, 15
t.ok(handler.started, "2) touchstart sets the started flag to true");
t.ok(!handler.pinching, "2) touchstart sets the pinching flag to false");
customCb = function(evt, pinchdata) {
t.eq(pinchdata.distance, 80, "3) calculated distance is correct");
t.eq(pinchdata.delta, 20, "3) calculated delta is correct");
t.eq(pinchdata.scale, 0.8, "3) calculated scale is correct");
}
// test 16, 17, 18, 19, 20, 21, 22
map.events.handleBrowserEvent(testEvents.move);
// test 23, 24
t.ok(handler.started, "3) started flag still set to true");
t.ok(handler.pinching, "3) touchmove sets the pinching flag to true");
OpenLayers.Event.isMultiTouch = old_isMultiTouch;
customCb = function(evt, first, last) {
t.eq(first.distance, 100, "4) calculated distance is correct");
t.eq(first.delta, 0, "4) calculated delta is correct");
t.eq(first.scale, 1, "4) calculated scale is correct");
t.eq(last.distance, 80, "4) calculated distance is correct");
t.eq(last.delta, 20, "4) calculated delta is correct");
t.eq(last.scale, 0.8, "4) calculated scale is correct");
}
// test 25, 26, 27, 28, 29, 30
map.events.handleBrowserEvent(testEvents.done);
// test 31, 32
t.ok(!handler.started, "4) started flag is set to false");
t.ok(!handler.pinching, "4) touchdone sets the pinching flag to false");
OpenLayers.Event.stop = old_stop;
// test move or done before start
customCb = function(evt) {
t.fail("should not pass here")
}
// no callbacks with tests expected
map.events.handleBrowserEvent(testEvents.move);
map.events.handleBrowserEvent(testEvents.done);
}
function test_deactivate(t) {
t.plan(6);
var map = new OpenLayers.Map('map');
var control = new OpenLayers.Control();
map.addControl(control);
var handler = new OpenLayers.Handler.Pinch(control);
handler.active = false;
var deactivated = handler.deactivate();
t.ok(!deactivated,
"deactivate returns false if the handler was not already active");
handler.active = true;
handler.pinching = true;
deactivated = handler.deactivate();
t.ok(deactivated,
"deactivate returns true if the handler was active already");
t.ok(!handler.started,
"deactivate sets started to false");
t.ok(!handler.pinching,
"deactivate sets pinching to false");
t.ok(handler.start == null,
"deactivate sets start to null");
t.ok(handler.last == null,
"deactivate sets start to null");
}
</script>
</head>
<body>
<div id="map" style="width: 300px; height: 150px;"/>
</body>
</html>
|