11package org .fxmisc .richtext .keyboard ;
22
3+ import javafx .application .Platform ;
34import javafx .geometry .Bounds ;
45import javafx .stage .Stage ;
56import org .fxmisc .richtext .InlineCssTextAreaAppTest ;
@@ -26,7 +27,7 @@ public void start(Stage stage) throws Exception {
2627 }
2728
2829 @ Test
29- public void page_up_moves_caret_to_top_of_viewport () {
30+ public void page_up_leaves_caret_at_bottom_of_viewport () {
3031 interact (() -> {
3132 area .moveTo (5 , 0 );
3233 area .requestFollowCaret ();
@@ -36,14 +37,16 @@ public void page_up_moves_caret_to_top_of_viewport() {
3637
3738 type (PAGE_UP );
3839
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+ });
4346 }
4447
4548 @ 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 {
4750 interact (() -> {
4851 area .moveTo (0 );
4952 area .requestFollowCaret ();
@@ -53,14 +56,16 @@ public void page_down_moves_caret_to_bottom_of_viewport() throws Exception {
5356
5457 type (PAGE_DOWN );
5558
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+ });
6065 }
6166
6267 @ 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 () {
6469 interact (() -> {
6570 area .moveTo (5 , 0 );
6671 area .requestFollowCaret ();
@@ -70,14 +75,16 @@ public void shift_page_up_moves_caret_to_top_of_viewport_and_makes_selection() {
7075
7176 press (SHIFT ).type (PAGE_UP ).release (SHIFT );
7277
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+ });
7784 }
7885
7986 @ 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 () {
8188 interact (() -> {
8289 area .moveTo (0 );
8390 area .requestFollowCaret ();
@@ -87,11 +94,28 @@ public void shift_page_down_moves_caret_to_bottom_of_viewport_and_makes_selectio
8794
8895 press (SHIFT ).type (PAGE_DOWN ).release (SHIFT );
8996
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+ });
94103 }
95104
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+ }
96120}
97121
0 commit comments