1# (C) Copyright 2005-2020 Enthought, Inc., Austin, TX
2# All rights reserved.
3#
4# This software is provided without warranty under the terms of the BSD
5# license included in LICENSE.txt and may be redistributed only under
6# the conditions described in the aforementioned license. The license
7# is also available online at http://www.enthought.com/licenses/BSD.txt
8#
9# Thanks for using Enthought open source!
10
11
12import unittest
13
14from ..action.api import (
15    Action,
16    MenuManager,
17    MenuBarManager,
18    StatusBarManager,
19    ToolBarManager,
20)
21from ..application_window import ApplicationWindow
22from ..toolkit import toolkit_object
23from ..image_resource import ImageResource
24
25GuiTestAssistant = toolkit_object("util.gui_test_assistant:GuiTestAssistant")
26no_gui_test_assistant = GuiTestAssistant.__name__ == "Unimplemented"
27
28
29@unittest.skipIf(no_gui_test_assistant, "No GuiTestAssistant")
30class TestApplicationWindow(unittest.TestCase, GuiTestAssistant):
31    def setUp(self):
32        GuiTestAssistant.setUp(self)
33        self.window = ApplicationWindow()
34
35    def tearDown(self):
36        if self.window.control is not None:
37            with self.delete_widget(self.window.control):
38                self.window.destroy()
39        GuiTestAssistant.tearDown(self)
40
41    def test_close(self):
42        # test that close works even when no control
43        self.window.close()
44
45    def test_open_close(self):
46        # test that opening and closing works as expected
47        with self.assertTraitChanges(self.window, "opening", count=1):
48            with self.assertTraitChanges(self.window, "opened", count=1):
49                with self.event_loop():
50                    self.window.open()
51        with self.assertTraitChanges(self.window, "closing", count=1):
52            with self.assertTraitChanges(self.window, "closed", count=1):
53                with self.event_loop():
54                    self.window.close()
55
56    def test_show(self):
57        # test that show and hide works as expected
58        with self.event_loop():
59            self.window._create()
60            self.window.show(True)
61        with self.event_loop():
62            self.window.show(False)
63        with self.event_loop():
64            self.window.close()
65
66    def test_activate(self):
67        # test that activation works as expected
68        with self.event_loop():
69            self.window.open()
70        with self.event_loop():
71            self.window.activate()
72        with self.event_loop():
73            self.window.close()
74
75    def test_position(self):
76        # test that default position works as expected
77        self.window.position = (100, 100)
78        with self.event_loop():
79            self.window.open()
80        with self.event_loop():
81            self.window.close()
82
83    def test_reposition(self):
84        # test that changing position works as expected
85        with self.event_loop():
86            self.window.open()
87        with self.event_loop():
88            self.window.position = (100, 100)
89        with self.event_loop():
90            self.window.close()
91
92    def test_size(self):
93        # test that default size works as expected
94        self.window.size = (100, 100)
95        with self.event_loop():
96            self.window.open()
97        with self.event_loop():
98            self.window.close()
99
100    def test_resize(self):
101        # test that changing size works as expected
102        self.window.open()
103        with self.event_loop():
104            self.window.size = (100, 100)
105        with self.event_loop():
106            self.window.close()
107
108    def test_title(self):
109        # test that default title works as expected
110        self.window.title = "Test Title"
111        with self.event_loop():
112            self.window.open()
113        with self.event_loop():
114            self.window.close()
115
116    def test_retitle(self):
117        # test that changing title works as expected
118        with self.event_loop():
119            self.window.open()
120        with self.event_loop():
121            self.window.title = "Test Title"
122        with self.event_loop():
123            self.window.close()
124
125    def test_menubar(self):
126        # test that menubar gets created as expected
127        self.window.menu_bar_manager = MenuBarManager(
128            MenuManager(
129                Action(name="New"),
130                Action(name="Open"),
131                Action(name="Save"),
132                Action(name="Close"),
133                Action(name="Quit"),
134                name="File",
135            )
136        )
137        with self.event_loop():
138            self.window._create()
139        with self.event_loop():
140            self.window.show(True)
141        with self.event_loop():
142            self.window.show(False)
143        with self.event_loop():
144            self.window.close()
145
146    def test_menubar_multiple_menus(self):
147        # test that menubar gets created as expected
148        self.window.menu_bar_manager = MenuBarManager(
149            MenuManager(
150                Action(name="New"),
151                Action(name="Open"),
152                Action(name="Save"),
153                Action(name="Close"),
154                Action(name="Quit"),
155                name="File",
156            ),
157            MenuManager(
158                Action(name="Zoom in"),
159                Action(name="Zoom out"),
160                name="View",
161            )
162        )
163        with self.event_loop():
164            self.window._create()
165        with self.event_loop():
166            self.window.show(True)
167        with self.event_loop():
168            self.window.show(False)
169        with self.event_loop():
170            self.window.close()
171
172    def test_toolbar(self):
173        # test that toolbar gets created as expected
174        self.window.tool_bar_managers = [
175            ToolBarManager(
176                Action(name="New", image=ImageResource("core")),
177                Action(name="Open", image=ImageResource("core")),
178                Action(name="Save", image=ImageResource("core")),
179                Action(name="Close", image=ImageResource("core")),
180                Action(name="Quit", image=ImageResource("core")),
181            )
182        ]
183        with self.event_loop():
184            self.window._create()
185        with self.event_loop():
186            self.window.show(True)
187        with self.event_loop():
188            self.window.show(False)
189        with self.event_loop():
190            self.window.close()
191
192    def test_toolbar_changed(self):
193        # test that toolbar gets changed as expected
194        self.window.tool_bar_managers = [
195            ToolBarManager(
196                Action(name="New", image=ImageResource("core")),
197                Action(name="Open", image=ImageResource("core")),
198                Action(name="Save", image=ImageResource("core")),
199                Action(name="Close", image=ImageResource("core")),
200                Action(name="Quit", image=ImageResource("core")),
201            )
202        ]
203        with self.event_loop():
204            self.window._create()
205        with self.event_loop():
206            self.window.show(True)
207        with self.event_loop():
208            self.window.tool_bar_managers = [
209                ToolBarManager(
210                    Action(name="New", image=ImageResource("core")),
211                    Action(name="Open", image=ImageResource("core")),
212                    Action(name="Save", image=ImageResource("core")),
213                    Action(name="Close", image=ImageResource("core")),
214                    Action(name="Quit", image=ImageResource("core")),
215                )
216            ]
217        with self.event_loop():
218            self.window.show(False)
219        with self.event_loop():
220            self.window.close()
221
222    def test_statusbar(self):
223        # test that status bar gets created as expected
224        self.window.status_bar_manager = StatusBarManager(
225            message="hello world"
226        )
227        with self.event_loop():
228            self.window._create()
229        with self.event_loop():
230            self.window.show(True)
231        with self.event_loop():
232            self.window.show(False)
233        with self.event_loop():
234            self.window.close()
235
236    def test_statusbar_changed(self):
237        # test that status bar gets changed as expected
238        self.window.status_bar_manager = StatusBarManager(
239            message="hello world"
240        )
241        with self.event_loop():
242            self.window._create()
243        with self.event_loop():
244            self.window.show(True)
245        with self.event_loop():
246            self.window.status_bar_manager = StatusBarManager(
247                message="goodbye world"
248            )
249        with self.event_loop():
250            self.window.show(False)
251        with self.event_loop():
252            self.window.close()
253
254    def test_icon(self):
255        # test that status bar gets created as expected
256        self.window.icon = ImageResource("core")
257        with self.event_loop():
258            self.window._create()
259        with self.event_loop():
260            self.window.show(True)
261        with self.event_loop():
262            self.window.show(False)
263        with self.event_loop():
264            self.window.close()
265