Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
JordanMartinez opened this issue Mar 22, 2018 · 2 comments
Labels

Comments

@JordanMartinez
Copy link
Contributor

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.

@JordanMartinez
Copy link
Contributor Author

AFAIK, the overriding behavior has worked in the past. So, I think this might be a regression that was introduced in the JDK at some point. Still, we should try overriding a number of different handlers in the default behavior of both KeyEvent and MouseEvent types to help narrow the issue down. It might just be an issue in WellBehavedFX.

Also, keep in mind FXMisc/WellBehavedFX#15

@JordanMartinez
Copy link
Contributor Author

See my answer in this comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant