1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
from guizero import App, Waffle
from threading import Event
from unittest.mock import MagicMock
from common_test import (
schedule_after_test,
schedule_repeat_test,
destroy_test,
enable_test,
display_test,
color_test,
events_test,
cascaded_properties_test,
inherited_properties_test,
grid_layout_test,
auto_layout_test
)
def test_default_values():
a = App()
w = Waffle(a)
assert w.master == a
assert w.grid == None
assert w.align == None
assert w.width == 3
assert w.height == 3
assert w.pixel_size == 20
assert w.pad == 5
assert w.color == "white"
assert w.dotty == False
assert a.description > ""
a.destroy()
def test_alt_values():
a = App(layout = "grid")
w = Waffle(
a,
height = 10,
width = 11,
dim = 12,
pad = 13,
color = "red",
grid = [0,1],
align = "top",
dotty = True,
bg = "green"
)
assert w.master == a
assert w.grid[0] == 0
assert w.grid[1] == 1
assert w.align == "top"
assert w.height == 10
assert w.width == 11
assert w.pixel_size == 12
assert w.pad == 13
assert w.color == "red"
assert w.dotty == True
assert w.bg == "green"
a.destroy()
def test_getters_setters():
a = App()
w = Waffle(a)
w.width = 10
assert w.width == 10
w.height = 11
assert w.height == 11
w.pixel_size = 12
assert w.pixel_size == 12
w.pad = 13
assert w.pad == 13
w.color = "red"
assert w.color == "red"
w.dotty = True
assert w.dotty == True
a.destroy()
def test_set_get_all():
from itertools import chain
a = App()
w = Waffle(a)
w.set_all("red")
# turn 2d list into 1d list
pixels = chain.from_iterable(zip(*w.get_all()))
count = 0
for pixel in pixels:
assert pixel == "red"
count += 1
assert count == w.width * w.height
a.destroy()
def test_set_get_pixel():
a = App()
w = Waffle(a)
w.set_pixel(0,0, "red")
assert w.get_pixel(0,0) == "red"
a.destroy()
def test_pixel_getters_setters():
a = App()
w = Waffle(a)
pixel = w[0,1]
assert pixel.x == 0
assert pixel.y == 1
assert pixel.size == w.pixel_size
assert pixel.color == w.color
assert pixel.dotty == w.dotty
pixel.color = "red"
assert pixel.color == "red"
pixel.dotty = True
assert pixel.dotty
a.destroy()
def test_reset():
from itertools import chain
a = App()
w = Waffle(a)
w.set_pixel(0, 1, "red")
w.reset()
pixels = chain.from_iterable(zip(*w.get_all()))
count = 0
for pixel in pixels:
assert pixel == w.color
count += 1
assert count == w.width * w.height
a.destroy()
def test_command():
a = App()
callback_event = Event()
def callback():
callback_event.set()
w = Waffle(a, command = callback)
assert not callback_event.is_set()
mock_waffle_clicked(w)
assert callback_event.is_set()
a.destroy()
def test_command_with_parameters():
a = App()
callback_event = Event()
def callback(x, y):
assert x == 0
assert y == 1
callback_event.set()
w = Waffle(a, command = callback)
assert not callback_event.is_set()
mock_waffle_clicked(w)
assert callback_event.is_set()
a.destroy()
def test_update_command():
a = App()
callback_event = Event()
def callback():
callback_event.set()
w = Waffle(a)
mock_waffle_clicked(w)
assert not callback_event.is_set()
w.update_command(callback)
mock_waffle_clicked(w)
assert callback_event.is_set()
callback_event.clear()
w.update_command(None)
mock_waffle_clicked(w)
assert not callback_event.is_set()
a.destroy()
def test_update_command_with_parameters():
a = App()
callback_event = Event()
def callback(x, y):
assert x == 0
assert y == 1
callback_event.set()
w = Waffle(a)
w.update_command(callback)
mock_waffle_clicked(w)
assert callback_event.is_set()
a.destroy()
def mock_waffle_clicked(w):
# you cant invoke a tk canvas - this is better than no tests!
# create a mock event
ev = MagicMock()
ev.widget = w
ev.tk_event.widget = w._canvas
# the x event occurs in the middle of the first pixel
ev.tk_event.x = ((w.pixel_size + w.pad) / 2)
# the y event occurs in the middle of the second pixel
ev.tk_event.y = 1 + (w.pixel_size + w.pad) + ((w.pixel_size + w.pad) / 2)
w._clicked_on(ev)
def test_after_schedule():
a = App()
w = Waffle(a)
schedule_after_test(a, w)
a.destroy()
def test_repeat_schedule():
a = App()
w = Waffle(a)
schedule_repeat_test(a, w)
a.destroy()
def test_destroy():
a = App()
w = Waffle(a)
destroy_test(w)
a.destroy()
def test_enable():
a = App()
w = Waffle(a)
enable_test(w)
a.destroy()
def test_display():
a = App()
w = Waffle(a)
display_test(w)
a.destroy()
def test_color():
a = App()
w = Waffle(a)
color_test(w)
a.destroy()
def test_events():
a = App()
w = Waffle(a)
events_test(w)
a.destroy()
def test_cascaded_properties():
a = App()
w = Waffle(a)
cascaded_properties_test(a, w, False)
a.destroy()
def test_inherited_properties():
a = App()
inherited_properties_test(a, lambda: Waffle(a), False)
a.destroy()
def test_auto_layout():
a = App()
w = Waffle(a)
auto_layout_test(w, None)
a.destroy()
def test_grid_layout():
a = App(layout="grid")
w = Waffle(a, grid=[1,2])
grid_layout_test(w, 1, 2, 1, 1, None)
ws = Waffle(a, grid=[1,2,3,4])
grid_layout_test(ws, 1, 2, 3, 4, None)
wa = Waffle(a, grid=[1,2], align="top")
grid_layout_test(wa, 1, 2, 1, 1, "top")
a.destroy()
|