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 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 2 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 4 | |
michael@0 | 5 | const TESTCASE_URI = TEST_BASE + "autocomplete.html"; |
michael@0 | 6 | const MAX_SUGGESTIONS = 15; |
michael@0 | 7 | |
michael@0 | 8 | // Pref which decides if CSS autocompletion is enabled in Style Editor or not. |
michael@0 | 9 | const AUTOCOMPLETION_PREF = "devtools.styleeditor.autocompletion-enabled"; |
michael@0 | 10 | |
michael@0 | 11 | const {CSSProperties, CSSValues} = getCSSKeywords(); |
michael@0 | 12 | |
michael@0 | 13 | // Test cases to test that autocompletion works correctly when enabled. |
michael@0 | 14 | // Format: |
michael@0 | 15 | // [ |
michael@0 | 16 | // key, |
michael@0 | 17 | // { |
michael@0 | 18 | // total: Number of suggestions in the popup (-1 if popup is closed), |
michael@0 | 19 | // current: Index of selected suggestion, |
michael@0 | 20 | // inserted: 1 to check whether the selected suggestion is inserted into the editor or not, |
michael@0 | 21 | // entered: 1 if the suggestion is inserted and finalized |
michael@0 | 22 | // } |
michael@0 | 23 | // ] |
michael@0 | 24 | let TEST_CASES = [ |
michael@0 | 25 | ['VK_RIGHT'], |
michael@0 | 26 | ['VK_RIGHT'], |
michael@0 | 27 | ['VK_RIGHT'], |
michael@0 | 28 | ['VK_RIGHT'], |
michael@0 | 29 | ['Ctrl+Space', {total: 1, current: 0}], |
michael@0 | 30 | ['VK_LEFT'], |
michael@0 | 31 | ['VK_RIGHT'], |
michael@0 | 32 | ['VK_DOWN'], |
michael@0 | 33 | ['VK_RIGHT'], |
michael@0 | 34 | ['VK_RIGHT'], |
michael@0 | 35 | ['VK_RIGHT'], |
michael@0 | 36 | ['Ctrl+Space', { total: getSuggestionNumberFor("font"), current: 0}], |
michael@0 | 37 | ['VK_END'], |
michael@0 | 38 | ['VK_RETURN'], |
michael@0 | 39 | ['b', {total: getSuggestionNumberFor("b"), current: 0}], |
michael@0 | 40 | ['a', {total: getSuggestionNumberFor("ba"), current: 0}], |
michael@0 | 41 | ['VK_DOWN', {total: getSuggestionNumberFor("ba"), current: 0, inserted: 1}], |
michael@0 | 42 | ['VK_TAB', {total: getSuggestionNumberFor("ba"), current: 1, inserted: 1}], |
michael@0 | 43 | ['VK_RETURN', {current: 1, inserted: 1, entered: 1}], |
michael@0 | 44 | ['b', {total: getSuggestionNumberFor("background", "b"), current: 0}], |
michael@0 | 45 | ['l', {total: getSuggestionNumberFor("background", "bl"), current: 0}], |
michael@0 | 46 | ['VK_TAB', {total: getSuggestionNumberFor("background", "bl"), current: 0, inserted: 1}], |
michael@0 | 47 | ['VK_DOWN', {total: getSuggestionNumberFor("background", "bl"), current: 1, inserted: 1}], |
michael@0 | 48 | ['VK_UP', {total: getSuggestionNumberFor("background", "bl"), current: 0, inserted: 1}], |
michael@0 | 49 | ['VK_TAB', {total: getSuggestionNumberFor("background", "bl"), current: 1, inserted: 1}], |
michael@0 | 50 | ['VK_TAB', {total: getSuggestionNumberFor("background", "bl"), current: 2, inserted: 1}], |
michael@0 | 51 | [';'], |
michael@0 | 52 | ['VK_RETURN'], |
michael@0 | 53 | ['c', {total: getSuggestionNumberFor("c"), current: 0}], |
michael@0 | 54 | ['o', {total: getSuggestionNumberFor("co"), current: 0}], |
michael@0 | 55 | ['VK_RETURN', {current: 0, inserted: 1}], |
michael@0 | 56 | ['r', {total: getSuggestionNumberFor("color", "r"), current: 0}], |
michael@0 | 57 | ['VK_RETURN', {current: 0, inserted: 1}], |
michael@0 | 58 | [';'], |
michael@0 | 59 | ['VK_LEFT'], |
michael@0 | 60 | ['VK_RIGHT'], |
michael@0 | 61 | ['VK_DOWN'], |
michael@0 | 62 | ['VK_RETURN'], |
michael@0 | 63 | ['b', {total: 2, current: 0}], |
michael@0 | 64 | ['u', {total: 1, current: 0}], |
michael@0 | 65 | ['VK_RETURN', {current: 0, inserted: 1}], |
michael@0 | 66 | ['{'], |
michael@0 | 67 | ['VK_HOME'], |
michael@0 | 68 | ['VK_DOWN'], |
michael@0 | 69 | ['VK_DOWN'], |
michael@0 | 70 | ['VK_RIGHT'], |
michael@0 | 71 | ['VK_RIGHT'], |
michael@0 | 72 | ['VK_RIGHT'], |
michael@0 | 73 | ['VK_RIGHT'], |
michael@0 | 74 | ['VK_RIGHT'], |
michael@0 | 75 | ['VK_RIGHT'], |
michael@0 | 76 | ['VK_RIGHT'], |
michael@0 | 77 | ['VK_RIGHT'], |
michael@0 | 78 | ['VK_RIGHT'], |
michael@0 | 79 | ['VK_RIGHT'], |
michael@0 | 80 | ['Ctrl+Space', {total: 1, current: 0}], |
michael@0 | 81 | ]; |
michael@0 | 82 | |
michael@0 | 83 | let gEditor; |
michael@0 | 84 | let gPopup; |
michael@0 | 85 | let index = 0; |
michael@0 | 86 | |
michael@0 | 87 | function test() |
michael@0 | 88 | { |
michael@0 | 89 | waitForExplicitFinish(); |
michael@0 | 90 | |
michael@0 | 91 | addTabAndOpenStyleEditors(1, testEditorAdded); |
michael@0 | 92 | |
michael@0 | 93 | content.location = TESTCASE_URI; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | function testEditorAdded(panel) { |
michael@0 | 97 | info("Editor added, getting the source editor and starting tests"); |
michael@0 | 98 | panel.UI.editors[0].getSourceEditor().then(editor => { |
michael@0 | 99 | info("source editor found, starting tests."); |
michael@0 | 100 | gEditor = editor.sourceEditor; |
michael@0 | 101 | gPopup = gEditor.getAutocompletionPopup(); |
michael@0 | 102 | waitForFocus(testState, gPanelWindow); |
michael@0 | 103 | }); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | function testState() { |
michael@0 | 107 | if (index == TEST_CASES.length) { |
michael@0 | 108 | testAutocompletionDisabled(); |
michael@0 | 109 | return; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | let [key, details] = TEST_CASES[index]; |
michael@0 | 113 | let entered; |
michael@0 | 114 | if (details) { |
michael@0 | 115 | entered = details.entered; |
michael@0 | 116 | } |
michael@0 | 117 | let mods = {}; |
michael@0 | 118 | |
michael@0 | 119 | info("pressing key " + key + " to get result: " + |
michael@0 | 120 | JSON.stringify(TEST_CASES[index]) + " for index " + index); |
michael@0 | 121 | |
michael@0 | 122 | let evt = "after-suggest"; |
michael@0 | 123 | |
michael@0 | 124 | if (key == 'Ctrl+Space') { |
michael@0 | 125 | key = " "; |
michael@0 | 126 | mods.accelKey = true; |
michael@0 | 127 | } |
michael@0 | 128 | else if (key == "VK_RETURN" && entered) { |
michael@0 | 129 | evt = "popup-hidden"; |
michael@0 | 130 | } |
michael@0 | 131 | else if (/(left|right|return|home|end)/ig.test(key) || |
michael@0 | 132 | (key == "VK_DOWN" && !gPopup.isOpen)) { |
michael@0 | 133 | evt = "cursorActivity"; |
michael@0 | 134 | } |
michael@0 | 135 | else if (key == "VK_TAB" || key == "VK_UP" || key == "VK_DOWN") { |
michael@0 | 136 | evt = "suggestion-entered"; |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | gEditor.once(evt, checkState); |
michael@0 | 140 | EventUtils.synthesizeKey(key, mods, gPanelWindow); |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | function checkState() { |
michael@0 | 144 | executeSoon(() => { |
michael@0 | 145 | let [key, details] = TEST_CASES[index]; |
michael@0 | 146 | details = details || {}; |
michael@0 | 147 | let {total, current, inserted} = details; |
michael@0 | 148 | |
michael@0 | 149 | if (total != undefined) { |
michael@0 | 150 | ok(gPopup.isOpen, "Popup is open for index " + index); |
michael@0 | 151 | is(total, gPopup.itemCount, |
michael@0 | 152 | "Correct total suggestions for index " + index); |
michael@0 | 153 | is(current, gPopup.selectedIndex, |
michael@0 | 154 | "Correct index is selected for index " + index); |
michael@0 | 155 | if (inserted) { |
michael@0 | 156 | let { preLabel, label, text } = gPopup.getItemAtIndex(current); |
michael@0 | 157 | let { line, ch } = gEditor.getCursor(); |
michael@0 | 158 | let lineText = gEditor.getText(line); |
michael@0 | 159 | is(lineText.substring(ch - text.length, ch), text, |
michael@0 | 160 | "Current suggestion from the popup is inserted into the editor."); |
michael@0 | 161 | } |
michael@0 | 162 | } |
michael@0 | 163 | else { |
michael@0 | 164 | ok(!gPopup.isOpen, "Popup is closed for index " + index); |
michael@0 | 165 | if (inserted) { |
michael@0 | 166 | let { preLabel, label, text } = gPopup.getItemAtIndex(current); |
michael@0 | 167 | let { line, ch } = gEditor.getCursor(); |
michael@0 | 168 | let lineText = gEditor.getText(line); |
michael@0 | 169 | is(lineText.substring(ch - text.length, ch), text, |
michael@0 | 170 | "Current suggestion from the popup is inserted into the editor."); |
michael@0 | 171 | } |
michael@0 | 172 | } |
michael@0 | 173 | index++; |
michael@0 | 174 | testState(); |
michael@0 | 175 | }); |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | function testAutocompletionDisabled() { |
michael@0 | 179 | gBrowser.removeCurrentTab(); |
michael@0 | 180 | |
michael@0 | 181 | index = 0; |
michael@0 | 182 | info("Starting test to check if autocompletion is disabled correctly.") |
michael@0 | 183 | Services.prefs.setBoolPref(AUTOCOMPLETION_PREF, false); |
michael@0 | 184 | |
michael@0 | 185 | addTabAndOpenStyleEditors(1, testEditorAddedDisabled); |
michael@0 | 186 | |
michael@0 | 187 | content.location = TESTCASE_URI; |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | function testEditorAddedDisabled(panel) { |
michael@0 | 191 | info("Editor added, getting the source editor and starting tests"); |
michael@0 | 192 | panel.UI.editors[0].getSourceEditor().then(editor => { |
michael@0 | 193 | ok(!editor.sourceEditor.getAutocompletionPopup, |
michael@0 | 194 | "Autocompletion popup does not exist"); |
michael@0 | 195 | cleanup(); |
michael@0 | 196 | }); |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | function cleanup() { |
michael@0 | 200 | Services.prefs.clearUserPref(AUTOCOMPLETION_PREF); |
michael@0 | 201 | gEditor = null; |
michael@0 | 202 | gPopup = null; |
michael@0 | 203 | finish(); |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | /** |
michael@0 | 207 | * Returns a list of all property names and a map of property name vs possible |
michael@0 | 208 | * CSS values provided by the Gecko engine. |
michael@0 | 209 | * |
michael@0 | 210 | * @return {Object} An object with following properties: |
michael@0 | 211 | * - CSSProperties {Array} Array of string containing all the possible |
michael@0 | 212 | * CSS property names. |
michael@0 | 213 | * - CSSValues {Object|Map} A map where key is the property name and |
michael@0 | 214 | * value is an array of string containing all the possible |
michael@0 | 215 | * CSS values the property can have. |
michael@0 | 216 | */ |
michael@0 | 217 | function getCSSKeywords() { |
michael@0 | 218 | let domUtils = Cc["@mozilla.org/inspector/dom-utils;1"] |
michael@0 | 219 | .getService(Ci.inIDOMUtils); |
michael@0 | 220 | let props = {}; |
michael@0 | 221 | let propNames = domUtils.getCSSPropertyNames(domUtils.INCLUDE_ALIASES); |
michael@0 | 222 | propNames.forEach(prop => { |
michael@0 | 223 | props[prop] = domUtils.getCSSValuesForProperty(prop).sort(); |
michael@0 | 224 | }); |
michael@0 | 225 | return { |
michael@0 | 226 | CSSValues: props, |
michael@0 | 227 | CSSProperties: propNames.sort() |
michael@0 | 228 | }; |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | /** |
michael@0 | 232 | * Returns the number of expected suggestions for the given property and value. |
michael@0 | 233 | * If the value is not null, returns the number of values starting with `value`. |
michael@0 | 234 | * Returns the number of properties starting with `property` otherwise. |
michael@0 | 235 | */ |
michael@0 | 236 | function getSuggestionNumberFor(property, value) { |
michael@0 | 237 | if (value == null) { |
michael@0 | 238 | return CSSProperties.filter(prop => prop.startsWith(property)) |
michael@0 | 239 | .slice(0, MAX_SUGGESTIONS).length; |
michael@0 | 240 | } |
michael@0 | 241 | return CSSValues[property].filter(val => val.startsWith(value)) |
michael@0 | 242 | .slice(0, MAX_SUGGESTIONS).length; |
michael@0 | 243 | } |