diff options
author | Juha Vuolle <juha.vuolle@insta.fi> | 2021-06-10 13:27:19 +0300 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-06-15 09:39:14 +0000 |
commit | ce77413488e1da71ef286ec40f5a4afd40cab512 (patch) | |
tree | 759a95366e4e4a0667048fef84452eac0b5e13f5 | |
parent | 7a82ee1645ef0df9c766031edd20ea99d5ea7b6e (diff) |
Sensor identifier autotest and fix related findings
This commit adds a (QML) test for sensor identifier (and activation
whose logic is closely related). The related changes:
* Change sensor 'identifier' and 'type' properties to QByteArray. This
now matches with C++ side that uses QByteArray
* Allow changing of 'identifier' after componentComplete. This is now
aligned with C++ side. Changing identifier is fine as long as the
backend is not connected.
Task-number: QTBUG-92513
Task-number: QTBUG-92505
Change-Id: I326d840d5a4efb13a3a6578711563e8054cc9961
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
(cherry picked from commit c3cc1253ba159e6f4c9bd4c92da2b8c300ee94f2)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/sensors/doc/src/qt6-changes.qdoc | 5 | ||||
-rw-r--r-- | src/sensorsquick/qmlsensor.cpp | 31 | ||||
-rw-r--r-- | src/sensorsquick/qmlsensor_p.h | 12 | ||||
-rw-r--r-- | tests/auto/qml/qml_quick/tst_sensors_basic.qml | 89 |
4 files changed, 106 insertions, 31 deletions
diff --git a/src/sensors/doc/src/qt6-changes.qdoc b/src/sensors/doc/src/qt6-changes.qdoc index f71cde15..ffde4d19 100644 --- a/src/sensors/doc/src/qt6-changes.qdoc +++ b/src/sensors/doc/src/qt6-changes.qdoc @@ -79,4 +79,9 @@ that in most if not all cases the changes are not mandatory. Instead it is up to the individual sensor backend implementations to decide if the changes are useful. + \section2 Sensor::type and and QSensor::type properties marked as constant + + The \c type property cannot change after instantiation and is marked as constant + in both C++ and QML. + */ diff --git a/src/sensorsquick/qmlsensor.cpp b/src/sensorsquick/qmlsensor.cpp index 14dc21c7..2ff544a5 100644 --- a/src/sensorsquick/qmlsensor.cpp +++ b/src/sensorsquick/qmlsensor.cpp @@ -104,16 +104,14 @@ QmlSensor::~QmlSensor() Please see QSensor::identifier for information about this property. */ -QString QmlSensor::identifier() const +QByteArray QmlSensor::identifier() const { - return m_identifier; + return sensor()->identifier(); } -void QmlSensor::setIdentifier(const QString &identifier) +void QmlSensor::setIdentifier(const QByteArray &identifier) { - if (m_componentComplete) return; - m_identifier = identifier; - Q_EMIT identifierChanged(); + sensor()->setIdentifier(identifier); } /*! @@ -121,9 +119,9 @@ void QmlSensor::setIdentifier(const QString &identifier) This property holds the type of the sensor. */ -QString QmlSensor::type() const +QByteArray QmlSensor::type() const { - return QString::fromLatin1(sensor()->type()); + return sensor()->type(); } /*! @@ -448,26 +446,17 @@ void QmlSensor::componentComplete() connect(sensor(), SIGNAL(maxBufferSizeChanged(int)), this, SIGNAL(maxBufferSizeChanged(int))); connect(sensor(), SIGNAL(efficientBufferSizeChanged(int)), this, SIGNAL(efficientBufferSizeChanged(int))); connect(sensor(), &QSensor::busyChanged, this, &QmlSensor::busyChanged); - - // We need to set this on the sensor object now - sensor()->setIdentifier(m_identifier.toLocal8Bit()); + connect(sensor(), &QSensor::identifierChanged, this, &QmlSensor::identifierChanged); // These can change! - QByteArray oldIdentifier = sensor()->identifier(); int oldDataRate = dataRate(); int oldOutputRange = outputRange(); - bool ok = sensor()->connectToBackend(); - if (ok) { + if (sensor()->connectToBackend()) Q_EMIT connectedToBackendChanged(); - m_reading = createReading(); - m_reading->setParent(this); - } - if (oldIdentifier != sensor()->identifier()) { - m_identifier = QString::fromLatin1(sensor()->identifier()); - Q_EMIT identifierChanged(); - } + m_reading = createReading(); + m_reading->setParent(this); if (oldDataRate != dataRate()) Q_EMIT dataRateChanged(); if (oldOutputRange != outputRange()) diff --git a/src/sensorsquick/qmlsensor_p.h b/src/sensorsquick/qmlsensor_p.h index 2b893c99..276b6e76 100644 --- a/src/sensorsquick/qmlsensor_p.h +++ b/src/sensorsquick/qmlsensor_p.h @@ -72,8 +72,8 @@ class Q_SENSORSQUICK_PRIVATE_EXPORT QmlSensor : public QObject, public QQmlParse Q_OBJECT Q_DECLARE_PRIVATE(QmlSensor) Q_INTERFACES(QQmlParserStatus) - Q_PROPERTY(QString identifier READ identifier WRITE setIdentifier NOTIFY identifierChanged) - Q_PROPERTY(QString type READ type NOTIFY typeChanged) + Q_PROPERTY(QByteArray identifier READ identifier WRITE setIdentifier NOTIFY identifierChanged) + Q_PROPERTY(QByteArray type READ type CONSTANT) Q_PROPERTY(bool connectedToBackend READ isConnectedToBackend NOTIFY connectedToBackendChanged) Q_PROPERTY(QQmlListProperty<QmlSensorRange> availableDataRates READ availableDataRates NOTIFY availableDataRatesChanged) Q_PROPERTY(int dataRate READ dataRate WRITE setDataRate NOTIFY dataRateChanged) @@ -108,10 +108,10 @@ public: explicit QmlSensor(QObject *parent = 0); ~QmlSensor(); - QString identifier() const; - void setIdentifier(const QString &identifier); + QByteArray identifier() const; + void setIdentifier(const QByteArray &identifier); - QString type() const; + QByteArray type() const; bool isConnectedToBackend() const; @@ -162,7 +162,6 @@ public Q_SLOTS: Q_SIGNALS: void identifierChanged(); - void typeChanged(); void connectedToBackendChanged(); void availableDataRatesChanged(); void dataRateChanged(); @@ -196,7 +195,6 @@ private: virtual void _update(); bool m_componentComplete = false; bool m_activateOnComplete = false; - QString m_identifier; QmlSensorReading *m_reading = nullptr; }; diff --git a/tests/auto/qml/qml_quick/tst_sensors_basic.qml b/tests/auto/qml/qml_quick/tst_sensors_basic.qml index 298ecef8..37895ba0 100644 --- a/tests/auto/qml/qml_quick/tst_sensors_basic.qml +++ b/tests/auto/qml/qml_quick/tst_sensors_basic.qml @@ -31,6 +31,7 @@ import QtSensors TestCase { id: testCase + name: "SensorTest" SignalSpy { id: sensorActiveSpy @@ -47,12 +48,97 @@ TestCase { signalName: "busyChanged" } + SignalSpy { + id: sensorIdentifierSpy + signalName: "identifierChanged" + } + function init() { TestControl.registerTestBackends() } function cleanup() { TestControl.unregisterTestBackends() + sensorBusySpy.clear() + sensorActiveSpy.clear() + sensorReadingSpy.clear() + sensorIdentifierSpy.clear() + } + + function test_activate() { + + // create sensor without proper identifier and verify activation fails + var sensor = Qt.createQmlObject("import QtSensors; Accelerometer {identifier: \"nonexistent\"}",testCase); + sensorActiveSpy.target = sensor + sensorIdentifierSpy.target = sensor + verify(!sensor.active) + compare(sensor.identifier, "nonexistent") + sensor.active = true + verify(!sensor.active) + compare(sensorActiveSpy.count, 0) + + // set proper identifier and verify activation succeeds + sensor.identifier = "QAccelerometer" + compare(sensor.identifier, "QAccelerometer") + compare(sensorIdentifierSpy.count, 1) + sensor.active = true + compare(sensorActiveSpy.count, 1) + verify(sensor.active) + compare(sensor.reading.x, 1.0) + + // set identifier again, verify no impact + sensor.identifier = "QAccelerometer" + compare(sensor.identifier, "QAccelerometer") + compare(sensorIdentifierSpy.count, 1) + + // set activate again, verify no impact + sensor.active = true + sensor.start() + compare(sensorActiveSpy.count, 1) + verify(sensor.active) + + // deactivate + sensor.active = false + compare(sensorActiveSpy.count, 2) + verify(!sensor.active) + + // reactivate and stop + sensor.active = true + compare(sensorActiveSpy.count, 3) + verify(sensor.active) + sensor.stop() + compare(sensorActiveSpy.count, 4) + verify(!sensor.active) + + // create sensor with proper id and active 'true' on creation time + var sensor2 = Qt.createQmlObject("import QtSensors; Accelerometer {identifier: \"QAccelerometer\"; active: true}", testCase); + verify(sensor2.active) + + // create sensor with nonexistent id and active 'true' on creation time + var sensor3 = Qt.createQmlObject("import QtSensors; Accelerometer {identifier: \"nonexistent\"; active: true}", testCase); + verify(!sensor3.active) + sensor3.identifier = "QAccelerometer" + sensor3.start() + verify(sensor3.active) + + // create sensor with empty id, and check that a default is used + var sensor4 = Qt.createQmlObject("import QtSensors; Accelerometer {active: true}", testCase); + verify(sensor4.active) + compare(sensor4.identifier, QmlSensors.defaultSensorForType("QAccelerometer")); + + // same as previous but with delayed activation + var sensor5 = Qt.createQmlObject("import QtSensors; Accelerometer {}", testCase); + verify(!sensor5.active) + sensor5.active = true + verify(sensor5.active) + compare(sensor5.identifier, QmlSensors.defaultSensorForType("QAccelerometer")); + + // tidy up + sensor.destroy() + sensor2.destroy() + sensor3.destroy() + sensor4.destroy() + sensor5.destroy() } function test_busy() { @@ -71,7 +157,6 @@ TestCase { // tidy up sensor.destroy() - sensorBusySpy.clear() } function test_reading(data) { @@ -119,8 +204,6 @@ TestCase { // tidy up sensor.destroy() - sensorActiveSpy.clear() - sensorReadingSpy.clear() } function test_reading_data() { |