1import unittest 2import Tkinter as tkinter 3from test.test_support import requires, run_unittest 4from test_ttk.support import AbstractTkTest 5 6requires('gui') 7 8class MiscTest(AbstractTkTest, unittest.TestCase): 9 10 def test_after(self): 11 root = self.root 12 cbcount = {'count': 0} 13 14 def callback(start=0, step=1): 15 cbcount['count'] = start + step 16 17 # Without function, sleeps for ms. 18 self.assertIsNone(root.after(1)) 19 20 # Set up with callback with no args. 21 cbcount['count'] = 0 22 timer1 = root.after(0, callback) 23 self.assertIn(timer1, root.tk.call('after', 'info')) 24 (script, _) = root.tk.splitlist(root.tk.call('after', 'info', timer1)) 25 root.update() # Process all pending events. 26 self.assertEqual(cbcount['count'], 1) 27 with self.assertRaises(tkinter.TclError): 28 root.tk.call(script) 29 30 # Set up with callback with args. 31 cbcount['count'] = 0 32 timer1 = root.after(0, callback, 42, 11) 33 root.update() # Process all pending events. 34 self.assertEqual(cbcount['count'], 53) 35 36 # Cancel before called. 37 timer1 = root.after(1000, callback) 38 self.assertIn(timer1, root.tk.call('after', 'info')) 39 (script, _) = root.tk.splitlist(root.tk.call('after', 'info', timer1)) 40 root.after_cancel(timer1) # Cancel this event. 41 self.assertEqual(cbcount['count'], 53) 42 with self.assertRaises(tkinter.TclError): 43 root.tk.call(script) 44 45 def test_after_idle(self): 46 root = self.root 47 cbcount = {'count': 0} 48 49 def callback(start=0, step=1): 50 cbcount['count'] = start + step 51 52 # Set up with callback with no args. 53 cbcount['count'] = 0 54 idle1 = root.after_idle(callback) 55 self.assertIn(idle1, root.tk.call('after', 'info')) 56 (script, _) = root.tk.splitlist(root.tk.call('after', 'info', idle1)) 57 root.update_idletasks() # Process all pending events. 58 self.assertEqual(cbcount['count'], 1) 59 with self.assertRaises(tkinter.TclError): 60 root.tk.call(script) 61 62 # Set up with callback with args. 63 cbcount['count'] = 0 64 idle1 = root.after_idle(callback, 42, 11) 65 root.update_idletasks() # Process all pending events. 66 self.assertEqual(cbcount['count'], 53) 67 68 # Cancel before called. 69 idle1 = root.after_idle(callback) 70 self.assertIn(idle1, root.tk.call('after', 'info')) 71 (script, _) = root.tk.splitlist(root.tk.call('after', 'info', idle1)) 72 root.after_cancel(idle1) # Cancel this event. 73 self.assertEqual(cbcount['count'], 53) 74 with self.assertRaises(tkinter.TclError): 75 root.tk.call(script) 76 77 def test_after_cancel(self): 78 root = self.root 79 cbcount = {'count': 0} 80 81 def callback(): 82 cbcount['count'] += 1 83 84 timer1 = root.after(5000, callback) 85 idle1 = root.after_idle(callback) 86 87 # No value for id raises a ValueError. 88 with self.assertRaises(ValueError): 89 root.after_cancel(None) 90 91 # Cancel timer event. 92 cbcount['count'] = 0 93 (script, _) = root.tk.splitlist(root.tk.call('after', 'info', timer1)) 94 root.tk.call(script) 95 self.assertEqual(cbcount['count'], 1) 96 root.after_cancel(timer1) 97 with self.assertRaises(tkinter.TclError): 98 root.tk.call(script) 99 self.assertEqual(cbcount['count'], 1) 100 with self.assertRaises(tkinter.TclError): 101 root.tk.call('after', 'info', timer1) 102 103 # Cancel same event - nothing happens. 104 root.after_cancel(timer1) 105 106 # Cancel idle event. 107 cbcount['count'] = 0 108 (script, _) = root.tk.splitlist(root.tk.call('after', 'info', idle1)) 109 root.tk.call(script) 110 self.assertEqual(cbcount['count'], 1) 111 root.after_cancel(idle1) 112 with self.assertRaises(tkinter.TclError): 113 root.tk.call(script) 114 self.assertEqual(cbcount['count'], 1) 115 with self.assertRaises(tkinter.TclError): 116 root.tk.call('after', 'info', idle1) 117 118 119tests_gui = (MiscTest, ) 120 121if __name__ == "__main__": 122 run_unittest(*tests_gui) 123