File: test_modal_dialog_tester.py

package info (click to toggle)
python-pyface 8.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,944 kB
  • sloc: python: 54,107; makefile: 82
file content (175 lines) | stat: -rw-r--r-- 6,035 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
# (C) Copyright 2005-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 for the tabular editor tester. """


import unittest
from io import StringIO

from pyface.qt import QtGui, is_qt6
from pyface.api import Dialog, MessageDialog, OK, CANCEL
from traits.api import HasStrictTraits

from pyface.ui.qt.util.testing import silence_output
from pyface.ui.qt.util.gui_test_assistant import GuiTestAssistant
from pyface.ui.qt.util.modal_dialog_tester import ModalDialogTester
from pyface.util.testing import skip_if_no_traitsui


class MyClass(HasStrictTraits):
    def default_traits_view(self):
        from traitsui.api import CancelButton, OKButton, View

        view = View(
            buttons=[OKButton, CancelButton],
            resizable=False,
            title="My class dialog",
        )
        return view

    def run(self):
        ui = self.edit_traits(kind="livemodal")
        if ui.result:
            return "accepted"
        else:
            return "rejected"

    def do_not_show_dialog(self):
        return True


class TestModalDialogTester(GuiTestAssistant, unittest.TestCase):
    """ Tests for the modal dialog tester. """

    # Tests ----------------------------------------------------------------

    def test_on_message_dialog(self):
        dialog = MessageDialog()
        tester = ModalDialogTester(dialog.open)

        # accept
        tester.open_and_run(when_opened=lambda x: x.close(accept=True))
        self.assertTrue(tester.value_assigned())
        self.assertEqual(tester.result, OK)
        self.assertTrue(tester.dialog_was_opened)

        # reject
        tester.open_and_run(when_opened=lambda x: x.close())
        self.assertTrue(tester.value_assigned())
        self.assertEqual(tester.result, CANCEL)
        self.assertTrue(tester.dialog_was_opened)

    @skip_if_no_traitsui
    def test_on_traitsui_dialog(self):
        my_class = MyClass()
        tester = ModalDialogTester(my_class.run)

        # accept
        tester.open_and_run(when_opened=lambda x: x.close(accept=True))
        self.assertTrue(tester.value_assigned())
        self.assertEqual(tester.result, "accepted")
        self.assertTrue(tester.dialog_was_opened)

        # reject
        tester.open_and_run(when_opened=lambda x: x.close())
        self.assertTrue(tester.value_assigned())
        self.assertEqual(tester.result, "rejected")
        self.assertTrue(tester.dialog_was_opened)

    @skip_if_no_traitsui
    def test_dialog_was_not_opened_on_traitsui_dialog(self):
        my_class = MyClass()
        tester = ModalDialogTester(my_class.do_not_show_dialog)

        # it runs okay
        tester.open_and_run(when_opened=lambda x: x.close(accept=True))
        self.assertTrue(tester.value_assigned())
        self.assertEqual(tester.result, True)

        # but no dialog is opened
        self.assertFalse(tester.dialog_was_opened)

    @unittest.skipIf(is_qt6, "TEMPORARY: getting tests to run on pyside6")
    def test_capture_errors_on_failure(self):
        dialog = MessageDialog()
        tester = ModalDialogTester(dialog.open)

        def failure(tester):
            try:
                with tester.capture_error():
                    # this failure will appear in the console and get recorded
                    self.fail()
            finally:
                tester.close()
                self.gui.process_events()

        with self.assertRaises(AssertionError):
            alt_stderr = StringIO
            with silence_output(err=alt_stderr):
                tester.open_and_run(when_opened=failure)
            self.assertIn("raise self.failureException(msg)", alt_stderr)

    @unittest.skipIf(is_qt6, "TEMPORARY: getting tests to run on pyside6")
    def test_capture_errors_on_error(self):
        dialog = MessageDialog()
        tester = ModalDialogTester(dialog.open)

        def raise_error(tester):
            try:
                with tester.capture_error():
                    # this error will appear in the console and get recorded
                    1 / 0
            finally:
                tester.close()
                self.gui.process_events()

        with self.assertRaises(ZeroDivisionError):
            alt_stderr = StringIO
            with silence_output(err=alt_stderr):
                tester.open_and_run(when_opened=raise_error)
            self.assertIn("ZeroDivisionError", alt_stderr)

    @unittest.skip("has_widget code not working as designed. Issue #282.")
    def test_has_widget(self):
        dialog = Dialog()
        tester = ModalDialogTester(dialog.open)

        def check_and_close(tester):
            try:
                with tester.capture_error():
                    self.assertTrue(
                        tester.has_widget("OK", QtGui.QAbstractButton)
                    )
                    self.assertFalse(
                        tester.has_widget(text="I am a virtual button")
                    )
            finally:
                tester.close()

        tester.open_and_run(when_opened=check_and_close)

    @unittest.skip("has_widget code not working as designed. Issue #282.")
    def test_find_widget(self):
        dialog = Dialog()
        tester = ModalDialogTester(dialog.open)

        def check_and_close(tester):
            try:
                with tester.capture_error():
                    widget = tester.find_qt_widget(
                        type_=QtGui.QAbstractButton,
                        test=lambda x: x.text() == "OK",
                    )
                    self.assertIsInstance(widget, QtGui.QPushButton)
            finally:
                tester.close()

        tester.open_and_run(when_opened=check_and_close)