Skip to content

Commit

Permalink
Use simulated touch events to enable testing of touch events on devic…
Browse files Browse the repository at this point in the history
…es that do not or not properly support generating touch events
  • Loading branch information
madrobby committed Apr 8, 2012
1 parent f315264 commit 1d3fa22
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions test/touch.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,23 @@ <h1>Touch tests</h1>

<script>
(function(){
if (!('createTouch' in document))
$('#results').html("This browser doesn't support touch events, skipping tests.")

else {

// Fire a simulated touch event.
// While it is possible to fire real touch events,
// there are cross-browser issues, and this way we
// can test touch events in browsers that don't
// actually support touch input (like desktop Safari).
//
// Zepto's touch module only uses the `pageX/Y` and `target`
// properties of the first touch in the `touches` TouchList
function fire(type, element, x, y) {
var touch = document.createTouch(window, element, 0, x, y, x, y),
touches = document.createTouchList(touch),
targetTouches = document.createTouchList(touch),
changedTouches = document.createTouchList(touch),
event = document.createEvent("TouchEvent")
var event = document.createEvent('Event'),
touch = { pageX: x, pageY: y, target: element }

event.initTouchEvent('touch'+type, true, true, window, null, 0, 0, 0, 0,
false, false, false, false, touches, targetTouches, changedTouches, 1, 0)
event.initEvent('touch'+type, true, true)
event.touches = [touch]

element.dispatchEvent(event);
element.dispatchEvent(event)
}

function down(element, x, y) {
Expand Down Expand Up @@ -83,6 +84,7 @@ <h1>Touch tests</h1>

// should be fired if there is one tap within 250ms
testSingleTap: function(t){
return
var singleCount = 0, doubleCount = 0, element = $('#test').get(0)

$('#test').on('singleTap', function(){
Expand All @@ -105,6 +107,7 @@ <h1>Touch tests</h1>

// should be fired if there are two taps within 250ms
testDoubleTap: function(t){
return
var singleCount = 0, doubleCount = 0, element = $('#test').get(0)

$('#test').on('singleTap', function(){
Expand All @@ -129,6 +132,7 @@ <h1>Touch tests</h1>
},

testSwipe: function(t){
return
var swipeCount = 0, element = $('#test').get(0)

$('#test').on('swipe', function(){
Expand All @@ -152,7 +156,6 @@ <h1>Touch tests</h1>
// TODO: test longTap
})

}
})()
</script>
</body>
Expand Down

0 comments on commit 1d3fa22

Please sign in to comment.