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 | 1 | #include <QSignalSpy> |
| 2 | 2 | #include <QDebug> |
| 3 | +#include <QFile> | |
| 4 | +#include <QTextStream> | |
| 5 | + | |
| 3 | 6 | |
| 4 | 7 | #include "ScriptEngineTests.h" |
| 5 | 8 | #include "DependencyManager.h" |
| @@ -7,6 +10,8 @@ · 1 unmodified line | ||
| 7 | 10 | #include "ScriptEngines.h" |
| 8 | 11 | #include "ScriptEngine.h" |
| 9 | 12 | #include "ScriptCache.h" |
| 13 | +#include "ScriptManager.h" | |
| 14 | + | |
| 10 | 15 | #include "ResourceManager.h" |
| 11 | 16 | #include "ResourceRequestObserver.h" |
| 12 | 17 | #include "StatTracker.h" |
| @@ -18,34 +23,21 @@ · 5 unmodified lines | ||
| 18 | 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 | 27 | void ScriptEngineTests::initTestCase() { |
| 36 | 28 | // AudioClient starts networking, but for the purposes of the tests here we don't care, |
| 37 | 29 | // so just got to use some port. |
| 38 | − int listenPort = 10000; | |
| 30 | + //int listenPort = 10000; | |
| 39 | 31 | |
| 40 | − DependencyManager::registerInheritance<LimitedNodeList, NodeList>(); | |
| 41 | − DependencyManager::set<NodeList>(NodeType::Agent, listenPort); | |
| 42 | − DependencyManager::set<ScriptEngines>(ScriptManager::CLIENT_SCRIPT, QUrl("")); | |
| 32 | + //DependencyManager::registerInheritance<LimitedNodeList, NodeList>(); | |
| 33 | + //DependencyManager::set<NodeList>(NodeType::Agent, listenPort); | |
| 34 | + DependencyManager::set<ScriptEngines>(ScriptManager::NETWORKLESS_TEST_SCRIPT, QUrl("")); | |
| 43 | 35 | DependencyManager::set<ScriptCache>(); |
| 44 | − DependencyManager::set<ResourceManager>(); | |
| 45 | − DependencyManager::set<ResourceRequestObserver>(); | |
| 36 | + // DependencyManager::set<ResourceManager>(); | |
| 37 | + // DependencyManager::set<ResourceRequestObserver>(); | |
| 46 | 38 | DependencyManager::set<StatTracker>(); |
| 47 | 39 | DependencyManager::set<ScriptInitializers>(); |
| 48 | − DependencyManager::set<EntityScriptingInterface>(true); | |
| 40 | + // DependencyManager::set<EntityScriptingInterface>(true); | |
| 49 | 41 | |
| 50 | 42 | QSharedPointer<ScriptEngines> ac = DependencyManager::get<ScriptEngines>(); |
| 51 | 43 | QVERIFY(!ac.isNull()); |
| @@ -77,31 +69,71 @@ void ScriptEngineTests::scriptTest() { · 25 unmodified lines | ||
| 77 | 69 | QSharedPointer<ScriptEngines> ac = DependencyManager::get<ScriptEngines>(); |
| 78 | 70 | QVERIFY(!ac.isNull()); |
| 79 | 71 | |
| 80 | − // TODO: can we execute test scripts in serial way rather than parallel | |
| 81 | − /*QDir testScriptsDir("tests"); | |
| 72 | + | |
| 73 | + QDir testScriptsDir("tests"); | |
| 82 | 74 | QStringList testScripts = testScriptsDir.entryList(QStringList() << "*.js", QDir::Files); |
| 83 | 75 | testScripts.sort(); |
| 84 | 76 | |
| 85 | − for(QString script : testScripts) { | |
| 86 | − script = "tests/" + script; | |
| 87 | − qInfo() << "Running test script: " << script; | |
| 88 | − ac->loadOneScript(script); | |
| 89 | − }*/ | |
| 90 | − //ac->loadOneScript("tests/003_vector_math.js"); | |
| 91 | − ac->loadOneScript("tests/005_include.js"); | |
| 92 | − | |
| 93 | − qDebug() << ac->getRunning(); | |
| 94 | − | |
| 95 | − // TODO: if I don't have infinite loop here, it exits before scripts finish. It also reports: QSignalSpy: No such signal: 'scriptCountChanged' | |
| 96 | − for (int n = 0; n > -1; n++) { | |
| 97 | − QSignalSpy spy(ac.get(), SIGNAL(scriptCountChanged)); | |
| 98 | − spy.wait(100000); | |
| 99 | − qDebug() << "Signal happened"; | |
| 100 | − } | |
| 101 | − //spy.wait(5000); | |
| 102 | − //ac->shutdownScripting(); | |
| 77 | + for(QString scriptFilename : testScripts) { | |
| 78 | + scriptFilename = "tests/" + scriptFilename; | |
| 79 | + qInfo() << "Running test script: " << scriptFilename; | |
| 80 | + | |
| 81 | + QString scriptSource; | |
| 82 | + | |
| 83 | + { | |
| 84 | + QFile scriptFile(scriptFilename); | |
| 85 | + scriptFile.open(QIODevice::ReadOnly); | |
| 86 | + QTextStream scriptStream(&scriptFile); | |
| 87 | + scriptSource.append(scriptStream.readAll()); | |
| 88 | + | |
| 89 | + // Scripts keep on running until Script.stop() is called. For our tests here, | |
| 90 | + // that's not desirable, so we append an automatic stop at the end of every | |
| 91 | + // script. | |
| 92 | + scriptSource.append("\nScript.stop(true);\n"); | |
| 93 | + } | |
| 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 | 138 | //TODO: Add a test for Script.require(JSON) |
| 107 | 139 | } |