This is a slightly hacky way to make ScriptManager work without any networking, and minimize the number of dependencies needed to run a test.
Showing the changes to tests/script-engine/src/ScriptEngineTests.cpp (1 of 7 files). Show all
Unified · Split
| @@ -1,5 +1,8 @@ | |||
| 1 | #include <QSignalSpy> | 1 | #include <QSignalSpy> |
| 2 | #include <QDebug> | 2 | #include <QDebug> |
| 3 | #include <QFile> | ||
| 4 | #include <QTextStream> | ||
| 5 | |||
| 3 | 6 | ||
| 4 | #include "ScriptEngineTests.h" | 7 | #include "ScriptEngineTests.h" |
| 5 | #include "DependencyManager.h" | 8 | #include "DependencyManager.h" |
| @@ -7,6 +10,8 @@ · 1 unmodified line | |||
| 7 | #include "ScriptEngines.h" | 10 | #include "ScriptEngines.h" |
| 8 | #include "ScriptEngine.h" | 11 | #include "ScriptEngine.h" |
| 9 | #include "ScriptCache.h" | 12 | #include "ScriptCache.h" |
| 13 | #include "ScriptManager.h" | ||
| 14 | |||
| 10 | #include "ResourceManager.h" | 15 | #include "ResourceManager.h" |
| 11 | #include "ResourceRequestObserver.h" | 16 | #include "ResourceRequestObserver.h" |
| 12 | #include "StatTracker.h" | 17 | #include "StatTracker.h" |
| @@ -18,34 +23,21 @@ · 5 unmodified lines | |||
| 18 | QTEST_MAIN(ScriptEngineTests) | 23 | QTEST_MAIN(ScriptEngineTests) |
| 19 | 24 | ||
| 20 | 25 | ||
| 21 | // script factory generates scriptmanager -- singleton | ||
| 22 | // | ||
| 23 | // default scripts -- all in one thread, but chat spawns a separate thread | ||
| 24 | // // https://apidocs.overte.org/Script.html#.executeOnScriptThread | ||
| 25 | // | ||
| 26 | // scriptmanager | ||
| 27 | // every thread has a manager, and its own engine | ||
| 28 | // provides non-qt interface? | ||
| 29 | // | ||
| 30 | // special threads for entity scripts -- 12 (fixed? dynamic?) | ||
| 31 | |||
| 32 | |||
| 33 | |||
| 34 | 26 | ||
| 35 | void ScriptEngineTests::initTestCase() { | 27 | void ScriptEngineTests::initTestCase() { |
| 36 | // AudioClient starts networking, but for the purposes of the tests here we don't care, | 28 | // AudioClient starts networking, but for the purposes of the tests here we don't care, |
| 37 | // so just got to use some port. | 29 | // so just got to use some port. |
| 38 | int listenPort = 10000; | 30 | //int listenPort = 10000; |
| 39 | 31 | ||
| 40 | DependencyManager::registerInheritance<LimitedNodeList, NodeList>(); | 32 | //DependencyManager::registerInheritance<LimitedNodeList, NodeList>(); |
| 41 | DependencyManager::set<NodeList>(NodeType::Agent, listenPort); | 33 | //DependencyManager::set<NodeList>(NodeType::Agent, listenPort); |
| 42 | DependencyManager::set<ScriptEngines>(ScriptManager::CLIENT_SCRIPT, QUrl("")); | 34 | DependencyManager::set<ScriptEngines>(ScriptManager::NETWORKLESS_TEST_SCRIPT, QUrl("")); |
| 43 | DependencyManager::set<ScriptCache>(); | 35 | DependencyManager::set<ScriptCache>(); |
| 44 | DependencyManager::set<ResourceManager>(); | 36 | // DependencyManager::set<ResourceManager>(); |
| 45 | DependencyManager::set<ResourceRequestObserver>(); | 37 | // DependencyManager::set<ResourceRequestObserver>(); |
| 46 | DependencyManager::set<StatTracker>(); | 38 | DependencyManager::set<StatTracker>(); |
| 47 | DependencyManager::set<ScriptInitializers>(); | 39 | DependencyManager::set<ScriptInitializers>(); |
| 48 | DependencyManager::set<EntityScriptingInterface>(true); | 40 | // DependencyManager::set<EntityScriptingInterface>(true); |
| 49 | 41 | ||
| 50 | QSharedPointer<ScriptEngines> ac = DependencyManager::get<ScriptEngines>(); | 42 | QSharedPointer<ScriptEngines> ac = DependencyManager::get<ScriptEngines>(); |
| 51 | QVERIFY(!ac.isNull()); | 43 | QVERIFY(!ac.isNull()); |
| @@ -77,31 +69,71 @@ void ScriptEngineTests::scriptTest() { · 25 unmodified lines | |||
| 77 | QSharedPointer<ScriptEngines> ac = DependencyManager::get<ScriptEngines>(); | 69 | QSharedPointer<ScriptEngines> ac = DependencyManager::get<ScriptEngines>(); |
| 78 | QVERIFY(!ac.isNull()); | 70 | QVERIFY(!ac.isNull()); |
| 79 | 71 | ||
| 80 | // TODO: can we execute test scripts in serial way rather than parallel | 72 | |
| 81 | /*QDir testScriptsDir("tests"); | 73 | QDir testScriptsDir("tests"); |
| 82 | QStringList testScripts = testScriptsDir.entryList(QStringList() << "*.js", QDir::Files); | 74 | QStringList testScripts = testScriptsDir.entryList(QStringList() << "*.js", QDir::Files); |
| 83 | testScripts.sort(); | 75 | testScripts.sort(); |
| 84 | 76 | ||
| 85 | for(QString script : testScripts) { | 77 | for(QString scriptFilename : testScripts) { |
| 86 | script = "tests/" + script; | 78 | scriptFilename = "tests/" + scriptFilename; |
| 87 | qInfo() << "Running test script: " << script; | 79 | qInfo() << "Running test script: " << scriptFilename; |
| 88 | ac->loadOneScript(script); | 80 | |
| 89 | }*/ | 81 | QString scriptSource; |
| 90 | //ac->loadOneScript("tests/003_vector_math.js"); | 82 | |
| 91 | ac->loadOneScript("tests/005_include.js"); | 83 | { |
| 92 | 84 | QFile scriptFile(scriptFilename); | |
| 93 | qDebug() << ac->getRunning(); | 85 | scriptFile.open(QIODevice::ReadOnly); |
| 94 | 86 | QTextStream scriptStream(&scriptFile); | |
| 95 | // TODO: if I don't have infinite loop here, it exits before scripts finish. It also reports: QSignalSpy: No such signal: 'scriptCountChanged' | 87 | scriptSource.append(scriptStream.readAll()); |
| 96 | for (int n = 0; n > -1; n++) { | 88 | |
| 97 | QSignalSpy spy(ac.get(), SIGNAL(scriptCountChanged)); | 89 | // Scripts keep on running until Script.stop() is called. For our tests here, |
| 98 | spy.wait(100000); | 90 | // that's not desirable, so we append an automatic stop at the end of every |
| 99 | qDebug() << "Signal happened"; | 91 | // script. |
| 100 | } | 92 | scriptSource.append("\nScript.stop(true);\n"); |
| 101 | //spy.wait(5000); | 93 | } |
| 102 | //ac->shutdownScripting(); | 94 | |
| 95 | |||
| 96 | //qDebug() << "Source: " << scriptSource; | ||
| 103 | 97 | ||
| 98 | ScriptManagerPointer sm = newScriptManager(ScriptManager::NETWORKLESS_TEST_SCRIPT, scriptSource, scriptFilename); | ||
| 104 | 99 | ||
| 105 | 100 | ||
| 101 | connect(sm.get(), &ScriptManager::scriptLoaded, [](const QString& filename){ | ||
| 102 | qWarning() << "Loaded script" << filename; | ||
| 103 | }); | ||
| 104 | |||
| 105 | |||
| 106 | connect(sm.get(), &ScriptManager::errorLoadingScript, [](const QString& filename){ | ||
| 107 | qWarning() << "Failed to load script" << filename; | ||
| 108 | }); | ||
| 109 | |||
| 110 | connect(sm.get(), &ScriptManager::printedMessage, [](const QString& message, const QString& engineName){ | ||
| 111 | qDebug() << "Printed message from engine" << engineName << ": " << message; | ||
| 112 | }); | ||
| 113 | |||
| 114 | connect(sm.get(), &ScriptManager::infoMessage, [](const QString& message, const QString& engineName){ | ||
| 115 | qInfo() << "Info message from engine" << engineName << ": " << message; | ||
| 116 | }); | ||
| 117 | |||
| 118 | connect(sm.get(), &ScriptManager::warningMessage, [](const QString& message, const QString& engineName){ | ||
| 119 | qWarning() << "Warning from engine" << engineName << ": " << message; | ||
| 120 | }); | ||
| 121 | |||
| 122 | connect(sm.get(), &ScriptManager::errorMessage, [](const QString& message, const QString& engineName){ | ||
| 123 | qCritical() << "Error from engine" << engineName << ": " << message; | ||
| 124 | }); | ||
| 125 | |||
| 126 | connect(sm.get(), &ScriptManager::finished, [](const QString& fileNameString, ScriptManagerPointer smp){ | ||
| 127 | qInfo() << "Finished running script" << fileNameString; | ||
| 128 | }); | ||
| 129 | |||
| 130 | connect(sm.get(), &ScriptManager::runningStateChanged, [sm](){ | ||
| 131 | qInfo() << "Running state changed. Running = " << sm->isRunning() << "; Stopped = " << sm->isStopped() << "; Finished = " << sm->isFinished(); | ||
| 132 | }); | ||
| 133 | |||
| 134 | |||
| 135 | sm->run(); | ||
| 136 | } | ||
| 137 | |||
| 106 | //TODO: Add a test for Script.require(JSON) | 138 | //TODO: Add a test for Script.require(JSON) |
| 107 | } | 139 | } |