Skip to content

Commit aa0c5bf

Browse files
committed
Fake tkinter now tracks created fake Tk and Toplevel objects.
1 parent 19a53d1 commit aa0c5bf

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

tests/fake_tkinter.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,19 @@ class FakeTkinter:
106106
def __init__(self, screen_width, screen_height):
107107
self.screen_width = screen_width
108108
self.screen_height = screen_height
109+
self.windows = []
109110
self.canvases = []
110111
self.photoimage_init_calls = []
111112

112113
def Tk(self):
113-
return FakeTk(self.screen_width, self.screen_height)
114+
window = FakeTk(self.screen_width, self.screen_height)
115+
self.windows.append(window)
116+
return window
114117

115118
def Toplevel(self):
116-
return FakeToplevel(self.screen_width, self.screen_height)
119+
window = FakeToplevel(self.screen_width, self.screen_height)
120+
self.windows.append(window)
121+
return window
117122

118123
def Canvas(self, *args, **kwargs):
119124
canvas = FakeCanvas(*args, **kwargs)

tests/test_window.py

+30-27
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@ def test_create(self):
6262
def test_create_window_creates_underlying_tk_object(self):
6363

6464
w = self._Window()
65-
self.assertIsInstance(w._tk_window, fake_tkinter.FakeTk)
65+
wrapped_tk_window = self.tkinter.windows[0]
66+
self.assertIsInstance(wrapped_tk_window, fake_tkinter.FakeTk)
6667

6768

6869
def test_default_title_was_set(self):
6970

7071
w = self._Window()
71-
tk_window = w._tk_window
72-
tk_window.title.assert_called_once_with('A-Turtle')
72+
wrapped_tk_window = self.tkinter.windows[0]
73+
wrapped_tk_window.title.assert_called_once_with('A-Turtle')
7374

7475

7576
def test_default_width(self):
@@ -134,10 +135,10 @@ def test_resize_handler_in_place(self):
134135

135136
w = self._Window()
136137

137-
tk_window = w._tk_window
138-
tk_window.bind.assert_called_once()
138+
wrapped_tk_window = self.tkinter.windows[0]
139+
wrapped_tk_window.bind.assert_called_once()
139140

140-
(sequence, func), _kwargs = tk_window.bind.call_args
141+
(sequence, func), _kwargs = wrapped_tk_window.bind.call_args
141142
self.assertEqual(sequence, '<Configure>')
142143
self.assertTrue(callable(func), 'Resize handler not callable.')
143144

@@ -207,8 +208,8 @@ def test_custom_fill_passed_to_canvas(self):
207208
def test_custom_title_passed_to_tk_window(self):
208209

209210
w = self._Window(title='Test Title')
210-
tk_window = w._tk_window
211-
tk_window.title.assert_called_once_with('Test Title')
211+
wrapped_tk_window = self.tkinter.windows[0]
212+
wrapped_tk_window.title.assert_called_once_with('Test Title')
212213

213214

214215
def test_positive_horizontal_placement(self):
@@ -295,14 +296,15 @@ def setUp(self):
295296

296297
super().setUp()
297298
self.w = self._Window()
299+
self.wrapped_tk_window = self.tkinter.windows[0]
298300

299301

300302
def test_bind_calls_tk_window_bind(self):
301303

302304
handler = mock.Mock()
303305
self.w.bind('<KeyPress-a>', handler)
304306

305-
self.w._tk_window.bind.assert_called_with('<KeyPress-a>', handler)
307+
self.wrapped_tk_window.bind.assert_called_with('<KeyPress-a>', handler)
306308

307309

308310
def test_bind_unbind_calls_tk_window_bind_unbind(self):
@@ -311,8 +313,8 @@ def test_bind_unbind_calls_tk_window_bind_unbind(self):
311313
self.w.bind('<KeyPress-a>', handler)
312314
self.w.unbind('<KeyPress-a>')
313315

314-
self.w._tk_window.bind.assert_called_with('<KeyPress-a>', handler)
315-
self.w._tk_window.unbind.assert_called_with('<KeyPress-a>', mock.ANY)
316+
self.wrapped_tk_window.bind.assert_called_with('<KeyPress-a>', handler)
317+
self.wrapped_tk_window.unbind.assert_called_with('<KeyPress-a>', mock.ANY)
316318

317319

318320
def test_unbind_unbound_raises_ValueError(self):
@@ -324,7 +326,7 @@ def test_unbind_unbound_raises_ValueError(self):
324326
def test_unbind_default_works_with_no_bindings(self):
325327

326328
self.w.unbind()
327-
self.w._tk_window.unbind.assert_not_called()
329+
self.wrapped_tk_window.unbind.assert_not_called()
328330

329331

330332
def test_unbind_default_unbinds_all_bindings(self):
@@ -333,7 +335,7 @@ def test_unbind_default_unbinds_all_bindings(self):
333335
self.w.bind('<KeyPress-b>', mock.Mock())
334336

335337
self.w.unbind()
336-
unbind_call_args = self.w._tk_window.unbind.call_args_list
338+
unbind_call_args = self.wrapped_tk_window.unbind.call_args_list
337339
self.assertEqual(len(unbind_call_args), 2, 'unbind call count')
338340

339341

@@ -346,11 +348,11 @@ def test_bind_direct_key_with_no_cbs_raises_ValueError(self):
346348
def test_bind_direct_key_with_press_cb_calls_window_bind_twice(self):
347349

348350
# Ignore any window setup bind calls that may have taken place.
349-
self.w._tk_window.bind.reset_mock()
351+
self.wrapped_tk_window.bind.reset_mock()
350352

351353
self.w.bind_direct_key('x', mock.Mock())
352354

353-
bind_call_args = self.w._tk_window.bind.call_args_list
355+
bind_call_args = self.wrapped_tk_window.bind.call_args_list
354356
self.assertEqual(len(bind_call_args), 2, 'unbind call count')
355357

356358
first_call, second_call = bind_call_args
@@ -361,11 +363,11 @@ def test_bind_direct_key_with_press_cb_calls_window_bind_twice(self):
361363
def test_bind_direct_key_with_release_cb_calls_window_bind_twice(self):
362364

363365
# Ignore any window setup bind calls that may have taken place.
364-
self.w._tk_window.bind.reset_mock()
366+
self.wrapped_tk_window.bind.reset_mock()
365367

366368
self.w.bind_direct_key('y', None, mock.Mock())
367369

368-
bind_call_args = self.w._tk_window.bind.call_args_list
370+
bind_call_args = self.wrapped_tk_window.bind.call_args_list
369371
self.assertEqual(len(bind_call_args), 2, 'unbind call count')
370372

371373
first_call, second_call = bind_call_args
@@ -376,11 +378,11 @@ def test_bind_direct_key_with_release_cb_calls_window_bind_twice(self):
376378
def test_bind_direct_key_with_both_cbs_calls_window_bind_twice(self):
377379

378380
# Ignore any window setup bind calls that may have taken place.
379-
self.w._tk_window.bind.reset_mock()
381+
self.wrapped_tk_window.bind.reset_mock()
380382

381383
self.w.bind_direct_key('z', mock.Mock(), mock.Mock())
382384

383-
bind_call_args = self.w._tk_window.bind.call_args_list
385+
bind_call_args = self.wrapped_tk_window.bind.call_args_list
384386
self.assertEqual(len(bind_call_args), 2, 'bind call count')
385387

386388
first_call, second_call = bind_call_args
@@ -391,19 +393,19 @@ def test_bind_direct_key_with_both_cbs_calls_window_bind_twice(self):
391393
def test_bind_unbind_direct_key_calls_window_bind_unbind_twice(self):
392394

393395
# Ignore any window setup bind calls that may have taken place.
394-
self.w._tk_window.bind.reset_mock()
396+
self.wrapped_tk_window.bind.reset_mock()
395397

396398
self.w.bind_direct_key('a', mock.Mock(), mock.Mock())
397399
self.w.unbind_direct_key('a')
398400

399-
bind_call_args = self.w._tk_window.bind.call_args_list
401+
bind_call_args = self.wrapped_tk_window.bind.call_args_list
400402
self.assertEqual(len(bind_call_args), 2, 'bind call count')
401403

402404
first_call, second_call = bind_call_args
403405
self.assertEqual(first_call, mock.call('<KeyPress-a>', mock.ANY))
404406
self.assertEqual(second_call, mock.call('<KeyRelease-a>', mock.ANY))
405407

406-
unbind_call_args = self.w._tk_window.unbind.call_args_list
408+
unbind_call_args = self.wrapped_tk_window.unbind.call_args_list
407409
self.assertEqual(len(unbind_call_args), 2, 'unbind call count')
408410

409411
first_call, second_call = unbind_call_args
@@ -420,7 +422,7 @@ def test_unbind_direct_key_unknown_raises_ValueError(self):
420422
def test_unbind_direct_key_default_works_with_no_bindings(self):
421423

422424
self.w.unbind_direct_key()
423-
self.w._tk_window.unbind.assert_not_called()
425+
self.wrapped_tk_window.unbind.assert_not_called()
424426

425427

426428
def test_unbind_direct_key_default_unbinds_all_direct_keys(self):
@@ -431,7 +433,7 @@ def test_unbind_direct_key_default_unbinds_all_direct_keys(self):
431433
self.w.unbind_direct_key()
432434

433435
# Expect 4 unbind calls: KeyPress/KeyRelease for 2 keys.
434-
unbind_call_args = self.w._tk_window.unbind.call_args_list
436+
unbind_call_args = self.wrapped_tk_window.unbind.call_args_list
435437
self.assertEqual(len(unbind_call_args), 4, 'unbind call count')
436438

437439

@@ -529,9 +531,10 @@ def test_first_has_underlying_tk_others_have_underlying_toplevels(self):
529531
w2 = self._Window()
530532
w3 = self._Window()
531533

532-
self.assertIsInstance(w1._tk_window, fake_tkinter.FakeTk)
533-
self.assertIsInstance(w2._tk_window, fake_tkinter.FakeToplevel)
534-
self.assertIsInstance(w3._tk_window, fake_tkinter.FakeToplevel)
534+
wrapped_tk_windows = self.tkinter.windows
535+
self.assertIsInstance(wrapped_tk_windows[0], fake_tkinter.FakeTk)
536+
self.assertIsInstance(wrapped_tk_windows[1], fake_tkinter.FakeToplevel)
537+
self.assertIsInstance(wrapped_tk_windows[2], fake_tkinter.FakeToplevel)
535538

536539

537540
def test_close_first_window_raises_if_there_are_other_windows(self):

0 commit comments

Comments
 (0)