test_macosx.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. "Test macosx, coverage 45% on Windows."
  2. from idlelib import macosx
  3. import unittest
  4. from test.support import requires
  5. import tkinter as tk
  6. import unittest.mock as mock
  7. from idlelib.filelist import FileList
  8. mactypes = {'carbon', 'cocoa', 'xquartz'}
  9. nontypes = {'other'}
  10. alltypes = mactypes | nontypes
  11. def setUpModule():
  12. global orig_tktype
  13. orig_tktype = macosx._tk_type
  14. def tearDownModule():
  15. macosx._tk_type = orig_tktype
  16. class InitTktypeTest(unittest.TestCase):
  17. "Test _init_tk_type."
  18. @classmethod
  19. def setUpClass(cls):
  20. requires('gui')
  21. cls.root = tk.Tk()
  22. cls.root.withdraw()
  23. cls.orig_platform = macosx.platform
  24. @classmethod
  25. def tearDownClass(cls):
  26. cls.root.update_idletasks()
  27. cls.root.destroy()
  28. del cls.root
  29. macosx.platform = cls.orig_platform
  30. def test_init_sets_tktype(self):
  31. "Test that _init_tk_type sets _tk_type according to platform."
  32. for platform, types in ('darwin', alltypes), ('other', nontypes):
  33. with self.subTest(platform=platform):
  34. macosx.platform = platform
  35. macosx._tk_type = None
  36. macosx._init_tk_type()
  37. self.assertIn(macosx._tk_type, types)
  38. class IsTypeTkTest(unittest.TestCase):
  39. "Test each of the four isTypeTk predecates."
  40. isfuncs = ((macosx.isAquaTk, ('carbon', 'cocoa')),
  41. (macosx.isCarbonTk, ('carbon')),
  42. (macosx.isCocoaTk, ('cocoa')),
  43. (macosx.isXQuartz, ('xquartz')),
  44. )
  45. @mock.patch('idlelib.macosx._init_tk_type')
  46. def test_is_calls_init(self, mockinit):
  47. "Test that each isTypeTk calls _init_tk_type when _tk_type is None."
  48. macosx._tk_type = None
  49. for func, whentrue in self.isfuncs:
  50. with self.subTest(func=func):
  51. func()
  52. self.assertTrue(mockinit.called)
  53. mockinit.reset_mock()
  54. def test_isfuncs(self):
  55. "Test that each isTypeTk return correct bool."
  56. for func, whentrue in self.isfuncs:
  57. for tktype in alltypes:
  58. with self.subTest(func=func, whentrue=whentrue, tktype=tktype):
  59. macosx._tk_type = tktype
  60. (self.assertTrue if tktype in whentrue else self.assertFalse)\
  61. (func())
  62. class SetupTest(unittest.TestCase):
  63. "Test setupApp."
  64. @classmethod
  65. def setUpClass(cls):
  66. requires('gui')
  67. cls.root = tk.Tk()
  68. cls.root.withdraw()
  69. def cmd(tkpath, func):
  70. assert isinstance(tkpath, str)
  71. assert isinstance(func, type(cmd))
  72. cls.root.createcommand = cmd
  73. @classmethod
  74. def tearDownClass(cls):
  75. cls.root.update_idletasks()
  76. cls.root.destroy()
  77. del cls.root
  78. @mock.patch('idlelib.macosx.overrideRootMenu') #27312
  79. def test_setupapp(self, overrideRootMenu):
  80. "Call setupApp with each possible graphics type."
  81. root = self.root
  82. flist = FileList(root)
  83. for tktype in alltypes:
  84. with self.subTest(tktype=tktype):
  85. macosx._tk_type = tktype
  86. macosx.setupApp(root, flist)
  87. if tktype in ('carbon', 'cocoa'):
  88. self.assertTrue(overrideRootMenu.called)
  89. overrideRootMenu.reset_mock()
  90. if __name__ == '__main__':
  91. unittest.main(verbosity=2)