Skip to content

Overriding default KeyEvent behavior runs default and then overriding handler #707

@JordanMartinez

Description

@JordanMartinez

Originally reported in #699

Expected Behavior

Overriding the default KeyEvent behavior (e.g. when a user presses the TAB character, replace the selection with 4 spaces instead of 1 tab character) with a different handler should only run the overriding handler:

Actual Behavior

The default behavior's handler is run, followed by the overriding behavior's handler. Using the example above, the selection is replaced with 1 tab character and then four spaces are inserted after the tab character.

Reproducible Test

import javafx.scene.input.KeyCode;
import org.fxmisc.richtext.InlineCssTextAreaAppTest;
import org.fxmisc.wellbehaved.event.EventPattern;
import org.fxmisc.wellbehaved.event.InputMap;
import org.fxmisc.wellbehaved.event.Nodes;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import org.testfx.api.FxAssert;
import org.testfx.util.DebugUtils;

public class KeyboardOverrides extends InlineCssTextAreaAppTest {

    @Test
    public void testOverrideTabKeyBehavior() {
        String spaces = "    ";
        interact(() -> {
            Nodes.addInputMap(area,
                    InputMap.consume(
                            EventPattern.keyTyped()
                                // factor out possibility of shift/control/alt being pressed
                                .onlyIf(e -> e.getCharacter().equals("\t")),
                            e -> {
                                area.replaceSelection(spaces);
                                // insure event truly is consumed
                                e.consume();
                            }));
        });

        type(KeyCode.TAB);

        String tabCharReplacement = "#";
        String spaceCharReplacment = "!";

        String actual = area.getText()
                .replace("\t", tabCharReplacement)
                .replace(" ", spaceCharReplacment);
        String expected = spaces
                .replace(" ", spaceCharReplacment);

        FxAssert.verifyThat(expected, CoreMatchers.equalTo(actual),
                DebugUtils.informedErrorMessage("Context", false, true, this, true, true));
    }
}

This outputs the following error message:

java.lang.AssertionError: 
Expected: "#!!!!"
     but: was "!!!!"

   Context
      Keys pressed at test failure:
      Mouse Buttons pressed at test failure:
      Fired events since test began:
         WindowEvent [source = javafx.stage.Stage@366e2eef, target = javafx.stage.Stage@366e2eef, eventType = WINDOW_SHOWING, consumed = false]
         WindowEvent [source = javafx.stage.Stage@366e2eef, target = javafx.stage.Stage@366e2eef, eventType = WINDOW_SHOWN, consumed = false]
         MouseEvent [source = javafx.stage.Stage@366e2eef, target = javafx.scene.Scene@6df97b55, eventType = MOUSE_ENTERED_TARGET, consumed = false, x = 318.0, y = 64.0, z = 0.0, button = NONE, pickResult = PickResult [node = InlineCssTextArea@7081a20e[styleClass=root styled-text-area], point = Point3D [x = 318.0, y = 64.0, z = 0.0], distance = 746.4101615137755]
         MouseEvent [source = javafx.stage.Stage@366e2eef, target = InlineCssTextArea@7081a20e[styleClass=root styled-text-area], eventType = MOUSE_ENTERED_TARGET, consumed = false, x = 318.0, y = 64.0, z = 0.0, button = NONE, pickResult = PickResult [node = InlineCssTextArea@7081a20e[styleClass=root styled-text-area], point = Point3D [x = 318.0, y = 64.0, z = 0.0], distance = 746.4101615137755]
         MouseEvent [source = javafx.stage.Stage@366e2eef, target = VirtualFlow@6ffe1684[styleClass=virtual-flow], eventType = MOUSE_ENTERED_TARGET, consumed = false, x = 318.0, y = 64.0, z = 0.0, button = NONE, pickResult = PickResult [node = Navigator@42e115e7, point = Point3D [x = 318.0, y = 64.0, z = 0.0], distance = 746.4101615137755]
         MouseEvent [source = javafx.stage.Stage@366e2eef, target = Navigator@42e115e7, eventType = MOUSE_ENTERED_TARGET, consumed = false, x = 318.0, y = 64.0, z = 0.0, button = NONE, pickResult = PickResult [node = Navigator@42e115e7, point = Point3D [x = 318.0, y = 64.0, z = 0.0], distance = 746.4101615137755]
         KeyEvent [source = javafx.stage.Stage@366e2eef, target = InlineCssTextArea@7081a20e[styleClass=root styled-text-area], eventType = KEY_PRESSED, consumed = false, character =  , text = 	, code = TAB]
         KeyEvent [source = javafx.stage.Stage@366e2eef, target = InlineCssTextArea@7081a20e[styleClass=root styled-text-area], eventType = KEY_TYPED, consumed = false, character = 	, text = , code = UNDEFINED]
         KeyEvent [source = javafx.stage.Stage@366e2eef, target = InlineCssTextArea@7081a20e[styleClass=root styled-text-area], eventType = KEY_RELEASED, consumed = false, character =  , text = 	, code = TAB]


	at org.testfx.api.FxAssert.verifyThatImpl(FxAssert.java:223)
	at org.testfx.api.FxAssert.verifyThat(FxAssert.java:68)
	at org.fxmisc.richtext.override.KeyboardOverrides.testOverrideTabKeyBehavior(KeyboardOverrides.java:39)

Environment info:

  • RichTextFX Version: 0.8.2
  • Operating System: Linux Mint 18.3
  • Java version:
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

Also reported to not work on Windows 10.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions