Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion web-testbed/tests/tests_backend/widgets/passwordinput.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .textinput import TextInputProbe


class PasswordInputProbe(TextInputProbe):
pass
pass
7 changes: 4 additions & 3 deletions web-testbed/tests/tests_backend/widgets/switch.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from .base import SimpleProbe


class SwitchProbe(SimpleProbe):
@property
def text(self):
page = self._page()
return page.run_coro(lambda p: p.locator(f"#{self.dom_id}").text_content())

@property
def height(self):
page = self._page()
box = page.run_coro(lambda p: p.locator(f"#{self.dom_id}").bounding_box())
return None if box is None else box["height"]

async def press(self):
page = self._page()
page.run_coro(lambda p: p.locator(f"#{self.dom_id}").click())
page.run_coro(lambda p: p.locator(f"#{self.dom_id}").click())
29 changes: 19 additions & 10 deletions web-testbed/tests/tests_backend/widgets/textinput.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .base import SimpleProbe


class TextInputProbe(SimpleProbe):
def __init__(self, widget):
super().__init__(widget)
Expand All @@ -11,14 +12,17 @@ def _read_remote_value(self) -> str:

@property
def value(self):
page = self._page()
page = self._page()

def _run(p):
async def steps():
root = p.locator(f"#{self.dom_id}")
inner = root.locator("input,textarea").first
target = inner if (await inner.count()) > 0 else root
return await target.input_value()

return steps()

return page.run_coro(_run)

@property
Expand All @@ -35,20 +39,26 @@ async def steps():
t = await target.get_attribute("type")
if (t or "").lower() == "password":
return True

return steps()

return bool(page.run_coro(_run))


async def type_character(self, ch: str):
page = self._page()

def _run(p):
async def steps():
root = p.locator(f"#{self.dom_id}")
target = (await root.locator("input,textarea").first.count()) and root.locator("input,textarea").first or root
try: await target.focus()
except Exception: pass
target = (
(await root.locator("input,textarea").first.count())
and root.locator("input,textarea").first
or root
)
try:
await target.focus()
except Exception:
pass

if ch == "\n":
await target.press("Enter")
Expand All @@ -58,6 +68,7 @@ async def steps():
await target.press("Backspace")
else:
await target.type(ch)

return steps()

page.run_coro(_run)
Expand All @@ -66,19 +77,19 @@ async def undo(self):
page = self._page()
page.run_coro(lambda p: p.locator(f"#{self.dom_id}").press("Control+Z"))


async def redo(self):
page = self._page()
page.run_coro(lambda p: p.locator(f"#{self.dom_id}").press("Control+Y"))

def set_cursor_at_end(self):
page = self._page()
page.run_coro(
lambda p: p.evaluate(
"""(sel) => {
const root = document.querySelector(sel);
if (!root) return;
const el = root.matches('input,textarea') ? root : root.querySelector('input,textarea');
const el = root.matches('input,textarea') ?
root : root.querySelector('input,textarea');
if (!el) return;
el.focus();
const len = (el.value ?? '').length;
Expand All @@ -95,5 +106,3 @@ async def redraw(self, _msg: str = ""):
page = self._page()
page.run_coro(lambda p: p.wait_for_timeout(0))
self._last_remote_value = self._read_remote_value()


3 changes: 1 addition & 2 deletions web-testbed/tests/widgets/test_passwordinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import toga



@pytest.fixture
async def widget():
return toga.PasswordInput(value="sekrit")
Expand All @@ -25,4 +24,4 @@ async def test_value_hidden(widget, probe):

widget.value = "something"
await probe.redraw("Value changed from empty to non-empty")
assert probe.value_hidden
assert probe.value_hidden
12 changes: 6 additions & 6 deletions web-testbed/tests/widgets/test_textinput.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from unittest.mock import Mock, call
import toga
import pytest

import pytest
from tests.data import TEXTS

import toga
from toga.constants import CENTER
from toga.style import Pack
from toga.style.pack import RIGHT, SERIF

from tests.data import TEXTS


@pytest.fixture
async def widget():
Expand Down Expand Up @@ -69,6 +67,7 @@ async def test_on_change_programmatic(widget, probe, on_change, focused, placeho
on_change.assert_called_once_with(widget)
on_change.reset_mock()


async def test_on_change_user(widget, probe, on_change):
"The on_change handler is triggered on user input"
# This test simulates typing, so the widget must be focused.
Expand Down Expand Up @@ -266,6 +265,7 @@ async def test_no_event_on_initialization(widget, probe, on_change):
on_change.assert_not_called()
on_change.reset_mock()


async def test_no_event_on_style_change(widget, probe, on_change):
"The widget doesn't fire on_change events on text style changes."
# font changes
Expand All @@ -284,4 +284,4 @@ async def test_no_event_on_style_change(widget, probe, on_change):
widget.style.color = "#0000FF"
await probe.redraw("Text color has been changed")
on_change.assert_not_called()
on_change.reset_mock()
on_change.reset_mock()