hain, touchId, i, keyModifiers, cb);
      break;

    case "release":
      this.generateEvents(
        "release",
        this.lastCoordinates[0],
        this.lastCoordinates[1],
        touchId,
        null,
        keyModifiers
      );
      this.actions(chain, null, i, keyModifiers, cb);
      this.scrolling = false;
      break;

    case "move":
      webEl = lazy.WebElement.fromUUID(pack[1]);
      el = this.seenEls.get(webEl);
      c = lazy.element.coordinates(el);
      this.generateEvents("move", c.x, c.y, touchId, null, keyModifiers);
      this.actions(chain, touchId, i, keyModifiers, cb);
      break;

    case "moveByOffset":
      this.generateEvents(
        "move",
        this.lastCoordinates[0] + pack[1],
        this.lastCoordinates[1] + pack[2],
        touchId,
        null,
        keyModifiers
      );
      this.actions(chain, touchId, i, keyModifiers, cb);
      break;

    case "wait":
      if (pack[1] != null) {
        let time = pack[1] * 1000;

        // standard waiting time to fire contextmenu
        let standard = lazy.Preferences.get(
          CONTEXT_MENU_DELAY_PREF,
          DEFAULT_CONTEXT_MENU_DELAY
        );

        if (time >= standard && this.isTap) {
          chain.splice(i, 0, ["longPress"], ["wait", (time - standard) / 1000]);
          time = standard;
        }
        this.checkTimer.initWithCallback(
          () => this.actions(chain, touchId, i, keyModifiers, cb),
          time,
          Ci.nsITimer.TYPE_ONE_SHOT
        );
      } else {
        this.actions(chain, touchId, i, keyModifiers, cb);
      }
      break;

    case "cancel":
      this.generateEvents(
        "cancel",
        this.lastCoordinates[0],
        this.lastCoordinates[1],
        touchId,
        null,
        keyModifiers
      );
      this.actions(chain, touchId, i, keyModifiers, cb);
      this.scrolling = false;
      break;

    case "longPress":
      this.generateEvents(
        "contextmenu",
        this.lastCoordinates[0],
        this.lastCoordinates[1],
        touchId,
        null,
        keyModifiers
      );
      this.actions(chain, touchId, i, keyModifiers, cb);
      break;
  }
};

/**
 * Given an element and a pair of coordinates, returns an array of the
 * form [clientX, clientY, pageX, pageY, screenX, screenY].
 */
action.Chain.prototype.getCoordinateInfo = function (el, corx, cory) {
  let win = el.ownerGlobal;
  return [
    corx, // clientX
    cory, // clientY
    corx + win.pageXOffset, // pageX
    cory + win.pageYOffset, // pageY
    corx + win.mozInnerScreenX, // screenX
    cory + win.mozInnerScreenY, // screenY
  ];
};

/**
 * @param {string} type
 *     The event type (eg "tap", "press", ...).
 * @param {number} x
 *     X coordinate of the location to generate the event that is relative
 *     to the viewport.
 * @param {number} y
 *     Y coordinate of the location to generate the event that is relative
 *     to the viewport.
 * @param {number} touchId
 *     The current touch id.
 * @param {Element} target
 *     The Element on which the events should be created.
 */
action.Chain.prototype.generateEvents = function (
  type,
  x,
  y,
  touchId,
  target,
  keyModifiers
) {
  this.lastCoordinates = [x, y];
  let doc = this.container.frame.document;

  switch (type) {
    case "tap":
      if (this.mouseEventsOnly) {
        let touch = this.createATouch(target, x, y, touchId);
        this.mouseTap(
          touch.target.ownerDocument,
          touch.clientX,
          touch.clientY,
          null,
          null,
          keyModifiers
        );
      } else {
        touchId = this.nextTouchId++;
        let touch = this.createATouch(target, x, y, touchId);
        this.emitTouchEvent(doc, "touchstart", touch);
        this.emitTouchEvent(doc, "touchend", touch);
        this.mouseTap(
          touch.target.ownerDocument,
          touch.clientX,
          touch.clientY,
          null,
          null,
          keyModifiers
        );
      }
      this.lastCoordinates = null;
      break;

    case "press":
      this.isTap = true;
      if (this.mouseEventsOnly) {
        this.emitMouseEvent(doc, "mousemove", x, y, null, null, keyModifiers);
        this.emitMouseEvent(doc, "mousedown", x, y, null, null, keyModifiers);
      } else {
        touchId = this.nextTouchId++;
        let touch = this.createATouch(target, x, y, touchId);
        this.emitTouchEvent(doc, "touchstart", touch);
        this.touchIds[touchId] = touch;
        return touchId;
      }
      break;

    case "release":
      if (this.mouseEventsOnly) {
        let [x, y] = this.lastCoordinates;
        this.emitMouseEvent(doc, "mouseup", x, y, null, null, keyModifiers);
      } else {
        let touch = this.touchIds[touchId];
        let [x, y] = this.lastCoordinates;

        touch = this.createATouch(touch.target, x, y, touchId);
        this.emitTouchEvent(doc, "touchend", touch);

        if (this.isTap) {
          this.mouseTap(
            touch.target.ownerDocument,
            touch.clientX,
            touch.clientY,
            null,
            null,
            keyModifiers
          );
        }
        delete this.touchIds[touchId];
      }

      this.isTap = false;
      this.lastCoordinates = null;
      break;

    case "cancel":
      this.isTap = false;
      if (this.mouseEventsOnly) {
        let [x, y] = this.lastCoordinates;
        this.emitMouseEvent(doc, "mouseup", x, y, null, null, keyModifiers);
      } else {
        this.emitTouchEvent(doc, "touchcancel", this.touchIds[touchId]);
        delete this.touchIds[touchId];
      }
      this.lastCoordinates = null;
      break;

    case "move":
      this.isTap = false;
      if (this.mouseEventsOnly) {
        this.emitMouseEvent(doc, "mousemove", x, y, null, null, keyModifiers);
      } else {
        let touch = this.createATouch(
          this.touchIds[touchId].target,
          x,
          y,
          touchId
        );
        this.touchIds[touchId] = touch;
        this.emitTouchEvent(doc, "touchmove", touch);
      }
      break;

    case "contextmenu":
      this.isTap = false;
      let event = this.container.frame.document.createEvent("MouseEvents");
      if (this.mouseEventsOnly) {
        target = doc.elementFromPoint(
          this.lastCoordinates[0],
          this.lastCoordinates[1]
        );
      } else {
        target = this.touchIds[touchId].target;
      }

      let [clientX, clientY, , , screenX, screenY] = this.getCoordinateInfo(
        target,
        x,
        y
      );

      event.initMouseEvent(
        "contextmenu",
        true,
        true,
        target.ownerGlobal,
        1,
        screenX,
        screenY,
        clientX,
        clientY,
        false,
        false,
        false,
        false,
        0,
        null
      );
      target.dispatchEvent(event);
      break;

    default:
      throw new lazy.error.WebDriverError("Unknown event type: " + type);
  }
  return null;
};

action.Chain.prototype.mouseTap = function (doc, x, y, button, count, mod) {
  this.emitMouseEvent(doc, "mousemove", x, y, button, count, mod);
  this.emitMouseEvent(doc, "mousedown", x, y, button, count, mod);
  this.emitMouseEvent(doc, "mouseup", x, y, button, count, mod);
};
PK