diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2022-02-21 18:22:55 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-03-02 17:14:37 +0000 |
commit | 593b42df141bde3725444f62434995ed5530dd7f (patch) | |
tree | 6c395aa82c7dd45a2ec03368cf01ca6f0657e8e4 | |
parent | 82e76f43210ab3b7a1c4f9e575db322512ffa892 (diff) |
tst_qquickaccessibility: wrap dynamic object allocations into smart pointers
This helps to prevent memory leaks if some of the tests fails.
On Android it also helps to prevent crashes when one of the tests fails.
As a drive-by: replace QScopedPointer usage with std::unique_ptr.
Task-number: QTBUG-77371
Change-Id: Ib0d12f82d967116569266e6f65367bcf89f66c9c
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
(cherry picked from commit 3fb70ea89c813072f8c191298bf2c18efc4bdab5)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | tests/auto/quick/qquickaccessible/tst_qquickaccessible.cpp | 55 |
1 files changed, 25 insertions, 30 deletions
diff --git a/tests/auto/quick/qquickaccessible/tst_qquickaccessible.cpp b/tests/auto/quick/qquickaccessible/tst_qquickaccessible.cpp index 136703d517..7cba462ff8 100644 --- a/tests/auto/quick/qquickaccessible/tst_qquickaccessible.cpp +++ b/tests/auto/quick/qquickaccessible/tst_qquickaccessible.cpp @@ -147,17 +147,17 @@ void tst_QQuickAccessible::commonTests() qDebug() << "testing" << accessibleRoleFileName; - QQuickView *view = new QQuickView(); + auto view = std::make_unique<QQuickView>(); // view->setFixedSize(240,320); view->setSource(testFileUrl(accessibleRoleFileName)); view->show(); // view->setFocus(); QVERIFY(view->rootObject() != nullptr); - QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(view); + QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(view.get()); QVERIFY(iface); - delete view; + view.reset(); QTestAccessibility::clearEvents(); } @@ -168,12 +168,11 @@ void tst_QQuickAccessible::quickAttachedProperties() QQmlComponent component(&engine); component.setData("import QtQuick 2.0\nItem {\n" "}", QUrl()); - QObject *object = component.create(); + auto object = std::unique_ptr<QObject>(component.create()); QVERIFY(object != nullptr); - QObject *attachedObject = QQuickAccessibleAttached::attachedProperties(object); + QObject *attachedObject = QQuickAccessibleAttached::attachedProperties(object.get()); QCOMPARE(attachedObject, static_cast<QObject*>(nullptr)); - delete object; } // Attaching to non-item @@ -202,11 +201,11 @@ void tst_QQuickAccessible::quickAttachedProperties() component.setData("import QtQuick 2.0\nItem {\n" "Accessible.role: Accessible.Button\n" "}", QUrl()); - QObject *object = component.create(); + auto object = std::unique_ptr<QObject>(component.create()); QVERIFY(object != nullptr); const auto attachedObject = qobject_cast<QQuickAccessibleAttached*>( - QQuickAccessibleAttached::attachedProperties(object)); + QQuickAccessibleAttached::attachedProperties(object.get())); QVERIFY(attachedObject); if (attachedObject) { QVariant p = attachedObject->property("role"); @@ -220,7 +219,6 @@ void tst_QQuickAccessible::quickAttachedProperties() QVERIFY2(p.value<QString>().isEmpty(), QTest::toString(p)); QCOMPARE(attachedObject->wasNameExplicitlySet(), false); } - delete object; } // Attached property @@ -232,11 +230,11 @@ void tst_QQuickAccessible::quickAttachedProperties() "Accessible.name: \"Donald\"\n" "Accessible.description: \"Duck\"\n" "}", QUrl()); - QObject *object = component.create(); + auto object = std::unique_ptr<QObject>(component.create()); QVERIFY(object != nullptr); const auto attachedObject = qobject_cast<QQuickAccessibleAttached*>( - QQuickAccessibleAttached::attachedProperties(object)); + QQuickAccessibleAttached::attachedProperties(object.get())); QVERIFY(attachedObject); if (attachedObject) { QVariant p = attachedObject->property("role"); @@ -250,7 +248,6 @@ void tst_QQuickAccessible::quickAttachedProperties() QCOMPARE(p.toString(), QLatin1String("Duck")); QCOMPARE(attachedObject->wasNameExplicitlySet(), true); } - delete object; } // Check overriding of attached role for Text @@ -262,10 +259,10 @@ void tst_QQuickAccessible::quickAttachedProperties() "Accessible.name: \"TextButton\"\n" "Accessible.description: \"Text Button\"\n" "}", QUrl()); - QObject *object = component.create(); + auto object = std::unique_ptr<QObject>(component.create()); QVERIFY(object != nullptr); - QObject *attachedObject = QQuickAccessibleAttached::attachedProperties(object); + QObject *attachedObject = QQuickAccessibleAttached::attachedProperties(object.get()); QVERIFY(attachedObject); if (attachedObject) { QVariant p = attachedObject->property("role"); @@ -278,7 +275,6 @@ void tst_QQuickAccessible::quickAttachedProperties() QCOMPARE(p.isNull(), false); QCOMPARE(p.toString(), QLatin1String("Text Button")); } - delete object; } // Check overriding of attached role for Text { @@ -294,10 +290,10 @@ void tst_QQuickAccessible::quickAttachedProperties() "Accessible.description: \"Text Button\"\n" "}\n" "}", QUrl()); - QObject *object = component.create(); + auto object = std::unique_ptr<QObject>(component.create()); QVERIFY(object != nullptr); - QQuickListView *listview = qobject_cast<QQuickListView *>(object); + QQuickListView *listview = qobject_cast<QQuickListView *>(object.get()); QVERIFY(listview != nullptr); QQuickItem *contentItem = listview->contentItem(); QQuickText *childItem = QQuickVisualTestUtils::findItem<QQuickText>(contentItem, "acc_text"); @@ -316,7 +312,6 @@ void tst_QQuickAccessible::quickAttachedProperties() QCOMPARE(p.isNull(), false); QCOMPARE(p.toString(), QLatin1String("Text Button")); } - delete object; } // Check that a name can be implicitly set. { @@ -328,11 +323,11 @@ void tst_QQuickAccessible::quickAttachedProperties() Accessible.role: Accessible.Button Accessible.description: "Text Button" })", QUrl()); - QScopedPointer<QObject> object(component.create()); + auto object = std::unique_ptr<QObject>(component.create()); QVERIFY(object); const auto attachedObject = qobject_cast<QQuickAccessibleAttached*>( - QQuickAccessibleAttached::attachedProperties(object.data())); + QQuickAccessibleAttached::attachedProperties(object.get())); QVERIFY(attachedObject); QVERIFY(!attachedObject->wasNameExplicitlySet()); @@ -353,12 +348,12 @@ void tst_QQuickAccessible::basicPropertiesTest() QAccessibleInterface *app = QAccessible::queryAccessibleInterface(qApp); QCOMPARE(app->childCount(), 0); - QQuickView *window = new QQuickView(); + auto window = std::make_unique<QQuickView>(); window->setSource(testFileUrl("text.qml")); window->show(); QCOMPARE(app->childCount(), 1); - QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(window); + QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(window.get()); QVERIFY(iface); QCOMPARE(iface->childCount(), 1); @@ -483,7 +478,7 @@ void tst_QQuickAccessible::basicPropertiesTest() attached->setRole(QAccessible::StaticText); QVERIFY(!text3->state().readOnly); - delete window; + window.reset(); QTestAccessibility::clearEvents(); } @@ -502,11 +497,11 @@ QAccessibleInterface *topLevelChildAt(QAccessibleInterface *iface, int x, int y) void tst_QQuickAccessible::hitTest() { - QQuickView *window = new QQuickView; + auto window = std::make_unique<QQuickView>(); window->setSource(testFileUrl("hittest.qml")); window->show(); - QAccessibleInterface *windowIface = QAccessible::queryAccessibleInterface(window); + QAccessibleInterface *windowIface = QAccessible::queryAccessibleInterface(window.get()); QVERIFY(windowIface); QAccessibleInterface *rootItem = windowIface->child(0); QRect rootRect = rootItem->rect(); @@ -555,13 +550,13 @@ void tst_QQuickAccessible::hitTest() } } - delete window; + window.reset(); QTestAccessibility::clearEvents(); } void tst_QQuickAccessible::checkableTest() { - QScopedPointer<QQuickView> window(new QQuickView()); + auto window = std::make_unique<QQuickView>(); window->setSource(testFileUrl("checkbuttons.qml")); window->show(); @@ -574,7 +569,7 @@ void tst_QQuickAccessible::checkableTest() QAccessible::State activatedChange; activatedChange.active = true; - QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(window.data()); + QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(window.get()); QVERIFY(iface); QAccessibleInterface *root = iface->child(0); @@ -638,7 +633,7 @@ void tst_QQuickAccessible::checkableTest() void tst_QQuickAccessible::ignoredTest() { - QScopedPointer<QQuickView> window(new QQuickView()); + auto window = std::make_unique<QQuickView>(); window->setSource(testFileUrl("ignored.qml")); window->show(); @@ -651,7 +646,7 @@ void tst_QQuickAccessible::ignoredTest() QAccessible::State activatedChange; activatedChange.active = true; - QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(window.data()); + QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(window.get()); QVERIFY(iface); QAccessibleInterface *rectangleA = iface->child(0); |