File: test_gui_application.py

package info (click to toggle)
python-pyface 8.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,944 kB
  • sloc: python: 54,107; makefile: 82
file content (295 lines) | stat: -rw-r--r-- 9,574 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!


import os
from shutil import rmtree
from tempfile import mkdtemp
import unittest

from traits.api import Bool, observe

from ..application_window import ApplicationWindow
from ..gui_application import GUIApplication
from ..toolkit import toolkit_object

GuiTestAssistant = toolkit_object("util.gui_test_assistant:GuiTestAssistant")
no_gui_test_assistant = GuiTestAssistant.__name__ == "Unimplemented"

EVENTS = [
    "starting",
    "started",
    "application_initialized",
    "stopping",
    "stopped",
]


class TestingApp(GUIApplication):

    #: Whether the app should start cleanly.
    start_cleanly = Bool(True)

    #: Whether the app should stop cleanly.
    stop_cleanly = Bool(True)

    #: Whether to try invoking exit method.
    do_exit = Bool(False)

    #: Whether the exit should be invoked as an error exit.
    error_exit = Bool(False)

    #: Whether to try force the exit (ie. ignore vetoes).
    force_exit = Bool(False)

    #: Whether to veto a call to the exit method.
    veto_exit = Bool(False)

    #: Whether to veto a opening a window.
    veto_open_window = Bool(False)

    #: Whether to veto a closing a window.
    veto_close_window = Bool(False)

    #: Whether or not a call to the open a window was vetoed.
    window_open_vetoed = Bool(False)

    #: Whether or not a call to the exit method was vetoed.
    exit_vetoed = Bool(False)

    #: Whether exit preparation happened.
    exit_prepared = Bool(False)

    #: Whether exit preparation raises an error.
    exit_prepared_error = Bool(False)

    def start(self):
        if not self.start_cleanly:
            return False
        super().start()

        window = self.windows[0]
        window.observe(self._on_window_closing, "closing")
        return True

    def stop(self):
        super().stop()
        return self.stop_cleanly

    def _on_window_closing(self, event):
        window = event.new
        if self.veto_close_window and not self.exit_vetoed:
            window.veto = True
            self.exit_vetoed = True

    @observe("application_initialized")
    def _update_window_open_vetoed(self, event):
        self.window_open_vetoed = (
            len(self.windows) > 0 and self.windows[0].control is None
        )

    @observe('exiting')
    def _set_veto_on_exiting_event(self, event):
        vetoable_event = event.new
        vetoable_event.veto = self.veto_exit
        self.exit_vetoed = self.veto_exit

    def _prepare_exit(self):
        super()._prepare_exit()
        if not self.exit_vetoed:
            self.exit_prepared = True
        if self.exit_prepared_error:
            raise Exception("Exit preparation failed")

    @observe("windows:items:opening")
    def _on_activate_window(self, event):
        if self.veto_open_window:
            window = event.new
            window.veto = self.veto_open_window


@unittest.skipIf(no_gui_test_assistant, "No GuiTestAssistant")
class TestGUIApplication(unittest.TestCase, GuiTestAssistant):
    def setUp(self):
        GuiTestAssistant.setUp(self)
        self.application_events = []

        if toolkit_object.toolkit == "wx":
            import wx

            self.event_loop()
            wx.GetApp().DeletePendingEvents()
        else:
            self.event_loop()

    def tearDown(self):
        GuiTestAssistant.tearDown(self)

    def event_listener(self, event):
        application_event = event.new
        self.application_events.append(application_event)

    def connect_listeners(self, app):
        for event in EVENTS:
            app.observe(self.event_listener, event)

    def test_defaults(self):
        from traits.etsconfig.api import ETSConfig

        app = GUIApplication()

        self.assertEqual(app.home, ETSConfig.application_home)
        self.assertEqual(app.user_data, ETSConfig.user_data)
        self.assertEqual(app.company, ETSConfig.company)

    def test_initialize_application_home(self):
        dirpath = mkdtemp()
        home = os.path.join(dirpath, "test")
        app = GUIApplication(home=home)

        app.initialize_application_home()
        try:
            self.assertTrue(os.path.exists(home))
        finally:
            rmtree(dirpath)

    def test_lifecycle(self):

        app = GUIApplication()
        self.connect_listeners(app)
        window = ApplicationWindow()
        app.observe(lambda _: app.add_window(window), "started")

        with self.assertMultiTraitChanges([app], EVENTS, []):
            self.gui.invoke_after(1000, app.exit)
            result = app.run()

        self.assertTrue(result)
        event_order = [event.event_type for event in self.application_events]
        self.assertEqual(event_order, EVENTS)
        self.assertEqual(app.windows, [])

    def test_exit_prepare_error(self):
        app = TestingApp(exit_prepared_error=True)
        self.connect_listeners(app)

        with self.assertMultiTraitChanges([app], EVENTS, []):
            self.gui.invoke_after(1000, app.exit)
            result = app.run()

        self.assertTrue(result)
        self.assertFalse(app.exit_vetoed)
        self.assertTrue(app.exit_prepared)
        event_order = [event.event_type for event in self.application_events]
        self.assertEqual(event_order, EVENTS)
        self.assertEqual(app.windows, [])

    def test_veto_exit(self):
        app = TestingApp(veto_exit=True)
        self.connect_listeners(app)

        with self.assertMultiTraitChanges([app], EVENTS, []):
            self.gui.invoke_after(1000, app.exit)
            self.gui.invoke_after(2000, app.exit, force=True)
            result = app.run()

        self.assertTrue(result)
        self.assertTrue(app.exit_vetoed)
        self.assertFalse(app.exit_prepared)
        event_order = [event.event_type for event in self.application_events]
        self.assertEqual(event_order, EVENTS)
        self.assertEqual(app.windows, [])

    def test_veto_open_window(self):
        app = TestingApp(veto_open_window=True)
        self.connect_listeners(app)

        with self.assertMultiTraitChanges([app], EVENTS, []):
            self.gui.invoke_after(1000, app.exit)
            result = app.run()

        self.assertTrue(result)
        self.assertTrue(app.window_open_vetoed)
        self.assertFalse(app.exit_vetoed)
        self.assertTrue(app.exit_prepared)
        event_order = [event.event_type for event in self.application_events]
        self.assertEqual(event_order, EVENTS)
        self.assertEqual(app.windows, [])

    def test_veto_close_window(self):
        app = TestingApp(veto_close_window=True)
        self.connect_listeners(app)

        with self.assertMultiTraitChanges([app], EVENTS, []):
            self.gui.invoke_after(1000, app.exit)
            self.gui.invoke_after(2000, app.exit, force=True)
            result = app.run()

        self.assertTrue(result)
        self.assertTrue(app.exit_vetoed)
        self.assertFalse(app.exit_prepared)
        event_order = [event.event_type for event in self.application_events]
        self.assertEqual(event_order, EVENTS)
        self.assertEqual(app.windows, [])

    def test_force_exit(self):
        app = TestingApp(do_exit=True, force_exit=True, veto_exit=True)
        self.connect_listeners(app)

        with self.assertMultiTraitChanges([app], EVENTS, []):
            self.gui.invoke_after(100, app.exit, True)
            result = app.run()

        self.assertTrue(result)
        self.assertFalse(app.exit_vetoed)
        self.assertTrue(app.exit_prepared)
        event_order = [event.event_type for event in self.application_events]
        self.assertEqual(event_order, EVENTS)
        self.assertEqual(app.windows, [])

    def test_force_exit_close_veto(self):
        app = TestingApp(do_exit=True, force_exit=True, veto_close_window=True)
        self.connect_listeners(app)

        with self.assertMultiTraitChanges([app], EVENTS, []):
            self.gui.invoke_after(1000, app.exit, True)
            result = app.run()

        self.assertTrue(result)
        self.assertFalse(app.exit_vetoed)
        self.assertTrue(app.exit_prepared)
        event_order = [event.event_type for event in self.application_events]
        self.assertEqual(event_order, EVENTS)
        self.assertEqual(app.windows, [])

    def test_bad_start(self):
        app = TestingApp(start_cleanly=False)
        self.connect_listeners(app)

        with self.assertMultiTraitChanges([app], EVENTS[:1], EVENTS[1:]):
            result = app.run()

        self.assertFalse(result)
        event_order = [event.event_type for event in self.application_events]
        self.assertEqual(event_order, EVENTS[:1])
        self.assertEqual(app.windows, [])

    def test_bad_stop(self):
        app = TestingApp(stop_cleanly=False)
        self.connect_listeners(app)

        with self.assertMultiTraitChanges([app], EVENTS[:-1], EVENTS[-1:]):
            self.gui.invoke_after(1000, app.exit, True)
            result = app.run()

        self.assertFalse(result)
        event_order = [event.event_type for event in self.application_events]
        self.assertEqual(event_order, EVENTS[:-1])
        self.assertEqual(app.windows, [])