From 1364f4391d1ce0400a3ab1e16d1754f99f7793d4 Mon Sep 17 00:00:00 2001 From: NissimHadar Date: Thu, 10 May 2018 18:02:44 -0700 Subject: [PATCH 1/2] Can store JSON object. --- interface/src/Application.cpp | 29 ++++++++++++------- interface/src/Application.h | 3 +- .../src/scripting/TestScriptingInterface.cpp | 27 +++++++++++++++++ .../src/scripting/TestScriptingInterface.h | 5 ++++ 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 20efe73f08..89cbf0f6c0 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -734,9 +734,9 @@ extern InputPluginList getInputPlugins(); extern void saveInputPluginSettings(const InputPluginList& plugins); // Parameters used for running tests from teh command line -const QString TEST_SCRIPT_COMMAND { "--testScript" }; -const QString TEST_QUIT_WHEN_FINISHED_OPTION { "quitWhenFinished" }; -const QString TEST_SNAPSHOT_LOCATION_COMMAND { "--testSnapshotLocation" }; +const QString TEST_SCRIPT_COMMAND{ "--testScript" }; +const QString TEST_QUIT_WHEN_FINISHED_OPTION{ "quitWhenFinished" }; +const QString TEST_RESULTS_LOCATION_COMMAND{ "--testResultsLocation" }; bool setupEssentials(int& argc, char** argv, bool runningMarkerExisted) { const char** constArgv = const_cast(argv); @@ -1014,22 +1014,25 @@ Application::Application(int& argc, char** argv, QElapsedTimer& startupTimer, bo // If the URL scheme is http(s) or ftp, then use as is, else - treat it as a local file // This is done so as not break previous command line scripts - if (testScriptPath.left(URL_SCHEME_HTTP.length()) == URL_SCHEME_HTTP || testScriptPath.left(URL_SCHEME_FTP.length()) == URL_SCHEME_FTP) { + if (testScriptPath.left(URL_SCHEME_HTTP.length()) == URL_SCHEME_HTTP || + testScriptPath.left(URL_SCHEME_FTP.length()) == URL_SCHEME_FTP) { + setProperty(hifi::properties::TEST, QUrl::fromUserInput(testScriptPath)); } else if (QFileInfo(testScriptPath).exists()) { setProperty(hifi::properties::TEST, QUrl::fromLocalFile(testScriptPath)); } - // quite when finished parameter must directly follow the test script + // quite when finished parameter must directly follow the test script if ((i + 2) < args.size() && args.at(i + 2) == TEST_QUIT_WHEN_FINISHED_OPTION) { quitWhenFinished = true; } - } else if (args.at(i) == TEST_SNAPSHOT_LOCATION_COMMAND) { + } else if (args.at(i) == TEST_RESULTS_LOCATION_COMMAND) { // Set test snapshot location only if it is a writeable directory - QString pathname(args.at(i + 1)); - QFileInfo fileInfo(pathname); + QString path(args.at(i + 1)); + + QFileInfo fileInfo(path); if (fileInfo.isDir() && fileInfo.isWritable()) { - testSnapshotLocation = pathname; + testResultsLocation = path; } } } @@ -7475,7 +7478,9 @@ void Application::loadAvatarBrowser() const { void Application::takeSnapshot(bool notify, bool includeAnimated, float aspectRatio, const QString& filename) { postLambdaEvent([notify, includeAnimated, aspectRatio, filename, this] { // Get a screenshot and save it - QString path = Snapshot::saveSnapshot(getActiveDisplayPlugin()->getScreenshot(aspectRatio), filename, testSnapshotLocation); + QString path = + Snapshot::saveSnapshot(getActiveDisplayPlugin()->getScreenshot(aspectRatio), filename, testResultsLocation); + // If we're not doing an animated snapshot as well... if (!includeAnimated) { // Tell the dependency manager that the capture of the still snapshot has taken place. @@ -7489,7 +7494,9 @@ void Application::takeSnapshot(bool notify, bool includeAnimated, float aspectRa void Application::takeSecondaryCameraSnapshot(const QString& filename) { postLambdaEvent([filename, this] { - QString snapshotPath = Snapshot::saveSnapshot(getActiveDisplayPlugin()->getSecondaryCameraScreenshot(), filename, testSnapshotLocation); + QString snapshotPath = + Snapshot::saveSnapshot(getActiveDisplayPlugin()->getSecondaryCameraScreenshot(), filename, testResultsLocation); + emit DependencyManager::get()->stillSnapshotTaken(snapshotPath, true); }); } diff --git a/interface/src/Application.h b/interface/src/Application.h index aa6469c592..3189b87775 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -418,6 +418,7 @@ public slots: void updateVerboseLogging(); Q_INVOKABLE void openAndroidActivity(const QString& activityName); + QString getTestResultsLocation() { return testResultsLocation; }; private slots: void onDesktopRootItemCreated(QQuickItem* qmlContext); @@ -751,7 +752,7 @@ private: std::atomic _pendingIdleEvent { true }; std::atomic _pendingRenderEvent { true }; - QString testSnapshotLocation; + QString testResultsLocation; bool quitWhenFinished { false }; }; #endif // hifi_Application_h diff --git a/interface/src/scripting/TestScriptingInterface.cpp b/interface/src/scripting/TestScriptingInterface.cpp index 9e7c0e142e..15a024fa03 100644 --- a/interface/src/scripting/TestScriptingInterface.cpp +++ b/interface/src/scripting/TestScriptingInterface.cpp @@ -160,3 +160,30 @@ void TestScriptingInterface::clearCaches() { qApp->reloadResourceCaches(); } +// Writes a JSON object from javascript to a file +void TestScriptingInterface::saveObject(QVariant variant, const QString& filename) { + QString testResultsLocation = qApp->getTestResultsLocation(); + if (testResultsLocation.isNull()) { + return; + } + + QJsonDocument jsonDocument; + jsonDocument = QJsonDocument::fromVariant(variant); + if (jsonDocument.isNull()) { + return; + } + + QByteArray jsonData = jsonDocument.toJson(); + + // Append trailing slash if needed + if (testResultsLocation.right(1) != "/") { + testResultsLocation += "/"; + } + + QString filepath = QDir::cleanPath(testResultsLocation + filename); + QFile file(filepath); + + file.open(QFile::WriteOnly); + file.write(jsonData); + file.close(); +} diff --git a/interface/src/scripting/TestScriptingInterface.h b/interface/src/scripting/TestScriptingInterface.h index 687cb41689..4b469244d1 100644 --- a/interface/src/scripting/TestScriptingInterface.h +++ b/interface/src/scripting/TestScriptingInterface.h @@ -83,6 +83,11 @@ public slots: */ void clearCaches(); + /**jsdoc + * Save a JSON object to a file in the test results location + */ + void saveObject(QVariant v, const QString& filename); + private: bool waitForCondition(qint64 maxWaitMs, std::function condition); }; From d4d2f3c9573f375a8af51886a8e468a0c9500386 Mon Sep 17 00:00:00 2001 From: NissimHadar Date: Mon, 14 May 2018 16:32:04 -0700 Subject: [PATCH 2/2] Moved ownership of test location into the Test Scripting class. --- interface/src/Application.cpp | 10 +++++----- interface/src/Application.h | 3 --- interface/src/scripting/TestScriptingInterface.cpp | 9 ++++----- interface/src/scripting/TestScriptingInterface.h | 8 ++++++-- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 08550a81f0..61ed5acdd2 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1033,7 +1033,7 @@ Application::Application(int& argc, char** argv, QElapsedTimer& startupTimer, bo QFileInfo fileInfo(path); if (fileInfo.isDir() && fileInfo.isWritable()) { - testResultsLocation = path; + TestScriptingInterface::getInstance()->setTestResultsLocation(path); } } } @@ -7591,8 +7591,8 @@ void Application::loadAvatarBrowser() const { void Application::takeSnapshot(bool notify, bool includeAnimated, float aspectRatio, const QString& filename) { postLambdaEvent([notify, includeAnimated, aspectRatio, filename, this] { // Get a screenshot and save it - QString path = - Snapshot::saveSnapshot(getActiveDisplayPlugin()->getScreenshot(aspectRatio), filename, testResultsLocation); + QString path = Snapshot::saveSnapshot(getActiveDisplayPlugin()->getScreenshot(aspectRatio), filename, + TestScriptingInterface::getInstance()->getTestResultsLocation()); // If we're not doing an animated snapshot as well... if (!includeAnimated) { @@ -7607,8 +7607,8 @@ void Application::takeSnapshot(bool notify, bool includeAnimated, float aspectRa void Application::takeSecondaryCameraSnapshot(const QString& filename) { postLambdaEvent([filename, this] { - QString snapshotPath = - Snapshot::saveSnapshot(getActiveDisplayPlugin()->getSecondaryCameraScreenshot(), filename, testResultsLocation); + QString snapshotPath = Snapshot::saveSnapshot(getActiveDisplayPlugin()->getSecondaryCameraScreenshot(), filename, + TestScriptingInterface::getInstance()->getTestResultsLocation()); emit DependencyManager::get()->stillSnapshotTaken(snapshotPath, true); }); diff --git a/interface/src/Application.h b/interface/src/Application.h index d61e986e55..17e28f0e6e 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -419,8 +419,6 @@ public slots: void updateVerboseLogging(); Q_INVOKABLE void openAndroidActivity(const QString& activityName); - QString getTestResultsLocation() { return testResultsLocation; }; - private slots: void onDesktopRootItemCreated(QQuickItem* qmlContext); void onDesktopRootContextCreated(QQmlContext* qmlContext); @@ -754,7 +752,6 @@ private: std::atomic _pendingIdleEvent { true }; std::atomic _pendingRenderEvent { true }; - QString testResultsLocation; bool quitWhenFinished { false }; }; #endif // hifi_Application_h diff --git a/interface/src/scripting/TestScriptingInterface.cpp b/interface/src/scripting/TestScriptingInterface.cpp index 15a024fa03..700994c517 100644 --- a/interface/src/scripting/TestScriptingInterface.cpp +++ b/interface/src/scripting/TestScriptingInterface.cpp @@ -162,8 +162,7 @@ void TestScriptingInterface::clearCaches() { // Writes a JSON object from javascript to a file void TestScriptingInterface::saveObject(QVariant variant, const QString& filename) { - QString testResultsLocation = qApp->getTestResultsLocation(); - if (testResultsLocation.isNull()) { + if (_testResultsLocation.isNull()) { return; } @@ -176,11 +175,11 @@ void TestScriptingInterface::saveObject(QVariant variant, const QString& filenam QByteArray jsonData = jsonDocument.toJson(); // Append trailing slash if needed - if (testResultsLocation.right(1) != "/") { - testResultsLocation += "/"; + if (_testResultsLocation.right(1) != "/") { + _testResultsLocation += "/"; } - QString filepath = QDir::cleanPath(testResultsLocation + filename); + QString filepath = QDir::cleanPath(_testResultsLocation + filename); QFile file(filepath); file.open(QFile::WriteOnly); diff --git a/interface/src/scripting/TestScriptingInterface.h b/interface/src/scripting/TestScriptingInterface.h index 4b469244d1..5666417727 100644 --- a/interface/src/scripting/TestScriptingInterface.h +++ b/interface/src/scripting/TestScriptingInterface.h @@ -18,6 +18,10 @@ class QScriptValue; class TestScriptingInterface : public QObject { Q_OBJECT +public: + void setTestResultsLocation(const QString path) { _testResultsLocation = path; } + const QString& getTestResultsLocation() { return _testResultsLocation; }; + public slots: static TestScriptingInterface* getInstance(); @@ -46,7 +50,6 @@ public slots: */ void waitIdle(); - bool waitForConnection(qint64 maxWaitMs = 10000); void wait(int milliseconds); @@ -90,6 +93,7 @@ public slots: private: bool waitForCondition(qint64 maxWaitMs, std::function condition); + QString _testResultsLocation; }; -#endif // hifi_TestScriptingInterface_h +#endif // hifi_TestScriptingInterface_h