summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/browser/browser_keyevents_during_autoscrolling.js
blob: f92d7089ab0babbcf5d37a51240c9fa839b45e06 (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
add_task(async function () {
  const kPrefName_AutoScroll = "general.autoScroll";
  Services.prefs.setBoolPref(kPrefName_AutoScroll, true);
  registerCleanupFunction(() =>
    Services.prefs.clearUserPref(kPrefName_AutoScroll)
  );

  const kNoKeyEvents = 0;
  const kKeyDownEvent = 1;
  const kKeyPressEvent = 2;
  const kKeyUpEvent = 4;
  const kAllKeyEvents = 7;

  var expectedKeyEvents;
  var dispatchedKeyEvents;
  var key;

  /**
   * Encapsulates EventUtils.sendChar().
   */
  function sendChar(aChar) {
    key = aChar;
    dispatchedKeyEvents = kNoKeyEvents;
    EventUtils.sendChar(key);
    is(
      dispatchedKeyEvents,
      expectedKeyEvents,
      "unexpected key events were dispatched or not dispatched: " + key
    );
  }

  /**
   * Encapsulates EventUtils.sendKey().
   */
  function sendKey(aKey) {
    key = aKey;
    dispatchedKeyEvents = kNoKeyEvents;
    EventUtils.sendKey(key);
    is(
      dispatchedKeyEvents,
      expectedKeyEvents,
      "unexpected key events were dispatched or not dispatched: " + key
    );
  }

  function onKey(aEvent) {
    //    if (aEvent.target != root && aEvent.target != root.ownerDocument.body) {
    //      ok(false, "unknown target: " + aEvent.target.tagName);
    //      return;
    //    }

    var keyFlag;
    switch (aEvent.type) {
      case "keydown":
        keyFlag = kKeyDownEvent;
        break;
      case "keypress":
        keyFlag = kKeyPressEvent;
        break;
      case "keyup":
        keyFlag = kKeyUpEvent;
        break;
      default:
        ok(false, "Unknown events: " + aEvent.type);
        return;
    }
    dispatchedKeyEvents |= keyFlag;
    is(keyFlag, expectedKeyEvents & keyFlag, aEvent.type + " fired: " + key);
  }

  var dataUri = 'data:text/html,<body style="height:10000px;"></body>';

  await BrowserTestUtils.withNewTab(dataUri, async function (browser) {
    info("Loaded data URI in new tab");
    await SimpleTest.promiseFocus(browser);
    info("Focused selected browser");

    window.addEventListener("keydown", onKey);
    window.addEventListener("keypress", onKey);
    window.addEventListener("keyup", onKey);
    registerCleanupFunction(() => {
      window.removeEventListener("keydown", onKey);
      window.removeEventListener("keypress", onKey);
      window.removeEventListener("keyup", onKey);
    });

    // Test whether the key events are handled correctly under normal condition
    expectedKeyEvents = kAllKeyEvents;
    sendChar("A");

    // Start autoscrolling by middle button click on the page
    info("Creating popup shown promise");
    let shownPromise = BrowserTestUtils.waitForEvent(
      window,
      "popupshown",
      false,
      event => event.originalTarget.className == "autoscroller"
    );
    await BrowserTestUtils.synthesizeMouseAtPoint(
      10,
      10,
      { button: 1 },
      gBrowser.selectedBrowser
    );
    info("Waiting for autoscroll popup to show");
    await shownPromise;

    // Most key events should be eaten by the browser.
    expectedKeyEvents = kNoKeyEvents;
    sendChar("A");
    sendKey("DOWN");
    sendKey("RETURN");
    sendKey("RETURN");
    sendKey("HOME");
    sendKey("END");
    sendKey("TAB");
    sendKey("RETURN");

    // Finish autoscrolling by ESC key.  Note that only keydown and keypress
    // events are eaten because keyup event is fired *after* the autoscrolling
    // is finished.
    expectedKeyEvents = kKeyUpEvent;
    sendKey("ESCAPE");

    // Test whether the key events are handled correctly under normal condition
    expectedKeyEvents = kAllKeyEvents;
    sendChar("A");
  });
});