test_macosx.py 3.2 KB

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