Skip to content

Commit

Permalink
unify and add missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 authored and samschott committed Sep 14, 2024
1 parent e263d9b commit 5a6248b
Show file tree
Hide file tree
Showing 19 changed files with 146 additions and 56 deletions.
8 changes: 4 additions & 4 deletions testbed/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@

# Use this for widgets or tests which are not supported on some platforms, but could be
# supported in the future.
def skip_on_platforms(*platforms):
def skip_on_platforms(*platforms, reason=None):
current_platform = toga.platform.current_platform
if current_platform in platforms:
skip(f"not yet implemented on {current_platform}")
skip(reason or f"not yet implemented on {current_platform}")


# Use this for widgets or tests which are not supported on some platforms, and will not
# be supported in the foreseeable future.
def xfail_on_platforms(*platforms):
def xfail_on_platforms(*platforms, reason=None):
current_platform = toga.platform.current_platform
if current_platform in platforms:
skip(f"not applicable on {current_platform}")
skip(reason or f"not applicable on {current_platform}")


@fixture(autouse=True)
Expand Down
2 changes: 1 addition & 1 deletion testbed/tests/widgets/test_activityindicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async def widget():


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

widget = toga.ActivityIndicator()
ref = weakref.ref(widget)
Expand Down
3 changes: 3 additions & 0 deletions testbed/tests/widgets/test_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import toga
from toga.style import Pack

from ..conftest import skip_on_platforms
from .properties import ( # noqa: F401
test_background_color,
test_background_color_reset,
Expand All @@ -21,6 +22,8 @@ async def widget():


async def test_cleanup():
skip_on_platforms("ios", reason="Leaks memory")

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

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

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

from ..conftest import skip_on_platforms
from .properties import ( # noqa: F401
test_background_color,
test_background_color_reset,
Expand Down Expand Up @@ -108,6 +111,18 @@ def assert_pixel(image, x, y, color):
assert image.getpixel((x, y)) == color


async def test_cleanup():
skip_on_platforms("macOS", reason="Segfaults on macOS")

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

del widget
gc.collect()

assert ref() is None


async def test_resize(widget, probe, on_resize_handler):
"Resizing the widget causes on-resize events"
# Make the canvas visible against window background.
Expand Down
15 changes: 15 additions & 0 deletions testbed/tests/widgets/test_detailedlist.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import gc
import weakref
from unittest.mock import Mock

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

from ..conftest import skip_on_platforms
from .properties import ( # noqa: F401
test_enable_noop,
test_flex_widget_size,
Expand Down Expand Up @@ -74,6 +77,18 @@ async def widget(
)


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

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

del widget
gc.collect()

assert ref() is None


async def test_scroll(widget, probe):
"""The detailedList can be scrolled"""

Expand Down
2 changes: 0 additions & 2 deletions testbed/tests/widgets/test_divider.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ async def widget():


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

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

Expand Down
12 changes: 12 additions & 0 deletions testbed/tests/widgets/test_mapview.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio
import gc
import platform
import weakref
from time import time
from unittest.mock import Mock

Expand Down Expand Up @@ -57,6 +59,16 @@ async def widget(on_select):
toga.App.app._gc_protector.append(widget)


async def test_cleanup():
widget = toga.MapView()
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None


# The next two tests fail about 75% of the time in the macOS x86_64 CI configuration.
# The failure mode appears to be that the widget *exists*, but doesn't respond to
# changes in location or zoom. I've been unable to reproduce this in actual testing on
Expand Down
15 changes: 9 additions & 6 deletions testbed/tests/widgets/test_multilinetextinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import toga
from toga.style import Pack

from ..conftest import skip_on_platforms
from .properties import ( # noqa: F401
test_alignment,
test_background_color,
Expand Down Expand Up @@ -39,7 +40,15 @@ async def widget():
return toga.MultilineTextInput(value="Hello", style=Pack(flex=1))


@pytest.fixture
def verify_font_sizes():
# We can't verify font sizes inside the MultilineTextInput
return False, False


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

widget = toga.MultilineTextInput(value="Hello")
ref = weakref.ref(widget)

Expand All @@ -49,12 +58,6 @@ async def test_cleanup():
assert ref() is None


@pytest.fixture
def verify_font_sizes():
# We can't verify font sizes inside the MultilineTextInput
return False, False


async def test_scroll_position(widget, probe):
"The widget can be programmatically scrolled."
# The document initially fits within the visible area.
Expand Down
20 changes: 10 additions & 10 deletions testbed/tests/widgets/test_numberinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,6 @@ async def widget():
return toga.NumberInput(value="1.23", step="0.01")


async def test_cleanup():
widget = toga.NumberInput(value="1.23", step="0.01")
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None


@pytest.fixture
def verify_font_sizes():
# We can't verify font width inside the TextInput
Expand All @@ -53,6 +43,16 @@ def verify_focus_handlers():
return False


async def test_cleanup():
widget = toga.NumberInput(value="1.23", step="0.01")
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None


async def test_on_change_handler(widget, probe):
"The on_change handler is triggered when the user types."
widget.step = "0.01"
Expand Down
3 changes: 3 additions & 0 deletions testbed/tests/widgets/test_optioncontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from toga.colors import CORNFLOWERBLUE, GOLDENROD, REBECCAPURPLE, SEAGREEN
from toga.style.pack import Pack

from ..conftest import skip_on_platforms
from .probe import get_probe
from .properties import ( # noqa: F401
test_enable_noop,
Expand Down Expand Up @@ -74,6 +75,8 @@ async def widget(content1, content2, content3, on_select_handler):


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

widget = toga.OptionContainer(content=[("Tab 1", toga.Box())])
ref = weakref.ref(widget)

Expand Down
12 changes: 6 additions & 6 deletions testbed/tests/widgets/test_passwordinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ async def widget():
return toga.PasswordInput(value="sekrit")


@pytest.fixture
def verify_font_sizes():
# We can't verify font width inside the TextInput
return False, True


async def test_cleanup():
widget = toga.PasswordInput(value="sekrit")
ref = weakref.ref(widget)
Expand All @@ -51,12 +57,6 @@ async def test_cleanup():
assert ref() is None


@pytest.fixture
def verify_font_sizes():
# We can't verify font width inside the TextInput
return False, True


async def test_value_hidden(widget, probe):
"Value should always be hidden in a PasswordInput"
assert probe.value_hidden
Expand Down
3 changes: 3 additions & 0 deletions testbed/tests/widgets/test_scrollcontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from toga.colors import CORNFLOWERBLUE, REBECCAPURPLE, TRANSPARENT
from toga.style.pack import COLUMN, ROW, Pack

from ..conftest import skip_on_platforms
from .properties import ( # noqa: F401
test_background_color,
test_background_color_reset,
Expand Down Expand Up @@ -75,6 +76,8 @@ async def widget(content, on_scroll):


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

widget = toga.ScrollContainer(content=toga.Box())
ref = weakref.ref(widget)

Expand Down
23 changes: 13 additions & 10 deletions testbed/tests/widgets/test_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from toga.constants import CENTER
from toga.sources import ListSource

from ..conftest import skip_on_platforms
from .properties import ( # noqa: F401
test_alignment,
test_background_color,
Expand Down Expand Up @@ -42,16 +43,6 @@ async def widget():
return toga.Selection(items=["first", "second", "third"])


async def test_cleanup():
widget = toga.Selection(items=["first", "second", "third"])
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None


@pytest.fixture
def verify_font_sizes():
# Font size does not affect the width of this widget.
Expand All @@ -63,6 +54,18 @@ def verify_vertical_alignment():
return CENTER


async def test_cleanup():
skip_on_platforms("windows", reason="Leaks memory")

widget = toga.Selection(items=["first", "second", "third"])
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None


async def test_item_titles(widget, probe):
"""The selection is able to build display titles from a range of data types"""
on_change_handler = Mock()
Expand Down
14 changes: 7 additions & 7 deletions testbed/tests/widgets/test_slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ async def widget():
return toga.Slider()


@fixture
def on_change(widget):
handler = Mock()
widget.on_change = handler
return handler


async def test_cleanup():
widget = toga.Slider()
ref = weakref.ref(widget)
Expand All @@ -46,13 +53,6 @@ async def test_cleanup():
assert ref() is None


@fixture
def on_change(widget):
handler = Mock()
widget.on_change = handler
return handler


async def test_init(widget, probe):
assert widget.value == 0.5
assert widget.min == 0
Expand Down
3 changes: 3 additions & 0 deletions testbed/tests/widgets/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import toga

from ..conftest import skip_on_platforms
from ..data import TEXTS
from .properties import ( # noqa: F401
test_color,
Expand All @@ -30,6 +31,8 @@ async def widget():


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

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

Expand Down
14 changes: 14 additions & 0 deletions testbed/tests/widgets/test_table.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import contextlib
import gc
import weakref
from unittest.mock import Mock

import pytest
Expand Down Expand Up @@ -111,6 +113,18 @@ async def multiselect_probe(main_window, multiselect_widget):
main_window.content = old_content


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

widget = toga.Table(headings=["A", "B", "C"])
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None


async def test_scroll(widget, probe):
"""The table can be scrolled"""

Expand Down
Loading

0 comments on commit 5a6248b

Please sign in to comment.