browser_rules_completion-new-property_01.js (3518B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 // Tests that CSS property names are autocompleted and cycled correctly when 7 // creating a new property in the rule view. 8 9 // format : 10 // [ 11 // what key to press, 12 // expected input box value after keypress, 13 // is the popup open, 14 // is a suggestion selected in the popup, 15 // ] 16 const OPEN = true, 17 SELECTED = true; 18 var testData = [ 19 ["d", "display", OPEN, SELECTED], 20 ["VK_DOWN", "dominant-baseline", OPEN, SELECTED], 21 ["VK_DOWN", "d", OPEN, SELECTED], 22 ["VK_DOWN", "direction", OPEN, SELECTED], 23 ["VK_DOWN", "display", OPEN, SELECTED], 24 ["VK_UP", "direction", OPEN, SELECTED], 25 ["VK_UP", "d", OPEN, SELECTED], 26 ["VK_UP", "dominant-baseline", OPEN, SELECTED], 27 ["VK_UP", "display", OPEN, SELECTED], 28 ["VK_BACK_SPACE", "d", !OPEN, !SELECTED], 29 ["i", "display", OPEN, SELECTED], 30 ["s", "display", !OPEN, !SELECTED], 31 ["VK_BACK_SPACE", "dis", !OPEN, !SELECTED], 32 ["VK_BACK_SPACE", "di", !OPEN, !SELECTED], 33 ["VK_BACK_SPACE", "d", !OPEN, !SELECTED], 34 ["VK_BACK_SPACE", "", !OPEN, !SELECTED], 35 ["f", "font-size", OPEN, SELECTED], 36 ["i", "filter", OPEN, SELECTED], 37 ["VK_ESCAPE", null, !OPEN, !SELECTED], 38 ]; 39 40 const TEST_URI = "<h1 style='border: 1px solid red'>Header</h1>"; 41 42 add_task(async function () { 43 await addTab("data:text/html;charset=utf-8," + encodeURIComponent(TEST_URI)); 44 const { toolbox, inspector, view } = await openRuleView(); 45 46 info("Test autocompletion after 1st page load"); 47 await runAutocompletionTest(toolbox, inspector, view); 48 49 info("Test autocompletion after page navigation"); 50 await reloadBrowser(); 51 await runAutocompletionTest(toolbox, inspector, view); 52 }); 53 54 async function runAutocompletionTest(toolbox, inspector, view) { 55 info("Selecting the test node"); 56 await selectNode("h1", inspector); 57 58 info("Focusing the css property editable field"); 59 const ruleEditor = getRuleViewRuleEditor(view, 0); 60 const editor = await focusNewRuleViewProperty(ruleEditor); 61 62 info("Starting to test for css property completion"); 63 for (let i = 0; i < testData.length; i++) { 64 if (!testData[i].length) { 65 continue; 66 } 67 await testCompletion(testData[i], editor, view); 68 } 69 } 70 71 async function testCompletion( 72 [key, completion, open, isSelected], 73 editor, 74 view 75 ) { 76 info("Pressing key " + key); 77 info("Expecting " + completion); 78 info("Is popup opened: " + open); 79 info("Is item selected: " + isSelected); 80 81 let onSuggest; 82 83 if (/(right|back_space|escape)/gi.test(key)) { 84 info("Adding event listener for right|back_space|escape keys"); 85 onSuggest = once(editor.input, "keypress"); 86 } else { 87 info("Waiting for after-suggest event on the editor"); 88 onSuggest = editor.once("after-suggest"); 89 } 90 91 // Also listening for popup opened/closed events if needed. 92 const popupEvent = open ? "popup-opened" : "popup-closed"; 93 const onPopupEvent = 94 editor.popup.isOpen !== open ? once(editor.popup, popupEvent) : null; 95 96 info("Synthesizing key " + key); 97 EventUtils.synthesizeKey(key, {}, view.styleWindow); 98 99 await onSuggest; 100 await onPopupEvent; 101 102 info("Checking the state"); 103 if (completion !== null) { 104 is(editor.input.value, completion, "Correct value is autocompleted"); 105 } 106 if (!open) { 107 ok(!(editor.popup && editor.popup.isOpen), "Popup is closed"); 108 } else { 109 ok(editor.popup.isOpen, "Popup is open"); 110 is(editor.popup.selectedIndex !== -1, isSelected, "An item is selected"); 111 } 112 }