File: test_dialog.py

package info (click to toggle)
python-enaml 0.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,284 kB
  • sloc: python: 31,443; cpp: 4,499; makefile: 140; javascript: 68; lisp: 53; sh: 20
file content (145 lines) | stat: -rw-r--r-- 4,299 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
#------------------------------------------------------------------------------
# Copyright (c) 2024, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
""" Test the Dialog widget.

"""
from utils import compile_source, wait_for_window_displayed

SOURCE = """
from enaml.widgets.api import Container, Dialog, Label, MainWindow, PushButton

enamldef TestDialog(Dialog): dialog:

    title = "Test Dialog"

    alias cancel_button:cancel_button
    alias ok_button:ok_button

    Container:
        PushButton: ok_button:
            text = "Ok"
            clicked::
                dialog.accept()
        PushButton: cancel_button:
            text = "Cancel"
            clicked::
                dialog.reject()

enamldef Main(MainWindow): window:

    title = "Main Test Window"

    alias async_button:async_button

    attr starting = 0
    attr started  = 0
    attr finished = 0
    attr accepted = 0
    attr rejected = 0

    func do_starting():
        window.starting += 1

    func do_started():
        window.started += 1

    func do_finished():
        window.finished += 1

    func do_accepted():
        window.accepted += 1

    func do_rejected():
        window.rejected += 1

    Container:

        Label:
            text = f"Starting: {window.starting}"

        Label:
            text = f"Started:  {window.started}"

        Label:
            text = f"Finished: {window.finished}"

        Label:
            text = f"Accepted: {window.accepted}"

        Label:
            text = f"Rejected: {window.rejected}"

        PushButton: async_button:
            text = "Launch Dialog Asynchronously"
            clicked::
                window.do_starting()
                dialog = TestDialog(self)
                dialog.finished.bind(lambda e: window.do_finished())
                dialog.accepted.bind(lambda e: window.do_accepted())
                dialog.rejected.bind(lambda e: window.do_rejected())
                dialog.open()
                window.do_started()
"""

def test_dialog_asynchronous(enaml_qtbot):

    from enaml.qt import QtCore

    window = compile_source(SOURCE, "Main")()
    window.show()
    wait_for_window_displayed(enaml_qtbot, window)

    assert len(window.windows) == 1
    assert window.starting == 0
    assert window.started  == 0
    assert window.finished == 0
    assert window.accepted == 0
    assert window.rejected == 0

    # click the button to launch the dialog.
    enaml_qtbot.mouseClick(window.async_button.proxy.widget, QtCore.Qt.LeftButton)

    assert len(window.windows) == 2
    assert window.starting == 1
    assert window.started  == 1
    assert window.finished == 0
    assert window.accepted == 0
    assert window.rejected == 0

    # click the Ok button in the dialog.
    enaml_qtbot.mouseClick(next(otherWindow for otherWindow in window.windows if otherWindow.title == 'Test Dialog').ok_button.proxy.widget, QtCore.Qt.LeftButton)
    enaml_qtbot.waitUntil(lambda: len(window.windows) == 1)

    # FIXME it appears as if dialogs do not properly unregister themselves from the window set when closed and out of scope.
    assert len(window.windows) == 1
    assert window.starting == 1
    assert window.started  == 1
    assert window.finished == 1
    assert window.accepted == 1
    assert window.rejected == 0

    # click the button to launch the dialog.
    enaml_qtbot.mouseClick(window.async_button.proxy.widget, QtCore.Qt.LeftButton)

    assert len(window.windows) == 2
    assert window.starting == 2
    assert window.started  == 2
    assert window.finished == 1
    assert window.accepted == 1
    assert window.rejected == 0

    # click the Cancel button in the dialog.
    enaml_qtbot.mouseClick(next(otherWindow for otherWindow in window.windows if otherWindow.title == 'Test Dialog').cancel_button.proxy.widget, QtCore.Qt.LeftButton)
    enaml_qtbot.waitUntil(lambda: len(window.windows) == 1)

    assert len(window.windows) == 1
    assert window.starting == 2
    assert window.started  == 2
    assert window.finished == 2
    assert window.accepted == 1
    assert window.rejected == 1