| 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 | |
| 30 | #include <QtTest/QtTest> |
| 31 | |
| 32 | #include <QtScript/qscriptengine.h> |
| 33 | |
| 34 | class tst_QScriptExtensionPlugin : public QObject |
| 35 | { |
| 36 | Q_OBJECT |
| 37 | |
| 38 | public: |
| 39 | tst_QScriptExtensionPlugin(); |
| 40 | |
| 41 | private slots: |
| 42 | void initTestCase(); |
| 43 | void importSimplePlugin(); |
| 44 | void importStaticPlugin(); |
| 45 | |
| 46 | private: |
| 47 | const QString m_pluginsDirectory; |
| 48 | |
| 49 | }; |
| 50 | |
| 51 | tst_QScriptExtensionPlugin::tst_QScriptExtensionPlugin() : |
| 52 | m_pluginsDirectory(QFINDTESTDATA("plugins" )) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | void tst_QScriptExtensionPlugin::initTestCase() |
| 57 | { |
| 58 | QVERIFY2(!m_pluginsDirectory.isEmpty(), "'plugins' directory not found" ); |
| 59 | } |
| 60 | |
| 61 | void tst_QScriptExtensionPlugin::importSimplePlugin() |
| 62 | { |
| 63 | QScriptEngine eng; |
| 64 | QCoreApplication::addLibraryPath(m_pluginsDirectory); |
| 65 | |
| 66 | QVERIFY(eng.importedExtensions().isEmpty()); |
| 67 | |
| 68 | QStringList available = eng.availableExtensions(); |
| 69 | QVERIFY(available.contains("simple" )); |
| 70 | QVERIFY(available.contains("simple.foo" )); |
| 71 | QVERIFY(available.contains("simple.foo.bar" )); |
| 72 | |
| 73 | QScriptValue extensionObject; |
| 74 | { |
| 75 | QVERIFY(eng.importExtension("simple" ).isUndefined()); |
| 76 | QCOMPARE(eng.importedExtensions().size(), 1); |
| 77 | QCOMPARE(eng.importedExtensions().at(0), QString::fromLatin1("simple" )); |
| 78 | QVERIFY(eng.availableExtensions().contains("simple" )); |
| 79 | QVERIFY(eng.globalObject().property("pluginKey" ).equals("simple" )); |
| 80 | QVERIFY(eng.globalObject().property("package" ).isObject()); |
| 81 | extensionObject = eng.globalObject().property(name: "simple" ); |
| 82 | QVERIFY(extensionObject.isObject()); |
| 83 | QVERIFY(extensionObject.equals(eng.globalObject().property("package" ))); |
| 84 | } |
| 85 | |
| 86 | { |
| 87 | QVERIFY(eng.importExtension("simple.foo" ).isUndefined()); |
| 88 | QCOMPARE(eng.importedExtensions().size(), 2); |
| 89 | QCOMPARE(eng.importedExtensions().at(1), QString::fromLatin1("simple.foo" )); |
| 90 | QVERIFY(eng.availableExtensions().contains("simple.foo" )); |
| 91 | QVERIFY(eng.globalObject().property("pluginKey" ).equals("simple.foo" )); |
| 92 | QVERIFY(eng.globalObject().property("package" ).isObject()); |
| 93 | QVERIFY(!extensionObject.equals(eng.globalObject().property("package" ))); |
| 94 | QVERIFY(extensionObject.equals(eng.globalObject().property("simple" ))); |
| 95 | QVERIFY(extensionObject.property("foo" ).isObject()); |
| 96 | QVERIFY(extensionObject.property("foo" ).equals(eng.globalObject().property("package" ))); |
| 97 | } |
| 98 | |
| 99 | { |
| 100 | QVERIFY(eng.importExtension("simple.foo.bar" ).isUndefined()); |
| 101 | QCOMPARE(eng.importedExtensions().size(), 3); |
| 102 | QCOMPARE(eng.importedExtensions().at(2), QString::fromLatin1("simple.foo.bar" )); |
| 103 | QVERIFY(eng.availableExtensions().contains("simple.foo.bar" )); |
| 104 | QVERIFY(eng.globalObject().property("pluginKey" ).equals("simple.foo.bar" )); |
| 105 | QVERIFY(eng.globalObject().property("package" ).isObject()); |
| 106 | QVERIFY(!extensionObject.equals(eng.globalObject().property("package" ))); |
| 107 | QVERIFY(extensionObject.equals(eng.globalObject().property("simple" ))); |
| 108 | QVERIFY(extensionObject.property("foo" ).property("bar" ).isObject()); |
| 109 | QVERIFY(extensionObject.property("foo" ).property("bar" ).equals(eng.globalObject().property("package" ))); |
| 110 | } |
| 111 | |
| 112 | // Extensions can't be imported multiple times. |
| 113 | eng.globalObject().setProperty(name: "pluginKey" , value: QScriptValue()); |
| 114 | QVERIFY(eng.importExtension("simple" ).isUndefined()); |
| 115 | QCOMPARE(eng.importedExtensions().size(), 3); |
| 116 | QVERIFY(!eng.globalObject().property("pluginKey" ).isValid()); |
| 117 | |
| 118 | QVERIFY(eng.importExtension("simple.foo" ).isUndefined()); |
| 119 | QCOMPARE(eng.importedExtensions().size(), 3); |
| 120 | QVERIFY(!eng.globalObject().property("pluginKey" ).isValid()); |
| 121 | |
| 122 | QVERIFY(eng.importExtension("simple.foo.bar" ).isUndefined()); |
| 123 | QCOMPARE(eng.importedExtensions().size(), 3); |
| 124 | QVERIFY(!eng.globalObject().property("pluginKey" ).isValid()); |
| 125 | } |
| 126 | |
| 127 | void tst_QScriptExtensionPlugin::importStaticPlugin() |
| 128 | { |
| 129 | Q_INIT_RESOURCE(staticplugin); |
| 130 | QScriptEngine eng; |
| 131 | QVERIFY(eng.availableExtensions().contains("static" )); |
| 132 | QVERIFY(eng.importExtension("static" ).isUndefined()); |
| 133 | QCOMPARE(eng.importedExtensions().size(), 1); |
| 134 | QCOMPARE(eng.importedExtensions().at(0), QString::fromLatin1("static" )); |
| 135 | QVERIFY(eng.availableExtensions().contains("static" )); |
| 136 | QVERIFY(eng.globalObject().property("pluginKey" ).equals("static" )); |
| 137 | |
| 138 | // Verify that :/qtscriptextension/static/__init__.js was evaluated. |
| 139 | QVERIFY(eng.evaluate("spy" ).isObject()); |
| 140 | QVERIFY(eng.evaluate("spy.extension" ).equals("static" )); |
| 141 | QVERIFY(eng.evaluate("spy.setupPackage" ).isFunction()); |
| 142 | QVERIFY(eng.evaluate("spy.postInit" ).isUndefined()); |
| 143 | |
| 144 | QVERIFY(eng.evaluate("postInitWasCalled" ).isBool()); |
| 145 | QVERIFY(eng.evaluate("postInitWasCalled" ).toBool()); |
| 146 | |
| 147 | // Extensions can't be imported multiple times. |
| 148 | eng.globalObject().setProperty(name: "pluginKey" , value: QScriptValue()); |
| 149 | QVERIFY(eng.importExtension("static" ).isUndefined()); |
| 150 | QCOMPARE(eng.importedExtensions().size(), 1); |
| 151 | QVERIFY(!eng.globalObject().property("pluginKey" ).isValid()); |
| 152 | } |
| 153 | |
| 154 | Q_IMPORT_PLUGIN(StaticPlugin) |
| 155 | |
| 156 | QTEST_MAIN(tst_QScriptExtensionPlugin) |
| 157 | #include "tst_qscriptextensionplugin.moc" |
| 158 | |