File indexing completed on 2024-04-28 04:55:54

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "qmltesthelper.h"
0019 #include "qml/qmlplugins.h"
0020 #include <QQmlComponent>
0021 #include <QDebug>
0022 
0023 QmlTestHelper::QmlTestHelper()
0024 {
0025     QmlPlugins::registerQmlTypes();
0026     qmlRegisterType<QmlTestItem>("org.kde.falkon.test", 1, 0, "TestItem");
0027     QQmlComponent component(&engine);
0028     component.setData("import org.kde.falkon 1.0 as Falkon\n"
0029                       "import org.kde.falkon.test 1.0 as FalkonTest\n"
0030                       "import QtQuick 2.7\n"
0031                       "FalkonTest.TestItem {"
0032                       "    evalFunc: function(source) {"
0033                       "        return eval(source);"
0034                       "    }"
0035                       "}"
0036                       , QUrl());
0037     testItem = qobject_cast<QmlTestItem*>(component.create());
0038     Q_ASSERT(testItem);
0039 }
0040 
0041 QJSValue QmlTestHelper::evaluate(const QString &source)
0042 {
0043     auto out = testItem->evaluate(source);
0044     if (out.isError()) {
0045         qWarning() << "Error:" << out.toString();
0046     }
0047     return out;
0048 }
0049 
0050 QObject *QmlTestHelper::evaluateQObject(const QString &source)
0051 {
0052     auto out = evaluate(source);
0053     if (out.isQObject()) {
0054         return out.toQObject();
0055     }
0056     return out.toVariant().value<QObject*>();
0057 }