Debian Codegraph @

traitsui/examples/demo/Standard_Editors/tests/test_EnumEditor_demo.py

raw
1 # (C) Copyright 2004-2023 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 """
12 This example demonstrates how to test interacting with the various modes of
13 the EnumEditor.
14
15 The GUI being tested is written in the demo under the same name (minus the
16 preceding 'test') in the outer directory.
17 """
18
19 import os
20 import runpy
21 import unittest
22
23 from traitsui.testing.api import (
24 DisplayedText,
25 Index,
26 KeyClick,
27 KeySequence,
28 MouseClick,
29 SelectedText,
30 UITester,
31 )
32
33 #: Filename of the demo script
34 FILENAME = "EnumEditor_demo.py"
35
36 #: Path of the demo script
37 DEMO_PATH = os.path.join(os.path.dirname(__file__), "..", FILENAME)
38
39
40 class TestEnumEditorDemo(unittest.TestCase):
41 def test_enum_editor_demo(self):
42 demo = runpy.run_path(DEMO_PATH)["demo"]
43
44 tester = UITester()
45 with tester.create_ui(demo) as ui:
46 simple_enum = tester.find_by_id(ui, "simple")
47 simple_text_enum = tester.find_by_id(ui, "simple_text")
48 radio_enum = tester.find_by_id(ui, "radio")
49 list_enum = tester.find_by_id(ui, "list")
50 text = tester.find_by_id(ui, "text")
51 readonly = tester.find_by_id(ui, "readonly")
52
53 self.assertEqual(demo.name_list, 'A-495')
54 simple_enum.locate(Index(1)).perform(MouseClick())
55 self.assertEqual(demo.name_list, 'A-498')
56
57 for _ in range(5):
58 simple_text_enum.perform(KeyClick("Backspace"))
59 simple_text_enum.perform(KeySequence("R-1226"))
60 simple_text_enum.perform(KeyClick("Enter"))
61 self.assertEqual(demo.name_list, 'R-1226')
62
63 radio_enum.locate(Index(5)).perform(MouseClick())
64 self.assertEqual(demo.name_list, 'Foo')
65
66 list_enum.locate(Index(3)).perform(MouseClick())
67 self.assertEqual(demo.name_list, 'TS-17')
68
69 for _ in range(5):
70 text.perform(KeyClick("Backspace"))
71 text.perform(KeySequence("A-498"))
72 text.perform(KeyClick("Enter"))
73 self.assertEqual(demo.name_list, 'A-498')
74
75 demo.name_list = 'Foo'
76
77 displayed_simple = simple_enum.inspect(DisplayedText())
78 disp_simple_text = simple_text_enum.inspect(DisplayedText())
79 selected_radio = radio_enum.inspect(SelectedText())
80 selected_list = list_enum.inspect(SelectedText())
81 displayed_text = text.inspect(DisplayedText())
82 displayed_readonly = readonly.inspect(DisplayedText())
83
84 displayed_selected = [
85 displayed_simple,
86 disp_simple_text,
87 selected_radio,
88 selected_list,
89 displayed_text,
90 displayed_readonly,
91 ]
92 for text in displayed_selected:
93 self.assertEqual(text, 'Foo')
94
95
96 # Run the test(s)
97 unittest.TextTestRunner().run(
98 unittest.TestLoader().loadTestsFromTestCase(TestEnumEditorDemo)
99 )