Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 1 | function emitKeyboardEvent (element, type, keyCode) { |
| 2 | // event type : keydown, keyup, keypress |
| 3 | // http://stackoverflow.com/questions/596481/simulate-javascript-key-events |
| 4 | // http://stackoverflow.com/questions/961532/firing-a-keyboard-event-in-javascript |
| 5 | var keyboardEvent = document.createEvent("KeyboardEvent"); |
| 6 | var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? |
| 7 | "initKeyboardEvent" : "initKeyEvent"; |
| 8 | |
| 9 | keyboardEvent[initMethod]( |
| 10 | type, |
| 11 | true, // bubbles |
| 12 | true, // cancelable |
| 13 | window, // viewArg: should be window |
| 14 | false, // ctrlKeyArg |
| 15 | false, // altKeyArg |
| 16 | false, // shiftKeyArg |
| 17 | false, // metaKeyArg |
| 18 | keyCode, // keyCodeArg : unsigned long the virtual key code, else 0 |
| 19 | 0 // charCodeArgs : unsigned long the Unicode character |
| 20 | // associated with the depressed key, else 0 |
| 21 | ); |
| 22 | |
| 23 | element.dispatchEvent(keyboardEvent); |
| 24 | }; |
| 25 | |
| 26 | |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame^] | 27 | describe('KorAP.InputField', function () { |
| 28 | var input; |
| 29 | |
| 30 | beforeEach(function () { |
| 31 | input = document.createElement("input"); |
| 32 | input.setAttribute("type", "text"); |
| 33 | input.setAttribute("value", "abcdefghijklmno"); |
| 34 | input.style.position = 'absolute'; |
| 35 | input.style.top = "20px"; |
| 36 | input.style.left = "30px"; |
| 37 | input.focus(); |
| 38 | input.selectionStart = 5; |
| 39 | }); |
| 40 | |
| 41 | afterAll(function () { |
| 42 | try { |
| 43 | // document.getElementsByTagName("body")[0].removeChild(input); |
| 44 | document.getElementsByTagName("body")[0].removeChild( |
| 45 | document.getElementById("searchMirror") |
| 46 | ); |
| 47 | } |
| 48 | catch (e) {}; |
| 49 | }); |
| 50 | |
| 51 | it('should be initializable', function () { |
| 52 | // Supports: context, searchField |
| 53 | var inputField = KorAP.InputField.create(input); |
| 54 | expect(inputField._element).not.toBe(undefined); |
| 55 | }); |
| 56 | |
| 57 | it('should have text', function () { |
| 58 | expect(input.value).toEqual('abcdefghijklmno'); |
| 59 | var inputField = KorAP.InputField.create(input); |
| 60 | |
| 61 | expect(inputField.value()).toEqual("abcdefghijklmno"); |
| 62 | |
| 63 | expect(inputField.element().selectionStart).toEqual(5); |
| 64 | expect(inputField.split()[0]).toEqual("abcde"); |
| 65 | expect(inputField.split()[1]).toEqual("fghijklmno"); |
| 66 | |
| 67 | inputField.insert("xyz"); |
| 68 | expect(inputField.value()).toEqual('abcdexyzfghijklmno'); |
| 69 | expect(inputField.split()[0]).toEqual("abcdexyz"); |
| 70 | expect(inputField.split()[1]).toEqual("fghijklmno"); |
| 71 | }); |
| 72 | |
| 73 | it('should be correctly positioned', function () { |
| 74 | expect(input.value).toEqual('abcdefghijklmno'); |
| 75 | var inputField = KorAP.InputField.create(input); |
| 76 | document.getElementsByTagName("body")[0].appendChild(input); |
| 77 | inputField.reposition(); |
| 78 | expect(inputField.mirror().style.left).toEqual("30px"); |
| 79 | expect(inputField.mirror().style.top.match(/^(\d+)px$/)[1]).toBeGreaterThan(20); |
| 80 | }); |
| 81 | |
| 82 | it('should have a correct context', function () { |
| 83 | expect(input.value).toEqual('abcdefghijklmno'); |
| 84 | var inputField = KorAP.InputField.create(input); |
| 85 | |
| 86 | expect(inputField.value()).toEqual("abcdefghijklmno"); |
| 87 | |
| 88 | expect(inputField.element().selectionStart).toEqual(5); |
| 89 | expect(inputField.split()[0]).toEqual("abcde"); |
| 90 | expect(inputField.context()).toEqual("abcde"); |
| 91 | }); |
| 92 | |
| 93 | /* |
| 94 | it('should be correctly triggerable', function () { |
| 95 | // https://developer.mozilla.org/samples/domref/dispatchEvent.html |
| 96 | var hint = KorAP.Hint.create({ "inputField" : input }); |
| 97 | emitKeyboardEvent(hint.inputField.element, "keypress", 20); |
| 98 | }); |
| 99 | */ |
| 100 | }); |
| 101 | |
| 102 | |
| 103 | describe('KorAP.ContextAnalyzer', function () { |
| 104 | it('should be initializable', function () { |
| 105 | var analyzer = KorAP.ContextAnalyzer.create(")"); |
| 106 | expect(analyzer).toBe(undefined); |
| 107 | |
| 108 | analyzer = KorAP.ContextAnalyzer.create(".+?"); |
| 109 | expect(analyzer).not.toBe(undefined); |
| 110 | |
| 111 | }); |
| 112 | |
| 113 | it('should check correctly', function () { |
| 114 | analyzer = KorAP.ContextAnalyzer.create(KorAP.context); |
| 115 | expect(analyzer.test("cnx/]cnx/c=")).toEqual("cnx/c="); |
| 116 | expect(analyzer.test("cnx/c=")).toEqual("cnx/c="); |
| 117 | expect(analyzer.test("cnx/c=np mate/m=mood:")).toEqual("mate/m=mood:"); |
| 118 | expect(analyzer.test("impcnx/")).toEqual("impcnx/"); |
| 119 | expect(analyzer.test("cnx/c=npcnx/")).toEqual("npcnx/"); |
| 120 | expect(analyzer.test("mate/m=degree:pos corenlp/ne_dewac_175m_600=")) |
| 121 | .toEqual("corenlp/ne_dewac_175m_600="); |
| 122 | }); |
| 123 | }); |
| 124 | |
| 125 | |
| 126 | describe('KorAP.Hint', function () { |
| 127 | KorAP.hintArray = { |
| 128 | "corenlp/" : [ |
| 129 | ["Named Entity", "ne=" , "Combined"], |
| 130 | ["Named Entity", "ne_dewac_175m_600=" , "ne_dewac_175m_600"], |
| 131 | ["Named Entity", "ne_hgc_175m_600=", "ne_hgc_175m_600"] |
| 132 | ] |
| 133 | }; |
| 134 | |
| 135 | beforeEach(function () { |
| 136 | input = document.createElement("input"); |
| 137 | input.setAttribute("type", "text"); |
| 138 | input.setAttribute("value", "abcdefghijklmno"); |
| 139 | input.style.position = 'absolute'; |
| 140 | input.style.top = "20px"; |
| 141 | input.style.left = "30px"; |
| 142 | input.focus(); |
| 143 | input.selectionStart = 5; |
| 144 | }); |
| 145 | |
| 146 | it('should be initializable', function () { |
| 147 | // Supports: context, searchField |
| 148 | var hint = KorAP.Hint.create({ |
| 149 | inputField : input |
| 150 | }); |
| 151 | |
| 152 | expect(hint).toBeTruthy(); |
| 153 | }); |
| 154 | }); |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | xdescribe('KorAP.MenuItem', function () { |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 167 | it('should be initializable', function () { |
| 168 | |
| 169 | expect( |
| 170 | function() { KorAP.MenuItem.create([]) } |
| 171 | ).toThrow(new Error("Missing parameters")); |
| 172 | |
| 173 | expect( |
| 174 | function() { KorAP.MenuItem.create(['CoreNLP']) } |
| 175 | ).toThrow(new Error("Missing parameters")); |
| 176 | |
| 177 | var menuItem = KorAP.MenuItem.create(['CoreNLP', 'corenlp/']); |
| 178 | expect(menuItem.name).toEqual('CoreNLP'); |
| 179 | expect(menuItem.action).toEqual('corenlp/'); |
| 180 | expect(menuItem.desc).toBeUndefined(); |
| 181 | expect(menuItem.lcfield).toEqual(' corenlp'); |
| 182 | |
| 183 | menuItem = KorAP.MenuItem.create(['CoreNLP', 'corenlp/', 'It\'s funny']); |
| 184 | expect(menuItem.name).toEqual('CoreNLP'); |
| 185 | expect(menuItem.action).toEqual('corenlp/'); |
| 186 | expect(menuItem.desc).not.toBeUndefined(); |
| 187 | expect(menuItem.desc).toEqual('It\'s funny'); |
| 188 | expect(menuItem.lcfield).toEqual(' corenlp it\'s funny'); |
| 189 | }); |
| 190 | |
| 191 | it('should have an element', function () { |
| 192 | var menuItem = KorAP.MenuItem.create(['CoreNLP', 'corenlp/']); |
| 193 | expect(menuItem.element).not.toBe(undefined); |
| 194 | expect(menuItem.element.nodeName).toEqual("LI"); |
| 195 | expect(menuItem.element.getAttribute("data-action")).toEqual("corenlp/"); |
| 196 | |
| 197 | var title = menuItem.element.firstChild; |
| 198 | expect(title.nodeName).toEqual("STRONG"); |
| 199 | expect(title.firstChild.nodeType).toEqual(3); |
| 200 | expect(title.firstChild.nodeValue).toEqual("CoreNLP"); |
| 201 | |
| 202 | expect(menuItem.element.childNodes[0]).not.toBe(undefined); |
| 203 | expect(menuItem.element.childNodes[1]).toBe(undefined); |
| 204 | |
| 205 | menuItem = KorAP.MenuItem.create(['CoreNLP', 'corenlp/', 'my DescRiption']); |
| 206 | expect(menuItem.element).not.toBe(undefined); |
| 207 | expect(menuItem.element.nodeName).toEqual("LI"); |
| 208 | expect(menuItem.element.getAttribute("data-action")).toEqual("corenlp/"); |
| 209 | |
| 210 | title = menuItem.element.firstChild; |
| 211 | expect(title.nodeName).toEqual("STRONG"); |
| 212 | expect(title.firstChild.nodeType).toEqual(3); // TextNode |
| 213 | expect(title.firstChild.nodeValue).toEqual("CoreNLP"); |
| 214 | |
| 215 | expect(menuItem.element.childNodes[0]).not.toBe(undefined); |
| 216 | expect(menuItem.element.childNodes[1]).not.toBe(undefined); |
| 217 | |
| 218 | var desc = menuItem.element.lastChild; |
| 219 | expect(desc.nodeName).toEqual("SPAN"); |
| 220 | expect(desc.firstChild.nodeType).toEqual(3); // TextNode |
| 221 | expect(desc.firstChild.nodeValue).toEqual("my DescRiption"); |
| 222 | |
| 223 | expect(menuItem.lcfield).toEqual(' corenlp my description'); |
| 224 | }); |
| 225 | |
| 226 | it('should be activatable and deactivateable by class', function () { |
| 227 | var menuItem = KorAP.MenuItem.create(['CoreNLP', 'corenlp/']); |
| 228 | expect(menuItem.active()).toBe(false); |
| 229 | expect(menuItem.element.getAttribute("class")).toBe(null); |
| 230 | menuItem.active(true); |
| 231 | expect(menuItem.active()).toBe(true); |
| 232 | expect(menuItem.element.getAttribute("class")).toEqual("active"); |
| 233 | menuItem.active(false); // Is active |
| 234 | expect(menuItem.active()).toBe(false); |
| 235 | expect(menuItem.element.getAttribute("class")).toEqual(""); |
| 236 | menuItem.active(true); |
| 237 | expect(menuItem.active()).toBe(true); |
| 238 | expect(menuItem.element.getAttribute("class")).toEqual("active"); |
| 239 | |
| 240 | menuItem = KorAP.MenuItem.create(['CoreNLP', 'corenlp/']); |
| 241 | expect(menuItem.active()).toBe(false); |
| 242 | expect(menuItem.element.getAttribute("class")).toBe(null); |
| 243 | menuItem.active(false); // Is not active |
| 244 | expect(menuItem.active()).toBe(false); |
| 245 | expect(menuItem.element.getAttribute("class")).toBe(null); |
| 246 | }); |
| 247 | |
| 248 | it('should be set to boundary', function () { |
| 249 | var menuItem = KorAP.MenuItem.create(['CoreNLP', 'corenlp/']); |
| 250 | expect(menuItem.active()).toBe(false); |
| 251 | expect(menuItem.element.getAttribute("class")).toBe(null); |
| 252 | |
| 253 | // Set active |
| 254 | menuItem.active(true); |
| 255 | expect(menuItem.active()).toBe(true); |
| 256 | expect(menuItem.noMore()).toBe(false); |
| 257 | expect(menuItem.element.getAttribute("class")).toEqual("active"); |
| 258 | |
| 259 | // Set no more |
| 260 | menuItem.noMore(true); |
| 261 | expect(menuItem.active()).toBe(true); |
| 262 | expect(menuItem.noMore()).toBe(true); |
| 263 | expect(menuItem.element.getAttribute("class")).toEqual("active no-more"); |
| 264 | |
| 265 | // No no more |
| 266 | menuItem.noMore(false); |
| 267 | expect(menuItem.active()).toBe(true); |
| 268 | expect(menuItem.noMore()).toBe(false); |
| 269 | expect(menuItem.element.getAttribute("class")).toEqual("active"); |
| 270 | |
| 271 | |
| 272 | // Set no more, deactivate |
| 273 | menuItem.noMore(true); |
| 274 | menuItem.active(false); |
| 275 | expect(menuItem.active()).toBe(false); |
| 276 | expect(menuItem.noMore()).toBe(true); |
| 277 | expect(menuItem.element.getAttribute("class")).toEqual("no-more"); |
| 278 | |
| 279 | // Set active |
| 280 | menuItem.active(true); |
| 281 | expect(menuItem.active()).toBe(true); |
| 282 | expect(menuItem.noMore()).toBe(true); |
| 283 | expect(menuItem.element.getAttribute("class")).toEqual("no-more active"); |
| 284 | }); |
| 285 | |
| 286 | |
| 287 | it('should be highlightable', function () { |
| 288 | // Highlight in the middle |
| 289 | var menuItem = KorAP.MenuItem.create(['CoreNLP', 'corenlp/']); |
| 290 | menuItem.highlight("ren"); |
| 291 | expect(menuItem.element.innerHTML).toEqual("<strong>Co<em>reN</em>LP</strong>"); |
| 292 | |
| 293 | menuItem.lowlight(); |
| 294 | expect(menuItem.element.innerHTML).toEqual("<strong>CoreNLP</strong>"); |
| 295 | |
| 296 | // Starting highlight |
| 297 | menuItem = KorAP.MenuItem.create(['CoreNLP', 'corenlp/']); |
| 298 | menuItem.highlight("cor"); |
| 299 | expect(menuItem.element.innerHTML).toEqual("<strong><em>Cor</em>eNLP</strong>"); |
| 300 | |
| 301 | menuItem.lowlight(); |
| 302 | expect(menuItem.element.innerHTML).toEqual("<strong>CoreNLP</strong>"); |
| 303 | |
| 304 | // Starting highlight - short |
| 305 | menuItem = KorAP.MenuItem.create(['CoreNLP', 'corenlp/']); |
| 306 | menuItem.highlight("c"); |
| 307 | expect(menuItem.element.innerHTML).toEqual("<strong><em>C</em>oreNLP</strong>"); |
| 308 | |
| 309 | menuItem.lowlight(); |
| 310 | expect(menuItem.element.innerHTML).toEqual("<strong>CoreNLP</strong>"); |
| 311 | |
| 312 | // Highlight at the end |
| 313 | menuItem = KorAP.MenuItem.create(['CoreNLP', 'corenlp/']); |
| 314 | menuItem.highlight("nlp"); |
| 315 | expect(menuItem.element.innerHTML).toEqual("<strong>Core<em>NLP</em></strong>"); |
| 316 | |
| 317 | menuItem.lowlight(); |
| 318 | expect(menuItem.element.innerHTML).toEqual("<strong>CoreNLP</strong>"); |
| 319 | |
| 320 | // Highlight at the end - short |
| 321 | menuItem = KorAP.MenuItem.create(['CoreNLP', 'corenlp/']); |
| 322 | menuItem.highlight("p"); |
| 323 | expect(menuItem.element.innerHTML).toEqual("<strong>CoreNL<em>P</em></strong>"); |
| 324 | |
| 325 | menuItem.lowlight(); |
| 326 | expect(menuItem.element.innerHTML).toEqual("<strong>CoreNLP</strong>"); |
| 327 | |
| 328 | // No highlight |
| 329 | menuItem = KorAP.MenuItem.create(['CoreNLP', 'corenlp/']); |
| 330 | menuItem.highlight("xp"); |
| 331 | expect(menuItem.element.innerHTML).toEqual("<strong>CoreNLP</strong>"); |
| 332 | |
| 333 | menuItem.lowlight(); |
| 334 | expect(menuItem.element.innerHTML).toEqual("<strong>CoreNLP</strong>"); |
| 335 | |
| 336 | // Highlight in the middle - first |
| 337 | menuItem = KorAP.MenuItem.create(['CoreNLP', 'corenlp/', 'This is my Example']); |
| 338 | menuItem.highlight("ren"); |
| 339 | expect(menuItem.element.innerHTML).toEqual("<strong>Co<em>reN</em>LP</strong><span>This is my Example</span>"); |
| 340 | |
| 341 | menuItem.lowlight(); |
| 342 | expect(menuItem.element.innerHTML).toEqual("<strong>CoreNLP</strong><span>This is my Example</span>"); |
| 343 | |
| 344 | // Highlight in the middle - second |
| 345 | menuItem = KorAP.MenuItem.create(['CoreNLP', 'corenlp/', 'This is my Example']); |
| 346 | menuItem.highlight("ampl"); |
| 347 | expect(menuItem.element.innerHTML).toEqual("<strong>CoreNLP</strong><span>This is my Ex<em>ampl</em>e</span>"); |
| 348 | |
| 349 | menuItem.lowlight(); |
| 350 | expect(menuItem.element.innerHTML).toEqual("<strong>CoreNLP</strong><span>This is my Example</span>"); |
| 351 | |
| 352 | // Highlight in the middle - both |
| 353 | menuItem = KorAP.MenuItem.create(['CoreNLP', 'corenlp/', 'This is my Example']); |
| 354 | menuItem.highlight("e"); |
| 355 | expect(menuItem.element.innerHTML).toEqual("<strong>Cor<em>e</em>NLP</strong><span>This is my <em>E</em>xample</span>"); |
| 356 | |
| 357 | menuItem.lowlight(); |
| 358 | expect(menuItem.element.innerHTML).toEqual("<strong>CoreNLP</strong><span>This is my Example</span>"); |
| 359 | |
| 360 | // Highlight in the end - second |
| 361 | menuItem = KorAP.MenuItem.create(['CoreNLP', 'corenlp/', 'This is my Example']); |
| 362 | menuItem.highlight("le"); |
| 363 | expect(menuItem.element.innerHTML).toEqual("<strong>CoreNLP</strong><span>This is my Examp<em>le</em></span>"); |
| 364 | |
| 365 | menuItem.lowlight(); |
| 366 | expect(menuItem.element.innerHTML).toEqual("<strong>CoreNLP</strong><span>This is my Example</span>"); |
| 367 | |
| 368 | // Highlight at the beginning - second |
| 369 | menuItem = KorAP.MenuItem.create(['CoreNLP', 'corenlp/', 'This is my Example']); |
| 370 | menuItem.highlight("this"); |
| 371 | expect(menuItem.element.innerHTML).toEqual("<strong>CoreNLP</strong><span><em>This</em> is my Example</span>"); |
| 372 | |
| 373 | menuItem.lowlight(); |
| 374 | expect(menuItem.element.innerHTML).toEqual("<strong>CoreNLP</strong><span>This is my Example</span>"); |
| 375 | }); |
| 376 | }); |
| 377 | |
| 378 | |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame^] | 379 | describe('KorAP.HintMenu', function () { |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 380 | |
| 381 | var list = [ |
| 382 | ["Constituency", "c=", "Example 1"], |
| 383 | ["Lemma", "l="], |
| 384 | ["Morphology", "m=", "Example 2"], |
| 385 | ["Part-of-Speech", "p="], |
| 386 | ["Syntax", "syn="] |
| 387 | ]; |
| 388 | |
| 389 | |
| 390 | it('should be initializable', function () { |
| 391 | |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame^] | 392 | var menu = KorAP.HintMenu.create("cnx/", list); |
| 393 | expect(menu.context()).toEqual('cnx/'); |
| 394 | expect(menu.element().nodeName).toEqual('UL'); |
| 395 | expect(menu.element().style.opacity).toEqual("0"); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 396 | |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame^] | 397 | menu.limit(8); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 398 | |
| 399 | // view |
| 400 | menu.show(); |
| 401 | |
| 402 | // First element in list |
| 403 | expect(menu.item(0).active()).toBe(true); |
| 404 | expect(menu.item(0).noMore()).toBe(true); |
| 405 | |
| 406 | // Middle element in list |
| 407 | expect(menu.item(2).active()).toBe(false); |
| 408 | expect(menu.item(2).noMore()).toBe(false); |
| 409 | |
| 410 | // Last element in list |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame^] | 411 | expect(menu.item(menu.length() - 1).active()).toBe(false); |
| 412 | expect(menu.item(menu.length() - 1).noMore()).toBe(true); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 413 | }); |
| 414 | }); |