Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | import tkinter |
Georg Brandl | cf8a209 | 2010-12-19 10:17:46 +0000 | [diff] [blame] | 3 | import os |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 4 | from tkinter import ttk |
| 5 | from test.support import requires, run_unittest |
Ned Deily | 11f00f3 | 2011-06-27 23:12:20 -0700 | [diff] [blame] | 6 | import sys |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 7 | |
| 8 | import tkinter.test.support as support |
| 9 | from tkinter.test.test_ttk.test_functions import MockTclObj, MockStateSpec |
| 10 | |
| 11 | requires('gui') |
| 12 | |
| 13 | class WidgetTest(unittest.TestCase): |
| 14 | """Tests methods available in every ttk widget.""" |
| 15 | |
| 16 | def setUp(self): |
Guilherme Polo | 8ec5829 | 2009-02-02 01:02:20 +0000 | [diff] [blame] | 17 | support.root_deiconify() |
Antoine Pitrou | cb15988 | 2010-09-26 16:36:33 +0000 | [diff] [blame] | 18 | self.widget = ttk.Button(width=0, text="Text") |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 19 | self.widget.pack() |
| 20 | self.widget.wait_visibility() |
| 21 | |
| 22 | def tearDown(self): |
| 23 | self.widget.destroy() |
Guilherme Polo | 8ec5829 | 2009-02-02 01:02:20 +0000 | [diff] [blame] | 24 | support.root_withdraw() |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 25 | |
| 26 | |
| 27 | def test_identify(self): |
| 28 | self.widget.update_idletasks() |
Antoine Pitrou | cb15988 | 2010-09-26 16:36:33 +0000 | [diff] [blame] | 29 | self.assertEqual(self.widget.identify( |
| 30 | int(self.widget.winfo_width() / 2), |
| 31 | int(self.widget.winfo_height() / 2) |
| 32 | ), "label") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 33 | self.assertEqual(self.widget.identify(-1, -1), "") |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 34 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 35 | self.assertRaises(tkinter.TclError, self.widget.identify, None, 5) |
| 36 | self.assertRaises(tkinter.TclError, self.widget.identify, 5, None) |
| 37 | self.assertRaises(tkinter.TclError, self.widget.identify, 5, '') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 38 | |
| 39 | |
| 40 | def test_widget_state(self): |
| 41 | # XXX not sure about the portability of all these tests |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 42 | self.assertEqual(self.widget.state(), ()) |
| 43 | self.assertEqual(self.widget.instate(['!disabled']), True) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 44 | |
| 45 | # changing from !disabled to disabled |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 46 | self.assertEqual(self.widget.state(['disabled']), ('!disabled', )) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 47 | # no state change |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 48 | self.assertEqual(self.widget.state(['disabled']), ()) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 49 | # change back to !disable but also active |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 50 | self.assertEqual(self.widget.state(['!disabled', 'active']), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 51 | ('!active', 'disabled')) |
| 52 | # no state changes, again |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 53 | self.assertEqual(self.widget.state(['!disabled', 'active']), ()) |
| 54 | self.assertEqual(self.widget.state(['active', '!disabled']), ()) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 55 | |
| 56 | def test_cb(arg1, **kw): |
| 57 | return arg1, kw |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 58 | self.assertEqual(self.widget.instate(['!disabled'], |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 59 | test_cb, "hi", **{"msg": "there"}), |
| 60 | ('hi', {'msg': 'there'})) |
| 61 | |
| 62 | # attempt to set invalid statespec |
| 63 | currstate = self.widget.state() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 64 | self.assertRaises(tkinter.TclError, self.widget.instate, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 65 | ['badstate']) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 66 | self.assertRaises(tkinter.TclError, self.widget.instate, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 67 | ['disabled', 'badstate']) |
| 68 | # verify that widget didn't change its state |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 69 | self.assertEqual(currstate, self.widget.state()) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 70 | |
| 71 | # ensuring that passing None as state doesn't modify current state |
| 72 | self.widget.state(['active', '!disabled']) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 73 | self.assertEqual(self.widget.state(), ('active', )) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 74 | |
| 75 | |
| 76 | class ButtonTest(unittest.TestCase): |
| 77 | |
| 78 | def test_invoke(self): |
| 79 | success = [] |
| 80 | btn = ttk.Button(command=lambda: success.append(1)) |
| 81 | btn.invoke() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 82 | self.assertTrue(success) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 83 | |
| 84 | |
| 85 | class CheckbuttonTest(unittest.TestCase): |
| 86 | |
| 87 | def test_invoke(self): |
| 88 | success = [] |
| 89 | def cb_test(): |
| 90 | success.append(1) |
| 91 | return "cb test called" |
| 92 | |
| 93 | cbtn = ttk.Checkbutton(command=cb_test) |
| 94 | # the variable automatically created by ttk.Checkbutton is actually |
| 95 | # undefined till we invoke the Checkbutton |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 96 | self.assertEqual(cbtn.state(), ('alternate', )) |
| 97 | self.assertRaises(tkinter.TclError, cbtn.tk.globalgetvar, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 98 | cbtn['variable']) |
| 99 | |
| 100 | res = cbtn.invoke() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 101 | self.assertEqual(res, "cb test called") |
| 102 | self.assertEqual(cbtn['onvalue'], |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 103 | cbtn.tk.globalgetvar(cbtn['variable'])) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 104 | self.assertTrue(success) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 105 | |
| 106 | cbtn['command'] = '' |
| 107 | res = cbtn.invoke() |
Serhiy Storchaka | 06ce077 | 2013-08-22 17:51:58 +0300 | [diff] [blame^] | 108 | self.assertEqual(str(res), '') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 109 | self.assertFalse(len(success) > 1) |
| 110 | self.assertEqual(cbtn['offvalue'], |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 111 | cbtn.tk.globalgetvar(cbtn['variable'])) |
| 112 | |
| 113 | |
| 114 | class ComboboxTest(unittest.TestCase): |
| 115 | |
| 116 | def setUp(self): |
Guilherme Polo | 8ec5829 | 2009-02-02 01:02:20 +0000 | [diff] [blame] | 117 | support.root_deiconify() |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 118 | self.combo = ttk.Combobox() |
| 119 | |
| 120 | def tearDown(self): |
| 121 | self.combo.destroy() |
Guilherme Polo | 8ec5829 | 2009-02-02 01:02:20 +0000 | [diff] [blame] | 122 | support.root_withdraw() |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 123 | |
| 124 | def _show_drop_down_listbox(self): |
| 125 | width = self.combo.winfo_width() |
| 126 | self.combo.event_generate('<ButtonPress-1>', x=width - 5, y=5) |
| 127 | self.combo.event_generate('<ButtonRelease-1>', x=width - 5, y=5) |
| 128 | self.combo.update_idletasks() |
| 129 | |
| 130 | |
| 131 | def test_virtual_event(self): |
| 132 | success = [] |
| 133 | |
| 134 | self.combo['values'] = [1] |
| 135 | self.combo.bind('<<ComboboxSelected>>', |
| 136 | lambda evt: success.append(True)) |
| 137 | self.combo.pack() |
| 138 | self.combo.wait_visibility() |
| 139 | |
| 140 | height = self.combo.winfo_height() |
| 141 | self._show_drop_down_listbox() |
| 142 | self.combo.update() |
| 143 | self.combo.event_generate('<Return>') |
| 144 | self.combo.update() |
| 145 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 146 | self.assertTrue(success) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 147 | |
| 148 | |
| 149 | def test_postcommand(self): |
| 150 | success = [] |
| 151 | |
| 152 | self.combo['postcommand'] = lambda: success.append(True) |
| 153 | self.combo.pack() |
| 154 | self.combo.wait_visibility() |
| 155 | |
| 156 | self._show_drop_down_listbox() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 157 | self.assertTrue(success) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 158 | |
| 159 | # testing postcommand removal |
| 160 | self.combo['postcommand'] = '' |
| 161 | self._show_drop_down_listbox() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 162 | self.assertEqual(len(success), 1) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 163 | |
| 164 | |
| 165 | def test_values(self): |
| 166 | def check_get_current(getval, currval): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 167 | self.assertEqual(self.combo.get(), getval) |
| 168 | self.assertEqual(self.combo.current(), currval) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 169 | |
| 170 | check_get_current('', -1) |
| 171 | |
| 172 | self.combo['values'] = ['a', 1, 'c'] |
| 173 | |
| 174 | self.combo.set('c') |
| 175 | check_get_current('c', 2) |
| 176 | |
| 177 | self.combo.current(0) |
| 178 | check_get_current('a', 0) |
| 179 | |
| 180 | self.combo.set('d') |
| 181 | check_get_current('d', -1) |
| 182 | |
| 183 | # testing values with empty string |
| 184 | self.combo.set('') |
| 185 | self.combo['values'] = (1, 2, '', 3) |
| 186 | check_get_current('', 2) |
| 187 | |
| 188 | # testing values with empty string set through configure |
| 189 | self.combo.configure(values=[1, '', 2]) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 190 | self.assertEqual(self.combo['values'], ('1', '', '2')) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 191 | |
Serhiy Storchaka | b139652 | 2013-01-15 17:56:08 +0200 | [diff] [blame] | 192 | # testing values with spaces |
| 193 | self.combo['values'] = ['a b', 'a\tb', 'a\nb'] |
| 194 | self.assertEqual(self.combo['values'], ('a b', 'a\tb', 'a\nb')) |
| 195 | |
| 196 | # testing values with special characters |
| 197 | self.combo['values'] = [r'a\tb', '"a"', '} {'] |
| 198 | self.assertEqual(self.combo['values'], (r'a\tb', '"a"', '} {')) |
| 199 | |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 200 | # out of range |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 201 | self.assertRaises(tkinter.TclError, self.combo.current, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 202 | len(self.combo['values'])) |
| 203 | # it expects an integer (or something that can be converted to int) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 204 | self.assertRaises(tkinter.TclError, self.combo.current, '') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 205 | |
| 206 | # testing creating combobox with empty string in values |
| 207 | combo2 = ttk.Combobox(values=[1, 2, '']) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 208 | self.assertEqual(combo2['values'], ('1', '2', '')) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 209 | combo2.destroy() |
| 210 | |
| 211 | |
| 212 | class EntryTest(unittest.TestCase): |
| 213 | |
| 214 | def setUp(self): |
Guilherme Polo | 8ec5829 | 2009-02-02 01:02:20 +0000 | [diff] [blame] | 215 | support.root_deiconify() |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 216 | self.entry = ttk.Entry() |
| 217 | |
| 218 | def tearDown(self): |
| 219 | self.entry.destroy() |
Guilherme Polo | 8ec5829 | 2009-02-02 01:02:20 +0000 | [diff] [blame] | 220 | support.root_withdraw() |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 221 | |
| 222 | |
| 223 | def test_bbox(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 224 | self.assertEqual(len(self.entry.bbox(0)), 4) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 225 | for item in self.entry.bbox(0): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 226 | self.assertTrue(isinstance(item, int)) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 227 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 228 | self.assertRaises(tkinter.TclError, self.entry.bbox, 'noindex') |
| 229 | self.assertRaises(tkinter.TclError, self.entry.bbox, None) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 230 | |
| 231 | |
| 232 | def test_identify(self): |
| 233 | self.entry.pack() |
| 234 | self.entry.wait_visibility() |
| 235 | self.entry.update_idletasks() |
| 236 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 237 | self.assertEqual(self.entry.identify(5, 5), "textarea") |
| 238 | self.assertEqual(self.entry.identify(-1, -1), "") |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 239 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 240 | self.assertRaises(tkinter.TclError, self.entry.identify, None, 5) |
| 241 | self.assertRaises(tkinter.TclError, self.entry.identify, 5, None) |
| 242 | self.assertRaises(tkinter.TclError, self.entry.identify, 5, '') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 243 | |
| 244 | |
| 245 | def test_validation_options(self): |
| 246 | success = [] |
| 247 | test_invalid = lambda: success.append(True) |
| 248 | |
| 249 | self.entry['validate'] = 'none' |
| 250 | self.entry['validatecommand'] = lambda: False |
| 251 | |
| 252 | self.entry['invalidcommand'] = test_invalid |
| 253 | self.entry.validate() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 254 | self.assertTrue(success) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 255 | |
| 256 | self.entry['invalidcommand'] = '' |
| 257 | self.entry.validate() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 258 | self.assertEqual(len(success), 1) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 259 | |
| 260 | self.entry['invalidcommand'] = test_invalid |
| 261 | self.entry['validatecommand'] = lambda: True |
| 262 | self.entry.validate() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 263 | self.assertEqual(len(success), 1) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 264 | |
| 265 | self.entry['validatecommand'] = '' |
| 266 | self.entry.validate() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 267 | self.assertEqual(len(success), 1) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 268 | |
| 269 | self.entry['validatecommand'] = True |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 270 | self.assertRaises(tkinter.TclError, self.entry.validate) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 271 | |
| 272 | |
| 273 | def test_validation(self): |
| 274 | validation = [] |
| 275 | def validate(to_insert): |
| 276 | if not 'a' <= to_insert.lower() <= 'z': |
| 277 | validation.append(False) |
| 278 | return False |
| 279 | validation.append(True) |
| 280 | return True |
| 281 | |
| 282 | self.entry['validate'] = 'key' |
| 283 | self.entry['validatecommand'] = self.entry.register(validate), '%S' |
| 284 | |
| 285 | self.entry.insert('end', 1) |
| 286 | self.entry.insert('end', 'a') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 287 | self.assertEqual(validation, [False, True]) |
| 288 | self.assertEqual(self.entry.get(), 'a') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 289 | |
| 290 | |
| 291 | def test_revalidation(self): |
| 292 | def validate(content): |
| 293 | for letter in content: |
| 294 | if not 'a' <= letter.lower() <= 'z': |
| 295 | return False |
| 296 | return True |
| 297 | |
| 298 | self.entry['validatecommand'] = self.entry.register(validate), '%P' |
| 299 | |
| 300 | self.entry.insert('end', 'avocado') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 301 | self.assertEqual(self.entry.validate(), True) |
| 302 | self.assertEqual(self.entry.state(), ()) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 303 | |
| 304 | self.entry.delete(0, 'end') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 305 | self.assertEqual(self.entry.get(), '') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 306 | |
| 307 | self.entry.insert('end', 'a1b') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 308 | self.assertEqual(self.entry.validate(), False) |
| 309 | self.assertEqual(self.entry.state(), ('invalid', )) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 310 | |
| 311 | self.entry.delete(1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 312 | self.assertEqual(self.entry.validate(), True) |
| 313 | self.assertEqual(self.entry.state(), ()) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 314 | |
| 315 | |
| 316 | class PanedwindowTest(unittest.TestCase): |
| 317 | |
| 318 | def setUp(self): |
Guilherme Polo | 8ec5829 | 2009-02-02 01:02:20 +0000 | [diff] [blame] | 319 | support.root_deiconify() |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 320 | self.paned = ttk.Panedwindow() |
| 321 | |
| 322 | def tearDown(self): |
| 323 | self.paned.destroy() |
Guilherme Polo | 8ec5829 | 2009-02-02 01:02:20 +0000 | [diff] [blame] | 324 | support.root_withdraw() |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 325 | |
| 326 | |
| 327 | def test_add(self): |
| 328 | # attempt to add a child that is not a direct child of the paned window |
| 329 | label = ttk.Label(self.paned) |
| 330 | child = ttk.Label(label) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 331 | self.assertRaises(tkinter.TclError, self.paned.add, child) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 332 | label.destroy() |
| 333 | child.destroy() |
| 334 | # another attempt |
| 335 | label = ttk.Label() |
| 336 | child = ttk.Label(label) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 337 | self.assertRaises(tkinter.TclError, self.paned.add, child) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 338 | child.destroy() |
| 339 | label.destroy() |
| 340 | |
| 341 | good_child = ttk.Label() |
| 342 | self.paned.add(good_child) |
| 343 | # re-adding a child is not accepted |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 344 | self.assertRaises(tkinter.TclError, self.paned.add, good_child) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 345 | |
| 346 | other_child = ttk.Label(self.paned) |
| 347 | self.paned.add(other_child) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 348 | self.assertEqual(self.paned.pane(0), self.paned.pane(1)) |
| 349 | self.assertRaises(tkinter.TclError, self.paned.pane, 2) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 350 | good_child.destroy() |
| 351 | other_child.destroy() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 352 | self.assertRaises(tkinter.TclError, self.paned.pane, 0) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 353 | |
| 354 | |
| 355 | def test_forget(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 356 | self.assertRaises(tkinter.TclError, self.paned.forget, None) |
| 357 | self.assertRaises(tkinter.TclError, self.paned.forget, 0) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 358 | |
| 359 | self.paned.add(ttk.Label()) |
| 360 | self.paned.forget(0) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 361 | self.assertRaises(tkinter.TclError, self.paned.forget, 0) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 362 | |
| 363 | |
| 364 | def test_insert(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 365 | self.assertRaises(tkinter.TclError, self.paned.insert, None, 0) |
| 366 | self.assertRaises(tkinter.TclError, self.paned.insert, 0, None) |
| 367 | self.assertRaises(tkinter.TclError, self.paned.insert, 0, 0) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 368 | |
| 369 | child = ttk.Label() |
| 370 | child2 = ttk.Label() |
| 371 | child3 = ttk.Label() |
| 372 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 373 | self.assertRaises(tkinter.TclError, self.paned.insert, 0, child) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 374 | |
| 375 | self.paned.insert('end', child2) |
| 376 | self.paned.insert(0, child) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 377 | self.assertEqual(self.paned.panes(), (str(child), str(child2))) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 378 | |
| 379 | self.paned.insert(0, child2) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 380 | self.assertEqual(self.paned.panes(), (str(child2), str(child))) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 381 | |
| 382 | self.paned.insert('end', child3) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 383 | self.assertEqual(self.paned.panes(), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 384 | (str(child2), str(child), str(child3))) |
| 385 | |
| 386 | # reinserting a child should move it to its current position |
| 387 | panes = self.paned.panes() |
| 388 | self.paned.insert('end', child3) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 389 | self.assertEqual(panes, self.paned.panes()) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 390 | |
| 391 | # moving child3 to child2 position should result in child2 ending up |
| 392 | # in previous child position and child ending up in previous child3 |
| 393 | # position |
| 394 | self.paned.insert(child2, child3) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 395 | self.assertEqual(self.paned.panes(), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 396 | (str(child3), str(child2), str(child))) |
| 397 | |
| 398 | |
| 399 | def test_pane(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 400 | self.assertRaises(tkinter.TclError, self.paned.pane, 0) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 401 | |
| 402 | child = ttk.Label() |
| 403 | self.paned.add(child) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 404 | self.assertTrue(isinstance(self.paned.pane(0), dict)) |
| 405 | self.assertEqual(self.paned.pane(0, weight=None), 0) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 406 | # newer form for querying a single option |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 407 | self.assertEqual(self.paned.pane(0, 'weight'), 0) |
| 408 | self.assertEqual(self.paned.pane(0), self.paned.pane(str(child))) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 409 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 410 | self.assertRaises(tkinter.TclError, self.paned.pane, 0, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 411 | badoption='somevalue') |
| 412 | |
| 413 | |
| 414 | def test_sashpos(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 415 | self.assertRaises(tkinter.TclError, self.paned.sashpos, None) |
| 416 | self.assertRaises(tkinter.TclError, self.paned.sashpos, '') |
| 417 | self.assertRaises(tkinter.TclError, self.paned.sashpos, 0) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 418 | |
| 419 | child = ttk.Label(self.paned, text='a') |
| 420 | self.paned.add(child, weight=1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 421 | self.assertRaises(tkinter.TclError, self.paned.sashpos, 0) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 422 | child2 = ttk.Label(self.paned, text='b') |
| 423 | self.paned.add(child2) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 424 | self.assertRaises(tkinter.TclError, self.paned.sashpos, 1) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 425 | |
| 426 | self.paned.pack(expand=True, fill='both') |
| 427 | self.paned.wait_visibility() |
| 428 | |
| 429 | curr_pos = self.paned.sashpos(0) |
| 430 | self.paned.sashpos(0, 1000) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 431 | self.assertTrue(curr_pos != self.paned.sashpos(0)) |
| 432 | self.assertTrue(isinstance(self.paned.sashpos(0), int)) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 433 | |
| 434 | |
| 435 | class RadiobuttonTest(unittest.TestCase): |
| 436 | |
| 437 | def test_invoke(self): |
| 438 | success = [] |
| 439 | def cb_test(): |
| 440 | success.append(1) |
| 441 | return "cb test called" |
| 442 | |
| 443 | myvar = tkinter.IntVar() |
| 444 | cbtn = ttk.Radiobutton(command=cb_test, variable=myvar, value=0) |
| 445 | cbtn2 = ttk.Radiobutton(command=cb_test, variable=myvar, value=1) |
| 446 | |
| 447 | res = cbtn.invoke() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 448 | self.assertEqual(res, "cb test called") |
| 449 | self.assertEqual(cbtn['value'], myvar.get()) |
| 450 | self.assertEqual(myvar.get(), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 451 | cbtn.tk.globalgetvar(cbtn['variable'])) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 452 | self.assertTrue(success) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 453 | |
| 454 | cbtn2['command'] = '' |
| 455 | res = cbtn2.invoke() |
Serhiy Storchaka | 06ce077 | 2013-08-22 17:51:58 +0300 | [diff] [blame^] | 456 | self.assertEqual(str(res), '') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 457 | self.assertFalse(len(success) > 1) |
| 458 | self.assertEqual(cbtn2['value'], myvar.get()) |
| 459 | self.assertEqual(myvar.get(), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 460 | cbtn.tk.globalgetvar(cbtn['variable'])) |
| 461 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 462 | self.assertEqual(str(cbtn['variable']), str(cbtn2['variable'])) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 463 | |
| 464 | |
| 465 | |
| 466 | class ScaleTest(unittest.TestCase): |
| 467 | |
| 468 | def setUp(self): |
Guilherme Polo | 8ec5829 | 2009-02-02 01:02:20 +0000 | [diff] [blame] | 469 | support.root_deiconify() |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 470 | self.scale = ttk.Scale() |
| 471 | self.scale.pack() |
| 472 | self.scale.update() |
| 473 | |
| 474 | def tearDown(self): |
| 475 | self.scale.destroy() |
Guilherme Polo | 8ec5829 | 2009-02-02 01:02:20 +0000 | [diff] [blame] | 476 | support.root_withdraw() |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 477 | |
| 478 | |
| 479 | def test_custom_event(self): |
| 480 | failure = [1, 1, 1] # will need to be empty |
| 481 | |
| 482 | funcid = self.scale.bind('<<RangeChanged>>', lambda evt: failure.pop()) |
| 483 | |
| 484 | self.scale['from'] = 10 |
| 485 | self.scale['from_'] = 10 |
| 486 | self.scale['to'] = 3 |
| 487 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 488 | self.assertFalse(failure) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 489 | |
| 490 | failure = [1, 1, 1] |
| 491 | self.scale.configure(from_=2, to=5) |
| 492 | self.scale.configure(from_=0, to=-2) |
| 493 | self.scale.configure(to=10) |
| 494 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 495 | self.assertFalse(failure) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 496 | |
| 497 | |
| 498 | def test_get(self): |
| 499 | scale_width = self.scale.winfo_width() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 500 | self.assertEqual(self.scale.get(scale_width, 0), self.scale['to']) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 501 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 502 | self.assertEqual(self.scale.get(0, 0), self.scale['from']) |
| 503 | self.assertEqual(self.scale.get(), self.scale['value']) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 504 | self.scale['value'] = 30 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 505 | self.assertEqual(self.scale.get(), self.scale['value']) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 506 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 507 | self.assertRaises(tkinter.TclError, self.scale.get, '', 0) |
| 508 | self.assertRaises(tkinter.TclError, self.scale.get, 0, '') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 509 | |
| 510 | |
| 511 | def test_set(self): |
| 512 | # set restricts the max/min values according to the current range |
| 513 | max = self.scale['to'] |
| 514 | new_max = max + 10 |
| 515 | self.scale.set(new_max) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 516 | self.assertEqual(self.scale.get(), max) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 517 | min = self.scale['from'] |
| 518 | self.scale.set(min - 1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 519 | self.assertEqual(self.scale.get(), min) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 520 | |
| 521 | # changing directly the variable doesn't impose this limitation tho |
| 522 | var = tkinter.DoubleVar() |
| 523 | self.scale['variable'] = var |
| 524 | var.set(max + 5) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 525 | self.assertEqual(self.scale.get(), var.get()) |
| 526 | self.assertEqual(self.scale.get(), max + 5) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 527 | del var |
| 528 | |
| 529 | # the same happens with the value option |
| 530 | self.scale['value'] = max + 10 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 531 | self.assertEqual(self.scale.get(), max + 10) |
| 532 | self.assertEqual(self.scale.get(), self.scale['value']) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 533 | |
| 534 | # nevertheless, note that the max/min values we can get specifying |
| 535 | # x, y coords are the ones according to the current range |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 536 | self.assertEqual(self.scale.get(0, 0), min) |
| 537 | self.assertEqual(self.scale.get(self.scale.winfo_width(), 0), max) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 538 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 539 | self.assertRaises(tkinter.TclError, self.scale.set, None) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 540 | |
| 541 | |
| 542 | class NotebookTest(unittest.TestCase): |
| 543 | |
| 544 | def setUp(self): |
Guilherme Polo | 8ec5829 | 2009-02-02 01:02:20 +0000 | [diff] [blame] | 545 | support.root_deiconify() |
Antoine Pitrou | cb15988 | 2010-09-26 16:36:33 +0000 | [diff] [blame] | 546 | self.nb = ttk.Notebook(padding=0) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 547 | self.child1 = ttk.Label() |
| 548 | self.child2 = ttk.Label() |
| 549 | self.nb.add(self.child1, text='a') |
| 550 | self.nb.add(self.child2, text='b') |
| 551 | |
| 552 | def tearDown(self): |
| 553 | self.child1.destroy() |
| 554 | self.child2.destroy() |
| 555 | self.nb.destroy() |
Guilherme Polo | 8ec5829 | 2009-02-02 01:02:20 +0000 | [diff] [blame] | 556 | support.root_withdraw() |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 557 | |
| 558 | |
| 559 | def test_tab_identifiers(self): |
| 560 | self.nb.forget(0) |
| 561 | self.nb.hide(self.child2) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 562 | self.assertRaises(tkinter.TclError, self.nb.tab, self.child1) |
| 563 | self.assertEqual(self.nb.index('end'), 1) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 564 | self.nb.add(self.child2) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 565 | self.assertEqual(self.nb.index('end'), 1) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 566 | self.nb.select(self.child2) |
| 567 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 568 | self.assertTrue(self.nb.tab('current')) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 569 | self.nb.add(self.child1, text='a') |
| 570 | |
| 571 | self.nb.pack() |
| 572 | self.nb.wait_visibility() |
Ned Deily | 11f00f3 | 2011-06-27 23:12:20 -0700 | [diff] [blame] | 573 | if sys.platform == 'darwin': |
| 574 | tb_idx = "@20,5" |
| 575 | else: |
| 576 | tb_idx = "@5,5" |
| 577 | self.assertEqual(self.nb.tab(tb_idx), self.nb.tab('current')) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 578 | |
| 579 | for i in range(5, 100, 5): |
Ned Deily | 11f00f3 | 2011-06-27 23:12:20 -0700 | [diff] [blame] | 580 | try: |
| 581 | if self.nb.tab('@%d, 5' % i, text=None) == 'a': |
| 582 | break |
| 583 | except tkinter.TclError: |
| 584 | pass |
| 585 | |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 586 | else: |
| 587 | self.fail("Tab with text 'a' not found") |
| 588 | |
| 589 | |
| 590 | def test_add_and_hidden(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 591 | self.assertRaises(tkinter.TclError, self.nb.hide, -1) |
| 592 | self.assertRaises(tkinter.TclError, self.nb.hide, 'hi') |
| 593 | self.assertRaises(tkinter.TclError, self.nb.hide, None) |
| 594 | self.assertRaises(tkinter.TclError, self.nb.add, None) |
| 595 | self.assertRaises(tkinter.TclError, self.nb.add, ttk.Label(), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 596 | unknown='option') |
| 597 | |
| 598 | tabs = self.nb.tabs() |
| 599 | self.nb.hide(self.child1) |
| 600 | self.nb.add(self.child1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 601 | self.assertEqual(self.nb.tabs(), tabs) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 602 | |
| 603 | child = ttk.Label() |
| 604 | self.nb.add(child, text='c') |
| 605 | tabs = self.nb.tabs() |
| 606 | |
| 607 | curr = self.nb.index('current') |
| 608 | # verify that the tab gets readded at its previous position |
| 609 | child2_index = self.nb.index(self.child2) |
| 610 | self.nb.hide(self.child2) |
| 611 | self.nb.add(self.child2) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 612 | self.assertEqual(self.nb.tabs(), tabs) |
| 613 | self.assertEqual(self.nb.index(self.child2), child2_index) |
| 614 | self.assertTrue(str(self.child2) == self.nb.tabs()[child2_index]) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 615 | # but the tab next to it (not hidden) is the one selected now |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 616 | self.assertEqual(self.nb.index('current'), curr + 1) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 617 | |
| 618 | |
| 619 | def test_forget(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 620 | self.assertRaises(tkinter.TclError, self.nb.forget, -1) |
| 621 | self.assertRaises(tkinter.TclError, self.nb.forget, 'hi') |
| 622 | self.assertRaises(tkinter.TclError, self.nb.forget, None) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 623 | |
| 624 | tabs = self.nb.tabs() |
| 625 | child1_index = self.nb.index(self.child1) |
| 626 | self.nb.forget(self.child1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 627 | self.assertFalse(str(self.child1) in self.nb.tabs()) |
| 628 | self.assertEqual(len(tabs) - 1, len(self.nb.tabs())) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 629 | |
| 630 | self.nb.add(self.child1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 631 | self.assertEqual(self.nb.index(self.child1), 1) |
| 632 | self.assertFalse(child1_index == self.nb.index(self.child1)) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 633 | |
| 634 | |
| 635 | def test_index(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 636 | self.assertRaises(tkinter.TclError, self.nb.index, -1) |
| 637 | self.assertRaises(tkinter.TclError, self.nb.index, None) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 638 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 639 | self.assertTrue(isinstance(self.nb.index('end'), int)) |
| 640 | self.assertEqual(self.nb.index(self.child1), 0) |
| 641 | self.assertEqual(self.nb.index(self.child2), 1) |
| 642 | self.assertEqual(self.nb.index('end'), 2) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 643 | |
| 644 | |
| 645 | def test_insert(self): |
| 646 | # moving tabs |
| 647 | tabs = self.nb.tabs() |
| 648 | self.nb.insert(1, tabs[0]) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 649 | self.assertEqual(self.nb.tabs(), (tabs[1], tabs[0])) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 650 | self.nb.insert(self.child1, self.child2) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 651 | self.assertEqual(self.nb.tabs(), tabs) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 652 | self.nb.insert('end', self.child1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 653 | self.assertEqual(self.nb.tabs(), (tabs[1], tabs[0])) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 654 | self.nb.insert('end', 0) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 655 | self.assertEqual(self.nb.tabs(), tabs) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 656 | # bad moves |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 657 | self.assertRaises(tkinter.TclError, self.nb.insert, 2, tabs[0]) |
| 658 | self.assertRaises(tkinter.TclError, self.nb.insert, -1, tabs[0]) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 659 | |
| 660 | # new tab |
| 661 | child3 = ttk.Label() |
| 662 | self.nb.insert(1, child3) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 663 | self.assertEqual(self.nb.tabs(), (tabs[0], str(child3), tabs[1])) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 664 | self.nb.forget(child3) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 665 | self.assertEqual(self.nb.tabs(), tabs) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 666 | self.nb.insert(self.child1, child3) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 667 | self.assertEqual(self.nb.tabs(), (str(child3), ) + tabs) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 668 | self.nb.forget(child3) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 669 | self.assertRaises(tkinter.TclError, self.nb.insert, 2, child3) |
| 670 | self.assertRaises(tkinter.TclError, self.nb.insert, -1, child3) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 671 | |
| 672 | # bad inserts |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 673 | self.assertRaises(tkinter.TclError, self.nb.insert, 'end', None) |
| 674 | self.assertRaises(tkinter.TclError, self.nb.insert, None, 0) |
| 675 | self.assertRaises(tkinter.TclError, self.nb.insert, None, None) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 676 | |
| 677 | |
| 678 | def test_select(self): |
| 679 | self.nb.pack() |
| 680 | self.nb.wait_visibility() |
| 681 | |
| 682 | success = [] |
| 683 | tab_changed = [] |
| 684 | |
| 685 | self.child1.bind('<Unmap>', lambda evt: success.append(True)) |
| 686 | self.nb.bind('<<NotebookTabChanged>>', |
| 687 | lambda evt: tab_changed.append(True)) |
| 688 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 689 | self.assertEqual(self.nb.select(), str(self.child1)) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 690 | self.nb.select(self.child2) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 691 | self.assertTrue(success) |
| 692 | self.assertEqual(self.nb.select(), str(self.child2)) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 693 | |
| 694 | self.nb.update() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 695 | self.assertTrue(tab_changed) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 696 | |
| 697 | |
| 698 | def test_tab(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 699 | self.assertRaises(tkinter.TclError, self.nb.tab, -1) |
| 700 | self.assertRaises(tkinter.TclError, self.nb.tab, 'notab') |
| 701 | self.assertRaises(tkinter.TclError, self.nb.tab, None) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 702 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 703 | self.assertTrue(isinstance(self.nb.tab(self.child1), dict)) |
| 704 | self.assertEqual(self.nb.tab(self.child1, text=None), 'a') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 705 | # newer form for querying a single option |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 706 | self.assertEqual(self.nb.tab(self.child1, 'text'), 'a') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 707 | self.nb.tab(self.child1, text='abc') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 708 | self.assertEqual(self.nb.tab(self.child1, text=None), 'abc') |
| 709 | self.assertEqual(self.nb.tab(self.child1, 'text'), 'abc') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 710 | |
| 711 | |
| 712 | def test_tabs(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 713 | self.assertEqual(len(self.nb.tabs()), 2) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 714 | |
| 715 | self.nb.forget(self.child1) |
| 716 | self.nb.forget(self.child2) |
| 717 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 718 | self.assertEqual(self.nb.tabs(), ()) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 719 | |
| 720 | |
| 721 | def test_traversal(self): |
| 722 | self.nb.pack() |
| 723 | self.nb.wait_visibility() |
| 724 | |
| 725 | self.nb.select(0) |
| 726 | |
| 727 | support.simulate_mouse_click(self.nb, 5, 5) |
Benjamin Peterson | a082089 | 2010-04-11 22:04:51 +0000 | [diff] [blame] | 728 | self.nb.focus_force() |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 729 | self.nb.event_generate('<Control-Tab>') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 730 | self.assertEqual(self.nb.select(), str(self.child2)) |
Benjamin Peterson | a082089 | 2010-04-11 22:04:51 +0000 | [diff] [blame] | 731 | self.nb.focus_force() |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 732 | self.nb.event_generate('<Shift-Control-Tab>') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 733 | self.assertEqual(self.nb.select(), str(self.child1)) |
Benjamin Peterson | a082089 | 2010-04-11 22:04:51 +0000 | [diff] [blame] | 734 | self.nb.focus_force() |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 735 | self.nb.event_generate('<Shift-Control-Tab>') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 736 | self.assertEqual(self.nb.select(), str(self.child2)) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 737 | |
| 738 | self.nb.tab(self.child1, text='a', underline=0) |
| 739 | self.nb.enable_traversal() |
Benjamin Peterson | a082089 | 2010-04-11 22:04:51 +0000 | [diff] [blame] | 740 | self.nb.focus_force() |
Antoine Pitrou | cb15988 | 2010-09-26 16:36:33 +0000 | [diff] [blame] | 741 | support.simulate_mouse_click(self.nb, 5, 5) |
Ned Deily | 11f00f3 | 2011-06-27 23:12:20 -0700 | [diff] [blame] | 742 | if sys.platform == 'darwin': |
| 743 | self.nb.event_generate('<Option-a>') |
| 744 | else: |
| 745 | self.nb.event_generate('<Alt-a>') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 746 | self.assertEqual(self.nb.select(), str(self.child1)) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 747 | |
| 748 | |
| 749 | class TreeviewTest(unittest.TestCase): |
| 750 | |
| 751 | def setUp(self): |
Guilherme Polo | 8ec5829 | 2009-02-02 01:02:20 +0000 | [diff] [blame] | 752 | support.root_deiconify() |
Antoine Pitrou | cb15988 | 2010-09-26 16:36:33 +0000 | [diff] [blame] | 753 | self.tv = ttk.Treeview(padding=0) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 754 | |
| 755 | def tearDown(self): |
| 756 | self.tv.destroy() |
Guilherme Polo | 8ec5829 | 2009-02-02 01:02:20 +0000 | [diff] [blame] | 757 | support.root_withdraw() |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 758 | |
| 759 | |
| 760 | def test_bbox(self): |
| 761 | self.tv.pack() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 762 | self.assertEqual(self.tv.bbox(''), '') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 763 | self.tv.wait_visibility() |
| 764 | self.tv.update() |
| 765 | |
| 766 | item_id = self.tv.insert('', 'end') |
| 767 | children = self.tv.get_children() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 768 | self.assertTrue(children) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 769 | |
| 770 | bbox = self.tv.bbox(children[0]) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 771 | self.assertEqual(len(bbox), 4) |
| 772 | self.assertTrue(isinstance(bbox, tuple)) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 773 | for item in bbox: |
| 774 | if not isinstance(item, int): |
| 775 | self.fail("Invalid bounding box: %s" % bbox) |
| 776 | break |
| 777 | |
| 778 | # compare width in bboxes |
| 779 | self.tv['columns'] = ['test'] |
| 780 | self.tv.column('test', width=50) |
| 781 | bbox_column0 = self.tv.bbox(children[0], 0) |
| 782 | root_width = self.tv.column('#0', width=None) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 783 | self.assertEqual(bbox_column0[0], bbox[0] + root_width) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 784 | |
| 785 | # verify that bbox of a closed item is the empty string |
| 786 | child1 = self.tv.insert(item_id, 'end') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 787 | self.assertEqual(self.tv.bbox(child1), '') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 788 | |
| 789 | |
| 790 | def test_children(self): |
| 791 | # no children yet, should get an empty tuple |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 792 | self.assertEqual(self.tv.get_children(), ()) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 793 | |
| 794 | item_id = self.tv.insert('', 'end') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 795 | self.assertTrue(isinstance(self.tv.get_children(), tuple)) |
| 796 | self.assertEqual(self.tv.get_children()[0], item_id) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 797 | |
| 798 | # add item_id and child3 as children of child2 |
| 799 | child2 = self.tv.insert('', 'end') |
| 800 | child3 = self.tv.insert('', 'end') |
| 801 | self.tv.set_children(child2, item_id, child3) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 802 | self.assertEqual(self.tv.get_children(child2), (item_id, child3)) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 803 | |
| 804 | # child3 has child2 as parent, thus trying to set child2 as a children |
| 805 | # of child3 should result in an error |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 806 | self.assertRaises(tkinter.TclError, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 807 | self.tv.set_children, child3, child2) |
| 808 | |
| 809 | # remove child2 children |
| 810 | self.tv.set_children(child2) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 811 | self.assertEqual(self.tv.get_children(child2), ()) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 812 | |
| 813 | # remove root's children |
| 814 | self.tv.set_children('') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 815 | self.assertEqual(self.tv.get_children(), ()) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 816 | |
| 817 | |
| 818 | def test_column(self): |
| 819 | # return a dict with all options/values |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 820 | self.assertTrue(isinstance(self.tv.column('#0'), dict)) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 821 | # return a single value of the given option |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 822 | self.assertTrue(isinstance(self.tv.column('#0', width=None), int)) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 823 | # set a new value for an option |
| 824 | self.tv.column('#0', width=10) |
| 825 | # testing new way to get option value |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 826 | self.assertEqual(self.tv.column('#0', 'width'), 10) |
| 827 | self.assertEqual(self.tv.column('#0', width=None), 10) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 828 | # check read-only option |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 829 | self.assertRaises(tkinter.TclError, self.tv.column, '#0', id='X') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 830 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 831 | self.assertRaises(tkinter.TclError, self.tv.column, 'invalid') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 832 | invalid_kws = [ |
| 833 | {'unknown_option': 'some value'}, {'stretch': 'wrong'}, |
| 834 | {'anchor': 'wrong'}, {'width': 'wrong'}, {'minwidth': 'wrong'} |
| 835 | ] |
| 836 | for kw in invalid_kws: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 837 | self.assertRaises(tkinter.TclError, self.tv.column, '#0', |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 838 | **kw) |
| 839 | |
| 840 | |
| 841 | def test_delete(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 842 | self.assertRaises(tkinter.TclError, self.tv.delete, '#0') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 843 | |
| 844 | item_id = self.tv.insert('', 'end') |
| 845 | item2 = self.tv.insert(item_id, 'end') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 846 | self.assertEqual(self.tv.get_children(), (item_id, )) |
| 847 | self.assertEqual(self.tv.get_children(item_id), (item2, )) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 848 | |
| 849 | self.tv.delete(item_id) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 850 | self.assertFalse(self.tv.get_children()) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 851 | |
| 852 | # reattach should fail |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 853 | self.assertRaises(tkinter.TclError, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 854 | self.tv.reattach, item_id, '', 'end') |
| 855 | |
| 856 | # test multiple item delete |
| 857 | item1 = self.tv.insert('', 'end') |
| 858 | item2 = self.tv.insert('', 'end') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 859 | self.assertEqual(self.tv.get_children(), (item1, item2)) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 860 | |
| 861 | self.tv.delete(item1, item2) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 862 | self.assertFalse(self.tv.get_children()) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 863 | |
| 864 | |
| 865 | def test_detach_reattach(self): |
| 866 | item_id = self.tv.insert('', 'end') |
| 867 | item2 = self.tv.insert(item_id, 'end') |
| 868 | |
| 869 | # calling detach without items is valid, although it does nothing |
| 870 | prev = self.tv.get_children() |
| 871 | self.tv.detach() # this should do nothing |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 872 | self.assertEqual(prev, self.tv.get_children()) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 873 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 874 | self.assertEqual(self.tv.get_children(), (item_id, )) |
| 875 | self.assertEqual(self.tv.get_children(item_id), (item2, )) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 876 | |
| 877 | # detach item with children |
| 878 | self.tv.detach(item_id) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 879 | self.assertFalse(self.tv.get_children()) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 880 | |
| 881 | # reattach item with children |
| 882 | self.tv.reattach(item_id, '', 'end') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 883 | self.assertEqual(self.tv.get_children(), (item_id, )) |
| 884 | self.assertEqual(self.tv.get_children(item_id), (item2, )) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 885 | |
| 886 | # move a children to the root |
| 887 | self.tv.move(item2, '', 'end') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 888 | self.assertEqual(self.tv.get_children(), (item_id, item2)) |
| 889 | self.assertEqual(self.tv.get_children(item_id), ()) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 890 | |
| 891 | # bad values |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 892 | self.assertRaises(tkinter.TclError, |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 893 | self.tv.reattach, 'nonexistent', '', 'end') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 894 | self.assertRaises(tkinter.TclError, |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 895 | self.tv.detach, 'nonexistent') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 896 | self.assertRaises(tkinter.TclError, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 897 | self.tv.reattach, item2, 'otherparent', 'end') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 898 | self.assertRaises(tkinter.TclError, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 899 | self.tv.reattach, item2, '', 'invalid') |
| 900 | |
| 901 | # multiple detach |
| 902 | self.tv.detach(item_id, item2) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 903 | self.assertEqual(self.tv.get_children(), ()) |
| 904 | self.assertEqual(self.tv.get_children(item_id), ()) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 905 | |
| 906 | |
| 907 | def test_exists(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 908 | self.assertEqual(self.tv.exists('something'), False) |
| 909 | self.assertEqual(self.tv.exists(''), True) |
| 910 | self.assertEqual(self.tv.exists({}), False) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 911 | |
| 912 | # the following will make a tk.call equivalent to |
| 913 | # tk.call(treeview, "exists") which should result in an error |
| 914 | # in the tcl interpreter since tk requires an item. |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 915 | self.assertRaises(tkinter.TclError, self.tv.exists, None) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 916 | |
| 917 | |
| 918 | def test_focus(self): |
| 919 | # nothing is focused right now |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 920 | self.assertEqual(self.tv.focus(), '') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 921 | |
| 922 | item1 = self.tv.insert('', 'end') |
| 923 | self.tv.focus(item1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 924 | self.assertEqual(self.tv.focus(), item1) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 925 | |
| 926 | self.tv.delete(item1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 927 | self.assertEqual(self.tv.focus(), '') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 928 | |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 929 | # try focusing inexistent item |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 930 | self.assertRaises(tkinter.TclError, self.tv.focus, 'hi') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 931 | |
| 932 | |
| 933 | def test_heading(self): |
| 934 | # check a dict is returned |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 935 | self.assertTrue(isinstance(self.tv.heading('#0'), dict)) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 936 | |
| 937 | # check a value is returned |
| 938 | self.tv.heading('#0', text='hi') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 939 | self.assertEqual(self.tv.heading('#0', 'text'), 'hi') |
| 940 | self.assertEqual(self.tv.heading('#0', text=None), 'hi') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 941 | |
| 942 | # invalid option |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 943 | self.assertRaises(tkinter.TclError, self.tv.heading, '#0', |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 944 | background=None) |
| 945 | # invalid value |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 946 | self.assertRaises(tkinter.TclError, self.tv.heading, '#0', |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 947 | anchor=1) |
| 948 | |
Georg Brandl | cf8a209 | 2010-12-19 10:17:46 +0000 | [diff] [blame] | 949 | # XXX skipping for now; should be fixed to work with newer ttk |
Serhiy Storchaka | ae4ef4d | 2013-03-19 13:25:20 +0200 | [diff] [blame] | 950 | @unittest.skip("skipping pending resolution of Issue #10734") |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 951 | def test_heading_callback(self): |
| 952 | def simulate_heading_click(x, y): |
| 953 | support.simulate_mouse_click(self.tv, x, y) |
| 954 | self.tv.update_idletasks() |
| 955 | |
| 956 | success = [] # no success for now |
| 957 | |
| 958 | self.tv.pack() |
| 959 | self.tv.wait_visibility() |
| 960 | self.tv.heading('#0', command=lambda: success.append(True)) |
| 961 | self.tv.column('#0', width=100) |
| 962 | self.tv.update() |
| 963 | |
| 964 | # assuming that the coords (5, 5) fall into heading #0 |
| 965 | simulate_heading_click(5, 5) |
| 966 | if not success: |
| 967 | self.fail("The command associated to the treeview heading wasn't " |
| 968 | "invoked.") |
| 969 | |
| 970 | success = [] |
| 971 | commands = self.tv.master._tclCommands |
| 972 | self.tv.heading('#0', command=str(self.tv.heading('#0', command=None))) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 973 | self.assertEqual(commands, self.tv.master._tclCommands) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 974 | simulate_heading_click(5, 5) |
| 975 | if not success: |
| 976 | self.fail("The command associated to the treeview heading wasn't " |
| 977 | "invoked.") |
| 978 | |
| 979 | # XXX The following raises an error in a tcl interpreter, but not in |
| 980 | # Python |
| 981 | #self.tv.heading('#0', command='I dont exist') |
| 982 | #simulate_heading_click(5, 5) |
| 983 | |
| 984 | |
| 985 | def test_index(self): |
| 986 | # item 'what' doesn't exist |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 987 | self.assertRaises(tkinter.TclError, self.tv.index, 'what') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 988 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 989 | self.assertEqual(self.tv.index(''), 0) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 990 | |
| 991 | item1 = self.tv.insert('', 'end') |
| 992 | item2 = self.tv.insert('', 'end') |
| 993 | c1 = self.tv.insert(item1, 'end') |
| 994 | c2 = self.tv.insert(item1, 'end') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 995 | self.assertEqual(self.tv.index(item1), 0) |
| 996 | self.assertEqual(self.tv.index(c1), 0) |
| 997 | self.assertEqual(self.tv.index(c2), 1) |
| 998 | self.assertEqual(self.tv.index(item2), 1) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 999 | |
| 1000 | self.tv.move(item2, '', 0) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1001 | self.assertEqual(self.tv.index(item2), 0) |
| 1002 | self.assertEqual(self.tv.index(item1), 1) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1003 | |
| 1004 | # check that index still works even after its parent and siblings |
| 1005 | # have been detached |
| 1006 | self.tv.detach(item1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1007 | self.assertEqual(self.tv.index(c2), 1) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1008 | self.tv.detach(c1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1009 | self.assertEqual(self.tv.index(c2), 0) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1010 | |
| 1011 | # but it fails after item has been deleted |
| 1012 | self.tv.delete(item1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1013 | self.assertRaises(tkinter.TclError, self.tv.index, c2) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1014 | |
| 1015 | |
| 1016 | def test_insert_item(self): |
| 1017 | # parent 'none' doesn't exist |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1018 | self.assertRaises(tkinter.TclError, self.tv.insert, 'none', 'end') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1019 | |
| 1020 | # open values |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1021 | self.assertRaises(tkinter.TclError, self.tv.insert, '', 'end', |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1022 | open='') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1023 | self.assertRaises(tkinter.TclError, self.tv.insert, '', 'end', |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1024 | open='please') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1025 | self.assertFalse(self.tv.delete(self.tv.insert('', 'end', open=True))) |
| 1026 | self.assertFalse(self.tv.delete(self.tv.insert('', 'end', open=False))) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1027 | |
| 1028 | # invalid index |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1029 | self.assertRaises(tkinter.TclError, self.tv.insert, '', 'middle') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1030 | |
| 1031 | # trying to duplicate item id is invalid |
| 1032 | itemid = self.tv.insert('', 'end', 'first-item') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1033 | self.assertEqual(itemid, 'first-item') |
| 1034 | self.assertRaises(tkinter.TclError, self.tv.insert, '', 'end', |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1035 | 'first-item') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1036 | self.assertRaises(tkinter.TclError, self.tv.insert, '', 'end', |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1037 | MockTclObj('first-item')) |
| 1038 | |
| 1039 | # unicode values |
| 1040 | value = '\xe1ba' |
| 1041 | item = self.tv.insert('', 'end', values=(value, )) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1042 | self.assertEqual(self.tv.item(item, 'values'), (value, )) |
| 1043 | self.assertEqual(self.tv.item(item, values=None), (value, )) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1044 | |
| 1045 | self.tv.item(item, values=list(self.tv.item(item, values=None))) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1046 | self.assertEqual(self.tv.item(item, values=None), (value, )) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1047 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1048 | self.assertTrue(isinstance(self.tv.item(item), dict)) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1049 | |
| 1050 | # erase item values |
| 1051 | self.tv.item(item, values='') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1052 | self.assertFalse(self.tv.item(item, values=None)) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1053 | |
| 1054 | # item tags |
| 1055 | item = self.tv.insert('', 'end', tags=[1, 2, value]) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1056 | self.assertEqual(self.tv.item(item, tags=None), ('1', '2', value)) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1057 | self.tv.item(item, tags=[]) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1058 | self.assertFalse(self.tv.item(item, tags=None)) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1059 | self.tv.item(item, tags=(1, 2)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1060 | self.assertEqual(self.tv.item(item, tags=None), ('1', '2')) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1061 | |
| 1062 | # values with spaces |
| 1063 | item = self.tv.insert('', 'end', values=('a b c', |
| 1064 | '%s %s' % (value, value))) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1065 | self.assertEqual(self.tv.item(item, values=None), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1066 | ('a b c', '%s %s' % (value, value))) |
| 1067 | |
| 1068 | # text |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1069 | self.assertEqual(self.tv.item( |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1070 | self.tv.insert('', 'end', text="Label here"), text=None), |
| 1071 | "Label here") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1072 | self.assertEqual(self.tv.item( |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1073 | self.tv.insert('', 'end', text=value), text=None), |
| 1074 | value) |
| 1075 | |
| 1076 | |
| 1077 | def test_set(self): |
| 1078 | self.tv['columns'] = ['A', 'B'] |
| 1079 | item = self.tv.insert('', 'end', values=['a', 'b']) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1080 | self.assertEqual(self.tv.set(item), {'A': 'a', 'B': 'b'}) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1081 | |
| 1082 | self.tv.set(item, 'B', 'a') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1083 | self.assertEqual(self.tv.item(item, values=None), ('a', 'a')) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1084 | |
| 1085 | self.tv['columns'] = ['B'] |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1086 | self.assertEqual(self.tv.set(item), {'B': 'a'}) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1087 | |
| 1088 | self.tv.set(item, 'B', 'b') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1089 | self.assertEqual(self.tv.set(item, column='B'), 'b') |
| 1090 | self.assertEqual(self.tv.item(item, values=None), ('b', 'a')) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1091 | |
| 1092 | self.tv.set(item, 'B', 123) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1093 | self.assertEqual(self.tv.set(item, 'B'), 123) |
| 1094 | self.assertEqual(self.tv.item(item, values=None), (123, 'a')) |
| 1095 | self.assertEqual(self.tv.set(item), {'B': 123}) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1096 | |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 1097 | # inexistent column |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1098 | self.assertRaises(tkinter.TclError, self.tv.set, item, 'A') |
| 1099 | self.assertRaises(tkinter.TclError, self.tv.set, item, 'A', 'b') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1100 | |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 1101 | # inexistent item |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1102 | self.assertRaises(tkinter.TclError, self.tv.set, 'notme') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1103 | |
| 1104 | |
| 1105 | def test_tag_bind(self): |
| 1106 | events = [] |
| 1107 | item1 = self.tv.insert('', 'end', tags=['call']) |
| 1108 | item2 = self.tv.insert('', 'end', tags=['call']) |
| 1109 | self.tv.tag_bind('call', '<ButtonPress-1>', |
| 1110 | lambda evt: events.append(1)) |
| 1111 | self.tv.tag_bind('call', '<ButtonRelease-1>', |
| 1112 | lambda evt: events.append(2)) |
| 1113 | |
| 1114 | self.tv.pack() |
| 1115 | self.tv.wait_visibility() |
| 1116 | self.tv.update() |
| 1117 | |
| 1118 | pos_y = set() |
| 1119 | found = set() |
| 1120 | for i in range(0, 100, 10): |
| 1121 | if len(found) == 2: # item1 and item2 already found |
| 1122 | break |
| 1123 | item_id = self.tv.identify_row(i) |
| 1124 | if item_id and item_id not in found: |
| 1125 | pos_y.add(i) |
| 1126 | found.add(item_id) |
| 1127 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1128 | self.assertEqual(len(pos_y), 2) # item1 and item2 y pos |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1129 | for y in pos_y: |
| 1130 | support.simulate_mouse_click(self.tv, 0, y) |
| 1131 | |
| 1132 | # by now there should be 4 things in the events list, since each |
| 1133 | # item had a bind for two events that were simulated above |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1134 | self.assertEqual(len(events), 4) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1135 | for evt in zip(events[::2], events[1::2]): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1136 | self.assertEqual(evt, (1, 2)) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1137 | |
| 1138 | |
| 1139 | def test_tag_configure(self): |
| 1140 | # Just testing parameter passing for now |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1141 | self.assertRaises(TypeError, self.tv.tag_configure) |
| 1142 | self.assertRaises(tkinter.TclError, self.tv.tag_configure, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1143 | 'test', sky='blue') |
| 1144 | self.tv.tag_configure('test', foreground='blue') |
Benjamin Peterson | a082089 | 2010-04-11 22:04:51 +0000 | [diff] [blame] | 1145 | self.assertEqual(str(self.tv.tag_configure('test', 'foreground')), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1146 | 'blue') |
Benjamin Peterson | a082089 | 2010-04-11 22:04:51 +0000 | [diff] [blame] | 1147 | self.assertEqual(str(self.tv.tag_configure('test', foreground=None)), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1148 | 'blue') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1149 | self.assertTrue(isinstance(self.tv.tag_configure('test'), dict)) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1150 | |
| 1151 | |
| 1152 | tests_gui = ( |
| 1153 | WidgetTest, ButtonTest, CheckbuttonTest, RadiobuttonTest, |
| 1154 | ComboboxTest, EntryTest, PanedwindowTest, ScaleTest, NotebookTest, |
| 1155 | TreeviewTest |
| 1156 | ) |
| 1157 | |
| 1158 | if __name__ == "__main__": |
| 1159 | run_unittest(*tests_gui) |