summaryrefslogtreecommitdiff
path: root/tests/auto/qml/intents/tst_intents.qml
blob: c2badd3e9345ae4b03b10654d8a517239ba7b4c7 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Copyright (C) 2019 Luxoft Sweden AB
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtApplicationManager module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtQuick 2.4
import QtTest 1.0
import QtApplicationManager 2.0
import QtApplicationManager.SystemUI 2.0

TestCase {
    id: testCase
    when: windowShown
    name: "Intents"

    property var stdParams: { "para": "meter" }
    property var matchParams: { "list": "a", "int": 42, "string": "foo_x_bar", "complex": { "a": 1 } }

    ListView {
        id: listView
        model: ApplicationManager
        delegate: Item {
            property var modelData: model
        }
    }

    function initTestCase() {
        verify(ApplicationManager.application("intents1"))
        verify(ApplicationManager.application("intents2.1"))
        if (!ApplicationManager.singleProcess)
            verify(ApplicationManager.application("cannot-start"))
    }

    SignalSpy {
        id: requestSpy
        target: null
        signalName: "replyReceived"
    }

    function test_intent_object() {
        verify(IntentServer.count> 0)

        // test intent properties
        var intent = IntentServer.applicationIntent("both", "intents1")
        verify(intent)
        compare(intent.intentId, "both")
        compare(intent.applicationId, "intents1")
        compare(intent.visibility, IntentObject.Public)
        compare(intent.requiredCapabilities, [])
        compare(intent.parameterMatch, {})

        verify(!IntentServer.applicationIntent("both", "intents3"))
        verify(!IntentServer.applicationIntent("bothx", "intents1"))
        verify(!IntentServer.applicationIntent("both", ""))
        verify(!IntentServer.applicationIntent("", "intents1"))
        verify(!IntentServer.applicationIntent("", ""))
    }


    function test_match() {
        // first, check the matching on the server API
        var intent = IntentServer.applicationIntent("match", "intents1")
        verify(!intent)
        intent = IntentServer.applicationIntent("match", "intents1", matchParams)
        verify(intent)
        compare(intent.parameterMatch, { "list": [ "a", "b" ], "int": 42, "string": "^foo_.*_bar$", "complex": { "a": 1 } })

        var params = matchParams
        params.list = "c"
        verify(!IntentServer.applicationIntent("match", "intents1", params))
        params.list = "b"
        verify(IntentServer.applicationIntent("match", "intents1", params))

        params.int = 2
        verify(!IntentServer.applicationIntent("match", "intents1", params))
        params.int = 42

        params.string = "foo"
        verify(!IntentServer.applicationIntent("match", "intents1", params))
        params.string = "foo_test_bar"
        verify(IntentServer.applicationIntent("match", "intents1", params))

        params.complex = "string"
        verify(!IntentServer.applicationIntent("match", "intents1", params))
        params.complex = matchParams.complex
    }


    function test_intents_data() {
        return [
                    {tag: "1-1", intentId: "only1", appId: "intents1", succeeding: true },
                    {tag: "2-2", intentId: "only2", appId: "intents2.1", succeeding: true },
                    {tag: "1-2", intentId: "only1", appId: "intents2.1", succeeding: false,
                        errorMessage: "No matching IntentHandler found." },
                    {tag: "2-1", intentId: "only2", appId: "intents1", succeeding: false,
                        errorMessage: "No matching intent handler registered.",
                        ignoreWarning: 'Unknown intent "only2" was requested from application ":sysui:"' },
                    {tag: "match-1", intentId: "match", appId: "intents1", succeeding: true, params: matchParams },
                    {tag: "match-2", intentId: "match", appId: "intents1", succeeding: false,
                        params: function() { var x = Object.assign({}, matchParams); x.int = 1; return x }(),
                        errorMessage: "No matching intent handler registered.",
                        ignoreWarning: 'Unknown intent "match" was requested from application ":sysui:"' },
                    {tag: "match-3", intentId: "match", appId: "intents1", succeeding: false, params: {},
                        errorMessage: "No matching intent handler registered.",
                        ignoreWarning: 'Unknown intent "match" was requested from application ":sysui:"' },
                    {tag: "unknown-1", intentId: "unknown", appId: "intents1", succeeding: false,
                        errorMessage: "No matching intent handler registered.",
                        ignoreWarning: 'Unknown intent "unknown" was requested from application ":sysui:"' },
                    {tag: "unknown-2", intentId: "unknown", appId: "", succeeding: false,
                        errorMessage: "No matching intent handler registered.",
                        ignoreWarning: 'Unknown intent "unknown" was requested from application ":sysui:"' },
                    {tag: "custom-error", intentId: "custom-error", appId: "intents1", succeeding: false,
                        errorMessage: "custom error" },
                    {tag: "cannot-start", intentId: "cannot-start-intent", appId: "cannot-start", succeeding: false,
                        errorMessage: /Starting handler application timed out after .*/, isTimeout: true },
                ];
    }

    function test_intents(data) {
        if (data.appId && !ApplicationManager.application(data.appId))
            skip("Application \"" + data.appId + "\" is not available")

        if (data.ignoreWarning)
            ignoreWarning(data.ignoreWarning)

        var params = ("params" in data) ? data.params : stdParams

        var req = IntentClient.sendIntentRequest(data.intentId, data.appId, params)
        verify(req)
        requestSpy.target = req
        let requestTimeout = 1000
        if (data.isTimeout)
            requestTimeout *= 10
        tryCompare(requestSpy, "count", 1, requestTimeout)
        compare(req.succeeded, data.succeeding)
        if (req.succeeded) {
            compare(req.result, { "from": data.appId, "in": params })
        } else {
            if (data.errorMessage instanceof RegExp)
                verify(data.errorMessage.test(req.errorMessage))
            else
                compare(req.errorMessage, data.errorMessage)
        }
        requestSpy.clear()
        requestSpy.target = null
    }

    function test_disambiguate_data() {
        return [
                    {tag: "no-signal", action: "none", succeeding: true },
                    {tag: "reject", action: "reject", succeeding: false,
                        errorMessage: "Disambiguation was rejected" },
                    {tag: "timeout", action: "timeout", succeeding: false,
                        errorMessage: /Disambiguation timed out after .*/ },
                    {tag: "ack-invalid", action: "acknowledge", acknowledgeIntentId: "only1", succeeding: false,
                        errorMessage: "Failed to disambiguate",
                        ignoreWarning: /IntentServer::acknowledgeDisambiguationRequest for intent .* tried to disambiguate to the intent "only1" which was not in the list of potential disambiguations/ },
                    {tag: "ack-valid", action: "acknowledge", succeeding: true }
                ];
    }

    SignalSpy {
        id: disambiguateSpy
        target: IntentServer
    }

    function test_disambiguate(data) {
        var intentId = "both"

        if (data.ignoreWarning)
            ignoreWarning(data.ignoreWarning)

        disambiguateSpy.signalName = data.action === "none" ? "" : "disambiguationRequest";

        var req = IntentClient.sendIntentRequest("both", stdParams)
        verify(req)
        requestSpy.target = req

        if (data.action !== "none") {
            tryCompare(disambiguateSpy, "count", 1, 1000)
            var possibleIntents = disambiguateSpy.signalArguments[0][1]
            compare(possibleIntents.length, 2)
            compare(possibleIntents[0].intentId, intentId)
            compare(possibleIntents[1].intentId, intentId)
            compare(possibleIntents[0].applicationId, "intents1")
            compare(possibleIntents[1].applicationId, "intents2.1")
            compare(disambiguateSpy.signalArguments[0][2], stdParams)

            switch (data.action) {
            case "reject":
                IntentServer.rejectDisambiguationRequest(disambiguateSpy.signalArguments[0][0])
                break
            case "acknowledge":
                var intent = data.acknowledgeIntentId ? IntentServer.applicationIntent(data.acknowledgeIntentId,
                                                                                possibleIntents[0].applicationId)
                                                      : possibleIntents[1]
                IntentServer.acknowledgeDisambiguationRequest(disambiguateSpy.signalArguments[0][0], intent)
                break
            }
            disambiguateSpy.clear()
        }

        tryCompare(requestSpy, "count", 1, data.action === "timeout" ? 15000 : 1000)
        var succeeding = data.succeeding
        compare(req.succeeded, succeeding)
        if (succeeding) {
            compare(req.errorMessage, "")
            verify(req.result !== {})
        } else {
            if (data.errorMessage instanceof RegExp)
                verify(data.errorMessage.test(req.errorMessage))
            else
                compare(req.errorMessage, data.errorMessage)
            compare(req.result, {})
        }
        requestSpy.clear()
    }
}