diff options
author | Jedrzej Nowacki <jedrzej.nowacki@nokia.com> | 2011-02-09 11:51:10 (GMT) |
---|---|---|
committer | Jedrzej Nowacki <jedrzej.nowacki@nokia.com> | 2011-02-10 07:54:31 (GMT) |
commit | ad8ff4b6f6204a982d6050a45997d3a27be01fac (patch) | |
tree | fcda1c48e751ae41b849d144840dc737deabb2c0 /tests/auto/qscriptengineagent | |
parent | e8abc416f07f74dd12e75b61be0aeac297b7a1f8 (diff) | |
download | Qt-ad8ff4b6f6204a982d6050a45997d3a27be01fac.zip Qt-ad8ff4b6f6204a982d6050a45997d3a27be01fac.tar.gz Qt-ad8ff4b6f6204a982d6050a45997d3a27be01fac.tar.bz2 |
Update tests for QScriptEngineAgent.
New test checking what happens in case of recursive unload event
(scriptUnload that evaluate a script that cause scriptUnload...) was
added.
Change existing tests so they are less strict. It is not specified when
exactly scriptUnload event happens.
Reviewed-by: Kent Hansen
Diffstat (limited to 'tests/auto/qscriptengineagent')
-rw-r--r-- | tests/auto/qscriptengineagent/tst_qscriptengineagent.cpp | 57 |
1 files changed, 50 insertions, 7 deletions
diff --git a/tests/auto/qscriptengineagent/tst_qscriptengineagent.cpp b/tests/auto/qscriptengineagent/tst_qscriptengineagent.cpp index dce09d3..d89f9f1 100644 --- a/tests/auto/qscriptengineagent/tst_qscriptengineagent.cpp +++ b/tests/auto/qscriptengineagent/tst_qscriptengineagent.cpp @@ -73,6 +73,7 @@ signals: void testSignal(double arg); private slots: + void unloadRecursion(); void scriptLoadAndUnload_statement(); void scriptLoadAndUnload(); void scriptLoadAndUnload_eval(); @@ -345,6 +346,46 @@ QVariant ScriptEngineSpy::extension(Extension ext, const QVariant &arg) return QVariant(); } +static void collectScriptObjects(QScriptEngine *engine) +{ + // We call garbage collection few times to collect objects that + // are unreferenced after first gc. We try to force full gc. + engine->collectGarbage(); + engine->collectGarbage(); + engine->collectGarbage(); +} + +class EvaluatingAgent : public QScriptEngineAgent { +public: + EvaluatingAgent(QScriptEngine *engine) + : QScriptEngineAgent(engine) + , count(0) + {} + + virtual void scriptUnload(qint64) + { + if (++count > 10) // recursion breaker. + return; + // check if recursive evaluation works + engine()->evaluate(";"); + collectScriptObjects(engine()); + } + + bool isOk() const { return count > 10; } +private: + int count; +}; + +void tst_QScriptEngineAgent::unloadRecursion() +{ + QScriptEngine engine; + EvaluatingAgent *agent = new EvaluatingAgent(&engine); + engine.setAgent(agent); + engine.evaluate(";"); + collectScriptObjects(&engine); + QVERIFY(agent->isOk()); +} + void tst_QScriptEngineAgent::scriptLoadAndUnload_statement() { QScriptEngine eng; @@ -358,6 +399,8 @@ void tst_QScriptEngineAgent::scriptLoadAndUnload_statement() int lineNumber = 123; eng.evaluate(code, fileName, lineNumber); + // Script object have to be garbage collected first. + collectScriptObjects(&eng); QCOMPARE(spy->count(), 2); QCOMPARE(spy->at(0).type, ScriptEngineEvent::ScriptLoad); @@ -377,6 +420,8 @@ void tst_QScriptEngineAgent::scriptLoadAndUnload_statement() int lineNumber = 456; eng.evaluate(code, fileName, lineNumber); + // Script object have to be garbage collected first. + collectScriptObjects(&eng); QCOMPARE(spy->count(), 2); QCOMPARE(spy->at(0).type, ScriptEngineEvent::ScriptLoad); @@ -414,7 +459,8 @@ void tst_QScriptEngineAgent::scriptLoadAndUnload() code = "foo = null"; eng.evaluate(code); - QCOMPARE(spy->count(), 3); + collectScriptObjects(&eng); // foo() is GC'ed + QCOMPARE(spy->count(), 4); QCOMPARE(spy->at(1).type, ScriptEngineEvent::ScriptLoad); QVERIFY(spy->at(1).scriptId != -1); @@ -425,8 +471,6 @@ void tst_QScriptEngineAgent::scriptLoadAndUnload() QCOMPARE(spy->at(2).type, ScriptEngineEvent::ScriptUnload); QCOMPARE(spy->at(2).scriptId, spy->at(1).scriptId); - eng.collectGarbage(); // foo() is GC'ed - QCOMPARE(spy->count(), 4); QCOMPARE(spy->at(3).type, ScriptEngineEvent::ScriptUnload); QCOMPARE(spy->at(3).scriptId, spy->at(0).scriptId); } @@ -448,6 +492,7 @@ void tst_QScriptEngineAgent::scriptLoadAndUnload() code = "bar = foo(); foo = null"; eng.evaluate(code); + collectScriptObjects(&eng); QCOMPARE(spy->count(), 3); QCOMPARE(spy->at(1).type, ScriptEngineEvent::ScriptLoad); @@ -458,14 +503,12 @@ void tst_QScriptEngineAgent::scriptLoadAndUnload() QCOMPARE(spy->at(2).type, ScriptEngineEvent::ScriptUnload); QCOMPARE(spy->at(2).scriptId, spy->at(1).scriptId); - eng.collectGarbage(); // foo() is not GC'ed + collectScriptObjects(&eng); // foo() is not GC'ed QCOMPARE(spy->count(), 3); code = "bar = null"; eng.evaluate(code); - QCOMPARE(spy->count(), 5); - - eng.collectGarbage(); // foo() is GC'ed + collectScriptObjects(&eng); // foo() is GC'ed QCOMPARE(spy->count(), 6); } delete spy; |