-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
683 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
var KEY = '__tests'; | ||
|
||
function onHalfTime(api) { | ||
api.set('actionTime', +new Date()); | ||
api.switchToIframe(); | ||
api.injectLocalJs('./../../node_modules/mock-phantom-touch-events/lib/index.js'); | ||
api.evaluate(function (key) { | ||
|
||
function collectEvents(subKey) { | ||
return function (synteticEvent) { | ||
window[key] = window[key] || {}; | ||
window[key][subKey] = window[key][subKey] || {}; | ||
window[key][subKey][synteticEvent.type] = window[key][subKey][synteticEvent.type] || []; | ||
var o = { | ||
't': +new Date(), | ||
'x': synteticEvent.pageX, | ||
'y': synteticEvent.pageY, | ||
// we keep the event so we can collect/instrument before closing down page | ||
'__event': synteticEvent | ||
}; | ||
window[key][subKey][synteticEvent.type].push(o); | ||
}; | ||
} | ||
|
||
try { | ||
// window.touchActionSequence( | ||
// document.getElementById('GARDR').getElementsByTagName('div')[0], | ||
// [85, 80], | ||
// [400, 45], | ||
// 750, | ||
// 50, | ||
// collectEvents('testSwipe') | ||
// ); | ||
|
||
var gardrElem = document.getElementById('GARDR'); | ||
if (gardrElem !== null) { | ||
var elem = gardrElem.querySelector('div[onclick],div'); | ||
|
||
window.swipeTop( | ||
elem, | ||
250, | ||
20, | ||
collectEvents('swipeTop') | ||
); | ||
setTimeout(function () { | ||
window.swipeRight( | ||
elem, | ||
250, | ||
20, | ||
collectEvents('swipeRight') | ||
); | ||
setTimeout(function () { | ||
window.swipeLeft( | ||
elem, | ||
250, | ||
20, | ||
collectEvents('swipeLeft') | ||
); | ||
}, 1500); | ||
}, 1500); | ||
} | ||
|
||
} catch (e) { | ||
console.log('Failed swiping', e, e.message, e.stack); | ||
} | ||
}, KEY); | ||
} | ||
|
||
function onBeforeExit(api) { | ||
var probed = api.evaluate(function (key) { | ||
if (!window[key]) { | ||
return { | ||
noTouchEventsFound: true | ||
}; | ||
} | ||
Object.keys(window[key]).forEach(function (_key) { | ||
Object.keys(window[key][_key]).forEach(function (_method) { | ||
window[key][_key][_method].forEach(function (e) { | ||
e.returnValue = e.__event.returnValue; | ||
e.defaultPrevented = e.__event.defaultPrevented; | ||
delete e.__event; | ||
}); | ||
}); | ||
}); | ||
return { | ||
tests: window[key] | ||
}; | ||
}, KEY); | ||
api.set('touchEventData', (probed && probed.tests || {})); | ||
} | ||
|
||
module.exports = { | ||
'onHalfTime': onHalfTime, | ||
'onBeforeExit': onBeforeExit | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
var allowedToPreventKeys = ['swipeLeft', 'swipeRight']; | ||
var notAllowedToSwipe = ['swipeTop', 'swipeBottom']; | ||
|
||
function verify(touchEvents, mapper){ | ||
return Object.keys(touchEvents).some(function(touchEventType){ | ||
var events = touchEvents[touchEventType]; | ||
return events && events.some(mapper); | ||
}); | ||
} | ||
|
||
function validate(harvested, report, next/*, globalOptions*/) { | ||
|
||
if (!(harvested.touch)) { | ||
return next(); | ||
} | ||
|
||
var data = harvested.touch.touchEventData; | ||
var detectedLegalUsage = false; | ||
var detectedWrongUsage = false; | ||
Object.keys(data).forEach(function(swipeKey){ | ||
var result; | ||
if (allowedToPreventKeys.indexOf(swipeKey) > -1){ | ||
result = verify(data[swipeKey], function(entry){ | ||
return (entry.defaultPrevented === true); | ||
}); | ||
if (result === true){ | ||
detectedLegalUsage = true; | ||
} | ||
} else if (notAllowedToSwipe.indexOf(swipeKey) > -1){ | ||
result = verify(data[swipeKey], function(entry){ | ||
return (entry.defaultPrevented === true || entry.returnValue === false); | ||
}); | ||
if (result === true){ | ||
detectedWrongUsage = true; | ||
report.error('Detected illegal swipe usage. Please only use horizontal touch events +/- 30%'); | ||
} | ||
} | ||
|
||
}); | ||
|
||
if (detectedLegalUsage && detectedWrongUsage !== true){ | ||
report.info('Detected swipe events inside code'); | ||
} | ||
|
||
|
||
next(); | ||
} | ||
|
||
module.exports = { | ||
dependencies: ['touch'], | ||
validate: validate | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.