test_misc.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import unittest
  2. import tkinter
  3. from test import support
  4. from tkinter.test.support import AbstractTkTest
  5. support.requires('gui')
  6. class MiscTest(AbstractTkTest, unittest.TestCase):
  7. def test_repr(self):
  8. t = tkinter.Toplevel(self.root, name='top')
  9. f = tkinter.Frame(t, name='child')
  10. self.assertEqual(repr(f), '<tkinter.Frame object .top.child>')
  11. def test_generated_names(self):
  12. t = tkinter.Toplevel(self.root)
  13. f = tkinter.Frame(t)
  14. f2 = tkinter.Frame(t)
  15. b = tkinter.Button(f2)
  16. for name in str(b).split('.'):
  17. self.assertFalse(name.isidentifier(), msg=repr(name))
  18. def test_tk_setPalette(self):
  19. root = self.root
  20. root.tk_setPalette('black')
  21. self.assertEqual(root['background'], 'black')
  22. root.tk_setPalette('white')
  23. self.assertEqual(root['background'], 'white')
  24. self.assertRaisesRegex(tkinter.TclError,
  25. '^unknown color name "spam"$',
  26. root.tk_setPalette, 'spam')
  27. root.tk_setPalette(background='black')
  28. self.assertEqual(root['background'], 'black')
  29. root.tk_setPalette(background='blue', highlightColor='yellow')
  30. self.assertEqual(root['background'], 'blue')
  31. self.assertEqual(root['highlightcolor'], 'yellow')
  32. root.tk_setPalette(background='yellow', highlightColor='blue')
  33. self.assertEqual(root['background'], 'yellow')
  34. self.assertEqual(root['highlightcolor'], 'blue')
  35. self.assertRaisesRegex(tkinter.TclError,
  36. '^unknown color name "spam"$',
  37. root.tk_setPalette, background='spam')
  38. self.assertRaisesRegex(tkinter.TclError,
  39. '^must specify a background color$',
  40. root.tk_setPalette, spam='white')
  41. self.assertRaisesRegex(tkinter.TclError,
  42. '^must specify a background color$',
  43. root.tk_setPalette, highlightColor='blue')
  44. def test_after(self):
  45. root = self.root
  46. def callback(start=0, step=1):
  47. nonlocal count
  48. count = start + step
  49. # Without function, sleeps for ms.
  50. self.assertIsNone(root.after(1))
  51. # Set up with callback with no args.
  52. count = 0
  53. timer1 = root.after(0, callback)
  54. self.assertIn(timer1, root.tk.call('after', 'info'))
  55. (script, _) = root.tk.splitlist(root.tk.call('after', 'info', timer1))
  56. root.update() # Process all pending events.
  57. self.assertEqual(count, 1)
  58. with self.assertRaises(tkinter.TclError):
  59. root.tk.call(script)
  60. # Set up with callback with args.
  61. count = 0
  62. timer1 = root.after(0, callback, 42, 11)
  63. root.update() # Process all pending events.
  64. self.assertEqual(count, 53)
  65. # Cancel before called.
  66. timer1 = root.after(1000, callback)
  67. self.assertIn(timer1, root.tk.call('after', 'info'))
  68. (script, _) = root.tk.splitlist(root.tk.call('after', 'info', timer1))
  69. root.after_cancel(timer1) # Cancel this event.
  70. self.assertEqual(count, 53)
  71. with self.assertRaises(tkinter.TclError):
  72. root.tk.call(script)
  73. def test_after_idle(self):
  74. root = self.root
  75. def callback(start=0, step=1):
  76. nonlocal count
  77. count = start + step
  78. # Set up with callback with no args.
  79. count = 0
  80. idle1 = root.after_idle(callback)
  81. self.assertIn(idle1, root.tk.call('after', 'info'))
  82. (script, _) = root.tk.splitlist(root.tk.call('after', 'info', idle1))
  83. root.update_idletasks() # Process all pending events.
  84. self.assertEqual(count, 1)
  85. with self.assertRaises(tkinter.TclError):
  86. root.tk.call(script)
  87. # Set up with callback with args.
  88. count = 0
  89. idle1 = root.after_idle(callback, 42, 11)
  90. root.update_idletasks() # Process all pending events.
  91. self.assertEqual(count, 53)
  92. # Cancel before called.
  93. idle1 = root.after_idle(callback)
  94. self.assertIn(idle1, root.tk.call('after', 'info'))
  95. (script, _) = root.tk.splitlist(root.tk.call('after', 'info', idle1))
  96. root.after_cancel(idle1) # Cancel this event.
  97. self.assertEqual(count, 53)
  98. with self.assertRaises(tkinter.TclError):
  99. root.tk.call(script)
  100. def test_after_cancel(self):
  101. root = self.root
  102. def callback():
  103. nonlocal count
  104. count += 1
  105. timer1 = root.after(5000, callback)
  106. idle1 = root.after_idle(callback)
  107. # No value for id raises a ValueError.
  108. with self.assertRaises(ValueError):
  109. root.after_cancel(None)
  110. # Cancel timer event.
  111. count = 0
  112. (script, _) = root.tk.splitlist(root.tk.call('after', 'info', timer1))
  113. root.tk.call(script)
  114. self.assertEqual(count, 1)
  115. root.after_cancel(timer1)
  116. with self.assertRaises(tkinter.TclError):
  117. root.tk.call(script)
  118. self.assertEqual(count, 1)
  119. with self.assertRaises(tkinter.TclError):
  120. root.tk.call('after', 'info', timer1)
  121. # Cancel same event - nothing happens.
  122. root.after_cancel(timer1)
  123. # Cancel idle event.
  124. count = 0
  125. (script, _) = root.tk.splitlist(root.tk.call('after', 'info', idle1))
  126. root.tk.call(script)
  127. self.assertEqual(count, 1)
  128. root.after_cancel(idle1)
  129. with self.assertRaises(tkinter.TclError):
  130. root.tk.call(script)
  131. self.assertEqual(count, 1)
  132. with self.assertRaises(tkinter.TclError):
  133. root.tk.call('after', 'info', idle1)
  134. def test_clipboard(self):
  135. root = self.root
  136. root.clipboard_clear()
  137. root.clipboard_append('Ùñî')
  138. self.assertEqual(root.clipboard_get(), 'Ùñî')
  139. root.clipboard_append('çōđě')
  140. self.assertEqual(root.clipboard_get(), 'Ùñîçōđě')
  141. root.clipboard_clear()
  142. with self.assertRaises(tkinter.TclError):
  143. root.clipboard_get()
  144. def test_clipboard_astral(self):
  145. root = self.root
  146. root.clipboard_clear()
  147. root.clipboard_append('𝔘𝔫𝔦')
  148. self.assertEqual(root.clipboard_get(), '𝔘𝔫𝔦')
  149. root.clipboard_append('𝔠𝔬𝔡𝔢')
  150. self.assertEqual(root.clipboard_get(), '𝔘𝔫𝔦𝔠𝔬𝔡𝔢')
  151. root.clipboard_clear()
  152. with self.assertRaises(tkinter.TclError):
  153. root.clipboard_get()
  154. tests_gui = (MiscTest, )
  155. if __name__ == "__main__":
  156. support.run_unittest(*tests_gui)