aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/quickcontrols/controls/data/tst_action.qml
blob: 845a6507af5a84bce8fc77c400cbb78b3288ab9c (plain)
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
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

import QtQuick
import QtTest
import QtQuick.Controls
import QtQuick.Templates as T

TestCase {
    id: testCase
    width: 400
    height: 400
    visible: true
    when: windowShown
    name: "Action"

    Component {
        id: component
        Action { }
    }

    Component {
        id: signalSpy
        SignalSpy { }
    }

    function test_empty() {
        failOnWarning(/.?/)

        let control = createTemporaryObject(component, testCase)
        verify(control)
    }

    function test_enabled() {
        var action = createTemporaryObject(component, testCase)
        verify(action)

        var spy = createTemporaryObject(signalSpy, testCase, {target: action, signalName: "triggered"})
        verify(spy.valid)

        action.trigger()
        compare(spy.count, 1)

        action.enabled = false
        action.trigger()
        compare(spy.count, 1)

        action.enabled = undefined // reset
        action.trigger()
        compare(spy.count, 2)
    }

    Component {
        id: buttonAndMenu
        Item {
            property alias button: button
            property alias menu: menu
            property alias menuItem: menuItem
            property alias action: sharedAction
            property var lastSource
            Action {
                id: sharedAction
                text: "Shared"
                shortcut: "Ctrl+B"
                onTriggered: (source) => lastSource = source
            }
            Button {
                id: button
                action: sharedAction
                Menu {
                    id: menu
                    MenuItem {
                        id: menuItem
                        action: sharedAction
                    }
                }
            }
        }
    }

    function test_shared() {
        var container = createTemporaryObject(buttonAndMenu, testCase)
        verify(container)

        keyClick(Qt.Key_B, Qt.ControlModifier)
        compare(container.lastSource, container.button)

        container.menu.open()
        keyClick(Qt.Key_B, Qt.ControlModifier)
        compare(container.lastSource, container.menuItem)

        tryVerify(function() { return !container.menu.visible })
        keyClick(Qt.Key_B, Qt.ControlModifier)
        compare(container.lastSource, container.button)

        container.button.visible = false
        keyClick(Qt.Key_B, Qt.ControlModifier)
        compare(container.lastSource, container.action)
    }

    Component {
        id: actionAndRepeater
        Item {
            property alias action: testAction
            Action {
                id: testAction
                shortcut: "Ctrl+A"
            }
            Repeater {
                model: 1
                Button {
                    action: testAction
                }
            }
        }
    }

    function test_repeater() {
        var container = createTemporaryObject(actionAndRepeater, testCase)
        verify(container)

        var spy = signalSpy.createObject(container, {target: container.action, signalName: "triggered"})
        verify(spy.valid)

        keyClick(Qt.Key_A, Qt.ControlModifier)
        compare(spy.count, 1)
    }

    Component {
        id: shortcutBinding
        Item {
            Action {
                id: action
                shortcut: StandardKey.Copy
            }

            Shortcut {
                id: indirectShortcut
                sequences: [ action.shortcut ]
            }

            Shortcut {
                id: directShortcut
                sequences: [ StandardKey.Copy ]
            }

            property alias indirect: indirectShortcut;
            property alias direct: directShortcut
        }
    }

    function test_shortcutBinding() {
        var container = createTemporaryObject(shortcutBinding, testCase);
        verify(container)
        compare(container.indirect.nativeText, container.direct.nativeText);
    }

    Component {
        id: shortcutCleanup
        Item {
            property alias page: page
            property alias action: action
            property alias menu: menu
            Item {
                id: page
                Action {
                    id: action
                    text: "action"
                    shortcut: "Insert"
                }
                Menu {
                    id: menu
                    MenuItem { action: action }
                }
            }
        }
    }

    function test_shortcutCleanup() {
        {
            var container = createTemporaryObject(shortcutCleanup, testCase);
            verify(container)
            container.action.shortcut = "Delete"
            container.menu.open()
            container.page.destroy()
            tryVerify(function() { return !container.page })
        }
        keyClick(Qt.Key_Delete, Qt.NoModifier)
    }
}