B2G/gecko/testing/marionette/marionette-listener.js
//x and y are coordinates relative to the viewport
x and y are coordinates relative to the viewport
x and y are coordinates relative to the viewport
function generateEvents(type, x, y, touchId, target) {
  lastCoordinates = [x, y];
  let doc = curWindow.document;
  switch (type) {
    case 'tap':
      if (mouseEventsOnly) {
        mousetap(target.ownerDocument, x, y);
      }
      else {
        let touchId = nextTouchId++;
        let touch = createATouch(target, x, y, touchId);
        emitTouchEvent('touchstart', touch);
        emitTouchEvent('touchend', touch);
        mousetap(target.ownerDocument, x, y);
      }
      lastCoordinates = null;
      break;
    case 'press':
      isTap = true;
      if (mouseEventsOnly) {
        emitMouseEvent(doc, 'mousemove', x, y);
        emitMouseEvent(doc, 'mousedown', x, y);
      }
      else {
        let touchId = nextTouchId++;
        let touch = createATouch(target, x, y, touchId);
        emitTouchEvent('touchstart', touch);
        touchIds[touchId] = touch;
        return touchId;
      }
      break;
    case 'release':
      if (mouseEventsOnly) {
        emitMouseEvent(doc, 'mouseup', lastCoordinates[0], lastCoordinates[1]);
      }
      else {
        let touch = touchIds[touchId];
        touch = createATouch(touch.target, lastCoordinates[0], lastCoordinates[1], touchId);
        emitTouchEvent('touchend', touch);
        if (isTap) {
          mousetap(touch.target.ownerDocument, touch.clientX, touch.clientY);
        }
        delete touchIds[touchId];
      }
      isTap = false;
      lastCoordinates = null;
      break;
    case 'cancel':
      isTap = false;
      if (mouseEventsOnly) {
        emitMouseEvent(doc, 'mouseup', lastCoordinates[0], lastCoordinates[1]);
      }
      else {
        emitTouchEvent('touchcancel', touchIds[touchId]);
        delete touchIds[touchId];
      }
      lastCoordinates = null;
      break;
    case 'move':
      isTap = false;
      if (mouseEventsOnly) {
        emitMouseEvent(doc, 'mousemove', x, y);
      }
      else {
        touch = createATouch(touchIds[touchId].target, x, y, touchId);
        touchIds[touchId] = touch;
        emitTouchEvent('touchmove', touch);
      }
      break;
    case 'contextmenu':
      isTap = false;
      let event = curWindow.document.createEvent('HTMLEvents');
      event.initEvent('contextmenu', true, true);
      if (mouseEventsOnly) {
        target = doc.elementFromPoint(lastCoordinates[0], lastCoordinates[1]);
      }
      else {
        target = touchIds[touchId].target;
      }
      target.dispatchEvent(event);
      break;
    default:
      throw {message:"Unknown event type: " + type, code: 500, stack:null};
  }
}
function generateEvents ( type , x , y , touchId , target ) { lastCoordinates = [ x , y ] ; let doc = curWindow . document ; switch ( type ) { case 'tap' : if ( mouseEventsOnly ) { mousetap ( target . ownerDocument , x , y ) ; } else { let touchId = nextTouchId ++ ; let touch = createATouch ( target , x , y , touchId ) ; emitTouchEvent ( 'touchstart' , touch ) ; emitTouchEvent ( 'touchend' , touch ) ; mousetap ( target . ownerDocument , x , y ) ; } lastCoordinates = null ; break ; case 'press' : isTap = true ; if ( mouseEventsOnly ) { emitMouseEvent ( doc , 'mousemove' , x , y ) ; emitMouseEvent ( doc , 'mousedown' , x , y ) ; } else { let touchId = nextTouchId ++ ; let touch = createATouch ( target , x , y , touchId ) ; emitTouchEvent ( 'touchstart' , touch ) ; touchIds [ touchId ] = touch ; return touchId ; } break ; case 'release' : if ( mouseEventsOnly ) { emitMouseEvent ( doc , 'mouseup' , lastCoordinates [ 0 ] , lastCoordinates [ 1 ] ) ; } else { let touch = touchIds [ touchId ] ; touch = createATouch ( touch . target , lastCoordinates [ 0 ] , lastCoordinates [ 1 ] , touchId ) ; emitTouchEvent ( 'touchend' , touch ) ; if ( isTap ) { mousetap ( touch . target . ownerDocument , touch . clientX , touch . clientY ) ; } delete touchIds [ touchId ] ; } isTap = false ; lastCoordinates = null ; break ; case 'cancel' : isTap = false ; if ( mouseEventsOnly ) { emitMouseEvent ( doc , 'mouseup' , lastCoordinates [ 0 ] , lastCoordinates [ 1 ] ) ; } else { emitTouchEvent ( 'touchcancel' , touchIds [ touchId ] ) ; delete touchIds [ touchId ] ; } lastCoordinates = null ; break ; case 'move' : isTap = false ; if ( mouseEventsOnly ) { emitMouseEvent ( doc , 'mousemove' , x , y ) ; } else { touch = createATouch ( touchIds [ touchId ] . target , x , y , touchId ) ; touchIds [ touchId ] = touch ; emitTouchEvent ( 'touchmove' , touch ) ; } break ; case 'contextmenu' : isTap = false ; let event = curWindow . document . createEvent ( 'HTMLEvents' ) ; event . initEvent ( 'contextmenu' , true , true ) ; if ( mouseEventsOnly ) { target = doc . elementFromPoint ( lastCoordinates [ 0 ] , lastCoordinates [ 1 ] ) ; } else { target = touchIds [ touchId ] . target ; } target . dispatchEvent ( event ) ; break ; default : throw { message : "Unknown event type: " + type , code : 500 , stack : null } ; } }
x and y are coordinates relative to the viewport
x and y are coordinates relative to the viewport