summaryrefslogtreecommitdiffstats
path: root/dom/events/test/bug418986-3.js
blob: ab792cab23a1748e9f6a6d1107cff264b3a5830d (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
SimpleTest.waitForExplicitFinish();

// The main testing function.
var test = async function(isContent) {
  await SpecialPowers.contentTransformsReceived(window);

  // Each definition is [eventType, prefSetting]
  // Where we are setting the "privacy.resistFingerprinting" pref.
  let eventDefs = [
    ["mousedown", true],
    ["mouseup", true],
    ["mousedown", false],
    ["mouseup", false],
  ];

  let testCounter = 0;

  // Declare ahead of time.
  let setup;

  // This function is called when the event handler fires.
  let handleEvent = function(event, prefVal) {
    let resisting = prefVal && isContent;
    if (resisting) {
      is(
        event.screenX,
        event.clientX,
        "event.screenX and event.clientX should be the same"
      );
      is(
        event.screenY,
        event.clientY,
        "event.screenY and event.clientY should be the same"
      );
    } else {
      // We can't be sure about X coordinates not being equal, but we can test Y.
      isnot(event.screenY, event.clientY, "event.screenY !== event.clientY");
    }
    ++testCounter;
    if (testCounter < eventDefs.length) {
      nextTest();
    } else {
      SimpleTest.finish();
    }
  };

  // In this function, we set up the nth div and event handler,
  // and then synthesize a mouse event in the div, to test
  // whether the resulting events resist fingerprinting by
  // suppressing absolute screen coordinates.
  nextTest = function() {
    let [eventType, prefVal] = eventDefs[testCounter];
    SpecialPowers.pushPrefEnv(
      { set: [["privacy.resistFingerprinting", prefVal]] },
      function() {
        // The following code creates a new div for each event in eventDefs,
        // attaches a listener to listen for the event, and then generates
        // a fake event at the center of the div.
        let div = document.createElementNS(
          "http://www.w3.org/1999/xhtml",
          "div"
        );
        div.style.width = "10px";
        div.style.height = "10px";
        div.style.backgroundColor = "red";
        // Name the div after the event we're listening for.
        div.id = eventType;
        document.getElementById("body").appendChild(div);
        // Seems we can't add an event listener in chrome unless we run
        // it in a later task.
        window.setTimeout(function() {
          div.addEventListener(eventType, event => handleEvent(event, prefVal));
          // For some reason, the following synthesizeMouseAtCenter call only seems to run if we
          // wrap it in a window.setTimeout(..., 0).
          window.setTimeout(function() {
            synthesizeMouseAtCenter(div, { type: eventType });
          }, 0);
        }, 0);
      }
    );
  };

  // Now run by starting with the 0th event.
  nextTest();
};