summaryrefslogtreecommitdiffstats
path: root/docshell/test/browser/browser_bfcache_copycommand.js
blob: cd0015ac03e400da6177b7ee5d62c08fbe530104 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

function dummyPageURL(domain, query = "") {
  return (
    getRootDirectory(gTestPath).replace(
      "chrome://mochitests/content",
      `https://${domain}`
    ) +
    "dummy_page.html" +
    query
  );
}

async function openContextMenu(browser) {
  let contextMenu = document.getElementById("contentAreaContextMenu");
  let awaitPopupShown = BrowserTestUtils.waitForEvent(
    contextMenu,
    "popupshown"
  );
  await BrowserTestUtils.synthesizeMouse(
    "body",
    1,
    1,
    {
      type: "contextmenu",
      button: 2,
    },
    browser
  );
  await awaitPopupShown;
}

async function closeContextMenu() {
  let contextMenu = document.getElementById("contentAreaContextMenu");
  let awaitPopupHidden = BrowserTestUtils.waitForEvent(
    contextMenu,
    "popuphidden"
  );
  contextMenu.hidePopup();
  await awaitPopupHidden;
}

async function testWithDomain(domain) {
  // Passing a query to make sure the next load is never a same-document
  // navigation.
  let dummy = dummyPageURL("example.org", "?start");
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, dummy);
  let browser = tab.linkedBrowser;

  let sel = await SpecialPowers.spawn(browser, [], function () {
    let sel = content.getSelection();
    sel.removeAllRanges();
    sel.selectAllChildren(content.document.body);
    return sel.toString();
  });

  await openContextMenu(browser);

  let copyItem = document.getElementById("context-copy");
  ok(!copyItem.disabled, "Copy item should be enabled if text is selected.");

  await closeContextMenu();

  let loaded = BrowserTestUtils.browserLoaded(browser, false, null, true);
  BrowserTestUtils.startLoadingURIString(browser, dummyPageURL(domain));
  await loaded;
  loaded = BrowserTestUtils.waitForLocationChange(gBrowser, dummy);
  browser.goBack();
  await loaded;

  let sel2 = await SpecialPowers.spawn(browser, [], function () {
    return content.getSelection().toString();
  });
  is(sel, sel2, "Selection should remain when coming out of BFCache.");

  await openContextMenu(browser);

  ok(!copyItem.disabled, "Copy item should be enabled if text is selected.");

  await closeContextMenu();

  await BrowserTestUtils.removeTab(tab);
}

add_task(async function testValidSameOrigin() {
  await testWithDomain("example.org");
});

add_task(async function testValidCrossOrigin() {
  await testWithDomain("example.com");
});

add_task(async function testInvalid() {
  await testWithDomain("this.is.invalid");
});