1
1
package org .fxmisc .richtext .keyboard ;
2
2
3
+ import javafx .application .Platform ;
3
4
import javafx .geometry .Bounds ;
4
5
import javafx .stage .Stage ;
5
6
import org .fxmisc .richtext .InlineCssTextAreaAppTest ;
@@ -26,7 +27,7 @@ public void start(Stage stage) throws Exception {
26
27
}
27
28
28
29
@ Test
29
- public void page_up_moves_caret_to_top_of_viewport () {
30
+ public void page_up_leaves_caret_at_bottom_of_viewport () {
30
31
interact (() -> {
31
32
area .moveTo (5 , 0 );
32
33
area .requestFollowCaret ();
@@ -36,14 +37,16 @@ public void page_up_moves_caret_to_top_of_viewport() {
36
37
37
38
type (PAGE_UP );
38
39
39
- Bounds afterBounds = area .getCaretBounds ().get ();
40
- assertEquals (0 , area .getCaretPosition ());
41
- assertTrue (area .getSelectedText ().isEmpty ());
42
- assertTrue (beforeBounds .getMinY () > afterBounds .getMinY ());
40
+ runLater ( 150 , () -> {
41
+ Bounds afterBounds = area .getCaretBounds ().get ();
42
+ assertEquals (8 , area .getCaretPosition ());
43
+ assertTrue (area .getSelectedText ().isEmpty ());
44
+ assertTrue (beforeBounds .getMinY () > afterBounds .getMinY ());
45
+ });
43
46
}
44
47
45
48
@ Test
46
- public void page_down_moves_caret_to_bottom_of_viewport () throws Exception {
49
+ public void page_down_leaves_caret_at_top_of_viewport () throws Exception {
47
50
interact (() -> {
48
51
area .moveTo (0 );
49
52
area .requestFollowCaret ();
@@ -53,14 +56,16 @@ public void page_down_moves_caret_to_bottom_of_viewport() throws Exception {
53
56
54
57
type (PAGE_DOWN );
55
58
56
- Bounds afterBounds = area .getCaretBounds ().get ();
57
- assertEquals (area .getAbsolutePosition (5 , 0 ), area .getCaretPosition ());
58
- assertTrue (area .getSelectedText ().isEmpty ());
59
- assertTrue (beforeBounds .getMinY () < afterBounds .getMinY ());
59
+ runLater ( 150 , () -> {
60
+ Bounds afterBounds = area .getCaretBounds ().get ();
61
+ assertEquals (area .getAbsolutePosition (3 , 0 ), area .getCaretPosition ());
62
+ assertTrue (area .getSelectedText ().isEmpty ());
63
+ assertTrue (beforeBounds .getMinY () < afterBounds .getMinY ());
64
+ });
60
65
}
61
66
62
67
@ Test
63
- public void shift_page_up_moves_caret_to_top_of_viewport_and_makes_selection () {
68
+ public void shift_page_up_leaves_caret_at_bottom_of_viewport_and_makes_selection () {
64
69
interact (() -> {
65
70
area .moveTo (5 , 0 );
66
71
area .requestFollowCaret ();
@@ -70,14 +75,16 @@ public void shift_page_up_moves_caret_to_top_of_viewport_and_makes_selection() {
70
75
71
76
press (SHIFT ).type (PAGE_UP ).release (SHIFT );
72
77
73
- Bounds afterBounds = area .getCaretBounds ().get ();
74
- assertEquals (0 , area .getCaretPosition ());
75
- assertEquals (area .getText (0 , 0 , 5 , 0 ), area .getSelectedText ());
76
- assertTrue (beforeBounds .getMinY () > afterBounds .getMinY ());
78
+ runLater ( 150 , () -> {
79
+ Bounds afterBounds = area .getCaretBounds ().get ();
80
+ assertEquals (8 , area .getCaretPosition ());
81
+ assertEquals (area .getText (0 , 0 , 5 , 0 ), area .getSelectedText ());
82
+ assertTrue (beforeBounds .getMinY () > afterBounds .getMinY ());
83
+ });
77
84
}
78
85
79
86
@ Test
80
- public void shift_page_down_moves_caret_to_bottom_of_viewport_and_makes_selection () {
87
+ public void shift_page_down_leaves_caret_at_top_of_viewport_and_makes_selection () {
81
88
interact (() -> {
82
89
area .moveTo (0 );
83
90
area .requestFollowCaret ();
@@ -87,11 +94,28 @@ public void shift_page_down_moves_caret_to_bottom_of_viewport_and_makes_selectio
87
94
88
95
press (SHIFT ).type (PAGE_DOWN ).release (SHIFT );
89
96
90
- Bounds afterBounds = area .getCaretBounds ().get ();
91
- assertEquals (area .getAbsolutePosition (5 , 0 ), area .getCaretPosition ());
92
- assertEquals (area .getText (0 , 0 , 5 , 0 ), area .getSelectedText ());
93
- assertTrue (beforeBounds .getMinY () < afterBounds .getMinY ());
97
+ runLater ( 150 , () -> {
98
+ Bounds afterBounds = area .getCaretBounds ().get ();
99
+ assertEquals (area .getAbsolutePosition (3 , 0 ), area .getCaretPosition ());
100
+ assertEquals (area .getText (0 , 0 , 3 , 0 ), area .getSelectedText ());
101
+ assertTrue (beforeBounds .getMinY () < afterBounds .getMinY ());
102
+ });
94
103
}
95
104
105
+ // Can't use sleep( t ) as that seems to delay the key press & release actions as well,
106
+ // defeating the purpose of it. So here a new thread is created for the delay ...
107
+ private void runLater ( final long time , final Runnable runFX )
108
+ {
109
+ new Thread ( () -> {
110
+ long t0 = System .currentTimeMillis ();
111
+ long t1 = t0 + time ;
112
+
113
+ while ( t0 < t1 ) try { Thread .sleep ( t1 - t0 ); } catch ( Exception e ) {}
114
+ finally { t0 = System .currentTimeMillis (); }
115
+
116
+ Platform .runLater ( runFX );
117
+
118
+ } ).start ();
119
+ }
96
120
}
97
121
0 commit comments