Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Merge pull request #429 from arv/touch-list-fix
Browse files Browse the repository at this point in the history
Touch event fixes
  • Loading branch information
dfreedm committed Apr 29, 2014
2 parents 68c2bfa + 2015c23 commit 91c249e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/wrappers/TouchEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
var UIEvent = scope.wrappers.UIEvent;
var mixin = scope.mixin;
var registerWrapper = scope.registerWrapper;
var unwrap = scope.unwrap;
var wrap = scope.wrap;

// TouchEvent is WebKit/Blink only.
Expand All @@ -26,6 +27,8 @@
return;
}

var nonEnumDescriptor = {enumerable: false};

function nonEnum(obj, prop) {
Object.defineProperty(obj, prop, nonEnumDescriptor);
}
Expand Down
79 changes: 66 additions & 13 deletions test/js/TouchEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,8 @@ htmlSuite('Events', function() {
return;
}

test('TouchEvent', function() {
var e = document.createEvent('TouchEvent');
assert.instanceOf(e, TouchEvent);
assert.instanceOf(e, UIEvent);
assert.instanceOf(e, Event);
});

test('Touch', function() {
// There is no way to create a native Touch object so we use a mock impl.

var target = document.createElement('div');
var impl = {
function createMockTouch(nativeTarget) {
return {
clientX: 1,
clientY: 2,
screenX: 3,
Expand All @@ -39,8 +29,22 @@ htmlSuite('Events', function() {
webkitRadiusY: 9,
webkitRotationAngle: 10,
webkitForce: 11,
target: unwrap(target)
target: nativeTarget
};
}

test('TouchEvent', function() {
var e = document.createEvent('TouchEvent');
assert.instanceOf(e, TouchEvent);
assert.instanceOf(e, UIEvent);
assert.instanceOf(e, Event);
});

test('Touch', function() {
// There is no way to create a native Touch object so we use a mock impl.

var target = document.createElement('div');
var impl = createMockTouch(unwrap(target));
var touch = new Touch(impl);

assert.equal(touch.clientX, 1);
Expand All @@ -57,4 +61,53 @@ htmlSuite('Events', function() {
assert.equal(touch.target, target);
});

test('TouchList', function() {

function createMockTouchList(elements) {
var arr = [];
for (var i = 0; i < elements.length; i++) {
arr[i] = createMockTouch(unwrap(elements[i]));
}
return arr;
}

var a = document.createElement('a');
var b = document.createElement('b');
var c = document.createElement('c');
var d = document.createElement('d');
var e = document.createElement('e');
var f = document.createElement('f');

var mockEvent = {
__proto__: unwrap(document.createEvent('TouchEvent')).__proto__,
touches: createMockTouchList([a]),
targetTouches: createMockTouchList([b, c]),
changedTouches: createMockTouchList([d, e, f])
};

var event = wrap(mockEvent);

assert.instanceOf(event.touches, TouchList);
assert.instanceOf(event.targetTouches, TouchList);
assert.instanceOf(event.changedTouches, TouchList);

assert.equal(event.touches.length, 1);
assert.equal(event.targetTouches.length, 2);
assert.equal(event.changedTouches.length, 3);

assert.instanceOf(event.touches[0], Touch);
assert.instanceOf(event.targetTouches[0], Touch);
assert.instanceOf(event.targetTouches[1], Touch);
assert.instanceOf(event.changedTouches[0], Touch);
assert.instanceOf(event.changedTouches[1], Touch);
assert.instanceOf(event.changedTouches[2], Touch);

assert.equal(event.touches[0].target, a);
assert.equal(event.targetTouches[0].target, b);
assert.equal(event.targetTouches[1].target, c);
assert.equal(event.changedTouches[0].target, d);
assert.equal(event.changedTouches[1].target, e);
assert.equal(event.changedTouches[2].target, f);
});

});

0 comments on commit 91c249e

Please sign in to comment.