| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #include <qtest.h> |
| 30 | #include <QtCore/qdir.h> |
| 31 | #include <QtCore/qfile.h> |
| 32 | #include <QtCore/qtextstream.h> |
| 33 | #include <QtScript/qscriptengine.h> |
| 34 | #include <QtScript/qscriptvalue.h> |
| 35 | #include "context2d.h" |
| 36 | #include "environment.h" |
| 37 | #include "qcontext2dcanvas.h" |
| 38 | |
| 39 | static QString readFile(const QString &filename) |
| 40 | { |
| 41 | QFile file(filename); |
| 42 | if (!file.open(QFile::ReadOnly)) |
| 43 | return QString(); |
| 44 | QTextStream stream(&file); |
| 45 | stream.setCodec("UTF-8"); |
| 46 | return stream.readAll(); |
| 47 | } |
| 48 | |
| 49 | class tst_Context2D : public QObject |
| 50 | { |
| 51 | Q_OBJECT |
| 52 | |
| 53 | public: |
| 54 | tst_Context2D(); |
| 55 | ~tst_Context2D(); |
| 56 | |
| 57 | private slots: |
| 58 | void singleExecution_data(); |
| 59 | void singleExecution(); |
| 60 | void repeatedExecution_data(); |
| 61 | void repeatedExecution(); |
| 62 | |
| 63 | private: |
| 64 | void newEnvironment(); |
| 65 | |
| 66 | private: |
| 67 | QDir testsDir; |
| 68 | Environment *m_env; |
| 69 | QContext2DCanvas *m_canvas; |
| 70 | }; |
| 71 | |
| 72 | tst_Context2D::tst_Context2D() |
| 73 | : m_env(0), m_canvas(0) |
| 74 | { |
| 75 | testsDir = QDir(":/scripts"); |
| 76 | if (!testsDir.exists()) |
| 77 | qWarning("*** no scripts/ dir!"); |
| 78 | } |
| 79 | |
| 80 | tst_Context2D::~tst_Context2D() |
| 81 | { |
| 82 | delete m_canvas; |
| 83 | delete m_env; |
| 84 | } |
| 85 | |
| 86 | void tst_Context2D::newEnvironment() |
| 87 | { |
| 88 | delete m_canvas; |
| 89 | delete m_env; |
| 90 | m_env = new Environment(); |
| 91 | Context2D *context = new Context2D(m_env); |
| 92 | context->setSize(150, 150); // Hard-coded in many of the scripts. |
| 93 | m_canvas = new QContext2DCanvas(context, m_env); |
| 94 | m_canvas->setFixedSize(context->size()); |
| 95 | m_canvas->setObjectName("tutorial"); // Acts as the DOM element ID. |
| 96 | m_env->addCanvas(m_canvas); |
| 97 | } |
| 98 | |
| 99 | void tst_Context2D::singleExecution_data() |
| 100 | { |
| 101 | QTest::addColumn<QString>("testName"); |
| 102 | const QFileInfoList testFileInfos = testsDir.entryInfoList(QStringList() << "*.js", QDir::Files); |
| 103 | for (const QFileInfo &tfi : testFileInfos) { |
| 104 | QString name = tfi.baseName(); |
| 105 | QTest::newRow(name.toLatin1().constData()) << name; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void tst_Context2D::singleExecution() |
| 110 | { |
| 111 | QFETCH(QString, testName); |
| 112 | QString script = readFile(testsDir.absoluteFilePath(testName + ".js")); |
| 113 | QVERIFY(!script.isEmpty()); |
| 114 | |
| 115 | newEnvironment(); |
| 116 | QBENCHMARK { |
| 117 | m_env->evaluate(script, testName); |
| 118 | // Some of the scripts (e.g. plasma.js) merely start a timer and do |
| 119 | // the actual drawing in the timer event. Trigger the timers now to |
| 120 | // ensure that the real work is done. |
| 121 | m_env->triggerTimers(); |
| 122 | } |
| 123 | QVERIFY(!m_env->engine()->hasUncaughtException()); |
| 124 | } |
| 125 | |
| 126 | void tst_Context2D::repeatedExecution_data() |
| 127 | { |
| 128 | // We look for scripts that register an interval timer. |
| 129 | // Such scripts run a function every n milliseconds to update the canvas. |
| 130 | // The benchmark will execute this function repeatedly, which can allow |
| 131 | // us to observe potential effects of profiling-based JIT optimizations. |
| 132 | QTest::addColumn<QString>("testName"); |
| 133 | QTest::addColumn<QString>("script"); |
| 134 | const QFileInfoList testFileInfos = testsDir.entryInfoList(QStringList() << "*.js", QDir::Files); |
| 135 | for (const QFileInfo &tfi : testFileInfos) { |
| 136 | QString script = readFile(tfi.absoluteFilePath()); |
| 137 | QString name = tfi.baseName(); |
| 138 | newEnvironment(); |
| 139 | m_env->evaluate(script, name); |
| 140 | if (m_env->engine()->hasUncaughtException()) |
| 141 | continue; |
| 142 | if (m_env->hasIntervalTimers()) |
| 143 | QTest::newRow(name.toLatin1().constData()) << name << script; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void tst_Context2D::repeatedExecution() |
| 148 | { |
| 149 | QFETCH(QString, testName); |
| 150 | QFETCH(QString, script); |
| 151 | |
| 152 | newEnvironment(); |
| 153 | m_env->evaluate(script, testName); |
| 154 | QBENCHMARK { |
| 155 | // Trigger the update function repeatedly, effectively |
| 156 | // performing several frames of animation. |
| 157 | for (int i = 0; i < 16; ++i) |
| 158 | m_env->triggerTimers(); |
| 159 | } |
| 160 | QVERIFY(!m_env->engine()->hasUncaughtException()); |
| 161 | } |
| 162 | |
| 163 | QTEST_MAIN(tst_Context2D) |
| 164 | #include "tst_context2d.moc" |