File: test_list_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 (328 lines) | stat: -rw-r--r-- 10,898 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
# (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 shutil
import tempfile
import unittest

from pyface.toolkit import toolkit_object
from traits.api import (
    Directory,
    HasStrictTraits,
    Instance,
    Int,
    List,
    Str,
    TraitError,
)

from traitsui.api import Item, ListEditor, View
from traitsui.testing.api import (
    DisplayedText,
    Index,
    KeyClick,
    KeySequence,
    LocationNotSupported,
    MouseClick,
    Textbox,
    UITester,
)
from traitsui.tests._tools import (
    requires_toolkit,
    ToolkitName,
)

ModalDialogTester = toolkit_object(
    "util.modal_dialog_tester:ModalDialogTester"
)


# 'Person' class:
class Person(HasStrictTraits):

    # Trait definitions:
    name = Str()
    age = Int()

    # Traits view definition:
    traits_view = View('name', 'age', width=0.18, buttons=['OK', 'Cancel'])


def get_people():
    # Sample data:
    return [
        Person(name='Dave', age=39),
        Person(name='Mike', age=28),
        Person(name='Joe', age=34),
        Person(name='Tom', age=22),
        Person(name='Dick', age=63),
        Person(name='Harry', age=46),
        Person(name='Sally', age=43),
        Person(name='Fields', age=31),
    ]


# 'ListTraitTest' class:
class ListTraitTest(HasStrictTraits):

    # Trait definitions:
    people = List(Instance(Person, ()))
    num_columns = Int(1)
    style = Str("custom")

    # Traits view definitions:
    def default_traits_view(self):
        view = View(
            Item(
                'people',
                label='List',
                id='list',
                style=self.style,
                editor=ListEditor(style=self.style, columns=self.num_columns),
            ),
            resizable=True,
        )
        return view


class Phonebook(HasStrictTraits):
    people = List(Instance(Person))


notebook_view = View(
    Item(
        "people",
        style="custom",
        editor=ListEditor(use_notebook=True),
    )
)


@requires_toolkit([ToolkitName.qt, ToolkitName.wx])
class TestCustomListEditor(unittest.TestCase):
    def test_locate_element_and_edit(self):
        # varying the number of columns in the view tests the logic for
        # getting the correct nested ui
        for col in range(1, 5):
            obj = ListTraitTest(people=get_people(), num_columns=col)
            tester = UITester()
            with tester.create_ui(obj) as ui:
                # sanity check
                self.assertEqual(obj.people[7].name, "Fields")
                people_list = tester.find_by_name(ui, "people")
                item = people_list.locate(Index(7))
                name_field = item.find_by_name("name")
                for _ in range(6):
                    name_field.perform(KeyClick("Backspace"))
                name_field.perform(KeySequence("David"))
                displayed = name_field.inspect(DisplayedText())
                self.assertEqual(obj.people[7].name, "David")
                self.assertEqual(displayed, obj.people[7].name)

    def test_useful_err_message(self):
        obj = ListTraitTest(people=get_people())
        tester = UITester()
        with tester.create_ui(obj) as ui:
            with self.assertRaises(LocationNotSupported) as exc:
                people_list = tester.find_by_name(ui, "people")
                people_list.locate(Textbox())
            self.assertIn(Index, exc.exception.supported)

    def test_index_out_of_range(self):
        obj = ListTraitTest(people=get_people())
        tester = UITester()
        with tester.create_ui(obj) as ui:
            people_list = tester.find_by_name(ui, "people")
            with self.assertRaises(IndexError):
                people_list.locate(Index(10))


@requires_toolkit([ToolkitName.qt, ToolkitName.wx])
class TestSimpleListEditor(unittest.TestCase):
    def test_locate_element_and_edit(self):
        obj = ListTraitTest(people=get_people(), style="simple")
        tester = UITester()
        with tester.create_ui(obj) as ui:
            # sanity check
            self.assertEqual(obj.people[7].name, "Fields")
            people_list = tester.find_by_name(ui, "people")
            item = people_list.locate(Index(7))
            item.perform(MouseClick())
            name_field = item.find_by_name("name")
            for _ in range(6):
                name_field.perform(KeyClick("Backspace"))
            name_field.perform(KeySequence("David"))
            displayed = name_field.inspect(DisplayedText())
            self.assertEqual(obj.people[7].name, "David")
            self.assertEqual(displayed, obj.people[7].name)

    def test_useful_err_message(self):
        obj = ListTraitTest(people=get_people(), style="simple")
        tester = UITester()
        with tester.create_ui(obj) as ui:
            with self.assertRaises(LocationNotSupported) as exc:
                people_list = tester.find_by_name(ui, "people")
                people_list.locate(Textbox())
            self.assertIn(Index, exc.exception.supported)

    def test_index_out_of_range(self):
        obj = ListTraitTest(people=get_people(), style="simple")
        tester = UITester()
        with tester.create_ui(obj) as ui:
            people_list = tester.find_by_name(ui, "people")
            with self.assertRaises(IndexError):
                people_list.locate(Index(10))

    # regression test for enthought/traitsui#1154
    @requires_toolkit([ToolkitName.qt])
    def test_add_item_fails(self):
        class Foo(HasStrictTraits):
            dirs = List(Directory(exists=True))

        obj = Foo()
        tester = UITester(auto_process_events=False)
        with tester.create_ui(obj) as ui:
            dirs_list_editor = tester.find_by_name(ui, "dirs")

            def trigger_error():
                try:
                    dirs_list_editor._target.add_empty()
                except TraitError:
                    return False
                return True

            mdtester = ModalDialogTester(trigger_error)
            mdtester.open_and_run(lambda x: x.close())
            # we want an error dialog to open, but don't want to raise a
            # TraitError and crash the full application
            self.assertTrue(mdtester.dialog_was_opened)
            self.assertTrue(mdtester.result)

            self.assertEqual(len(obj.dirs), 0)

    # this test hits a problem on wx, see issue enthought/traitsui#1653
    @requires_toolkit([ToolkitName.qt])
    def test_default_factory(self):
        temp_dir = tempfile.mkdtemp()

        def test_callable():
            return temp_dir

        class Foo(HasStrictTraits):
            dirs = List(Directory(exists=True))
            view = View(
                Item("dirs", editor=ListEditor(item_factory=test_callable))
            )

        obj = Foo()
        tester = UITester()
        with tester.create_ui(obj) as ui:
            dirs_list_editor = tester.find_by_name(ui, "dirs")
            # should not raise error (see above test_add_item_fails)
            dirs_list_editor._target.add_empty()
            self.assertEqual(len(obj.dirs), 1)

        shutil.rmtree(temp_dir)

    def test_default_factory_with_args(self):
        class Foo(HasStrictTraits):
            bar = Int()
            baz = Str()

        def test_callable(bar, baz=''):
            return Foo(bar=bar, baz=baz)

        class TestFoo(HasStrictTraits):
            foos = List(Foo)
            view = View(
                Item(
                    "foos",
                    editor=ListEditor(
                        item_factory=test_callable,
                        item_factory_args=(7,),
                        item_factory_kwargs={'baz': "python"},
                    ),
                )
            )

        obj = TestFoo()
        tester = UITester()
        with tester.create_ui(obj) as ui:
            foos_list_editor = tester.find_by_name(ui, "foos")
            # should not raise error (see above test_add_item_fails)
            foos_list_editor._target.add_empty()
            self.assertEqual(len(obj.foos), 1)
            self.assertEqual(obj.foos[0].bar, 7)
            self.assertEqual(obj.foos[0].baz, "python")


@requires_toolkit([ToolkitName.qt, ToolkitName.wx])
class TestNotebookListEditor(unittest.TestCase):
    def test_modify_person_name(self):
        phonebook = Phonebook(
            people=get_people(),
        )
        tester = UITester()
        with tester.create_ui(phonebook, dict(view=notebook_view)) as ui:
            list_ = tester.find_by_name(ui, "people")
            list_.locate(Index(1)).perform(MouseClick())
            name_field = list_.locate(Index(1)).find_by_name("name")
            for _ in range(4):
                name_field.perform(KeyClick("Backspace"))
            name_field.perform(KeySequence("Pete"))

            self.assertEqual(phonebook.people[1].name, "Pete")

    def test_get_person_name(self):
        person1 = Person()
        person2 = Person(name="Mary")
        phonebook = Phonebook(
            people=[person1, person2],
        )
        tester = UITester()
        with tester.create_ui(phonebook, dict(view=notebook_view)) as ui:
            list_ = tester.find_by_name(ui, "people")
            list_.locate(Index(1)).perform(MouseClick())
            name_field = list_.locate(Index(1)).find_by_name("name")
            actual = name_field.inspect(DisplayedText())
            self.assertEqual(actual, "Mary")

    def test_index_out_of_bound(self):
        phonebook = Phonebook(
            people=[],
        )
        tester = UITester()
        with tester.create_ui(phonebook, dict(view=notebook_view)) as ui:
            with self.assertRaises(IndexError):
                tester.find_by_name(ui, "people").locate(Index(0)).perform(
                    MouseClick()
                )

    # regression test for enthought/traitsui#1790
    def test_initial_selected(self):
        class PhoneBookWithSelected(Phonebook):
            selected = Instance(Person)

            traits_view = View(
                Item(
                    "people",
                    style="custom",
                    editor=ListEditor(
                        use_notebook=True,
                        selected='selected',
                    ),
                )
            )

        tester = UITester()
        phonebook = PhoneBookWithSelected(people=get_people())
        with tester.create_ui(phonebook) as ui:
            self.assertEqual(
                phonebook.selected, phonebook.people[0]
            )