summaryrefslogtreecommitdiffstats
path: root/dom/browser-element/mochitest/browserElement_OpenTab.js
blob: d01927e9b26678e632d26fb612b2c664b8233177 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Any copyright is dedicated to the public domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Bug 1144015 - test middle/ctrl/cmd-click on a link.

"use strict";
SimpleTest.waitForExplicitFinish();
browserElementTestHelpers.setEnabledPref(true);
browserElementTestHelpers.addPermission();

function runTest() {
  let iframe = document.createElement('iframe');
  iframe.setAttribute('mozbrowser', 'true');
  document.body.appendChild(iframe);

  let x = 2;
  let y = 2;
  // First we force a reflow so that getChildProcessOffset actually returns
  // meaningful data.
  iframe.getBoundingClientRect();
  // We need to make sure the event coordinates are actually inside the iframe,
  // relative to the chome window.
  let tabParent = SpecialPowers.wrap(iframe)
                  .QueryInterface(SpecialPowers.Ci.nsIFrameLoaderOwner)
                  .frameLoader.tabParent;
  if (tabParent) {
    let offsetX = {};
    let offsetY = {};
    tabParent.getChildProcessOffset(offsetX, offsetY);
    x -= offsetX.value;
    y -= offsetY.value;
  }

  let sendCtrlClick = () => {
    let nsIDOMWindowUtils = SpecialPowers.Ci.nsIDOMWindowUtils;
    let mod = nsIDOMWindowUtils.MODIFIER_META |
              nsIDOMWindowUtils.MODIFIER_CONTROL;
    iframe.sendMouseEvent('mousedown', x, y, 0, 1, mod);
    iframe.sendMouseEvent('mouseup', x, y, 0, 1, mod);
  }

  let onCtrlClick = e => {
    is(e.detail.url, 'http://example.com/', 'URL matches');
    iframe.removeEventListener('mozbrowseropentab', onCtrlClick);
    iframe.addEventListener('mozbrowseropentab', onMiddleClick);
    sendMiddleClick();
  }

  let sendMiddleClick = () => {
    iframe.sendMouseEvent('mousedown', x, y, 1, 1, 0);
    iframe.sendMouseEvent('mouseup', x, y, 1, 1, 0);
  }

  let onMiddleClick = e => {
    is(e.detail.url, 'http://example.com/', 'URL matches');
    iframe.removeEventListener('mozbrowseropentab', onMiddleClick);
    SimpleTest.finish();
  }

  iframe.addEventListener('mozbrowserloadend', e => {
    iframe.addEventListener('mozbrowseropentab', onCtrlClick);
    sendCtrlClick();
  });


  iframe.src = 'data:text/html,<body style="margin:0"><a href="http://example.com"><span>click here</span></a></body>';
}

addEventListener('testready', runTest);