Debian Codegraph @

envisage/ui/action/tests/test_action_manager_builder.py

raw
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 """ Tests for the action manager builder. """
11
12 # Standard library imports.
13 import unittest
14
15 # Enthought library imports.
16 from envisage.ui.action.api import Action, ActionSet, Group, Menu
17
18 # Local imports.
19 from .dummy_action_manager_builder import DummyActionManagerBuilder
20
21
22 class ActionManagerBuilderTestCase(unittest.TestCase):
23 """Tests for the action manager builder."""
24
25 def test_action_with_nonexistent_group(self):
26 """action with non-existent group"""
27
28 action_sets = [
29 ActionSet(
30 actions=[
31 Action(
32 class_name="Exit", path="MenuBar/File", group="Bogus"
33 ),
34 ]
35 ),
36 ]
37
38 # Create a builder containing the action set.
39 builder = DummyActionManagerBuilder(action_sets=action_sets)
40
41 # Create a menu bar manager for the 'MenuBar'.
42 with self.assertRaises(ValueError):
43 builder.create_menu_bar_manager("MenuBar")
44
45 def test_action_with_nonexistent_sibling(self):
46 """action with non-existent sibling"""
47
48 action_sets = [
49 ActionSet(
50 actions=[
51 Action(
52 class_name="Exit",
53 path="MenuBar/File",
54 before="NonExistentAction",
55 ),
56 ]
57 ),
58 ]
59
60 # Create a builder containing the action set.
61 builder = DummyActionManagerBuilder(action_sets=action_sets)
62
63 # Create a menu bar manager for the 'MenuBar'.
64 with self.assertRaises(ValueError):
65 builder.create_menu_bar_manager("MenuBar")
66
67 def test_group_with_nonexistent_sibling(self):
68 """group with non-existent sibling"""
69
70 action_sets = [
71 ActionSet(
72 groups=[
73 Group(id="FileMenuGroup", path="MenuBar", before="Bogus")
74 ]
75 )
76 ]
77
78 # Create a builder containing the action set.
79 builder = DummyActionManagerBuilder(action_sets=action_sets)
80
81 # Create a menu bar manager for the 'MenuBar'.
82 with self.assertRaises(ValueError):
83 builder.create_menu_bar_manager("MenuBar")
84
85 def test_menu_with_nonexistent_sibling(self):
86 """menu with non-existent sibling"""
87
88 action_sets = [
89 ActionSet(
90 menus=[Menu(name="&File", path="MenuBar", before="Bogus")]
91 )
92 ]
93
94 # Create a builder containing the action set.
95 builder = DummyActionManagerBuilder(action_sets=action_sets)
96
97 # Create a menu bar manager for the 'MenuBar'.
98 with self.assertRaises(ValueError):
99 builder.create_menu_bar_manager("MenuBar")
100
101 def test_action_with_path_component_that_is_not_a_menu(self):
102 """action with path component that is not a menu"""
103
104 action_sets = [
105 ActionSet(
106 actions=[
107 Action(class_name="Exit", path="MenuBar/File"),
108 Action(class_name="Broken", path="MenuBar/File/Exit"),
109 ]
110 ),
111 ]
112
113 # Create a builder containing the action set.
114 builder = DummyActionManagerBuilder(action_sets=action_sets)
115
116 # Create a menu bar manager for the 'MenuBar'.
117 with self.assertRaises(ValueError):
118 builder.create_menu_bar_manager("MenuBar")
119
120 def test_single_top_level_menu_with_no_group(self):
121 """single top level menu with no group"""
122
123 action_sets = [ActionSet(menus=[Menu(name="&File", path="MenuBar")])]
124
125 # Create a builder containing the action set.
126 builder = DummyActionManagerBuilder(action_sets=action_sets)
127
128 # Create a menu bar manager for the 'MenuBar'.
129 menu_bar_manager = builder.create_menu_bar_manager("MenuBar")
130
131 # Make sure that the 'File' menu was added to the 'additions' group
132 # of the menubar.
133 self.assertEqual(1, len(menu_bar_manager.groups))
134
135 group = menu_bar_manager.find_group("additions")
136 ids = [item.id for item in group.items]
137 self.assertEqual(["File"], ids)
138
139 def test_single_top_level_group(self):
140 """single top level group"""
141
142 action_sets = [
143 ActionSet(groups=[Group(id="FileMenuGroup", path="MenuBar")])
144 ]
145
146 # Create a builder containing the action set.
147 builder = DummyActionManagerBuilder(action_sets=action_sets)
148
149 # Create a menu bar manager for the 'MenuBar'.
150 menu_bar_manager = builder.create_menu_bar_manager("MenuBar")
151
152 # Make sure that the group was added before the 'additions' group.
153 self.assertEqual(2, len(menu_bar_manager.groups))
154
155 ids = [group.id for group in menu_bar_manager.groups]
156 self.assertEqual(["FileMenuGroup", "additions"], ids)
157
158 def test_top_level_menus_with_no_groups(self):
159 """top level menus with_no groups"""
160
161 action_sets = [
162 ActionSet(
163 menus=[
164 Menu(name="&File", path="MenuBar"),
165 Menu(name="&Edit", path="MenuBar"),
166 Menu(name="&Tools", path="MenuBar"),
167 Menu(name="&Help", path="MenuBar"),
168 ],
169 )
170 ]
171
172 # Create a builder containing the action set.
173 builder = DummyActionManagerBuilder(action_sets=action_sets)
174
175 # Create a menu bar manager for the 'MenuBar'.
176 menu_bar_manager = builder.create_menu_bar_manager("MenuBar")
177
178 # Make sure that all of the menus were added the the 'additions' group
179 # of the menubar (and in the right order!).
180 self.assertEqual(1, len(menu_bar_manager.groups))
181
182 group = menu_bar_manager.find_group("additions")
183 ids = [item.id for item in group.items]
184 self.assertEqual(["File", "Edit", "Tools", "Help"], ids)
185
186 def test_top_level_menus_no_groups_before_and_after(self):
187 """top level menus no groups, before and after"""
188
189 action_sets = [
190 ActionSet(
191 menus=[Menu(name="&Edit", path="MenuBar", after="File")],
192 ),
193 ActionSet(menus=[Menu(name="&File", path="MenuBar")]),
194 ActionSet(menus=[Menu(name="&Help", path="MenuBar")]),
195 ActionSet(
196 menus=[Menu(name="&Tools", path="MenuBar", before="Help")],
197 ),
198 ]
199
200 # Create a builder containing the action set.
201 builder = DummyActionManagerBuilder(action_sets=action_sets)
202
203 # Create a menu bar manager for the 'MenuBar'.
204 menu_manager = builder.create_menu_bar_manager("MenuBar")
205
206 # Make sure that all of the menus were added the the 'additions' group
207 # of the menubar.
208 self.assertEqual(1, len(menu_manager.groups))
209
210 additions = menu_manager.find_group("additions")
211 ids = [item.id for item in additions.items]
212 self.assertEqual(["File", "Edit", "Tools", "Help"], ids)
213
214 def test_top_level_menu_non_existent_group(self):
215 """top level menu non-existent group"""
216
217 action_sets = [
218 ActionSet(
219 menus=[
220 Menu(name="&File", path="MenuBar", group="FileMenuGroup"),
221 ],
222 )
223 ]
224
225 # Create a builder containing the action set.
226 builder = DummyActionManagerBuilder(action_sets=action_sets)
227
228 # Create a menu bar manager for the 'MenuBar'.
229 with self.assertRaises(ValueError):
230 builder.create_menu_bar_manager("MenuBar")
231
232 def test_top_level_menu_group(self):
233 """top level menu group"""
234
235 action_sets = [
236 ActionSet(
237 groups=[Group(id="FileMenuGroup", path="MenuBar")],
238 menus=[
239 Menu(name="&File", path="MenuBar", group="FileMenuGroup"),
240 ],
241 )
242 ]
243
244 # Create a builder containing the action set.
245 builder = DummyActionManagerBuilder(action_sets=action_sets)
246
247 # Create a menu bar manager for the 'MenuBar'.
248 menu_manager = builder.create_menu_bar_manager("MenuBar")
249
250 # Make sure that the 'File' menu was added to the 'FileMenuGroup'
251 # group of the menubar.
252 self.assertEqual(2, len(menu_manager.groups))
253
254 ids = [group.id for group in menu_manager.groups]
255 self.assertEqual(["FileMenuGroup", "additions"], ids)
256
257 group = menu_manager.find_group("FileMenuGroup")
258 self.assertEqual("File", group.items[0].id)
259
260 def test_sub_menus_no_groups(self):
261 """sub-menus no groups"""
262
263 # We split the contributions into different action sets just because
264 # that is how it might end up in an actual application... not because
265 # you *have* to split them up this way!
266 action_sets = [
267 ActionSet(
268 menus=[
269 Menu(name="&File", path="MenuBar"),
270 Menu(name="&Edit", path="MenuBar"),
271 Menu(name="&Tools", path="MenuBar"),
272 Menu(name="&Help", path="MenuBar"),
273 ],
274 ),
275 ActionSet(menus=[Menu(name="&New", path="MenuBar/File")]),
276 ]
277
278 # Create a builder containing the action set.
279 builder = DummyActionManagerBuilder(action_sets=action_sets)
280
281 # Create a menu bar manager for the 'MenuBar'.
282 menu_manager = builder.create_menu_bar_manager("MenuBar")
283
284 # Make sure the 'New' sub-menu got added to the 'additions' group
285 # of the 'File' menu.
286 menu = menu_manager.find_item("File")
287 additions = menu.find_group("additions")
288
289 self.assertEqual("New", additions.items[0].id)
290
291 def test_actions_no_groups(self):
292 """actions no groups"""
293
294 # We split the contributions into different action sets just because
295 # that is how it might end up in an actual application... not because
296 # you *have* to split them up this way!
297 action_sets = [
298 ActionSet(
299 menus=[
300 Menu(name="&File", path="MenuBar"),
301 Menu(name="&Edit", path="MenuBar"),
302 Menu(name="&Tools", path="MenuBar"),
303 Menu(name="&Help", path="MenuBar"),
304 ]
305 ),
306 ActionSet(
307 actions=[
308 Action(class_name="Exit", path="MenuBar/File"),
309 Action(class_name="About", path="MenuBar/Help"),
310 ]
311 ),
312 ]
313
314 # Create a builder containing the action set.
315 builder = DummyActionManagerBuilder(action_sets=action_sets)
316
317 # Create a menu bar manager for the 'MenuBar'.
318 menu_manager = builder.create_menu_bar_manager("MenuBar")
319
320 # Make sure the 'ExitAction' action got added to the 'additions' group
321 # of the 'File' menu.
322 menu = menu_manager.find_item("File")
323 additions = menu.find_group("additions")
324
325 self.assertEqual("Exit", additions.items[0].id)
326
327 # Make sure the 'AboutAction' action got added to the 'additions' group
328 # of the 'File' menu.
329 menu = menu_manager.find_item("Help")
330 additions = menu.find_group("additions")
331
332 self.assertEqual("About", additions.items[0].id)
333
334 def test_actions_make_submenus(self):
335 """actions make submenus"""
336
337 action_sets = [
338 ActionSet(
339 actions=[
340 Action(class_name="Folder", path="MenuBar/File/New"),
341 Action(class_name="File", path="MenuBar/File/New"),
342 ]
343 )
344 ]
345
346 # Create a builder containing the action set.
347 builder = DummyActionManagerBuilder(action_sets=action_sets)
348
349 # Create a menu bar manager for the 'MenuBar'.
350 menu_manager = builder.create_menu_bar_manager("MenuBar")
351
352 # Make sure the 'File' menu got added to the 'additions' group of the
353 # menubar.
354 self.assertEqual(1, len(menu_manager.groups))
355
356 additions = menu_manager.find_group("additions")
357 self.assertEqual("File", additions.items[0].id)
358
359 # Make sure the 'New' sub-menu got added to the 'additions' group
360 # of the 'File' menu.
361 menu = menu_manager.find_item("File")
362 additions = menu.find_group("additions")
363
364 self.assertEqual("New", additions.items[0].id)
365
366 # Make sure the new 'Folder' and 'File' actions got added to the
367 # 'additions' group of the 'New' menu.
368 menu = menu_manager.find_item("File/New")
369 additions = menu.find_group("additions")
370
371 self.assertEqual("Folder", additions.items[0].id)
372 self.assertEqual("File", additions.items[1].id)
373
374 def test_actions_make_submenus_before_and_after(self):
375 """actions make submenus before and after"""
376
377 action_sets = [
378 ActionSet(
379 actions=[
380 Action(
381 class_name="File",
382 path="MenuBar/File/New",
383 after="Folder",
384 ),
385 Action(
386 class_name="Project",
387 path="MenuBar/File/New",
388 before="Folder",
389 ),
390 Action(class_name="Folder", path="MenuBar/File/New"),
391 ]
392 )
393 ]
394
395 # Create a builder containing the action set.
396 builder = DummyActionManagerBuilder(action_sets=action_sets)
397
398 # Create a menu bar manager for the 'MenuBar'.
399 menu_manager = builder.create_menu_bar_manager("MenuBar")
400
401 # Make sure the 'File' menu got added to the 'additions' group of the
402 # menubar.
403 self.assertEqual(1, len(menu_manager.groups))
404
405 additions = menu_manager.find_group("additions")
406 self.assertEqual("File", additions.items[0].id)
407
408 # Make sure the 'New' sub-menu got added to the 'additions' group
409 # of the 'File' menu.
410 menu = menu_manager.find_item("File")
411 additions = menu.find_group("additions")
412
413 self.assertEqual("New", additions.items[0].id)
414
415 # Make sure the new 'Folder' and 'File' actions got added to the
416 # 'additions' group of the 'New' menu.
417 menu = menu_manager.find_item("File/New")
418 additions = menu.find_group("additions")
419
420 ids = [item.id for item in additions.items]
421 self.assertEqual(["Project", "Folder", "File"], ids)
422
423 def test_explicit_groups(self):
424 """explicit groups"""
425
426 action_sets = [
427 ActionSet(
428 menus=[
429 Menu(name="&File", path="MenuBar"),
430 Menu(name="&Edit", path="MenuBar"),
431 Menu(name="&Tools", path="MenuBar"),
432 Menu(name="&Help", path="MenuBar"),
433 ],
434 ),
435 ActionSet(
436 menus=[
437 Menu(name="&New", path="MenuBar/File", group="NewGroup"),
438 ],
439 ),
440 ActionSet(
441 actions=[
442 Action(
443 class_name="Exit",
444 path="MenuBar/File",
445 group="ExitGroup",
446 ),
447 ]
448 ),
449 ActionSet(
450 groups=[
451 Group(id="ExitGroup", path="MenuBar/File"),
452 Group(
453 id="SaveGroup", path="MenuBar/File", after="NewGroup"
454 ),
455 Group(
456 id="NewGroup", path="MenuBar/File", before="ExitGroup"
457 ),
458 ]
459 ),
460 ]
461
462 # Create a builder containing the action set.
463 builder = DummyActionManagerBuilder(action_sets=action_sets)
464
465 # Create a menu bar manager for the 'MenuBar'.
466 menu_manager = builder.create_menu_bar_manager("MenuBar")
467
468 # Make sure that all of the menus were added the the 'additions' group
469 # of the menubar.
470 self.assertEqual(1, len(menu_manager.groups))
471
472 additions = menu_manager.find_group("additions")
473 ids = [item.id for item in additions.items]
474 self.assertEqual(["File", "Edit", "Tools", "Help"], ids)
475
476 # Make sure the 'File' menu has got 3 groups, 'NewGroup', 'ExitGroup'
477 # and 'additions' (and in that order!).
478 menu = menu_manager.find_item("File")
479 self.assertEqual(4, len(menu.groups))
480
481 ids = [group.id for group in menu.groups]
482 self.assertEqual(
483 ["NewGroup", "SaveGroup", "ExitGroup", "additions"], ids
484 )
485
486 # Make sure the 'New' sub-menu got added to the 'NewGroup' group
487 # of the 'File' menu.
488 menu = menu_manager.find_item("File")
489 group = menu.find_group("NewGroup")
490
491 self.assertEqual("New", group.items[0].id)
492
493 # Make sure the 'Exit' action got added to the 'ExitGroup' group
494 # of the 'File' menu.
495 menu = menu_manager.find_item("File")
496 group = menu.find_group("ExitGroup")
497
498 self.assertEqual("Exit", group.items[0].id)
499
500 def test_actions_and_menus_in_groups(self):
501 """actions and menus in groups"""
502
503 action_sets = [
504 ActionSet(
505 menus=[
506 Menu(
507 name="&File",
508 path="MenuBar",
509 groups=[
510 Group(
511 id="NewGroup",
512 path="MenuBar/File",
513 before="ExitGroup",
514 ),
515 Group(id="ExitGroup", path="MenuBar/File"),
516 ],
517 ),
518 Menu(name="&Edit", path="MenuBar"),
519 Menu(name="&Tools", path="MenuBar"),
520 Menu(name="&Help", path="MenuBar"),
521 ],
522 ),
523 ActionSet(
524 menus=[
525 Menu(name="&New", path="MenuBar/File", group="NewGroup"),
526 ],
527 ),
528 ActionSet(
529 actions=[
530 Action(
531 class_name="Exit",
532 path="MenuBar/File",
533 group="ExitGroup",
534 ),
535 ]
536 ),
537 ]
538
539 # Create a builder containing the action set.
540 builder = DummyActionManagerBuilder(action_sets=action_sets)
541
542 # Create a menu bar manager for the 'MenuBar'.
543 menu_manager = builder.create_menu_bar_manager("MenuBar")
544
545 # Make sure that all of the menus were added the the 'additions' group
546 # of the menubar.
547 self.assertEqual(1, len(menu_manager.groups))
548
549 additions = menu_manager.find_group("additions")
550 ids = [item.id for item in additions.items]
551 self.assertEqual(["File", "Edit", "Tools", "Help"], ids)
552
553 # Make sure the 'File' menu has got 3 groups, 'NewGroup', 'ExitGroup'
554 # and 'additions' (and in that order!).
555 menu = menu_manager.find_item("File")
556 self.assertEqual(3, len(menu.groups))
557
558 ids = [group.id for group in menu.groups]
559 self.assertEqual(["NewGroup", "ExitGroup", "additions"], ids)
560
561 # Make sure the 'New' sub-menu got added to the 'NewGroup' group
562 # of the 'File' menu.
563 menu = menu_manager.find_item("File")
564 group = menu.find_group("NewGroup")
565
566 self.assertEqual("New", group.items[0].id)
567
568 # Make sure the 'Exit' action got added to the 'ExitGroup' group
569 # of the 'File' menu.
570 menu = menu_manager.find_item("File")
571 group = menu.find_group("ExitGroup")
572
573 self.assertEqual("Exit", group.items[0].id)
574
575 def test_duplicate_menu(self):
576 """duplicate menu"""
577
578 action_sets = [
579 ActionSet(
580 menus=[
581 Menu(
582 name="&File",
583 path="MenuBar",
584 groups=[
585 Group(id="NewGroup", path="MenuBar/File"),
586 Group(id="ExitGroup", path="MenuBar/File"),
587 ],
588 ),
589 ],
590 ),
591 ActionSet(
592 menus=[
593 Menu(
594 name="&File",
595 path="MenuBar",
596 groups=[
597 Group(id="ExtraGroup", path="MenuBar/File"),
598 ],
599 ),
600 ],
601 ),
602 ]
603
604 # Create a builder containing the action set.
605 builder = DummyActionManagerBuilder(action_sets=action_sets)
606
607 # Create a menu bar manager for the 'MenuBar'.
608 menu_manager = builder.create_menu_bar_manager("MenuBar")
609
610 # Make sure that all of the menus were added the the 'additions' group
611 # of the menubar.
612 self.assertEqual(1, len(menu_manager.groups))
613
614 # Make sure we only get *one* 'File' menu.
615 additions = menu_manager.find_group("additions")
616 ids = [item.id for item in additions.items]
617 self.assertEqual(["File"], ids)
618
619 # Make sure the 'File' menu has got 4 groups, 'NewGroup', 'ExitGroup',
620 # 'ExtraGroup' and 'additions' (and in that order!).
621 menu = menu_manager.find_item("File")
622 self.assertEqual(4, len(menu.groups))
623
624 ids = [group.id for group in menu.groups]
625 self.assertEqual(
626 ["NewGroup", "ExitGroup", "ExtraGroup", "additions"], ids
627 )
628
629 def test_duplicate_group(self):
630 """duplicate group"""
631
632 action_sets = [
633 ActionSet(
634 menus=[
635 Menu(
636 name="&File",
637 path="MenuBar",
638 groups=[
639 Group(id="NewGroup", path="MenuBar/File"),
640 Group(id="ExitGroup", path="MenuBar/File"),
641 ],
642 ),
643 ],
644 ),
645 ActionSet(
646 menus=[
647 Menu(
648 name="&File",
649 path="MenuBar",
650 groups=[
651 Group(id="NewGroup", path="MenuBar/File"),
652 ],
653 ),
654 ],
655 ),
656 ]
657
658 # Create a builder containing the action set.
659 builder = DummyActionManagerBuilder(action_sets=action_sets)
660
661 # Create a menu bar manager for the 'MenuBar'.
662 menu_manager = builder.create_menu_bar_manager("MenuBar")
663
664 # Make sure that all of the menus were added the the 'additions' group
665 # of the menubar.
666 self.assertEqual(1, len(menu_manager.groups))
667
668 # Make sure we only get *one* 'File' menu.
669 additions = menu_manager.find_group("additions")
670 ids = [item.id for item in additions.items]
671 self.assertEqual(["File"], ids)
672
673 # Make sure the 'File' menu has got 3 groups, 'NewGroup', 'ExitGroup'
674 # and 'additions' (and in that order!).
675 menu = menu_manager.find_item("File")
676 self.assertEqual(3, len(menu.groups))
677
678 ids = [group.id for group in menu.groups]
679 self.assertEqual(["NewGroup", "ExitGroup", "additions"], ids)