Skip to content

Commit

Permalink
move shared cleanup test code to a central location
Browse files Browse the repository at this point in the history
  • Loading branch information
samschott committed Sep 15, 2024
1 parent 6df10ab commit 2ae8d42
Show file tree
Hide file tree
Showing 26 changed files with 133 additions and 327 deletions.
28 changes: 28 additions & 0 deletions testbed/tests/widgets/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import gc
import weakref
from unittest.mock import Mock

from pytest import fixture

import toga
from toga.style.pack import TOP

from ..conftest import skip_on_platforms, xfail_on_platforms
from .probe import get_probe


Expand Down Expand Up @@ -78,3 +81,28 @@ def verify_focus_handlers():
def verify_vertical_alignment():
"""The widget's default vertical alignment"""
return TOP


def build_cleanup_test(
widget_class, args=None, kwargs=None, skip_platforms=(), xfail_platforms=()
):
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 {}),
)
ref = weakref.ref(widget)

# Args or kwargs may hold a backref to the widget itself, for example if they
# are widget content. Ensure that they are deleted before garbage collection.
del widget, args, kwargs
gc.collect()

assert ref() is None

return test_cleanup
17 changes: 4 additions & 13 deletions testbed/tests/widgets/test_activityindicator.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import gc
import weakref

import pytest

import toga

from ..conftest import skip_on_platforms
from .conftest import build_cleanup_test
from .properties import ( # noqa: F401
test_enable_noop,
test_focus_noop,
Expand All @@ -18,16 +16,9 @@ async def widget():
return toga.ActivityIndicator()


async def test_cleanup():
skip_on_platforms("android", "windows")

widget = toga.ActivityIndicator()
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None
test_cleanup = build_cleanup_test(
toga.ActivityIndicator, skip_platforms=("android", "windows")
)


async def test_start_stop(widget, probe):
Expand Down
16 changes: 2 additions & 14 deletions testbed/tests/widgets/test_box.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import gc
import weakref

import pytest

import toga
from toga.style import Pack

from ..conftest import xfail_on_platforms
from .conftest import build_cleanup_test
from .properties import ( # noqa: F401
test_background_color,
test_background_color_reset,
Expand All @@ -21,13 +18,4 @@ async def widget():
return toga.Box(style=Pack(width=100, height=200))


async def test_cleanup():
xfail_on_platforms("iOS", reason="Leaks memory")

widget = toga.Box(style=Pack(width=100, height=200))
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None
test_cleanup = build_cleanup_test(toga.Box, xfail_platforms=("iOS",))
17 changes: 4 additions & 13 deletions testbed/tests/widgets/test_button.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import gc
import weakref
from unittest.mock import Mock

from pytest import approx, fixture
Expand All @@ -8,8 +6,8 @@
from toga.colors import TRANSPARENT

from ..assertions import assert_color
from ..conftest import xfail_on_platforms
from ..data import TEXTS
from .conftest import build_cleanup_test
from .properties import ( # noqa: F401
test_background_color,
test_background_color_reset,
Expand All @@ -34,16 +32,9 @@ async def widget():
return toga.Button("Hello")


async def test_cleanup():
xfail_on_platforms("android", reason="Leaks memory")

widget = toga.Button("Hello")
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None
test_cleanup = build_cleanup_test(
toga.Button, args=("Hello",), skip_platforms=("android",)
)


async def test_text(widget, probe):
Expand Down
15 changes: 2 additions & 13 deletions testbed/tests/widgets/test_canvas.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import gc
import math
import os
import weakref
from math import pi, radians
from unittest.mock import Mock, call

Expand All @@ -23,7 +21,7 @@
from toga.fonts import BOLD
from toga.style.pack import SYSTEM, Pack

from ..conftest import xfail_on_platforms
from .conftest import build_cleanup_test
from .properties import ( # noqa: F401
test_background_color,
test_background_color_reset,
Expand Down Expand Up @@ -111,16 +109,7 @@ def assert_pixel(image, x, y, color):
assert image.getpixel((x, y)) == color


async def test_cleanup():
xfail_on_platforms("android", reason="Leaks memory")

widget = toga.Canvas()
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None
test_cleanup = build_cleanup_test(toga.Canvas, xfail_platforms=("android",))


async def test_resize(widget, probe, on_resize_handler):
Expand Down
21 changes: 7 additions & 14 deletions testbed/tests/widgets/test_dateinput.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import gc
import weakref
from datetime import date, datetime, timedelta
from unittest.mock import Mock, call

from pytest import fixture

import toga

from ..conftest import skip_on_platforms, xfail_on_platforms
from ..conftest import skip_on_platforms
from .conftest import build_cleanup_test
from .properties import ( # noqa: F401
test_background_color,
test_background_color_reset,
Expand Down Expand Up @@ -84,17 +83,11 @@ async def widget():
return toga.DateInput()


async def test_cleanup():
skip_on_platforms("macOS", "iOS", "linux")
xfail_on_platforms("android", reason="Leaks memory")

widget = toga.DateInput()
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None
test_cleanup = build_cleanup_test(
toga.DateInput,
skip_platforms=("macOS", "iOS", "linux"),
xfail_platforms=("android",),
)


async def test_init():
Expand Down
17 changes: 4 additions & 13 deletions testbed/tests/widgets/test_detailedlist.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import gc
import weakref
from unittest.mock import Mock

import pytest
Expand All @@ -8,7 +6,7 @@
from toga.sources import ListSource
from toga.style.pack import Pack

from ..conftest import xfail_on_platforms
from .conftest import build_cleanup_test
from .properties import ( # noqa: F401
test_enable_noop,
test_flex_widget_size,
Expand Down Expand Up @@ -77,16 +75,9 @@ async def widget(
)


async def test_cleanup():
xfail_on_platforms("android", "linux", reason="Leaks memory")

widget = toga.DetailedList()
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None
test_cleanup = build_cleanup_test(
toga.DetailedList, xfail_platforms=("android", "linux")
)


async def test_scroll(widget, probe):
Expand Down
16 changes: 2 additions & 14 deletions testbed/tests/widgets/test_divider.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import gc
import weakref

import pytest

import toga
from toga.constants import Direction
from toga.style.pack import COLUMN, ROW

from ..conftest import xfail_on_platforms
from .conftest import build_cleanup_test
from .properties import ( # noqa: F401
test_enable_noop,
test_focus_noop,
Expand All @@ -19,16 +16,7 @@ async def widget():
return toga.Divider()


async def test_cleanup():
xfail_on_platforms("iOS", reason="Leaks memory")

widget = toga.Divider()
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None
test_cleanup = build_cleanup_test(toga.Divider, xfail_platforms=("iOS",))


async def test_directions(widget, probe):
Expand Down
15 changes: 4 additions & 11 deletions testbed/tests/widgets/test_imageview.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import gc
import weakref

import pytest

import toga
from toga.style.pack import COLUMN, ROW

from .conftest import build_cleanup_test
from .properties import ( # noqa: F401
test_background_color,
test_background_color_reset,
Expand All @@ -20,14 +18,9 @@ async def widget():
return toga.ImageView(image="resources/sample.png")


async def test_cleanup():
widget = toga.ImageView(image="resources/sample.png")
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None
test_cleanup = build_cleanup_test(
toga.ImageView, kwargs={"image": "resources/sample.png"}, xfail_platforms=("iOS",)
)


async def test_implicit_size(widget, probe, container_probe):
Expand Down
13 changes: 2 additions & 11 deletions testbed/tests/widgets/test_label.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import gc
import weakref

from pytest import approx, fixture

import toga

from .conftest import build_cleanup_test
from .properties import ( # noqa: F401
test_alignment,
test_background_color,
Expand All @@ -27,14 +25,7 @@ async def widget():
return toga.Label("hello, this is a label")


async def test_cleanup():
widget = toga.Label("hello, this is a label")
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None
test_cleanup = build_cleanup_test(toga.Label, args=("hello, this is a label",))


async def test_multiline(widget, probe):
Expand Down
15 changes: 2 additions & 13 deletions testbed/tests/widgets/test_mapview.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import asyncio
import gc
import platform
import weakref
from time import time
from unittest.mock import Mock

Expand All @@ -10,7 +8,7 @@
import toga
from toga.style import Pack

from ..conftest import xfail_on_platforms
from .conftest import build_cleanup_test
from .properties import ( # noqa: F401
test_flex_widget_size,
)
Expand Down Expand Up @@ -60,16 +58,7 @@ async def widget(on_select):
toga.App.app._gc_protector.append(widget)


async def test_cleanup():
xfail_on_platforms("android", reason="Leaks memory")

widget = toga.MapView()
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None
test_cleanup = build_cleanup_test(toga.MapView, xfail_platforms=("android"))


# The next two tests fail about 75% of the time in the macOS x86_64 CI configuration.
Expand Down
Loading

0 comments on commit 2ae8d42

Please sign in to comment.