130 lines · 3.6 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 "Application.h" TestScriptingInterface* TestScriptingInterface::getInstance() {    static TestScriptingInterface sharedInstance;    return &sharedInstance;} void TestScriptingInterface::quit() {    qApp->quit();} void TestScriptingInterface::waitForTextureIdle() {    waitForCondition(0, []()->bool {        return (0 == gpu::Context::getTextureGPUTransferCount());    });} 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) {    // FIXME implement    //    qApp->postLambdaEvent([isClient] {    //        auto tree = qApp->getEntityClipboard();    //        tree->setIsClient(isClient);    //    });    /*    Test.setClientTree(false);    Resources.overrideUrlPrefix("atp:/", TEST_BINARY_ROOT + scene + ".atp/");    if (!Clipboard.importEntities(TEST_SCENES_ROOT + scene + ".json")) {    return false;    }    var position = { x: 0, y: 0, z: 0 };    var pastedEntityIDs = Clipboard.pasteEntities(position);    for (var id in pastedEntityIDs) {    print("QQQ ID imported " + id);    }    */    return false;} 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();}