summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorØystein Heskestad <oystein.heskestad@qt.io>2021-05-10 15:28:52 +0200
committerArno Rehn <a.rehn@menlosystems.com>2021-05-20 13:38:10 +0000
commitf8ee2550f7d8f65954facb919bf45bfcf2bb4c3d (patch)
tree21fae844ba2f90f040ee45b13a158658673418b2
parenta7199de7d90f48ce3d95cae795bd9209c39516ce (diff)
downloadqtwebchannel-f8ee2550f7d8f65954facb919bf45bfcf2bb4c3d.tar.gz
Make blockUpdates bindable
[ChangeLog] Make blockUpdates bindable Task-number: QTBUG-93601 Change-Id: I7a5e06ecb3258b11988343321b78ffaab1d48dc0 Reviewed-by: Arno Rehn <a.rehn@menlosystems.com>
-rw-r--r--src/webchannel/qmetaobjectpublisher.cpp26
-rw-r--r--src/webchannel/qmetaobjectpublisher_p.h6
-rw-r--r--src/webchannel/qwebchannel.cpp8
-rw-r--r--src/webchannel/qwebchannel.h4
-rw-r--r--tests/auto/webchannel/tst_webchannel.cpp49
-rw-r--r--tests/auto/webchannel/tst_webchannel.h1
6 files changed, 66 insertions, 28 deletions
diff --git a/src/webchannel/qmetaobjectpublisher.cpp b/src/webchannel/qmetaobjectpublisher.cpp
index f478ede..53cf840 100644
--- a/src/webchannel/qmetaobjectpublisher.cpp
+++ b/src/webchannel/qmetaobjectpublisher.cpp
@@ -202,7 +202,9 @@ void QWebChannelPropertyChangeNotifier::notify(
QMetaObjectPublisher::QMetaObjectPublisher(QWebChannel *webChannel)
: QObject(webChannel),
webChannel(webChannel),
- blockUpdates(false),
+ blockUpdatesStatus(false),
+ blockUpdatesHandler(blockUpdatesStatus.onValueChanged(
+ std::function<void()>([&]() { this->onBlockUpdatesChanged(); }))),
propertyUpdatesInitialized(false),
propertyUpdateIntervalTime(50),
propertyUpdateIntervalHandler(propertyUpdateIntervalTime.onValueChanged(
@@ -394,7 +396,7 @@ void QMetaObjectPublisher::initializePropertyUpdates(QObject *const object, cons
void QMetaObjectPublisher::sendPendingPropertyUpdates()
{
- if (blockUpdates) {
+ if (blockUpdatesStatus) {
return;
}
@@ -621,8 +623,9 @@ void QMetaObjectPublisher::propertyValueChanged(const QObject *object, const int
void QMetaObjectPublisher::startPropertyUpdateTimer(bool forceRestart)
{
- if (blockUpdates)
+ if (blockUpdatesStatus)
return;
+
if (propertyUpdateIntervalTime >= 0) {
if (forceRestart || !timer.isActive())
timer.start(propertyUpdateIntervalTime, this);
@@ -1066,19 +1069,24 @@ void QMetaObjectPublisher::setPropertyUpdateInterval(int ms)
void QMetaObjectPublisher::setBlockUpdates(bool block)
{
- if (blockUpdates == block) {
- return;
- }
- blockUpdates = block;
+ blockUpdatesStatus = block;
+}
- if (!blockUpdates) {
+void QMetaObjectPublisher::onBlockUpdatesChanged()
+{
+ if (!blockUpdatesStatus) {
startPropertyUpdateTimer();
sendPendingPropertyUpdates();
} else if (timer.isActive()) {
timer.stop();
}
- emit blockUpdatesChanged(block);
+ emit blockUpdatesChanged(blockUpdatesStatus);
+}
+
+bool QMetaObjectPublisher::blockUpdates() const
+{
+ return blockUpdatesStatus;
}
void QMetaObjectPublisher::timerEvent(QTimerEvent *event)
diff --git a/src/webchannel/qmetaobjectpublisher_p.h b/src/webchannel/qmetaobjectpublisher_p.h
index 60a855f..a84eb52 100644
--- a/src/webchannel/qmetaobjectpublisher_p.h
+++ b/src/webchannel/qmetaobjectpublisher_p.h
@@ -320,6 +320,7 @@ public:
* When updates are blocked, no property updates are transmitted to remote clients.
*/
void setBlockUpdates(bool block);
+ bool blockUpdates() const;
Q_SIGNALS:
void blockUpdatesChanged(bool block);
@@ -334,6 +335,8 @@ protected:
void timerEvent(QTimerEvent *) override;
private:
+ void onBlockUpdatesChanged();
+
friend class QQmlWebChannelPrivate;
friend class QWebChannel;
friend class TestWebChannel;
@@ -353,8 +356,9 @@ private:
QHash<QWebChannelAbstractTransport *, TransportState> transportState;
// true when no property updates should be sent, false otherwise
- bool blockUpdates;
+ Q_OBJECT_BINDABLE_PROPERTY(QMetaObjectPublisher, bool, blockUpdatesStatus);
+ QPropertyChangeHandler<std::function<void()>> blockUpdatesHandler;
// true when at least one client was initialized and thus
// the property updates have been initialized and the
// object info map set.
diff --git a/src/webchannel/qwebchannel.cpp b/src/webchannel/qwebchannel.cpp
index 289001e..4752891 100644
--- a/src/webchannel/qwebchannel.cpp
+++ b/src/webchannel/qwebchannel.cpp
@@ -215,7 +215,7 @@ void QWebChannel::deregisterObject(QObject *object)
bool QWebChannel::blockUpdates() const
{
Q_D(const QWebChannel);
- return d->publisher->blockUpdates;
+ return d->publisher->blockUpdates();
}
void QWebChannel::setBlockUpdates(bool block)
@@ -224,6 +224,12 @@ void QWebChannel::setBlockUpdates(bool block)
d->publisher->setBlockUpdates(block);
}
+QBindable<bool> QWebChannel::bindableBlockUpdates()
+{
+ Q_D(QWebChannel);
+ return &d->publisher->blockUpdatesStatus;
+}
+
/*!
\property QWebChannel::propertyUpdateInterval
diff --git a/src/webchannel/qwebchannel.h b/src/webchannel/qwebchannel.h
index c52cc64..9d51143 100644
--- a/src/webchannel/qwebchannel.h
+++ b/src/webchannel/qwebchannel.h
@@ -55,7 +55,8 @@ class Q_WEBCHANNEL_EXPORT QWebChannel : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(QWebChannel)
- Q_PROPERTY(bool blockUpdates READ blockUpdates WRITE setBlockUpdates NOTIFY blockUpdatesChanged)
+ Q_PROPERTY(bool blockUpdates READ blockUpdates WRITE setBlockUpdates NOTIFY blockUpdatesChanged
+ BINDABLE bindableBlockUpdates)
Q_PROPERTY(int propertyUpdateInterval READ propertyUpdateInterval WRITE
setPropertyUpdateInterval BINDABLE bindablePropertyUpdateInterval)
public:
@@ -69,6 +70,7 @@ public:
bool blockUpdates() const;
void setBlockUpdates(bool block);
+ QBindable<bool> bindableBlockUpdates();
int propertyUpdateInterval() const;
void setPropertyUpdateInterval(int ms);
diff --git a/tests/auto/webchannel/tst_webchannel.cpp b/tests/auto/webchannel/tst_webchannel.cpp
index 282348f..c19bf68 100644
--- a/tests/auto/webchannel/tst_webchannel.cpp
+++ b/tests/auto/webchannel/tst_webchannel.cpp
@@ -1160,7 +1160,7 @@ void TestWebChannel::testPropertyUpdateInterval()
QFETCH(int, secondUpdateWithin);
propertyUpdateInterval = secondUpdateInterval;
publisher->setClientIsIdle(true, &transport);
- obj1 = "and";
+ obj1 = "!!!";
if (secondUpdateInterval > 0) {
QCoreApplication::processEvents();
@@ -1175,24 +1175,41 @@ void TestWebChannel::testPropertyUpdateInterval()
} else {
QCOMPARE(transport.messagesSent().size(), 2u);
}
+}
+
+void TestWebChannel::testQPropertyBlockUpdates()
+{
+ DummyTransport transport;
+ QWebChannel channel;
+ QMetaObjectPublisher *publisher = channel.d_func()->publisher;
+ publisher->setPropertyUpdateInterval(0);
+ QProperty<bool> blockUpdates(false);
+ channel.bindableBlockUpdates().setBinding(Qt::makePropertyBinding(blockUpdates));
+ QCOMPARE(channel.blockUpdates(), false);
+ bool blockedSignalled(false);
+ connect(&channel, &QWebChannel::blockUpdatesChanged, this,
+ [&](bool block) { blockedSignalled = block; });
+ TestObject testObj;
+ testObj.setObjectName("testObject");
+ channel.registerObject(testObj.objectName(), &testObj);
+ channel.connectTo(&transport);
+ QProperty<QString> obj1("Hello");
+ testObj.bindableStringProperty().setBinding(Qt::makePropertyBinding(obj1));
+ publisher->initializeClient(&transport);
+ QVERIFY(transport.messagesSent().isEmpty());
+
+ blockUpdates = true;
+ QCOMPARE(blockedSignalled, true);
publisher->setClientIsIdle(true, &transport);
- channel.setBlockUpdates(true);
- obj1 = "again";
+ obj1 = "world";
- QCOMPARE(transport.messagesSent().size(), 2u);
- if (secondUpdateWithin > 0) {
- // Confirm that it is blocked even when waiting
- QThread::msleep(secondUpdateWithin);
- QCoreApplication::processEvents();
- QCOMPARE(transport.messagesSent().size(), 2u);
- } else if (secondUpdateInterval == 0) {
- // Confirm that it is blocked even when processing events
- QCoreApplication::processEvents();
- QCOMPARE(transport.messagesSent().size(), 2u);
- }
- channel.setBlockUpdates(false);
- QCOMPARE(transport.messagesSent().size(), 3u);
+ QTest::qWait(0);
+ QCOMPARE(transport.messagesSent().size(), 0u);
+
+ blockUpdates = false;
+ QCOMPARE(blockedSignalled, false);
+ QCOMPARE(transport.messagesSent().size(), 1u);
}
void TestWebChannel::testPropertyMultipleTransports()
diff --git a/tests/auto/webchannel/tst_webchannel.h b/tests/auto/webchannel/tst_webchannel.h
index bbcf2b8..071498f 100644
--- a/tests/auto/webchannel/tst_webchannel.h
+++ b/tests/auto/webchannel/tst_webchannel.h
@@ -360,6 +360,7 @@ private slots:
void testPropertyUpdateInterval_data();
void testPropertyUpdateInterval();
void testPropertyMultipleTransports();
+ void testQPropertyBlockUpdates();
void testDeletionDuringMethodInvocation_data();
void testDeletionDuringMethodInvocation();