From c07f87e35062e39e9d4b3b978362dbf94dd36fbb Mon Sep 17 00:00:00 2001 From: Renato Araujo Oliveira Filho Date: Tue, 9 Oct 2012 15:44:25 -0300 Subject: Fixed crash during model destruction in QML. --- tests/client/CMakeLists.txt | 5 ++- tests/client/loadmodel.qml | 38 ++++++++++++++++++ tests/client/modeltest.cpp | 22 ++++++++++ tests/client/qmlfiles.h.in | 2 + tests/client/qmltest.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++ tests/client/script_qmltest.py | 12 ++++++ 6 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 tests/client/loadmodel.qml create mode 100644 tests/client/qmlfiles.h.in create mode 100644 tests/client/qmltest.cpp create mode 100755 tests/client/script_qmltest.py (limited to 'tests') diff --git a/tests/client/CMakeLists.txt b/tests/client/CMakeLists.txt index 7455b16..9bce370 100644 --- a/tests/client/CMakeLists.txt +++ b/tests/client/CMakeLists.txt @@ -1,6 +1,6 @@ macro(declare_test testname) add_executable(${testname} ${testname}.cpp) - qt5_use_modules(${testname} Core DBus Widgets Test) + qt5_use_modules(${testname} Core DBus Widgets Test Qml Quick) target_link_libraries(${testname} qmenumodel dbusmenuscript @@ -50,5 +50,8 @@ declare_test(servicetest) declare_test(menuchangestest) declare_test(modeltest) declare_test(actiongrouptest) +declare_test(qmltest) declare_simple_test(convertertest) +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/qmlfiles.h.in + ${CMAKE_CURRENT_BINARY_DIR}/qmlfiles.h) diff --git a/tests/client/loadmodel.qml b/tests/client/loadmodel.qml new file mode 100644 index 0000000..a91c18b --- /dev/null +++ b/tests/client/loadmodel.qml @@ -0,0 +1,38 @@ +import QtQuick 2.0 +import QMenuModel 0.1 + +Item { + id: root + width: 100 + height: 100 + + property bool reset: resetModel + + onResetChanged: { + if (reset) { + console.log("Remove page"); + view.model.destroy(); + //pop(); + } + } + + ListView { + id: view + anchors.fill: parent + delegate: Text { + text: label + } + onCountChanged: { + console.log("Row count: " + count); + } + } + + Component.onCompleted: { + var model = Qt.createQmlObject("import QMenuModel 0.1; QDBusMenuModel { id: menuModel; busType: globalBusType; busName: globalBusName; objectPath: globalObjectPath; }", view, ""); + model.start(); + console.log("New model: " + model) + console.log("New model2: " + model) + view.model = model; + } +} + diff --git a/tests/client/modeltest.cpp b/tests/client/modeltest.cpp index 542b38a..8f20bc3 100644 --- a/tests/client/modeltest.cpp +++ b/tests/client/modeltest.cpp @@ -209,6 +209,28 @@ private Q_SLOTS: QCOMPARE(v.toString(), QString("42")); } + /* + * Test if model is destroyed without crash + */ + void testDestroyModel() + { + // Make menu available + m_script.publishMenu(); + m_script.run(); + + // create a new model + QDBusMenuModel *model = new QDBusMenuModel(); + model->setBusType(DBusEnums::SessionBus); + model->setBusName(MENU_SERVICE_NAME); + model->setObjectPath(MENU_OBJECT_PATH); + model->start(); + + // Wait for dbus sync + QTest::qWait(500); + + delete model; + } + }; QTEST_MAIN(ModelTest) diff --git a/tests/client/qmlfiles.h.in b/tests/client/qmlfiles.h.in new file mode 100644 index 0000000..ebb534c --- /dev/null +++ b/tests/client/qmlfiles.h.in @@ -0,0 +1,2 @@ +const char* QML_BASE_DIR = "@libqmenumodel_BINARY_DIR@"; +const char* LOADMODEL_QML = "@CMAKE_CURRENT_SOURCE_DIR@/loadmodel.qml"; diff --git a/tests/client/qmltest.cpp b/tests/client/qmltest.cpp new file mode 100644 index 0000000..ae0d914 --- /dev/null +++ b/tests/client/qmltest.cpp @@ -0,0 +1,91 @@ +/* + * Copyright 2012 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 . + * + * Authors: + * Renato Araujo Oliveira Filho + */ + +extern "C" { +#include +} + +#include "qdbusmenumodel.h" +#include "dbusmenuscript.h" +#include "qmlfiles.h" + +#include +#include +#include +#include + +#include +#include +#include + + +class QMLTest : public QObject +{ + Q_OBJECT +private: + DBusMenuScript m_script; + +private Q_SLOTS: + void initTestCase() + { + g_type_init(); + Q_ASSERT(m_script.connect()); + } + + void cleanupTestCase() + { + m_script.quit(); + } + + void init() + { + } + + void cleanup() + { + m_script.unpublishMenu(); + } + + /* + * Test if model is destroyed without crash + */ + void destoyModel() + { + m_script.publishMenu(); + m_script.run(); + QTest::qWait(500); + + QQuickView *view = new QQuickView; + view->engine()->addImportPath(QML_BASE_DIR); + view->engine()->rootContext()->setContextProperty("resetModel", QVariant(false)); + view->engine()->rootContext()->setContextProperty("globalBusType", DBusEnums::SessionBus); + view->engine()->rootContext()->setContextProperty("globalBusName", MENU_SERVICE_NAME); + view->engine()->rootContext()->setContextProperty("globalObjectPath", MENU_OBJECT_PATH); + view->setSource(QUrl::fromLocalFile(LOADMODEL_QML)); + view->show(); + QTest::qWait(500); + view->engine()->rootContext()->setContextProperty("resetModel", true); + QTest::qWait(500); + } + +}; + +QTEST_MAIN(QMLTest) + +#include "qmltest.moc" diff --git a/tests/client/script_qmltest.py b/tests/client/script_qmltest.py new file mode 100755 index 0000000..385256c --- /dev/null +++ b/tests/client/script_qmltest.py @@ -0,0 +1,12 @@ +#!/usr/bin/python2.7 + +import time +from menuscript import Script, ActionList, MENU_OBJECT_PATH + +al = ActionList(MENU_OBJECT_PATH) +al.appendItem("Menu0", "Menu0") +al.appendItem("Menu1", "Menu1") + +t = Script.create(al) +t.run() + -- cgit v1.2.3