Skip to content

Commit

Permalink
iOS Button tests to 100%.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Feb 25, 2023
1 parent da9c1ba commit 50c4fee
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 22 deletions.
3 changes: 2 additions & 1 deletion iOS/src/toga_iOS/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ def native_color(c):
try:
color = CACHE[c]
except KeyError:
# Color needs to be retained to be kept in the cache
color = UIColor.colorWithRed(
c.rgba.r / 255, green=c.rgba.g / 255, blue=c.rgba.b / 255, alpha=c.rgba.a
)
).retain()
CACHE[c] = color

return color
19 changes: 19 additions & 0 deletions iOS/src/toga_iOS/widgets/button.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from rubicon.objc import SEL, CGSize, objc_method, objc_property
from travertino.size import at_least

from toga.colors import TRANSPARENT
from toga_iOS.colors import native_color
from toga_iOS.libs import (
UIButton,
UIColor,
Expand Down Expand Up @@ -48,6 +50,23 @@ def set_on_press(self, handler):
# No special handling required.
pass

def set_color(self, color):
if color is None:
self.native.setTitleColor(
self.native.tintColor, forState=UIControlStateNormal
)
else:
self.native.setTitleColor(
native_color(color), forState=UIControlStateNormal
)

def set_background_color(self, color):
# By default, background color can't be changed
if color == TRANSPARENT or color is None:
self.native.backgroundColor = None
else:
self.native.backgroundColor = native_color(color)

def rehint(self):
fitting_size = self.native.systemLayoutSizeFittingSize(CGSize(0, 0))
self.interface.intrinsic.width = at_least(fitting_size.width)
Expand Down
10 changes: 4 additions & 6 deletions iOS/tests_backend/widgets/button.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from pytest import skip, xfail

from toga_iOS.libs import UIButton
from toga_iOS.libs import UIButton, UIControlStateNormal

from .base import SimpleProbe
from .properties import toga_font
from .properties import toga_color, toga_font


class ButtonProbe(SimpleProbe):
Expand All @@ -15,12 +13,12 @@ def text(self):

@property
def color(self):
xfail("Can't get/set the text color of a button on iOS")
return toga_color(self.native.titleColorForState(UIControlStateNormal))

@property
def font(self):
return toga_font(self.native.font)

@property
def background_color(self):
skip("Background color not supported yet")
return toga_color(self.native.backgroundColor)
2 changes: 0 additions & 2 deletions iOS/tests_backend/widgets/properties.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from ctypes import byref
from dataclasses import dataclass

from pytest import skip
from rubicon.objc import CGFloat

from toga.colors import rgba
Expand All @@ -17,7 +16,6 @@

def toga_color(color):
if color:
skip("Can't convert UIColor to toga color yet")
red = CGFloat()
green = CGFloat()
blue = CGFloat()
Expand Down
4 changes: 2 additions & 2 deletions testbed/tests/assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def assert_set_get(obj, name, value):


def assert_color(actual, expected):
if expected in [None, TRANSPARENT]:
if expected in {None, TRANSPARENT}:
assert expected == actual
else:
if actual in [None, TRANSPARENT]:
if actual in {None, TRANSPARENT}:
assert expected == actual
else:
assert (actual.r, actual.g, actual.b, actual.a) == (
Expand Down
11 changes: 0 additions & 11 deletions testbed/tests/widgets/test_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,6 @@ async def widget():
return toga.Button("Hello")


test_font = mark.skipif(
current_platform in {"iOS"},
reason="font changes don't alter size",
)(test_font)

test_text = mark.skipif(
current_platform in {"iOS"},
reason="round trip empty strings don't work",
)(test_text)


async def test_press(widget, probe):
# Press the button before installing a handler
probe.press()
Expand Down

0 comments on commit 50c4fee

Please sign in to comment.