|
25 | 25 |
|
26 | 26 | package test.javafx.scene.control;
|
27 | 27 |
|
| 28 | +import javafx.application.Platform; |
28 | 29 | import javafx.beans.InvalidationListener;
|
29 | 30 | import javafx.beans.Observable;
|
30 | 31 | import javafx.beans.property.BooleanProperty;
|
|
35 | 36 | import javafx.beans.value.ObservableValue;
|
36 | 37 | import javafx.css.CssMetaData;
|
37 | 38 | import javafx.css.StyleableProperty;
|
| 39 | +import javafx.event.Event; |
38 | 40 | import javafx.event.EventHandler;
|
39 | 41 | import javafx.scene.Scene;
|
40 | 42 | import javafx.scene.input.KeyCode;
|
41 | 43 | import javafx.scene.input.KeyEvent;
|
42 | 44 | import javafx.scene.input.Clipboard;
|
43 | 45 | import javafx.scene.input.ClipboardContent;
|
| 46 | +import javafx.scene.layout.VBox; |
44 | 47 | import javafx.scene.text.Font;
|
45 | 48 | import javafx.scene.layout.StackPane;
|
46 | 49 | import javafx.stage.Stage;
|
47 | 50 | import java.util.Arrays;
|
48 | 51 | import java.util.Collection;
|
| 52 | +import java.util.concurrent.Semaphore; |
| 53 | + |
49 | 54 | import javafx.scene.control.IndexRange;
|
50 | 55 | import javafx.scene.control.PasswordField;
|
51 | 56 | import javafx.scene.control.TextArea;
|
@@ -2043,6 +2048,105 @@ public void caretAndAnchorPositionAfterSettingText() {
|
2043 | 2048 | tk.firePulse();
|
2044 | 2049 | }
|
2045 | 2050 |
|
| 2051 | + // Test for case 1 of JDK-8176270 |
| 2052 | + @Test public void addingListenerWorks() { |
| 2053 | + VBox vBox = new VBox(); |
| 2054 | + TextField textField = new TextField(); |
| 2055 | + textField.setText("1234 5678"); |
| 2056 | + vBox.getChildren().add(textField); |
| 2057 | + textField.selectedTextProperty() |
| 2058 | + .addListener((observable -> {})); |
| 2059 | + |
| 2060 | + Scene scene = new Scene(vBox); |
| 2061 | + Stage stage = new Stage(); |
| 2062 | + stage.setScene(scene); |
| 2063 | + stage.show(); |
| 2064 | + } |
| 2065 | + |
| 2066 | + // Test for case 2 of JDK-8176270 |
| 2067 | + @Test public void replaceSelectionWorks() throws Exception { |
| 2068 | + VBox vBox = new VBox(); |
| 2069 | + TextField textField = new TextField(); |
| 2070 | + textField.setText("1234 5678"); |
| 2071 | + vBox.getChildren().add(textField); |
| 2072 | + textField.selectedTextProperty() |
| 2073 | + .addListener((observable -> {})); |
| 2074 | + |
| 2075 | + Scene scene = new Scene(vBox); |
| 2076 | + Stage stage = new Stage(); |
| 2077 | + stage.setScene(scene); |
| 2078 | + stage.show(); |
| 2079 | + |
| 2080 | + textField.selectedTextProperty() |
| 2081 | + .addListener(observable -> { |
| 2082 | + // accessing the selectedTextProperty causes a |
| 2083 | + // StringOutOfBoundsException |
| 2084 | + observable.toString(); |
| 2085 | + }); |
| 2086 | + textField.positionCaret(5); |
| 2087 | + Semaphore semaphore = new Semaphore(0); |
| 2088 | + Platform.runLater(semaphore::release); |
| 2089 | + semaphore.acquire(); |
| 2090 | + |
| 2091 | + // select 2nd word |
| 2092 | + textField.selectNextWord(); |
| 2093 | + semaphore = new Semaphore(0); |
| 2094 | + Platform.runLater(semaphore::release); |
| 2095 | + semaphore.acquire(); |
| 2096 | + |
| 2097 | + // replace selection |
| 2098 | + Platform.runLater(() -> {Event.fireEvent(scene, new KeyEvent(KeyEvent.KEY_PRESSED, "", KeyCode.DIGIT0.getName(), KeyCode.DIGIT0, false, false, false, false));}); |
| 2099 | + Platform.runLater(() -> {Event.fireEvent(scene, new KeyEvent(KeyEvent.KEY_RELEASED, "", KeyCode.DIGIT0.getName(), KeyCode.DIGIT0, false, false, false, false));}); |
| 2100 | + semaphore = new Semaphore(0); |
| 2101 | + Platform.runLater(semaphore::release); |
| 2102 | + semaphore.acquire(); |
| 2103 | + } |
| 2104 | + |
| 2105 | + // Test for workaround of JDK-8176270 |
| 2106 | + @Test public void accessingTheValueInInvalidationListenerWorks() throws Exception { |
| 2107 | + VBox vBox = new VBox(); |
| 2108 | + TextField textField = new TextField(); |
| 2109 | + textField.setText("1234 5678"); |
| 2110 | + vBox.getChildren().add(textField); |
| 2111 | + textField.selectedTextProperty() |
| 2112 | + .addListener((observable -> {})); |
| 2113 | + |
| 2114 | + Scene scene = new Scene(vBox); |
| 2115 | + Stage stage = new Stage(); |
| 2116 | + stage.setScene(scene); |
| 2117 | + stage.show(); |
| 2118 | + |
| 2119 | + textField.selectedTextProperty() |
| 2120 | + .addListener(new InvalidationListener() { |
| 2121 | + @Override |
| 2122 | + public void invalidated(Observable observable) { |
| 2123 | + Platform.runLater(() -> observable.toString()); |
| 2124 | + } |
| 2125 | + }); |
| 2126 | + |
| 2127 | + textField.positionCaret(5); |
| 2128 | + Semaphore semaphore = new Semaphore(0); |
| 2129 | + Platform.runLater(semaphore::release); |
| 2130 | + semaphore.acquire(); |
| 2131 | + |
| 2132 | + // select 2nd word |
| 2133 | + textField.selectNextWord(); |
| 2134 | + semaphore = new Semaphore(0); |
| 2135 | + Platform.runLater(semaphore::release); |
| 2136 | + semaphore.acquire(); |
| 2137 | + |
| 2138 | + // replace selection |
| 2139 | + Platform.runLater(() -> {Event.fireEvent(scene, |
| 2140 | + new KeyEvent(KeyEvent.KEY_PRESSED, "", KeyCode.DIGIT0.getName(), KeyCode.DIGIT0, |
| 2141 | + false, false, false, false));}); |
| 2142 | + Platform.runLater(() -> {Event.fireEvent(scene, |
| 2143 | + new KeyEvent(KeyEvent.KEY_RELEASED, "", KeyCode.DIGIT0.getName(), KeyCode.DIGIT0, |
| 2144 | + false, false, false, false));}); |
| 2145 | + semaphore = new Semaphore(0); |
| 2146 | + Platform.runLater(semaphore::release); |
| 2147 | + semaphore.acquire(); |
| 2148 | + } |
| 2149 | + |
2046 | 2150 | // TODO tests for Content firing event notification properly
|
2047 | 2151 |
|
2048 | 2152 | // TODO tests for Content not allowing illegal characters
|
|
0 commit comments