summaryrefslogtreecommitdiffstats
path: root/toolkit/components/aboutwindowsmessages/tests/browser/browser_aboutwindowsmessages.js
blob: 18fbff351a2f2f1b73183f5e5b28f8909fd7dfbf (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

async function resizeWindow(windowToResize, width, height) {
  let resizePromise = BrowserTestUtils.waitForEvent(
    windowToResize,
    "resize",
    false
  );
  windowToResize.resizeTo(width, height);
  await resizePromise;
}

add_task(async () => {
  const TEST_LINK = "https://example.com/";
  let originalBrowserWindow =
    Services.wm.getMostRecentWindow("navigator:browser");
  let originalBrowser = originalBrowserWindow.gBrowser;
  // Resize this window so we can check for WM_NCCALCSIZE events below
  await resizeWindow(originalBrowserWindow, 500, 400);
  await resizeWindow(originalBrowserWindow, 600, 500);
  await resizeWindow(originalBrowserWindow, 700, 600);
  let newWindow = await BrowserTestUtils.openNewBrowserWindow({
    url: TEST_LINK,
  });
  registerCleanupFunction(async function () {
    await BrowserTestUtils.closeWindow(newWindow);
  });
  await BrowserTestUtils.withNewTab(
    { gBrowser: originalBrowser, url: "about:windows-messages" },
    async () => {
      let messagesList = content.document.getElementById("windows-div");
      // This is tricky because the test framework has its own windows
      Assert.greaterOrEqual(
        messagesList.childNodes.length,
        2,
        "should have enough window entries"
      );

      let firstList = true;
      for (let sublist of messagesList.childNodes) {
        const messages = Array.from(sublist.querySelectorAll("li.message"));
        const numberOfMessages = messages.length;
        // Every window gets a few window messages when it opens, so there
        // should be at least a few here.
        Assert.greaterOrEqual(
          numberOfMessages,
          3,
          "should have enough messages for the current window"
        );
        if (firstList) {
          // It would be nice if we could check for a less obscure event.
          // However, since we're resizing the window programatically we don't
          // get WM_SIZE or WM_SIZING events.
          Assert.greaterOrEqual(
            messages.filter(messageLi =>
              messageLi.innerHTML.includes("WM_NCCALCSIZE")
            ).length,
            5,
            "active window should have enough WM_NCCALCSIZE events"
          );
          firstList = false;
        }
        let buttons = sublist.querySelectorAll("button");
        Assert.equal(buttons.length, 1, "should have only one button");
        let clipboardButton = buttons[0];
        clipboardButton.click();
        // Wait until copying is done and the button becomes clickable.
        await BrowserTestUtils.waitForMutationCondition(
          clipboardButton,
          { attributes: true },
          () => !clipboardButton.disabled
        );
        const clipboardText = await navigator.clipboard.readText();
        const numberOfLines = Array.from(clipboardText.matchAll("\n")).length;
        Assert.equal(
          numberOfLines,
          numberOfMessages,
          "should copy the right number of lines to the clipboard"
        );
      }
    }
  );
});