Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | function test() |
michael@0 | 2 | { |
michael@0 | 3 | const kPrefName_AutoScroll = "general.autoScroll"; |
michael@0 | 4 | Services.prefs.setBoolPref(kPrefName_AutoScroll, true); |
michael@0 | 5 | |
michael@0 | 6 | gBrowser.selectedTab = gBrowser.addTab(); |
michael@0 | 7 | |
michael@0 | 8 | const kNoKeyEvents = 0; |
michael@0 | 9 | const kKeyDownEvent = 1; |
michael@0 | 10 | const kKeyPressEvent = 2; |
michael@0 | 11 | const kKeyUpEvent = 4; |
michael@0 | 12 | const kAllKeyEvents = 7; |
michael@0 | 13 | |
michael@0 | 14 | var expectedKeyEvents; |
michael@0 | 15 | var dispatchedKeyEvents; |
michael@0 | 16 | var key; |
michael@0 | 17 | var root; |
michael@0 | 18 | |
michael@0 | 19 | /** |
michael@0 | 20 | * Encapsulates EventUtils.sendChar(). |
michael@0 | 21 | */ |
michael@0 | 22 | function sendChar(aChar) |
michael@0 | 23 | { |
michael@0 | 24 | key = aChar; |
michael@0 | 25 | dispatchedKeyEvents = kNoKeyEvents; |
michael@0 | 26 | EventUtils.sendChar(key, gBrowser.contentWindow); |
michael@0 | 27 | is(dispatchedKeyEvents, expectedKeyEvents, |
michael@0 | 28 | "unexpected key events were dispatched or not dispatched: " + key); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | /** |
michael@0 | 32 | * Encapsulates EventUtils.sendKey(). |
michael@0 | 33 | */ |
michael@0 | 34 | function sendKey(aKey) |
michael@0 | 35 | { |
michael@0 | 36 | key = aKey; |
michael@0 | 37 | dispatchedKeyEvents = kNoKeyEvents; |
michael@0 | 38 | EventUtils.sendKey(key, gBrowser.contentWindow); |
michael@0 | 39 | is(dispatchedKeyEvents, expectedKeyEvents, |
michael@0 | 40 | "unexpected key events were dispatched or not dispatched: " + key); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | function onKey(aEvent) |
michael@0 | 44 | { |
michael@0 | 45 | if (aEvent.target != root && aEvent.target != root.ownerDocument.body) { |
michael@0 | 46 | ok(false, "unknown target: " + aEvent.target.tagName); |
michael@0 | 47 | return; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | var keyFlag; |
michael@0 | 51 | switch (aEvent.type) { |
michael@0 | 52 | case "keydown": |
michael@0 | 53 | keyFlag = kKeyDownEvent; |
michael@0 | 54 | break; |
michael@0 | 55 | case "keypress": |
michael@0 | 56 | keyFlag = kKeyPressEvent; |
michael@0 | 57 | break; |
michael@0 | 58 | case "keyup": |
michael@0 | 59 | keyFlag = kKeyUpEvent; |
michael@0 | 60 | break; |
michael@0 | 61 | default: |
michael@0 | 62 | ok(false, "Unknown events: " + aEvent.type); |
michael@0 | 63 | return; |
michael@0 | 64 | } |
michael@0 | 65 | dispatchedKeyEvents |= keyFlag; |
michael@0 | 66 | is(keyFlag, expectedKeyEvents & keyFlag, aEvent.type + " fired: " + key); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | waitForExplicitFinish(); |
michael@0 | 70 | gBrowser.selectedBrowser.addEventListener("pageshow", onLoad, false); |
michael@0 | 71 | var dataUri = 'data:text/html,<body style="height:10000px;"></body>'; |
michael@0 | 72 | gBrowser.loadURI(dataUri); |
michael@0 | 73 | |
michael@0 | 74 | function onLoad() { |
michael@0 | 75 | gBrowser.selectedBrowser.removeEventListener("pageshow", onLoad, false); |
michael@0 | 76 | waitForFocus(onFocus, content); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | function onFocus() { |
michael@0 | 80 | var doc = gBrowser.contentDocument; |
michael@0 | 81 | |
michael@0 | 82 | root = doc.documentElement; |
michael@0 | 83 | root.addEventListener("keydown", onKey, true); |
michael@0 | 84 | root.addEventListener("keypress", onKey, true); |
michael@0 | 85 | root.addEventListener("keyup", onKey, true); |
michael@0 | 86 | |
michael@0 | 87 | // Test whether the key events are handled correctly under normal condition |
michael@0 | 88 | expectedKeyEvents = kAllKeyEvents; |
michael@0 | 89 | sendChar("A"); |
michael@0 | 90 | |
michael@0 | 91 | // Start autoscrolling by middle button lick on the page |
michael@0 | 92 | EventUtils.synthesizeMouse(root, 10, 10, { button: 1 }, |
michael@0 | 93 | gBrowser.contentWindow); |
michael@0 | 94 | |
michael@0 | 95 | // Before continuing the test, we need to ensure that the IPC |
michael@0 | 96 | // message that starts autoscrolling has had time to arrive. |
michael@0 | 97 | executeSoon(continueTest); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | function continueTest() { |
michael@0 | 101 | // Most key events should be eaten by the browser. |
michael@0 | 102 | expectedKeyEvents = kNoKeyEvents; |
michael@0 | 103 | sendChar("A"); |
michael@0 | 104 | sendKey("DOWN"); |
michael@0 | 105 | sendKey("RETURN"); |
michael@0 | 106 | sendKey("RETURN"); |
michael@0 | 107 | sendKey("HOME"); |
michael@0 | 108 | sendKey("END"); |
michael@0 | 109 | sendKey("TAB"); |
michael@0 | 110 | sendKey("RETURN"); |
michael@0 | 111 | |
michael@0 | 112 | // Finish autoscrolling by ESC key. Note that only keydown and keypress |
michael@0 | 113 | // events are eaten because keyup event is fired *after* the autoscrolling |
michael@0 | 114 | // is finished. |
michael@0 | 115 | expectedKeyEvents = kKeyUpEvent; |
michael@0 | 116 | sendKey("ESCAPE"); |
michael@0 | 117 | |
michael@0 | 118 | // Test whether the key events are handled correctly under normal condition |
michael@0 | 119 | expectedKeyEvents = kAllKeyEvents; |
michael@0 | 120 | sendChar("A"); |
michael@0 | 121 | |
michael@0 | 122 | root.removeEventListener("keydown", onKey, true); |
michael@0 | 123 | root.removeEventListener("keypress", onKey, true); |
michael@0 | 124 | root.removeEventListener("keyup", onKey, true); |
michael@0 | 125 | |
michael@0 | 126 | // restore the changed prefs |
michael@0 | 127 | if (Services.prefs.prefHasUserValue(kPrefName_AutoScroll)) |
michael@0 | 128 | Services.prefs.clearUserPref(kPrefName_AutoScroll); |
michael@0 | 129 | |
michael@0 | 130 | // cleaning-up |
michael@0 | 131 | gBrowser.removeCurrentTab(); |
michael@0 | 132 | |
michael@0 | 133 | finish(); |
michael@0 | 134 | } |
michael@0 | 135 | } |