|
| 1 | +if (typeof process !== "undefined") { |
| 2 | + require("amd-loader"); |
| 3 | + require("../test/mockdom"); |
| 4 | +} |
| 5 | + |
| 6 | +var keys = require('../lib/keys'); |
| 7 | + |
| 8 | +"use strict"; |
| 9 | + |
| 10 | +require("../multi_select"); |
| 11 | +require("../theme/textmate"); |
| 12 | +var Editor = require("../editor").Editor; |
| 13 | +var Mode = require("../mode/java").Mode; |
| 14 | +var VirtualRenderer = require("../virtual_renderer").VirtualRenderer; |
| 15 | +var assert = require("../test/assertions"); |
| 16 | + |
| 17 | +function emit(keyCode) { |
| 18 | + var data = {bubbles: true, keyCode}; |
| 19 | + var event = new KeyboardEvent("keydown", data); |
| 20 | + |
| 21 | + var el = document.activeElement; |
| 22 | + el.dispatchEvent(event); |
| 23 | +} |
| 24 | + |
| 25 | +module.exports = { |
| 26 | + setUp : function(done) { |
| 27 | + this.editor = new Editor(new VirtualRenderer()); |
| 28 | + this.editor.container.style.position = "absolute"; |
| 29 | + this.editor.container.style.height = "500px"; |
| 30 | + this.editor.container.style.width = "500px"; |
| 31 | + this.editor.container.style.left = "50px"; |
| 32 | + this.editor.container.style.top = "10px"; |
| 33 | + document.body.appendChild(this.editor.container); |
| 34 | + done(); |
| 35 | + }, |
| 36 | + "test: keyboard code folding: basic functionality" : function(done) { |
| 37 | + var editor = this.editor; |
| 38 | + var value = "x {" + "\n".repeat(50) + "}\n"; |
| 39 | + value = value.repeat(50); |
| 40 | + editor.session.setMode(new Mode()); |
| 41 | + editor.setValue(value, -1); |
| 42 | + editor.setOption("enableKeyboardAccessibility", true); |
| 43 | + editor.renderer.$loop._flush(); |
| 44 | + |
| 45 | + var lines = editor.renderer.$gutterLayer.$lines; |
| 46 | + var toggler = lines.cells[0].element.children[1]; |
| 47 | + |
| 48 | + // Set focus to the gutter div. |
| 49 | + editor.renderer.$gutter.focus(); |
| 50 | + assert.equal(document.activeElement, editor.renderer.$gutter); |
| 51 | + |
| 52 | + // Focus on the fold widget. |
| 53 | + emit(keys["enter"]); |
| 54 | + |
| 55 | + setTimeout(function() { |
| 56 | + assert.equal(document.activeElement, lines.cells[0].element.childNodes[1]); |
| 57 | + |
| 58 | + // Click the fold widget. |
| 59 | + emit(keys["enter"]); |
| 60 | + |
| 61 | + setTimeout(function() { |
| 62 | + // Check that code is folded. |
| 63 | + editor.renderer.$loop._flush(); |
| 64 | + assert.ok(/ace_closed/.test(toggler.className)); |
| 65 | + assert.equal(lines.cells[1].element.textContent, "52"); |
| 66 | + |
| 67 | + // After escape focus should be back to the gutter. |
| 68 | + emit(keys["escape"]); |
| 69 | + assert.equal(document.activeElement, editor.renderer.$gutter); |
| 70 | + |
| 71 | + done(); |
| 72 | + }, 20); |
| 73 | + }, 20); |
| 74 | + }, |
| 75 | + "test: keyboard code folding: multiple folds" : function(done) { |
| 76 | + var editor = this.editor; |
| 77 | + var value = "\n x {" + "\n".repeat(5) + "}\n"; |
| 78 | + value = value.repeat(50); |
| 79 | + editor.session.setMode(new Mode()); |
| 80 | + editor.setValue(value, -1); |
| 81 | + editor.setOption("enableKeyboardAccessibility", true); |
| 82 | + editor.renderer.$loop._flush(); |
| 83 | + |
| 84 | + var lines = editor.renderer.$gutterLayer.$lines; |
| 85 | + |
| 86 | + // Set focus to the gutter div. |
| 87 | + editor.renderer.$gutter.focus(); |
| 88 | + assert.equal(document.activeElement, editor.renderer.$gutter); |
| 89 | + |
| 90 | + assert.equal(lines.cells[2].element.textContent, "3"); |
| 91 | + |
| 92 | + // Focus on the fold widgets. |
| 93 | + emit(keys["enter"]); |
| 94 | + |
| 95 | + setTimeout(function() { |
| 96 | + assert.equal(document.activeElement, lines.cells[1].element.childNodes[1]); |
| 97 | + |
| 98 | + // Click the first fold widget. |
| 99 | + emit(keys["enter"]); |
| 100 | + |
| 101 | + setTimeout(function() { |
| 102 | + // Check that code is folded. |
| 103 | + editor.renderer.$loop._flush(); |
| 104 | + assert.equal(lines.cells[2].element.textContent, "8"); |
| 105 | + |
| 106 | + // Move to the next fold widget. |
| 107 | + emit(keys["down"]); |
| 108 | + assert.equal(document.activeElement, lines.cells[3].element.childNodes[1]); |
| 109 | + assert.equal(lines.cells[4].element.textContent, "10"); |
| 110 | + |
| 111 | + // Click the fold widget. |
| 112 | + emit(keys["enter"]); |
| 113 | + |
| 114 | + setTimeout(function() { |
| 115 | + // Check that code is folded. |
| 116 | + assert.equal(lines.cells[4].element.textContent, "15"); |
| 117 | + |
| 118 | + // Move back up one fold widget. |
| 119 | + emit(keys["up"]); |
| 120 | + assert.equal(document.activeElement, lines.cells[1].element.childNodes[1]); |
| 121 | + |
| 122 | + done(); |
| 123 | + }, 20); |
| 124 | + }, 20); |
| 125 | + }, 20); |
| 126 | + }, |
| 127 | + "test: keyboard annotation: basic functionality" : function(done) { |
| 128 | + var editor = this.editor; |
| 129 | + var value = "x {" + "\n".repeat(50) + "}\n"; |
| 130 | + value = value.repeat(50); |
| 131 | + editor.session.setMode(new Mode()); |
| 132 | + editor.setOption("enableKeyboardAccessibility", true); |
| 133 | + editor.setValue(value, -1); |
| 134 | + editor.session.setAnnotations([{row: 0, column: 0, text: "error test", type: "error"}]); |
| 135 | + editor.renderer.$loop._flush(); |
| 136 | + |
| 137 | + var lines = editor.renderer.$gutterLayer.$lines; |
| 138 | + |
| 139 | + // Set focus to the gutter div. |
| 140 | + editor.renderer.$gutter.focus(); |
| 141 | + assert.equal(document.activeElement, editor.renderer.$gutter); |
| 142 | + |
| 143 | + // Focus on the annotation. |
| 144 | + emit(keys["enter"]); |
| 145 | + |
| 146 | + setTimeout(function() { |
| 147 | + emit(keys["left"]); |
| 148 | + assert.equal(document.activeElement, lines.cells[0].element.childNodes[2]); |
| 149 | + |
| 150 | + // Click annotation. |
| 151 | + emit(keys["enter"]); |
| 152 | + |
| 153 | + setTimeout(function() { |
| 154 | + // Check annotation is rendered. |
| 155 | + editor.renderer.$loop._flush(); |
| 156 | + var tooltip = editor.container.querySelector(".ace_tooltip"); |
| 157 | + assert.ok(/error test/.test(tooltip.textContent)); |
| 158 | + |
| 159 | + // Press escape to dismiss the tooltip. |
| 160 | + emit(keys["escape"]); |
| 161 | + |
| 162 | + // After escape again focus should be back to the gutter. |
| 163 | + emit(keys["escape"]); |
| 164 | + assert.equal(document.activeElement, editor.renderer.$gutter); |
| 165 | + |
| 166 | + done(); |
| 167 | + }, 20); |
| 168 | + }, 20); |
| 169 | + },"test: keyboard annotation: multiple annotations" : function(done) { |
| 170 | + var editor = this.editor; |
| 171 | + var value = "x {" + "\n".repeat(50) + "}\n"; |
| 172 | + value = value.repeat(50); |
| 173 | + editor.session.setMode(new Mode()); |
| 174 | + editor.setOption("enableKeyboardAccessibility", true); |
| 175 | + editor.setValue(value, -1); |
| 176 | + editor.session.setAnnotations([ |
| 177 | + {row: 1, column: 0, text: "error test", type: "error"}, |
| 178 | + {row: 2, column: 0, text: "warning test", type: "warning"} |
| 179 | + ]); |
| 180 | + editor.renderer.$loop._flush(); |
| 181 | + |
| 182 | + var lines = editor.renderer.$gutterLayer.$lines; |
| 183 | + |
| 184 | + // Set focus to the gutter div. |
| 185 | + editor.renderer.$gutter.focus(); |
| 186 | + assert.equal(document.activeElement, editor.renderer.$gutter); |
| 187 | + |
| 188 | + // Focus on the annotation. |
| 189 | + emit(keys["enter"]); |
| 190 | + |
| 191 | + setTimeout(function() { |
| 192 | + emit(keys["left"]); |
| 193 | + assert.equal(document.activeElement, lines.cells[1].element.childNodes[2]); |
| 194 | + |
| 195 | + // Click annotation. |
| 196 | + emit(keys["enter"]); |
| 197 | + |
| 198 | + setTimeout(function() { |
| 199 | + // Check annotation is rendered. |
| 200 | + editor.renderer.$loop._flush(); |
| 201 | + var tooltip = editor.container.querySelector(".ace_tooltip"); |
| 202 | + assert.ok(/error test/.test(tooltip.textContent)); |
| 203 | + |
| 204 | + // Press escape to dismiss the tooltip. |
| 205 | + emit(keys["escape"]); |
| 206 | + |
| 207 | + // Press down to move to next annotation. |
| 208 | + emit(keys["down"]); |
| 209 | + assert.equal(document.activeElement, lines.cells[2].element.childNodes[2]); |
| 210 | + |
| 211 | + // Click annotation. |
| 212 | + emit(keys["enter"]); |
| 213 | + |
| 214 | + setTimeout(function() { |
| 215 | + // Check annotation is rendered. |
| 216 | + editor.renderer.$loop._flush(); |
| 217 | + var tooltip = editor.container.querySelector(".ace_tooltip"); |
| 218 | + assert.ok(/warning test/.test(tooltip.textContent)); |
| 219 | + |
| 220 | + // Press escape to dismiss the tooltip. |
| 221 | + emit(keys["escape"]); |
| 222 | + |
| 223 | + // Move back up one annotation. |
| 224 | + emit(keys["up"]); |
| 225 | + assert.equal(document.activeElement, lines.cells[1].element.childNodes[2]); |
| 226 | + |
| 227 | + // Move back to the folds, focus should be on the fold on line 1. |
| 228 | + emit(keys["right"]); |
| 229 | + assert.equal(document.activeElement, lines.cells[0].element.childNodes[1]); |
| 230 | + |
| 231 | + done(); |
| 232 | + }, 20); |
| 233 | + }, 20); |
| 234 | + }, 20); |
| 235 | + },"test: keyboard annotation: no folds" : function(done) { |
| 236 | + var editor = this.editor; |
| 237 | + var value = "x\n"; |
| 238 | + value = value.repeat(50); |
| 239 | + editor.session.setMode(new Mode()); |
| 240 | + editor.setOption("enableKeyboardAccessibility", true); |
| 241 | + editor.setValue(value, -1); |
| 242 | + editor.session.setAnnotations([{row: 1, column: 0, text: "error test", type: "error"}]); |
| 243 | + editor.renderer.$loop._flush(); |
| 244 | + |
| 245 | + var lines = editor.renderer.$gutterLayer.$lines; |
| 246 | + |
| 247 | + // Set focus to the gutter div. |
| 248 | + editor.renderer.$gutter.focus(); |
| 249 | + assert.equal(document.activeElement, editor.renderer.$gutter); |
| 250 | + |
| 251 | + // Focus on gutter interaction. |
| 252 | + emit(keys["enter"]); |
| 253 | + |
| 254 | + setTimeout(function() { |
| 255 | + // Focus should be on the annotation directly. |
| 256 | + assert.equal(document.activeElement, lines.cells[1].element.childNodes[2]); |
| 257 | + done(); |
| 258 | + }, 20); |
| 259 | + }, |
| 260 | + |
| 261 | + tearDown : function() { |
| 262 | + this.editor.destroy(); |
| 263 | + document.body.removeChild(this.editor.container); |
| 264 | + } |
| 265 | + |
| 266 | +}; |
| 267 | + |
| 268 | +if (typeof module !== "undefined" && module === require.main) { |
| 269 | + require("asyncjs").test.testcase(module.exports).exec(); |
| 270 | +} |
0 commit comments