File: test_ui.py

package info (click to toggle)
python-traitsui 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 13,292 kB
  • sloc: python: 39,867; makefile: 120; sh: 5
file content (185 lines) | stat: -rw-r--r-- 5,476 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
"""
Test cases for the UI object.
"""

import nose.tools

from traits.has_traits import HasTraits
from traits.trait_types import Str, Int
import traitsui
from traitsui.item import Item
from traitsui.view import View

from traitsui.tests._tools import *


class FooDialog(HasTraits):
    """Test dialog that does nothing useful."""

    my_int = Int(2)
    my_str = Str('hallo')

    traits_view = View(
        Item('my_int'),
        Item('my_str'),
        buttons = ['OK']
    )


@skip_if_not_wx
def test_reset_with_destroy_wx():
    # Characterization test:
    # UI.reset(destroy=True) destroys all ui children of the top control

    foo = FooDialog()
    ui = foo.edit_traits()

    ui.reset(destroy=True)

    # the top control is still there
    nose.tools.assert_is_not_none(ui.control)
    # but its children are gone
    nose.tools.assert_equal(len(ui.control.GetChildren()), 0)


@skip_if_not_qt4
def test_reset_with_destroy_qt():
    # Characterization test:
    # UI.reset(destroy=True) destroys all ui children of the top control

    from pyface import qt

    foo = FooDialog()
    ui = foo.edit_traits()

    # decorate children's `deleteLater` function to check that it is called
    # on `reset`. check only with the editor parts (only widgets are scheduled,
    # see traitsui.qt4.toolkit.GUIToolkit.destroy_children)
    for c in ui.control.children():
        c.deleteLater = count_calls(c.deleteLater)

    ui.reset(destroy=True)

    # the top control is still there
    nose.tools.assert_is_not_none(ui.control)

    # but its children are scheduled for removal
    for c in ui.control.children():
        if isinstance(c, qt.QtGui.QWidget):
            nose.tools.assert_equal(c.deleteLater._n_calls, 1)


@skip_if_not_wx
def test_reset_without_destroy_wx():
    # Characterization test:
    # UI.reset(destroy=False) destroys all editor controls, but leaves editors
    # and ui children intact

    import wx

    foo = FooDialog()
    ui = foo.edit_traits()

    nose.tools.assert_equal(len(ui._editors), 2)
    nose.tools.assert_is_instance(ui._editors[0],
                                  traitsui.wx.text_editor.SimpleEditor)
    nose.tools.assert_is_instance(ui._editors[0].control,
                                  wx._controls.TextCtrl)

    ui.reset(destroy=False)

    nose.tools.assert_equal(len(ui._editors), 2)
    nose.tools.assert_is_instance(ui._editors[0],
                                  traitsui.wx.text_editor.SimpleEditor)
    nose.tools.assert_is_none(ui._editors[0].control)

    # children are still there: check first text control
    text_ctrl = ui.control.FindWindowByName('text')
    nose.tools.assert_is_not_none(text_ctrl)


@skip_if_not_qt4
def test_reset_without_destroy_qt():
    # Characterization test:
    # UI.reset(destroy=False) destroys all editor controls, but leaves editors
    # and ui children intact

    from pyface import qt

    foo = FooDialog()
    ui = foo.edit_traits()

    nose.tools.assert_equal(len(ui._editors), 2)
    nose.tools.assert_is_instance(ui._editors[0],
                                  traitsui.qt4.text_editor.SimpleEditor)
    nose.tools.assert_is_instance(ui._editors[0].control,
                                  qt.QtGui.QLineEdit)

    ui.reset(destroy=False)

    nose.tools.assert_equal(len(ui._editors), 2)
    nose.tools.assert_is_instance(ui._editors[0],
                                  traitsui.qt4.text_editor.SimpleEditor)
    nose.tools.assert_is_none(ui._editors[0].control)

    # children are still there: check first text control
    text_ctrl = ui.control.findChild(qt.QtGui.QLineEdit)
    nose.tools.assert_is_not_none(text_ctrl)


@skip_if_not_wx
def test_destroy_after_ok_wx():
    # Behavior: after pressing 'OK' in a dialog, the method UI.finish is
    # called and the view control and its children are destroyed

    import wx

    foo = FooDialog()
    ui = foo.edit_traits()

    # keep references to the children of the ui to check that they were deleted
    ui_children = []
    for c in ui.control.GetChildren():
        ui_children.append(c)

    # press the OK button and close the dialog
    okbutton = ui.control.FindWindowByName('button')
    click_event = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED,
                                  okbutton.GetId())
    okbutton.ProcessEvent(click_event)

    nose.tools.assert_is_none(ui.control)
    # and its children have been destroyed
    for c in ui_children:
        with nose.tools.assert_raises(wx._core.PyDeadObjectError):
            c.GetName()


@skip_if_not_qt4
def test_destroy_after_ok_qt():
    # Behavior: after pressing 'OK' in a dialog, the method UI.finish is
    # called and the view control and its children are destroyed

    from pyface import qt

    foo = FooDialog()
    ui = foo.edit_traits()

    # decorate children's `deleteLater` function to check that it is called
    for c in ui.control.children():
        c.deleteLater = count_calls(c.deleteLater)

    # keep references to the children of the ui to check that they were deleted
    ui_children = []
    for c in ui.control.children():
        ui_children.append(c)

    # press the OK button and close the dialog
    okb = ui.control.findChild(qt.QtGui.QPushButton)
    okb.click()

    nose.tools.assert_is_none(ui.control)
    # children are scheduled for removal
    for c in ui_children:
        if isinstance(c, qt.QtGui.QWidget):
            nose.tools.assert_equal(c.deleteLater._n_calls, 1)