Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Don't allow tst_QJSEngine or tst_QJSValue instance to be collected
Both of those unit tests currently change the ownership of the test
instance object, which could result in it being collected by the JS
GC and deleted if events were processed.

Change-Id: I5a9821fb56e19af1d52fea46e54755875dfbb29a
Reviewed-by: Michael Brasser <michael.brasser@nokia.com>
  • Loading branch information
Chris Adams authored and Qt by Nokia committed Mar 15, 2012
1 parent 5ae8cab commit 147247a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
24 changes: 15 additions & 9 deletions tests/auto/qml/qjsengine/tst_qjsengine.cpp
Expand Up @@ -899,6 +899,7 @@ void tst_QJSEngine::jsParseDate()
void tst_QJSEngine::newQObject()
{
QJSEngine eng;
QObject temp;

{
QJSValue qobject = eng.newQObject(0);
Expand All @@ -907,11 +908,11 @@ void tst_QJSEngine::newQObject()
QCOMPARE(qobject.toQObject(), (QObject *)0);
}
{
QJSValue qobject = eng.newQObject(this);
QJSValue qobject = eng.newQObject(&temp);
QVERIFY(!qobject.isUndefined());
QCOMPARE(qobject.isQObject(), true);
QCOMPARE(qobject.isObject(), true);
QCOMPARE(qobject.toQObject(), (QObject *)this);
QCOMPARE(qobject.toQObject(), (QObject *)&temp);
QVERIFY(!qobject.isCallable());
// prototype should be QObject.prototype
QCOMPARE(qobject.prototype().isObject(), true);
Expand Down Expand Up @@ -1068,6 +1069,7 @@ void tst_QJSEngine::newQObject_sameQObject()
#if 0 // FIXME: No prototype API in QScriptEngine
void tst_QJSEngine::newQObject_defaultPrototype()
{
QObject temp;
QScriptEngine eng;
// newQObject() should set the default prototype, if one has been registered
{
Expand All @@ -1076,14 +1078,14 @@ void tst_QJSEngine::newQObject_defaultPrototype()
QScriptValue qobjectProto = eng.newObject();
eng.setDefaultPrototype(qMetaTypeId<QObject*>(), qobjectProto);
{
QScriptValue ret = eng.newQObject(this);
QScriptValue ret = eng.newQObject(&temp);
QVERIFY(ret.prototype().equals(qobjectProto));
}
QScriptValue tstProto = eng.newObject();
int typeId = qRegisterMetaType<tst_QJSEngine*>("tst_QJSEngine*");
eng.setDefaultPrototype(typeId, tstProto);
{
QScriptValue ret = eng.newQObject(this);
QScriptValue ret = eng.newQObject(temp);
QVERIFY(ret.prototype().equals(tstProto));
}

Expand Down Expand Up @@ -1261,7 +1263,8 @@ void tst_QJSEngine::newQMetaObject()
QVERIFY(instanceofJS(inst, qclass3).strictlyEquals(false));
}
{
QScriptValue inst = qclass4.callAsConstructor(QScriptValueList() << eng.newQObject(this));
QObject temp;
QScriptValue inst = qclass4.callAsConstructor(QScriptValueList() << eng.newQObject(&temp));
QVERIFY(inst.isQObject());
QVERIFY(inst.toQObject() != 0);
QCOMPARE(inst.toQObject()->parent(), (QObject*)this);
Expand Down Expand Up @@ -2887,6 +2890,7 @@ void tst_QJSEngine::castWithPrototypeChain()
QScriptEngine eng;
Bar bar;
Baz baz;
QObject temp;
QScriptValue barProto = eng.toScriptValue(&bar);
QScriptValue bazProto = eng.toScriptValue(&baz);
eng.setDefaultPrototype(qMetaTypeId<Bar*>(), barProto);
Expand Down Expand Up @@ -2954,7 +2958,7 @@ void tst_QJSEngine::castWithPrototypeChain()
QVERIFY(pbar == 0);
}

bazProto.setPrototype(eng.newQObject(this));
bazProto.setPrototype(eng.newQObject(&temp));
{
Baz *pbaz = qscriptvalue_cast<Baz*>(baz2Value);
QVERIFY(pbaz != 0);
Expand Down Expand Up @@ -3756,8 +3760,9 @@ class ThreadedEngine : public QThread {
QScriptEngine* m_engine;
protected:
void run() {
QObject temp;
m_engine = new QScriptEngine();
m_engine->setGlobalObject(m_engine->newQObject(this));
m_engine->setGlobalObject(m_engine->newQObject(&temp));
m_engine->evaluate("while (1) { sleep(); }");
delete m_engine;
}
Expand Down Expand Up @@ -4941,6 +4946,7 @@ void tst_QJSEngine::reentrancy_Array()

void tst_QJSEngine::reentrancy_objectCreation()
{
QObject temp;
QJSEngine eng1;
QJSEngine eng2;
{
Expand All @@ -4957,8 +4963,8 @@ void tst_QJSEngine::reentrancy_objectCreation()
QCOMPARE(qjsvalue_cast<QRegExp>(r2), qjsvalue_cast<QRegExp>(r1));
}
{
QJSValue o1 = eng1.newQObject(this);
QJSValue o2 = eng2.newQObject(this);
QJSValue o1 = eng1.newQObject(&temp);
QJSValue o2 = eng2.newQObject(&temp);
QCOMPARE(o1.toQObject(), o2.toQObject());
QCOMPARE(o2.toQObject(), o1.toQObject());
}
Expand Down
34 changes: 21 additions & 13 deletions tests/auto/qml/qjsvalue/tst_qjsvalue.cpp
Expand Up @@ -986,6 +986,7 @@ Q_DECLARE_METATYPE(QVariant)
void tst_QJSValue::toVariant()
{
QJSEngine eng;
QObject temp;

QJSValue undefined = eng.toScriptValue(QVariant());
QCOMPARE(undefined.toVariant(), QVariant());
Expand Down Expand Up @@ -1020,11 +1021,11 @@ void tst_QJSValue::toVariant()
QJSValue object = eng.newObject();
QCOMPARE(object.toVariant(), QVariant(QVariantMap()));

QJSValue qobject = eng.newQObject(this);
QJSValue qobject = eng.newQObject(&temp);
{
QVariant var = qobject.toVariant();
QCOMPARE(var.userType(), int(QMetaType::QObjectStar));
QCOMPARE(qVariantValue<QObject*>(var), (QObject *)this);
QCOMPARE(qVariantValue<QObject*>(var), (QObject *)&temp);
}

{
Expand Down Expand Up @@ -1130,10 +1131,11 @@ Q_DECLARE_METATYPE(QPushButton*);
void tst_QJSValue::toQObject()
{
QJSEngine eng;
QObject temp;

QJSValue qobject = eng.newQObject(this);
QCOMPARE(qobject.toQObject(), (QObject *)this);
QCOMPARE(qjsvalue_cast<QObject*>(qobject), (QObject *)this);
QJSValue qobject = eng.newQObject(&temp);
QCOMPARE(qobject.toQObject(), (QObject *)&temp);
QCOMPARE(qjsvalue_cast<QObject*>(qobject), (QObject *)&temp);
QCOMPARE(qjsvalue_cast<QWidget*>(qobject), (QWidget *)0);

QWidget widget;
Expand Down Expand Up @@ -2168,14 +2170,15 @@ void tst_QJSValue::getSetData_objects_data()
{
#if 0 // FIXME: no setData/data API
newEngine();
QObject *temp = new QObject;

QTest::addColumn<QJSValue>("object");

QTest::newRow("object from evaluate") << engine->evaluate("new Object()");
QTest::newRow("object from engine") << engine->newObject();
QTest::newRow("Array") << engine->newArray();
QTest::newRow("Date") << engine->evaluate("new Date(12324)");
QTest::newRow("QObject") << engine->newQObject(this);
QTest::newRow("QObject") << engine->newQObject(temp);
QTest::newRow("RegExp") << engine->newRegExp(QRegExp());
#endif
}
Expand Down Expand Up @@ -2265,6 +2268,7 @@ class TestScriptClass : public QScriptClass
void tst_QJSValue::getSetScriptClass_emptyClass_data()
{
newEngine();
QObject *temp = new QObject;
QTest::addColumn<QJSValue>("value");

QTest::newRow("invalid") << QJSValue();
Expand All @@ -2281,7 +2285,7 @@ void tst_QJSValue::getSetScriptClass_emptyClass_data()
QTest::newRow("undefined") << QJSValue(engine->toScriptValue(QVariant()));
QTest::newRow("object") << QJSValue(engine->newObject());
QTest::newRow("date") << QJSValue(engine->evaluate("new Date()"));
QTest::newRow("qobject") << QJSValue(engine->newQObject(this));
QTest::newRow("qobject") << QJSValue(engine->newQObject(temp));
}

void tst_QJSValue::getSetScriptClass_emptyClass()
Expand Down Expand Up @@ -2341,9 +2345,10 @@ void tst_QJSValue::getSetScriptClass_QVariant()
void tst_QJSValue::getSetScriptClass_QObject()
{
QScriptEngine eng;
QObject temp;
TestScriptClass testClass(&eng);
{
QJSValue obj = eng.newQObject(this);
QJSValue obj = eng.newQObject(&temp);
QVERIFY(obj.isQObject());
obj.setScriptClass(&testClass);
QCOMPARE(obj.scriptClass(), (QScriptClass*)&testClass);
Expand Down Expand Up @@ -2999,6 +3004,7 @@ void tst_QJSValue::lessThan()
void tst_QJSValue::equals()
{
QJSEngine eng;
QObject temp;

QVERIFY(QJSValue().equals(QJSValue()));

Expand Down Expand Up @@ -3088,8 +3094,8 @@ void tst_QJSValue::equals()
QCOMPARE(obj1.equals(obj1), true);
QCOMPARE(obj2.equals(obj2), true);

QJSValue qobj1 = eng.newQObject(this);
QJSValue qobj2 = eng.newQObject(this);
QJSValue qobj1 = eng.newQObject(&temp);
QJSValue qobj2 = eng.newQObject(&temp);
QJSValue qobj3 = eng.newQObject(0);

// FIXME: No ScriptOwnership: QJSValue qobj4 = eng.newQObject(new QObject(), QScriptEngine::ScriptOwnership);
Expand Down Expand Up @@ -3137,6 +3143,7 @@ void tst_QJSValue::equals()
void tst_QJSValue::strictlyEquals()
{
QJSEngine eng;
QObject temp;

QVERIFY(QJSValue().strictlyEquals(QJSValue()));

Expand Down Expand Up @@ -3242,8 +3249,8 @@ void tst_QJSValue::strictlyEquals()
QCOMPARE(obj2.strictlyEquals(obj2), true);
QVERIFY(!obj1.strictlyEquals(QJSValue()));

QJSValue qobj1 = eng.newQObject(this);
QJSValue qobj2 = eng.newQObject(this);
QJSValue qobj1 = eng.newQObject(&temp);
QJSValue qobj2 = eng.newQObject(&temp);
QVERIFY(qobj1.strictlyEquals(qobj2));

{
Expand Down Expand Up @@ -3482,13 +3489,14 @@ void tst_QJSValue::prettyPrinter()
void tst_QJSValue::engineDeleted()
{
QJSEngine *eng = new QJSEngine;
QObject temp;
QJSValue v1 = eng->toScriptValue(123);
QVERIFY(v1.isNumber());
QJSValue v2 = eng->toScriptValue(QString("ciao"));
QVERIFY(v2.isString());
QJSValue v3 = eng->newObject();
QVERIFY(v3.isObject());
QJSValue v4 = eng->newQObject(this);
QJSValue v4 = eng->newQObject(&temp);
QVERIFY(v4.isQObject());
QJSValue v5 = "Hello";
QVERIFY(v2.isString());
Expand Down

0 comments on commit 147247a

Please sign in to comment.