| 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 QtQml module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 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 Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | #include <QString> |
| 40 | #include <QtTest> |
| 41 | #include <QQmlEngine> |
| 42 | #include <QQmlComponent> |
| 43 | #include <QDebug> |
| 44 | #include <QScopedPointer> |
| 45 | #include "../../shared/util.h" |
| 46 | |
| 47 | class tst_qqmlpromise : public QQmlDataTest |
| 48 | { |
| 49 | Q_OBJECT |
| 50 | |
| 51 | public: |
| 52 | tst_qqmlpromise() {} |
| 53 | |
| 54 | private slots: |
| 55 | void promise_all_empty_input(); |
| 56 | void promise_all_noniterable_input(); |
| 57 | // void promise_all_invoke_then_method(); |
| 58 | void promise_all_resolve(); |
| 59 | void promise_all_reject_reject_is_last(); |
| 60 | void promise_all_reject_reject_is_mid(); |
| 61 | void promise_get_length(); |
| 62 | void promise_executor_function_extensible(); |
| 63 | void promise_executor_reject(); |
| 64 | void promise_executor_resolve(); |
| 65 | void promise_executor_throw_exception(); |
| 66 | void promise_async_resolve_with_value(); |
| 67 | void promise_async_reject_with_value(); |
| 68 | void promise_resolve_with_value(); |
| 69 | void promise_resolve_function_length(); |
| 70 | void promise_resolve_is_a_function(); |
| 71 | void promise_resolve_with_array(); |
| 72 | void promise_resolve_with_empty(); |
| 73 | void promise_resolve_with_promise(); |
| 74 | void promise_race_empty_input(); |
| 75 | void promise_race_resolve_1st_in_executor_function(); |
| 76 | void promise_race_resolve_1st(); |
| 77 | void promise_race_resolve_2nd(); |
| 78 | void promise_reject_with_value(); |
| 79 | void promise_reject_catch(); |
| 80 | void then_resolve_chaining(); |
| 81 | void then_reject_chaining(); |
| 82 | void then_fulfilled_non_callable(); |
| 83 | void then_reject_non_callable(); |
| 84 | void then_resolve_multiple_then(); |
| 85 | void promiseChain(); |
| 86 | void promiseHandlerThrows(); |
| 87 | |
| 88 | private: |
| 89 | void execute_test(QString testName); |
| 90 | }; |
| 91 | |
| 92 | void tst_qqmlpromise::promise_all_empty_input() |
| 93 | { |
| 94 | execute_test(testName: "promise-all-empty-input.qml" ); |
| 95 | } |
| 96 | |
| 97 | void tst_qqmlpromise::promise_all_noniterable_input() |
| 98 | { |
| 99 | execute_test(testName: "promise-all-noniterable-input.qml" ); |
| 100 | } |
| 101 | |
| 102 | // TODO: Fix the test |
| 103 | //void tst_qqmlpromise::promise_all_invoke_then_method() |
| 104 | //{ |
| 105 | // execute_test("promise-all-invoke-then-method.qml"); |
| 106 | //} |
| 107 | |
| 108 | void tst_qqmlpromise::promise_all_resolve() |
| 109 | { |
| 110 | execute_test(testName: "promise-all-resolve.qml" ); |
| 111 | } |
| 112 | |
| 113 | void tst_qqmlpromise::promise_all_reject_reject_is_last() |
| 114 | { |
| 115 | execute_test(testName: "promise-all-reject-reject-is-last.qml" ); |
| 116 | } |
| 117 | |
| 118 | void tst_qqmlpromise::promise_all_reject_reject_is_mid() |
| 119 | { |
| 120 | execute_test(testName: "promise-all-reject-reject-is-mid.qml" ); |
| 121 | } |
| 122 | |
| 123 | void tst_qqmlpromise::promise_get_length() |
| 124 | { |
| 125 | execute_test(testName: "promise-get-length.qml" ); |
| 126 | } |
| 127 | |
| 128 | void tst_qqmlpromise::promise_executor_resolve() |
| 129 | { |
| 130 | QQmlEngine engine; |
| 131 | QQmlComponent component(&engine, testFileUrl(fileName: "promise-executor-resolve.qml" )); |
| 132 | QScopedPointer<QObject> object(component.beginCreate(engine.rootContext())); |
| 133 | QVERIFY(!object.isNull()); |
| 134 | component.completeCreate(); |
| 135 | |
| 136 | QTRY_COMPARE(object->property("wasExecutorCalled" ).toBool(), true); |
| 137 | QTRY_COMPARE(object->property("wasPromiseResolved" ).toBool(), true); |
| 138 | // TODO: now "object" type is returned. fix |
| 139 | // QCOMPARE(object->property("wasPromiseTypeReturnedByThen").toBool(), true); |
| 140 | QTRY_COMPARE(object->property("wasResolutionForwardedCorrectly" ).toBool(), true); |
| 141 | QTRY_COMPARE(object->property("wasNewPromiseObjectCreatedByThen" ).toBool(), true); |
| 142 | } |
| 143 | |
| 144 | void tst_qqmlpromise::promise_executor_throw_exception() |
| 145 | { |
| 146 | execute_test(testName: "promise-executor-throw-exception.qml" ); |
| 147 | } |
| 148 | |
| 149 | void tst_qqmlpromise::promise_async_resolve_with_value() |
| 150 | { |
| 151 | execute_test(testName: "promise-async-resolve-with-value.qml" ); |
| 152 | } |
| 153 | |
| 154 | void tst_qqmlpromise::promise_async_reject_with_value() |
| 155 | { |
| 156 | execute_test(testName: "promise-async-reject-with-value.qml" ); |
| 157 | } |
| 158 | |
| 159 | void tst_qqmlpromise::promise_resolve_with_value() |
| 160 | { |
| 161 | execute_test(testName: "promise-resolve-with-value.qml" ); |
| 162 | } |
| 163 | |
| 164 | void tst_qqmlpromise::promise_resolve_function_length() |
| 165 | { |
| 166 | execute_test(testName: "promise-resolve-function-length.qml" ); |
| 167 | } |
| 168 | |
| 169 | void tst_qqmlpromise::promise_resolve_is_a_function() |
| 170 | { |
| 171 | execute_test(testName: "promise-resolve-is-a-function.qml" ); |
| 172 | } |
| 173 | |
| 174 | void tst_qqmlpromise::promise_resolve_with_array() |
| 175 | { |
| 176 | execute_test(testName: "promise-resolve-with-array.qml" ); |
| 177 | } |
| 178 | |
| 179 | void tst_qqmlpromise::promise_resolve_with_empty() |
| 180 | { |
| 181 | execute_test(testName: "promise-resolve-with-empty.qml" ); |
| 182 | } |
| 183 | |
| 184 | void tst_qqmlpromise::promise_resolve_with_promise() |
| 185 | { |
| 186 | execute_test(testName: "promise-resolve-with-promise.qml" ); |
| 187 | } |
| 188 | |
| 189 | void tst_qqmlpromise::promise_race_empty_input() |
| 190 | { |
| 191 | execute_test(testName: "promise-race-empty-input.qml" ); |
| 192 | } |
| 193 | |
| 194 | void tst_qqmlpromise::promise_race_resolve_1st_in_executor_function() |
| 195 | { |
| 196 | execute_test(testName: "promise-race-resolve-1st-in-executor-function.qml" ); |
| 197 | } |
| 198 | |
| 199 | void tst_qqmlpromise::promise_race_resolve_1st() |
| 200 | { |
| 201 | execute_test(testName: "promise-race-resolve-1st.qml" ); |
| 202 | } |
| 203 | |
| 204 | void tst_qqmlpromise::promise_race_resolve_2nd() |
| 205 | { |
| 206 | execute_test(testName: "promise-race-resolve-2nd.qml" ); |
| 207 | } |
| 208 | |
| 209 | void tst_qqmlpromise::promise_reject_with_value() |
| 210 | { |
| 211 | execute_test(testName: "promise-reject-with-value.qml" ); |
| 212 | } |
| 213 | |
| 214 | void tst_qqmlpromise::promise_reject_catch() |
| 215 | { |
| 216 | execute_test(testName: "promise-reject-catch.qml" ); |
| 217 | } |
| 218 | |
| 219 | void tst_qqmlpromise::promise_executor_function_extensible() |
| 220 | { |
| 221 | execute_test(testName: "promise-executor-function-extensible.qml" ); |
| 222 | } |
| 223 | |
| 224 | void tst_qqmlpromise::promise_executor_reject() |
| 225 | { |
| 226 | QQmlEngine engine; |
| 227 | QQmlComponent component(&engine, testFileUrl(fileName: "promise-executor-reject.qml" )); |
| 228 | QScopedPointer<QObject> object(component.beginCreate(engine.rootContext())); |
| 229 | QVERIFY(!object.isNull()); |
| 230 | component.completeCreate(); |
| 231 | |
| 232 | QTRY_COMPARE(object->property("wasExecutorCalled" ).toBool(), true); |
| 233 | QTRY_COMPARE(object->property("wasPromiseRejected" ).toBool(), true); |
| 234 | // TODO: now "object" type is returned. fix |
| 235 | // QCOMPARE(object->property("wasPromiseTypeReturnedByThen").toBool(), true); |
| 236 | QTRY_COMPARE(object->property("wasResolutionForwardedCorrectly" ).toBool(), true); |
| 237 | } |
| 238 | |
| 239 | void tst_qqmlpromise::then_resolve_chaining() |
| 240 | { |
| 241 | execute_test(testName: "then-resolve-chaining.qml" ); |
| 242 | } |
| 243 | |
| 244 | void tst_qqmlpromise::then_reject_chaining() |
| 245 | { |
| 246 | execute_test(testName: "then-reject-chaining.qml" ); |
| 247 | } |
| 248 | |
| 249 | void tst_qqmlpromise::then_fulfilled_non_callable() |
| 250 | { |
| 251 | execute_test(testName: "then-fulfilled-non-callable.qml" ); |
| 252 | } |
| 253 | |
| 254 | void tst_qqmlpromise::then_reject_non_callable() |
| 255 | { |
| 256 | execute_test(testName: "then-reject-non-callable.qml" ); |
| 257 | } |
| 258 | |
| 259 | void tst_qqmlpromise::then_resolve_multiple_then() |
| 260 | { |
| 261 | execute_test(testName: "then-resolve-multiple-then.qml" ); |
| 262 | } |
| 263 | |
| 264 | void tst_qqmlpromise::execute_test(QString testName) |
| 265 | { |
| 266 | QQmlEngine engine; |
| 267 | QQmlComponent component(&engine, testFileUrl(fileName: testName)); |
| 268 | QScopedPointer<QObject> object(component.beginCreate(engine.rootContext())); |
| 269 | QVERIFY(!object.isNull()); |
| 270 | component.completeCreate(); |
| 271 | |
| 272 | QTRY_COMPARE(object->property("wasTestSuccessful" ).toBool(), true); |
| 273 | } |
| 274 | |
| 275 | void tst_qqmlpromise::promiseChain() |
| 276 | { |
| 277 | QQmlEngine engine; |
| 278 | QQmlComponent component(&engine, testFileUrl(fileName: "promisechain.qml" )); |
| 279 | QVERIFY(component.isReady()); |
| 280 | QTest::ignoreMessage(type: QtDebugMsg, message: "1" ); |
| 281 | QTest::ignoreMessage(type: QtDebugMsg, message: "2" ); |
| 282 | QTest::ignoreMessage(type: QtDebugMsg, message: "3" ); |
| 283 | QScopedPointer<QObject> root(component.create()); |
| 284 | QVERIFY(root); |
| 285 | QTRY_VERIFY(root->property("x" ) == 42); |
| 286 | |
| 287 | } |
| 288 | |
| 289 | void tst_qqmlpromise::promiseHandlerThrows() |
| 290 | { |
| 291 | QQmlEngine engine; |
| 292 | QQmlComponent component(&engine, testFileUrl(fileName: "promisehandlerthrows.qml" )); |
| 293 | QVERIFY(component.isReady()); |
| 294 | QTest::ignoreMessage(type: QtDebugMsg, message: "Rethrowing err" ); |
| 295 | QScopedPointer<QObject> root(component.create()); |
| 296 | QVERIFY(root); |
| 297 | QTRY_VERIFY(root->property("errorMessage" ) == QLatin1String("Some error" )); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | QTEST_MAIN(tst_qqmlpromise) |
| 302 | |
| 303 | #include "tst_qqmlpromise.moc" |
| 304 | |