BaseGridWidgetKeyboardHandler implements KeyDownHandler { @Override public void onKeyDown(final KeyDownEvent event) { final GridWidget selectedGridWidget = getSelectedGridWidget(); if (selectedGridWidget == null) { return; } final KeyboardOperation operation = getOperation(event); if (operation == null) { return; } if (!operation.isExecutable(selectedGridWidget)) { return; } flushDOMElements(selectedGridWidget); final boolean redraw = operation.perform(selectedGridWidget, event.isShiftKeyDown(), event.isControlKeyDown()); event.preventDefault(); event.stopPropagation(); if (redraw) { gridLayer.draw(); } } BaseGridWidgetKeyboardHandler(final GridLayer gridLayer); void addOperation(final KeyboardOperation... operations); @Override void onKeyDown(final KeyDownEvent event);  }
@Test public void testDestroyResourcesOnKeyDown() { when(layer.getGridWidgets()).thenReturn(gridWidgets); when(gridWidget1.isSelected()).thenReturn(true); when(event.getNativeKeyCode()).thenReturn(KeyCodes.KEY_RIGHT); final StringDOMElementColumn columnWithAdditionalDomElements = mock(StringDOMElementColumn.class); gridWidget1Data.appendColumn(columnWithAdditionalDomElements); handler.onKeyDown(event); verify(columnWithAdditionalDomElements).destroyResources(); }
@Test public void testDestroyResourcesAndFlushOnKeyDown() { when(layer.getGridWidgets()).thenReturn(gridWidgets); when(gridWidget1.isSelected()).thenReturn(true); when(event.getNativeKeyCode()).thenReturn(KeyCodes.KEY_RIGHT); final StringDOMElementSingletonColumn columnWithAdditionalDomElements = mock(StringDOMElementSingletonColumn.class); gridWidget1Data.appendColumn(columnWithAdditionalDomElements); handler.onKeyDown(event); verify(columnWithAdditionalDomElements).flush(); verify(columnWithAdditionalDomElements).destroyResources(); }
@Test public void noSelectedDecisionTable() { handler.onKeyDown(event); verifyNoOperationsInvoked(); }
@Test public void unhandledKeysInvokeNoAction() { when(layer.getGridWidgets()).thenReturn(gridWidgets); when(gridWidget1.isSelected()).thenReturn(true); when(event.getNativeKeyCode()).thenReturn(KeyCodes.KEY_A); handler.onKeyDown(event); verifyNoOperationsInvoked(); }
@Test public void deleteKeyClearsCells() { when(layer.getGridWidgets()).thenReturn(gridWidgets); when(gridWidget1.isSelected()).thenReturn(true); when(event.getNativeKeyCode()).thenReturn(KeyCodes.KEY_DELETE); gridWidget1Data.setCellValue(0, 0, new BaseGridCellValue<>("hello")); assertEquals("hello", gridWidget1Data.getCell(0, 0).getValue().getValue()); gridWidget1Data.selectCell(0, 0); handler.onKeyDown(event); verify(keyboardOperationClearCell, times(1)).perform(eq(gridWidget1), eq(false), eq(false)); assertNull(gridWidget1Data.getCell(0, 0)); verify(layer, times(1)).draw(); }
@Test public void enterKeyEditsCells() { when(layer.getGridWidgets()).thenReturn(gridWidgets); when(gridWidget1.isSelected()).thenReturn(true); when(event.getNativeKeyCode()).thenReturn(KeyCodes.KEY_ENTER); gridWidget1Data.selectCell(0, 0); handler.onKeyDown(event); verify(gridWidget1, times(1)).startEditingCell(eq(0), eq(0)); verify(layer, never()).draw(); verify(keyboardOperationEditCell, times(1)).perform(eq(gridWidget1), eq(false), eq(false)); }
@Test public void homeKeyMovesToTopLeft() { when(layer.getGridWidgets()).thenReturn(gridWidgets); when(gridWidget1.isSelected()).thenReturn(true); when(gridWidget1.selectCell(eq(0), eq(0), any(Boolean.class), any(Boolean.class))).thenReturn(true); when(event.getNativeKeyCode()).thenReturn(KeyCodes.KEY_HOME); handler.onKeyDown(event); verify(gridWidget1, times(1)).selectCell(eq(0), eq(0), eq(false), eq(false)); verify(layer, times(1)).draw(); verify(keyboardOperationSelectTopLeftCell, times(1)).perform(eq(gridWidget1), eq(false), eq(false)); }
@Test public void endKeyMovesToBottomRight() { when(layer.getGridWidgets()).thenReturn(gridWidgets); when(gridWidget1.isSelected()).thenReturn(true); when(gridWidget1.selectCell(eq(1), eq(1), any(Boolean.class), any(Boolean.class))).thenReturn(true); when(event.getNativeKeyCode()).thenReturn(KeyCodes.KEY_END); handler.onKeyDown(event); verify(gridWidget1, times(1)).selectCell(eq(1), eq(1), eq(false), eq(false)); verify(layer, times(1)).draw(); verify(keyboardOperationSelectBottomRightCell, times(1)).perform(eq(gridWidget1), eq(false), eq(false)); }
@Test public void leftCursorWithoutShiftKeyMovesLeftOneCell() { when(layer.getGridWidgets()).thenReturn(gridWidgets); when(gridWidget1.isSelected()).thenReturn(true); when(gridWidget1.selectCell(eq(0), eq(0), any(Boolean.class), any(Boolean.class))).thenReturn(true); when(event.getNativeKeyCode()).thenReturn(KeyCodes.KEY_LEFT); gridWidget1.getModel().selectCell(0, 1); handler.onKeyDown(event); verify(layer, times(1)).draw(); verify(keyboardOperationMoveLeft, times(1)).perform(eq(gridWidget1), eq(false), eq(false)); final List<GridData.SelectedCell> selectedCells = gridWidget1Data.getSelectedCells(); assertEquals(1, selectedCells.size()); assertEquals(0, selectedCells.get(0).getRowIndex()); assertEquals(0, selectedCells.get(0).getColumnIndex()); }
@Test public void leftCursorWithShiftKeyExtendsRangeLeftOneCell() { when(layer.getGridWidgets()).thenReturn(gridWidgets); when(gridWidget1.isSelected()).thenReturn(true); when(gridWidget1.selectCell(eq(0), eq(0), any(Boolean.class), any(Boolean.class))).thenReturn(true); when(event.getNativeKeyCode()).thenReturn(KeyCodes.KEY_LEFT); when(event.isShiftKeyDown()).thenReturn(true); gridWidget1.getModel().selectCell(0, 1); handler.onKeyDown(event); verify(layer, times(1)).draw(); verify(keyboardOperationMoveLeft, times(1)).perform(eq(gridWidget1), eq(true), eq(false)); final List<GridData.SelectedCell> selectedCells = gridWidget1Data.getSelectedCells(); assertEquals(2, selectedCells.size()); assertEquals(0, selectedCells.get(0).getRowIndex()); assertEquals(1, selectedCells.get(0).getColumnIndex()); assertEquals(0, selectedCells.get(1).getRowIndex()); assertEquals(0, selectedCells.get(1).getColumnIndex()); }
@Test public void rightCursorWithoutShiftKeyMovesRightOneCell() { when(layer.getGridWidgets()).thenReturn(gridWidgets); when(gridWidget1.isSelected()).thenReturn(true); when(gridWidget1.selectCell(eq(0), eq(1), any(Boolean.class), any(Boolean.class))).thenReturn(true); when(event.getNativeKeyCode()).thenReturn(KeyCodes.KEY_RIGHT); gridWidget1.getModel().selectCell(0, 0); handler.onKeyDown(event); verify(layer, times(1)).draw(); verify(keyboardOperationMoveRight, times(1)).perform(eq(gridWidget1), eq(false), eq(false)); final List<GridData.SelectedCell> selectedCells = gridWidget1Data.getSelectedCells(); assertEquals(1, selectedCells.size()); assertEquals(0, selectedCells.get(0).getRowIndex()); assertEquals(1, selectedCells.get(0).getColumnIndex()); }
@Test public void rightCursorWithShiftKeyExtendsRangeRightOneCell() { when(layer.getGridWidgets()).thenReturn(gridWidgets); when(gridWidget1.isSelected()).thenReturn(true); when(gridWidget1.selectCell(eq(0), eq(1), any(Boolean.class), any(Boolean.class))).thenReturn(true); when(event.getNativeKeyCode()).thenReturn(KeyCodes.KEY_RIGHT); when(event.isShiftKeyDown()).thenReturn(true); gridWidget1.getModel().selectCell(0, 0); handler.onKeyDown(event); verify(layer, times(1)).draw(); verify(keyboardOperationMoveRight, times(1)).perform(eq(gridWidget1), eq(true), eq(false)); final List<GridData.SelectedCell> selectedCells = gridWidget1Data.getSelectedCells(); assertEquals(2, selectedCells.size()); assertEquals(0, selectedCells.get(0).getRowIndex()); assertEquals(0, selectedCells.get(0).getColumnIndex()); assertEquals(0, selectedCells.get(1).getRowIndex()); assertEquals(1, selectedCells.get(1).getColumnIndex()); }
@Test public void upCursorWithoutShiftKeyMovesUpOneCell() { when(layer.getGridWidgets()).thenReturn(gridWidgets); when(gridWidget1.isSelected()).thenReturn(true); when(gridWidget1.selectCell(eq(0), eq(0), any(Boolean.class), any(Boolean.class))).thenReturn(true); when(event.getNativeKeyCode()).thenReturn(KeyCodes.KEY_UP); gridWidget1.getModel().selectCell(1, 0); handler.onKeyDown(event); verify(layer, times(1)).draw(); verify(keyboardOperationMoveUp, times(1)).perform(eq(gridWidget1), eq(false), eq(false)); final List<GridData.SelectedCell> selectedCells = gridWidget1Data.getSelectedCells(); assertEquals(1, selectedCells.size()); assertEquals(0, selectedCells.get(0).getRowIndex()); assertEquals(0, selectedCells.get(0).getColumnIndex()); }
@Test public void upCursorWithShiftKeyExtendsRangeUpOneCell() { when(layer.getGridWidgets()).thenReturn(gridWidgets); when(gridWidget1.isSelected()).thenReturn(true); when(gridWidget1.selectCell(eq(0), eq(0), any(Boolean.class), any(Boolean.class))).thenReturn(true); when(event.getNativeKeyCode()).thenReturn(KeyCodes.KEY_UP); when(event.isShiftKeyDown()).thenReturn(true); gridWidget1.getModel().selectCell(1, 0); handler.onKeyDown(event); verify(layer, times(1)).draw(); verify(keyboardOperationMoveUp, times(1)).perform(eq(gridWidget1), eq(true), eq(false)); final List<GridData.SelectedCell> selectedCells = gridWidget1Data.getSelectedCells(); assertEquals(2, selectedCells.size()); assertEquals(1, selectedCells.get(0).getRowIndex()); assertEquals(0, selectedCells.get(0).getColumnIndex()); assertEquals(0, selectedCells.get(1).getRowIndex()); assertEquals(0, selectedCells.get(1).getColumnIndex()); }
@Test public void downCursorWithoutShiftKeyMovesDownOneCell() { when(layer.getGridWidgets()).thenReturn(gridWidgets); when(gridWidget1.isSelected()).thenReturn(true); when(gridWidget1.selectCell(eq(1), eq(0), any(Boolean.class), any(Boolean.class))).thenReturn(true); when(event.getNativeKeyCode()).thenReturn(KeyCodes.KEY_DOWN); gridWidget1.getModel().selectCell(0, 0); handler.onKeyDown(event); verify(layer, times(1)).draw(); verify(keyboardOperationMoveDown, times(1)).perform(eq(gridWidget1), eq(false), eq(false)); final List<GridData.SelectedCell> selectedCells = gridWidget1Data.getSelectedCells(); assertEquals(1, selectedCells.size()); assertEquals(1, selectedCells.get(0).getRowIndex()); assertEquals(0, selectedCells.get(0).getColumnIndex()); }
@Test public void downCursorWithShiftKeyExtendsRangeDownOneCell() { when(layer.getGridWidgets()).thenReturn(gridWidgets); when(gridWidget1.isSelected()).thenReturn(true); when(gridWidget1.selectCell(eq(1), eq(0), any(Boolean.class), any(Boolean.class))).thenReturn(true); when(event.getNativeKeyCode()).thenReturn(KeyCodes.KEY_DOWN); when(event.isShiftKeyDown()).thenReturn(true); gridWidget1.getModel().selectCell(0, 0); handler.onKeyDown(event); verify(layer, times(1)).draw(); verify(keyboardOperationMoveDown, times(1)).perform(eq(gridWidget1), eq(true), eq(false)); final List<GridData.SelectedCell> selectedCells = gridWidget1Data.getSelectedCells(); assertEquals(2, selectedCells.size()); assertEquals(0, selectedCells.get(0).getRowIndex()); assertEquals(0, selectedCells.get(0).getColumnIndex()); assertEquals(1, selectedCells.get(1).getRowIndex()); assertEquals(0, selectedCells.get(1).getColumnIndex()); }