1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
|
from __future__ import annotations
import pytest
from textual.app import App, ComposeResult
from textual.reactive import var
from textual.widgets import Label, Tab, TabbedContent, TabPane, Tabs
from textual.widgets._tabbed_content import ContentTab
async def test_tabbed_content_switch_via_ui():
"""Check tab navigation via the user interface."""
class TabbedApp(App):
def compose(self) -> ComposeResult:
with TabbedContent():
with TabPane("foo", id="foo"):
yield Label("Foo", id="foo-label")
with TabPane("bar", id="bar"):
yield Label("Bar", id="bar-label")
with TabPane("baz", id="baz"):
yield Label("Baz", id="baz-label")
app = TabbedApp()
async with app.run_test() as pilot:
tabbed_content = app.query_one(TabbedContent)
# Check first tab
assert tabbed_content.active == "foo"
assert tabbed_content.active_pane.id == "foo"
await pilot.pause()
assert app.query_one("#foo-label").region
assert not app.query_one("#bar-label").region
assert not app.query_one("#baz-label").region
# Click second tab
await pilot.click(f"Tab#{ContentTab.add_prefix('bar')}")
assert tabbed_content.active == "bar"
assert tabbed_content.active_pane.id == "bar"
await pilot.pause()
assert not app.query_one("#foo-label").region
assert app.query_one("#bar-label").region
assert not app.query_one("#baz-label").region
# Click third tab
await pilot.click(f"Tab#{ContentTab.add_prefix('baz')}")
assert tabbed_content.active == "baz"
assert tabbed_content.active_pane.id == "baz"
await pilot.pause()
assert not app.query_one("#foo-label").region
assert not app.query_one("#bar-label").region
assert app.query_one("#baz-label").region
# Press left
await pilot.press("left")
assert tabbed_content.active == "bar"
await pilot.pause()
assert not app.query_one("#foo-label").region
assert app.query_one("#bar-label").region
assert not app.query_one("#baz-label").region
# Press right
await pilot.press("right")
assert tabbed_content.active == "baz"
await pilot.pause()
assert not app.query_one("#foo-label").region
assert not app.query_one("#bar-label").region
assert app.query_one("#baz-label").region
async def test_tabbed_content_switch_via_code():
"""Check tab navigation via code."""
class TabbedApp(App):
def compose(self) -> ComposeResult:
with TabbedContent():
with TabPane("foo", id="foo"):
yield Label("Foo", id="foo-label")
with TabPane("bar", id="bar"):
yield Label("Bar", id="bar-label")
with TabPane("baz", id="baz"):
yield Label("Baz", id="baz-label")
app = TabbedApp()
async with app.run_test() as pilot:
tabbed_content = app.query_one(TabbedContent)
# Check first tab
assert tabbed_content.active == "foo"
assert app.query_one("#foo-label").region
assert not app.query_one("#bar-label").region
assert not app.query_one("#baz-label").region
# Click second tab
tabbed_content.active = "bar"
await pilot.pause()
assert not app.query_one("#foo-label").region
assert app.query_one("#bar-label").region
assert not app.query_one("#baz-label").region
# Click third tab
tabbed_content.active = "baz"
await pilot.pause()
assert not app.query_one("#foo-label").region
assert not app.query_one("#bar-label").region
assert app.query_one("#baz-label").region
# Check fail with non existent tab
with pytest.raises(ValueError):
tabbed_content.active = "X"
async def test_unsetting_tabbed_content_active():
"""Check that setting `TabbedContent.active = ""` unsets active tab."""
messages = []
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent(initial="bar"):
with TabPane("foo", id="foo"):
yield Label("Foo", id="foo-label")
with TabPane("bar", id="bar"):
yield Label("Bar", id="bar-label")
with TabPane("baz", id="baz"):
yield Label("Baz", id="baz-label")
def on_tabbed_content_cleared(self, event: TabbedContent.Cleared) -> None:
messages.append(event)
app = TabbedApp()
async with app.run_test() as pilot:
tabbed_content = app.query_one(TabbedContent)
assert bool(tabbed_content.active)
tabbed_content.active = ""
await pilot.pause()
assert len(messages) == 1
assert isinstance(messages[0], TabbedContent.Cleared)
async def test_tabbed_content_initial():
"""Checked tabbed content with non-default tab."""
class TabbedApp(App):
def compose(self) -> ComposeResult:
with TabbedContent(initial="bar"):
with TabPane("foo", id="foo"):
yield Label("Foo", id="foo-label")
with TabPane("bar", id="bar"):
yield Label("Bar", id="bar-label")
with TabPane("baz", id="baz"):
yield Label("Baz", id="baz-label")
app = TabbedApp()
async with app.run_test():
tabbed_content = app.query_one(TabbedContent)
assert tabbed_content.active == "bar"
# Check only bar is visible
assert not app.query_one("#foo-label").region
assert app.query_one("#bar-label").region
assert not app.query_one("#baz-label").region
async def test_tabbed_content_messages():
class TabbedApp(App):
activation_history: list[Tab] = []
def compose(self) -> ComposeResult:
with TabbedContent():
with TabPane("foo", id="foo"):
yield Label("Foo", id="foo-label")
with TabPane("bar", id="bar"):
yield Label("Bar", id="bar-label")
with TabPane("baz", id="baz"):
yield Label("Baz", id="baz-label")
def on_tabbed_content_tab_activated(
self, event: TabbedContent.TabActivated
) -> None:
self.activation_history.append(event.tab)
app = TabbedApp()
async with app.run_test() as pilot:
tabbed_content = app.query_one(TabbedContent)
tabbed_content.active = "bar"
await pilot.pause()
assert app.activation_history == [
# foo was originally activated.
app.query_one(TabbedContent).get_tab("foo"),
# then we did bar "by hand"
app.query_one(TabbedContent).get_tab("bar"),
]
async def test_tabbed_content_add_later_from_empty():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
yield TabbedContent()
async with TabbedApp().run_test() as pilot:
tabbed_content = pilot.app.query_one(TabbedContent)
assert tabbed_content.active == ""
assert tabbed_content.tab_count == 0
await tabbed_content.add_pane(TabPane("Test 1", id="test-1"))
assert tabbed_content.tab_count == 1
assert tabbed_content.active == "test-1"
await tabbed_content.add_pane(TabPane("Test 2", id="test-2"))
assert tabbed_content.tab_count == 2
assert tabbed_content.active == "test-1"
async def test_tabbed_content_add_later_from_composed():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield TabPane("Test 1", id="initial-1")
yield TabPane("Test 2", id="initial-2")
yield TabPane("Test 3", id="initial-3")
async with TabbedApp().run_test() as pilot:
tabbed_content = pilot.app.query_one(TabbedContent)
assert tabbed_content.tab_count == 3
assert tabbed_content.active == "initial-1"
await tabbed_content.add_pane(TabPane("Test 4", id="test-1"))
assert tabbed_content.tab_count == 4
assert tabbed_content.active == "initial-1"
await tabbed_content.add_pane(TabPane("Test 5", id="test-2"))
assert tabbed_content.tab_count == 5
assert tabbed_content.active == "initial-1"
async def test_tabbed_content_add_before_id():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield TabPane("Test 1", id="initial-1")
async with TabbedApp().run_test() as pilot:
tabbed_content = pilot.app.query_one(TabbedContent)
assert tabbed_content.tab_count == 1
assert tabbed_content.active == "initial-1"
await tabbed_content.add_pane(TabPane("Added", id="new-1"), before="initial-1")
assert tabbed_content.tab_count == 2
assert tabbed_content.active == "initial-1"
assert [
ContentTab.sans_prefix(tab.id) for tab in tabbed_content.query(Tab)
] == [
"new-1",
"initial-1",
]
async def test_tabbed_content_add_before_pane():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield TabPane("Test 1", id="initial-1")
async with TabbedApp().run_test() as pilot:
tabbed_content = pilot.app.query_one(TabbedContent)
assert tabbed_content.tab_count == 1
assert tabbed_content.active == "initial-1"
await tabbed_content.add_pane(
TabPane("Added", id="new-1"),
before=pilot.app.query_one("TabPane#initial-1", TabPane),
)
assert tabbed_content.tab_count == 2
assert tabbed_content.active == "initial-1"
assert [
ContentTab.sans_prefix(tab.id)
for tab in tabbed_content.query(Tab).results(Tab)
] == [
"new-1",
"initial-1",
]
async def test_tabbed_content_add_before_badly():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield TabPane("Test 1", id="initial-1")
async with TabbedApp().run_test() as pilot:
tabbed_content = pilot.app.query_one(TabbedContent)
assert tabbed_content.tab_count == 1
assert tabbed_content.active == "initial-1"
with pytest.raises(Tabs.TabError):
await tabbed_content.add_pane(
TabPane("Added", id="new-1"), before="unknown-1"
)
async def test_tabbed_content_add_after():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield TabPane("Test 1", id="initial-1")
async with TabbedApp().run_test() as pilot:
tabbed_content = pilot.app.query_one(TabbedContent)
assert tabbed_content.tab_count == 1
assert tabbed_content.active == "initial-1"
await tabbed_content.add_pane(TabPane("Added", id="new-1"), after="initial-1")
assert tabbed_content.tab_count == 2
assert tabbed_content.active == "initial-1"
assert [
ContentTab.sans_prefix(tab.id)
for tab in tabbed_content.query(Tab).results(Tab)
] == [
"initial-1",
"new-1",
]
async def test_tabbed_content_add_after_pane():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield TabPane("Test 1", id="initial-1")
async with TabbedApp().run_test() as pilot:
tabbed_content = pilot.app.query_one(TabbedContent)
assert tabbed_content.tab_count == 1
assert tabbed_content.active == "initial-1"
await tabbed_content.add_pane(
TabPane("Added", id="new-1"),
after=pilot.app.query_one("TabPane#initial-1", TabPane),
)
await pilot.pause()
assert tabbed_content.tab_count == 2
assert tabbed_content.active == "initial-1"
assert [
ContentTab.sans_prefix(tab.id)
for tab in tabbed_content.query(Tab).results(Tab)
] == [
"initial-1",
"new-1",
]
async def test_tabbed_content_add_after_badly():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield TabPane("Test 1", id="initial-1")
async with TabbedApp().run_test() as pilot:
tabbed_content = pilot.app.query_one(TabbedContent)
assert tabbed_content.tab_count == 1
assert tabbed_content.active == "initial-1"
with pytest.raises(Tabs.TabError):
await tabbed_content.add_pane(
TabPane("Added", id="new-1"), after="unknown-1"
)
async def test_tabbed_content_add_before_and_after():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield TabPane("Test 1", id="initial-1")
async with TabbedApp().run_test() as pilot:
tabbed_content = pilot.app.query_one(TabbedContent)
assert tabbed_content.tab_count == 1
assert tabbed_content.active == "initial-1"
with pytest.raises(Tabs.TabError):
await tabbed_content.add_pane(
TabPane("Added", id="new-1"), before="initial-1", after="initial-1"
)
async def test_tabbed_content_removal():
class TabbedApp(App[None]):
cleared: var[int] = var(0)
def compose(self) -> ComposeResult:
with TabbedContent():
yield TabPane("Test 1", id="initial-1")
yield TabPane("Test 2", id="initial-2")
yield TabPane("Test 3", id="initial-3")
def on_tabbed_content_cleared(self) -> None:
self.cleared += 1
async with TabbedApp().run_test() as pilot:
tabbed_content = pilot.app.query_one(TabbedContent)
assert tabbed_content.tab_count == 3
assert pilot.app.cleared == 0
assert tabbed_content.active == "initial-1"
await tabbed_content.remove_pane("initial-1")
await pilot.pause()
assert tabbed_content.tab_count == 2
assert pilot.app.cleared == 0
assert tabbed_content.active == "initial-2"
await tabbed_content.remove_pane("initial-2")
await pilot.pause()
assert tabbed_content.tab_count == 1
assert pilot.app.cleared == 0
assert tabbed_content.active == "initial-3"
await tabbed_content.remove_pane("initial-3")
await pilot.pause()
assert tabbed_content.tab_count == 0
assert pilot.app.cleared == 1
assert tabbed_content.active == ""
async def test_tabbed_content_reversed_removal():
class TabbedApp(App[None]):
cleared: var[int] = var(0)
def compose(self) -> ComposeResult:
with TabbedContent():
yield TabPane("Test 1", id="initial-1")
yield TabPane("Test 2", id="initial-2")
yield TabPane("Test 3", id="initial-3")
def on_tabbed_content_cleared(self) -> None:
self.cleared += 1
async with TabbedApp().run_test() as pilot:
tabbed_content = pilot.app.query_one(TabbedContent)
assert tabbed_content.tab_count == 3
assert pilot.app.cleared == 0
assert tabbed_content.active == "initial-1"
await tabbed_content.remove_pane("initial-3")
await pilot.pause()
assert tabbed_content.tab_count == 2
assert pilot.app.cleared == 0
assert tabbed_content.active == "initial-1"
await tabbed_content.remove_pane("initial-2")
await pilot.pause()
assert tabbed_content.tab_count == 1
assert pilot.app.cleared == 0
assert tabbed_content.active == "initial-1"
await tabbed_content.remove_pane("initial-1")
await pilot.pause()
assert tabbed_content.tab_count == 0
assert pilot.app.cleared == 1
assert tabbed_content.active == ""
async def test_tabbed_content_clear():
class TabbedApp(App[None]):
cleared: var[int] = var(0)
def compose(self) -> ComposeResult:
with TabbedContent():
yield TabPane("Test 1", id="initial-1")
yield TabPane("Test 2", id="initial-2")
yield TabPane("Test 3", id="initial-3")
def on_tabbed_content_cleared(self) -> None:
self.cleared += 1
async with TabbedApp().run_test() as pilot:
tabbed_content = pilot.app.query_one(TabbedContent)
assert tabbed_content.tab_count == 3
assert tabbed_content.active == "initial-1"
assert pilot.app.cleared == 0
await tabbed_content.clear_panes()
await pilot.pause()
assert tabbed_content.tab_count == 0
assert tabbed_content.active == ""
assert pilot.app.cleared == 1
async def test_disabling_does_not_deactivate_tab():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield Label("tab-1")
def on_mount(self) -> None:
self.query_one(TabbedContent).get_tab("tab-1").disabled = True
app = TabbedApp()
async with app.run_test():
assert app.query_one(TabbedContent).active == "tab-1"
async def test_disabled_tab_cannot_be_clicked():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield Label("tab-1")
yield Label("tab-2")
def on_mount(self) -> None:
self.query_one(TabbedContent).get_tab("tab-2").disabled = True
app = TabbedApp()
async with app.run_test() as pilot:
await pilot.click(f"Tab#{ContentTab.add_prefix('tab-2')}")
assert app.query_one(TabbedContent).active == "tab-1"
async def test_disabling_via_tabbed_content():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield Label("tab-1")
yield Label("tab-2")
def on_mount(self) -> None:
self.query_one(TabbedContent).disable_tab("tab-2")
app = TabbedApp()
async with app.run_test() as pilot:
await pilot.click(f"Tab#{ContentTab.add_prefix('tab-2')}")
assert app.query_one(TabbedContent).active == "tab-1"
async def test_disabling_via_tab_pane():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield Label("tab-1")
yield Label("tab-2")
def on_mount(self) -> None:
self.query_one("TabPane#tab-2").disabled = True
app = TabbedApp()
async with app.run_test() as pilot:
await pilot.pause()
await pilot.click(f"Tab#{ContentTab.add_prefix('tab-2')}")
assert app.query_one(TabbedContent).active == "tab-1"
async def test_creating_disabled_tab():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
with TabPane("first"):
yield Label("hello")
with TabPane("second", disabled=True):
yield Label("world")
app = TabbedApp()
async with app.run_test() as pilot:
await pilot.click(f"Tab#{ContentTab.add_prefix('tab-2')}")
assert app.query_one(TabbedContent).active == "tab-1"
async def test_navigation_around_disabled_tabs():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield Label("tab-1")
yield Label("tab-2")
yield Label("tab-3")
yield Label("tab-4")
def on_mount(self) -> None:
self.query_one(TabbedContent).get_tab("tab-1").disabled = True
self.query_one(TabbedContent).get_tab("tab-3").disabled = True
app = TabbedApp()
async with app.run_test() as pilot:
tabbed_conent = app.query_one(TabbedContent)
tabs = app.query_one(Tabs)
assert tabbed_conent.active == "tab-1"
tabs.action_next_tab()
await pilot.pause()
assert tabbed_conent.active == "tab-2"
tabs.action_next_tab()
await pilot.pause()
assert tabbed_conent.active == "tab-4"
tabs.action_next_tab()
await pilot.pause()
assert tabbed_conent.active == "tab-2"
tabs.action_previous_tab()
await pilot.pause()
assert tabbed_conent.active == "tab-4"
async def test_reenabling_tab():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield Label("tab-1")
yield Label("tab-2")
def on_mount(self) -> None:
self.query_one(TabbedContent).get_tab("tab-2").disabled = True
def reenable(self) -> None:
self.query_one(TabbedContent).get_tab("tab-2").disabled = False
app = TabbedApp()
async with app.run_test() as pilot:
await pilot.click(f"Tab#{ContentTab.add_prefix('tab-2')}")
assert app.query_one(TabbedContent).active == "tab-1"
app.reenable()
await pilot.click(f"Tab#{ContentTab.add_prefix('tab-2')}")
assert app.query_one(TabbedContent).active == "tab-2"
async def test_reenabling_via_tabbed_content():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield Label("tab-1")
yield Label("tab-2")
def on_mount(self) -> None:
self.query_one(TabbedContent).disable_tab("tab-2")
def reenable(self) -> None:
self.query_one(TabbedContent).enable_tab("tab-2")
app = TabbedApp()
async with app.run_test() as pilot:
await pilot.click(f"Tab#{ContentTab.add_prefix('tab-2')}")
assert app.query_one(TabbedContent).active == "tab-1"
app.reenable()
await pilot.click(f"Tab#{ContentTab.add_prefix('tab-2')}")
assert app.query_one(TabbedContent).active == "tab-2"
async def test_reenabling_via_tab_pane():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield Label("tab-1")
yield Label("tab-2")
def on_mount(self) -> None:
self.query_one("TabPane#tab-2").disabled = True
def reenable(self) -> None:
self.query_one("TabPane#tab-2").disabled = False
app = TabbedApp()
async with app.run_test() as pilot:
await pilot.pause()
await pilot.click(f"Tab#{ContentTab.add_prefix('tab-2')}")
assert app.query_one(TabbedContent).active == "tab-1"
app.reenable()
await pilot.click(f"Tab#{ContentTab.add_prefix('tab-2')}")
assert app.query_one(TabbedContent).active == "tab-2"
async def test_disabling_unknown_tab():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield Label("tab-1")
app = TabbedApp()
async with app.run_test():
with pytest.raises(Tabs.TabError):
app.query_one(TabbedContent).disable_tab("foo")
async def test_enabling_unknown_tab():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield Label("tab-1")
app = TabbedApp()
async with app.run_test():
with pytest.raises(Tabs.TabError):
app.query_one(TabbedContent).enable_tab("foo")
async def test_hide_unknown_tab():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield Label("tab-1")
app = TabbedApp()
async with app.run_test():
with pytest.raises(Tabs.TabError):
app.query_one(TabbedContent).hide_tab("foo")
async def test_show_unknown_tab():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield Label("tab-1")
app = TabbedApp()
async with app.run_test():
with pytest.raises(Tabs.TabError):
app.query_one(TabbedContent).show_tab("foo")
async def test_hide_show_messages():
hide_msg = False
show_msg = False
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield Label("tab-1")
def on_tabs_tab_hidden(self) -> None:
nonlocal hide_msg
hide_msg = True
def on_tabs_tab_shown(self) -> None:
nonlocal show_msg
show_msg = True
app = TabbedApp()
async with app.run_test() as pilot:
app.query_one(TabbedContent).hide_tab("tab-1")
await pilot.pause()
assert hide_msg
app.query_one(TabbedContent).show_tab("tab-1")
await pilot.pause()
assert show_msg
async def test_hide_last_tab_means_no_tab_active():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield Label("tab-1")
app = TabbedApp()
async with app.run_test() as pilot:
tabbed_content = app.query_one(TabbedContent)
tabbed_content.hide_tab("tab-1")
await pilot.pause()
assert not tabbed_content.active
async def test_hiding_tabs_moves_active_to_next_tab():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield Label("tab-1")
yield Label("tab-2")
yield Label("tab-3")
app = TabbedApp()
async with app.run_test() as pilot:
tabbed_content = app.query_one(TabbedContent)
tabbed_content.hide_tab("tab-1")
await pilot.pause()
assert tabbed_content.active == "tab-2"
tabbed_content.hide_tab("tab-2")
await pilot.pause()
assert tabbed_content.active == "tab-3"
async def test_showing_tabs_does_not_change_active_tab():
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield Label("tab-1")
yield Label("tab-2")
yield Label("tab-3")
app = TabbedApp()
async with app.run_test() as pilot:
tabbed_content = app.query_one(TabbedContent)
tabbed_content.hide_tab("tab-1")
tabbed_content.hide_tab("tab-2")
await pilot.pause()
# sanity check
assert tabbed_content.active == "tab-3"
tabbed_content.show_tab("tab-1")
tabbed_content.show_tab("tab-2")
assert tabbed_content.active == "tab-3"
@pytest.mark.parametrize("tab_id", ["tab-1", "tab-2"])
async def test_showing_first_tab_activates_tab(tab_id: str):
class TabbedApp(App[None]):
def compose(self) -> ComposeResult:
with TabbedContent():
yield Label("tab-1")
yield Label("tab-2")
app = TabbedApp()
async with app.run_test() as pilot:
tabbed_content = app.query_one(TabbedContent)
tabbed_content.hide_tab("tab-1")
tabbed_content.hide_tab("tab-2")
await pilot.pause()
# sanity check
assert not tabbed_content.active
tabbed_content.show_tab(tab_id)
await pilot.pause()
assert tabbed_content.active == tab_id
async def test_disabling_nested_tabs():
"""Regression test for https://github.com/Textualize/textual/issues/3145."""
class TabbedApp(App):
def compose(self) -> ComposeResult:
with TabbedContent(id="tabbed-content"):
with TabPane("Tab Pane 1"):
yield Label("foo")
with TabPane("Tab Pane 2"):
yield Label("bar")
with TabPane("Tab Pane 3"):
with TabbedContent():
with TabPane("Inner Pane 1"):
yield Label("fizz")
with TabPane("Inner Pane 2"):
yield Label("bang")
app = TabbedApp()
async with app.run_test() as pilot:
tabber = app.query_one("#tabbed-content", expect_type=TabbedContent)
tabber.disable_tab("tab-1")
await pilot.pause()
tabber.enable_tab("tab-1")
await pilot.pause()
async def test_hiding_nested_tabs():
"""Regression test for https://github.com/Textualize/textual/issues/3145."""
class TabbedApp(App):
def compose(self) -> ComposeResult:
with TabbedContent(id="tabbed-content"):
with TabPane("Tab Pane 1"):
yield Label("foo")
with TabPane("Tab Pane 2"):
yield Label("bar")
with TabPane("Tab Pane 3"):
with TabbedContent():
with TabPane("Inner Pane 1"):
yield Label("fizz")
with TabPane("Inner Pane 2"):
yield Label("bang")
app = TabbedApp()
async with app.run_test() as pilot:
tabber = app.query_one("#tabbed-content", expect_type=TabbedContent)
tabber.hide_tab("tab-1")
await pilot.pause()
tabber.show_tab("tab-1")
await pilot.pause()
async def test_tabs_nested_in_tabbed_content_doesnt_crash():
"""Regression test for https://github.com/Textualize/textual/issues/3412"""
class TabsNestedInTabbedContent(App):
def compose(self) -> ComposeResult:
with TabbedContent():
with TabPane("Outer TabPane"):
yield Tabs("Inner Tab")
app = TabsNestedInTabbedContent()
async with app.run_test() as pilot:
await pilot.pause()
async def test_tabs_nested_doesnt_interfere_with_ancestor_tabbed_content():
"""When a Tabs is nested as a descendant in the DOM of a TabbedContent,
the messages posted from that Tabs should not interfere with the TabbedContent.
A TabbedContent should only handle messages from Tabs which are direct children.
Relates to https://github.com/Textualize/textual/issues/3412
"""
class TabsNestedInTabbedContent(App):
def compose(self) -> ComposeResult:
with TabbedContent():
with TabPane("OuterTab", id="outer1"):
yield Tabs(
Tab("Tab1", id="tab1"),
Tab("Tab2", id="tab2"),
id="inner-tabs",
)
app = TabsNestedInTabbedContent()
async with app.run_test():
inner_tabs = app.query_one("#inner-tabs", Tabs)
tabbed_content = app.query_one(TabbedContent)
assert inner_tabs.active_tab.id == "tab1"
assert tabbed_content.active == "outer1"
await inner_tabs.clear()
assert inner_tabs.active_tab is None
assert tabbed_content.active == "outer1"
async def test_disabling_tab_within_tabbed_content_stays_isolated():
"""Disabling a tab within a tab pane should not affect the TabbedContent."""
class TabsNestedInTabbedContent(App):
def compose(self) -> ComposeResult:
with TabbedContent():
with TabPane("TabbedContent", id="duplicate"):
yield Tabs(
Tab("Tab1", id="duplicate"),
Tab("Tab2", id="stay-enabled"),
id="test-tabs",
)
app = TabsNestedInTabbedContent()
async with app.run_test() as pilot:
assert app.query_one("Tab#duplicate").disabled is False
assert app.query_one("TabPane#duplicate").disabled is False
app.query_one("#test-tabs", Tabs).disable("duplicate")
await pilot.pause()
assert app.query_one("Tab#duplicate").disabled is True
assert app.query_one("TabPane#duplicate").disabled is False
|