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

8231692: Test Infrastructure: enhance KeyEventFirer to inject keyEvents into scene #20

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -78,8 +78,9 @@ public KeyEventFirer(EventTarget target) {
public KeyEventFirer(EventTarget target, Scene scene) {
this.target = target;
this.scene = scene;
if (target == null && scene == null)
if (target == null && scene == null) {
throw new NullPointerException("both target and scene are null");
This conversation was marked as resolved by kleopatra

This comment has been minimized.

Copy link
@aghaisas

aghaisas Nov 7, 2019

Enclose in braces {}

}
}

public void doUpArrowPress(KeyModifier... modifiers) {
@@ -31,7 +31,6 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.Ignore;

import static javafx.scene.input.KeyCode.*;
import static javafx.scene.input.KeyEvent.*;
@@ -40,7 +39,6 @@
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
@@ -49,11 +47,6 @@

/**
* Test of enhanced KeyEventFirer.
* <p>
*
* Most of these tests are meant to document how to use the KeyEventFirer and what
* happens if used incorrectly. The latter are ignored, because the build should pass.
*
*/
public class KeyEventFirerTest {

@@ -62,56 +55,6 @@
private Pane root;
private Stage stage;
private Scene scene;
private ComboBox<String> comboBox;

/**
* Verify failing test from bug report example.
*/
@Ignore
@Test
public void testFireComboEditorFailing() {
showAndFocus(comboBox);
List<KeyEvent> keys = new ArrayList<>();
comboBox.getEditor().addEventFilter(KEY_PRESSED, keys::add);
KeyEventFirer keyboard = new KeyEventFirer(comboBox.getEditor(), scene);
keyboard.doKeyPress(ENTER);
assertEquals("pressed ENTER filter on editor", 1, keys.size());
}

/**
* False-green from bug report example.
*/
@Ignore
@Test
public void testFireComboEditorFalseGreen() {
showAndFocus(comboBox);
List<KeyEvent> keys = new ArrayList<>();
comboBox.getEditor().addEventFilter(KEY_PRESSED, keys::add);
KeyEventFirer keyboard = new KeyEventFirer(comboBox.getEditor());
keyboard.doKeyPress(ENTER);
assertEquals("pressed ENTER filter on editor", 1, keys.size());
fail("false green by firing directly on target which is not focusOwner");
}

/**
* Test that keyEvent is delivered to focused control and nowhere else.
* Here we fire directly onto the scene - and see a different outcome from
* using scene.process: the events are delivered to the scene only, not
* the focused node.
*/
@Ignore
@Test
public void testFireSceneAsTarget() {
showAndFocus(button);
List<KeyEvent> buttonEvents = new ArrayList<>();
button.addEventHandler(KEY_PRESSED, buttonEvents::add);
List<KeyEvent> textFieldEvents = new ArrayList<>();
textField.addEventHandler(KEY_PRESSED, textFieldEvents::add);
KeyEventFirer firer = new KeyEventFirer(scene);
firer.doKeyPress(A);
assertEquals("button must have received the key", 1, buttonEvents.size());
assertEquals("textField must not have received the key", 0, textFieldEvents.size());
}

/**
* Test that keyEvent is delivered to focused control and nowhere else.
@@ -150,19 +93,27 @@ public void testFireViaSceneNullTarget() {
* This simulates a false positive: even though not focused, the textField handlers
* are notified when firing directly. That's possible, but typically not what we want to test!
*/
@Ignore
@Test
public void testFireTargetFalseGreen() {
showAndFocus(button);
List<KeyEvent> buttonEvents = new ArrayList<>();
button.addEventHandler(KEY_PRESSED, buttonEvents::add);
List<KeyEvent> textFieldEvents = new ArrayList<>();
textField.addEventHandler(KEY_PRESSED, textFieldEvents::add);
KeyEventFirer firer = new KeyEventFirer(textField);
firer.doKeyPress(A);
assertEquals("textField must have received the key", 1, textFieldEvents.size());
assertEquals("button must have received the key", 0, buttonEvents.size());
fail("false green by firing directly on target which is not focusOwner");
// firing on a node that is not focusOwner
KeyEventFirer incorrectFirer = new KeyEventFirer(textField);
incorrectFirer.doKeyPress(A);
int falseTextFieldNotification = textFieldEvents.size();
int falseButtonNotification = buttonEvents.size();
assertEquals("false green - textField must have received the key", 1, textFieldEvents.size());
assertEquals("false green - button must not have received the key", 0, buttonEvents.size());
textFieldEvents.clear();
buttonEvents.clear();
// firing on the scene makes a difference
KeyEventFirer correctFirer = new KeyEventFirer(null, scene);
correctFirer.doKeyPress(A);
assertNotEquals(falseTextFieldNotification, textFieldEvents.size());

This comment has been minimized.

Copy link
@aghaisas

aghaisas Nov 8, 2019

assertNotEquals does not compile for me.

This comment has been minimized.

Copy link
@kevinrushforth

kevinrushforth Nov 8, 2019

Member

It looks like assertNotEquals was added in a later version of junit than the one we use, which is still junit 4.8.2. We should upgrade to a newer version at some point, but until then we need to avoid using anything from newer versions of junit.

This comment has been minimized.

Copy link
@kleopatra

kleopatra Nov 8, 2019

Author Contributor

*I-must-not-push-without-commandline-build, *I-must-not-push-without-commandline-build, *I-must-...

sry, guys, for wasting your time - changed

assertNotEquals(falseButtonNotification, buttonEvents.size());
}

@Test (expected= NullPointerException.class)
@@ -181,7 +132,7 @@ public void testSingleParamConstructorNPE() {
*/
@Test
public void testUIState() {
assertEquals(List.of(button, textField, comboBox), root.getChildren());
assertEquals(List.of(button, textField), root.getChildren());
stage.show();
stage.requestFocus();
button.requestFocus();
@@ -201,20 +152,13 @@ private void showAndFocus(Node focused) {

@Before
public void setup() {
// This step is not needed (Just to make sure StubToolkit is loaded into VM)
// @SuppressWarnings("unused")
// Toolkit tk = (StubToolkit)Toolkit.getToolkit();
root = new VBox();
scene = new Scene(root);
stage = new Stage();
stage.setScene(scene);
button = new Button("I'm a button");
textField = new TextField("some text");
// to test the false-green example in the bug report
comboBox = new ComboBox<>();
comboBox.getItems().addAll("Test", "hello", "world");
comboBox.setEditable(true);
root.getChildren().addAll(button, textField, comboBox);
root.getChildren().addAll(button, textField);
}

@After
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.