Skip to content

Commit

Permalink
add tests for widget garbage collection
Browse files Browse the repository at this point in the history
# Conflicts:
#	testbed/tests/widgets/test_webview.py
  • Loading branch information
samschott committed Jul 21, 2024
1 parent 47ce375 commit 784755f
Show file tree
Hide file tree
Showing 20 changed files with 264 additions and 2 deletions.
15 changes: 15 additions & 0 deletions testbed/tests/widgets/test_activityindicator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import gc
import weakref

import pytest

import toga
Expand All @@ -15,6 +18,18 @@ async def widget():
return toga.ActivityIndicator()


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

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

del widget
gc.collect()

assert ref() is None


async def test_start_stop(widget, probe):
"The activity indicator can be started and stopped"
# Widget should be initially stopped
Expand Down
13 changes: 13 additions & 0 deletions testbed/tests/widgets/test_box.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import gc
import weakref

import pytest

import toga
Expand All @@ -15,3 +18,13 @@
@pytest.fixture
async def widget():
return toga.Box(style=Pack(width=100, height=200))


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

del widget
gc.collect()

assert ref() is None
12 changes: 12 additions & 0 deletions testbed/tests/widgets/test_button.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import gc
import weakref
from unittest.mock import Mock

from pytest import approx, fixture
Expand Down Expand Up @@ -31,6 +33,16 @@ async def widget():
return toga.Button("Hello")


async def test_cleanup():
widget = toga.Button("Hello")
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None


async def test_text(widget, probe):
"The text displayed on a button can be changed"
initial_height = probe.height
Expand Down
14 changes: 14 additions & 0 deletions testbed/tests/widgets/test_dateinput.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import gc
import weakref
from datetime import date, datetime, timedelta
from unittest.mock import Mock, call

Expand Down Expand Up @@ -82,6 +84,18 @@ async def widget():
return toga.DateInput()


async def test_cleanup():
skip_on_platforms("macOS", "iOS", "linux")

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

del widget
gc.collect()

assert ref() is None


async def test_init():
"Properties can be set in the constructor"
skip_on_platforms("macOS", "iOS", "linux")
Expand Down
15 changes: 15 additions & 0 deletions testbed/tests/widgets/test_divider.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import gc
import weakref

import pytest

import toga
Expand All @@ -17,6 +20,18 @@ async def widget():
return toga.Divider()


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

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

del widget
gc.collect()

assert ref() is None


async def test_directions(widget, probe):
"The divider has the right size as direction is changed"
# Widget should be initially horizontal.
Expand Down
13 changes: 13 additions & 0 deletions testbed/tests/widgets/test_imageview.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import gc
import weakref

import pytest

import toga
Expand All @@ -17,6 +20,16 @@ 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


async def test_implicit_size(widget, probe, container_probe):
"""If the image view size is implicit, the image provides flexible size hints."""

Expand Down
13 changes: 13 additions & 0 deletions testbed/tests/widgets/test_label.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import gc
import weakref

from pytest import approx, fixture

import toga
Expand All @@ -24,6 +27,16 @@ 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


async def test_multiline(widget, probe):
"""If the label contains multiline text, it resizes vertically."""

Expand Down
13 changes: 13 additions & 0 deletions testbed/tests/widgets/test_multilinetextinput.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import gc
import weakref

import pytest

import toga
Expand Down Expand Up @@ -36,6 +39,16 @@ async def widget():
return toga.MultilineTextInput(value="Hello", style=Pack(flex=1))


async def test_cleanup():
widget = toga.MultilineTextInput(value="Hello")
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None


@pytest.fixture
def verify_font_sizes():
# We can't verify font sizes inside the MultilineTextInput
Expand Down
12 changes: 12 additions & 0 deletions testbed/tests/widgets/test_numberinput.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import gc
import weakref
from decimal import Decimal
from unittest.mock import Mock, call

Expand Down Expand Up @@ -30,6 +32,16 @@ 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 Down
14 changes: 14 additions & 0 deletions testbed/tests/widgets/test_optioncontainer.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 Down Expand Up @@ -71,6 +73,18 @@ async def widget(content1, content2, content3, on_select_handler):
)


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

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

del widget
gc.collect()

assert ref() is None


async def test_select_tab(
widget,
probe,
Expand Down
13 changes: 13 additions & 0 deletions testbed/tests/widgets/test_passwordinput.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import gc
import weakref

import pytest

import toga
Expand Down Expand Up @@ -38,6 +41,16 @@ async def widget():
return toga.PasswordInput(value="sekrit")


async def test_cleanup():
widget = toga.PasswordInput(value="sekrit")
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 Down
14 changes: 14 additions & 0 deletions testbed/tests/widgets/test_progressbar.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import asyncio
import gc
import weakref

import pytest

import toga
Expand All @@ -19,6 +23,16 @@ async def widget():
return toga.ProgressBar(max=100, value=5)


async def test_cleanup():
widget = toga.ProgressBar(max=100, value=5)
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None


async def test_start_stop_determinate(widget, probe):
"A determinate progress bar can be started and stopped"
# Widget should be initially stopped and determinate
Expand Down
12 changes: 12 additions & 0 deletions testbed/tests/widgets/test_scrollcontainer.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 Down Expand Up @@ -72,6 +74,16 @@ async def widget(content, on_scroll):
)


async def test_cleanup():
widget = toga.ScrollContainer(content=toga.Box())
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None


async def test_clear_content(widget, probe, small_content):
"Widget content can be cleared and reset"
assert probe.document_width == probe.width - probe.scrollbar_inset
Expand Down
12 changes: 12 additions & 0 deletions testbed/tests/widgets/test_selection.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 Down Expand Up @@ -40,6 +42,16 @@ 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 Down
12 changes: 12 additions & 0 deletions testbed/tests/widgets/test_slider.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import gc
import weakref
from math import pi
from unittest.mock import Mock

Expand Down Expand Up @@ -34,6 +36,16 @@ async def widget():
return toga.Slider()


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

del widget
gc.collect()

assert ref() is None


@fixture
def on_change(widget):
handler = Mock()
Expand Down
19 changes: 17 additions & 2 deletions testbed/tests/widgets/test_splitcontainer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import gc
import weakref

import pytest
from pytest import approx

Expand All @@ -6,7 +9,7 @@
from toga.constants import Direction
from toga.style.pack import Pack

from ..conftest import xfail_on_platforms
from ..conftest import skip_on_platforms
from .probe import get_probe
from .properties import ( # noqa: F401
test_enable_noop,
Expand Down Expand Up @@ -56,10 +59,22 @@ async def content3_probe(content3):

@pytest.fixture
async def widget(content1, content2):
xfail_on_platforms("android", "iOS")
skip_on_platforms("android", "iOS")
return toga.SplitContainer(content=[content1, content2], style=Pack(flex=1))


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

widget = toga.SplitContainer(content=[toga.Box(), toga.Box()])
ref = weakref.ref(widget)

del widget
gc.collect()

assert ref() is None


async def test_set_content(
widget,
probe,
Expand Down
Loading

0 comments on commit 784755f

Please sign in to comment.