Skip to content

Commit

Permalink
Merge pull request #1755 from freakboy3742/gtk-probe
Browse files Browse the repository at this point in the history
Add GTK probe.
  • Loading branch information
mhsmith authored Jan 29, 2023
2 parents ecfca36 + 997102c commit 4a35692
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 8 deletions.
1 change: 1 addition & 0 deletions changes/1755.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A GTK GUI test probe was added.
1 change: 1 addition & 0 deletions gtk/MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ include CONTRIBUTING.md
include LICENSE
include README.rst
recursive-include tests *.py
recursive-include tests_backend *.py
22 changes: 16 additions & 6 deletions gtk/src/toga_gtk/libs/styles.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from toga.colors import TRANSPARENT

from . import Gtk

TOGA_DEFAULT_STYLES = b"""
Expand Down Expand Up @@ -37,12 +39,20 @@ def get_color_css(value):


def get_bg_color_css(value):
return (
".toga-bg-color {"
f"background-color: rgba({value.r}, {value.g}, {value.b}, {value.a});"
"background-image: none;"
"}"
)
if value == TRANSPARENT:
return (
".toga-bg-color {"
"background-color: rgba(0, 0, 0, 0);"
"background-image: none;"
"}"
)
else:
return (
".toga-bg-color {"
f"background-color: rgba({value.r}, {value.g}, {value.b}, {value.a});"
"background-image: none;"
"}"
)


def get_font_css(value):
Expand Down
Empty file added gtk/tests_backend/__init__.py
Empty file.
Empty file.
47 changes: 47 additions & 0 deletions gtk/tests_backend/widgets/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from pytest import skip

from toga_gtk.libs import Gtk


class SimpleProbe:
def __init__(self, widget):
self.widget = widget
self.impl = widget._impl
self.native = widget._impl.native
assert isinstance(self.native, self.native_class)

def assert_container(self, container):
container_native = container._impl.native
for control in container_native.get_children():
if control == self.native:
break
else:
raise ValueError(f"cannot find {self.native} in {container_native}")

async def redraw(self):
"""Request a redraw of the app, waiting until that redraw has completed."""
# Refresh the layout
self.widget.window.content.refresh()

# Force a repaint
while Gtk.events_pending():
Gtk.main_iteration_do(blocking=False)

@property
def enabled(self):
skip("enabled probe not implemented")

@property
def hidden(self):
skip("hidden probe not implemented")

@property
def width(self):
return self.native.get_allocation().width

@property
def height(self):
return self.native.get_allocation().height

def press(self):
skip("Press probe not implemented")
25 changes: 25 additions & 0 deletions gtk/tests_backend/widgets/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from pytest import skip

from toga_gtk.libs import Gtk

from .base import SimpleProbe


class ButtonProbe(SimpleProbe):
native_class = Gtk.Button

@property
def text(self):
return self.native.get_label()

@property
def color(self):
skip("color probe not implemented")

@property
def font(self):
skip("font probe not implemented")

@property
def background_color(self):
skip("background color probe not implemented")
29 changes: 29 additions & 0 deletions gtk/tests_backend/widgets/label.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from pytest import skip

from toga_gtk.libs import Gtk

from .base import SimpleProbe


class LabelProbe(SimpleProbe):
native_class = Gtk.Label

@property
def text(self):
return self.native.get_label()

@property
def color(self):
skip("color probe not implemented")

@property
def background_color(self):
skip("background color probe not implemented")

@property
def font(self):
skip("font probe not implemented")

@property
def alignment(self):
skip("alignment probe not implemented")
34 changes: 34 additions & 0 deletions gtk/tests_backend/widgets/properties.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from dataclasses import dataclass

from toga.fonts import NORMAL
from toga.style.pack import CENTER, JUSTIFY, LEFT, RIGHT
from toga_gtk.libs import Gtk, Pango


def toga_color(color):
return color


@dataclass
class Font:
family: str
size: int
style: str = NORMAL
variant: str = NORMAL
weight: str = NORMAL


def toga_font(font):
return Font(
family=font.get_family(),
size=font.get_size() / Pango.SCALE,
)


def toga_alignment(alignment):
return {
(0.0, Gtk.Justification.LEFT): LEFT,
(1.0, Gtk.Justification.RIGHT): RIGHT,
(0.5, Gtk.Justification.CENTER): CENTER,
(0.0, Gtk.Justification.FILL): JUSTIFY,
}[alignment]
22 changes: 22 additions & 0 deletions gtk/tests_backend/widgets/slider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from toga_gtk.libs import Gtk

from .base import SimpleProbe


class SliderProbe(SimpleProbe):
native_class = Gtk.Scale

@property
def position(self):
return (self.native.get_value() - self._min) / (self._max - self._min)

def change(self, position):
self.native.set_value(self._min + round(position * (self._max - self._min)))

@property
def _min(self):
return self.impl.adj.get_lower()

@property
def _max(self):
return self.impl.adj.get_upper()
3 changes: 3 additions & 0 deletions testbed/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ test_sources = [
]

[tool.briefcase.app.testbed.linux]
test_sources = [
"../gtk/tests_backend",
]
requires = [
"../gtk",
]
Expand Down
10 changes: 10 additions & 0 deletions testbed/tests/widgets/test_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ async def widget():
return toga.Button("Hello")


test_text_width_change = mark.skipif(
current_platform in {"linux"},
reason="resizes not applying correctly",
)(test_text_width_change)


async def test_press(widget, probe):
# Press the button before installing a handler
probe.press()
Expand Down Expand Up @@ -62,6 +68,10 @@ async def test_background_color_transparent(widget, probe):
current_platform in {"android", "iOS"},
reason="await redraw() not implemented",
)
@mark.skipif(
current_platform in {"linux"},
reason="resizes not applying correctly",
)
async def test_button_size(widget, probe):
"Check that the button resizes"
# Container is initially a non-flex row box.
Expand Down
7 changes: 7 additions & 0 deletions testbed/tests/widgets/test_label.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pytest import approx, fixture, mark

import toga
from toga.platform import current_platform
from toga.style.pack import CENTER, COLUMN, JUSTIFY, LEFT, LTR, RIGHT, RTL

from .properties import ( # noqa: F401
Expand All @@ -20,6 +21,12 @@ async def widget():
return toga.Label("hello")


test_text_width_change = mark.skipif(
current_platform in {"linux"},
reason="resizes not applying correctly",
)(test_text_width_change)


# TODO: a `width` test, for any widget whose width depends on its text.
@mark.skip("changing text does not trigger a refresh (#1289)")
async def test_multiline(widget, probe):
Expand Down
8 changes: 6 additions & 2 deletions testbed/tests/widgets/test_slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async def test_init(widget, probe, on_change):


@mark.skipif(
current_platform in ["windows", "macOS", "iOS"],
current_platform in ["windows", "macOS", "iOS", "linux"],
reason="on_change called 2 times",
)
@mark.skipif(current_platform == "android", reason="position is 0.0")
Expand All @@ -46,9 +46,13 @@ async def test_value(widget, probe, on_change):


@mark.skipif(
current_platform in ["android", "windows", "macOS", "iOS"],
current_platform in {"android", "windows", "macOS", "iOS"},
reason="on_change called 0 times",
)
@mark.skipif(
current_platform in {"linux"},
reason="min/max value not correct",
)
async def test_change(widget, probe, on_change):
for scale in SCALES:
widget.range = (0, scale)
Expand Down

0 comments on commit 4a35692

Please sign in to comment.