Debian Codegraph @

envisage/ui/tasks/tests/test_tasks_application.py

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