summaryrefslogtreecommitdiffstats
path: root/toolkit/components/pictureinpicture/tests/browser_preserveTabPipIconOverlay.js
blob: b1296607f1d7f1241a00313029e64fb3bbffd264 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

requestLongerTimeout(2);

const EVENTUTILS_URL =
  "chrome://mochikit/content/tests/SimpleTest/EventUtils.js";
var EventUtils = {};

Services.scriptloader.loadSubScript(EVENTUTILS_URL, EventUtils);

async function detachTab(tab) {
  let newWindowPromise = new Promise(resolve => {
    let observe = win => {
      Services.obs.removeObserver(observe, "domwindowopened");
      resolve(win);
    };
    Services.obs.addObserver(observe, "domwindowopened");
  });

  await EventUtils.synthesizePlainDragAndDrop({
    srcElement: tab,

    // destElement is null because tab detaching happens due
    // to a drag'n'drop on an invalid drop target.
    destElement: null,

    // don't move horizontally because that could cause a tab move
    // animation, and there's code to prevent a tab detaching if
    // the dragged tab is released while the animation is running.
    stepX: 0,
    stepY: 100,
  });

  return newWindowPromise;
}

/**
 * Tests that tabs dragged between windows with PiP open, the pip attribute stays
 */
add_task(async function test_dragging_pip_to_other_window() {
  // initialize
  let win1 = await BrowserTestUtils.openNewBrowserWindow();
  let win2 = await BrowserTestUtils.openNewBrowserWindow();

  let pipTab = await BrowserTestUtils.openNewForegroundTab(
    win1.gBrowser,
    TEST_PAGE
  );
  let destTab = await BrowserTestUtils.openNewForegroundTab(win2.gBrowser);

  let awaitCloseEventPromise = BrowserTestUtils.waitForEvent(
    pipTab,
    "TabClose"
  );
  let tabSwapPictureInPictureEventPromise = BrowserTestUtils.waitForEvent(
    pipTab,
    "TabSwapPictureInPicture"
  );

  // Open PiP
  let videoID = "with-controls";
  let browser = pipTab.linkedBrowser;

  let pipWin = await triggerPictureInPicture(browser, videoID);
  ok(pipWin, "Got Picture-in-Picture window.");

  // tear out window
  let effect = EventUtils.synthesizeDrop(
    pipTab,
    destTab,
    [[{ type: TAB_DROP_TYPE, data: pipTab }]],
    null,
    win1,
    win2
  );
  is(effect, "move", "Tab should be moved from win1 to win2.");

  let closeEvent = await awaitCloseEventPromise;
  let swappedPipTabsEvent = await tabSwapPictureInPictureEventPromise;

  is(
    closeEvent.detail.adoptedBy,
    swappedPipTabsEvent.detail,
    "Pip tab adopted by new tab created when original tab closed"
  );

  // make sure we reassign the pip tab to the new one
  pipTab = swappedPipTabsEvent.detail;

  // check PiP attribute
  ok(pipTab.hasAttribute("pictureinpicture"), "Tab should have PiP attribute");

  // end PiP
  let pipClosed = BrowserTestUtils.domWindowClosed(pipWin);
  let closeButton = pipWin.document.getElementById("close");
  EventUtils.synthesizeMouseAtCenter(closeButton, {}, pipWin);
  await pipClosed;

  // ensure PiP attribute is gone
  await TestUtils.waitForCondition(
    () => !pipTab.hasAttribute("pictureinpicture"),
    "pictureinpicture attribute was removed"
  );

  ok(true, "pictureinpicture attribute successfully cleared");

  // close windows
  await BrowserTestUtils.closeWindow(win1);
  await BrowserTestUtils.closeWindow(win2);
});

/**
 * Tests that tabs torn out into a new window with PiP open, the pip attribute stays
 */
add_task(async function test_dragging_pip_into_new_window() {
  // initialize
  await BrowserTestUtils.withNewTab(
    {
      url: TEST_PAGE,
      gBrowser,
    },
    async browser => {
      // Create PiP
      let videoID = "with-controls";
      let pipTab = gBrowser.getTabForBrowser(browser);

      let pipWin = await triggerPictureInPicture(browser, videoID);

      let tabSwapPictureInPictureEventPromise = BrowserTestUtils.waitForEvent(
        pipTab,
        "TabSwapPictureInPicture"
      );

      // tear out into new window
      let newWin = await detachTab(pipTab);

      let swappedPipTabsEvent = await tabSwapPictureInPictureEventPromise;
      pipTab = swappedPipTabsEvent.detail;

      // check PiP attribute
      ok(
        pipTab.hasAttribute("pictureinpicture"),
        "Tab should have PiP attribute"
      );

      // end PiP

      let pipClosed = BrowserTestUtils.domWindowClosed(pipWin);
      let closeButton = pipWin.document.getElementById("close");
      EventUtils.synthesizeMouseAtCenter(closeButton, {}, pipWin);
      await pipClosed;

      // ensure pip attribute is gone
      await TestUtils.waitForCondition(
        () => !pipTab.hasAttribute("pictureinpicture"),
        "pictureinpicture attribute was removed"
      );
      ok(true, "pictureinpicture attribute successfully cleared");

      // close windows
      await BrowserTestUtils.closeWindow(newWin);
    }
  );
});