From c6c3673143ee1ceef686a014373bd99da1bddabd Mon Sep 17 00:00:00 2001 From: Aurindam Jana Date: Wed, 9 May 2012 11:30:27 +0200 Subject: [PATCH] QQmlEngineDebugService: Query Expression If the scope object is null or if there no QML context for the scope object, try to use the root context of the engine. Change-Id: I0068ea36f18a179b44791b81a2f6b8ec5423f615 Reviewed-by: Kai Koehne --- src/qml/debugger/qqmlenginedebugservice.cpp | 11 ++++- .../qqmlenginedebugclient.cpp | 20 +++++++- .../qqmlenginedebugservice/qqmlenginedebugclient.h | 3 + .../tst_qqmlenginedebugservice.cpp | 50 ++++++++++++++++++++ 4 files changed, 82 insertions(+), 2 deletions(-) diff --git a/src/qml/debugger/qqmlenginedebugservice.cpp b/src/qml/debugger/qqmlenginedebugservice.cpp index b095e8f..21e9d67 100644 --- a/src/qml/debugger/qqmlenginedebugservice.cpp +++ b/src/qml/debugger/qqmlenginedebugservice.cpp @@ -499,11 +499,20 @@ void QQmlEngineDebugService::processMessage(const QByteArray &message) QString expr; ds >> objectId >> expr; + int engineId = -1; + if (!ds.atEnd()) + ds >> engineId; QObject *object = QQmlDebugService::objectForId(objectId); QQmlContext *context = qmlContext(object); + if (!context) { + QQmlEngine *engine = qobject_cast( + QQmlDebugService::objectForId(engineId)); + if (engine && m_engines.contains(engine)) + context = engine->rootContext(); + } QVariant result; - if (object && context) { + if (context) { QQmlExpression exprObj(context, object, expr); bool undefined = false; QVariant value = exprObj.evaluate(&undefined); diff --git a/tests/auto/qml/debugger/qqmlenginedebugservice/qqmlenginedebugclient.cpp b/tests/auto/qml/debugger/qqmlenginedebugservice/qqmlenginedebugclient.cpp index 0c5dfdd..eb04fb2 100644 --- a/tests/auto/qml/debugger/qqmlenginedebugservice/qqmlenginedebugclient.cpp +++ b/tests/auto/qml/debugger/qqmlenginedebugservice/qqmlenginedebugclient.cpp @@ -241,7 +241,25 @@ quint32 QQmlEngineDebugClient::queryExpressionResult( m_exprResult = QVariant(); quint32 id; *success = false; - if (state() == QQmlDebugClient::Enabled && objectDebugId != -1) { + if (state() == QQmlDebugClient::Enabled) { + id = getId(); + QByteArray message; + QDataStream ds(&message, QIODevice::WriteOnly); + ds << QByteArray("EVAL_EXPRESSION") << id << objectDebugId << expr + << engines()[0].debugId; + sendMessage(message); + *success = true; + } + return id; +} + +quint32 QQmlEngineDebugClient::queryExpressionResultBC( + int objectDebugId, const QString &expr, bool *success) +{ + m_exprResult = QVariant(); + quint32 id; + *success = false; + if (state() == QQmlDebugClient::Enabled) { id = getId(); QByteArray message; QDataStream ds(&message, QIODevice::WriteOnly); diff --git a/tests/auto/qml/debugger/qqmlenginedebugservice/qqmlenginedebugclient.h b/tests/auto/qml/debugger/qqmlenginedebugservice/qqmlenginedebugclient.h index 62f9b15..34d4e97 100644 --- a/tests/auto/qml/debugger/qqmlenginedebugservice/qqmlenginedebugclient.h +++ b/tests/auto/qml/debugger/qqmlenginedebugservice/qqmlenginedebugclient.h @@ -197,6 +197,9 @@ public: quint32 queryExpressionResult(int objectDebugId, const QString &expr, bool *success); + quint32 queryExpressionResultBC(int objectDebugId, + const QString &expr, + bool *success); quint32 setBindingForObject(int objectDebugId, const QString &propertyName, const QVariant &bindingExpression, bool isLiteralValue, diff --git a/tests/auto/qml/debugger/qqmlenginedebugservice/tst_qqmlenginedebugservice.cpp b/tests/auto/qml/debugger/qqmlenginedebugservice/tst_qqmlenginedebugservice.cpp index cc1193b..17b22b4 100644 --- a/tests/auto/qml/debugger/qqmlenginedebugservice/tst_qqmlenginedebugservice.cpp +++ b/tests/auto/qml/debugger/qqmlenginedebugservice/tst_qqmlenginedebugservice.cpp @@ -120,6 +120,9 @@ private slots: void queryObject_data(); void queryExpressionResult(); void queryExpressionResult_data(); + void queryExpressionResultInRootContext(); + void queryExpressionResultBC(); + void queryExpressionResultBC_data(); void setBindingForObject(); void setMethodBody(); @@ -721,6 +724,53 @@ void tst_QQmlEngineDebugService::queryExpressionResult_data() QTest::newRow("varObjMap") << "varObjMap" << qVariantFromValue(map); } +void tst_QQmlEngineDebugService::queryExpressionResultInRootContext() +{ + bool success; + const QString exp = QLatin1String("1"); + m_dbg->queryExpressionResult(-1, exp, &success); + QVERIFY(success); + QVERIFY(QQmlDebugTest::waitForSignal(m_dbg, SIGNAL(result()))); + + QCOMPARE(m_dbg->resultExpr().toString(), exp); +} + +void tst_QQmlEngineDebugService::queryExpressionResultBC() +{ + QFETCH(QString, expr); + QFETCH(QVariant, result); + + int objectId = findRootObject().debugId; + + bool success; + + QQmlEngineDebugClient *unconnected = new QQmlEngineDebugClient(0); + unconnected->queryExpressionResultBC(objectId, expr, &success); + QVERIFY(!success); + delete unconnected; + + m_dbg->queryExpressionResultBC(objectId, expr, &success); + QVERIFY(success); + QVERIFY(QQmlDebugTest::waitForSignal(m_dbg, SIGNAL(result()))); + + QCOMPARE(m_dbg->resultExpr(), result); +} + +void tst_QQmlEngineDebugService::queryExpressionResultBC_data() +{ + QTest::addColumn("expr"); + QTest::addColumn("result"); + + QTest::newRow("width + 50") << "width + 50" << qVariantFromValue(60); + QTest::newRow("blueRect.width") << "blueRect.width" << qVariantFromValue(500); + QTest::newRow("bad expr") << "aeaef" << qVariantFromValue(QString("")); + QTest::newRow("QObject*") << "varObj" << qVariantFromValue(QString("")); + QTest::newRow("list of QObject*") << "varObjList" << qVariantFromValue(QString("")); + QVariantMap map; + map.insert(QLatin1String("rect"), QVariant(QLatin1String(""))); + QTest::newRow("varObjMap") << "varObjMap" << qVariantFromValue(map); +} + void tst_QQmlEngineDebugService::setBindingForObject() { QmlDebugObjectReference rootObject = findRootObject(); -- 1.7.2.5