File: test_boolean_editor.py

package info (click to toggle)
python-traitsui 8.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,232 kB
  • sloc: python: 58,982; makefile: 113
file content (124 lines) | stat: -rw-r--r-- 4,334 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

import unittest

from traits.api import HasTraits, Bool
from traitsui.api import BooleanEditor, Item, View
from traitsui.tests._tools import (
    BaseTestMixin,
    requires_toolkit,
    ToolkitName,
)

from traitsui.testing.api import (
    DisplayedText,
    IsChecked,
    KeyClick,
    KeySequence,
    MouseClick,
    UITester,
)


class BoolModel(HasTraits):

    true_or_false = Bool()


# Run this against wx once enthought/traitsui#752 is also fixed for
# BooleanEditor
@requires_toolkit([ToolkitName.qt, ToolkitName.wx])
class TestBooleanEditor(BaseTestMixin, unittest.TestCase):
    def setUp(self):
        BaseTestMixin.setUp(self)

    def tearDown(self):
        BaseTestMixin.tearDown(self)

    def check_click_boolean_changes_trait(self, style):
        view = View(Item("true_or_false", style=style, editor=BooleanEditor()))
        obj = BoolModel()

        tester = UITester()
        with tester.create_ui(obj, dict(view=view)) as ui:
            # sanity check
            self.assertEqual(obj.true_or_false, False)
            checkbox = tester.find_by_name(ui, "true_or_false")
            checkbox.perform(MouseClick())
            self.assertEqual(obj.true_or_false, True)

    def test_click_boolean_changes_trait_simple(self):
        self.check_click_boolean_changes_trait('simple')

    def test_click_boolean_changes_trait_custom(self):
        self.check_click_boolean_changes_trait('custom')

    def test_change_text_boolean_changes_trait(self):
        view = View(Item("true_or_false", style='text'))
        obj = BoolModel()

        tester = UITester()
        with tester.create_ui(obj, dict(view=view)) as ui:
            self.assertEqual(obj.true_or_false, False)
            text_field = tester.find_by_name(ui, 'true_or_false')
            for _ in range(5):
                text_field.perform(KeyClick("Backspace"))
            text_field.perform(KeySequence("True"))
            text_field.perform(KeyClick("Enter"))
            self.assertEqual(obj.true_or_false, True)

    def check_trait_change_shown_in_gui(self, style):
        view = View(Item("true_or_false", style=style))
        obj = BoolModel()

        tester = UITester()
        with tester.create_ui(obj, dict(view=view)) as ui:
            checkbox = tester.find_by_name(ui, "true_or_false")
            checked = checkbox.inspect(IsChecked())
            # sanity check
            self.assertEqual(checked, False)
            obj.true_or_false = True
            checked = checkbox.inspect(IsChecked())
            self.assertEqual(checked, True)

    def test_trait_change_shown_in_gui_simple(self):
        self.check_trait_change_shown_in_gui('simple')

    def test_trait_change_shown_in_gui_custom(self):
        self.check_trait_change_shown_in_gui('custom')

    def test_trait_change_shown_in_gui_readonly(self):
        view = View(Item("true_or_false", style='readonly'))
        obj = BoolModel()

        tester = UITester()
        with tester.create_ui(obj, dict(view=view)) as ui:
            readonly = tester.find_by_name(ui, "true_or_false")
            displayed = readonly.inspect(DisplayedText())
            # sanity check
            self.assertEqual(displayed, 'False')
            obj.true_or_false = True
            displayed = readonly.inspect(DisplayedText())
            self.assertEqual(displayed, 'True')

    def test_trait_change_shown_in_gui_text(self):
        view = View(Item("true_or_false", style='text'))
        obj = BoolModel()

        tester = UITester()
        with tester.create_ui(obj, dict(view=view)) as ui:
            text_field = tester.find_by_name(ui, "true_or_false")
            displayed = text_field.inspect(DisplayedText())
            # sanity check
            self.assertEqual(displayed, 'False')
            obj.true_or_false = True
            displayed = text_field.inspect(DisplayedText())
            self.assertEqual(displayed, 'True')