| 1 |
/* |
| 2 |
* Copyright 2016 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <functional> |
| 18 |
|
| 19 |
#include <QtQml/QQmlEngine> |
| 20 |
#include <QtTest/QtTest> |
| 21 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 22 |
#include <LomiriToolkit-Qt6/private/asyncloader_p.h> |
| 23 |
#else |
| 24 |
#include <LomiriToolkit/private/asyncloader_p.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include "uctestcase.h" |
| 28 |
#include "uctestextras.h" |
| 29 |
|
| 30 |
UT_USE_NAMESPACE |
| 31 |
|
| 32 |
// Certain versions of Qt 5.15 and 6.0 will crash if we call QQmlIncubator::clear() |
| 33 |
// from QQmlIncubator::setInitialState(). This is reported as QTBUG-91519 and |
| 34 |
// promptly fixed in later versions. To avoid crashing on those versions, we worked |
| 35 |
// around it by preventing AsyncLoader from reset()'ing in Initializing state. |
| 36 |
// https://bugreports.qt.io/browse/QTBUG-91519 |
| 37 |
static constexpr bool WORKAROUND_QTBUG_91519 = |
| 38 |
#if defined(HAS_QTBUG_91519_FIXED) |
| 39 |
// In case people backported the fix to the problemetic version. |
| 40 |
false |
| 41 |
#elif QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) && QT_VERSION < QT_VERSION_CHECK(5, 15, 4) |
| 42 |
// The Qt 5.15.4 case is just in case, as we the open source users is |
| 43 |
// unlikely to encounter that version. |
| 44 |
true |
| 45 |
#elif QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) && QT_VERSION < QT_VERSION_CHECK(6, 0, 3) |
| 46 |
true |
| 47 |
#else |
| 48 |
// Qt 6.1.0+ are fixed. Qt 5.14 and below are not affected. |
| 49 |
false |
| 50 |
#endif |
| 51 |
; |
| 52 |
|
| 53 |
class LoaderSpy : public QObject |
| 54 |
{ |
| 55 |
Q_OBJECT |
| 56 |
public: |
| 57 |
AsyncLoader *m_loader; |
| 58 |
QScopedPointer<QObject> m_object; |
| 59 |
bool m_done = false; |
| 60 |
bool m_error = false; |
| 61 |
QList<AsyncLoader::LoadingStatus> m_statusList; |
| 62 |
public: |
| 63 |
LoaderSpy(AsyncLoader *loader) |
| 64 |
: QObject(0) |
| 65 |
, m_loader(loader) |
| 66 |
{ |
| 67 |
connect(loader, &AsyncLoader::loadingStatus, |
| 68 |
this, &LoaderSpy::onLoadingStatusChanged); |
| 69 |
} |
| 70 |
|
| 71 |
protected Q_SLOTS: |
| 72 |
virtual void onLoadingStatusChanged(UT_PREPEND_NAMESPACE(AsyncLoader)::LoadingStatus status, QObject *object) |
| 73 |
{ |
| 74 |
m_statusList << status; |
| 75 |
if (status == AsyncLoader::Ready) { |
| 76 |
m_done = true; |
| 77 |
m_object.reset(object); |
| 78 |
} |
| 79 |
if (status == AsyncLoader::Error) { |
| 80 |
m_error = true; |
| 81 |
} |
| 82 |
} |
| 83 |
}; |
| 84 |
|
| 85 |
class ResetLoaderSpy : public LoaderSpy |
| 86 |
{ |
| 87 |
Q_OBJECT |
| 88 |
public: |
| 89 |
bool m_reset = false; |
| 90 |
public: |
| 91 |
ResetLoaderSpy(AsyncLoader *loader) |
| 92 |
: LoaderSpy(loader) |
| 93 |
{ |
| 94 |
} |
| 95 |
|
| 96 |
void onLoadingStatusChanged(UT_PREPEND_NAMESPACE(AsyncLoader)::LoadingStatus status, QObject *object) override |
| 97 |
{ |
| 98 |
if (status == AsyncLoader::Loading) { |
| 99 |
m_loader->reset(); |
| 100 |
m_reset = true; |
| 101 |
} |
| 102 |
LoaderSpy::onLoadingStatusChanged(status, object); |
| 103 |
} |
| 104 |
}; |
| 105 |
|
| 106 |
class SecondLoaderSpy : public LoaderSpy |
| 107 |
{ |
| 108 |
Q_OBJECT |
| 109 |
public: |
| 110 |
bool m_loadResult = false; |
| 111 |
public: |
| 112 |
SecondLoaderSpy(AsyncLoader *loader, AsyncLoader::LoadingStatus loadAt, |
| 113 |
const QUrl &url, QQmlContext *context) |
| 114 |
: LoaderSpy(loader) |
| 115 |
, m_loadAt(loadAt) |
| 116 |
, m_url(url) |
| 117 |
, m_context(context) |
| 118 |
{ |
| 119 |
} |
| 120 |
|
| 121 |
void onLoadingStatusChanged(AsyncLoader::LoadingStatus status, QObject *object) override |
| 122 |
{ |
| 123 |
if (status == (AsyncLoader::LoadingStatus)m_loadAt) { |
| 124 |
m_loadAt = -1; |
| 125 |
delete object; |
| 126 |
object = nullptr; |
| 127 |
m_loadResult = m_loader->load(m_url, m_context); |
| 128 |
} |
| 129 |
LoaderSpy::onLoadingStatusChanged(status, object); |
| 130 |
} |
| 131 |
|
| 132 |
private: |
| 133 |
int m_loadAt; |
| 134 |
QUrl m_url; |
| 135 |
QQmlContext *m_context; |
| 136 |
}; |
| 137 |
|
| 138 |
/******************************************************************** |
| 139 |
* Test |
| 140 |
********************************************************************/ |
| 141 |
class tst_AsyncLoader : public QObject |
| 142 |
{ |
| 143 |
Q_OBJECT |
| 144 |
private Q_SLOTS: |
| 145 |
|
| 146 |
void test_load_data() |
| 147 |
{ |
| 148 |
QTest::addColumn<bool>("loadAsDocument"); |
| 149 |
QTest::addColumn<QString>("document"); |
| 150 |
QTest::addColumn<int>("mode"); |
| 151 |
QTest::addColumn< QList<int> >("statuses"); |
| 152 |
|
| 153 |
QTest::newRow("document") << true << "Document.qml" << (int)QQmlComponent::Asynchronous |
| 154 |
<< (QList<int>() |
| 155 |
<< AsyncLoader::Compiling |
| 156 |
<< AsyncLoader::Loading |
| 157 |
<< AsyncLoader::Initializing |
| 158 |
<< AsyncLoader::Ready |
| 159 |
); |
| 160 |
QTest::newRow("component, asynchronous") << false << "Document.qml" << (int)QQmlComponent::Asynchronous |
| 161 |
<< (QList<int>() |
| 162 |
<< AsyncLoader::Compiling |
| 163 |
<< AsyncLoader::Loading |
| 164 |
<< AsyncLoader::Initializing |
| 165 |
<< AsyncLoader::Ready |
| 166 |
); |
| 167 |
QTest::newRow("component, synchronous") << false << "Document.qml" << (int)QQmlComponent::PreferSynchronous |
| 168 |
<< (QList<int>() |
| 169 |
<< AsyncLoader::Loading |
| 170 |
<< AsyncLoader::Initializing |
| 171 |
<< AsyncLoader::Ready |
| 172 |
); |
| 173 |
} |
| 174 |
void test_load() |
| 175 |
{ |
| 176 |
QFETCH(bool, loadAsDocument); |
| 177 |
QFETCH(QString, document); |
| 178 |
QFETCH(int, mode); |
| 179 |
QFETCH(QList<int>, statuses); |
| 180 |
|
| 181 |
QScopedPointer<LomiriTestCase> view(new LomiriTestCase("TestApp.qml")); |
| 182 |
AsyncLoader loader; |
| 183 |
LoaderSpy spy(&loader); |
| 184 |
QScopedPointer<QQmlComponent> component; |
| 185 |
|
| 186 |
if (loadAsDocument) { |
| 187 |
loader.load(QUrl::fromLocalFile(document), view->rootContext()); |
| 188 |
} else { |
| 189 |
// create a component |
| 190 |
component.reset(new QQmlComponent(view->engine(), QUrl::fromLocalFile(document), (QQmlComponent::CompilationMode)mode)); |
| 191 |
loader.load(component.data(), view->rootContext()); |
| 192 |
} |
| 193 |
QTRY_VERIFY_WITH_TIMEOUT(spy.m_object != nullptr, 20000); |
| 194 |
// check the statuses |
| 195 |
QCOMPARE(spy.m_statusList.length(), statuses.length()); |
| 196 |
for (int i = 0; i < spy.m_statusList.length(); i++) { |
| 197 |
QVERIFY2((int)spy.m_statusList[i] == statuses[i], (QString(" Status at index %1 differs").arg(i)).toLocal8Bit().constData()); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
void test_load_with_error() |
| 202 |
{ |
| 203 |
QUrl document = QUrl::fromLocalFile("FaultyDocument.qml"); |
| 204 |
LomiriTestCase::ignoreWarning("FaultyDocument.qml", 20, "Label is not a type"); |
| 205 |
QScopedPointer<LomiriTestCase> view(new LomiriTestCase("TestApp.qml")); |
| 206 |
AsyncLoader loader; |
| 207 |
LoaderSpy spy(&loader); |
| 208 |
loader.load(document, view->rootContext()); |
| 209 |
QTRY_VERIFY_WITH_TIMEOUT(spy.m_error == true, 10000); |
| 210 |
} |
| 211 |
|
| 212 |
void test_load_no_context() |
| 213 |
{ |
| 214 |
AsyncLoader loader; |
| 215 |
QVERIFY(!loader.load(nullptr, nullptr)); |
| 216 |
QVERIFY(!loader.load(QUrl(), nullptr)); |
| 217 |
} |
| 218 |
|
| 219 |
void test_load_invalid_url() |
| 220 |
{ |
| 221 |
QQmlEngine engine; |
| 222 |
AsyncLoader loader; |
| 223 |
LoaderSpy spy(&loader); |
| 224 |
QVERIFY(!loader.load(QUrl(), engine.rootContext())); |
| 225 |
} |
| 226 |
|
| 227 |
void test_load_null_component() |
| 228 |
{ |
| 229 |
QQmlEngine engine; |
| 230 |
AsyncLoader loader; |
| 231 |
LoaderSpy spy(&loader); |
| 232 |
QVERIFY(!loader.load(nullptr, engine.rootContext())); |
| 233 |
} |
| 234 |
|
| 235 |
void test_load_and_cancel_data() |
| 236 |
{ |
| 237 |
QTest::addColumn<bool>("loadAsDocument"); |
| 238 |
QTest::addColumn<QString>("document"); |
| 239 |
QTest::addColumn<int>("mode"); |
| 240 |
|
| 241 |
QTest::newRow("document") << true << "HeavyDocument.qml" << (int)QQmlComponent::Asynchronous; |
| 242 |
QTest::newRow("component, asynchronous") << false << "HeavyDocument.qml" << (int)QQmlComponent::Asynchronous; |
| 243 |
QTest::newRow("component, synchronous") << false << "HeavyDocument.qml" << (int)QQmlComponent::PreferSynchronous; |
| 244 |
} |
| 245 |
void test_load_and_cancel() |
| 246 |
{ |
| 247 |
QFETCH(bool, loadAsDocument); |
| 248 |
QFETCH(QString, document); |
| 249 |
QFETCH(int, mode); |
| 250 |
|
| 251 |
QScopedPointer<LomiriTestCase> view(new LomiriTestCase("TestApp.qml")); |
| 252 |
AsyncLoader loader; |
| 253 |
ResetLoaderSpy spy(&loader); |
| 254 |
QScopedPointer<QQmlComponent> component; |
| 255 |
|
| 256 |
if (loadAsDocument) { |
| 257 |
loader.load(QUrl::fromLocalFile(document), view->rootContext()); |
| 258 |
} else { |
| 259 |
// create a component |
| 260 |
component.reset(new QQmlComponent(view->engine(), QUrl::fromLocalFile(document), (QQmlComponent::CompilationMode)mode)); |
| 261 |
loader.load(component.data(), view->rootContext()); |
| 262 |
} |
| 263 |
if (loader.status() < AsyncLoader::Loading) { |
| 264 |
// cannot reset yet! |
| 265 |
QVERIFY(!loader.reset()); |
| 266 |
} |
| 267 |
QTRY_VERIFY_WITH_TIMEOUT(spy.m_reset, 10000); |
| 268 |
} |
| 269 |
|
| 270 |
void test_second_load_scenarios_data() |
| 271 |
{ |
| 272 |
QTest::addColumn<QString> ("doc1"); |
| 273 |
QTest::addColumn<QString> ("doc2"); |
| 274 |
QTest::addColumn<int> ("when"); |
| 275 |
QTest::addColumn<bool> ("success"); |
| 276 |
|
| 277 |
QTest::newRow("status = Compiling") |
| 278 |
<< "Document.qml" << "HeavyDocument.qml" |
| 279 |
<< (int)AsyncLoader::Compiling << false; |
| 280 |
QTest::newRow("status = Loading") |
| 281 |
<< "Document.qml" << "HeavyDocument.qml" |
| 282 |
<< (int)AsyncLoader::Loading << true; |
| 283 |
QTest::newRow("status = Initializing") |
| 284 |
<< "Document.qml" << "HeavyDocument.qml" |
| 285 |
<< (int)AsyncLoader::Initializing << true; |
| 286 |
QTest::newRow("status = Ready") |
| 287 |
<< "Document.qml" << "HeavyDocument.qml" |
| 288 |
<< (int)AsyncLoader::Ready << true; |
| 289 |
QTest::newRow("status = Error") |
| 290 |
<< "FaultyDocument.qml" << "HeavyDocument.qml" |
| 291 |
<< (int)AsyncLoader::Error << true; |
| 292 |
} |
| 293 |
void test_second_load_scenarios() |
| 294 |
{ |
| 295 |
QFETCH(QString, doc1); |
| 296 |
QFETCH(QString, doc2); |
| 297 |
QFETCH(int, when); |
| 298 |
QFETCH(bool, success); |
| 299 |
|
| 300 |
// See above for what this works around, and AsyncLoader's code for how. |
| 301 |
if (WORKAROUND_QTBUG_91519 && when == (int) AsyncLoader::Initializing) { |
| 302 |
QSKIP("Some Qt 5.15 and Qt 6.0 will crash if we call QQmlIncubator::clear() " |
| 303 |
"from QQmlIncubator::setInitialState(). The code has been updated to " |
| 304 |
"prevent this. QTBUG-91519."); |
| 305 |
} |
| 306 |
|
| 307 |
QScopedPointer<LomiriTestCase> view(new LomiriTestCase("TestApp.qml")); |
| 308 |
AsyncLoader loader; |
| 309 |
if (when == (int)AsyncLoader::Error) { |
| 310 |
LomiriTestCase::ignoreWarning("FaultyDocument.qml", 20, "Label is not a type"); |
| 311 |
} |
| 312 |
|
| 313 |
SecondLoaderSpy spy(&loader, (AsyncLoader::LoadingStatus)when, QUrl::fromLocalFile(doc2), view->rootContext()); |
| 314 |
// load the first document |
| 315 |
QVERIFY(loader.load(QUrl::fromLocalFile(doc1), view->rootContext())); |
| 316 |
QTRY_VERIFY_WITH_TIMEOUT(spy.m_object != nullptr, 10000); |
| 317 |
QCOMPARE(spy.m_loadResult, success); |
| 318 |
} |
| 319 |
}; |
| 320 |
|
| 321 |
QTEST_MAIN(tst_AsyncLoader) |
| 322 |
|
| 323 |
#include "tst_asyncloader.moc" |