Skip to content

Commit

Permalink
allow passing in args constructors to ensure that all widgets are cre…
Browse files Browse the repository at this point in the history
…ated on the same thread
  • Loading branch information
samschott committed Sep 15, 2024
1 parent 9e98e71 commit 50d25a9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
22 changes: 17 additions & 5 deletions testbed/tests/widgets/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import gc
import weakref
from typing import Callable, Iterable, ParamSpec, Union
from unittest.mock import Mock

from pytest import fixture
Expand All @@ -10,6 +11,8 @@
from ..conftest import skip_on_platforms, xfail_on_platforms
from .probe import get_probe

P = ParamSpec("P")


@fixture
async def widget():
Expand Down Expand Up @@ -84,18 +87,27 @@ def verify_vertical_alignment():


def build_cleanup_test(
widget_class, args=None, kwargs=None, skip_platforms=(), xfail_platforms=()
widget_constructor: Callable[[P], toga.Widget],
args: Union[P.args, Callable[..., P.args]] = (),
kwargs: Union[P.kwargs, Callable[..., P.args], None] = None,
skip_platforms: Iterable[str] = (),
xfail_platforms: Iterable[str] = (),
):
async def test_cleanup():
nonlocal args, kwargs

skip_on_platforms(*skip_platforms)
xfail_on_platforms(*xfail_platforms, reason="Leaks memory")

widget = widget_class(
*(args if args is not None else ()),
**(kwargs if kwargs is not None else {}),
)
if callable(args):
args = args()

if callable(kwargs):
kwargs = kwargs()
elif args is None:
kwargs = {}

widget = widget_constructor(*args, **kwargs)
ref = weakref.ref(widget)

# Args or kwargs may hold a backref to the widget itself, for example if they
Expand Down
2 changes: 1 addition & 1 deletion testbed/tests/widgets/test_optioncontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async def widget(content1, content2, content3, on_select_handler):

test_cleanup = build_cleanup_test(
toga.OptionContainer,
kwargs={"content": [("Tab 1", toga.Box())]},
kwargs=lambda: {"content": [("Tab 1", toga.Box())]},
xfail_platforms=("android", "iOS", "linux"),
)

Expand Down
2 changes: 1 addition & 1 deletion testbed/tests/widgets/test_scrollcontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async def widget(content, on_scroll):

test_cleanup = build_cleanup_test(
toga.ScrollContainer,
kwargs={"content": toga.Box()},
kwargs=lambda: {"content": toga.Box()},
xfail_platforms=("android", "iOS", "linux"),
)

Expand Down
2 changes: 1 addition & 1 deletion testbed/tests/widgets/test_splitcontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async def widget(content1, content2):

test_cleanup = build_cleanup_test(
toga.SplitContainer,
kwargs={"content": [toga.Box(), toga.Box()]},
kwargs=lambda: {"content": [toga.Box(), toga.Box()]},
skip_platforms=("android", "iOS"),
)

Expand Down

0 comments on commit 50d25a9

Please sign in to comment.