Debian Codegraph @

envisage/ui/tasks/tests/test_tasks_application.py

raw ◀ ▶
1 Debian patch touch this file
1 # (C) Copyright 2007-2025 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 # Standard library imports.
12 import os
13 import pathlib
14 import shutil
15 import tempfile
16 import unittest
17
•18 try:
19 from importlib.resources import files
20 except ImportError:
21 from importlib_resources import files
22
•23 # Enthought library imports.
24 from pyface.gui import GUI
25 from pyface.i_gui import IGUI
26 from traits.api import Event, HasTraits, provides
27
28 from envisage.api import Plugin
29 from envisage.tests.support import requires_gui
30 from envisage.ui.tasks.api import TasksApplication
31 from envisage.ui.tasks.tasks_application import DEFAULT_STATE_FILENAME
32
33
34 @provides(IGUI)
35 class DummyGUI(HasTraits):
36 pass
37
38
39 class LifecycleRecordingPlugin(Plugin):
40 """
41 Plugin that fires events when started and stopped.
42 """
43
44 #: Event fired when plugin starts
45 started = Event()
46
47 #: Event fired when plugin stops
48 stopped = Event()
49
50 def start(self):
51 self.started = True
52
53 def stop(self):
54 self.stopped = True
55
56
57 class LifecycleRecordingGUI(GUI):
58 """
59 GUI subclass that adds events for watching start and stop of event loop.
60 """
61
62 #: Event fired just before we start the event loop.
63 starting = Event
64
65 #: Event fired just after we've exited the event loop.
66 stopped = Event
67
68 def start_event_loop(self):
69 """
70 Extend the base class method to fire the additional events.
71 """
72 self.starting = True
73 super().start_event_loop()
74 self.stopped = True
75
76
77 @requires_gui
78 class TestTasksApplication(unittest.TestCase):
79 def setUp(self):
80 self.tmpdir = tempfile.mkdtemp()
81 self.addCleanup(shutil.rmtree, self.tmpdir)
82
83 def test_layout_save_with_protocol_3(self):
84 # Test that the protocol can be overridden on a per-application basis.
85 state_location = self.tmpdir
86
87 # Create application, and set it up to exit as soon as it's launched.
88 app = TasksApplication(
89 state_location=state_location,
90 layout_save_protocol=3,
91 )
92 app.on_trait_change(app.exit, "application_initialized")
93
94 memento_file = os.path.join(state_location, app.state_filename)
95 self.assertFalse(os.path.exists(memento_file))
96 app.run()
97 self.assertTrue(os.path.exists(memento_file))
98
99 # Check that the generated file uses protocol 3.
100 with open(memento_file, "rb") as f:
101 protocol_bytes = f.read(2)
102 self.assertEqual(protocol_bytes, b"\x80\x03")
103
104 def test_layout_save_creates_directory(self):
105 # Test that state can still be saved if the target directory
106 # doesn't exist.
107 state_location = pathlib.Path(self.tmpdir) / "subdir"
108 state_filename = "memento_test"
109 state_path = state_location / state_filename
110
111 self.assertFalse(state_location.exists())
112 self.assertFalse(state_path.exists())
113
114 # Create application and set it up to exit as soon as it's launched.
115 app = TasksApplication(
116 state_location=state_location,
117 state_filename=state_filename,
118 )
119 app.on_trait_change(app.exit, "application_initialized")
120 app.run()
121
122 self.assertTrue(state_location.exists())
123 self.assertTrue(state_path.exists())
124
125 def test_layout_load(self):
126 # Check we can load a previously-created state. That previous state
127 # has an main window size of (492, 743) (to allow us to check that
128 # we're actually using the file).
•129 stored_state_location = files("envisage.ui.tasks.tests") / "data"
130 state_location = pathlib.Path(self.tmpdir)
131 (state_location / DEFAULT_STATE_FILENAME).write_bytes(
132 (stored_state_location / "application_memento_v2.pkl").read_bytes()
133 )
134
135 app = TasksApplication(state_location=state_location)
136 app.on_trait_change(app.exit, "application_initialized")
137 app.run()
138
139 state = app._state
140 self.assertEqual(state.previous_window_layouts[0].size, (492, 743))
141
142 def test_layout_load_pickle_protocol_3(self):
143 # Same as the above test, but using a state stored with pickle
144 # protocol 3.
•145 stored_state_location = files("envisage.ui.tasks.tests") / "data"
146
•147 state_location = pathlib.Path(self.tmpdir)
148 (state_location / "fancy_state.pkl").write_bytes(
149 (stored_state_location / "application_memento_v3.pkl").read_bytes()
150 )
151
152 # Use a non-standard filename, to exercise that machinery.
153 app = TasksApplication(
154 state_location=state_location,
155 state_filename="fancy_state.pkl",
156 )
157 app.on_trait_change(app.exit, "application_initialized")
158 app.run()
159
160 state = app._state
161 self.assertEqual(state.previous_window_layouts[0].size, (492, 743))
162
163 def test_gui_trait_expects_IGUI_interface(self):
164 # Trivial test where we simply set the trait
165 # and the test passes because no errors are raised.
166 app = TasksApplication()
167 app.gui = DummyGUI()
168
169 def test_simple_lifecycle(self):
170 app = TasksApplication(state_location=self.tmpdir)
171 app.observe(lambda event: app.exit(), "application_initialized")
172 app.run()
173
174 def test_lifecycle_with_plugin(self):
175 events = []
176 plugin = LifecycleRecordingPlugin(record_to=events)
177 plugin.observe(events.append, "started,stopped")
178
179 gui = LifecycleRecordingGUI()
180 gui.observe(events.append, "starting,stopped")
181
182 app = TasksApplication(
183 gui=gui, state_location=self.tmpdir, plugins=[plugin]
184 )
185 app.observe(events.append, "starting,started,stopping,stopped")
186
187 # When we start and stop the application
188 app.observe(lambda event: app.exit(), "application_initialized")
189 app.run()
190
191 # Then events occurred in the following order.
192 self.assertEqual(
193 [(event.object, event.name) for event in events],
194 [
195 (app, "starting"),
196 (plugin, "started"),
197 (app, "started"),
198 (gui, "starting"),
199 (gui, "stopped"),
200 (app, "stopping"),
201 (plugin, "stopped"),
202 (app, "stopped"),
203 ],
204 )