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