|
21 | 21 |
|
22 | 22 | import static org.hamcrest.Matchers.is; |
23 | 23 | import static org.hamcrest.Matchers.not; |
| 24 | +import static org.hamcrest.Matchers.notNullValue; |
24 | 25 | import static org.junit.Assert.assertThat; |
25 | 26 | import com.vaadin.testbench.TestBenchElement; |
26 | 27 | import org.hamcrest.Description; |
@@ -85,4 +86,109 @@ public void writeText() throws InterruptedException { |
85 | 86 | assertThat(term.currentLine(), is("WORLD")); |
86 | 87 | assertThat(term.cursorPosition(), is(pos.advance(0, 1).plus(5, 0))); |
87 | 88 | } |
| 89 | + |
| 90 | + private Integer getKeyCount() { |
| 91 | + return ((Long)getCommandExecutor().executeScript("return keyCount")).intValue(); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void customKeyHandlerLowLevel() throws InterruptedException { |
| 96 | + XTermElement term = $(XTermElement.class).first(); |
| 97 | + term.executeScript("window.keyCount=0"); |
| 98 | + |
| 99 | + // register an event handler |
| 100 | + term.executeScript( |
| 101 | + "r1=this.customKeyEventHandlers.register(ev=> ev.key=='E', ev=>++keyCount)"); |
| 102 | + assertThat(getKeyCount(), is(0)); |
| 103 | + |
| 104 | + // fire it |
| 105 | + term.sendKeys("E"); |
| 106 | + assertThat(term.currentLine(), is("E")); |
| 107 | + assertThat(getKeyCount(), is(1)); |
| 108 | + |
| 109 | + // register another event handler for the same key |
| 110 | + term.executeScript( |
| 111 | + "r2=this.customKeyEventHandlers.register(ev=> ev.key=='E', ev=>++keyCount)"); |
| 112 | + |
| 113 | + // fire it: increment is performed twice (by each handler) |
| 114 | + term.sendKeys("E"); |
| 115 | + assertThat(term.currentLine(), is("EE")); |
| 116 | + assertThat(getKeyCount(), is(3)); |
| 117 | + |
| 118 | + // deregister |
| 119 | + term.executeScript("r1.dispose()"); |
| 120 | + term.sendKeys("E"); |
| 121 | + assertThat(getKeyCount(), is(4)); |
| 122 | + |
| 123 | + term.executeScript("r2.dispose()"); |
| 124 | + term.sendKeys("E"); |
| 125 | + assertThat(getKeyCount(), is(4)); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void customKeyHandlerRegistrationOrder() throws InterruptedException { |
| 130 | + // assert that custom key handlers are processed in registration order |
| 131 | + XTermElement term = $(XTermElement.class).first(); |
| 132 | + term.executeScript("window.keyCount=0"); |
| 133 | + |
| 134 | + term.executeScript( |
| 135 | + "r1=this.customKeyEventHandlers.register(ev=> ev.key=='E', ev=>keyCount=keyCount*10+1)"); |
| 136 | + term.executeScript( |
| 137 | + "r2=this.customKeyEventHandlers.register(ev=> ev.key=='E', ev=>keyCount=keyCount*10+2)"); |
| 138 | + term.sendKeys("E"); |
| 139 | + assertThat(getKeyCount(), is(12)); |
| 140 | + term.executeScript("r1.dispose()"); |
| 141 | + term.executeScript("r2.dispose()"); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void customKeyHandlerStopImmediatePropagation() throws InterruptedException { |
| 146 | + // since custom key handlers are processed in registration order |
| 147 | + // then r2 will prevent the processing of r3 |
| 148 | + XTermElement term = $(XTermElement.class).first(); |
| 149 | + term.executeScript("window.keyCount=0"); |
| 150 | + |
| 151 | + term.executeScript( |
| 152 | + "r1=this.customKeyEventHandlers.register(ev=> ev.key=='E', ev=>keyCount=keyCount*10+1)"); |
| 153 | + term.executeScript( |
| 154 | + "r2=this.customKeyEventHandlers.register(ev=> ev.key=='E', ev=>ev.stopImmediatePropagation())"); |
| 155 | + term.executeScript( |
| 156 | + "r3=this.customKeyEventHandlers.register(ev=> ev.key=='E', ev=>keyCount=keyCount*10+3)"); |
| 157 | + term.sendKeys("E"); |
| 158 | + assertThat(getKeyCount(), is(1)); |
| 159 | + term.executeScript("r1.dispose()"); |
| 160 | + term.executeScript("r2.dispose()"); |
| 161 | + term.executeScript("r3.dispose()"); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void customKeyHandlerHighLevel() throws InterruptedException { |
| 166 | + XTermElement term = $(XTermElement.class).first(); |
| 167 | + term.executeScript("window.keyCount=0"); |
| 168 | + |
| 169 | + // register interest on CustomKey event |
| 170 | + Long id1 = (Long) term.executeScript("return this.registerCustomKeyListener({key:'E'})"); |
| 171 | + term.executeScript("this.addEventListener('CustomKey', ()=>++keyCount)"); |
| 172 | + assertThat(id1, is(notNullValue())); |
| 173 | + term.sendKeys("E"); |
| 174 | + assertThat(getKeyCount(), is(1)); |
| 175 | + |
| 176 | + // register interest again on CustomKey event for the same key |
| 177 | + // increment is performed by the CustomKey listener, which is called once |
| 178 | + Long id2 = (Long) term.executeScript("return this.registerCustomKeyListener({key:'E'})"); |
| 179 | + assertThat(id2, is(notNullValue())); |
| 180 | + assertThat(id2, is(not(id1))); |
| 181 | + term.sendKeys("E"); |
| 182 | + assertThat(getKeyCount(), is(2)); |
| 183 | + |
| 184 | + // deregister |
| 185 | + term.executeScript("this.customKeyEventHandlers.remove(arguments[0])", id1); |
| 186 | + term.sendKeys("E"); |
| 187 | + assertThat(getKeyCount(), is(3)); |
| 188 | + |
| 189 | + term.executeScript("this.customKeyEventHandlers.remove(arguments[0])", id2); |
| 190 | + term.sendKeys("E"); |
| 191 | + assertThat(getKeyCount(), is(3)); |
| 192 | + } |
| 193 | + |
88 | 194 | } |
0 commit comments