| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Stephen Kelly <stephen.kelly@kdab.com> |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtGui module 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 <QtTest/QTest> |
| 30 | #include <QWidget> |
| 31 | #include <QScriptEngine> |
| 32 | |
| 33 | class CustomQObject : public QObject |
| 34 | { |
| 35 | Q_OBJECT |
| 36 | public: |
| 37 | CustomQObject(QObject *parent = 0) |
| 38 | : QObject(parent) |
| 39 | { |
| 40 | |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | class tst_QScriptQWidgets : public QObject |
| 45 | { |
| 46 | Q_OBJECT |
| 47 | public: |
| 48 | explicit tst_QScriptQWidgets(QObject *parent = 0); |
| 49 | virtual ~tst_QScriptQWidgets(); |
| 50 | |
| 51 | private slots: |
| 52 | void testProperty(); |
| 53 | void testSlot(); |
| 54 | }; |
| 55 | |
| 56 | tst_QScriptQWidgets::tst_QScriptQWidgets(QObject *parent) |
| 57 | : QObject(parent) |
| 58 | { |
| 59 | qRegisterMetaType<QWidget*>(); |
| 60 | qRegisterMetaType<CustomQObject*>(); |
| 61 | } |
| 62 | |
| 63 | tst_QScriptQWidgets::~tst_QScriptQWidgets() |
| 64 | { |
| 65 | |
| 66 | } |
| 67 | |
| 68 | class ObjectUnderTest : public QObject |
| 69 | { |
| 70 | Q_OBJECT |
| 71 | Q_PROPERTY(QWidget* widget READ widget CONSTANT) |
| 72 | Q_PROPERTY(CustomQObject* customObject READ customObject CONSTANT) |
| 73 | public: |
| 74 | ObjectUnderTest(QObject *parent = 0) |
| 75 | : QObject(parent), m_widget(new QWidget), m_customObject(new CustomQObject) |
| 76 | { |
| 77 | |
| 78 | } |
| 79 | |
| 80 | QWidget* widget() const |
| 81 | { |
| 82 | return m_widget.data(); |
| 83 | } |
| 84 | |
| 85 | CustomQObject* customObject() const |
| 86 | { |
| 87 | return m_customObject.data(); |
| 88 | } |
| 89 | |
| 90 | public slots: |
| 91 | QWidget* widgetAccessor() const |
| 92 | { |
| 93 | return m_widget.data(); |
| 94 | } |
| 95 | |
| 96 | QWidget* widgetReturner(QWidget* widget) |
| 97 | { |
| 98 | return widget; |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | QScopedPointer<QWidget> m_widget; |
| 103 | QScopedPointer<CustomQObject> m_customObject; |
| 104 | }; |
| 105 | |
| 106 | void tst_QScriptQWidgets::testProperty() |
| 107 | { |
| 108 | QScriptEngine engine; |
| 109 | ObjectUnderTest *testObject = new ObjectUnderTest(this); |
| 110 | QCOMPARE(engine.newQObject(testObject).property("widget" ).toQObject(), testObject->widget()); |
| 111 | |
| 112 | QCOMPARE(engine.newQObject(testObject).property("customObject" ).toQObject(), testObject->customObject()); |
| 113 | } |
| 114 | |
| 115 | void tst_QScriptQWidgets::testSlot() |
| 116 | { |
| 117 | { |
| 118 | QScriptEngine engine; |
| 119 | ObjectUnderTest *testObject = new ObjectUnderTest(this); |
| 120 | QCOMPARE(engine.newQObject(testObject).property("widgetAccessor" ).call(QScriptValue()).toQObject(), testObject->widget()); |
| 121 | } |
| 122 | { |
| 123 | QScriptEngine engine; |
| 124 | ObjectUnderTest *testObject = new ObjectUnderTest(this); |
| 125 | QCOMPARE(engine.newQObject(testObject).property("widgetReturner" ).call(QScriptValue(), QScriptValueList() << engine.toScriptValue(testObject->widget())).toQObject(), testObject->widget()); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | QTEST_MAIN(tst_QScriptQWidgets) |
| 130 | |
| 131 | #include "tst_qscriptqwidgets.moc" |
| 132 | |