summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/fonts/test/browser_fontinspector_reveal-in-page.js
blob: bcefe0b3bf6a5a5aedb9d71bad1b36bab76beab2 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

// Test that fonts usage can be revealed in the page using the FontsHighlighter.

const TEST_URI = URL_ROOT + "doc_browser_fontinspector.html";

add_task(async function () {
  // Make sure the toolbox is tall enough to accomodate all fonts, otherwise mouseover
  // events simulation will fail.
  await pushPref("devtools.toolbox.footer.height", 500);

  const { view } = await openFontInspectorForURL(TEST_URI);
  await testFontHighlighting(view);

  info("Check that highlighting still works after reloading the page");
  await reloadBrowser();
  await testFontHighlighting(view);
});

async function testFontHighlighting(view) {
  // The number of window selection change events we expect to get as we hover over each
  // font in the list. Waiting for those events is how we know that text-runs were
  // highlighted in the page.
  // The reason why these numbers vary is because the highlighter may create more than
  // 1 selection range object, depending on the number of text-runs found.
  const expectedSelectionChangeEvents = [2, 2, 2, 1, 1];

  const viewDoc = view.document;

  // Wait for the view to have all the expected used fonts.
  const fontEls = await waitFor(() => {
    const els = getUsedFontsEls(viewDoc);
    if (els.length !== expectedSelectionChangeEvents.length) {
      return false;
    }

    return els;
  });

  for (let i = 0; i < fontEls.length; i++) {
    info(
      `Mousing over and out of font number ${i} ("${fontEls[i].textContent}") in the list`
    );

    // Simulating a mouse over event on the font name and expecting a selectionchange.
    const nameEl = fontEls[i];
    let onEvents = waitForNSelectionEvents(expectedSelectionChangeEvents[i]);
    EventUtils.synthesizeMouse(
      nameEl,
      2,
      2,
      { type: "mouseover" },
      viewDoc.defaultView
    );
    await onEvents;

    ok(
      true,
      `${expectedSelectionChangeEvents[i]} selectionchange events detected on mouseover`
    );

    // Simulating a mouse out event on the font name and expecting a selectionchange.
    const otherEl = viewDoc.querySelector("body");
    onEvents = waitForNSelectionEvents(1);
    EventUtils.synthesizeMouse(
      otherEl,
      2,
      2,
      { type: "mouseover" },
      viewDoc.defaultView
    );
    await onEvents;

    ok(true, "1 selectionchange events detected on mouseout");
  }
}

async function waitForNSelectionEvents(numberOfTimes) {
  await SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [numberOfTimes],
    async function (n) {
      const win = content.wrappedJSObject;

      await new Promise(resolve => {
        let received = 0;
        win.document.addEventListener("selectionchange", function listen() {
          received++;

          if (received === n) {
            win.document.removeEventListener("selectionchange", listen);
            resolve();
          }
        });
      });
    }
  );
}