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