142 lines · 4.3 KB · C++
RawDownload
////  Created by Bradley Austin Davis on 2016/12/12//  Copyright 2013-2016 High Fidelity, Inc.////  Distributed under the Apache License, Version 2.0.//  See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html//#include "TestScriptingInterface.h" #include <QtCore/QCoreApplication>#include <QtCore/QLoggingCategory>#include <QtCore/QThread> #include <DependencyManager.h>#include <Trace.h>#include <StatTracker.h>#include <OffscreenUi.h> #include "Application.h" Q_LOGGING_CATEGORY(trace_test, "trace.test") TestScriptingInterface* TestScriptingInterface::getInstance() {    static TestScriptingInterface sharedInstance;    return &sharedInstance;} void TestScriptingInterface::quit() {    qApp->quit();} void TestScriptingInterface::waitForTextureIdle() {    waitForCondition(0, []()->bool {        return (0 == gpu::Context::getTexturePendingGPUTransferCount());    });} void TestScriptingInterface::waitForDownloadIdle() {    waitForCondition(0, []()->bool {        return (0 == ResourceCache::getLoadingRequestCount()) && (0 == ResourceCache::getPendingRequestCount());    });} void TestScriptingInterface::waitForProcessingIdle() {    auto statTracker = DependencyManager::get<StatTracker>();    waitForCondition(0, [statTracker]()->bool {        return (0 == statTracker->getStat("Processing").toInt() && 0 == statTracker->getStat("PendingProcessing").toInt());    });} void TestScriptingInterface::waitIdle() {    // Initial wait for some incoming work    QThread::sleep(1);    waitForDownloadIdle();    waitForProcessingIdle();    waitForTextureIdle();} bool TestScriptingInterface::loadTestScene(QString scene) {    static const QString TEST_ROOT = "https://raw.githubusercontent.com/highfidelity/hifi_tests/master/";    static const QString TEST_BINARY_ROOT = "https://hifi-public.s3.amazonaws.com/test_scene_data/";    static const QString TEST_SCRIPTS_ROOT = TEST_ROOT + "scripts/";    static const QString TEST_SCENES_ROOT = TEST_ROOT + "scenes/";    return DependencyManager::get<OffscreenUi>()->returnFromUiThread([scene]()->QVariant {        ResourceManager::setUrlPrefixOverride("atp:/", TEST_BINARY_ROOT + scene + ".atp/");        auto tree = qApp->getEntities()->getTree();        auto treeIsClient = tree->getIsClient();        // Force the tree to accept the load regardless of permissions        tree->setIsClient(false);        auto result = tree->readFromURL(TEST_SCENES_ROOT + scene + ".json");        tree->setIsClient(treeIsClient);        return result;    }).toBool();} bool TestScriptingInterface::startTracing(QString logrules) {    if (!logrules.isEmpty()) {        QLoggingCategory::setFilterRules(logrules);    }     if (!DependencyManager::isSet<tracing::Tracer>()) {        return false;    }     DependencyManager::get<tracing::Tracer>()->startTracing();    return true;} bool TestScriptingInterface::stopTracing(QString filename) {    if (!DependencyManager::isSet<tracing::Tracer>()) {        return false;    }     auto tracer = DependencyManager::get<tracing::Tracer>();    tracer->stopTracing();    tracer->serialize(filename);    return true;} void TestScriptingInterface::clear() {    qApp->postLambdaEvent([] {        qApp->getEntities()->clear();    });} bool TestScriptingInterface::waitForConnection(qint64 maxWaitMs) {    // Wait for any previous connection to die    QThread::sleep(1);    return waitForCondition(maxWaitMs, []()->bool {        return DependencyManager::get<NodeList>()->getDomainHandler().isConnected();    });} void TestScriptingInterface::wait(int milliseconds) {    QThread::msleep(milliseconds);} bool TestScriptingInterface::waitForCondition(qint64 maxWaitMs, std::function<bool()> condition) {    QElapsedTimer elapsed;    elapsed.start();    while (!condition()) {        if (maxWaitMs > 0 && elapsed.elapsed() > maxWaitMs) {            return false;        }        QThread::msleep(1);    }    return condition();} void TestScriptingInterface::startTraceEvent(QString name) {    tracing::traceEvent(trace_test(), name, tracing::DurationBegin, "");} void TestScriptingInterface::endTraceEvent(QString name) {    tracing::traceEvent(trace_test(), name, tracing::DurationEnd);} void TestScriptingInterface::profileRange(const QString& name, QScriptValue fn) {    PROFILE_RANGE(script, name);    fn.call();}