Debian Codegraph @

traitsui/examples/demo/Standard_Editors/tests/test_TextEditor_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 a textbox created
13 using TextEditor.
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 DisplayedText, KeyClick, KeySequence, UITester
24
25 #: Filename of the demo script
26 FILENAME = "TextEditor_demo.py"
27
28 #: Path of the demo script
29 DEMO_PATH = os.path.join(os.path.dirname(__file__), "..", FILENAME)
30
31
32 class TestTextEditorDemo(unittest.TestCase):
33 def test_run_demo(self):
34 demo = runpy.run_path(DEMO_PATH)["demo"]
35
36 tester = UITester()
37 with tester.create_ui(demo) as ui:
38 simple_int_field = tester.find_by_id(ui, "simple_int")
39 custom_int_field = tester.find_by_id(ui, "custom_int")
40 text_int_field = tester.find_by_id(ui, "text_int")
41 readonly_int_field = tester.find_by_id(ui, "readonly_int")
42
43 # Modify the value to something invalid
44 simple_int_field.perform(KeyClick("Backspace"))
45 simple_int_field.perform(KeySequence("a")) # not a number!
46
47 # Check the value has not changed
48 self.assertEqual(demo.int_trait, 1)
49 self.assertEqual(custom_int_field.inspect(DisplayedText()), "1")
50 self.assertEqual(text_int_field.inspect(DisplayedText()), "1")
51 self.assertEqual(readonly_int_field.inspect(DisplayedText()), "1")
52
53 # Modify the value on the GUI to a good value
54 simple_int_field.perform(KeyClick("Backspace"))
55 simple_int_field.perform(KeySequence("2"))
56
57 # Check
58 self.assertEqual(demo.int_trait, 2)
59 self.assertEqual(custom_int_field.inspect(DisplayedText()), "2")
60 self.assertEqual(text_int_field.inspect(DisplayedText()), "2")
61 self.assertEqual(readonly_int_field.inspect(DisplayedText()), "2")
62
63
64 # Run the test(s)
65 unittest.TextTestRunner().run(
66 unittest.TestLoader().loadTestsFromTestCase(TestTextEditorDemo)
67 )