| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 |
** Contact: http://www.qt-project.org/legal |
| 5 |
** |
| 6 |
** This file is part of the test suite of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** GNU General Public License Usage |
| 18 |
** Alternatively, this file may be used under the terms of the GNU |
| 19 |
** General Public License version 3 as published by the Free Software |
| 20 |
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 |
** included in the packaging of this file. Please review the following |
| 22 |
** information to ensure the GNU General Public License requirements will |
| 23 |
** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 |
** |
| 25 |
** $QT_END_LICENSE$ |
| 26 |
** |
| 27 |
****************************************************************************/ |
| 28 |
|
| 29 |
import QtQuick 2.0 |
| 30 |
import QtTest 1.0 |
| 31 |
import QtServiceFramework 5.0 |
| 32 |
|
| 33 |
/* |
| 34 |
This test is a test of both a naughty SFW object and of the SFW QML API |
| 35 |
*/ |
| 36 |
|
| 37 |
TestCase { |
| 38 |
id: testCase |
| 39 |
|
| 40 |
name: "Service Object" |
| 41 |
|
| 42 |
property QtObject targetObject: testObject |
| 43 |
property QtObject sfwObj: empty |
| 44 |
|
| 45 |
Item { |
| 46 |
id: empty |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
ServiceLoader { |
| 51 |
id: testObject |
| 52 |
} |
| 53 |
|
| 54 |
ServiceLoader { |
| 55 |
id: testObject2 |
| 56 |
asynchronous: false |
| 57 |
} |
| 58 |
|
| 59 |
ServiceLoader { |
| 60 |
id: testObject3 |
| 61 |
} |
| 62 |
|
| 63 |
SignalSpy {id: statusChangedSpy; target: targetObject; signalName: "statusChanged"} |
| 64 |
SignalSpy {id: interfaceNameChangedSpy; target: targetObject; signalName: "interfaceNameChanged"} |
| 65 |
SignalSpy {id: descriptorChangedSpy; target: targetObject; signalName: "serviceDescriptorChanged"} |
| 66 |
SignalSpy {id: serviceObjectChangedSpy; target: targetObject; signalName: "serviceObjectChanged"} |
| 67 |
function resetSpies() { |
| 68 |
statusChangedSpy.clear() |
| 69 |
interfaceNameChangedSpy.clear() |
| 70 |
descriptorChangedSpy.clear() |
| 71 |
serviceObjectChangedSpy.clear() |
| 72 |
} |
| 73 |
|
| 74 |
ServiceFilter { |
| 75 |
id: testList |
| 76 |
} |
| 77 |
|
| 78 |
SignalSpy {id:serviceListNameSpy; target: testList; signalName: "serviceNameChanged" } |
| 79 |
SignalSpy {id:serviceListInterfaceSpy; target: testList; signalName: "interfaceNameChanged" } |
| 80 |
SignalSpy {id:serviceListMajorVersionSpy; target: testList; signalName: "majorVersionChanged" } |
| 81 |
SignalSpy {id:serviceListMinorVerisonSpy; target: testList; signalName: "minorVersionChanged" } |
| 82 |
SignalSpy {id:serviceListMonitoeRegSpy; target: testList; signalName: "monitorServiceRegistrationsChanged" } |
| 83 |
SignalSpy {id:serviceListServicesSpy; target: testList; signalName: "serviceDescriptionListChanged" } |
| 84 |
|
| 85 |
function resetListSpies() { |
| 86 |
serviceListNameSpy.clear() |
| 87 |
serviceListInterfaceSpy.clear() |
| 88 |
serviceListMajorVersionSpy.clear() |
| 89 |
serviceListMinorVerisonSpy.clear() |
| 90 |
serviceListServicesSpy.clear() |
| 91 |
} |
| 92 |
|
| 93 |
function init() { |
| 94 |
resetListSpies() |
| 95 |
} |
| 96 |
|
| 97 |
function test_service_Async_basic() { |
| 98 |
// always run first, check a pristine object |
| 99 |
compare(serviceObjectChangedSpy.count, 0) |
| 100 |
compare(interfaceNameChangedSpy.count, 0) |
| 101 |
compare(statusChangedSpy.count, 0) |
| 102 |
compare(descriptorChangedSpy.count, 0) |
| 103 |
|
| 104 |
compare(testObject.status, ServiceLoader.Null) |
| 105 |
compare(testObject.errorString(), "") |
| 106 |
|
| 107 |
testObject.interfaceName = "com.nokia.qt.tests.serviceobject" |
| 108 |
compare(interfaceNameChangedSpy.count, 1) |
| 109 |
|
| 110 |
compare(statusChangedSpy.count, 1) |
| 111 |
compare(testObject.status, ServiceLoader.Loading) |
| 112 |
|
| 113 |
serviceObjectChangedSpy.wait(1000) |
| 114 |
|
| 115 |
compare(testObject.errorString(), "") |
| 116 |
compare(testObject.status, ServiceLoader.Ready) |
| 117 |
compare(statusChangedSpy.count, 2) |
| 118 |
compare(serviceObjectChangedSpy.count, 1) |
| 119 |
|
| 120 |
var obj = testObject.serviceObject |
| 121 |
|
| 122 |
verify(obj != 0, "Verify we received a valid object") |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
function test_service_Sync_basic() { |
| 127 |
targetObject = testObject2 |
| 128 |
resetSpies() |
| 129 |
|
| 130 |
// always run first, check a pristine object |
| 131 |
compare(serviceObjectChangedSpy.count, 0) |
| 132 |
compare(interfaceNameChangedSpy.count, 0) |
| 133 |
compare(statusChangedSpy.count, 0) |
| 134 |
compare(descriptorChangedSpy.count, 0) |
| 135 |
|
| 136 |
compare(testObject2.status, ServiceLoader.Null) |
| 137 |
compare(testObject2.errorString(), "") |
| 138 |
|
| 139 |
testObject2.interfaceName = "com.nokia.qt.tests.serviceobject" |
| 140 |
compare(interfaceNameChangedSpy.count, 1) |
| 141 |
|
| 142 |
compare(serviceObjectChangedSpy.count, 1) |
| 143 |
compare(testObject2.errorString(), "") |
| 144 |
compare(statusChangedSpy.count, 1) |
| 145 |
compare(testObject2.status, ServiceLoader.Ready) |
| 146 |
|
| 147 |
var obj = testObject2.serviceObject |
| 148 |
|
| 149 |
verify(obj != 0, "Verify we received a valid object") |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
function test_service_invalid_and_unset() { |
| 154 |
resetSpies() |
| 155 |
targetObject = testObject3 |
| 156 |
|
| 157 |
compare(testObject3.serviceObject, null) |
| 158 |
compare(testObject3.status, ServiceLoader.Null) |
| 159 |
|
| 160 |
resetSpies() |
| 161 |
|
| 162 |
testObject3.interfaceName = "com.nokia.qt.tests.invalid.name.that.should.never.be.registered.we.really.really.hope" |
| 163 |
compare(interfaceNameChangedSpy.count, 1) |
| 164 |
|
| 165 |
statusChangedSpy.wait(1000) |
| 166 |
statusChangedSpy.wait(1000) |
| 167 |
compare(statusChangedSpy.count, 2) |
| 168 |
compare(testObject3.status, ServiceLoader.Error) |
| 169 |
compare(serviceObjectChangedSpy.count, 0) |
| 170 |
compare(testObject3.serviceObject, null) |
| 171 |
//TODO: Verify correct error string |
| 172 |
|
| 173 |
testObject3.interfaceName = "com.nokia.qt.tests.serviceobject" |
| 174 |
compare(interfaceNameChangedSpy.count, 2) |
| 175 |
|
| 176 |
statusChangedSpy.wait(1000) |
| 177 |
statusChangedSpy.wait(1000) |
| 178 |
compare(statusChangedSpy.count, 4) |
| 179 |
compare(testObject3.status, ServiceLoader.Ready) |
| 180 |
compare(serviceObjectChangedSpy.count, 1) |
| 181 |
|
| 182 |
testObject3.interfaceName = "" |
| 183 |
compare(interfaceNameChangedSpy.count, 3) |
| 184 |
compare(testObject3.serviceObject, null) |
| 185 |
compare(serviceObjectChangedSpy.count, 2) |
| 186 |
compare(testObject3.status, ServiceLoader.Null) |
| 187 |
compare(statusChangedSpy.count, 5) |
| 188 |
} |
| 189 |
|
| 190 |
function test_service_loading_creation() { |
| 191 |
|
| 192 |
// |
| 193 |
// Test multiple creation |
| 194 |
// |
| 195 |
targetObject = testObject; |
| 196 |
testObject.interfaceName = testObject2.interfaceName = testObject3.interfaceName = "" |
| 197 |
resetSpies() |
| 198 |
testObject.interfaceName = "com.nokia.qt.tests.serviceobject" |
| 199 |
testObject2.interfaceName = "com.nokia.qt.tests.serviceobject" |
| 200 |
testObject3.interfaceName = "com.nokia.qt.tests.serviceobject" |
| 201 |
serviceObjectChangedSpy.wait(1000) |
| 202 |
|
| 203 |
if (testObject.status == ServiceLoader.Error) |
| 204 |
dumpSpies() |
| 205 |
|
| 206 |
compare(testObject.status, ServiceLoader.Ready) |
| 207 |
|
| 208 |
var obj = testObject.serviceObject |
| 209 |
|
| 210 |
verify(obj != null, "Verify we have not received a null object") |
| 211 |
|
| 212 |
var o2 = testObject2.serviceObject |
| 213 |
|
| 214 |
verify(o2 != null, "Verify we have received a second valid object") |
| 215 |
} |
| 216 |
|
| 217 |
function test_service_loading_dblookup() { |
| 218 |
// |
| 219 |
// test simultaneous objects with failures |
| 220 |
// |
| 221 |
targetObject = testObject; |
| 222 |
testObject.interfaceName = testObject2.interfaceName = testObject3.interfaceName = "" |
| 223 |
resetSpies() |
| 224 |
testObject.interfaceName = "com.nokia.qt.tests.serviceobject" |
| 225 |
testObject2.interfaceName = "com.nokia.qt.tests.serviceobject2" |
| 226 |
testObject3.interfaceName = "com.nokia.qt.tests.serviceobject3" |
| 227 |
|
| 228 |
serviceObjectChangedSpy.wait(1000) |
| 229 |
|
| 230 |
compare(testObject.status, ServiceLoader.Ready) |
| 231 |
compare(testObject2.status, ServiceLoader.Error) |
| 232 |
compare(testObject3.status, ServiceLoader.Error) |
| 233 |
|
| 234 |
var obj = testObject.serviceObject |
| 235 |
|
| 236 |
verify(obj != null, "Verify we have not received a null object") |
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
function test_service_z_errors_async() { |
| 241 |
targetObject = testObject; |
| 242 |
testObject.interfaceName = "" |
| 243 |
testObject.asynchronous = true |
| 244 |
resetSpies() |
| 245 |
testObject.interfaceName = "com.nokia.qt.tests.serviceobject" |
| 246 |
|
| 247 |
serviceObjectChangedSpy.wait(1000) |
| 248 |
resetSpies() |
| 249 |
|
| 250 |
var obj = testObject.serviceObject |
| 251 |
|
| 252 |
verify(obj != null, "Can get a non-null pointer to the service") |
| 253 |
|
| 254 |
obj.slotWithoutArg() |
| 255 |
|
| 256 |
obj.closeClientSockets() |
| 257 |
|
| 258 |
statusChangedSpy.wait(1000) |
| 259 |
verify(obj.status, ServiceLoader.Error) |
| 260 |
|
| 261 |
// try using the use the object now |
| 262 |
try { |
| 263 |
obj.slotWithoutArg() |
| 264 |
} |
| 265 |
catch(err) { |
| 266 |
verify(err.message.search("has no method"), "should fail properly"); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
Item { |
| 271 |
id: dataTester |
| 272 |
signal done |
| 273 |
|
| 274 |
property bool enabled |
| 275 |
property int loops: 10 |
| 276 |
|
| 277 |
onEnabledChanged: if (enabled == true) { sfwObj.slotWithArg(1) } |
| 278 |
|
| 279 |
Connections { |
| 280 |
target: sfwObj == null ? null : sfwObj |
| 281 |
ignoreUnknownSignals: true |
| 282 |
onStateChanged: { |
| 283 |
if (dataTester.loops-- > 0) { |
| 284 |
sfwObj.slotWithArg(1) |
| 285 |
} else { |
| 286 |
dataTester.enabled = false |
| 287 |
dataTester.done() |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
SignalSpy {id: dataTestSpy; target: dataTester; signalName: "done" } |
| 294 |
|
| 295 |
function test_service_mover() { |
| 296 |
testObject.interfaceName = "" |
| 297 |
resetSpies() |
| 298 |
testObject.interfaceName = "com.nokia.qt.tests.serviceobject" |
| 299 |
|
| 300 |
serviceObjectChangedSpy.wait(1000) |
| 301 |
|
| 302 |
sfwObj = testObject.serviceObject |
| 303 |
|
| 304 |
verify(sfwObj != null, "Can't use a null object for testing") |
| 305 |
|
| 306 |
dataTester.loops = 100 |
| 307 |
dataTester.enabled = true |
| 308 |
|
| 309 |
dataTestSpy.wait(1000) |
| 310 |
} |
| 311 |
|
| 312 |
Item { |
| 313 |
id: dataTesterSignals |
| 314 |
signal done |
| 315 |
|
| 316 |
property bool enabled |
| 317 |
property int loops: 10 |
| 318 |
|
| 319 |
onEnabledChanged: if (enabled == true) { sfwObj.slotWithArg(loops) } |
| 320 |
|
| 321 |
Connections { |
| 322 |
target: ((sfwObj == null) && dataTesterSignals.enabled) ? empty : sfwObj |
| 323 |
ignoreUnknownSignals: true |
| 324 |
onStateChanged: { |
| 325 |
if (dataTester.loops-- <= 0) { |
| 326 |
dataTesterSignals.enabled = false |
| 327 |
dataTesterSignals.done() |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
SignalSpy {id: dataTestSignalsSpy; target: dataTesterSignals; signalName: "done" } |
| 334 |
|
| 335 |
function test_service_mover_signals() { |
| 336 |
testObject.interfaceName = "" |
| 337 |
resetSpies() |
| 338 |
testObject.interfaceName = "com.nokia.qt.tests.serviceobject" |
| 339 |
|
| 340 |
serviceObjectChangedSpy.wait(1000) |
| 341 |
|
| 342 |
sfwObj = testObject.serviceObject |
| 343 |
|
| 344 |
verify(sfwObj != null, "Can't use a null object for testing") |
| 345 |
|
| 346 |
dataTesterSignals.loops = 10 |
| 347 |
dataTesterSignals.enabled = true |
| 348 |
|
| 349 |
dataTestSignalsSpy.wait(1000) |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
function test_service_z_errors_sync() { |
| 354 |
targetObject = testObject |
| 355 |
testObject.interfaceName = "" |
| 356 |
testObject.asynchronous = false |
| 357 |
resetSpies() |
| 358 |
testObject.interfaceName = "com.nokia.qt.tests.serviceobject" |
| 359 |
|
| 360 |
statusChangedSpy.wait(1000) |
| 361 |
|
| 362 |
var obj = testObject.serviceObject |
| 363 |
|
| 364 |
console.log("5" + obj + " " + testObject.errorString() + " : " + testObject.status) |
| 365 |
verify(obj != null, "Can not get a valid pointer to the service") |
| 366 |
|
| 367 |
obj.slotWithoutArg() |
| 368 |
|
| 369 |
var b = obj.closeClientSocketsBlocking() |
| 370 |
|
| 371 |
statusChangedSpy.wait(1000) |
| 372 |
|
| 373 |
// try using the use the object now |
| 374 |
try { |
| 375 |
console.log("9") |
| 376 |
obj.slotWithoutArg() |
| 377 |
console.log("10") |
| 378 |
} |
| 379 |
catch(err) { |
| 380 |
verify(err.message.search("has no method"), "should fail properly"); |
| 381 |
} |
| 382 |
console.log("11") |
| 383 |
|
| 384 |
resetSpies() |
| 385 |
testObject.interfaceName = "com.nokia.qt.tests.serviceobject" |
| 386 |
console.log("12") |
| 387 |
serviceObjectChangedSpy.wait(1000) |
| 388 |
console.log("13") |
| 389 |
|
| 390 |
// old object should be destroyed, try the new one |
| 391 |
var b = testObject.serviceObject |
| 392 |
console.log("14") |
| 393 |
|
| 394 |
verify(b != null, "Must get a valid object back") |
| 395 |
verify(b.slotWithOk() == true, "") |
| 396 |
console.log("15") |
| 397 |
|
| 398 |
} |
| 399 |
|
| 400 |
function test_service_list_A_empty() { |
| 401 |
compare(serviceListInterfaceSpy.count, 0) |
| 402 |
compare(serviceListMajorVersionSpy.count, 0) |
| 403 |
compare(serviceListMinorVerisonSpy.count, 0) |
| 404 |
compare(serviceListMonitoeRegSpy.count, 0) |
| 405 |
compare(serviceListNameSpy.count, 0) |
| 406 |
|
| 407 |
verify(testList.serviceDescriptions.length > 0, "Make sure our service is listed") |
| 408 |
} |
| 409 |
|
| 410 |
function test_service_list_basics() { |
| 411 |
|
| 412 |
resetListSpies() |
| 413 |
|
| 414 |
testList.interfaceName = "com.nokia.qt.tests.serviceobject" |
| 415 |
|
| 416 |
serviceListServicesSpy.wait(1000) |
| 417 |
|
| 418 |
console.log("Test list2 is a: " + testList.serviceDescriptions.length + " " + serviceListServicesSpy.count); |
| 419 |
|
| 420 |
compare(testList.serviceDescriptions.length, 1, "Should only select our service") |
| 421 |
compare(serviceListServiceSpy.count, 1, "Only 1 signal") |
| 422 |
compare(serviceListNameSpy.count, 0, "No signal") |
| 423 |
|
| 424 |
testList.interfaceName = "com.nokia.qt.tests.invalid.name.that.should.never.be.registered" |
| 425 |
|
| 426 |
serviceListServicesSpy.wait(1000) |
| 427 |
compare(testList.serviceDescriptions.length, 0, "Should select no serviceDescriptions") |
| 428 |
|
| 429 |
resetListSpies() |
| 430 |
testList.interfaceName = "com.nokia.qt.tests.serviceobject" |
| 431 |
serviceListServicesSpy.wait(1000) |
| 432 |
resetListSpies() |
| 433 |
|
| 434 |
testList.majorVersion = 2 |
| 435 |
compare(testList.majorVersion, 2, "Major version should change") |
| 436 |
compare(serviceListMajorVersionSpy.count, 1, "Expect 1 signal on change") |
| 437 |
serviceListServicesSpy.wait(1000) |
| 438 |
|
| 439 |
resetListSpies() |
| 440 |
testList.majorVersion = 0 |
| 441 |
testList.minorVersion = 0 |
| 442 |
testList.interfaceName = "com.nokia.qt.tests.serviceobject" |
| 443 |
serviceListServicesSpy.wait(1000) |
| 444 |
compare(testList.serviceDescriptions.length, 1, "Should only select our service") |
| 445 |
resetListSpies() |
| 446 |
|
| 447 |
testList.majorVersion = 1 |
| 448 |
testList.minorVersion = 1 |
| 449 |
compare(testList.minorVersion, 1, "Minor versino should change") |
| 450 |
compare(serviceListMinorVerisonSpy.count, 1, "Expect 1 signal on change") |
| 451 |
console.log("length is " + testList.serviceDescriptions.length) |
| 452 |
serviceListServicesSpy.wait(1000) |
| 453 |
|
| 454 |
} |
| 455 |
|
| 456 |
function dumpSpies() { |
| 457 |
|
| 458 |
console.log("********") |
| 459 |
console.log(serviceObjectChangedSpy) |
| 460 |
console.log("validChangedSpy.count " + validChangedSpy.count) |
| 461 |
console.log("serviceObjectChanged.count " + serviceObjectChangedSpy.count) |
| 462 |
console.log("interfacenamedChanged.count " + interfaceNameChangedSpy.count) |
| 463 |
console.log("serviceNameChanged.count " + serviceNameChangedSpy.count) |
| 464 |
console.log("majorVersionChanged.count " + majorVersionChangedSpy.count) |
| 465 |
console.log("minorVersionChange.count " + minorVersionChangedSpy.count) |
| 466 |
console.log("errorChanged.count " + errorChangedSpy.count) |
| 467 |
console.log("lastError " + testObject.error) |
| 468 |
console.log("***********") |
| 469 |
|
| 470 |
} |
| 471 |
} |