📄 tst_qwebframe.cpp
字号:
// initial value (set in MyQObject constructor) { QString type; QVariant ret = evalJSV("myObject.intProperty", type); QCOMPARE(type, sNumber); QCOMPARE(ret.type(), QVariant::Double); QCOMPARE(ret.toInt(), 123); } QCOMPARE(evalJS("myObject.intProperty === 123.0"), sTrue); { QString type; QVariant ret = evalJSV("myObject.variantProperty", type); QCOMPARE(type, sString); QCOMPARE(ret.type(), QVariant::String); QCOMPARE(ret.toString(), QLatin1String("foo")); } QCOMPARE(evalJS("myObject.variantProperty == 'foo'"), sTrue); { QString type; QVariant ret = evalJSV("myObject.stringProperty", type); QCOMPARE(type, sString); QCOMPARE(ret.type(), QVariant::String); QCOMPARE(ret.toString(), QLatin1String("bar")); } QCOMPARE(evalJS("myObject.stringProperty === 'bar'"), sTrue); { QString type; QVariant ret = evalJSV("myObject.variantListProperty", type); QCOMPARE(type, sArray); QCOMPARE(ret.type(), QVariant::List); QVariantList vl = ret.value<QVariantList>(); QCOMPARE(vl.size(), 2); QCOMPARE(vl.at(0).toInt(), 123); QCOMPARE(vl.at(1).toString(), QLatin1String("foo")); } QCOMPARE(evalJS("myObject.variantListProperty.length === 2"), sTrue); QCOMPARE(evalJS("myObject.variantListProperty[0] === 123"), sTrue); QCOMPARE(evalJS("myObject.variantListProperty[1] === 'foo'"), sTrue); { QString type; QVariant ret = evalJSV("myObject.stringListProperty", type); QCOMPARE(type, sArray); QCOMPARE(ret.type(), QVariant::List); QVariantList vl = ret.value<QVariantList>(); QCOMPARE(vl.size(), 2); QCOMPARE(vl.at(0).toString(), QLatin1String("zig")); QCOMPARE(vl.at(1).toString(), QLatin1String("zag")); } QCOMPARE(evalJS("myObject.stringListProperty.length === 2"), sTrue); QCOMPARE(evalJS("typeof myObject.stringListProperty[0]"), sString); QCOMPARE(evalJS("myObject.stringListProperty[0]"), QLatin1String("zig")); QCOMPARE(evalJS("typeof myObject.stringListProperty[1]"), sString); QCOMPARE(evalJS("myObject.stringListProperty[1]"), QLatin1String("zag")); // property change in C++ should be reflected in script m_myObject->setIntProperty(456); QCOMPARE(evalJS("myObject.intProperty == 456"), sTrue); m_myObject->setIntProperty(789); QCOMPARE(evalJS("myObject.intProperty == 789"), sTrue); m_myObject->setVariantProperty(QLatin1String("bar")); QCOMPARE(evalJS("myObject.variantProperty === 'bar'"), sTrue); m_myObject->setVariantProperty(42); QCOMPARE(evalJS("myObject.variantProperty === 42"), sTrue); m_myObject->setVariantProperty(qVariantFromValue(QBrush()));//XFAIL// QCOMPARE(evalJS("typeof myObject.variantProperty"), sVariant); m_myObject->setStringProperty(QLatin1String("baz")); QCOMPARE(evalJS("myObject.stringProperty === 'baz'"), sTrue); m_myObject->setStringProperty(QLatin1String("zab")); QCOMPARE(evalJS("myObject.stringProperty === 'zab'"), sTrue); // property change in script should be reflected in C++ QCOMPARE(evalJS("myObject.intProperty = 123"), QLatin1String("123")); QCOMPARE(evalJS("myObject.intProperty == 123"), sTrue); QCOMPARE(m_myObject->intProperty(), 123); QCOMPARE(evalJS("myObject.intProperty = 'ciao!';" "myObject.intProperty == 0"), sTrue); QCOMPARE(m_myObject->intProperty(), 0); QCOMPARE(evalJS("myObject.intProperty = '123';" "myObject.intProperty == 123"), sTrue); QCOMPARE(m_myObject->intProperty(), 123); QCOMPARE(evalJS("myObject.stringProperty = 'ciao'"), QLatin1String("ciao")); QCOMPARE(evalJS("myObject.stringProperty"), QLatin1String("ciao")); QCOMPARE(m_myObject->stringProperty(), QLatin1String("ciao")); QCOMPARE(evalJS("myObject.stringProperty = 123;" "myObject.stringProperty"), QLatin1String("123")); QCOMPARE(m_myObject->stringProperty(), QLatin1String("123")); QCOMPARE(evalJS("myObject.stringProperty = null"), QString()); QCOMPARE(evalJS("myObject.stringProperty"), QString()); QCOMPARE(m_myObject->stringProperty(), QString()); QCOMPARE(evalJS("myObject.stringProperty = undefined"), sUndefined); QCOMPARE(evalJS("myObject.stringProperty"), QString()); QCOMPARE(m_myObject->stringProperty(), QString()); QCOMPARE(evalJS("myObject.variantProperty = new Number(1234);" "myObject.variantProperty").toDouble(), 1234.0); QCOMPARE(m_myObject->variantProperty().toDouble(), 1234.0); QCOMPARE(evalJS("myObject.variantProperty = new Boolean(1234);" "myObject.variantProperty"), sTrue); QCOMPARE(m_myObject->variantProperty().toBool(), true); QCOMPARE(evalJS("myObject.variantProperty = null;" "myObject.variantProperty.valueOf()"), sUndefined); QCOMPARE(m_myObject->variantProperty(), QVariant()); QCOMPARE(evalJS("myObject.variantProperty = undefined;" "myObject.variantProperty.valueOf()"), sUndefined); QCOMPARE(m_myObject->variantProperty(), QVariant()); QCOMPARE(evalJS("myObject.variantProperty = 'foo';" "myObject.variantProperty.valueOf()"), QLatin1String("foo")); QCOMPARE(m_myObject->variantProperty(), QVariant(QLatin1String("foo"))); QCOMPARE(evalJS("myObject.variantProperty = 42;" "myObject.variantProperty").toDouble(), 42.0); QCOMPARE(m_myObject->variantProperty().toDouble(), 42.0); QCOMPARE(evalJS("myObject.variantListProperty = [1, 'two', true];" "myObject.variantListProperty.length == 3"), sTrue); QCOMPARE(evalJS("myObject.variantListProperty[0] === 1"), sTrue); QCOMPARE(evalJS("myObject.variantListProperty[1]"), QLatin1String("two")); QCOMPARE(evalJS("myObject.variantListProperty[2] === true"), sTrue); QCOMPARE(evalJS("myObject.stringListProperty = [1, 'two', true];" "myObject.stringListProperty.length == 3"), sTrue); QCOMPARE(evalJS("typeof myObject.stringListProperty[0]"), sString); QCOMPARE(evalJS("myObject.stringListProperty[0] == '1'"), sTrue); QCOMPARE(evalJS("typeof myObject.stringListProperty[1]"), sString); QCOMPARE(evalJS("myObject.stringListProperty[1]"), QLatin1String("two")); QCOMPARE(evalJS("typeof myObject.stringListProperty[2]"), sString); QCOMPARE(evalJS("myObject.stringListProperty[2]"), QLatin1String("true")); // try to delete QCOMPARE(evalJS("delete myObject.intProperty"), sFalse); QCOMPARE(evalJS("myObject.intProperty == 123"), sTrue); QCOMPARE(evalJS("delete myObject.variantProperty"), sFalse); QCOMPARE(evalJS("myObject.variantProperty").toDouble(), 42.0); // custom property QCOMPARE(evalJS("myObject.customProperty"), sUndefined); QCOMPARE(evalJS("myObject.customProperty = 123;" "myObject.customProperty == 123"), sTrue); QVariant v = m_page->mainFrame()->evaluateJavaScript("myObject.customProperty"); QCOMPARE(v.type(), QVariant::Double); QCOMPARE(v.toInt(), 123); // non-scriptable property QCOMPARE(m_myObject->hiddenProperty(), 456.0); QCOMPARE(evalJS("myObject.hiddenProperty"), sUndefined); QCOMPARE(evalJS("myObject.hiddenProperty = 123;" "myObject.hiddenProperty == 123"), sTrue); QCOMPARE(m_myObject->hiddenProperty(), 456.0); // write-only property QCOMPARE(m_myObject->writeOnlyProperty(), 789); QCOMPARE(evalJS("typeof myObject.writeOnlyProperty"), sUndefined); QCOMPARE(evalJS("myObject.writeOnlyProperty = 123;" "typeof myObject.writeOnlyProperty"), sUndefined); QCOMPARE(m_myObject->writeOnlyProperty(), 123); // read-only property QCOMPARE(m_myObject->readOnlyProperty(), 987); QCOMPARE(evalJS("myObject.readOnlyProperty == 987"), sTrue); QCOMPARE(evalJS("myObject.readOnlyProperty = 654;" "myObject.readOnlyProperty == 987"), sTrue); QCOMPARE(m_myObject->readOnlyProperty(), 987);}void tst_QWebFrame::getSetDynamicProperty(){ // initially the object does not have the property // In WebKit, RuntimeObjects do not inherit Object, so don't have hasOwnProperty //QCOMPARE(evalJS("myObject.hasOwnProperty('dynamicProperty')"), sFalse); QCOMPARE(evalJS("typeof myObject.dynamicProperty"), sUndefined); // add a dynamic property in C++ QCOMPARE(m_myObject->setProperty("dynamicProperty", 123), false); //QCOMPARE(evalJS("myObject.hasOwnProperty('dynamicProperty')"), sTrue); QCOMPARE(evalJS("typeof myObject.dynamicProperty != 'undefined'"), sTrue); QCOMPARE(evalJS("myObject.dynamicProperty == 123"), sTrue); // property change in script should be reflected in C++ QCOMPARE(evalJS("myObject.dynamicProperty = 'foo';" "myObject.dynamicProperty"), QLatin1String("foo")); QCOMPARE(m_myObject->property("dynamicProperty").toString(), QLatin1String("foo")); // delete the property (XFAIL - can't delete properties) QEXPECT_FAIL("", "can't delete properties", Continue); QCOMPARE(evalJS("delete myObject.dynamicProperty"), sTrue); /* QCOMPARE(m_myObject->property("dynamicProperty").isValid(), false); QCOMPARE(evalJS("typeof myObject.dynamicProperty"), sUndefined); // QCOMPARE(evalJS("myObject.hasOwnProperty('dynamicProperty')"), sFalse); QCOMPARE(evalJS("typeof myObject.dynamicProperty"), sUndefined); */}void tst_QWebFrame::getSetChildren(){ // initially the object does not have the child // (again, no hasOwnProperty) //QCOMPARE(evalJS("myObject.hasOwnProperty('child')"), sFalse); QCOMPARE(evalJS("typeof myObject.child"), sUndefined); // add a child MyQObject* child = new MyQObject(m_myObject); child->setObjectName("child");// QCOMPARE(evalJS("myObject.hasOwnProperty('child')"), sTrue); QCOMPARE(evalJS("typeof myObject.child != 'undefined'"), sTrue); // add a grandchild MyQObject* grandChild = new MyQObject(child); grandChild->setObjectName("grandChild");// QCOMPARE(evalJS("myObject.child.hasOwnProperty('grandChild')"), sTrue); QCOMPARE(evalJS("typeof myObject.child.grandChild != 'undefined'"), sTrue); // delete grandchild delete grandChild;// QCOMPARE(evalJS("myObject.child.hasOwnProperty('grandChild')"), sFalse); QCOMPARE(evalJS("typeof myObject.child.grandChild == 'undefined'"), sTrue); // delete child delete child;// QCOMPARE(evalJS("myObject.hasOwnProperty('child')"), sFalse); QCOMPARE(evalJS("typeof myObject.child == 'undefined'"), sTrue);}Q_DECLARE_METATYPE(QVector<int>)Q_DECLARE_METATYPE(QVector<double>)Q_DECLARE_METATYPE(QVector<QString>)void tst_QWebFrame::callQtInvokable(){ qRegisterMetaType<QObjectList>(); m_myObject->resetQtFunctionInvoked(); QCOMPARE(evalJS("typeof myObject.myInvokable()"), sUndefined); QCOMPARE(m_myObject->qtFunctionInvoked(), 0); QCOMPARE(m_myObject->qtFunctionActuals(), QVariantList()); // extra arguments should silently be ignored m_myObject->resetQtFunctionInvoked(); QCOMPARE(evalJS("typeof myObject.myInvokable(10, 20, 30)"), sUndefined); QCOMPARE(m_myObject->qtFunctionInvoked(), 0); QCOMPARE(m_myObject->qtFunctionActuals(), QVariantList()); m_myObject->resetQtFunctionInvoked(); QCOMPARE(evalJS("typeof myObject.myInvokableWithIntArg(123)"), sUndefined); QCOMPARE(m_myObject->qtFunctionInvoked(), 1); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QCOMPARE(m_myObject->qtFunctionActuals().at(0).toInt(), 123); m_myObject->resetQtFunctionInvoked(); QCOMPARE(evalJS("typeof myObject.myInvokableWithIntArg('123')"), sUndefined); QCOMPARE(m_myObject->qtFunctionInvoked(), 1); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QCOMPARE(m_myObject->qtFunctionActuals().at(0).toInt(), 123); m_myObject->resetQtFunctionInvoked(); QCOMPARE(evalJS("typeof myObject.myInvokableWithLonglongArg(123)"), sUndefined); QCOMPARE(m_myObject->qtFunctionInvoked(), 2); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QCOMPARE(m_myObject->qtFunctionActuals().at(0).toLongLong(), qlonglong(123)); m_myObject->resetQtFunctionInvoked(); QCOMPARE(evalJS("typeof myObject.myInvokableWithFloatArg(123.5)"), sUndefined); QCOMPARE(m_myObject->qtFunctionInvoked(), 3); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QCOMPARE(m_myObject->qtFunctionActuals().at(0).toDouble(), 123.5); m_myObject->resetQtFunctionInvoked(); QCOMPARE(evalJS("typeof myObject.myInvokableWithDoubleArg(123.5)"), sUndefined); QCOMPARE(m_myObject->qtFunctionInvoked(), 4); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QCOMPARE(m_myObject->qtFunctionActuals().at(0).toDouble(), 123.5); m_myObject->resetQtFunctionInvoked(); QCOMPARE(evalJS("typeof myObject.myInvokableWithDoubleArg(new Number(1234.5))"), sUndefined); QCOMPARE(m_myObject->qtFunctionInvoked(), 4); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QCOMPARE(m_myObject->qtFunctionActuals().at(0).toDouble(), 1234.5); m_myObject->resetQtFunctionInvoked(); QCOMPARE(evalJS("typeof myObject.myInvokableWithBoolArg(new Boolean(true))"), sUndefined); QCOMPARE(m_myObject->qtFunctionInvoked(), 52); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QCOMPARE(m_myObject->qtFunctionActuals().at(0).toBool(), true); m_myObject->resetQtFunctionInvoked(); QCOMPARE(evalJS("typeof myObject.myInvokableWithStringArg('ciao')"), sUndefined); QCOMPARE(m_myObject->qtFunctionInvoked(), 5); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QCOMPARE(m_myObject->qtFunctionActuals().at(0).toString(), QLatin1String("ciao")); m_myObject->resetQtFunctionInvoked(); QCOMPARE(evalJS("typeof myObject.myInvokableWithStringArg(123)"), sUndefined); QCOMPARE(m_myObject->qtFunctionInvoked(), 5); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QCOMPARE(m_myObject->qtFunctionActuals().at(0).toString(), QLatin1String("123")); m_myObject->resetQtFunctionInvoked(); QCOMPARE(evalJS("typeof myObject.myInvokableWithStringArg(null)"), sUndefined); QCOMPARE(m_myObject->qtFunctionInvoked(), 5); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QCOMPARE(m_myObject->qtFunctionActuals().at(0).toString(), QString()); QVERIFY(m_myObject->qtFunctionActuals().at(0).toString().isEmpty()); m_myObject->resetQtFunctionInvoked(); QCOMPARE(evalJS("typeof myObject.myInvokableWithStringArg(undefined)"), sUndefined); QCOMPARE(m_myObject->qtFunctionInvoked(), 5); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); QCOMPARE(m_myObject->qtFunctionActuals().at(0).toString(), QString()); QVERIFY(m_myObject->qtFunctionActuals().at(0).toString().isEmpty()); m_myObject->resetQtFunctionInvoked(); QCOMPARE(evalJS("typeof myObject.myInvokableWithIntArgs(123, 456)"), sUndefined); QCOMPARE(m_myObject->qtFunctionInvoked(), 6); 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("myObject.myInvokableReturningInt()"), QLatin1String("123")); QCOMPARE(m_myObject->qtFunctionInvoked(), 7); QCOMPARE(m_myObject->qtFunctionActuals(), QVariantList()); m_myObject->resetQtFunctionInvoked(); QCOMPARE(evalJS("myObject.myInvokableReturningLongLong()"), QLatin1String("456")); QCOMPARE(m_myObject->qtFunctionInvoked(), 39); QCOMPARE(m_myObject->qtFunctionActuals(), QVariantList()); m_myObject->resetQtFunctionInvoked(); QCOMPARE(evalJS("myObject.myInvokableReturningString()"), QLatin1String("ciao")); QCOMPARE(m_myObject->qtFunctionInvoked(), 8); QCOMPARE(m_myObject->qtFunctionActuals(), QVariantList()); m_myObject->resetQtFunctionInvoked();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -