summaryrefslogtreecommitdiffstats
path: root/browser/components/contextualidentity/test/browser/browser_usercontextid_tabdrop.js
blob: bddf39eb87bef5a2cc65449287975e7db235aa7d (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"use strict";

let EventUtils = {};
Services.scriptloader.loadSubScript(
  "chrome://mochikit/content/tests/SimpleTest/EventUtils.js",
  EventUtils
);

/**
 * Dragging an URL to a tab without userContextId set.
 */
add_task(async function () {
  let tab = BrowserTestUtils.addTab(gBrowser, "http://example.com/");
  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);

  let awaitDrop = BrowserTestUtils.waitForEvent(gBrowser.tabContainer, "drop");
  let newTabPromise = BrowserTestUtils.waitForNewTab(
    gBrowser,
    "http://test1.example.com/",
    true
  );

  // A drop type of "link" onto an existing tab would normally trigger a
  // load in that same tab, but tabbrowser code in _getDragTargetTab treats
  // drops on the outer edges of a tab differently (loading a new tab
  // instead). Make events created by synthesizeDrop have all of their
  // coordinates set to 0 (screenX/screenY), so they're treated as drops
  // on the outer edge of the tab, thus they open new tabs.
  let event = {
    clientX: 0,
    clientY: 0,
    screenX: 0,
    screenY: 0,
  };
  EventUtils.synthesizeDrop(
    tab,
    tab,
    [[{ type: "text/plain", data: "http://test1.example.com/" }]],
    "link",
    window,
    undefined,
    event
  );

  await awaitDrop;

  let tab2 = await newTabPromise;
  Assert.ok(
    !tab2.hasAttribute("usercontextid"),
    "Tab shouldn't have usercontextid attribute"
  );

  await SpecialPowers.spawn(tab2.linkedBrowser, [], async function () {
    Assert.equal(content.document.documentURI, "http://test1.example.com/");
    Assert.equal(
      content.document.nodePrincipal.originAttributes.userContextId,
      0
    );

    // referrer is empty when urls are dragged to new or existing tabs.
    // If this changes in the future, it would be okay to send the referrer
    // in this case because we are creating a new tab with the default
    // usercontextid as the original tab.
    Assert.equal(content.document.referrer, "", "referrer should be empty");
  });

  BrowserTestUtils.removeTab(tab);
  BrowserTestUtils.removeTab(tab2);
});

/**
 * When dragging an URL to a new tab, the new tab should have the same
 * userContextId as the original tab.
 */
add_task(async function () {
  let tab = BrowserTestUtils.addTab(gBrowser, "http://example.com/", {
    userContextId: 1,
  });
  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);

  let awaitDrop = BrowserTestUtils.waitForEvent(gBrowser.tabContainer, "drop");
  let newTabPromise = BrowserTestUtils.waitForNewTab(
    gBrowser,
    "http://test1.example.com/",
    true
  );

  // A drop type of "link" onto an existing tab would normally trigger a
  // load in that same tab, but tabbrowser code in _getDragTargetTab treats
  // drops on the outer edges of a tab differently (loading a new tab
  // instead). Make events created by synthesizeDrop have all of their
  // coordinates set to 0 (screenX/screenY), so they're treated as drops
  // on the outer edge of the tab, thus they open new tabs.
  let event = {
    clientX: 0,
    clientY: 0,
    screenX: 0,
    screenY: 0,
  };
  EventUtils.synthesizeDrop(
    tab,
    tab,
    [[{ type: "text/plain", data: "http://test1.example.com/" }]],
    "link",
    window,
    undefined,
    event
  );

  await awaitDrop;

  let tab2 = await newTabPromise;
  Assert.equal(tab2.getAttribute("usercontextid"), 1);

  await SpecialPowers.spawn(tab2.linkedBrowser, [], async function () {
    Assert.equal(content.document.documentURI, "http://test1.example.com/");
    Assert.equal(
      content.document.nodePrincipal.originAttributes.userContextId,
      1
    );

    // referrer is empty when urls are dragged to new or existing tabs.
    // If this changes in the future, it would be okay to send the referrer
    // in this case because we are creating a new tab with the same
    // usercontextid as the original tab.
    Assert.equal(content.document.referrer, "", "referrer should be empty");
  });

  BrowserTestUtils.removeTab(tab);
  BrowserTestUtils.removeTab(tab2);
});

/**
 * When dragging a URL from one tab or link on a tab to an existing tab, the
 * existing tab should not change its userContextId.
 * Ex: if you drag a link from tab 1 with userContext 1 to tab 2 with
 * userContext 2, the link will open in tab 2 with userContext 2.
 */
add_task(async function () {
  let tab = BrowserTestUtils.addTab(gBrowser, "http://example.com/", {
    userContextId: 1,
  });
  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);

  let tab2 = BrowserTestUtils.addTab(gBrowser, "http://example.org/", {
    userContextId: 2,
  });
  await BrowserTestUtils.browserLoaded(tab2.linkedBrowser);

  let awaitDrop = BrowserTestUtils.waitForEvent(gBrowser.tabContainer, "drop");

  EventUtils.synthesizeDrop(
    tab,
    tab2,
    [[{ type: "text/plain", data: "http://test1.example.com/" }]],
    "link",
    window
  );

  await awaitDrop;
  Assert.equal(tab2.getAttribute("usercontextid"), 2);

  await BrowserTestUtils.browserLoaded(tab2.linkedBrowser);

  await SpecialPowers.spawn(tab2.linkedBrowser, [], async function () {
    Assert.equal(content.document.documentURI, "http://test1.example.com/");
    Assert.equal(
      content.document.nodePrincipal.originAttributes.userContextId,
      2
    );

    // referrer is empty when urls are dragged to new or existing tabs.
    // If this changes in the future, we should ensure that we are not sending
    // a referrer for this case!  When opening links across user contexts, we
    // don't want the referrer to follow the user from one context to another.
    Assert.equal(content.document.referrer, "", "referrer should be empty");
  });

  BrowserTestUtils.removeTab(tab);
  BrowserTestUtils.removeTab(tab2);
});