33//! These use GPUI's test infrastructure which requires the `test-support` feature:
44//!
55//! ```sh
6- //! cargo test --example text_views -p gpui --features test-support
6+ //! cargo test --example view_example -p gpui --features test-support
77//! ```
88
99#[ cfg( test) ]
@@ -13,19 +13,25 @@ mod tests {
1313 use gpui:: { Context , Entity , KeyBinding , TestAppContext , Window , prelude:: * } ;
1414
1515 use crate :: example_editor:: ExampleEditor ;
16- use crate :: example_input:: ExampleInput ;
16+ use crate :: example_input:: { ExampleInput , ExampleInputState } ;
1717 use crate :: example_render_log:: RenderLog ;
1818 use crate :: example_text_area:: ExampleTextArea ;
1919 use crate :: { Backspace , Delete , End , Enter , Home , Left , Right } ;
2020
2121 struct InputWrapper {
22- editor : Entity < ExampleEditor > ,
22+ input_state : Entity < ExampleInputState > ,
2323 render_log : Entity < RenderLog > ,
2424 }
2525
26+ impl InputWrapper {
27+ fn editor ( & self , cx : & gpui:: App ) -> Entity < ExampleEditor > {
28+ self . input_state . read ( cx) . editor . clone ( )
29+ }
30+ }
31+
2632 impl Render for InputWrapper {
2733 fn render ( & mut self , _window : & mut Window , _cx : & mut Context < Self > ) -> impl IntoElement {
28- ExampleInput :: new ( self . editor . clone ( ) , self . render_log . clone ( ) )
34+ ExampleInput :: new ( self . input_state . clone ( ) , self . render_log . clone ( ) )
2935 }
3036 }
3137
@@ -60,12 +66,15 @@ mod tests {
6066 bind_keys ( cx) ;
6167
6268 let ( wrapper, cx) = cx. add_window_view ( |window, cx| {
63- let editor = cx. new ( |cx| ExampleEditor :: new ( window, cx) ) ;
6469 let render_log = cx. new ( |cx| RenderLog :: new ( cx) ) ;
65- InputWrapper { editor, render_log }
70+ let input_state = cx. new ( |cx| ExampleInputState :: new ( render_log. clone ( ) , window, cx) ) ;
71+ InputWrapper {
72+ input_state,
73+ render_log,
74+ }
6675 } ) ;
6776
68- let editor = cx. read_entity ( & wrapper, |wrapper, _cx | wrapper. editor . clone ( ) ) ;
77+ let editor = cx. read_entity ( & wrapper, |wrapper, cx | wrapper. editor ( cx ) ) ;
6978
7079 cx. update ( |window, cx| {
7180 let focus_handle = editor. read ( cx) . focus_handle . clone ( ) ;
@@ -155,23 +164,34 @@ mod tests {
155164 fn test_cursor_blink ( cx : & mut TestAppContext ) {
156165 let ( editor, cx) = init_input ( cx) ;
157166
167+ // Typing calls reset_blink(), which makes cursor visible and
168+ // restarts the blink timer.
169+ cx. simulate_input ( "a" ) ;
170+
158171 cx. read_entity ( & editor, |editor, _cx| {
159- assert ! ( editor. cursor_visible) ;
172+ assert ! (
173+ editor. cursor_visible,
174+ "cursor should be visible after typing"
175+ ) ;
160176 } ) ;
161177
178+ // After 500ms the blink task toggles it off.
162179 cx. background_executor
163180 . advance_clock ( Duration :: from_millis ( 500 ) ) ;
164181 cx. run_until_parked ( ) ;
165182
166183 cx. read_entity ( & editor, |editor, _cx| {
167- assert ! ( !editor. cursor_visible) ;
184+ assert ! ( !editor. cursor_visible, "cursor should have blinked off" ) ;
168185 } ) ;
169186
170- // Typing resets the blink.
171- cx. simulate_input ( "a " ) ;
187+ // Typing again resets the blink.
188+ cx. simulate_input ( "b " ) ;
172189
173190 cx. read_entity ( & editor, |editor, _cx| {
174- assert ! ( editor. cursor_visible) ;
191+ assert ! (
192+ editor. cursor_visible,
193+ "cursor should be visible after typing again"
194+ ) ;
175195 } ) ;
176196 }
177197
0 commit comments