Debian Codegraph @

traitsui/examples/demo/Dynamic_Forms/tests/test_visible_when.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 the use of visible_when to make UI
13 components visible or not
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 IsVisible, UITester
24
25 #: Filename of the demo script
26 FILENAME = "visible_when.py"
27
28 #: Path of the demo script
29 DEMO_PATH = os.path.join(os.path.dirname(__file__), "..", FILENAME)
30
31
32 class TestVisibleWhenDemo(unittest.TestCase):
33 def test_visible_when_demo(self):
34 demo = runpy.run_path(DEMO_PATH)["demo"]
35
36 tester = UITester()
37 with tester.create_ui(demo) as ui:
38 # person should be 16 by default
39 self.assertLess(demo.age, 18)
40
41 marital_status_field = tester.find_by_name(ui, 'marital_status')
42 legal_guardian_field = tester.find_by_name(ui, 'legal_guardian')
43 self.assertFalse(marital_status_field.inspect(IsVisible()))
44 self.assertTrue(legal_guardian_field.inspect(IsVisible()))
45
46 # UITester is yet to support SimpleSpinEditor so we change the
47 # value of the trait directly
48 demo.age = 20
49
50 registered_voter_field = tester.find_by_name(
51 ui, 'registered_voter'
52 )
53 school_field = tester.find_by_name(ui, 'school')
54 self.assertTrue(registered_voter_field.inspect(IsVisible()))
55 self.assertFalse(school_field.inspect(IsVisible()))
56
57
58 # Run the test(s)
59 unittest.TextTestRunner().run(
60 unittest.TestLoader().loadTestsFromTestCase(TestVisibleWhenDemo)
61 )