/* * Copyright 2013 Canonical Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * Author: Zsombor Egri */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) #include #else #include #endif #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) #include #else #include #endif #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) #include #else #include #endif #define protected public #define private public #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) #include #else #include #endif #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) #include #else #include #endif #undef private #undef protected #include "uctestcase.h" UT_USE_NAMESPACE class tst_StateSaverTest : public QObject { Q_OBJECT public: tst_StateSaverTest() {} private: QTemporaryDir *tempDir = nullptr; QString m_modulePath; QQmlEngine *engine = nullptr; QQuickView *createView(const QString &file) { LomiriTestCase* testCase = new LomiriTestCase(file); return qobject_cast(testCase); } void resetView(QScopedPointer &view, const QString &file) { Q_EMIT StateSaverBackend::instance()->initiateStateSaving(); view.reset(); // Make sure that the state is reloaded from file StateSaverBackend::instance()->m_archive.data()->sync(); view.reset(new LomiriTestCase(file)); } void resetView(QScopedPointer &view, const QString &file) { Q_EMIT StateSaverBackend::instance()->initiateStateSaving(); view.reset(); // Make sure that the state is reloaded from file StateSaverBackend::instance()->m_archive.data()->sync(); view.reset(createView(file)); } QString stateFile(const QString &appId) { return QString("%1/%2/statesaver.appstate") .arg(QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation)) .arg(appId); } private Q_SLOTS: void initTestCase() { QCoreApplication::setApplicationName("savedstate"); QCoreApplication::setOrganizationName(""); QDir modules (LOMIRI_QML_IMPORT_PATH); QVERIFY(modules.exists()); m_modulePath = modules.absolutePath(); // XDG_RUNTIME_DIR may not be set in a test environment tempDir = new QTemporaryDir(); QVERIFY(tempDir->isValid()); QString testRuntimeDir(tempDir->filePath("tst_statesaver")); // Create manually to avoid wrong ownership QDir(testRuntimeDir).mkdir("tst_statesaver"); setenv("XDG_RUNTIME_DIR", testRuntimeDir.toUtf8(), 1); QFile::setPermissions(testRuntimeDir.toUtf8(), QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner); } void cleanupTestCase() { delete tempDir; } void init() { engine = new QQmlEngine; LomiriToolkitModule::initializeContextProperties(engine); // invoke initialization StateSaverBackend::instance(engine); } void cleanup() { StateSaverBackend::instance()->reset(); delete engine; } void test_SaveArrays() { QScopedPointer view(createView("SaveArrays.qml")); QVERIFY(view); QObject *testItem = view->rootObject(); QVERIFY(testItem); QVariantList boolValues; boolValues << true << true; QVariantList intValues; intValues << 100 << 200; QVariantList realValues; realValues << 123.45 << 678.9; QVariantList stringValues; stringValues << "one" << "two"; testItem->setProperty("boolArray", boolValues); testItem->setProperty("intArray", intValues); testItem->setProperty("realArray", realValues); testItem->setProperty("stringArray", stringValues); resetView(view, "SaveArrays.qml"); QVERIFY(view); testItem = view->rootObject(); QVERIFY(testItem); QCOMPARE(testItem->property("boolArray").value(), boolValues); QCOMPARE(testItem->property("intArray").value(), intValues); QCOMPARE(testItem->property("realArray").value(), realValues); QCOMPARE(testItem->property("stringArray").value(), stringValues); } void test_SaveStructures() { QScopedPointer view(createView("SaveSupportedTypes.qml")); QVERIFY(view); QObject *testItem = view->rootObject(); QVERIFY(testItem); QVariantHash values; values.insert("intValue", 1000); values.insert("boolValue", true); values.insert("realValue", 345.12); values.insert("doubleValue", 65535.33244); values.insert("string", "test string"); values.insert("url", QUrl::fromLocalFile("tst_statesaver.cpp")); values.insert("date", QDateTime::currentDateTime().addDays(5)); values.insert("point", QPointF(100, 100)); values.insert("rect", QRectF(100, 100, 200, 200)); values.insert("size", QSizeF(345, 678)); values.insert("color", QColor("blue")); values.insert("font", QFont("Noto Sans")); values.insert("vector2d", QVector2D(100.0, 200.0)); values.insert("vector3d", QVector3D(100.0, 200.0, 300.0)); values.insert("vector4d", QVector4D(100.0, 200.0, 300.0, 400.0)); values.insert("quaternion", QQuaternion(1, 100.0, 200.0, 300.0)); values.insert("matrix4x4", QMatrix4x4(5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)); QHashIterator i(values); while (i.hasNext()) { i.next(); QVERIFY2(testItem->setProperty(i.key().toLocal8Bit().constData(), i.value()), QString("set %1").arg(i.key()).toLocal8Bit().constData()); } resetView(view, "SaveSupportedTypes.qml"); QVERIFY(view); testItem = view->rootObject(); QVERIFY(testItem); UCStateSaverAttached *stateSaver = qobject_cast(qmlAttachedPropertiesObject(testItem, false)); QVERIFY(stateSaver); QStringList properties = stateSaver->properties().split(','); Q_FOREACH(const QString &property, properties) { QVERIFY2(testItem->property(property.toLocal8Bit().constData()) == values.value(property), QString("verifying %1").arg(property).toLocal8Bit().constData()); } } void test_SaveEnum() { /* This test is important because when saving the value of an enum * property to QSettings, it is deserialized as a string. Setting that * string value to an enum property fails and therefore the state * restoration does not work. * In most cases implicit type conversion from string to the appropriate * type works. */ QScopedPointer view(createView("SaveEnum.qml")); QVERIFY(view); QObject *testItem = view->rootObject(); QVERIFY(testItem); testItem->setProperty("horizontalAlignment", Qt::AlignRight); resetView(view, "SaveEnum.qml"); QVERIFY(view); testItem = view->rootObject(); QVERIFY(testItem); QVERIFY(testItem->property("horizontalAlignment") == Qt::AlignRight); } void test_SavePropertyGroup() { QScopedPointer view(createView("SavePropertyGroups.qml")); QVERIFY(view); QObject *testItem = view->rootObject(); QVERIFY(testItem); QVERIFY(testItem->setProperty("color", QColor("red"))); // set property groups using QQmlProperty QQmlProperty borderColor(testItem, "border.color"); borderColor.write(QColor("green")); QQmlProperty propertyGroup(testItem, "propertyGroup.color"); propertyGroup.write(QColor("blue")); resetView(view, "SavePropertyGroups.qml"); QVERIFY(view); testItem = view->rootObject(); QVERIFY(testItem); QCOMPARE(testItem->property("color"), QVariant(QColor("red"))); // read property groups using QQmlProperty QQmlProperty verifyBorderColor(testItem, "border.color"); QCOMPARE(verifyBorderColor.read(), QVariant(QColor("green"))); QQmlProperty verifyPropertyGroup(testItem, "propertyGroup.color"); QCOMPARE(verifyPropertyGroup.read(), QVariant(QColor("blue"))); } void test_SaveObject() { QScopedPointer view(createView("SaveObject.qml")); QVERIFY(view); QObject *testItem = view->rootObject(); QVERIFY(testItem); QScopedPointer obj(new QQuickItem); obj->setObjectName("internal"); testItem->setProperty("object", QVariant::fromValue(obj.data())); resetView(view, "SaveObject.qml"); QVERIFY(view); testItem = view->rootObject(); QVERIFY(testItem); QVERIFY(testItem->property("object").value() != obj.data()); } void test_ValidUID() { QScopedPointer view(createView("ValidUID.qml")); QVERIFY(view); QObject *testItem = view->rootObject()->findChild("testItem"); QVERIFY(testItem); testItem->setObjectName("updated"); resetView(view, "ValidUID.qml"); QVERIFY(view); testItem = view->rootObject()->findChild("updated"); QVERIFY(testItem); } void test_InvalidUID() { QString filePath(QFileInfo("InvalidUID.qml").absoluteFilePath()); QString objectId(filePath.replace("/", "_") + ":21:5:testItem"); QRegularExpression warningRe(QStringLiteral( ": QML (Item|InvalidUID): All the parents must have an id\\.\n" "State saving disabled for %1, class (QQuickItem|InvalidUID)$" ).arg(QRegularExpression::escape(objectId))); // The warning happens twice. QTest::ignoreMessage(QtWarningMsg, warningRe); QTest::ignoreMessage(QtWarningMsg, warningRe); QScopedPointer view(new LomiriTestCase("InvalidUID.qml")); QObject *testItem = view->rootObject()->findChild("testItem"); QVERIFY(testItem); testItem->setObjectName("updated"); resetView(view, "InvalidUID.qml"); QVERIFY(view); testItem = view->rootObject()->findChild("updated"); QVERIFY(testItem == 0); } void test_ValidGroupProperty() { QScopedPointer view(createView("ValidGroupProperty.qml")); QVERIFY(view); QObject *testItem = view->rootObject()->findChild("testItem"); QVERIFY(testItem); testItem->setObjectName("group"); resetView(view, "ValidGroupProperty.qml"); QVERIFY(view); testItem = view->rootObject()->findChild("group"); QVERIFY(testItem); } void test_InvalidGroupProperty() { LomiriTestCase::ignoreWarning("InvalidGroupProperty.qml", 24, 29, "QML QtObject: Warning: attachee must have an ID. State will not be saved.", 2); QScopedPointer view(new LomiriTestCase("InvalidGroupProperty.qml")); QObject *testItem = view->rootObject()->findChild("testItem"); QVERIFY(testItem); testItem->setObjectName("group"); resetView(view, "InvalidGroupProperty.qml"); testItem = view->rootObject()->findChild("group"); QVERIFY(testItem == 0); } void test_Dynamic() { QScopedPointer view(createView("Dynamic.qml")); QVERIFY(view); QObject *testItem = view->rootObject()->findChild("testItem"); QVERIFY(testItem); testItem->setObjectName("updated"); resetView(view, "Dynamic.qml"); QVERIFY(view); testItem = view->rootObject()->findChild("updated"); QVERIFY(testItem); } void test_TwoDynamics() { QScopedPointer view(createView("TwoDynamics.qml")); QVERIFY(view); QObject *testItem = view->rootObject()->findChild("testItem"); QVERIFY(testItem); testItem->setObjectName("updated"); resetView(view, "TwoDynamics.qml"); QVERIFY(view); testItem = view->rootObject()->findChild("updated"); QVERIFY(testItem); } void test_DisabledStateSaver() { QScopedPointer view(createView("DisabledStateSaver.qml")); QVERIFY(view); QObject *testItem = view->rootObject()->findChild("testItem"); QVERIFY(testItem); testItem->setObjectName("updated"); resetView(view, "DisabledStateSaver.qml"); QVERIFY(view); testItem = view->rootObject()->findChild("updated"); QVERIFY(testItem == 0); } void test_SameIdsInDifferentComponents() { QSKIP("Failing on Qt 5.9 due to unknown parent missing ID."); QScopedPointer view(createView("SameIdsInDifferentComponents.qml")); QVERIFY(view); QObject *testItem = view->rootObject()->findChild("testItem"); QVERIFY(testItem); testItem->setProperty("source", "SecondComponent.qml"); // check if the SecondComponent state saver is enabled QObject *item = testItem->property("item").value(); QVERIFY(item); QObject *innerItem = item->findChild("secondComponent"); UCStateSaverAttached *stateSaver = qobject_cast(qmlAttachedPropertiesObject(innerItem, false)); QVERIFY(stateSaver); QVERIFY(stateSaver->enabled()); resetView(view, "SameIdsInDifferentComponents.qml"); QVERIFY(view); testItem = view->rootObject()->findChild("secondComponent"); QVERIFY(testItem); } void test_ComponentsWithStateSavers() { QScopedPointer view(createView("ComponentsWithStateSavers.qml")); QVERIFY(view); QObject *control1 = view->rootObject()->findChild("control1"); QVERIFY(control1); QObject *control2 = view->rootObject()->findChild("control2"); QVERIFY(control2); UCStateSaverAttached *stateSaver1 = qobject_cast(qmlAttachedPropertiesObject(control1, false)); QVERIFY(stateSaver1); QVERIFY(stateSaver1->enabled()); UCStateSaverAttached *stateSaver2 = qobject_cast(qmlAttachedPropertiesObject(control2, false)); QVERIFY(stateSaver2); QVERIFY(stateSaver2->enabled()); QVERIFY(control1->setProperty("color", QColor("green"))); QVERIFY(control2->setProperty("color", QColor("blue"))); resetView(view, "ComponentsWithStateSavers.qml"); control1 = view->rootObject()->findChild("control1"); QVERIFY(control1); control2 = view->rootObject()->findChild("control2"); QVERIFY(control2); stateSaver1 = qobject_cast(qmlAttachedPropertiesObject(control1, false)); QVERIFY(stateSaver1); QVERIFY(stateSaver1->enabled()); stateSaver2 = qobject_cast(qmlAttachedPropertiesObject(control2, false)); QVERIFY(stateSaver2); QVERIFY(stateSaver2->enabled()); QVERIFY(view); QCOMPARE(control1->property("color"), QVariant(QColor("green"))); QCOMPARE(control2->property("color"), QVariant(QColor("blue"))); } void test_ComponentsWithStateSaversNoId() { QRegularExpression warningRe(QStringLiteral( ": QML (Rectangle|CustomControl): Warning: attachee must have an ID\\. State will not be saved\\.$" )); QTest::ignoreMessage(QtWarningMsg, warningRe); QScopedPointer view(new LomiriTestCase("ComponentsWithStateSaversNoId.qml")); QObject *control1 = view->rootObject()->findChild("control1"); QVERIFY(control1); QObject *control2 = view->rootObject()->findChild("control2"); QVERIFY(control2); UCStateSaverAttached *stateSaver1 = qobject_cast(qmlAttachedPropertiesObject(control1, false)); QVERIFY(stateSaver1); QVERIFY(stateSaver1->enabled() == false); UCStateSaverAttached *stateSaver2 = qobject_cast(qmlAttachedPropertiesObject(control2, false)); QVERIFY(stateSaver2); QVERIFY(stateSaver2->enabled()); } void test_nestedDynamics() { QSKIP("Failing on Qt 5.9 due to unknown parent missing ID."); QScopedPointer view(createView("NestedDynamics.qml")); QVERIFY(view); QObject *topLoader = view->rootObject()->findChild("outerLoader"); QVERIFY(topLoader); topLoader->setProperty("source", "Dynamic.qml"); QCoreApplication::processEvents(); QObject *testItem = view->rootObject()->findChild("testItem"); QVERIFY(testItem); testItem->setObjectName("updated"); resetView(view, "NestedDynamics.qml"); QVERIFY(view); testItem = view->rootObject()->findChild("updated"); QVERIFY(testItem); } void test_repeaterStates() { QScopedPointer view(createView("RepeaterStates.qml")); QVERIFY(view); QQuickItem *column = view->rootObject()->findChild("column"); QVERIFY(column); QList items = column->childItems(); QCOMPARE(items.count(), 5); // 4 Rectangles + 1 Repeater Q_FOREACH(QQuickItem *item, items) { if (QuickUtils::instance()->className(item) == "QQuickRectangle") { item->setHeight(25); } } resetView(view, "RepeaterStates.qml"); QVERIFY(view); column = view->rootObject()->findChild("column"); QVERIFY(column); items = column->childItems(); QCOMPARE(items.count(), 5); // 4 Rectangles + 1 Repeater Q_FOREACH(QQuickItem *item, items) { if (QuickUtils::instance()->className(item) == "QQuickRectangle") { QCOMPARE(item->height(), 25.0); } } } void test_ListViewItemStates() { QScopedPointer view(createView("ListViewItems.qml")); QVERIFY(view); QQuickItem *list = view->rootObject()->findChild("list"); QVERIFY(list); QQuickItem *contentItem = list->property("contentItem").value(); QVERIFY(contentItem); QList items = contentItem->childItems(); int testItemCount = 0; Q_FOREACH(QQuickItem *item, items) { if (item->objectName() == "testItem") { item->setHeight(25); testItemCount++; } } QCOMPARE(testItemCount, 2); resetView(view, "ListViewItems.qml"); QVERIFY(view); list = view->rootObject()->findChild("list"); QVERIFY(list); contentItem = list->property("contentItem").value(); QVERIFY(contentItem); items = contentItem->childItems(); Q_FOREACH(QQuickItem *item, items) { if (item->objectName() == "testItem") { QCOMPARE(item->height(), 25.0); } } } void test_GridViewItemStates() { QScopedPointer view(createView("GridViewItems.qml")); QVERIFY(view); QQuickItem *list = view->rootObject()->findChild("grid"); QVERIFY(list); QQuickItem *contentItem = list->property("contentItem").value(); QVERIFY(contentItem); QList items = contentItem->childItems(); int testItemCount = 0; Q_FOREACH(QQuickItem *item, items) { if (item->objectName() == "testItem") { item->setHeight(25); testItemCount++; } } QCOMPARE(testItemCount, 2); resetView(view, "GridViewItems.qml"); QVERIFY(view); list = view->rootObject()->findChild("grid"); QVERIFY(list); contentItem = list->property("contentItem").value(); QVERIFY(contentItem); items = contentItem->childItems(); Q_FOREACH(QQuickItem *item, items) { if (item->objectName() == "testItem") { QCOMPARE(item->height(), 25.0); } } } void test_normalAppClose() { QProcess testApp; testApp.start("qmlscene", QStringList() << "-I" << LOMIRI_QML_IMPORT_PATH << "NormalAppClose.qml"); testApp.waitForFinished(); QString fileName = stateFile("NormalAppClose"); QVERIFY(!QFile(fileName).exists()); } void test_SigTerm() { QProcess testApp; testApp.start("qmlscene",QStringList() << "-I" << LOMIRI_QML_IMPORT_PATH << "SimpleApp.qml"); testApp.waitForStarted(); // send SIGTERM signal to the process, use terminate() to do that. testApp.terminate(); testApp.waitForFinished(); QString fileName = stateFile("SimpleApp"); QVERIFY(!QFile(fileName).exists()); } void test_SigInt() { QProcess testApp; testApp.start("qmlscene",QStringList() << "-I" << LOMIRI_QML_IMPORT_PATH << "SimpleApp.qml"); testApp.waitForStarted(); QTest::qWait(1000); // make sure we are not killing the parent #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) QVERIFY(testApp.pid() != QCoreApplication::applicationPid()); #else QVERIFY(testApp.processId() != QCoreApplication::applicationPid()); #endif // skip tests if the application PID is zero => the child app PID seems to be zero as well #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) if (!testApp.pid()) { #else if (!testApp.processId()) { #endif // kill child process testApp.close(); QSKIP("This test requires valid PID"); } // send SIGINT #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) ::kill(testApp.pid(), SIGINT); #else ::kill(testApp.processId(), SIGINT); #endif testApp.waitForFinished(); QString fileName = stateFile("SimpleApp"); QVERIFY2(QFile(fileName).exists(), qPrintable(fileName)); // clean the file QFile::remove(fileName); } }; QTEST_MAIN(tst_StateSaverTest) #include "tst_statesaver.moc"