tests/benchmarks/uimodels/GraphicsViewBenchmark/testautom/scriptrunner.cpp
changeset 3 41300fa6a67c
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     5 **
       
     6 ** This file is part of the examples of the Qt Toolkit.
       
     7 **
       
     8 ** $QT_BEGIN_LICENSE:LGPL$
       
     9 ** No Commercial Usage
       
    10 ** This file contains pre-release code and may not be distributed.
       
    11 ** You may use this file in accordance with the terms and conditions
       
    12 ** contained in the either Technology Preview License Agreement or the
       
    13 ** Beta Release License Agreement.
       
    14 **
       
    15 ** GNU Lesser General Public License Usage
       
    16 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    17 ** General Public License version 2.1 as published by the Free Software
       
    18 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    19 ** packaging of this file.  Please review the following information to
       
    20 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    22 **
       
    23 ** In addition, as a special exception, Nokia gives you certain
       
    24 ** additional rights. These rights are described in the Nokia Qt LGPL
       
    25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
       
    26 ** package.
       
    27 **
       
    28 ** GNU General Public License Usage
       
    29 ** Alternatively, this file may be used under the terms of the GNU
       
    30 ** General Public License version 3.0 as published by the Free Software
       
    31 ** Foundation and appearing in the file LICENSE.GPL included in the
       
    32 ** packaging of this file.  Please review the following information to
       
    33 ** ensure the GNU General Public License version 3.0 requirements will be
       
    34 ** met: http://www.gnu.org/copyleft/gpl.html.
       
    35 **
       
    36 ** If you are unsure which license is appropriate for your use, please
       
    37 ** contact the sales department at http://qt.nokia.com/contact.
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QDebug>
       
    43 #include <QScriptEngine>
       
    44 #include <QFile>
       
    45 #include <QStringList>
       
    46 
       
    47 #include "scriptrunner.h"
       
    48 #include "resultlogger.h"
       
    49 
       
    50 static const QString TestScriptName = ":/testscripts/testscript.js";
       
    51 
       
    52 Q_DECLARE_METATYPE(ScriptRunner*)
       
    53 Q_DECLARE_METATYPE(ResultLogger*)
       
    54 Q_DECLARE_METATYPE(TestFunctionResult*)
       
    55 Q_DECLARE_METATYPE(Benchmark*)
       
    56 Q_DECLARE_METATYPE(Benchmarks*)
       
    57 
       
    58 QScriptValue fileLoggerToScriptValue(QScriptEngine *engine, ResultLogger* const &in)
       
    59 {
       
    60     return engine->newQObject(in);
       
    61 }
       
    62 
       
    63 void fileLoggerFromScriptValue(const QScriptValue &object, ResultLogger* &out)
       
    64 {
       
    65     out = qobject_cast<ResultLogger*>(object.toQObject());
       
    66 }
       
    67 
       
    68 QScriptValue testFunctionResultToScriptValue(QScriptEngine *engine, TestFunctionResult* const &in)
       
    69 {
       
    70     return engine->newQObject(in);
       
    71 }
       
    72 
       
    73 void testFunctionResultFromScriptValue(const QScriptValue &object, TestFunctionResult* &out)
       
    74 {
       
    75     out = qobject_cast<TestFunctionResult*>(object.toQObject());
       
    76 }
       
    77 
       
    78 QScriptValue benchmarkToScriptValue(QScriptEngine *engine, Benchmark* const &in)
       
    79 {
       
    80     return engine->newQObject(in);
       
    81 }
       
    82 
       
    83 void benchmarkFromScriptValue(const QScriptValue &object, Benchmark* &out)
       
    84 {
       
    85     out = qobject_cast<Benchmark*>(object.toQObject());
       
    86 }
       
    87 
       
    88 QScriptValue benchmarksToScriptValue(QScriptEngine *engine, Benchmarks* const &in)
       
    89 {
       
    90     return engine->newQObject(in);
       
    91 }
       
    92 
       
    93 void benchmarksFromScriptValue(const QScriptValue &object, Benchmarks* &out)
       
    94 {
       
    95     out = qobject_cast<Benchmarks*>(object.toQObject());
       
    96 }
       
    97 
       
    98 QScriptValue scriptRunnerToScriptValue(QScriptEngine *engine, ScriptRunner* const &in)
       
    99 {
       
   100     return engine->newQObject(in);
       
   101 }
       
   102 
       
   103 void scriptRunnerFromScriptValue(const QScriptValue &object, ScriptRunner* &out)
       
   104 {
       
   105     out = qobject_cast<ScriptRunner*>(object.toQObject());
       
   106 }
       
   107 
       
   108 ScriptRunner::ScriptRunner(const QString &scriptName, TestController *ctrl, QObject *parent)
       
   109     : TestThread(ctrl, parent),
       
   110     m_engine(new QScriptEngine(this)),
       
   111     m_testScriptFileName(TestScriptName)
       
   112 {
       
   113     if (scriptName.length() > 0)
       
   114         m_testScriptFileName = scriptName;
       
   115 
       
   116     qScriptRegisterMetaType(m_engine,
       
   117                             scriptRunnerToScriptValue,
       
   118                             scriptRunnerFromScriptValue);
       
   119 
       
   120 
       
   121     qScriptRegisterMetaType(m_engine,
       
   122                             fileLoggerToScriptValue,
       
   123                             fileLoggerFromScriptValue);
       
   124 
       
   125     qScriptRegisterMetaType(m_engine,
       
   126                             testFunctionResultToScriptValue,
       
   127                             testFunctionResultFromScriptValue);
       
   128 
       
   129     qScriptRegisterMetaType(m_engine,
       
   130                             benchmarkToScriptValue,
       
   131                             benchmarkFromScriptValue);
       
   132 
       
   133     qScriptRegisterMetaType(m_engine,
       
   134                             benchmarksToScriptValue,
       
   135                             benchmarksFromScriptValue);
       
   136 
       
   137 
       
   138     if (!m_resultLogger) {
       
   139         m_resultLogger = new ResultLogger();
       
   140 
       
   141         m_logger = m_engine->newQObject(m_resultLogger, QScriptEngine::QtOwnership);
       
   142         m_engine->globalObject().setProperty("fileLogger", m_logger);
       
   143 
       
   144         m_resultLogger->setResultFileInformation(controller()->resultFileName(),
       
   145                                               controller()->resultFileFormat());
       
   146     }
       
   147 }
       
   148 
       
   149 
       
   150 ScriptRunner::~ScriptRunner()
       
   151 {
       
   152     if (m_engine->isEvaluating())
       
   153         m_engine->abortEvaluation();
       
   154 }
       
   155 
       
   156 void ScriptRunner::run()
       
   157 {
       
   158     if(!controller())
       
   159         return;
       
   160 
       
   161     if (m_doExit) return;
       
   162 
       
   163     tests();
       
   164 
       
   165     controller()->applicationExit();
       
   166 }
       
   167 
       
   168 void ScriptRunner::tests()
       
   169 {
       
   170     QFile file(m_testScriptFileName);
       
   171 
       
   172     if (m_doExit) return;
       
   173 
       
   174     TestFunctionResult *tf = m_resultLogger->getTestFunctionResult("initTestCase");
       
   175     if (!tf)
       
   176         tf = m_resultLogger->createTestFunctionResult("initTestCase");
       
   177 
       
   178     if(file.open(QIODevice::ReadOnly)) {
       
   179         QString scriptContent = file.readAll();
       
   180 
       
   181         if (scriptContent.length() <= 0) {
       
   182             tf->addError("Can't evaluate empty script from file: '" + m_testScriptFileName +"'");
       
   183             stop();
       
   184             return;
       
   185         }
       
   186 
       
   187         QScriptSyntaxCheckResult result = QScriptEngine::checkSyntax(scriptContent);
       
   188         if (result.state() != QScriptSyntaxCheckResult::Valid)
       
   189         {
       
   190             QString err = "Can't evaluate script content from file. Check syntax of script on file: '" + m_testScriptFileName +"'"
       
   191                        + " Error: " + result.errorMessage()
       
   192                        + " line: " + result.errorLineNumber()
       
   193                        + " column: " + result.errorColumnNumber();
       
   194             tf->addError(err);
       
   195             stop();
       
   196             return;
       
   197         }
       
   198         if (m_doExit) return;
       
   199 
       
   200         QScriptValue val = m_engine->evaluate(scriptContent, m_testScriptFileName);
       
   201         if(m_engine->hasUncaughtException()) {
       
   202             QString err = "Can't evaluate script content from file. Check syntax of script on file: '" + m_testScriptFileName + "'"
       
   203                        + " Error: " + m_engine->uncaughtExceptionBacktrace().join(" ")
       
   204                        + " at line " + m_engine->uncaughtExceptionLineNumber();
       
   205             tf->addError(err);
       
   206             stop();
       
   207             return;
       
   208         }
       
   209     }
       
   210     else {
       
   211         stop();
       
   212         tf->addError("Failed to read script from file '" + m_testScriptFileName +"'");
       
   213         return;
       
   214     }
       
   215 
       
   216     if (m_doExit) return;
       
   217 
       
   218     QScriptValue ctor = m_engine->evaluate("Tests");
       
   219     QScriptValue script = m_engine->newQObject(this, QScriptEngine::QtOwnership);
       
   220     QScriptValue scripttests = ctor.construct(QScriptValueList() << script);
       
   221     if(m_engine->hasUncaughtException()) {
       
   222         QString err = "Can't evaluate script content from file. Check syntax of script on file: '" + m_testScriptFileName +"'"
       
   223                  + " Error: " + m_engine->uncaughtExceptionBacktrace().join(" ");
       
   224         tf->addError(err);
       
   225         stop();
       
   226         return;
       
   227     }
       
   228 }