File: test_suspend.py

package info (click to toggle)
textual 2.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,084 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (63 lines) | stat: -rw-r--r-- 2,119 bytes parent folder | download | duplicates (2)
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
import sys

import pytest

from textual.app import App, SuspendNotSupported
from textual.drivers.headless_driver import HeadlessDriver


async def test_suspend_not_supported() -> None:
    """Suspending when not supported should raise an error."""
    async with App().run_test() as pilot:
        # Pilot uses the headless driver, the headless driver doesn't
        # support suspend, and so...
        with pytest.raises(SuspendNotSupported):
            with pilot.app.suspend():
                pass


async def test_suspend_supported(capfd: pytest.CaptureFixture[str]) -> None:
    """Suspending when supported should call the relevant driver methods."""

    calls: set[str] = set()

    class HeadlessSuspendDriver(HeadlessDriver):
        @property
        def is_headless(self) -> bool:
            return False

        @property
        def can_suspend(self) -> bool:
            return True

        def suspend_application_mode(self) -> None:
            nonlocal calls
            calls.add("suspend")

        def resume_application_mode(self) -> None:
            nonlocal calls
            calls.add("resume")

    class SuspendApp(App[None]):
        def on_suspend(self, _) -> None:
            nonlocal calls
            calls.add("suspend signal")

        def on_resume(self, _) -> None:
            nonlocal calls
            calls.add("resume signal")

        def on_mount(self) -> None:
            self.app_suspend_signal.subscribe(self, self.on_suspend, immediate=True)
            self.app_resume_signal.subscribe(self, self.on_resume, immediate=True)

    async with SuspendApp(driver_class=HeadlessSuspendDriver).run_test(
        headless=False
    ) as pilot:
        calls = set()
        with pilot.app.suspend():
            _ = capfd.readouterr()  # Clear the existing buffer.
            print("USE THEM TOGETHER.", end="", flush=True)
            print("USE THEM IN PEACE.", file=sys.stderr, end="", flush=True)
            assert ("USE THEM TOGETHER.", "USE THEM IN PEACE.") == capfd.readouterr()
        assert calls == {"suspend", "resume", "suspend signal", "resume signal"}