|
| 1 | +from pytest import MonkeyPatch |
| 2 | +from travertino.colors import color |
| 3 | + |
| 4 | +import toga |
| 5 | + |
| 6 | + |
| 7 | +# test that a container-like widget in normal mode has a default background |
| 8 | +def test_box_no_background_in_normal_mode(): |
| 9 | + """A Box has no default background.""" |
| 10 | + # Disable layout debug mode |
| 11 | + with MonkeyPatch.context() as mp: |
| 12 | + mp.setenv("TOGA_DEBUG_LAYOUT", "0") |
| 13 | + box = toga.Box() |
| 14 | + # assert that the bg is default |
| 15 | + assert hasattr(box.style, "background_color") |
| 16 | + assert not box.style.background_color |
| 17 | + |
| 18 | + |
| 19 | +# test that a non-container-like widget in layout debug mode has a default background |
| 20 | +def test_button_debug_background(): |
| 21 | + """A Button in layout debug mode has a default background.""" |
| 22 | + # Enable layout debug mode |
| 23 | + with MonkeyPatch.context() as mp: |
| 24 | + mp.setenv("TOGA_DEBUG_LAYOUT", "1") |
| 25 | + button = toga.Button() |
| 26 | + # assert that the bg is default |
| 27 | + assert hasattr(button.style, "background_color") |
| 28 | + assert not button.style.background_color |
| 29 | + |
| 30 | + |
| 31 | +# test that a label in layout debug mode has a default background |
| 32 | +def test_label_no_debug_background(): |
| 33 | + """A Label in layout debug mode has a default background.""" |
| 34 | + # Enable layout debug mode |
| 35 | + with MonkeyPatch.context() as mp: |
| 36 | + mp.setenv("TOGA_DEBUG_LAYOUT", "1") |
| 37 | + label = toga.Label("label") |
| 38 | + # assert that the bg is default |
| 39 | + assert hasattr(label.style, "background_color") |
| 40 | + assert not label.style.background_color |
| 41 | + |
| 42 | + |
| 43 | +# test that a container-like widget in layout debug mode has a non-default background |
| 44 | +# that matches the expected debug_background_palette |
| 45 | +def test_box_debug_backgrounds(): |
| 46 | + """A Box in layout debug mode has a non-default background.""" |
| 47 | + # Enable layout debug mode |
| 48 | + with MonkeyPatch.context() as mp: |
| 49 | + mp.setenv("TOGA_DEBUG_LAYOUT", "1") |
| 50 | + |
| 51 | + boxes = [] |
| 52 | + debug_bg_palette_length = len(toga.widgets.base.debug_background_palette) |
| 53 | + # need enough for coverage of debug_background_palette array index rollover |
| 54 | + for i in range(debug_bg_palette_length + 3): |
| 55 | + boxes.append(toga.Box()) |
| 56 | + |
| 57 | + for counter, box in enumerate(boxes, start=1): |
| 58 | + index = counter % debug_bg_palette_length |
| 59 | + print(counter) |
| 60 | + assert hasattr(box.style, "background_color") |
| 61 | + assert box.style.background_color == color( |
| 62 | + toga.widgets.base.debug_background_palette[index] |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +# test that a scroll container widget in layout debug mode doesn't have |
| 67 | +# a default background |
| 68 | +def test_scroll_container_debug_background(): |
| 69 | + """A container widget has a default background.""" |
| 70 | + # Disable layout debug mode |
| 71 | + with MonkeyPatch.context() as mp: |
| 72 | + mp.setenv("TOGA_DEBUG_LAYOUT", "1") |
| 73 | + sc = toga.ScrollContainer() |
| 74 | + # assert that the bg is not default |
| 75 | + assert hasattr(sc.style, "background_color") |
| 76 | + assert sc.style.background_color != color("white") |
0 commit comments