Debian Codegraph @

tests/scene/test_tab_bar.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_tab_bar.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "scene/gui/tab_bar.h"
34 #include "scene/main/window.h"
35
36 #include "tests/test_macros.h"
37
38 namespace TestTabBar {
39 TEST_CASE("[SceneTree][TabBar] tab operations") {
40 TabBar *tab_bar = memnew(TabBar);
41 SceneTree::get_singleton()->get_root()->add_child(tab_bar);
42 tab_bar->set_clip_tabs(false);
43 MessageQueue::get_singleton()->flush();
44
45 SIGNAL_WATCH(tab_bar, "tab_selected");
46 SIGNAL_WATCH(tab_bar, "tab_changed");
47
48 SUBCASE("[TabBar] no tabs") {
49 CHECK(tab_bar->get_tab_count() == 0);
50 CHECK(tab_bar->get_current_tab() == -1);
51 CHECK(tab_bar->get_previous_tab() == -1);
52 }
53
54 SUBCASE("[TabBar] add tabs disallowing deselection") {
55 tab_bar->set_deselect_enabled(false);
56 tab_bar->add_tab("tab0");
57 CHECK(tab_bar->get_tab_count() == 1);
58 CHECK(tab_bar->get_current_tab() == 0);
59 CHECK(tab_bar->get_previous_tab() == -1);
60 SIGNAL_CHECK("tab_selected", { { 0 } });
61 SIGNAL_CHECK("tab_changed", { { 0 } });
62
63 tab_bar->add_tab("tab1");
64 CHECK(tab_bar->get_tab_count() == 2);
65 CHECK(tab_bar->get_current_tab() == 0);
66 CHECK(tab_bar->get_previous_tab() == -1);
67 SIGNAL_CHECK_FALSE("tab_selected");
68 SIGNAL_CHECK_FALSE("tab_changed");
69
70 tab_bar->add_tab("tab2");
71 CHECK(tab_bar->get_tab_count() == 3);
72 CHECK(tab_bar->get_current_tab() == 0);
73 CHECK(tab_bar->get_previous_tab() == -1);
74 SIGNAL_CHECK_FALSE("tab_selected");
75 SIGNAL_CHECK_FALSE("tab_changed");
76
77 CHECK(tab_bar->get_tab_title(0) == "tab0");
78 CHECK(tab_bar->get_tab_tooltip(0) == "");
79 CHECK(tab_bar->get_tab_text_direction(0) == Control::TEXT_DIRECTION_INHERITED);
80 CHECK_FALSE(tab_bar->is_tab_disabled(0));
81 CHECK_FALSE(tab_bar->is_tab_hidden(0));
82
83 CHECK(tab_bar->get_tab_title(1) == "tab1");
84 CHECK(tab_bar->get_tab_tooltip(1) == "");
85 CHECK(tab_bar->get_tab_text_direction(1) == Control::TEXT_DIRECTION_INHERITED);
86 CHECK_FALSE(tab_bar->is_tab_disabled(1));
87 CHECK_FALSE(tab_bar->is_tab_hidden(1));
88
89 CHECK(tab_bar->get_tab_title(2) == "tab2");
90 CHECK(tab_bar->get_tab_tooltip(2) == "");
91 CHECK(tab_bar->get_tab_text_direction(2) == Control::TEXT_DIRECTION_INHERITED);
92 CHECK_FALSE(tab_bar->is_tab_disabled(2));
93 CHECK_FALSE(tab_bar->is_tab_hidden(2));
94 }
95
96 SUBCASE("[TabBar] add tabs allowing deselection") {
97 tab_bar->set_deselect_enabled(true);
98 tab_bar->add_tab("tab0");
99 CHECK(tab_bar->get_tab_count() == 1);
100 CHECK(tab_bar->get_current_tab() == -1);
101 CHECK(tab_bar->get_previous_tab() == -1);
102 SIGNAL_CHECK_FALSE("tab_selected");
103 SIGNAL_CHECK_FALSE("tab_changed");
104
105 tab_bar->add_tab("tab1");
106 CHECK(tab_bar->get_tab_count() == 2);
107 CHECK(tab_bar->get_current_tab() == -1);
108 CHECK(tab_bar->get_previous_tab() == -1);
109 SIGNAL_CHECK_FALSE("tab_selected");
110 SIGNAL_CHECK_FALSE("tab_changed");
111 }
112
113 SUBCASE("[TabBar] set tab count") {
114 // Adds multiple tabs at once.
115 tab_bar->set_tab_count(3);
116 CHECK(tab_bar->get_tab_count() == 3);
117 CHECK(tab_bar->get_current_tab() == 0);
118 CHECK(tab_bar->get_previous_tab() == -1);
119 SIGNAL_CHECK_FALSE("tab_selected");
120 SIGNAL_CHECK_FALSE("tab_changed");
121
122 CHECK(tab_bar->get_tab_title(0) == "");
123 CHECK(tab_bar->get_tab_tooltip(0) == "");
124 CHECK(tab_bar->get_tab_text_direction(0) == Control::TEXT_DIRECTION_INHERITED);
125 CHECK_FALSE(tab_bar->is_tab_disabled(0));
126 CHECK_FALSE(tab_bar->is_tab_hidden(0));
127
128 CHECK(tab_bar->get_tab_title(1) == "");
129 CHECK(tab_bar->get_tab_tooltip(1) == "");
130 CHECK(tab_bar->get_tab_text_direction(1) == Control::TEXT_DIRECTION_INHERITED);
131 CHECK_FALSE(tab_bar->is_tab_disabled(1));
132 CHECK_FALSE(tab_bar->is_tab_hidden(1));
133
134 CHECK(tab_bar->get_tab_title(2) == "");
135 CHECK(tab_bar->get_tab_tooltip(2) == "");
136 CHECK(tab_bar->get_tab_text_direction(2) == Control::TEXT_DIRECTION_INHERITED);
137 CHECK_FALSE(tab_bar->is_tab_disabled(2));
138 CHECK_FALSE(tab_bar->is_tab_hidden(2));
139
140 // Setting to less tabs than there are removes from the end.
141 tab_bar->set_tab_title(0, "tab0");
142 tab_bar->set_tab_title(1, "tab1");
143 tab_bar->set_tab_title(2, "tab2");
144
145 tab_bar->set_tab_count(2);
146 CHECK(tab_bar->get_tab_count() == 2);
147 CHECK(tab_bar->get_current_tab() == 0);
148 CHECK(tab_bar->get_previous_tab() == -1);
149 CHECK(tab_bar->get_tab_title(0) == "tab0");
150 CHECK(tab_bar->get_tab_title(1) == "tab1");
151
152 // Remove all tabs.
153 tab_bar->set_tab_count(0);
154 CHECK(tab_bar->get_tab_count() == 0);
155 CHECK(tab_bar->get_current_tab() == -1);
156 CHECK(tab_bar->get_previous_tab() == -1);
157 }
158
159 SUBCASE("[TabBar] clear tabs") {
160 CHECK(tab_bar->get_tab_count() == 0);
161 CHECK(tab_bar->get_current_tab() == -1);
162 CHECK(tab_bar->get_previous_tab() == -1);
163
164 tab_bar->set_tab_count(2);
165 CHECK(tab_bar->get_tab_count() == 2);
166 CHECK(tab_bar->get_current_tab() == 0);
167 CHECK(tab_bar->get_previous_tab() == -1);
168 SIGNAL_DISCARD("tab_selected");
169 SIGNAL_DISCARD("tab_changed");
170
171 tab_bar->clear_tabs();
172 CHECK(tab_bar->get_tab_count() == 0);
173 CHECK(tab_bar->get_current_tab() == -1);
174 CHECK(tab_bar->get_previous_tab() == -1);
175 SIGNAL_CHECK_FALSE("tab_selected");
176 SIGNAL_CHECK_FALSE("tab_changed");
177 }
178
179 SUBCASE("[TabBar] remove tabs") {
180 tab_bar->add_tab("tab0");
181 tab_bar->add_tab("tab1");
182 tab_bar->add_tab("tab2");
183 tab_bar->set_current_tab(1);
184 CHECK(tab_bar->get_tab_count() == 3);
185 CHECK(tab_bar->get_current_tab() == 1);
186 CHECK(tab_bar->get_previous_tab() == 0);
187 SIGNAL_DISCARD("tab_selected");
188 SIGNAL_DISCARD("tab_changed");
189
190 // Remove first tab.
191 tab_bar->remove_tab(0);
192 CHECK(tab_bar->get_tab_count() == 2);
193 CHECK(tab_bar->get_tab_title(0) == "tab1");
194 CHECK(tab_bar->get_tab_title(1) == "tab2");
195 CHECK(tab_bar->get_current_tab() == 0);
196 CHECK(tab_bar->get_previous_tab() == 0);
197 SIGNAL_CHECK_FALSE("tab_selected");
198 SIGNAL_CHECK_FALSE("tab_changed");
199
200 // Remove last tab.
201 tab_bar->remove_tab(1);
202 CHECK(tab_bar->get_tab_count() == 1);
203 CHECK(tab_bar->get_tab_title(0) == "tab1");
204 CHECK(tab_bar->get_current_tab() == 0);
205 CHECK(tab_bar->get_previous_tab() == 0);
206 SIGNAL_CHECK_FALSE("tab_selected");
207 SIGNAL_CHECK_FALSE("tab_changed");
208
209 // Remove only tab.
210 tab_bar->remove_tab(0);
211 CHECK(tab_bar->get_tab_count() == 0);
212 CHECK(tab_bar->get_current_tab() == -1);
213 CHECK(tab_bar->get_previous_tab() == -1);
214 SIGNAL_CHECK_FALSE("tab_selected");
215 SIGNAL_CHECK("tab_changed", { { -1 } });
216
217 // Remove current tab when there are other tabs.
218 tab_bar->add_tab("tab0");
219 tab_bar->add_tab("tab1");
220 tab_bar->add_tab("tab2");
221 tab_bar->set_current_tab(1);
222 tab_bar->set_current_tab(2);
223 CHECK(tab_bar->get_tab_count() == 3);
224 CHECK(tab_bar->get_current_tab() == 2);
225 CHECK(tab_bar->get_previous_tab() == 1);
226 SIGNAL_DISCARD("tab_selected");
227 SIGNAL_DISCARD("tab_changed");
228
229 tab_bar->remove_tab(2);
230 CHECK(tab_bar->get_tab_count() == 2);
231 CHECK(tab_bar->get_current_tab() == 1);
232 CHECK(tab_bar->get_previous_tab() == 1);
233 SIGNAL_CHECK_FALSE("tab_selected");
234 SIGNAL_CHECK("tab_changed", { { 1 } });
235 }
236
237 SUBCASE("[TabBar] move tabs") {
238 tab_bar->add_tab("tab0");
239 tab_bar->add_tab("tab1");
240 tab_bar->add_tab("tab2");
241 tab_bar->set_current_tab(1);
242 CHECK(tab_bar->get_current_tab() == 1);
243 CHECK(tab_bar->get_previous_tab() == 0);
244 SIGNAL_DISCARD("tab_selected");
245 SIGNAL_DISCARD("tab_changed");
246
247 // Don't move if index is the same.
248 tab_bar->move_tab(0, 0);
249 CHECK(tab_bar->get_tab_title(0) == "tab0");
250 CHECK(tab_bar->get_tab_title(1) == "tab1");
251 CHECK(tab_bar->get_tab_title(2) == "tab2");
252 CHECK(tab_bar->get_current_tab() == 1);
253 CHECK(tab_bar->get_previous_tab() == 0);
254 SIGNAL_CHECK_FALSE("tab_selected");
255 SIGNAL_CHECK_FALSE("tab_changed");
256
257 // Move the first tab to the end.
258 tab_bar->move_tab(0, 2);
259 CHECK(tab_bar->get_tab_title(0) == "tab1");
260 CHECK(tab_bar->get_tab_title(1) == "tab2");
261 CHECK(tab_bar->get_tab_title(2) == "tab0");
262 CHECK(tab_bar->get_current_tab() == 0);
263 CHECK(tab_bar->get_previous_tab() == 2);
264 SIGNAL_CHECK_FALSE("tab_selected");
265 SIGNAL_CHECK_FALSE("tab_changed");
266
267 // Move the second tab to the front.
268 tab_bar->move_tab(1, 0);
269 CHECK(tab_bar->get_tab_title(0) == "tab2");
270 CHECK(tab_bar->get_tab_title(1) == "tab1");
271 CHECK(tab_bar->get_tab_title(2) == "tab0");
272 CHECK(tab_bar->get_current_tab() == 1);
273 CHECK(tab_bar->get_previous_tab() == 2);
274 SIGNAL_CHECK_FALSE("tab_selected");
275 SIGNAL_CHECK_FALSE("tab_changed");
276 }
277
278 SUBCASE("[TabBar] set current tab") {
279 tab_bar->add_tab("tab0");
280 tab_bar->add_tab("tab1");
281 tab_bar->add_tab("tab2");
282 CHECK(tab_bar->get_current_tab() == 0);
283 CHECK(tab_bar->get_previous_tab() == -1);
284 SIGNAL_CHECK("tab_selected", { { 0 } });
285 SIGNAL_CHECK("tab_changed", { { 0 } });
286
287 // Set the current tab.
288 tab_bar->set_current_tab(1);
289 CHECK(tab_bar->get_current_tab() == 1);
290 CHECK(tab_bar->get_previous_tab() == 0);
291 SIGNAL_CHECK("tab_selected", { { 1 } });
292 SIGNAL_CHECK("tab_changed", { { 1 } });
293
294 // Set to same tab.
295 tab_bar->set_current_tab(1);
296 CHECK(tab_bar->get_current_tab() == 1);
297 CHECK(tab_bar->get_previous_tab() == 1);
298 SIGNAL_CHECK("tab_selected", { { 1 } });
299 SIGNAL_CHECK_FALSE("tab_changed");
300
301 // Out of bounds.
302 ERR_PRINT_OFF;
303 tab_bar->set_current_tab(-5);
304 CHECK(tab_bar->get_current_tab() == 1);
305 CHECK(tab_bar->get_previous_tab() == 1);
306 SIGNAL_CHECK_FALSE("tab_selected");
307 SIGNAL_CHECK_FALSE("tab_changed");
308
309 tab_bar->set_current_tab(5);
310 CHECK(tab_bar->get_current_tab() == 1);
311 CHECK(tab_bar->get_previous_tab() == 1);
312 SIGNAL_CHECK_FALSE("tab_selected");
313 SIGNAL_CHECK_FALSE("tab_changed");
314 ERR_PRINT_ON;
315 }
316
317 SUBCASE("[TabBar] deselection enabled") {
318 tab_bar->add_tab("tab0");
319 tab_bar->add_tab("tab1");
320 tab_bar->add_tab("tab2");
321 CHECK(tab_bar->get_current_tab() == 0);
322 CHECK(tab_bar->get_previous_tab() == -1);
323 SIGNAL_DISCARD("tab_selected");
324 SIGNAL_DISCARD("tab_changed");
325
326 // Setting deselect enabled doesn't change current tab.
327 tab_bar->set_deselect_enabled(true);
328 CHECK(tab_bar->get_deselect_enabled());
329 CHECK(tab_bar->get_current_tab() == 0);
330 CHECK(tab_bar->get_previous_tab() == -1);
331 SIGNAL_CHECK_FALSE("tab_selected");
332 SIGNAL_CHECK_FALSE("tab_changed");
333
334 // Can deselect all tabs by setting current to -1.
335 tab_bar->set_current_tab(-1);
336 CHECK(tab_bar->get_current_tab() == -1);
337 CHECK(tab_bar->get_previous_tab() == 0);
338 SIGNAL_CHECK("tab_selected", { { -1 } });
339 SIGNAL_CHECK("tab_changed", { { -1 } });
340
341 // Adding first tab will NOT change the current tab. (stays deselected)
342 tab_bar->clear_tabs();
343 CHECK(tab_bar->get_current_tab() == -1);
344 CHECK(tab_bar->get_previous_tab() == -1);
345
346 tab_bar->add_tab("tab0");
347 tab_bar->add_tab("tab1");
348 tab_bar->add_tab("tab2");
349 CHECK(tab_bar->get_tab_count() == 3);
350 CHECK(tab_bar->get_current_tab() == -1);
351 CHECK(tab_bar->get_previous_tab() == -1);
352 SIGNAL_CHECK_FALSE("tab_selected");
353 SIGNAL_CHECK_FALSE("tab_changed");
354
355 tab_bar->set_current_tab(-1);
356 SIGNAL_DISCARD("tab_selected");
357 SIGNAL_DISCARD("tab_changed");
358
359 // Disabling while at -1 will select the first available tab.
360 tab_bar->set_deselect_enabled(false);
361 CHECK_FALSE(tab_bar->get_deselect_enabled());
362 CHECK(tab_bar->get_current_tab() == 0);
363 CHECK(tab_bar->get_previous_tab() == -1);
364 SIGNAL_CHECK("tab_selected", { { 0 } });
365 SIGNAL_CHECK("tab_changed", { { 0 } });
366
367 // Cannot set to -1 if disabled.
368 ERR_PRINT_OFF;
369 tab_bar->set_current_tab(-1);
370 CHECK(tab_bar->get_current_tab() == 0);
371 CHECK(tab_bar->get_previous_tab() == -1);
372 SIGNAL_CHECK_FALSE("tab_selected");
373 SIGNAL_CHECK_FALSE("tab_changed");
374 ERR_PRINT_ON;
375
376 // Disabling while at -1 skips any disabled or hidden tabs.
377 tab_bar->set_deselect_enabled(true);
378 tab_bar->set_tab_disabled(0, true);
379 tab_bar->set_tab_hidden(1, true);
380 tab_bar->set_current_tab(-1);
381 SIGNAL_DISCARD("tab_selected");
382 SIGNAL_DISCARD("tab_changed");
383 tab_bar->set_deselect_enabled(false);
384 CHECK(tab_bar->get_current_tab() == 2);
385 CHECK(tab_bar->get_previous_tab() == -1);
386 SIGNAL_CHECK("tab_selected", { { 2 } });
387 SIGNAL_CHECK("tab_changed", { { 2 } });
388 }
389
390 SUBCASE("[TabBar] hidden tabs") {
391 tab_bar->add_tab("tab0");
392 tab_bar->add_tab("tab1");
393 tab_bar->add_tab("tab2");
394 tab_bar->set_current_tab(1);
395 CHECK(tab_bar->get_current_tab() == 1);
396 CHECK(tab_bar->get_previous_tab() == 0);
397 CHECK_FALSE(tab_bar->is_tab_hidden(1));
398 SIGNAL_DISCARD("tab_selected");
399 SIGNAL_DISCARD("tab_changed");
400
401 MessageQueue::get_singleton()->flush();
402 Vector<Rect2> tab_rects = {
403 tab_bar->get_tab_rect(0),
404 tab_bar->get_tab_rect(1),
405 tab_bar->get_tab_rect(2)
406 };
407
408 // Hiding a tab does not affect current tab.
409 tab_bar->set_tab_hidden(1, true);
410 CHECK(tab_bar->is_tab_hidden(1));
411 CHECK(tab_bar->get_current_tab() == 1);
412 CHECK(tab_bar->get_previous_tab() == 0);
413 SIGNAL_CHECK_FALSE("tab_selected");
414 SIGNAL_CHECK_FALSE("tab_changed");
415
416 // The tabs after are moved over.
417 MessageQueue::get_singleton()->flush();
418 CHECK(tab_bar->get_tab_rect(0) == tab_rects[0]);
419 CHECK(tab_bar->get_tab_rect(2) == tab_rects[1]);
420
421 // Unhiding a tab does not affect current tab.
422 tab_bar->set_tab_hidden(1, false);
423 CHECK_FALSE(tab_bar->is_tab_hidden(1));
424 CHECK(tab_bar->get_current_tab() == 1);
425 CHECK(tab_bar->get_previous_tab() == 0);
426 SIGNAL_CHECK_FALSE("tab_selected");
427 SIGNAL_CHECK_FALSE("tab_changed");
428
429 // The tabs are back where they were.
430 MessageQueue::get_singleton()->flush();
431 CHECK(tab_bar->get_tab_rect(0) == tab_rects[0]);
432 CHECK(tab_bar->get_tab_rect(1) == tab_rects[1]);
433 CHECK(tab_bar->get_tab_rect(2) == tab_rects[2]);
434 }
435
436 SUBCASE("[TabBar] disabled tabs") {
437 tab_bar->add_tab("tab0");
438 tab_bar->add_tab("tab1");
439 tab_bar->add_tab("tab2");
440 CHECK_FALSE(tab_bar->is_tab_disabled(1));
441 tab_bar->set_current_tab(1);
442 CHECK(tab_bar->get_current_tab() == 1);
443 CHECK(tab_bar->get_previous_tab() == 0);
444 CHECK_FALSE(tab_bar->is_tab_hidden(1));
445 SIGNAL_DISCARD("tab_selected");
446 SIGNAL_DISCARD("tab_changed");
447
448 // Disabling a tab does not affect current tab.
449 tab_bar->set_tab_disabled(1, true);
450 CHECK(tab_bar->is_tab_disabled(1));
451 CHECK(tab_bar->get_current_tab() == 1);
452 CHECK(tab_bar->get_previous_tab() == 0);
453 SIGNAL_CHECK_FALSE("tab_selected");
454 SIGNAL_CHECK_FALSE("tab_changed");
455
456 // Enabling a tab does not affect current tab.
457 tab_bar->set_tab_disabled(1, false);
458 CHECK_FALSE(tab_bar->is_tab_disabled(1));
459 CHECK(tab_bar->get_current_tab() == 1);
460 CHECK(tab_bar->get_previous_tab() == 0);
461 SIGNAL_CHECK_FALSE("tab_selected");
462 SIGNAL_CHECK_FALSE("tab_changed");
463 }
464
465 SUBCASE("[TabBar] select next available") {
466 tab_bar->add_tab("tab0");
467 tab_bar->add_tab("tab1");
468 tab_bar->add_tab("tab2");
469 tab_bar->add_tab("tab3");
470 tab_bar->add_tab("tab4");
471 tab_bar->set_tab_disabled(2, true);
472 tab_bar->set_tab_hidden(3, true);
473 tab_bar->set_current_tab(0);
474 CHECK(tab_bar->get_current_tab() == 0);
475 CHECK(tab_bar->get_previous_tab() == 0);
476 SIGNAL_DISCARD("tab_selected");
477 SIGNAL_DISCARD("tab_changed");
478
479 // Selects the next tab.
480 CHECK(tab_bar->select_next_available());
481 CHECK(tab_bar->get_current_tab() == 1);
482 CHECK(tab_bar->get_previous_tab() == 0);
483 SIGNAL_CHECK("tab_selected", { { 1 } });
484 SIGNAL_CHECK("tab_changed", { { 1 } });
485
486 // Skips over disabled and hidden tabs.
487 CHECK(tab_bar->select_next_available());
488 CHECK(tab_bar->get_current_tab() == 4);
489 CHECK(tab_bar->get_previous_tab() == 1);
490 SIGNAL_CHECK("tab_selected", { { 4 } });
491 SIGNAL_CHECK("tab_changed", { { 4 } });
492
493 // Does not wrap around.
494 CHECK_FALSE(tab_bar->select_next_available());
495 CHECK(tab_bar->get_current_tab() == 4);
496 CHECK(tab_bar->get_previous_tab() == 1);
497 SIGNAL_CHECK_FALSE("tab_selected");
498 SIGNAL_CHECK_FALSE("tab_changed");
499
500 // Fails if there is only one valid tab.
501 tab_bar->remove_tab(0);
502 tab_bar->remove_tab(3);
503 CHECK(tab_bar->get_current_tab() == 0);
504 CHECK(tab_bar->get_previous_tab() == 0);
505 SIGNAL_DISCARD("tab_selected");
506 SIGNAL_DISCARD("tab_changed");
507
508 CHECK_FALSE(tab_bar->select_next_available());
509 CHECK(tab_bar->get_current_tab() == 0);
510 CHECK(tab_bar->get_previous_tab() == 0);
511 SIGNAL_CHECK_FALSE("tab_selected");
512 SIGNAL_CHECK_FALSE("tab_changed");
513
514 // Fails if there are no valid tabs.
515 tab_bar->remove_tab(0);
516 CHECK(tab_bar->get_current_tab() == -1);
517 CHECK(tab_bar->get_previous_tab() == 0);
518 SIGNAL_DISCARD("tab_selected");
519 SIGNAL_DISCARD("tab_changed");
520
521 CHECK_FALSE(tab_bar->select_next_available());
522 CHECK(tab_bar->get_current_tab() == -1);
523 CHECK(tab_bar->get_previous_tab() == 0);
524 SIGNAL_CHECK_FALSE("tab_selected");
525 SIGNAL_CHECK_FALSE("tab_changed");
526
527 // Fails if there are no tabs.
528 tab_bar->clear_tabs();
529 CHECK(tab_bar->get_current_tab() == -1);
530 CHECK(tab_bar->get_previous_tab() == -1);
531 SIGNAL_DISCARD("tab_selected");
532 SIGNAL_DISCARD("tab_changed");
533
534 CHECK_FALSE(tab_bar->select_next_available());
535 CHECK(tab_bar->get_current_tab() == -1);
536 CHECK(tab_bar->get_previous_tab() == -1);
537 SIGNAL_CHECK_FALSE("tab_selected");
538 SIGNAL_CHECK_FALSE("tab_changed");
539 }
540
541 SUBCASE("[TabBar] select previous available") {
542 tab_bar->add_tab("tab0");
543 tab_bar->add_tab("tab1");
544 tab_bar->add_tab("tab2");
545 tab_bar->add_tab("tab3");
546 tab_bar->add_tab("tab4");
547 tab_bar->set_tab_disabled(1, true);
548 tab_bar->set_tab_hidden(2, true);
549 tab_bar->set_current_tab(4);
550 CHECK(tab_bar->get_current_tab() == 4);
551 CHECK(tab_bar->get_previous_tab() == 0);
552 SIGNAL_DISCARD("tab_selected");
553 SIGNAL_DISCARD("tab_changed");
554
555 // Selects the previous tab.
556 CHECK(tab_bar->select_previous_available());
557 CHECK(tab_bar->get_current_tab() == 3);
558 CHECK(tab_bar->get_previous_tab() == 4);
559 SIGNAL_CHECK("tab_selected", { { 3 } });
560 SIGNAL_CHECK("tab_changed", { { 3 } });
561
562 // Skips over disabled and hidden tabs.
563 CHECK(tab_bar->select_previous_available());
564 CHECK(tab_bar->get_current_tab() == 0);
565 CHECK(tab_bar->get_previous_tab() == 3);
566 SIGNAL_CHECK("tab_selected", { { 0 } });
567 SIGNAL_CHECK("tab_changed", { { 0 } });
568
569 // Does not wrap around.
570 CHECK_FALSE(tab_bar->select_previous_available());
571 CHECK(tab_bar->get_current_tab() == 0);
572 CHECK(tab_bar->get_previous_tab() == 3);
573 SIGNAL_CHECK_FALSE("tab_selected");
574 SIGNAL_CHECK_FALSE("tab_changed");
575
576 // Fails if there is only one valid tab.
577 tab_bar->remove_tab(4);
578 tab_bar->remove_tab(3);
579 CHECK(tab_bar->get_current_tab() == 0);
580 CHECK(tab_bar->get_previous_tab() == 2);
581 SIGNAL_DISCARD("tab_selected");
582 SIGNAL_DISCARD("tab_changed");
583
584 CHECK_FALSE(tab_bar->select_previous_available());
585 CHECK(tab_bar->get_current_tab() == 0);
586 CHECK(tab_bar->get_previous_tab() == 2);
587 SIGNAL_CHECK_FALSE("tab_selected");
588 SIGNAL_CHECK_FALSE("tab_changed");
589
590 // Fails if there are no valid tabs.
591 tab_bar->remove_tab(0);
592 CHECK(tab_bar->get_current_tab() == -1);
593 CHECK(tab_bar->get_previous_tab() == 1);
594 SIGNAL_DISCARD("tab_selected");
595 SIGNAL_DISCARD("tab_changed");
596
597 CHECK_FALSE(tab_bar->select_previous_available());
598 CHECK(tab_bar->get_current_tab() == -1);
599 CHECK(tab_bar->get_previous_tab() == 1);
600 SIGNAL_CHECK_FALSE("tab_selected");
601 SIGNAL_CHECK_FALSE("tab_changed");
602
603 // Fails if there are no tabs.
604 tab_bar->clear_tabs();
605 CHECK(tab_bar->get_current_tab() == -1);
606 CHECK(tab_bar->get_previous_tab() == -1);
607 SIGNAL_DISCARD("tab_selected");
608 SIGNAL_DISCARD("tab_changed");
609
610 CHECK_FALSE(tab_bar->select_previous_available());
611 CHECK(tab_bar->get_current_tab() == -1);
612 CHECK(tab_bar->get_previous_tab() == -1);
613 SIGNAL_CHECK_FALSE("tab_selected");
614 SIGNAL_CHECK_FALSE("tab_changed");
615 }
616
617 SIGNAL_UNWATCH(tab_bar, "tab_selected");
618 SIGNAL_UNWATCH(tab_bar, "tab_changed");
619
620 memdelete(tab_bar);
621 }
622
623 TEST_CASE("[SceneTree][TabBar] initialization") {
624 TabBar *tab_bar = memnew(TabBar);
625
626 SIGNAL_WATCH(tab_bar, "tab_selected");
627 SIGNAL_WATCH(tab_bar, "tab_changed");
628
629 SUBCASE("[TabBar] current tab can be set before tabs are set") {
630 // This queues the current tab to update on when tabs are set.
631 tab_bar->set_current_tab(1);
632 CHECK(tab_bar->get_current_tab() == -1);
633 CHECK(tab_bar->get_previous_tab() == -1);
634 SIGNAL_CHECK_FALSE("tab_selected");
635 SIGNAL_CHECK_FALSE("tab_changed");
636
637 tab_bar->set_tab_count(2);
638 CHECK(tab_bar->get_tab_count() == 2);
639 CHECK(tab_bar->get_current_tab() == 1);
640 CHECK(tab_bar->get_previous_tab() == 0);
641 SIGNAL_CHECK("tab_selected", { { 1 } });
642 SIGNAL_CHECK("tab_changed", { { 1 } });
643
644 // Does not work again.
645 ERR_PRINT_OFF;
646 tab_bar->set_current_tab(2);
647 CHECK(tab_bar->get_tab_count() == 2);
648 CHECK(tab_bar->get_current_tab() == 1);
649 CHECK(tab_bar->get_previous_tab() == 0);
650 SIGNAL_CHECK_FALSE("tab_selected");
651 SIGNAL_CHECK_FALSE("tab_changed");
652
653 tab_bar->set_tab_count(3);
654 CHECK(tab_bar->get_tab_count() == 3);
655 CHECK(tab_bar->get_current_tab() == 1);
656 CHECK(tab_bar->get_previous_tab() == 0);
657 SIGNAL_CHECK_FALSE("tab_selected");
658 SIGNAL_CHECK_FALSE("tab_changed");
659 ERR_PRINT_ON;
660 }
661
662 SUBCASE("[TabBar] setting tabs works normally if no current tab was set") {
663 CHECK(tab_bar->get_current_tab() == -1);
664 CHECK(tab_bar->get_previous_tab() == -1);
665
666 tab_bar->set_tab_count(2);
667 CHECK(tab_bar->get_tab_count() == 2);
668 CHECK(tab_bar->get_current_tab() == 0);
669 CHECK(tab_bar->get_previous_tab() == -1);
670 SIGNAL_CHECK_FALSE("tab_selected");
671 SIGNAL_CHECK_FALSE("tab_changed");
672 }
673
674 SUBCASE("[TabBar] cannot set current tab to an invalid value before tabs are set") {
675 tab_bar->set_current_tab(100);
676 CHECK(tab_bar->get_current_tab() == -1);
677 CHECK(tab_bar->get_previous_tab() == -1);
678 SIGNAL_CHECK_FALSE("tab_selected");
679 SIGNAL_CHECK_FALSE("tab_changed");
680
681 // This will print an error message as if `set_current_tab` was called after.
682 ERR_PRINT_OFF;
683 tab_bar->set_tab_count(2);
684 CHECK(tab_bar->get_tab_count() == 2);
685 CHECK(tab_bar->get_current_tab() == 0);
686 CHECK(tab_bar->get_previous_tab() == -1);
687 SIGNAL_CHECK_FALSE("tab_selected");
688 SIGNAL_CHECK_FALSE("tab_changed");
689 ERR_PRINT_ON;
690 }
691
692 SUBCASE("[TabBar] setting the current tab before tabs only works when out of tree") {
693 tab_bar->set_current_tab(1);
694 CHECK(tab_bar->get_current_tab() == -1);
695 CHECK(tab_bar->get_previous_tab() == -1);
696 SIGNAL_CHECK_FALSE("tab_selected");
697 SIGNAL_CHECK_FALSE("tab_changed");
698
699 SceneTree::get_singleton()->get_root()->add_child(tab_bar);
700 MessageQueue::get_singleton()->flush();
701 CHECK(tab_bar->get_tab_count() == 0);
702 CHECK(tab_bar->get_current_tab() == -1);
703 CHECK(tab_bar->get_previous_tab() == -1);
704 SIGNAL_CHECK_FALSE("tab_selected");
705 SIGNAL_CHECK_FALSE("tab_changed");
706
707 // Works normally.
708 tab_bar->set_tab_count(2);
709 CHECK(tab_bar->get_tab_count() == 2);
710 CHECK(tab_bar->get_current_tab() == 0);
711 CHECK(tab_bar->get_previous_tab() == -1);
712 SIGNAL_CHECK_FALSE("tab_selected");
713 SIGNAL_CHECK_FALSE("tab_changed");
714 }
715
716 SIGNAL_UNWATCH(tab_bar, "tab_selected");
717 SIGNAL_UNWATCH(tab_bar, "tab_changed");
718
719 memdelete(tab_bar);
720 }
721
722 TEST_CASE("[SceneTree][TabBar] layout and offset") {
723 TabBar *tab_bar = memnew(TabBar);
724 SceneTree::get_singleton()->get_root()->add_child(tab_bar);
725
726 tab_bar->set_clip_tabs(false);
727 tab_bar->add_tab("tab0");
728 tab_bar->add_tab("tab1 ");
729 tab_bar->add_tab("tab2 ");
730 MessageQueue::get_singleton()->flush();
731 Size2 all_tabs_size = tab_bar->get_size();
732
733 Vector<Rect2> tab_rects = {
734 tab_bar->get_tab_rect(0),
735 tab_bar->get_tab_rect(1),
736 tab_bar->get_tab_rect(2)
737 };
738
739 SUBCASE("[TabBar] tabs are arranged next to each other") {
740 // Horizontal positions are next to each other.
741 CHECK(tab_rects[0].position.x == 0);
742 CHECK(tab_rects[1].position.x == tab_rects[0].size.x);
743 CHECK(tab_rects[2].position.x == tab_rects[1].position.x + tab_rects[1].size.x);
744
745 // Fills the entire width.
746 CHECK(tab_rects[2].position.x + tab_rects[2].size.x == all_tabs_size.x);
747
748 // Horizontal sizes are positive.
749 CHECK(tab_rects[0].size.x > 0);
750 CHECK(tab_rects[1].size.x > 0);
751 CHECK(tab_rects[2].size.x > 0);
752
753 // Vertical positions are at 0.
754 CHECK(tab_rects[0].position.y == 0);
755 CHECK(tab_rects[1].position.y == 0);
756 CHECK(tab_rects[2].position.y == 0);
757
758 // Vertical sizes are the same.
759 CHECK(tab_rects[0].size.y == tab_rects[1].size.y);
760 CHECK(tab_rects[1].size.y == tab_rects[2].size.y);
761 }
762
763 SUBCASE("[TabBar] tab alignment") {
764 // Add extra space so the alignment can be seen.
765 tab_bar->set_size(Size2(all_tabs_size.x + 100, all_tabs_size.y));
766
767 // Left alignment.
768 tab_bar->set_tab_alignment(TabBar::ALIGNMENT_LEFT);
769 MessageQueue::get_singleton()->flush();
770 tab_rects = {
771 tab_bar->get_tab_rect(0),
772 tab_bar->get_tab_rect(1),
773 tab_bar->get_tab_rect(2)
774 };
775 CHECK(tab_bar->get_tab_alignment() == TabBar::ALIGNMENT_LEFT);
776 CHECK(tab_rects[0].position.x == 0);
777 CHECK(tab_rects[1].position.x == tab_rects[0].size.x);
778 CHECK(tab_rects[2].position.x == tab_rects[1].position.x + tab_rects[1].size.x);
779
780 // Right alignment.
781 tab_bar->set_tab_alignment(TabBar::ALIGNMENT_RIGHT);
782 MessageQueue::get_singleton()->flush();
783 tab_rects = {
784 tab_bar->get_tab_rect(0),
785 tab_bar->get_tab_rect(1),
786 tab_bar->get_tab_rect(2)
787 };
788 CHECK(tab_bar->get_tab_alignment() == TabBar::ALIGNMENT_RIGHT);
789 CHECK(tab_rects[2].position.x == tab_bar->get_size().x - tab_rects[2].size.x);
790 CHECK(tab_rects[1].position.x == tab_rects[2].position.x - tab_rects[1].size.x);
791 CHECK(tab_rects[0].position.x == tab_rects[1].position.x - tab_rects[0].size.x);
792
793 // Center alignment.
794 tab_bar->set_tab_alignment(TabBar::ALIGNMENT_CENTER);
795 MessageQueue::get_singleton()->flush();
796 tab_rects = {
797 tab_bar->get_tab_rect(0),
798 tab_bar->get_tab_rect(1),
799 tab_bar->get_tab_rect(2)
800 };
801 CHECK(tab_bar->get_tab_alignment() == TabBar::ALIGNMENT_CENTER);
802 float center_pos = tab_bar->get_size().x / 2;
803 CHECK(tab_rects[0].position.x == center_pos - all_tabs_size.x / 2);
804 CHECK(tab_rects[1].position.x == tab_rects[0].position.x + tab_rects[0].size.x);
805 CHECK(tab_rects[2].position.x == tab_rects[1].position.x + tab_rects[1].size.x);
806 }
807
808 SUBCASE("[TabBar] clip tabs") {
809 // Clip tabs disabled means all tabs are visible and the minimum size holds all of them.
810 tab_bar->set_clip_tabs(false);
811 CHECK_FALSE(tab_bar->get_clip_tabs());
812 MessageQueue::get_singleton()->flush();
813 CHECK(tab_bar->get_tab_offset() == 0);
814 CHECK(tab_bar->get_minimum_size() == tab_bar->get_size());
815 CHECK(tab_bar->get_size().x == tab_rects[0].size.x + tab_rects[1].size.x + tab_rects[2].size.x);
816 CHECK(tab_bar->get_size().y == MAX(tab_rects[0].size.y, MAX(tab_rects[1].size.y, tab_rects[2].size.y)));
817
818 tab_bar->set_clip_tabs(true);
819 CHECK(tab_bar->get_clip_tabs());
820 MessageQueue::get_singleton()->flush();
821 CHECK(tab_bar->get_tab_offset() == 0);
822
823 // Horizontal size and minimum size get set to the widest tab plus arrow icons.
824 const float offset_button_size = tab_bar->get_theme_icon("decrement_icon")->get_width() + tab_bar->get_theme_icon("increment_icon")->get_width();
825 CHECK(tab_bar->get_minimum_size().x == offset_button_size + MAX(tab_rects[0].size.x, MAX(tab_rects[1].size.x, tab_rects[2].size.x)));
826 CHECK(tab_bar->get_minimum_size().y == all_tabs_size.y);
827 CHECK(tab_bar->get_size().x == offset_button_size + MAX(tab_rects[0].size.x, MAX(tab_rects[1].size.x, tab_rects[2].size.x)));
828 CHECK(tab_bar->get_size().y == all_tabs_size.y);
829 }
830
831 SUBCASE("[TabBar] ensure tab visible") {
832 tab_bar->set_scroll_to_selected(false);
833 tab_bar->set_clip_tabs(true);
834
835 // Resize tab bar to only be able to fit 2 tabs.
836 const float offset_button_size = tab_bar->get_theme_icon("decrement_icon")->get_width() + tab_bar->get_theme_icon("increment_icon")->get_width();
837 tab_bar->set_size(Size2(tab_rects[2].size.x + tab_rects[1].size.x + offset_button_size, all_tabs_size.y));
838 MessageQueue::get_singleton()->flush();
839 CHECK(tab_bar->get_tab_offset() == 0);
840 CHECK(tab_bar->get_offset_buttons_visible());
841
842 // Scroll right to a tab that is not visible.
843 tab_bar->ensure_tab_visible(2);
844 CHECK(tab_bar->get_tab_offset() == 1);
845 CHECK(tab_bar->get_tab_rect(1).position.x == 0);
846 CHECK(tab_bar->get_tab_rect(2).position.x == tab_rects[1].size.x);
847
848 tab_bar->set_tab_offset(2);
849 CHECK(tab_bar->get_tab_offset() == 2);
850 CHECK(tab_bar->get_tab_rect(2).position.x == 0);
851
852 // Scroll left to a previous tab.
853 tab_bar->ensure_tab_visible(1);
854 CHECK(tab_bar->get_tab_offset() == 1);
855 CHECK(tab_bar->get_tab_rect(1).position.x == 0);
856 CHECK(tab_bar->get_tab_rect(2).position.x == tab_rects[1].size.x);
857
858 // Will not scroll if the tab is already visible.
859 tab_bar->ensure_tab_visible(2);
860 CHECK(tab_bar->get_tab_offset() == 1);
861 CHECK(tab_bar->get_tab_rect(1).position.x == 0);
862 CHECK(tab_bar->get_tab_rect(2).position.x == tab_rects[1].size.x);
863 }
864
865 memdelete(tab_bar);
866 }
867
868 TEST_CASE("[SceneTree][TabBar] Mouse interaction") {
869 TabBar *tab_bar = memnew(TabBar);
870 SceneTree::get_singleton()->get_root()->add_child(tab_bar);
871
872 tab_bar->set_clip_tabs(false);
873 tab_bar->add_tab("tab0");
874 tab_bar->add_tab("tab1 ");
875 tab_bar->add_tab("tab2 ");
876 MessageQueue::get_singleton()->flush();
877
878 Vector<Rect2> tab_rects = {
879 tab_bar->get_tab_rect(0),
880 tab_bar->get_tab_rect(1),
881 tab_bar->get_tab_rect(2)
882 };
883
884 SIGNAL_WATCH(tab_bar, "active_tab_rearranged");
885 SIGNAL_WATCH(tab_bar, "tab_button_pressed");
886 SIGNAL_WATCH(tab_bar, "tab_changed");
887 SIGNAL_WATCH(tab_bar, "tab_clicked");
888 SIGNAL_WATCH(tab_bar, "tab_close_pressed");
889 SIGNAL_WATCH(tab_bar, "tab_hovered");
890 SIGNAL_WATCH(tab_bar, "tab_selected");
891
892 SUBCASE("[TabBar] Hover over tabs") {
893 // Default is -1.
894 CHECK(tab_bar->get_hovered_tab() == -1);
895
896 // Hover over the first tab.
897 SEND_GUI_MOUSE_MOTION_EVENT(tab_rects[0].position, MouseButtonMask::NONE, Key::NONE);
898 SIGNAL_CHECK("tab_hovered", { { 0 } });
899 CHECK(tab_bar->get_hovered_tab() == 0);
900
901 // Hover over same tab won't send signal again.
902 SEND_GUI_MOUSE_MOTION_EVENT(tab_rects[0].position + Point2(2, 2), MouseButtonMask::NONE, Key::NONE);
903 SIGNAL_CHECK_FALSE("tab_hovered");
904 CHECK(tab_bar->get_hovered_tab() == 0);
905
906 // Hover over different tab.
907 SEND_GUI_MOUSE_MOTION_EVENT(tab_rects[1].position, MouseButtonMask::NONE, Key::NONE);
908 SIGNAL_CHECK("tab_hovered", { { 1 } });
909 CHECK(tab_bar->get_hovered_tab() == 1);
910
911 // Exit area.
912 SEND_GUI_MOUSE_MOTION_EVENT(Point2(-1, -1), MouseButtonMask::NONE, Key::NONE);
913 SIGNAL_CHECK_FALSE("tab_hovered");
914 CHECK(tab_bar->get_hovered_tab() == -1);
915 }
916
917 SUBCASE("[TabBar] Click to change current") {
918 CHECK(tab_bar->get_current_tab() == 0);
919 CHECK(tab_bar->get_previous_tab() == -1);
920 SIGNAL_DISCARD("tab_selected");
921 SIGNAL_DISCARD("tab_changed");
922
923 // Click to set the current tab.
924 SEND_GUI_MOUSE_BUTTON_EVENT(tab_rects[1].position, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
925 CHECK(tab_bar->get_current_tab() == 1);
926 CHECK(tab_bar->get_previous_tab() == 0);
927 SIGNAL_CHECK("tab_selected", { { 1 } });
928 SIGNAL_CHECK("tab_changed", { { 1 } });
929 SIGNAL_CHECK("tab_clicked", { { 1 } });
930
931 // Click on the same tab.
932 SEND_GUI_MOUSE_BUTTON_EVENT(tab_rects[1].position, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
933 CHECK(tab_bar->get_current_tab() == 1);
934 CHECK(tab_bar->get_previous_tab() == 1);
935 SIGNAL_CHECK("tab_selected", { { 1 } });
936 SIGNAL_CHECK_FALSE("tab_changed");
937 SIGNAL_CHECK("tab_clicked", { { 1 } });
938 }
939
940 SUBCASE("[TabBar] Click on close button") {
941 const Size2 close_button_size = tab_bar->get_theme_icon("close_icon")->get_size();
942 int h_separation = tab_bar->get_theme_constant("h_separation");
943 float margin = tab_bar->get_theme_stylebox("tab_hovered_style")->get_margin(SIDE_RIGHT);
944
945 tab_bar->set_tab_close_display_policy(TabBar::CLOSE_BUTTON_SHOW_ALWAYS);
946 MessageQueue::get_singleton()->flush();
947 tab_rects = { tab_bar->get_tab_rect(0), tab_bar->get_tab_rect(1), tab_bar->get_tab_rect(2) };
948
949 Point2 cb_pos = Size2(tab_rects[0].get_end().x - close_button_size.x - h_separation - margin + 1, tab_rects[0].position.y + (tab_rects[0].size.y - close_button_size.y) / 2 + 1);
950 SEND_GUI_MOUSE_MOTION_EVENT(cb_pos, MouseButtonMask::LEFT, Key::NONE);
951 SEND_GUI_MOUSE_BUTTON_EVENT(cb_pos, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
952 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(cb_pos, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
953 SIGNAL_CHECK("tab_close_pressed", { { 0 } });
954 SIGNAL_CHECK_FALSE("tab_clicked");
955 SIGNAL_CHECK_FALSE("tab_selected");
956 SIGNAL_CHECK_FALSE("tab_changed");
957 SIGNAL_CHECK_FALSE("tab_button_pressed");
958 // It does not remove the tab.
959 CHECK(tab_bar->get_tab_count() == 3);
960 }
961
962 SUBCASE("[TabBar] Drag and drop internally") {
963 // Cannot drag if not enabled.
964 CHECK_FALSE(tab_bar->get_drag_to_rearrange_enabled());
965 SEND_GUI_MOUSE_BUTTON_EVENT(tab_rects[0].position, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
966 SEND_GUI_MOUSE_MOTION_EVENT(tab_rects[1].position, MouseButtonMask::LEFT, Key::NONE);
967 SIGNAL_CHECK("tab_selected", { { 0 } });
968 SIGNAL_CHECK_FALSE("tab_changed");
969 CHECK_FALSE(tab_bar->get_viewport()->gui_is_dragging());
970 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(tab_rects[1].position, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
971 CHECK_FALSE(tab_bar->get_viewport()->gui_is_dragging());
972 CHECK(tab_bar->get_tab_title(0) == "tab0");
973 CHECK(tab_bar->get_tab_title(1) == "tab1 ");
974 CHECK(tab_bar->get_tab_title(2) == "tab2 ");
975 SIGNAL_CHECK_FALSE("active_tab_rearranged");
976 SIGNAL_CHECK_FALSE("tab_selected");
977 SIGNAL_CHECK_FALSE("tab_changed");
978
979 tab_bar->set_drag_to_rearrange_enabled(true);
980 CHECK(tab_bar->get_drag_to_rearrange_enabled());
981
982 // Release over the same tab to not move.
983 SEND_GUI_MOUSE_BUTTON_EVENT(tab_rects[0].position, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
984 SEND_GUI_MOUSE_MOTION_EVENT(tab_rects[1].position, MouseButtonMask::LEFT, Key::NONE);
985 SEND_GUI_MOUSE_MOTION_EVENT(tab_rects[0].position, MouseButtonMask::LEFT, Key::NONE);
986 SIGNAL_CHECK("tab_selected", { { 0 } });
987 SIGNAL_CHECK_FALSE("tab_changed");
988 CHECK(tab_bar->get_viewport()->gui_is_dragging());
989 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(tab_rects[0].position, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
990 CHECK_FALSE(tab_bar->get_viewport()->gui_is_dragging());
991 CHECK(tab_bar->get_tab_title(0) == "tab0");
992 CHECK(tab_bar->get_tab_title(1) == "tab1 ");
993 CHECK(tab_bar->get_tab_title(2) == "tab2 ");
994 SIGNAL_CHECK_FALSE("active_tab_rearranged");
995 SIGNAL_CHECK_FALSE("tab_selected");
996 SIGNAL_CHECK_FALSE("tab_changed");
997
998 // Move the first tab after the second.
999 SEND_GUI_MOUSE_BUTTON_EVENT(tab_rects[0].position, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1000 SEND_GUI_MOUSE_MOTION_EVENT(tab_rects[1].position, MouseButtonMask::LEFT, Key::NONE);
1001 SIGNAL_CHECK("tab_selected", { { 0 } });
1002 SIGNAL_CHECK_FALSE("tab_changed");
1003 CHECK(tab_bar->get_viewport()->gui_is_dragging());
1004 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(tab_rects[1].position + Point2(tab_rects[1].size.x / 2 + 1, 0), MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1005 CHECK_FALSE(tab_bar->get_viewport()->gui_is_dragging());
1006 CHECK(tab_bar->get_tab_title(0) == "tab1 ");
1007 CHECK(tab_bar->get_tab_title(1) == "tab0");
1008 CHECK(tab_bar->get_tab_title(2) == "tab2 ");
1009 CHECK(tab_bar->get_current_tab() == 1);
1010 SIGNAL_CHECK("active_tab_rearranged", { { 1 } });
1011 SIGNAL_CHECK("tab_selected", { { 1 } });
1012 SIGNAL_CHECK_FALSE("tab_changed");
1013
1014 tab_rects = { tab_bar->get_tab_rect(0), tab_bar->get_tab_rect(1), tab_bar->get_tab_rect(2) };
1015
1016 // Move the last tab to be the first.
1017 SEND_GUI_MOUSE_BUTTON_EVENT(tab_rects[2].position, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1018 SEND_GUI_MOUSE_MOTION_EVENT(tab_rects[0].position, MouseButtonMask::LEFT, Key::NONE);
1019 SIGNAL_CHECK("tab_selected", { { 2 } });
1020 SIGNAL_CHECK("tab_changed", { { 2 } });
1021 CHECK(tab_bar->get_viewport()->gui_is_dragging());
1022 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(tab_rects[0].position, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1023 CHECK_FALSE(tab_bar->get_viewport()->gui_is_dragging());
1024 CHECK(tab_bar->get_tab_title(0) == "tab2 ");
1025 CHECK(tab_bar->get_tab_title(1) == "tab1 ");
1026 CHECK(tab_bar->get_tab_title(2) == "tab0");
1027 CHECK(tab_bar->get_current_tab() == 0);
1028 SIGNAL_CHECK("active_tab_rearranged", { { 0 } });
1029 SIGNAL_CHECK("tab_selected", { { 0 } });
1030 SIGNAL_CHECK_FALSE("tab_changed");
1031 }
1032
1033 SUBCASE("[TabBar] Drag and drop to different TabBar") {
1034 TabBar *target_tab_bar = memnew(TabBar);
1035 SceneTree::get_singleton()->get_root()->add_child(target_tab_bar);
1036
1037 target_tab_bar->set_clip_tabs(false);
1038 target_tab_bar->add_tab("other_tab0");
1039 MessageQueue::get_singleton()->flush();
1040 target_tab_bar->set_position(tab_bar->get_size());
1041 MessageQueue::get_singleton()->flush();
1042
1043 Vector<Rect2> target_tab_rects = { target_tab_bar->get_tab_rect(0) };
1044 tab_bar->set_drag_to_rearrange_enabled(true);
1045 tab_bar->set_tabs_rearrange_group(1);
1046
1047 Point2 target_tab_after_first = target_tab_bar->get_position() + target_tab_rects[0].position + Point2(target_tab_rects[0].size.x / 2 + 1, 0);
1048
1049 // Cannot drag to another TabBar that does not have drag to rearrange enabled.
1050 CHECK_FALSE(target_tab_bar->get_drag_to_rearrange_enabled());
1051 SEND_GUI_MOUSE_BUTTON_EVENT(tab_rects[0].position, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1052 SEND_GUI_MOUSE_MOTION_EVENT(tab_rects[0].position + Point2(20, 0), MouseButtonMask::LEFT, Key::NONE);
1053 SEND_GUI_MOUSE_MOTION_EVENT(target_tab_after_first, MouseButtonMask::LEFT, Key::NONE);
1054 SIGNAL_CHECK("tab_selected", { { 0 } });
1055 SIGNAL_CHECK_FALSE("tab_changed");
1056 CHECK(tab_bar->get_viewport()->gui_is_dragging());
1057 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(target_tab_after_first, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1058 CHECK_FALSE(tab_bar->get_viewport()->gui_is_dragging());
1059 CHECK(tab_bar->get_tab_count() == 3);
1060 CHECK(target_tab_bar->get_tab_count() == 1);
1061 SIGNAL_CHECK_FALSE("active_tab_rearranged");
1062 SIGNAL_CHECK_FALSE("tab_selected");
1063 SIGNAL_CHECK_FALSE("tab_changed");
1064
1065 // Cannot drag to another TabBar that has a tabs rearrange group of -1.
1066 target_tab_bar->set_drag_to_rearrange_enabled(true);
1067 tab_bar->set_tabs_rearrange_group(-1);
1068 SEND_GUI_MOUSE_BUTTON_EVENT(tab_rects[0].position, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1069 SEND_GUI_MOUSE_MOTION_EVENT(tab_rects[0].position + Point2(20, 0), MouseButtonMask::LEFT, Key::NONE);
1070 SEND_GUI_MOUSE_MOTION_EVENT(target_tab_after_first, MouseButtonMask::LEFT, Key::NONE);
1071 SIGNAL_CHECK("tab_selected", { { 0 } });
1072 SIGNAL_CHECK_FALSE("tab_changed");
1073 CHECK(tab_bar->get_viewport()->gui_is_dragging());
1074 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(target_tab_after_first, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1075 CHECK_FALSE(tab_bar->get_viewport()->gui_is_dragging());
1076 CHECK(tab_bar->get_tab_count() == 3);
1077 CHECK(target_tab_bar->get_tab_count() == 1);
1078 SIGNAL_CHECK_FALSE("active_tab_rearranged");
1079 SIGNAL_CHECK_FALSE("tab_selected");
1080 SIGNAL_CHECK_FALSE("tab_changed");
1081
1082 // Cannot drag to another TabBar that has a different tabs rearrange group.
1083 tab_bar->set_tabs_rearrange_group(1);
1084 target_tab_bar->set_tabs_rearrange_group(2);
1085 SEND_GUI_MOUSE_BUTTON_EVENT(tab_rects[0].position, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1086 SEND_GUI_MOUSE_MOTION_EVENT(tab_rects[0].position + Point2(20, 0), MouseButtonMask::LEFT, Key::NONE);
1087 SEND_GUI_MOUSE_MOTION_EVENT(target_tab_after_first, MouseButtonMask::LEFT, Key::NONE);
1088 SIGNAL_CHECK("tab_selected", { { 0 } });
1089 SIGNAL_CHECK_FALSE("tab_changed");
1090 CHECK(tab_bar->get_viewport()->gui_is_dragging());
1091 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(target_tab_after_first, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1092 CHECK_FALSE(tab_bar->get_viewport()->gui_is_dragging());
1093 CHECK(tab_bar->get_tab_count() == 3);
1094 CHECK(target_tab_bar->get_tab_count() == 1);
1095 SIGNAL_CHECK_FALSE("active_tab_rearranged");
1096 SIGNAL_CHECK_FALSE("tab_selected");
1097 SIGNAL_CHECK_FALSE("tab_changed");
1098
1099 // Drag to target container.
1100 target_tab_bar->set_tabs_rearrange_group(1);
1101 SEND_GUI_MOUSE_BUTTON_EVENT(tab_rects[0].position, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1102 SEND_GUI_MOUSE_MOTION_EVENT(tab_rects[0].position + Point2(20, 0), MouseButtonMask::LEFT, Key::NONE);
1103 SEND_GUI_MOUSE_MOTION_EVENT(target_tab_after_first, MouseButtonMask::LEFT, Key::NONE);
1104 SIGNAL_CHECK("tab_selected", { { 0 } });
1105 SIGNAL_CHECK_FALSE("tab_changed");
1106 CHECK(tab_bar->get_viewport()->gui_is_dragging());
1107 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(target_tab_after_first, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1108 CHECK_FALSE(tab_bar->get_viewport()->gui_is_dragging());
1109 CHECK(tab_bar->get_tab_count() == 2);
1110 CHECK(target_tab_bar->get_tab_count() == 2);
1111 CHECK(tab_bar->get_tab_title(0) == "tab1 ");
1112 CHECK(tab_bar->get_tab_title(1) == "tab2 ");
1113 CHECK(target_tab_bar->get_tab_title(0) == "other_tab0");
1114 CHECK(target_tab_bar->get_tab_title(1) == "tab0");
1115 CHECK(tab_bar->get_current_tab() == 0);
1116 CHECK(target_tab_bar->get_current_tab() == 1);
1117 SIGNAL_CHECK_FALSE("active_tab_rearranged");
1118 SIGNAL_CHECK_FALSE("tab_selected"); // Does not send since tab was removed.
1119 SIGNAL_CHECK("tab_changed", { { 0 } });
1120
1121 Point2 target_tab = target_tab_bar->get_position();
1122
1123 // Drag to target container at first index.
1124 target_tab_bar->set_tabs_rearrange_group(1);
1125 SEND_GUI_MOUSE_BUTTON_EVENT(tab_rects[0].position, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1126 SEND_GUI_MOUSE_MOTION_EVENT(tab_rects[0].position + Point2(20, 0), MouseButtonMask::LEFT, Key::NONE);
1127 SEND_GUI_MOUSE_MOTION_EVENT(target_tab, MouseButtonMask::LEFT, Key::NONE);
1128 SIGNAL_CHECK("tab_selected", { { 0 } });
1129 SIGNAL_CHECK_FALSE("tab_changed");
1130 CHECK(tab_bar->get_viewport()->gui_is_dragging());
1131 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(target_tab, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1132 CHECK_FALSE(tab_bar->get_viewport()->gui_is_dragging());
1133 CHECK(tab_bar->get_tab_count() == 1);
1134 CHECK(target_tab_bar->get_tab_count() == 3);
1135 CHECK(tab_bar->get_tab_title(0) == "tab2 ");
1136 CHECK(target_tab_bar->get_tab_title(0) == "tab1 ");
1137 CHECK(target_tab_bar->get_tab_title(1) == "other_tab0");
1138 CHECK(target_tab_bar->get_tab_title(2) == "tab0");
1139 CHECK(tab_bar->get_current_tab() == 0);
1140 CHECK(target_tab_bar->get_current_tab() == 0);
1141 SIGNAL_CHECK_FALSE("active_tab_rearranged");
1142 SIGNAL_CHECK_FALSE("tab_selected"); // Does not send since tab was removed.
1143 SIGNAL_CHECK("tab_changed", { { 0 } });
1144
1145 memdelete(target_tab_bar);
1146 }
1147
1148 SIGNAL_UNWATCH(tab_bar, "active_tab_rearranged");
1149 SIGNAL_UNWATCH(tab_bar, "tab_button_pressed");
1150 SIGNAL_UNWATCH(tab_bar, "tab_changed");
1151 SIGNAL_UNWATCH(tab_bar, "tab_clicked");
1152 SIGNAL_UNWATCH(tab_bar, "tab_close_pressed");
1153 SIGNAL_UNWATCH(tab_bar, "tab_hovered");
1154 SIGNAL_UNWATCH(tab_bar, "tab_selected");
1155
1156 memdelete(tab_bar);
1157 }
1158
1159 // FIXME: Add tests for keyboard navigation and other methods.
1160
1161 } // namespace TestTabBar