summaryrefslogtreecommitdiffstats
path: root/tests/auto/qscriptqobject
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/qscriptqobject')
-rw-r--r--tests/auto/qscriptqobject/qscriptqobject.pro2
-rw-r--r--tests/auto/qscriptqobject/tst_qscriptqobject.cpp155
2 files changed, 153 insertions, 4 deletions
diff --git a/tests/auto/qscriptqobject/qscriptqobject.pro b/tests/auto/qscriptqobject/qscriptqobject.pro
index e4e2b56..be3a980 100644
--- a/tests/auto/qscriptqobject/qscriptqobject.pro
+++ b/tests/auto/qscriptqobject/qscriptqobject.pro
@@ -1,5 +1,5 @@
load(qttest_p4)
-QT += script
+QT = core gui script
SOURCES += tst_qscriptqobject.cpp
diff --git a/tests/auto/qscriptqobject/tst_qscriptqobject.cpp b/tests/auto/qscriptqobject/tst_qscriptqobject.cpp
index 73400b2..ee914ab 100644
--- a/tests/auto/qscriptqobject/tst_qscriptqobject.cpp
+++ b/tests/auto/qscriptqobject/tst_qscriptqobject.cpp
@@ -107,6 +107,7 @@ class MyQObject : public QObject
Q_PROPERTY(int readOnlyProperty READ readOnlyProperty)
Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut)
Q_PROPERTY(CustomType propWithCustomType READ propWithCustomType WRITE setPropWithCustomType)
+ Q_PROPERTY(Policy enumProperty READ enumProperty WRITE setEnumProperty)
Q_ENUMS(Policy Strategy)
Q_FLAGS(Ability)
@@ -144,6 +145,7 @@ public:
m_hiddenValue(456.0),
m_writeOnlyValue(789),
m_readOnlyValue(987),
+ m_enumValue(BarPolicy),
m_qtFunctionInvoked(-1)
{ }
@@ -205,6 +207,11 @@ public:
void setPropWithCustomType(const CustomType &c)
{ m_customType = c; }
+ Policy enumProperty() const
+ { return m_enumValue; }
+ void setEnumProperty(Policy policy)
+ { m_enumValue = policy; }
+
int qtFunctionInvoked() const
{ return m_qtFunctionInvoked; }
@@ -255,8 +262,8 @@ public:
{ m_qtFunctionInvoked = 36; m_actuals << policy; }
Q_INVOKABLE Policy myInvokableReturningEnum()
{ m_qtFunctionInvoked = 37; return BazPolicy; }
- Q_INVOKABLE MyQObject::Policy myInvokableReturningQualifiedEnum()
- { m_qtFunctionInvoked = 38; return BazPolicy; }
+ Q_INVOKABLE MyQObject::Strategy myInvokableReturningQualifiedEnum()
+ { m_qtFunctionInvoked = 38; return BazStrategy; }
Q_INVOKABLE QVector<int> myInvokableReturningVectorOfInt()
{ m_qtFunctionInvoked = 11; return QVector<int>(); }
Q_INVOKABLE void myInvokableWithVectorOfIntArg(const QVector<int> &)
@@ -326,6 +333,10 @@ public:
{ emit mySignalWithDefaultArg(arg); }
void emitMySignalWithDefaultArg()
{ emit mySignalWithDefaultArg(); }
+ void emitMySignalWithVariantArg(const QVariant &arg)
+ { emit mySignalWithVariantArg(arg); }
+ void emitMySignalWithScriptEngineArg(QScriptEngine *arg)
+ { emit mySignalWithScriptEngineArg(arg); }
public Q_SLOTS:
void mySlot()
@@ -388,6 +399,8 @@ Q_SIGNALS:
void myOtherOverloadedSignal(const QString &arg);
void myOtherOverloadedSignal(int arg);
void mySignalWithDefaultArg(int arg = 123);
+ void mySignalWithVariantArg(const QVariant &arg);
+ void mySignalWithScriptEngineArg(QScriptEngine *arg);
protected:
void connectNotify(const char *signal) {
@@ -410,6 +423,7 @@ protected:
int m_readOnlyValue;
QKeySequence m_shortcut;
CustomType m_customType;
+ Policy m_enumValue;
int m_qtFunctionInvoked;
QVariantList m_actuals;
QByteArray m_connectedSignal;
@@ -417,6 +431,7 @@ protected:
};
Q_DECLARE_METATYPE(MyQObject*)
+Q_DECLARE_METATYPE(MyQObject::Policy)
class MyOtherQObject : public MyQObject
{
@@ -530,6 +545,24 @@ static QScriptValue getSetProperty(QScriptContext *ctx, QScriptEngine *)
return ctx->callee().property("value");
}
+static QScriptValue policyToScriptValue(QScriptEngine *engine, const MyQObject::Policy &policy)
+{
+ return qScriptValueFromValue(engine, policy);
+}
+
+static void policyFromScriptValue(const QScriptValue &value, MyQObject::Policy &policy)
+{
+ QString str = value.toString();
+ if (str == QLatin1String("red"))
+ policy = MyQObject::FooPolicy;
+ else if (str == QLatin1String("green"))
+ policy = MyQObject::BarPolicy;
+ else if (str == QLatin1String("blue"))
+ policy = MyQObject::BazPolicy;
+ else
+ policy = (MyQObject::Policy)-1;
+}
+
void tst_QScriptExtQObject::getSetStaticProperty()
{
QCOMPARE(m_engine->evaluate("myObject.noSuchProperty").isUndefined(), true);
@@ -572,6 +605,12 @@ void tst_QScriptExtQObject::getSetStaticProperty()
QVERIFY(!(mobj.propertyFlags("mySlot") & QScriptValue::Undeletable));
QVERIFY(!(mobj.propertyFlags("mySlot") & QScriptValue::SkipInEnumeration));
QVERIFY(mobj.propertyFlags("mySlot") & QScriptValue::QObjectMember);
+
+ // signature-based property
+ QVERIFY(!(mobj.propertyFlags("mySlot()") & QScriptValue::ReadOnly));
+ QVERIFY(!(mobj.propertyFlags("mySlot()") & QScriptValue::Undeletable));
+ QVERIFY(!(mobj.propertyFlags("mySlot()") & QScriptValue::SkipInEnumeration));
+ QVERIFY(mobj.propertyFlags("mySlot()") & QScriptValue::QObjectMember);
}
// property change in C++ should be reflected in script
@@ -703,6 +742,8 @@ void tst_QScriptExtQObject::getSetStaticProperty()
// test that we do value conversion if necessary when setting properties
{
QScriptValue br = m_engine->evaluate("myObject.brushProperty");
+ QVERIFY(br.isVariant());
+ QVERIFY(!br.strictlyEquals(m_engine->evaluate("myObject.brushProperty")));
QCOMPARE(qscriptvalue_cast<QBrush>(br), m_myObject->brushProperty());
QCOMPARE(qscriptvalue_cast<QColor>(br), m_myObject->brushProperty().color());
@@ -751,6 +792,31 @@ void tst_QScriptExtQObject::getSetStaticProperty()
QScriptValue::ReadOnly);
}
+ // enum property
+ QCOMPARE(m_myObject->enumProperty(), MyQObject::BarPolicy);
+ {
+ QScriptValue val = m_engine->evaluate("myObject.enumProperty");
+ QVERIFY(val.isNumber());
+ QCOMPARE(val.toInt32(), (int)MyQObject::BarPolicy);
+ }
+ m_engine->evaluate("myObject.enumProperty = 2");
+ QCOMPARE(m_myObject->enumProperty(), MyQObject::BazPolicy);
+ m_engine->evaluate("myObject.enumProperty = 'BarPolicy'");
+ QCOMPARE(m_myObject->enumProperty(), MyQObject::BarPolicy);
+ m_engine->evaluate("myObject.enumProperty = 'ScoobyDoo'");
+ // ### ouch! Shouldn't QMetaProperty::write() rather not change the value...?
+ QCOMPARE(m_myObject->enumProperty(), (MyQObject::Policy)-1);
+ // enum property with custom conversion
+ qScriptRegisterMetaType<MyQObject::Policy>(m_engine, policyToScriptValue, policyFromScriptValue);
+ m_engine->evaluate("myObject.enumProperty = 'red'");
+ QCOMPARE(m_myObject->enumProperty(), MyQObject::FooPolicy);
+ m_engine->evaluate("myObject.enumProperty = 'green'");
+ QCOMPARE(m_myObject->enumProperty(), MyQObject::BarPolicy);
+ m_engine->evaluate("myObject.enumProperty = 'blue'");
+ QCOMPARE(m_myObject->enumProperty(), MyQObject::BazPolicy);
+ m_engine->evaluate("myObject.enumProperty = 'nada'");
+ QCOMPARE(m_myObject->enumProperty(), (MyQObject::Policy)-1);
+
// auto-dereferencing of pointers
{
QBrush b = QColor(0xCA, 0xFE, 0xBA, 0xBE);
@@ -780,6 +846,17 @@ void tst_QScriptExtQObject::getSetStaticProperty()
mobj.setProperty("intProperty", m_engine->newFunction(getSetProperty),
QScriptValue::PropertyGetter | QScriptValue::PropertySetter);
}
+
+ // method properties are persistent
+ {
+ QScriptValue slot = m_engine->evaluate("myObject.mySlot");
+ QVERIFY(slot.isFunction());
+ QScriptValue sameSlot = m_engine->evaluate("myObject.mySlot");
+ QVERIFY(sameSlot.strictlyEquals(slot));
+ sameSlot = m_engine->evaluate("myObject['mySlot()']");
+ QEXPECT_FAIL("", "Signature-based method lookup creates new function wrapper object", Continue);
+ QVERIFY(sameSlot.strictlyEquals(slot));
+ }
}
void tst_QScriptExtQObject::getSetDynamicProperty()
@@ -829,6 +906,20 @@ void tst_QScriptExtQObject::getSetChildren()
QCOMPARE(m_engine->evaluate("myObject.hasOwnProperty('child')")
.strictlyEquals(QScriptValue(m_engine, true)), true);
+ QScriptValue mobj = m_engine->evaluate("myObject");
+ QVERIFY(mobj.propertyFlags("child") & QScriptValue::ReadOnly);
+ QVERIFY(mobj.propertyFlags("child") & QScriptValue::Undeletable);
+ QVERIFY(mobj.propertyFlags("child") & QScriptValue::SkipInEnumeration);
+ QVERIFY(!(mobj.propertyFlags("child") & QScriptValue::QObjectMember));
+
+ {
+ QScriptValue scriptChild = m_engine->evaluate("myObject.child");
+ QVERIFY(scriptChild.isQObject());
+ QCOMPARE(scriptChild.toQObject(), (QObject*)child);
+ QScriptValue sameChild = m_engine->evaluate("myObject.child");
+ QVERIFY(sameChild.strictlyEquals(scriptChild));
+ }
+
// add a grandchild
MyQObject *grandChild = new MyQObject(child);
grandChild->setObjectName("grandChild");
@@ -1501,6 +1592,29 @@ void tst_QScriptExtQObject::connectAndDisconnect()
m_myObject->emitMyOtherOverloadedSignal(123);
QVERIFY(!m_engine->evaluate("gotSignal").toBoolean());
+ // signal with QVariant arg: argument conversion should work
+ m_myObject->clearConnectedSignal();
+ QVERIFY(m_engine->evaluate("myObject.mySignalWithVariantArg.connect(myHandler)").isUndefined());
+ QCOMPARE(m_myObject->connectedSignal().constData(), SIGNAL(mySignalWithVariantArg(QVariant)));
+ m_engine->evaluate("gotSignal = false");
+ m_myObject->emitMySignalWithVariantArg(123);
+ QCOMPARE(m_engine->evaluate("gotSignal").toBoolean(), true);
+ QCOMPARE(m_engine->evaluate("signalArgs.length").toNumber(), 1.0);
+ QCOMPARE(m_engine->evaluate("signalArgs[0]").toNumber(), 123.0);
+ QVERIFY(m_engine->evaluate("myObject.mySignalWithVariantArg.disconnect(myHandler)").isUndefined());
+
+ // signal with argument type that's unknown to the meta-type system
+ m_myObject->clearConnectedSignal();
+ QVERIFY(m_engine->evaluate("myObject.mySignalWithScriptEngineArg.connect(myHandler)").isUndefined());
+ QCOMPARE(m_myObject->connectedSignal().constData(), SIGNAL(mySignalWithScriptEngineArg(QScriptEngine*)));
+ m_engine->evaluate("gotSignal = false");
+ QTest::ignoreMessage(QtWarningMsg, "QScriptEngine: Unable to handle unregistered datatype 'QScriptEngine*' when invoking handler of signal MyQObject::mySignalWithScriptEngineArg(QScriptEngine*)");
+ m_myObject->emitMySignalWithScriptEngineArg(m_engine);
+ QCOMPARE(m_engine->evaluate("gotSignal").toBoolean(), true);
+ QCOMPARE(m_engine->evaluate("signalArgs.length").toNumber(), 1.0);
+ QVERIFY(m_engine->evaluate("signalArgs[0]").isUndefined());
+ QVERIFY(m_engine->evaluate("myObject.mySignalWithScriptEngineArg.disconnect(myHandler)").isUndefined());
+
// connect(object, function)
m_engine->evaluate("otherObject = { name:'foo' }");
QVERIFY(m_engine->evaluate("myObject.mySignal.connect(otherObject, myHandler)").isUndefined());
@@ -1825,6 +1939,22 @@ void tst_QScriptExtQObject::cppConnectAndDisconnect()
QVERIFY(qScriptDisconnect(m_myObject, SIGNAL(mySignal()), QScriptValue(), fun));
QCOMPARE(m_myObject->disconnectedSignal().constData(), SIGNAL(mySignal()));
}
+
+ // bad args
+ QVERIFY(!qScriptConnect(0, SIGNAL(foo()), QScriptValue(), fun));
+ QVERIFY(!qScriptConnect(&edit, 0, QScriptValue(), fun));
+ QVERIFY(!qScriptConnect(&edit, SIGNAL(foo()), QScriptValue(), fun));
+ QVERIFY(!qScriptConnect(&edit, SIGNAL(textChanged(QString)), QScriptValue(), QScriptValue()));
+ QVERIFY(!qScriptDisconnect(0, SIGNAL(foo()), QScriptValue(), fun));
+ QVERIFY(!qScriptDisconnect(&edit, 0, QScriptValue(), fun));
+ QVERIFY(!qScriptDisconnect(&edit, SIGNAL(foo()), QScriptValue(), fun));
+ QVERIFY(!qScriptDisconnect(&edit, SIGNAL(textChanged(QString)), QScriptValue(), QScriptValue()));
+ {
+ QScriptEngine eng2;
+ QScriptValue receiverInDifferentEngine = eng2.newObject();
+ QVERIFY(!qScriptConnect(&edit, SIGNAL(textChanged(QString)), receiverInDifferentEngine, fun));
+ QVERIFY(!qScriptDisconnect(&edit, SIGNAL(textChanged(QString)), receiverInDifferentEngine, fun));
+ }
}
void tst_QScriptExtQObject::classEnums()
@@ -1857,7 +1987,7 @@ void tst_QScriptExtQObject::classEnums()
QCOMPARE(MyQObject::Ability(m_engine->evaluate("MyQObject.AllAbility").toInt32()),
MyQObject::AllAbility);
- QScriptValue::PropertyFlags expectedEnumFlags = QScriptValue::ReadOnly;
+ QScriptValue::PropertyFlags expectedEnumFlags = QScriptValue::ReadOnly | QScriptValue::Undeletable;
QCOMPARE(myClass.propertyFlags("FooPolicy"), expectedEnumFlags);
QCOMPARE(myClass.propertyFlags("BarPolicy"), expectedEnumFlags);
QCOMPARE(myClass.propertyFlags("BazPolicy"), expectedEnumFlags);
@@ -1879,6 +2009,16 @@ void tst_QScriptExtQObject::classEnums()
QCOMPARE(m_myObject->qtFunctionActuals().at(0).toInt(), int(MyQObject::BazPolicy));
m_myObject->resetQtFunctionInvoked();
+ QCOMPARE(m_engine->evaluate("myObject.myInvokableWithEnumArg('BarPolicy')").isUndefined(), true);
+ QCOMPARE(m_myObject->qtFunctionInvoked(), 10);
+ QCOMPARE(m_myObject->qtFunctionActuals().size(), 1);
+ QCOMPARE(m_myObject->qtFunctionActuals().at(0).toInt(), int(MyQObject::BarPolicy));
+
+ m_myObject->resetQtFunctionInvoked();
+ QVERIFY(m_engine->evaluate("myObject.myInvokableWithEnumArg('NoSuchPolicy')").isError());
+ QCOMPARE(m_myObject->qtFunctionInvoked(), -1);
+
+ m_myObject->resetQtFunctionInvoked();
QCOMPARE(m_engine->evaluate("myObject.myInvokableWithQualifiedEnumArg(MyQObject.BazPolicy)").isUndefined(), true);
QCOMPARE(m_myObject->qtFunctionInvoked(), 36);
QCOMPARE(m_myObject->qtFunctionActuals().size(), 1);
@@ -1898,6 +2038,15 @@ void tst_QScriptExtQObject::classEnums()
QCOMPARE(m_myObject->qtFunctionActuals().size(), 0);
QCOMPARE(ret.isNumber(), true);
}
+
+ // enum properties are not deletable or writable
+ QVERIFY(!m_engine->evaluate("delete MyQObject.BazPolicy").toBool());
+ myClass.setProperty("BazPolicy", QScriptValue());
+ QCOMPARE(static_cast<MyQObject::Policy>(myClass.property("BazPolicy").toInt32()),
+ MyQObject::BazPolicy);
+ myClass.setProperty("BazPolicy", MyQObject::FooPolicy);
+ QCOMPARE(static_cast<MyQObject::Policy>(myClass.property("BazPolicy").toInt32()),
+ MyQObject::BazPolicy);
}
QT_BEGIN_NAMESPACE