summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/browser/browser_file_xpi_no_process_switch.js
blob: 67933636986d16efae0747e9f6fbcb5c9602c4d8 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */

const ADDON_INSTALL_ID = "addon-webext-permissions";

let fileurl1 = get_addon_file_url("browser_dragdrop1.xpi");
let fileurl2 = get_addon_file_url("browser_dragdrop2.xpi");

function promiseInstallNotification(aBrowser) {
  return new Promise(resolve => {
    function popupshown(event) {
      let notification = PopupNotifications.getNotification(
        ADDON_INSTALL_ID,
        aBrowser
      );
      if (!notification) {
        return;
      }

      if (gBrowser.selectedBrowser !== aBrowser) {
        return;
      }

      PopupNotifications.panel.removeEventListener("popupshown", popupshown);
      ok(true, `Got ${ADDON_INSTALL_ID} popup for browser`);
      event.target.firstChild.secondaryButton.click();
      resolve();
    }

    PopupNotifications.panel.addEventListener("popupshown", popupshown);
  });
}

function CheckBrowserInPid(browser, expectedPid, message) {
  return SpecialPowers.spawn(browser, [{ expectedPid, message }], arg => {
    is(Services.appinfo.processID, arg.expectedPid, arg.message);
  });
}

async function testOpenedAndDraggedXPI(aBrowser) {
  // Get the current pid for browser for comparison later.
  let browserPid = await SpecialPowers.spawn(aBrowser, [], () => {
    return Services.appinfo.processID;
  });

  // No process switch for XPI file:// URI in the urlbar.
  let promiseNotification = promiseInstallNotification(aBrowser);
  let urlbar = gURLBar;
  urlbar.value = fileurl1.spec;
  urlbar.focus();
  EventUtils.synthesizeKey("KEY_Enter");
  await promiseNotification;
  await CheckBrowserInPid(
    aBrowser,
    browserPid,
    "Check that browser has not switched process."
  );

  // No process switch for XPI file:// URI dragged to tab.
  let tab = gBrowser.getTabForBrowser(aBrowser);
  promiseNotification = promiseInstallNotification(aBrowser);
  let effect = EventUtils.synthesizeDrop(
    tab,
    tab,
    [[{ type: "text/uri-list", data: fileurl1.spec }]],
    "move"
  );
  is(effect, "move", "Drag should be accepted");
  await promiseNotification;
  await CheckBrowserInPid(
    aBrowser,
    browserPid,
    "Check that browser has not switched process."
  );

  // No process switch for two XPI file:// URIs dragged to tab.
  promiseNotification = promiseInstallNotification(aBrowser);
  let promiseNewTab = BrowserTestUtils.waitForEvent(
    gBrowser.tabContainer,
    "TabOpen"
  );
  effect = EventUtils.synthesizeDrop(
    tab,
    tab,
    [
      [{ type: "text/uri-list", data: fileurl1.spec }],
      [{ type: "text/uri-list", data: fileurl2.spec }],
    ],
    "move"
  );
  is(effect, "move", "Drag should be accepted");
  // When drag'n'dropping two XPIs, one is loaded in the current tab while the
  // other one is loaded in a new tab.
  let { target: newTab } = await promiseNewTab;
  // This is the prompt for the first XPI in the current tab.
  await promiseNotification;

  let promiseSecondNotification = promiseInstallNotification(
    newTab.linkedBrowser
  );

  // We switch to the second tab and wait for the prompt for the second XPI.
  BrowserTestUtils.switchTab(gBrowser, newTab);
  await promiseSecondNotification;

  BrowserTestUtils.removeTab(newTab);

  await CheckBrowserInPid(
    aBrowser,
    browserPid,
    "Check that browser has not switched process."
  );
}

// Test for bug 1175267.
add_task(async function () {
  await BrowserTestUtils.withNewTab(
    "http://example.com",
    testOpenedAndDraggedXPI
  );
  await BrowserTestUtils.withNewTab("about:robots", testOpenedAndDraggedXPI);
});