tests/auto/declarative/qdeclarativedebug/tst_qdeclarativedebug.cpp
changeset 33 3e2da88830cd
parent 30 5dc02b23752f
child 37 758a864f9613
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
    69 class tst_QDeclarativeDebug : public QObject
    69 class tst_QDeclarativeDebug : public QObject
    70 {
    70 {
    71     Q_OBJECT
    71     Q_OBJECT
    72 
    72 
    73 private:
    73 private:
    74     QDeclarativeDebugObjectReference findRootObject();
    74     QDeclarativeDebugObjectReference findRootObject(int context = 0);
    75     QDeclarativeDebugPropertyReference findProperty(const QList<QDeclarativeDebugPropertyReference> &props, const QString &name) const;
    75     QDeclarativeDebugPropertyReference findProperty(const QList<QDeclarativeDebugPropertyReference> &props, const QString &name) const;
    76     void waitForQuery(QDeclarativeDebugQuery *query);
    76     void waitForQuery(QDeclarativeDebugQuery *query);
    77 
    77 
    78     void recursiveObjectTest(QObject *o, const QDeclarativeDebugObjectReference &oref, bool recursive) const;
    78     void recursiveObjectTest(QObject *o, const QDeclarativeDebugObjectReference &oref, bool recursive) const;
    79 
    79 
   109     void tst_QDeclarativeDebugFileReference();
   109     void tst_QDeclarativeDebugFileReference();
   110     void tst_QDeclarativeDebugEngineReference();
   110     void tst_QDeclarativeDebugEngineReference();
   111     void tst_QDeclarativeDebugObjectReference();
   111     void tst_QDeclarativeDebugObjectReference();
   112     void tst_QDeclarativeDebugContextReference();
   112     void tst_QDeclarativeDebugContextReference();
   113     void tst_QDeclarativeDebugPropertyReference();
   113     void tst_QDeclarativeDebugPropertyReference();
       
   114 
       
   115     void setMethodBody();
   114 };
   116 };
   115 
   117 
   116 QDeclarativeDebugObjectReference tst_QDeclarativeDebug::findRootObject()
   118 QDeclarativeDebugObjectReference tst_QDeclarativeDebug::findRootObject(int context)
   117 {
   119 {
   118     QDeclarativeDebugEnginesQuery *q_engines = m_dbg->queryAvailableEngines(this);
   120     QDeclarativeDebugEnginesQuery *q_engines = m_dbg->queryAvailableEngines(this);
   119     waitForQuery(q_engines);
   121     waitForQuery(q_engines);
   120     
   122     
   121     if (q_engines->engines().count() == 0)
   123     if (q_engines->engines().count() == 0)
   123     QDeclarativeDebugRootContextQuery *q_context = m_dbg->queryRootContexts(q_engines->engines()[0].debugId(), this);
   125     QDeclarativeDebugRootContextQuery *q_context = m_dbg->queryRootContexts(q_engines->engines()[0].debugId(), this);
   124     waitForQuery(q_context);
   126     waitForQuery(q_context);
   125 
   127 
   126     if (q_context->rootContext().objects().count() == 0)
   128     if (q_context->rootContext().objects().count() == 0)
   127         return QDeclarativeDebugObjectReference();
   129         return QDeclarativeDebugObjectReference();
   128     QDeclarativeDebugObjectQuery *q_obj = m_dbg->queryObject(q_context->rootContext().objects()[0], this);
   130     QDeclarativeDebugObjectQuery *q_obj = m_dbg->queryObject(q_context->rootContext().objects()[context], this);
   129     waitForQuery(q_obj);
   131     waitForQuery(q_obj);
   130 
   132 
   131     QDeclarativeDebugObjectReference result = q_obj->object();
   133     QDeclarativeDebugObjectReference result = q_obj->object();
   132 
   134 
   133     delete q_engines;
   135     delete q_engines;
   288                 "Text { color: blueRect.color; }"
   290                 "Text { color: blueRect.color; }"
   289                 "MouseArea {"
   291                 "MouseArea {"
   290                     "onEntered: { console.log('hello') }"
   292                     "onEntered: { console.log('hello') }"
   291                 "}"
   293                 "}"
   292             "}";
   294             "}";
       
   295 
   293     // add second component to test multiple root contexts
   296     // add second component to test multiple root contexts
   294     qml << "import Qt 4.7\n"
   297     qml << "import Qt 4.7\n"
   295             "Item {}";
   298             "Item {}";
       
   299 
       
   300     // and a third to test methods
       
   301     qml << "import Qt 4.7\n"
       
   302             "Item {"
       
   303                 "function myMethodNoArgs() { return 3; }\n"
       
   304                 "function myMethod(a) { return a + 9; }\n"
       
   305                 "function myMethodIndirect() { myMethod(3); }\n"
       
   306             "}";
   296 
   307 
   297     for (int i=0; i<qml.count(); i++) {
   308     for (int i=0; i<qml.count(); i++) {
   298         QDeclarativeComponent component(m_engine);
   309         QDeclarativeComponent component(m_engine);
   299         component.setData(qml[i], QUrl::fromLocalFile(""));
   310         component.setData(qml[i], QUrl::fromLocalFile(""));
   300         Q_ASSERT(component.isReady());  // fails if bad syntax
   311         Q_ASSERT(component.isReady());  // fails if bad syntax
   318 }
   329 }
   319 
   330 
   320 void tst_QDeclarativeDebug::cleanupTestCase()
   331 void tst_QDeclarativeDebug::cleanupTestCase()
   321 {
   332 {
   322     qDeleteAll(m_components);
   333     qDeleteAll(m_components);
       
   334 }
       
   335 
       
   336 void tst_QDeclarativeDebug::setMethodBody()
       
   337 {
       
   338     QDeclarativeDebugObjectReference obj = findRootObject(2);
       
   339 
       
   340     QObject *root = m_components.at(2);
       
   341     // Without args
       
   342     {
       
   343     QVariant rv;
       
   344     QVERIFY(QMetaObject::invokeMethod(root, "myMethodNoArgs", Qt::DirectConnection,
       
   345                                       Q_RETURN_ARG(QVariant, rv)));
       
   346     QVERIFY(rv == QVariant(qreal(3)));
       
   347 
       
   348 
       
   349     QVERIFY(m_dbg->setMethodBody(obj.debugId(), "myMethodNoArgs", "return 7"));
       
   350     QTest::qWait(100);
       
   351 
       
   352     QVERIFY(QMetaObject::invokeMethod(root, "myMethodNoArgs", Qt::DirectConnection,
       
   353                                       Q_RETURN_ARG(QVariant, rv)));
       
   354     QVERIFY(rv == QVariant(qreal(7)));
       
   355     }
       
   356 
       
   357     // With args
       
   358     {
       
   359     QVariant rv;
       
   360     QVERIFY(QMetaObject::invokeMethod(root, "myMethod", Qt::DirectConnection,
       
   361                                       Q_RETURN_ARG(QVariant, rv), Q_ARG(QVariant, QVariant(19))));
       
   362     QVERIFY(rv == QVariant(qreal(28)));
       
   363 
       
   364     QVERIFY(m_dbg->setMethodBody(obj.debugId(), "myMethod", "return a + 7"));
       
   365     QTest::qWait(100);
       
   366 
       
   367     QVERIFY(QMetaObject::invokeMethod(root, "myMethod", Qt::DirectConnection,
       
   368                                       Q_RETURN_ARG(QVariant, rv), Q_ARG(QVariant, QVariant(19))));
       
   369     QVERIFY(rv == QVariant(qreal(26)));
       
   370     }
   323 }
   371 }
   324 
   372 
   325 void tst_QDeclarativeDebug::watch_property()
   373 void tst_QDeclarativeDebug::watch_property()
   326 {
   374 {
   327     QDeclarativeDebugObjectReference obj = findRootObject();
   375     QDeclarativeDebugObjectReference obj = findRootObject();
   579     QDeclarativeContext *actualContext = m_engine->rootContext();
   627     QDeclarativeContext *actualContext = m_engine->rootContext();
   580     QDeclarativeDebugContextReference context = q_context->rootContext();
   628     QDeclarativeDebugContextReference context = q_context->rootContext();
   581     QCOMPARE(context.debugId(), QDeclarativeDebugService::idForObject(actualContext));
   629     QCOMPARE(context.debugId(), QDeclarativeDebugService::idForObject(actualContext));
   582     QCOMPARE(context.name(), actualContext->objectName());
   630     QCOMPARE(context.name(), actualContext->objectName());
   583 
   631 
   584     QCOMPARE(context.objects().count(), 2); // 2 qml component objects created for context in main()
   632     QCOMPARE(context.objects().count(), 3); // 3 qml component objects created for context in main()
   585 
   633 
   586     // root context query sends only root object data - it doesn't fill in
   634     // root context query sends only root object data - it doesn't fill in
   587     // the children or property info
   635     // the children or property info
   588     QCOMPARE(context.objects()[0].properties().count(), 0);
   636     QCOMPARE(context.objects()[0].properties().count(), 0);
   589     QCOMPARE(context.objects()[0].children().count(), 0);
   637     QCOMPARE(context.objects()[0].children().count(), 0);
   692     
   740     
   693     q_expr = m_dbg->queryExpressionResult(objectId, expr, this);
   741     q_expr = m_dbg->queryExpressionResult(objectId, expr, this);
   694     delete q_expr;
   742     delete q_expr;
   695 
   743 
   696     q_expr = m_dbg->queryExpressionResult(objectId, expr, this);
   744     q_expr = m_dbg->queryExpressionResult(objectId, expr, this);
   697     QCOMPARE(q_expr->expression(), expr);
   745     QCOMPARE(q_expr->expression().toString(), expr);
   698     waitForQuery(q_expr);
   746     waitForQuery(q_expr);
   699 
   747 
   700     QCOMPARE(q_expr->result(), result);
   748     QCOMPARE(q_expr->result(), result);
   701 
   749 
   702     delete q_engines;
   750     delete q_engines;