File: test_text_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 (366 lines) | stat: -rw-r--r-- 14,180 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# (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 packaging.version import Version

from traits import __version__ as TRAITS_VERSION
from traits.api import (
    HasTraits,
    Str,
)
from traits.testing.api import UnittestTools
from traitsui.api import TextEditor, View, Item
from traitsui.testing.api import (
    DisplayedText,
    KeyClick,
    KeySequence,
    MouseClick,
    InteractionNotSupported,
    UITester,
)
from traitsui.tests._tools import (
    BaseTestMixin,
    GuiTestAssistant,
    no_gui_test_assistant,
    requires_toolkit,
    ToolkitName,
)


class Foo(HasTraits):

    name = Str()

    nickname = Str()


def get_view(style, auto_set):
    """Return the default view for the Foo object.

    Parameters
    ----------
    style : str
        e.g. 'simple', or 'custom'
    auto_set : bool
        To be passed directly to the editor factory.
    """
    return View(
        Item("name", editor=TextEditor(auto_set=auto_set), style=style)
    )


# Skips tests if the backend is not either qt4 or qt5
@requires_toolkit([ToolkitName.qt])
@unittest.skipIf(no_gui_test_assistant, "No GuiTestAssistant")
class TestTextEditorQt(
    BaseTestMixin, GuiTestAssistant, UnittestTools, unittest.TestCase
):
    """Test on TextEditor with Qt backend."""

    def setUp(self):
        BaseTestMixin.setUp(self)
        GuiTestAssistant.setUp(self)

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

    def test_text_editor_placeholder_text(self):
        foo = Foo()
        editor = TextEditor(
            placeholder="Enter name",
        )
        view = View(Item(name="name", editor=editor))

        tester = UITester()
        with tester.create_ui(foo, dict(view=view)) as ui:
            (name_editor,) = ui.get_editors("name")
            self.assertEqual(
                name_editor.control.placeholderText(),
                "Enter name",
            )

    def test_text_editor_placeholder_text_and_readonly(self):
        # Placeholder can be set independently of read_only flag
        foo = Foo()
        editor = TextEditor(
            placeholder="Enter name",
            read_only=True,
        )
        view = View(Item(name="name", editor=editor))
        tester = UITester()
        with tester.create_ui(foo, dict(view=view)) as ui:
            (name_editor,) = ui.get_editors("name")
            self.assertEqual(
                name_editor.control.placeholderText(),
                "Enter name",
            )

    def test_text_editor_default_view(self):
        foo = Foo()
        tester = UITester()
        with tester.create_ui(foo) as ui:
            (name_editor,) = ui.get_editors("name")
            self.assertEqual(
                name_editor.control.placeholderText(),
                "",
            )

    def test_text_editor_custom_style_placeholder(self):
        # Test against CustomEditor using QTextEdit
        foo = Foo()
        view = View(
            Item(
                name="name",
                style="custom",
                editor=TextEditor(placeholder="Enter name"),
            )
        )
        tester = UITester()
        with tester.create_ui(foo, dict(view=view)) as ui:
            (name_editor,) = ui.get_editors("name")
            try:
                placeholder = name_editor.control.placeholderText()
            except AttributeError:
                # placeholderText is introduced to QTextEdit since Qt 5.2
                pass
            else:
                self.assertEqual(placeholder, "Enter name")

    def test_cancel_button(self):
        foo = Foo()
        view = View(
            Item(
                name="name",
                style="simple",
                editor=TextEditor(cancel_button=True),
            )
        )
        tester = UITester()
        with tester.create_ui(foo, dict(view=view)) as ui:
            (name_editor,) = ui.get_editors("name")
            # isClearButtonEnabled is introduced to QLineEdit since Qt 5.2
            if hasattr(name_editor.control, 'isClearButtonEnabled'):
                self.assertTrue(name_editor.control.isClearButtonEnabled())


# We should be able to run this test case against wx.
# Not running them now to avoid test interaction. See enthought/traitsui#752
@requires_toolkit([ToolkitName.qt, ToolkitName.wx])
class TestTextEditor(BaseTestMixin, unittest.TestCase, UnittestTools):
    """Tests that can be run with any toolkit as long as there is an
    implementation for simulating user interactions.
    """

    def setUp(self):
        BaseTestMixin.setUp(self)

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

    def check_editor_init_and_dispose(self, style, auto_set):
        # Smoke test to test setup and tear down of an editor.
        foo = Foo()
        view = get_view(style=style, auto_set=auto_set)
        with UITester().create_ui(foo, dict(view=view)):
            pass

    def test_simple_editor_init_and_dispose(self):
        # Smoke test to test setup and tear down of an editor.
        self.check_editor_init_and_dispose(style="simple", auto_set=True)

    def test_custom_editor_init_and_dispose(self):
        # Smoke test to test setup and tear down of an editor.
        self.check_editor_init_and_dispose(style="custom", auto_set=True)

    def test_readonly_editor_init_and_dispose(self):
        # Smoke test to test setup and tear down of an editor.
        self.check_editor_init_and_dispose(style="readonly", auto_set=True)

    def test_simple_editor_init_and_dispose_no_auto_set(self):
        # Smoke test to test setup and tear down of an editor.
        self.check_editor_init_and_dispose(style="simple", auto_set=False)

    def test_custom_editor_init_and_dispose_no_auto_set(self):
        # Smoke test to test setup and tear down of an editor.
        self.check_editor_init_and_dispose(style="custom", auto_set=False)

    def test_simple_auto_set_update_text(self):
        foo = Foo()
        view = get_view(style="simple", auto_set=True)
        tester = UITester()
        with tester.create_ui(foo, dict(view=view)) as ui:
            with self.assertTraitChanges(foo, "name", count=3):
                name_field = tester.find_by_name(ui, "name")
                name_field.perform(KeySequence("NEW"))
                # with auto-set the displayed name should match the name trait
            display_name = name_field.inspect(DisplayedText())
            self.assertEqual(foo.name, "NEW")
            self.assertEqual(display_name, foo.name)

    # Currently if auto_set is false, and enter_set is false (the default
    # behavior), on Qt to ensure the text is actually set, Enter will set
    # the value
    @requires_toolkit([ToolkitName.qt])
    def test_simple_auto_set_false_do_not_update_qt(self):
        foo = Foo(name="")
        view = get_view(style="simple", auto_set=False)
        tester = UITester()
        with tester.create_ui(foo, dict(view=view)) as ui:
            name_field = tester.find_by_name(ui, "name")
            name_field.perform(KeySequence("NEW"))
            # with auto-set as False the displayed name should match what has
            # been typed not the trait itself, After "Enter" is pressed it
            # should match the name trait
            display_name = name_field.inspect(DisplayedText())
            self.assertEqual(foo.name, "")
            self.assertEqual(display_name, "NEW")
            name_field.perform(KeyClick("Enter"))
            display_name = name_field.inspect(DisplayedText())
            self.assertEqual(foo.name, "NEW")
            self.assertEqual(display_name, foo.name)

    # If auto_set is false, the value can be set by killing the focus. This
    # can be done by simply moving to another textbox.
    @requires_toolkit([ToolkitName.wx])
    def test_simple_auto_set_false_do_not_update_wx(self):
        foo = Foo(name="")
        view = View(
            Item("name", editor=TextEditor(auto_set=False), style="simple"),
            Item(
                "nickname", editor=TextEditor(auto_set=False), style="simple"
            ),
        )
        tester = UITester()
        with tester.create_ui(foo, dict(view=view)) as ui:
            name_field = tester.find_by_name(ui, "name")
            name_field.perform(KeySequence("NEW"))
            # with auto-set as False the displayed name should match what has
            # been typed not the trait itself, After moving to another textbox
            # it should match the name trait
            display_name = name_field.inspect(DisplayedText())
            self.assertEqual(foo.name, "")
            self.assertEqual(display_name, "NEW")
            tester.find_by_name(ui, "nickname").perform(MouseClick())
            display_name = name_field.inspect(DisplayedText())
            self.assertEqual(foo.name, "NEW")
            self.assertEqual(display_name, foo.name)

    def test_custom_auto_set_true_update_text(self):
        # the auto_set flag is disregard for custom editor.  (not true on WX)
        foo = Foo()
        view = get_view(auto_set=True, style="custom")
        tester = UITester()
        with tester.create_ui(foo, dict(view=view)) as ui:
            with self.assertTraitChanges(foo, "name", count=3):
                name_field = tester.find_by_name(ui, "name")
                name_field.perform(KeySequence("NEW"))
            # with auto-set the displayed name should match the name trait
            display_name = name_field.inspect(DisplayedText())
            self.assertEqual(foo.name, "NEW")
            self.assertEqual(display_name, foo.name)

    @requires_toolkit([ToolkitName.qt])
    def test_custom_auto_set_false_update_text(self):
        # the auto_set flag is disregard for custom editor. (not true on WX)
        foo = Foo()
        view = get_view(auto_set=False, style="custom")
        tester = UITester()
        with tester.create_ui(foo, dict(view=view)) as ui:
            name_field = tester.find_by_name(ui, "name")
            name_field.perform(KeySequence("NEW"))
            name_field.perform(KeyClick("Enter"))
            display_name = name_field.inspect(DisplayedText())
            self.assertEqual(foo.name, "NEW\n")
            self.assertEqual(display_name, foo.name)

    # If auto_set is false, the value can be set by killing the focus. This
    # can be done by simply moving to another textbox.
    @requires_toolkit([ToolkitName.wx])
    def test_custom_auto_set_false_do_not_update_wx(self):
        foo = Foo(name="")
        view = View(
            Item("name", editor=TextEditor(auto_set=False), style="custom"),
            Item(
                "nickname", editor=TextEditor(auto_set=False), style="custom"
            ),
        )
        tester = UITester()
        with tester.create_ui(foo, dict(view=view)) as ui:
            name_field = tester.find_by_name(ui, "name")
            name_field.perform(KeySequence("NEW"))
            # with auto-set as False the displayed name should match what has
            # been typed not the trait itself, After moving to another textbox
            # it should match the name trait
            display_name = name_field.inspect(DisplayedText())
            self.assertEqual(foo.name, "")
            self.assertEqual(display_name, "NEW")
            tester.find_by_name(ui, "nickname").perform(MouseClick())
            display_name = name_field.inspect(DisplayedText())
            self.assertEqual(foo.name, "NEW")
            self.assertEqual(display_name, foo.name)

    def test_readonly_editor(self):
        foo = Foo(name="A name")
        view = get_view(style="readonly", auto_set=True)
        tester = UITester()
        with tester.create_ui(foo, dict(view=view)) as ui:
            name_field = tester.find_by_name(ui, "name")
            with self.assertRaises(InteractionNotSupported):
                name_field.perform(KeySequence("NEW"))
            # Trying to type should do nothing
            with self.assertRaises(InteractionNotSupported):
                name_field.perform(KeyClick("Space"))
            display_name = name_field.inspect(DisplayedText())
            self.assertEqual(display_name, "A name")

    def check_format_func_used(self, style):
        # Regression test for enthought/traitsui#790
        # The test will fail with traits < 6.1.0 because the bug
        # is fixed in traits, see enthought/traitsui#980 for moving those
        # relevant code to traitsui.
        foo = Foo(name="william", nickname="bill")
        view = View(
            Item("name", format_func=lambda s: s.upper(), style=style),
            Item("nickname", style=style),
        )
        tester = UITester()
        with tester.create_ui(foo, dict(view=view)) as ui:
            display_name = tester.find_by_name(ui, "name").inspect(
                DisplayedText()
            )
            display_nickname = tester.find_by_name(ui, "nickname").inspect(
                DisplayedText()
            )
            self.assertEqual(display_name, "WILLIAM")
            self.assertEqual(display_nickname, "bill")

    @unittest.skipUnless(
        Version(TRAITS_VERSION) >= Version("6.1.0"),
        "This test requires traits >= 6.1.0",
    )
    def test_format_func_used_simple(self):
        self.check_format_func_used(style='simple')

    @unittest.skipUnless(
        Version(TRAITS_VERSION) >= Version("6.1.0"),
        "This test requires traits >= 6.1.0",
    )
    def test_format_func_used_custom(self):
        self.check_format_func_used(style='custom')

    @unittest.skipUnless(
        Version(TRAITS_VERSION) >= Version("6.1.0"),
        "This test requires traits >= 6.1.0",
    )
    def test_format_func_used_readonly(self):
        self.check_format_func_used(style='readonly')