@@ -109,3 +109,129 @@ impl<'a, T: ToJsonTreeValue> JsonTree<'a, T> {
109
109
JsonTreeNode :: show ( self , ui)
110
110
}
111
111
}
112
+
113
+ #[ cfg( test) ]
114
+ mod tests {
115
+ use std:: sync:: LazyLock ;
116
+
117
+ use egui:: accesskit:: Role ;
118
+ use egui_kittest:: Node ;
119
+ use egui_kittest:: kittest:: NodeT ;
120
+ use egui_kittest:: { Harness , kittest:: Queryable } ;
121
+ use serde_json:: { Value , json} ;
122
+
123
+ use crate :: { DefaultExpand , JsonTree , JsonTreeStyle , ToggleButtonsState } ;
124
+
125
+ static OBJECT : LazyLock < Value > = LazyLock :: new ( || {
126
+ json ! ( {
127
+ "bar" : {
128
+ "grep" : 21 ,
129
+ "qux" : false ,
130
+ } ,
131
+ "baz" : null,
132
+ "foo" : [
133
+ 1 ,
134
+ "two"
135
+ ]
136
+ } )
137
+ } ) ;
138
+
139
+ #[ test]
140
+ fn render_object_with_toggle_buttons_visible_disabled ( ) {
141
+ let harness = Harness :: new_ui ( |ui| {
142
+ JsonTree :: new ( "id" , & * OBJECT )
143
+ . default_expand ( DefaultExpand :: All )
144
+ . style (
145
+ JsonTreeStyle :: new ( ) . toggle_buttons_state ( ToggleButtonsState :: VisibleDisabled ) ,
146
+ )
147
+ . show ( ui) ;
148
+ } ) ;
149
+
150
+ assert_eq ! ( query_all_collapsing_headers( & harness) . count( ) , 3 ) ;
151
+ assert ! (
152
+ query_all_collapsing_headers( & harness) . all( |node| node. accesskit_node( ) . is_disabled( ) )
153
+ )
154
+ }
155
+
156
+ #[ test]
157
+ fn render_object_with_toggle_buttons_visible_enabled ( ) {
158
+ let harness = Harness :: new_ui ( |ui| {
159
+ JsonTree :: new ( "id" , & * OBJECT )
160
+ . default_expand ( DefaultExpand :: All )
161
+ . style (
162
+ JsonTreeStyle :: new ( ) . toggle_buttons_state ( ToggleButtonsState :: VisibleEnabled ) ,
163
+ )
164
+ . show ( ui) ;
165
+ } ) ;
166
+
167
+ assert_eq ! ( query_all_collapsing_headers( & harness) . count( ) , 3 ) ;
168
+ assert ! (
169
+ query_all_collapsing_headers( & harness) . all( |node| !node. accesskit_node( ) . is_disabled( ) )
170
+ )
171
+ }
172
+
173
+ #[ test]
174
+ fn render_object_with_toggle_buttons_hidden ( ) {
175
+ let harness = Harness :: new_ui ( |ui| {
176
+ JsonTree :: new ( "id" , & * OBJECT )
177
+ . default_expand ( DefaultExpand :: All )
178
+ . style ( JsonTreeStyle :: new ( ) . toggle_buttons_state ( ToggleButtonsState :: Hidden ) )
179
+ . show ( ui) ;
180
+ } ) ;
181
+
182
+ assert_eq ! ( query_all_collapsing_headers( & harness) . count( ) , 0 ) ;
183
+ }
184
+
185
+ #[ test]
186
+ fn render_object_with_interaction_and_manual_reset_expanded ( ) {
187
+ let mut harness = Harness :: new_ui_state (
188
+ |ui, should_reset_expanded| {
189
+ let response = JsonTree :: new ( "id" , & * OBJECT )
190
+ . default_expand ( DefaultExpand :: None )
191
+ . style ( JsonTreeStyle :: new ( ) . abbreviate_root ( true ) )
192
+ . show ( ui) ;
193
+
194
+ if * should_reset_expanded {
195
+ response. reset_expanded ( ui) ;
196
+ }
197
+ } ,
198
+ false ,
199
+ ) ;
200
+
201
+ assert_eq ! ( query_all_collapsing_headers( & harness) . count( ) , 1 ) ;
202
+ assert_eq ! ( harness. query_all_by_role( Role :: Label ) . count( ) , 1 ) ;
203
+
204
+ get_collapsing_header_node ( & harness, "" ) . click ( ) ;
205
+ harness. run ( ) ;
206
+ assert_eq ! ( query_all_collapsing_headers( & harness) . count( ) , 3 ) ;
207
+ assert_eq ! ( harness. query_all_by_role( Role :: Label ) . count( ) , 11 ) ;
208
+
209
+ get_collapsing_header_node ( & harness, "/bar" ) . click ( ) ;
210
+ harness. run ( ) ;
211
+ assert_eq ! ( harness. query_all_by_role( Role :: Label ) . count( ) , 18 ) ;
212
+ assert ! ( harness. query_by_label( "\" grep\" " ) . is_some( ) ) ;
213
+ assert ! ( harness. query_by_label( "21" ) . is_some( ) ) ;
214
+ assert ! ( harness. query_by_label( "\" qux\" " ) . is_some( ) ) ;
215
+ assert ! ( harness. query_by_label( "false" ) . is_some( ) ) ;
216
+
217
+ * harness. state_mut ( ) = true ;
218
+ // Resetting expanded manually has a one frame delay, since the reset call happens after the tree renders, hence two runs.
219
+ harness. run ( ) ;
220
+ harness. run ( ) ;
221
+ assert_eq ! ( query_all_collapsing_headers( & harness) . count( ) , 1 ) ;
222
+ assert_eq ! ( harness. query_all_by_role( Role :: Label ) . count( ) , 1 ) ;
223
+ }
224
+
225
+ fn query_all_collapsing_headers < ' a , S > (
226
+ harness : & ' a Harness < ' _ , S > ,
227
+ ) -> impl Iterator < Item = Node < ' a > > {
228
+ harness. query_all_by_role ( Role :: Button )
229
+ }
230
+
231
+ fn get_collapsing_header_node < ' a , S > (
232
+ harness : & ' a Harness < ' _ , S > ,
233
+ pointer : & ' a str ,
234
+ ) -> Node < ' a > {
235
+ harness. get_by_role_and_label ( Role :: Button , pointer)
236
+ }
237
+ }
0 commit comments