This will fail against the current master, since the current behavior is not correct.
Showing the changes to tests/script-engine/src/ScriptEngineNetworkedTests.cpp (1 of 7 files). Show all
Unified · Split
| @@ -0,0 +1,158 @@ | |||
| 1 | // | ||
| 2 | // Copyright 2023 Overte e.V. | ||
| 3 | // | ||
| 4 | // Distributed under the Apache License, Version 2.0. | ||
| 5 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | ||
| 6 | // SPDX-License-Identifier: Apache-2.0 | ||
| 7 | // | ||
| 8 | |||
| 9 | #include <QSignalSpy> | ||
| 10 | #include <QDebug> | ||
| 11 | #include <QFile> | ||
| 12 | #include <QTextStream> | ||
| 13 | |||
| 14 | |||
| 15 | #include "ScriptEngineNetworkedTests.h" | ||
| 16 | #include "DependencyManager.h" | ||
| 17 | |||
| 18 | #include "ScriptEngines.h" | ||
| 19 | #include "ScriptEngine.h" | ||
| 20 | #include "ScriptCache.h" | ||
| 21 | #include "ScriptManager.h" | ||
| 22 | #include "ScriptEngines.h" | ||
| 23 | #include "AddressManager.h" | ||
| 24 | #include "AccountManager.h" | ||
| 25 | #include "DomainAccountManager.h" | ||
| 26 | #include "ResourceManager.h" | ||
| 27 | #include "ResourceRequestObserver.h" | ||
| 28 | #include "StatTracker.h" | ||
| 29 | #include "MessagesClient.h" | ||
| 30 | #include "ResourceScriptingInterface.h" | ||
| 31 | #include "UserActivityLogger.h" | ||
| 32 | #include "UserActivityLoggerScriptingInterface.h" | ||
| 33 | #include "EntityScriptingInterface.h" | ||
| 34 | #include "ResourceManager.h" | ||
| 35 | #include "NodeList.h" | ||
| 36 | #include "../../../libraries/entities/src/EntityScriptingInterface.h" | ||
| 37 | //#include "../../../libraries/entities/src/EntityScriptingInterface.h" | ||
| 38 | |||
| 39 | QTEST_MAIN(ScriptEngineNetworkedTests) | ||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | void ScriptEngineNetworkedTests::initTestCase() { | ||
| 44 | // AudioClient starts networking, but for the purposes of the tests here we don't care, | ||
| 45 | // so just got to use some port. | ||
| 46 | int listenPort = 10000; | ||
| 47 | |||
| 48 | DependencyManager::registerInheritance<LimitedNodeList, NodeList>(); | ||
| 49 | |||
| 50 | DependencyManager::set<AccountManager>(true); // use the default user agent getter | ||
| 51 | DependencyManager::set<DomainAccountManager>(); | ||
| 52 | DependencyManager::set<AddressManager>(); | ||
| 53 | DependencyManager::set<NodeList>(NodeType::Agent, listenPort); | ||
| 54 | |||
| 55 | DependencyManager::set<ScriptEngines>(ScriptManager::CLIENT_SCRIPT, QUrl("")); | ||
| 56 | DependencyManager::set<ScriptCache>(); | ||
| 57 | // DependencyManager::set<ResourceManager>(); | ||
| 58 | // DependencyManager::set<ResourceRequestObserver>(); | ||
| 59 | DependencyManager::set<StatTracker>(); | ||
| 60 | DependencyManager::set<ScriptInitializers>(); | ||
| 61 | // DependencyManager::set<EntityScriptingInterface>(true); | ||
| 62 | |||
| 63 | DependencyManager::set<MessagesClient>(); | ||
| 64 | DependencyManager::set<ResourceScriptingInterface>(); | ||
| 65 | DependencyManager::set<UserActivityLoggerScriptingInterface>(); | ||
| 66 | DependencyManager::set<EntityScriptingInterface>(true); | ||
| 67 | DependencyManager::set<ResourceManager>(); | ||
| 68 | DependencyManager::set<ResourceRequestObserver>(); | ||
| 69 | |||
| 70 | |||
| 71 | auto nodeList = DependencyManager::get<NodeList>(); | ||
| 72 | nodeList->startThread(); | ||
| 73 | nodeList->setFlagTimeForConnectionStep(true); | ||
| 74 | |||
| 75 | } | ||
| 76 | |||
| 77 | ScriptManagerPointer ScriptEngineNetworkedTests::makeManager(const QString &scriptSource, const QString &scriptFilename) { | ||
| 78 | ScriptManagerPointer sm = scriptManagerFactory(ScriptManager::CLIENT_SCRIPT, scriptSource, scriptFilename); | ||
| 79 | |||
| 80 | |||
| 81 | sm->setAbortOnUncaughtException(true); | ||
| 82 | |||
| 83 | connect(sm.get(), &ScriptManager::scriptLoaded, [](const QString& filename){ | ||
| 84 | qWarning() << "Loaded script" << filename; | ||
| 85 | }); | ||
| 86 | |||
| 87 | |||
| 88 | connect(sm.get(), &ScriptManager::errorLoadingScript, [](const QString& filename){ | ||
| 89 | qWarning() << "Failed to load script" << filename; | ||
| 90 | }); | ||
| 91 | |||
| 92 | connect(sm.get(), &ScriptManager::printedMessage, [](const QString& message, const QString& engineName){ | ||
| 93 | qDebug() << "Printed message from engine" << engineName << ": " << message; | ||
| 94 | }); | ||
| 95 | |||
| 96 | connect(sm.get(), &ScriptManager::infoMessage, [](const QString& message, const QString& engineName){ | ||
| 97 | qInfo() << "Info message from engine" << engineName << ": " << message; | ||
| 98 | }); | ||
| 99 | |||
| 100 | connect(sm.get(), &ScriptManager::warningMessage, [](const QString& message, const QString& engineName){ | ||
| 101 | qWarning() << "Warning from engine" << engineName << ": " << message; | ||
| 102 | }); | ||
| 103 | |||
| 104 | connect(sm.get(), &ScriptManager::errorMessage, [](const QString& message, const QString& engineName){ | ||
| 105 | qCritical() << "Error from engine" << engineName << ": " << message; | ||
| 106 | }); | ||
| 107 | |||
| 108 | connect(sm.get(), &ScriptManager::finished, [](const QString& fileNameString, ScriptManagerPointer smp){ | ||
| 109 | qInfo() << "Finished running script" << fileNameString; | ||
| 110 | }); | ||
| 111 | |||
| 112 | connect(sm.get(), &ScriptManager::runningStateChanged, [sm](){ | ||
| 113 | qInfo() << "Running state changed. Running = " << sm->isRunning() << "; Stopped = " << sm->isStopped() << "; Finished = " << sm->isFinished(); | ||
| 114 | }); | ||
| 115 | |||
| 116 | connect(sm.get(), &ScriptManager::unhandledException, [](std::shared_ptr<ScriptException> exception){ | ||
| 117 | qWarning() << "Exception from engine: " << exception; | ||
| 118 | }); | ||
| 119 | |||
| 120 | |||
| 121 | return sm; | ||
| 122 | } | ||
| 123 | |||
| 124 | void ScriptEngineNetworkedTests::testRequire() { | ||
| 125 | auto sm = makeManager( | ||
| 126 | "print(\"Starting\");" | ||
| 127 | "Script.require('./tests/c.js');" | ||
| 128 | "print(\"Done\");" | ||
| 129 | "Script.stop(true);", "testRequire.js"); | ||
| 130 | QStringList printed; | ||
| 131 | QStringList expected {"Starting", "Value from A: 6", "Value from B: 6", "Done"}; | ||
| 132 | |||
| 133 | |||
| 134 | QVERIFY(!sm->isRunning()); | ||
| 135 | QVERIFY(!sm->isStopped()); | ||
| 136 | QVERIFY(!sm->isFinished()); | ||
| 137 | |||
| 138 | connect(sm.get(), &ScriptManager::printedMessage, [&printed](const QString& message, const QString& engineName){ | ||
| 139 | printed.append(message); | ||
| 140 | }); | ||
| 141 | |||
| 142 | |||
| 143 | qInfo() << "About to run script"; | ||
| 144 | sm->run(); | ||
| 145 | |||
| 146 | QVERIFY(!sm->isRunning()); | ||
| 147 | QVERIFY(!sm->isStopped()); | ||
| 148 | QVERIFY(sm->isFinished()); | ||
| 149 | |||
| 150 | QVERIFY(printed.length() == expected.length()); | ||
| 151 | for(int i=0;i<printed.length();i++) { | ||
| 152 | QString nomatch = QString("Result '%1' didn't match expected '%2'").arg(printed[i]).arg(expected[i]); | ||
| 153 | QVERIFY2(printed[i] == expected[i], qPrintable(nomatch)); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | |||