📄 tst_qwebframe.cpp
字号:
QCOMPARE(evalJS("typeof myObject.myInvokableWithIntArg(123, 456)"), sUndefined); QCOMPARE(m_myObject->qtFunctionInvoked(), 9); QCOMPARE(m_myObject->qtFunctionActuals().size(), 2); QCOMPARE(m_myObject->qtFunctionActuals().at(0).toInt(), 123); QCOMPARE(m_myObject->qtFunctionActuals().at(1).toInt(), 456); m_myObject->resetQtFunctionInvoked(); QCOMPARE(evalJS("typeof myObject.myInvokableWithVoidStarArg(null)"), sUndefined); QCOMPARE(m_myObject->qtFunctionInvoked(), 44); m_myObject->resetQtFunctionInvoked(); { QString type; QString ret = evalJS("myObject.myInvokableWithVoidStarArg(123)", type); QCOMPARE(type, sError); QCOMPARE(ret, QLatin1String("TypeError: incompatible type of argument(s) in call to myInvokableWithVoidStarArg(); candidates were\n myInvokableWithVoidStarArg(void*)")); QCOMPARE(m_myObject->qtFunctionInvoked(), -1); } m_myObject->resetQtFunctionInvoked(); { QString type; QString ret = evalJS("myObject.myInvokableWithAmbiguousArg(123)", type); QCOMPARE(type, sError); QCOMPARE(ret, QLatin1String("TypeError: ambiguous call of overloaded function myInvokableWithAmbiguousArg(); candidates were\n myInvokableWithAmbiguousArg(int)\n myInvokableWithAmbiguousArg(uint)")); } m_myObject->resetQtFunctionInvoked(); { QString type; QString ret = evalJS("myObject.myInvokableWithDefaultArgs(123, 'hello')", type); QCOMPARE(type, sUndefined); QCOMPARE(m_myObject->qtFunctionInvoked(), 47); QCOMPARE(m_myObject->qtFunctionActuals().size(), 2); QCOMPARE(m_myObject->qtFunctionActuals().at(0).toInt(), 123); QCOMPARE(m_myObject->qtFunctionActuals().at(1).toString(), QLatin1String("hello")); } m_myObject->resetQtFunctionInvoked(); { QString type; QString ret = evalJS("myObject.myInvokableWithDefaultArgs(456)", type); QCOMPARE(type, sUndefined); QCOMPARE(m_myObject->qtFunctionInvoked(), 47); QCOMPARE(m_myObject->qtFunctionActuals().size(), 2); QCOMPARE(m_myObject->qtFunctionActuals().at(0).toInt(), 456); QCOMPARE(m_myObject->qtFunctionActuals().at(1).toString(), QString()); } // calling function that returns (const)ref m_myObject->resetQtFunctionInvoked(); { QString type; QString ret = evalJS("typeof myObject.myInvokableReturningRef()"); QCOMPARE(ret, sUndefined); //QVERIFY(!m_engine->hasUncaughtException()); QCOMPARE(m_myObject->qtFunctionInvoked(), 48); } m_myObject->resetQtFunctionInvoked(); { QString type; QString ret = evalJS("typeof myObject.myInvokableReturningConstRef()"); QCOMPARE(ret, sUndefined); //QVERIFY(!m_engine->hasUncaughtException()); QCOMPARE(m_myObject->qtFunctionInvoked(), 49); } m_myObject->resetQtFunctionInvoked(); { QString type; QVariant ret = evalJSV("myObject.myInvokableReturningQObjectStar()", type); QCOMPARE(m_myObject->qtFunctionInvoked(), 13); QCOMPARE(m_myObject->qtFunctionActuals().size(), 0); QCOMPARE(type, sObject); QCOMPARE(ret.userType(), int(QMetaType::QObjectStar)); } m_myObject->resetQtFunctionInvoked(); { QString type; QVariant ret = evalJSV("myObject.myInvokableWithQObjectListArg([myObject])", type); QCOMPARE(m_myObject->qtFunctionInvoked(), 14); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QCOMPARE(type, sArray); QCOMPARE(ret.userType(), int(QVariant::List)); // All lists get downgraded to QVariantList QVariantList vl = qvariant_cast<QVariantList>(ret); QCOMPARE(vl.count(), 1); } m_myObject->resetQtFunctionInvoked(); { QString type; m_myObject->setVariantProperty(QVariant(123)); QVariant ret = evalJSV("myObject.myInvokableWithVariantArg(myObject.variantProperty)", type); QCOMPARE(type, sNumber); QCOMPARE(m_myObject->qtFunctionInvoked(), 15); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QCOMPARE(m_myObject->qtFunctionActuals().at(0), m_myObject->variantProperty()); QCOMPARE(ret.userType(), int(QMetaType::Double)); // all JS numbers are doubles, even though this started as an int QCOMPARE(ret.toInt(),123); } m_myObject->resetQtFunctionInvoked(); { QString type; QVariant ret = evalJSV("myObject.myInvokableWithVariantArg(null)", type); QCOMPARE(type, sObject); QCOMPARE(m_myObject->qtFunctionInvoked(), 15); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QCOMPARE(m_myObject->qtFunctionActuals().at(0), QVariant()); QVERIFY(!m_myObject->qtFunctionActuals().at(0).isValid()); } m_myObject->resetQtFunctionInvoked(); { QString type; QVariant ret = evalJSV("myObject.myInvokableWithVariantArg(undefined)", type); QCOMPARE(type, sObject); QCOMPARE(m_myObject->qtFunctionInvoked(), 15); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QCOMPARE(m_myObject->qtFunctionActuals().at(0), QVariant()); QVERIFY(!m_myObject->qtFunctionActuals().at(0).isValid()); } /* XFAIL - variant support m_myObject->resetQtFunctionInvoked(); { m_myObject->setVariantProperty(qVariantFromValue(QBrush())); QVariant ret = evalJS("myObject.myInvokableWithVariantArg(myObject.variantProperty)"); QVERIFY(ret.isVariant()); QCOMPARE(m_myObject->qtFunctionInvoked(), 15); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QCOMPARE(ret.toVariant(), m_myObject->qtFunctionActuals().at(0)); QCOMPARE(ret.toVariant(), m_myObject->variantProperty()); } */ m_myObject->resetQtFunctionInvoked(); { QString type; QVariant ret = evalJSV("myObject.myInvokableWithVariantArg(123)", type); QCOMPARE(type, sNumber); QCOMPARE(m_myObject->qtFunctionInvoked(), 15); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QCOMPARE(m_myObject->qtFunctionActuals().at(0), QVariant(123)); QCOMPARE(ret.userType(), int(QMetaType::Double)); QCOMPARE(ret.toInt(),123); } m_myObject->resetQtFunctionInvoked(); { QString type; QVariant ret = evalJSV("myObject.myInvokableWithVariantMapArg({ a:123, b:'ciao' })", type); QCOMPARE(m_myObject->qtFunctionInvoked(), 16); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QVariant v = m_myObject->qtFunctionActuals().at(0); QCOMPARE(v.userType(), int(QMetaType::QVariantMap)); QVariantMap vmap = qvariant_cast<QVariantMap>(v); QCOMPARE(vmap.keys().size(), 2); QCOMPARE(vmap.keys().at(0), QLatin1String("a")); QCOMPARE(vmap.value("a"), QVariant(123)); QCOMPARE(vmap.keys().at(1), QLatin1String("b")); QCOMPARE(vmap.value("b"), QVariant("ciao")); QCOMPARE(type, sObject); QCOMPARE(ret.userType(), int(QMetaType::QVariantMap)); vmap = qvariant_cast<QVariantMap>(ret); QCOMPARE(vmap.keys().size(), 2); QCOMPARE(vmap.keys().at(0), QLatin1String("a")); QCOMPARE(vmap.value("a"), QVariant(123)); QCOMPARE(vmap.keys().at(1), QLatin1String("b")); QCOMPARE(vmap.value("b"), QVariant("ciao")); } m_myObject->resetQtFunctionInvoked(); { QString type; QVariant ret = evalJSV("myObject.myInvokableWithListOfIntArg([1, 5])", type); QCOMPARE(m_myObject->qtFunctionInvoked(), 17); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QVariant v = m_myObject->qtFunctionActuals().at(0); QCOMPARE(v.userType(), qMetaTypeId<QList<int> >()); QList<int> ilst = qvariant_cast<QList<int> >(v); QCOMPARE(ilst.size(), 2); QCOMPARE(ilst.at(0), 1); QCOMPARE(ilst.at(1), 5); QCOMPARE(type, sArray); QCOMPARE(ret.userType(), int(QMetaType::QVariantList)); // ints get converted to doubles, so this is a qvariantlist QVariantList vlst = qvariant_cast<QVariantList>(ret); QCOMPARE(vlst.size(), 2); QCOMPARE(vlst.at(0).toInt(), 1); QCOMPARE(vlst.at(1).toInt(), 5); } m_myObject->resetQtFunctionInvoked(); { QString type; QVariant ret = evalJSV("myObject.myInvokableWithQObjectStarArg(myObject)", type); QCOMPARE(m_myObject->qtFunctionInvoked(), 18); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QVariant v = m_myObject->qtFunctionActuals().at(0); QCOMPARE(v.userType(), int(QMetaType::QObjectStar)); QCOMPARE(qvariant_cast<QObject*>(v), (QObject*)m_myObject); QCOMPARE(ret.userType(), int(QMetaType::QObjectStar)); QCOMPARE(qvariant_cast<QObject*>(ret), (QObject*)m_myObject); QCOMPARE(type, sObject); } m_myObject->resetQtFunctionInvoked(); { // no implicit conversion from integer to QObject* QString type; evalJS("myObject.myInvokableWithQObjectStarArg(123)", type); QCOMPARE(type, sError); } /* m_myObject->resetQtFunctionInvoked(); { QString fun = evalJS("myObject.myInvokableWithQBrushArg"); Q_ASSERT(fun.isFunction()); QColor color(10, 20, 30, 40); // QColor should be converted to a QBrush QVariant ret = fun.call(QString(), QStringList() << qScriptValueFromValue(m_engine, color)); QCOMPARE(m_myObject->qtFunctionInvoked(), 19); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QVariant v = m_myObject->qtFunctionActuals().at(0); QCOMPARE(v.userType(), int(QMetaType::QBrush)); QCOMPARE(qvariant_cast<QColor>(v), color); QCOMPARE(qscriptvalue_cast<QColor>(ret), color); } */ // private slots should not be part of the QObject binding QCOMPARE(evalJS("typeof myObject.myPrivateSlot"), sUndefined); // protected slots should be fine m_myObject->resetQtFunctionInvoked(); evalJS("myObject.myProtectedSlot()"); QCOMPARE(m_myObject->qtFunctionInvoked(), 36); // call with too few arguments { QString type; QString ret = evalJS("myObject.myInvokableWithIntArg()", type); QCOMPARE(type, sError); QCOMPARE(ret, QLatin1String("SyntaxError: too few arguments in call to myInvokableWithIntArg(); candidates are\n myInvokableWithIntArg(int,int)\n myInvokableWithIntArg(int)")); } // call function where not all types have been registered m_myObject->resetQtFunctionInvoked(); { QString type; QString ret = evalJS("myObject.myInvokableWithBrushStyleArg(0)", type); QCOMPARE(type, sError); QCOMPARE(ret, QLatin1String("TypeError: cannot call myInvokableWithBrushStyleArg(): unknown type `Qt::BrushStyle'")); QCOMPARE(m_myObject->qtFunctionInvoked(), -1); } // call function with incompatible argument type m_myObject->resetQtFunctionInvoked(); { QString type; QString ret = evalJS("myObject.myInvokableWithQBrushArg(null)", type); QCOMPARE(type, sError); QCOMPARE(ret, QLatin1String("TypeError: incompatible type of argument(s) in call to myInvokableWithQBrushArg(); candidates were\n myInvokableWithQBrushArg(QBrush)")); QCOMPARE(m_myObject->qtFunctionInvoked(), -1); }}void tst_QWebFrame::connectAndDisconnect(){ // connect(function) QCOMPARE(evalJS("typeof myObject.mySignal"), sFunction); QCOMPARE(evalJS("typeof myObject.mySignal.connect"), sFunction); QCOMPARE(evalJS("typeof myObject.mySignal.disconnect"), sFunction); { QString type; evalJS("myObject.mySignal.connect(123)", type); QCOMPARE(type, sError); } evalJS("myHandler = function() { window.gotSignal = true; window.signalArgs = arguments; window.slotThisObject = this; window.signalSender = __qt_sender__; }"); QCOMPARE(evalJS("myObject.mySignal.connect(myHandler)"), sUndefined); evalJS("gotSignal = false"); evalJS("myObject.mySignal()"); QCOMPARE(evalJS("gotSignal"), sTrue); QCOMPARE(evalJS("signalArgs.length == 0"), sTrue); QCOMPARE(evalJS("signalSender"),evalJS("myObject")); QCOMPARE(evalJS("slotThisObject == window"), sTrue); evalJS("gotSignal = false"); m_myObject->emitMySignal(); QCOMPARE(evalJS("gotSignal"), sTrue); QCOMPARE(evalJS("signalArgs.length == 0"), sTrue); QCOMPARE(evalJS("myObject.mySignalWithIntArg.connect(myHandler)"), sUndefined); evalJS("gotSignal = false"); m_myObject->emitMySignalWithIntArg(123); QCOMPARE(evalJS("gotSignal"), sTrue); QCOMPARE(evalJS("signalArgs.length == 1"), sTrue); QCOMPARE(evalJS("signalArgs[0] == 123.0"), sTrue); QCOMPARE(evalJS("myObject.mySignal.disconnect(myHandler)"), sUndefined); { QString type; evalJS("myObject.mySignal.disconnect(myHandler)", type); QCOMPARE(type, sError); } evalJS("gotSignal = false"); QCOMPARE(evalJS("myObject.mySignal2.connect(myHandler)"), sUndefined); m_myObject->emitMySignal2(true); QCOMPARE(evalJS("gotSignal"), sTrue); QCOMPARE(evalJS("signalArgs.length == 1"), sTrue); QCOMPARE(evalJS("signalArgs[0]"), sTrue); QCOMPARE(evalJS("myObject.mySignal2.disconnect(myHandler)"), sUndefined); QCOMPARE(evalJS("typeof myObject['mySignal2()']"), sFunction); QCOMPARE(evalJS("typeof myObject['mySignal2()'].connect"), sFunction); QCOMPARE(evalJS("typeof myObject['mySignal2()'].disconnect"), sFunction); QCOMPARE(evalJS("myObject['mySignal2()'].connect(myHandler)"), sUndefined); evalJS("gotSignal = false"); m_myObject->emitMySignal2(); QCOMPARE(evalJS("gotSignal"), sTrue); QCOMPARE(evalJS("myObject['mySignal2()'].disconnect(myHandler)"), sUndefined); // connect(object, function) evalJS("otherObject = { name:'foo' }"); QCOMPARE(evalJS("myObject.mySignal.connect(otherObject, myHandler)"), sUndefined);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -