Skip to content

Commit

Permalink
Correct iOS test failure caused by focus.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed May 24, 2023
1 parent 3ad5352 commit e687898
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
7 changes: 6 additions & 1 deletion iOS/src/toga_iOS/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,13 @@ def get_enabled(self):
def set_enabled(self, value):
self.native.setEnabled(value)

@property
def has_focus(self):
return self.native.isFirstResponder

def focus(self):
self.native.becomeFirstResponder()
if not self.has_focus:
self.native.becomeFirstResponder()

def get_tab_index(self):
self.interface.factory.not_implemented("Widget.get_tab_index()")
Expand Down
2 changes: 1 addition & 1 deletion iOS/src/toga_iOS/widgets/multilinetextinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def get_value(self):

def set_value(self, value):
self.native.text = value
self.placeholder_label.setHidden(len(self.native.text) > 0)
self.placeholder_label.setHidden(self.has_focus or len(self.native.text) > 0)
self.interface.on_change(None)

def set_color(self, value):
Expand Down
46 changes: 30 additions & 16 deletions testbed/tests/widgets/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,8 @@ async def test_placeholder(widget, probe):

async def test_placeholder_focus(widget, probe):
"Placeholders interact correctly with focus changes"

widget.value = ""
widget.placeholder = "replacement"
widget.placeholder = "placeholder"
hides_on_focus = probe.placeholder_hides_on_focus

# Placeholder visibility can be focus dependent, so add another
Expand All @@ -162,58 +161,73 @@ async def test_placeholder_focus(widget, probe):
widget.focus()
await probe.redraw("Widget has focus")
assert widget.value == ""
assert widget.placeholder == "replacement"
assert probe.value == "" if hides_on_focus else "replacement"
assert widget.placeholder == "placeholder"
assert probe.value == "" if hides_on_focus else "placeholder"
assert probe.placeholder_visible == (not hides_on_focus)

# Give a different widget focus; this will show the placeholder
other.focus()
await probe.redraw("Widget has lost focus")
assert widget.value == ""
assert widget.placeholder == "replacement"
assert probe.value == "replacement"
assert widget.placeholder == "placeholder"
assert probe.value == "placeholder"
assert probe.placeholder_visible

# Give the widget focus, again
widget.focus()
await probe.redraw("Widget has focus; placeholder may not be visible")
assert widget.value == ""
assert widget.placeholder == "replacement"
assert probe.value == "" if hides_on_focus else "replacement"
assert widget.placeholder == "placeholder"
assert probe.value == "" if hides_on_focus else "placeholder"
assert probe.placeholder_visible == (not hides_on_focus)

# Change the placeholder text while the widget has focus
widget.placeholder = "placeholder"
await probe.redraw("Widget placeholder should be 'placeholder'")
widget.placeholder = "replacement"
await probe.redraw("Widget placeholder should be 'replacement'")
assert widget.value == ""
assert widget.placeholder == "placeholder"
assert probe.value == "" if hides_on_focus else "placeholder"
assert widget.placeholder == "replacement"
assert probe.value == "" if hides_on_focus else "replacement"
assert probe.placeholder_visible == (not hides_on_focus)

# Give a different widget focus; this will show the placeholder
other.focus()
await probe.redraw("Widget has lost focus; placeholder should be visible")
assert widget.value == ""
assert widget.placeholder == "placeholder"
assert probe.value == "placeholder"
assert widget.placeholder == "replacement"
assert probe.value == "replacement"
assert probe.placeholder_visible

# Focus in and out while a value is set.
widget.value = "example"
widget.focus()
await probe.redraw("Widget has focus; value is set")
assert widget.value == "example"
assert widget.placeholder == "placeholder"
assert widget.placeholder == "replacement"
assert probe.value == "example"
assert not probe.placeholder_visible

other.focus()
await probe.redraw("Widget has lost focus, value is set")
assert widget.value == "example"
assert widget.placeholder == "placeholder"
assert widget.placeholder == "replacement"
assert probe.value == "example"
assert not probe.placeholder_visible

# Value cleared while focus is set
widget.focus()
await probe.redraw("Widget has focus; value is set")
assert widget.value == "example"
assert widget.placeholder == "replacement"
assert probe.value == "example"
assert not probe.placeholder_visible

widget.value = ""
await probe.redraw("Value has been cleared")
assert widget.value == ""
assert widget.placeholder == "replacement"
assert probe.value == "" if hides_on_focus else "replacement"
assert probe.placeholder_visible == (not hides_on_focus)


async def test_placeholder_color(widget, probe):
"Placeholders interact correctly with custom colors"
Expand Down
1 change: 1 addition & 0 deletions testbed/tests/widgets/test_multilinetextinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ async def test_on_change_handler(widget, probe):
# Install a handler, and give the widget focus.
handler = Mock()
widget.on_change = handler
widget.placeholder = "placeholder"
widget.focus()

# Programmatic value changes trigger the event handler
Expand Down

0 comments on commit e687898

Please sign in to comment.