| 1 |
import unittest |
| 2 |
import tkinter |
| 3 |
from tkinter import dnd |
| 4 |
from test.support import requires |
| 5 |
from test.test_tkinter.support import setUpModule # noqa: F401 |
| 6 |
from test.test_tkinter.support import AbstractTkTest |
| 7 |
|
| 8 |
requires('gui') |
| 9 |
|
| 10 |
|
| 11 |
class Target: |
| 12 |
def __init__(self, widget, log): |
| 13 |
self.widget = widget |
| 14 |
self.log = log |
| 15 |
widget.dnd_accept = self.dnd_accept |
| 16 |
|
| 17 |
def dnd_accept(self, source, event): |
| 18 |
self.log.append('accept') |
| 19 |
return self |
| 20 |
|
| 21 |
def dnd_enter(self, source, event): |
| 22 |
self.log.append('enter') |
| 23 |
|
| 24 |
def dnd_motion(self, source, event): |
| 25 |
self.log.append('motion') |
| 26 |
|
| 27 |
def dnd_leave(self, source, event): |
| 28 |
self.log.append('leave') |
| 29 |
|
| 30 |
def dnd_commit(self, source, event): |
| 31 |
self.log.append('commit') |
| 32 |
|
| 33 |
|
| 34 |
class Source: |
| 35 |
def __init__(self, log): |
| 36 |
self.log = log |
| 37 |
|
| 38 |
def dnd_end(self, target, event): |
| 39 |
self.log.append('end') |
| 40 |
|
| 41 |
|
| 42 |
class FakeEvent: |
| 43 |
def __init__(self, widget, num=1): |
| 44 |
self.num = num |
| 45 |
self.widget = widget |
| 46 |
self.x = self.y = self.x_root = self.y_root = 0 |
| 47 |
|
| 48 |
|
| 49 |
class DndTest(AbstractTkTest, unittest.TestCase): |
| 50 |
|
| 51 |
def setUp(self): |
| 52 |
super().setUp() |
| 53 |
self.canvas = tkinter.Canvas(self.root) |
| 54 |
self.canvas.pack() |
| 55 |
# on_motion() locates the target with winfo_containing(). Bypass that |
| 56 |
# real screen lookup, which depends on the window being visible and |
| 57 |
# unobscured, so the test does not hinge on the window manager. |
| 58 |
self.canvas.winfo_containing = lambda x, y: self.canvas |
| 59 |
self.log = [] |
| 60 |
self.source = Source(self.log) |
| 61 |
self.target = Target(self.canvas, self.log) |
| 62 |
|
| 63 |
def tearDown(self): |
| 64 |
# Make sure no drag-and-drop is left active between tests: the |
| 65 |
# recursion guard is a name-mangled attribute on the root. |
| 66 |
try: |
| 67 |
del self.root._DndHandler__dnd |
| 68 |
except AttributeError: |
| 69 |
pass |
| 70 |
super().tearDown() |
| 71 |
|
| 72 |
def test_drag_and_drop(self): |
| 73 |
handler = dnd.dnd_start(self.source, FakeEvent(self.canvas)) |
| 74 |
self.assertIsNotNone(handler) |
| 75 |
handler.on_motion(FakeEvent(self.canvas)) # Enter the target. |
| 76 |
handler.on_motion(FakeEvent(self.canvas)) # Move within the target. |
| 77 |
handler.on_release(FakeEvent(self.canvas)) # Drop on the target. |
| 78 |
self.assertEqual(self.log, |
| 79 |
['accept', 'enter', 'accept', 'motion', 'commit', 'end']) |
| 80 |
|
| 81 |
def test_cancel(self): |
| 82 |
handler = dnd.dnd_start(self.source, FakeEvent(self.canvas)) |
| 83 |
handler.on_motion(FakeEvent(self.canvas)) # Enter the target. |
| 84 |
handler.cancel() # Leaves the target without committing. |
| 85 |
self.assertEqual(self.log, ['accept', 'enter', 'leave', 'end']) |
| 86 |
|
| 87 |
def test_no_target(self): |
| 88 |
# Nothing under the pointer accepts the drag. |
| 89 |
self.canvas.winfo_containing = lambda x, y: None |
| 90 |
handler = dnd.dnd_start(self.source, FakeEvent(self.canvas)) |
| 91 |
handler.on_motion(FakeEvent(self.canvas)) |
| 92 |
handler.on_release(FakeEvent(self.canvas)) |
| 93 |
self.assertEqual(self.log, ['end']) |
| 94 |
|
| 95 |
def test_no_recursive_start(self): |
| 96 |
handler = dnd.dnd_start(self.source, FakeEvent(self.canvas)) |
| 97 |
self.assertIsNotNone(handler) |
| 98 |
# A drag is already in progress, so a second start is ignored. |
| 99 |
self.assertIsNone(dnd.dnd_start(self.source, FakeEvent(self.canvas))) |
| 100 |
handler.cancel() |
| 101 |
|
| 102 |
def test_high_button_number_ignored(self): |
| 103 |
self.assertIsNone(dnd.dnd_start(self.source, FakeEvent(self.canvas, num=6))) |
| 104 |
|
| 105 |
def test_restart_after_finish(self): |
| 106 |
handler = dnd.dnd_start(self.source, FakeEvent(self.canvas)) |
| 107 |
handler.cancel() |
| 108 |
# Once a drag has finished a new one can start. |
| 109 |
handler = dnd.dnd_start(self.source, FakeEvent(self.canvas)) |
| 110 |
self.assertIsNotNone(handler) |
| 111 |
handler.cancel() |
| 112 |
|
| 113 |
def test_drag_cursor(self): |
| 114 |
self.canvas['cursor'] = 'watch' |
| 115 |
handler = dnd.dnd_start(self.source, FakeEvent(self.canvas)) |
| 116 |
# The drag cursor is shown while dragging, the original restored after. |
| 117 |
self.assertEqual(handler.save_cursor, 'watch') |
| 118 |
self.assertEqual(str(self.canvas['cursor']), 'hand2') |
| 119 |
handler.cancel() |
| 120 |
self.assertEqual(str(self.canvas['cursor']), 'watch') |
| 121 |
|
| 122 |
def test_bindings_added_and_removed(self): |
| 123 |
handler = dnd.dnd_start(self.source, FakeEvent(self.canvas)) |
| 124 |
self.assertIn('<Motion>', self.canvas.bind()) |
| 125 |
self.assertIn('<B1-ButtonRelease-1>', self.canvas.bind()) |
| 126 |
handler.cancel() |
| 127 |
self.assertNotIn('<Motion>', self.canvas.bind()) |
| 128 |
self.assertNotIn('<B1-ButtonRelease-1>', self.canvas.bind()) |
| 129 |
|
| 130 |
def test_switch_target(self): |
| 131 |
log1, log2 = [], [] |
| 132 |
w1, w2 = tkinter.Frame(self.root), tkinter.Frame(self.root) |
| 133 |
target1, target2 = Target(w1, log1), Target(w2, log2) |
| 134 |
handler = dnd.dnd_start(self.source, FakeEvent(self.canvas)) |
| 135 |
self.canvas.winfo_containing = lambda x, y: w1 |
| 136 |
handler.on_motion(FakeEvent(self.canvas)) # Enter target1. |
| 137 |
self.canvas.winfo_containing = lambda x, y: w2 |
| 138 |
handler.on_motion(FakeEvent(self.canvas)) # Leave target1, enter target2. |
| 139 |
self.assertIs(handler.target, target2) |
| 140 |
self.assertEqual(log1, ['accept', 'enter', 'leave']) |
| 141 |
self.assertEqual(log2, ['accept', 'enter']) |
| 142 |
handler.cancel() |
| 143 |
|
| 144 |
def test_target_in_ancestor(self): |
| 145 |
# The widget under the pointer has no dnd_accept, but an ancestor |
| 146 |
# does: the search walks up the master chain to find it. |
| 147 |
parent = tkinter.Frame(self.root) |
| 148 |
target = Target(parent, self.log) |
| 149 |
child = tkinter.Frame(parent) |
| 150 |
self.canvas.winfo_containing = lambda x, y: child |
| 151 |
handler = dnd.dnd_start(self.source, FakeEvent(self.canvas)) |
| 152 |
handler.on_motion(FakeEvent(self.canvas)) |
| 153 |
self.assertIs(handler.target, target) |
| 154 |
self.assertEqual(self.log, ['accept', 'enter']) |
| 155 |
handler.cancel() |
| 156 |
|
| 157 |
def test_accept_returning_none_continues(self): |
| 158 |
# dnd_accept() returning None means "not me, keep looking up". |
| 159 |
parent = tkinter.Frame(self.root) |
| 160 |
target = Target(parent, self.log) |
| 161 |
child = tkinter.Frame(parent) |
| 162 |
child.dnd_accept = lambda source, event: None |
| 163 |
self.canvas.winfo_containing = lambda x, y: child |
| 164 |
handler = dnd.dnd_start(self.source, FakeEvent(self.canvas)) |
| 165 |
handler.on_motion(FakeEvent(self.canvas)) |
| 166 |
self.assertIs(handler.target, target) |
| 167 |
handler.cancel() |
| 168 |
|
| 169 |
|
| 170 |
if __name__ == "__main__": |
| 171 |
unittest.main() |