File: test_ui_panel.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 (112 lines) | stat: -rw-r--r-- 3,175 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
# (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!

""" Tests to exercise logic for layouts, e.g. VGroup, HGroup.
"""

import unittest

from pyface.toolkit import toolkit_object
from pyface.constant import OK

try:
    from pyface.qt import QtWebkit  # noqa: F401

    NO_WEBKIT_OR_WEBENGINE = False
except ImportError:
    try:
        from pyface.qt import QtWebEngine  # noqa: F401

        NO_WEBKIT_OR_WEBENGINE = False
    except ImportError:
        NO_WEBKIT_OR_WEBENGINE = True
from traits.api import HasTraits, Int
from traitsui.api import HelpButton, HGroup, Item, spring, VGroup, View
from traitsui.testing.api import MouseClick, UITester
from traitsui.tests._tools import (
    BaseTestMixin,
    create_ui,
    requires_toolkit,
    ToolkitName,
)

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


class ObjectWithNumber(HasTraits):
    number1 = Int()
    number2 = Int()
    number3 = Int()


class HelpPanel(HasTraits):
    my_int = Int(2, help='this is the help for my int')

    def default_traits_view(self):
        view = View(
            Item(name="my_int"),
            title="HelpPanel",
            buttons=[HelpButton],
        )
        return view


@requires_toolkit([ToolkitName.qt, ToolkitName.wx])
class TestUIPanel(BaseTestMixin, unittest.TestCase):
    def setUp(self):
        BaseTestMixin.setUp(self)

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

    def test_grouped_layout_with_springy(self):
        # Regression test for enthought/traitsui#1066
        obj1 = ObjectWithNumber()
        view = View(
            HGroup(
                VGroup(
                    Item("number1"),
                ),
                VGroup(
                    Item("number2"),
                ),
                spring,
            )
        )
        # This should not fail.
        with create_ui(obj1, dict(view=view)):
            pass

    # Regression test for enthought/traitsui#1538
    @requires_toolkit([ToolkitName.qt])
    @unittest.skipIf(
        NO_WEBKIT_OR_WEBENGINE, "Tests require either QtWebKit or QtWebEngine"
    )
    def test_show_help(self):

        # This help window is not actually modal, when opened it will be the
        # active window not active modal widget
        class MyTester(ModalDialogTester):
            def get_dialog_widget(self):
                return self._qt_app.activeWindow()

        panel = HelpPanel()
        tester = UITester(auto_process_events=False)
        with tester.create_ui(panel) as ui:
            help_button = tester.find_by_id(ui, 'Help')

            def click_help():
                # should not fail
                help_button.perform(MouseClick())

            mdtester = MyTester(click_help)
            mdtester.open_and_run(lambda x: x.click_button(OK))