aboutsummaryrefslogtreecommitdiff
path: root/tests/client
diff options
context:
space:
mode:
authorRenato Araujo Oliveira Filho <renato.filho@canonical.com>2012-09-11 17:57:08 -0300
committerRenato Araujo Oliveira Filho <renato.filho@canonical.com>2012-09-11 17:57:08 -0300
commit6bacc0d5db885a72202cb7f80e505642a36052d4 (patch)
tree608aa95f959b9e362d7813476e59d37c6c07ddf6 /tests/client
parent31f8103187d2e27b1bb76eaed10dc9c9be226c90 (diff)
downloadqmenumodel-6bacc0d5db885a72202cb7f80e505642a36052d4.tar.gz
qmenumodel-6bacc0d5db885a72202cb7f80e505642a36052d4.tar.bz2
qmenumodel-6bacc0d5db885a72202cb7f80e505642a36052d4.zip
Splitted qmenumodel plugin in two libraries to make it testable.
Create unit test for qmenumodel library.
Diffstat (limited to 'tests/client')
-rw-r--r--tests/client/CMakeLists.txt46
-rw-r--r--tests/client/menuchangestest.cpp65
-rw-r--r--tests/client/modeltest.cpp185
-rwxr-xr-xtests/client/script_menuchangestest.py14
-rwxr-xr-xtests/client/script_modeltest.py31
-rwxr-xr-xtests/client/script_servicetest.py12
-rw-r--r--tests/client/servicetest.cpp101
7 files changed, 454 insertions, 0 deletions
diff --git a/tests/client/CMakeLists.txt b/tests/client/CMakeLists.txt
new file mode 100644
index 0000000..bf77461
--- /dev/null
+++ b/tests/client/CMakeLists.txt
@@ -0,0 +1,46 @@
+macro(declare_test testname)
+ set(TEST_MOC_FILE ${CMAKE_CURRENT_BINARY_DIR}/${testname}.moc)
+ qt4_generate_moc(${testname}.cpp ${TEST_MOC_FILE})
+
+ add_executable(${testname} ${testname}.cpp ${TEST_MOC_FILE})
+ target_link_libraries(${testname}
+ qmenumodelcommon
+ dbusmenuscript
+ ${QT_QTTEST_LIBRARY}
+ ${QT_QTCORE_LIBRARY}
+ ${QT_QTGUI_LIBRARY}
+ ${QT_QTDBUS_LIBRARY}
+ ${GLIB_LDFLAGS}
+ ${GIO_LDFLAGS})
+
+ add_test(${testname}
+ ${DBUS_RUNNER}
+ --task ${CMAKE_CURRENT_BINARY_DIR}/${testname} --task-name Client
+ --task ${CMAKE_CURRENT_SOURCE_DIR}/script_${testname}.py --task-name Server
+ --ignore-return)
+ set_tests_properties(${testname} PROPERTIES
+ TIMEOUT ${CTEST_TESTING_TIMEOUT}
+ ENVIRONMENT "PYTHONPATH=${TEST_PYTHONPATH}")
+
+endmacro(declare_test testname)
+
+include_directories(${qmenumodelcommon_SOURCE_DIR}
+ ${dbusmenuscript_SOURCE_DIR}
+ ${CMAKE_CURRENT_BINARY_DIR}
+ ${QT_INCLUDE_DIR}
+ ${QT_QTTEST_INCLUDE_DIR}
+ ${QT_QTCORE_INCLUDE_DIR}
+ ${QT_QTGUI_INCLUDE_DIR}
+ ${QT_QTDBUS_INCLUDE_DIR}
+ ${GLIB_INCLUDE_DIRS}
+ ${GIO_INCLUDE_DIRS})
+
+add_definitions(-DTEST_SUITE)
+set(TEST_PYTHONPATH ${dbusmenuscript_SOURCE_DIR})
+if(NOT CTEST_TESTING_TIMEOUT)
+ set(CTEST_TESTING_TIMEOUT 60)
+endif()
+
+declare_test(servicetest)
+declare_test(menuchangestest)
+declare_test(modeltest)
diff --git a/tests/client/menuchangestest.cpp b/tests/client/menuchangestest.cpp
new file mode 100644
index 0000000..7e6379c
--- /dev/null
+++ b/tests/client/menuchangestest.cpp
@@ -0,0 +1,65 @@
+#include "qdbusmenumodel.h"
+#include "dbusmenuscript.h"
+
+#include <QObject>
+#include <QSignalSpy>
+#include <QtTestGui>
+#include <QDebug>
+
+class MenuChangesTest : public QObject
+{
+ Q_OBJECT
+private:
+ DBusMenuScript m_script;
+ QDBusMenuModel m_model;
+
+private Q_SLOTS:
+ void initTestCase()
+ {
+ Q_ASSERT(m_script.connect());
+ }
+
+ void cleanupTestCase()
+ {
+ m_script.quit();
+ }
+
+ void init()
+ {
+ m_model.stop();
+ m_model.setBusType(QDBusObject::SessionBus);
+ m_model.setBusName(MENU_SERVICE_NAME);
+ m_model.setObjectPath(MENU_OBJECT_PATH);
+ }
+
+ void cleanup()
+ {
+ m_script.unpublishMenu();
+ }
+
+ void testMenuItemAppend()
+ {
+ m_script.publishMenu();
+ m_model.start();
+
+ // Create first Item
+ m_script.walk();
+ QCOMPARE(m_model.rowCount(), 1);
+
+ // Create second item
+ m_script.walk();
+ QCOMPARE(m_model.rowCount(), 2);
+
+ // Remove item0
+ m_script.walk();
+ QCOMPARE(m_model.rowCount(), 1);
+
+ // Remove item1
+ m_script.walk();
+ QCOMPARE(m_model.rowCount(), 0);
+ }
+};
+
+QTEST_MAIN(MenuChangesTest)
+
+#include "menuchangestest.moc"
diff --git a/tests/client/modeltest.cpp b/tests/client/modeltest.cpp
new file mode 100644
index 0000000..1963327
--- /dev/null
+++ b/tests/client/modeltest.cpp
@@ -0,0 +1,185 @@
+#include "qdbusmenumodel.h"
+#include "dbusmenuscript.h"
+
+#include <QObject>
+#include <QSignalSpy>
+#include <QtTestGui>
+#include <QDebug>
+
+class ModelTest : public QObject
+{
+ Q_OBJECT
+private:
+ DBusMenuScript m_script;
+ QDBusMenuModel m_model;
+
+private Q_SLOTS:
+ void initTestCase()
+ {
+ Q_ASSERT(m_script.connect());
+ }
+
+ void cleanupTestCase()
+ {
+ m_script.quit();
+ }
+
+ void init()
+ {
+ m_model.stop();
+ m_model.setBusType(QDBusObject::SessionBus);
+ m_model.setBusName(MENU_SERVICE_NAME);
+ m_model.setObjectPath(MENU_OBJECT_PATH);
+ }
+
+ void cleanup()
+ {
+ m_script.unpublishMenu();
+ }
+
+ void testColumnCount()
+ {
+ QCOMPARE(m_model.columnCount(), 1);
+ }
+
+ void testParent()
+ {
+ QCOMPARE(m_model.parent(QModelIndex()), QModelIndex());
+ }
+
+ void testBusTypeProperty()
+ {
+ m_model.setProperty("busType", 1);
+ QCOMPARE(m_model.busType(), QDBusObject::SessionBus);
+
+ m_model.setProperty("busType", 2);
+ QCOMPARE(m_model.busType(), QDBusObject::SystemBus);
+
+ m_model.setProperty("busType", 0);
+ QCOMPARE(m_model.busType(), QDBusObject::SystemBus);
+
+ m_model.setProperty("busType", 10);
+ QCOMPARE(m_model.busType(), QDBusObject::SystemBus);
+
+ }
+
+ void testData()
+ {
+ // Make menu available
+ m_script.publishMenu();
+ m_script.run();
+
+ // start model
+ m_model.start();
+
+ // Wait for dbus sync
+ QTest::qWait(500);
+
+ QCOMPARE(m_model.status(), QDBusObject::Connected);
+ QCOMPARE(m_model.rowCount(), 4);
+
+ // Label (String)
+ QVariant label = m_model.data(m_model.index(0, 0), QMenuModel::Label);
+ QVERIFY(label.isValid());
+ QCOMPARE(label.type(), QVariant::String);
+ QCOMPARE(label.toString(), QString("Menu0"));
+
+ // Action (String)
+ QVariant action = m_model.data(m_model.index(1, 0), QMenuModel::Action);
+ QVERIFY(action.isValid());
+ QCOMPARE(action.type(), QVariant::String);
+ QCOMPARE(action.toString(), QString("Menu1Act"));
+
+ // Section (QObject)
+ QVariant vSection = m_model.data(m_model.index(2, 0), QMenuModel::LinkSection);
+ QVERIFY(vSection.isValid());
+ QMenuModel *section = qobject_cast<QMenuModel*>(vSection.value<QObject*>());
+ QVERIFY(section);
+ QCOMPARE(section->rowCount(), 2);
+
+ // SubMenu (QObject)
+ QVariant vSubMenu = m_model.data(m_model.index(3, 0), QMenuModel::LinkSubMenu);
+ QVERIFY(vSubMenu.isValid());
+ QMenuModel *submenu = qobject_cast<QMenuModel*>(vSubMenu.value<QObject*>());
+ QVERIFY(submenu);
+
+ // Wait for menu load (submenus are loaded async)
+ QTest::qWait(500);
+ QCOMPARE(submenu->rowCount(), 2);
+ }
+
+ void testExtraData()
+ {
+ // Make menu available
+ m_script.publishMenu();
+ m_script.run();
+
+ // start model
+ m_model.start();
+
+ // Wait for dbus sync
+ QTest::qWait(500);
+
+ QCOMPARE(m_model.status(), QDBusObject::Connected);
+ QCOMPARE(m_model.rowCount(), 4);
+
+ QVariant e = m_model.data(m_model.index(0, 0), QMenuModel::Extra);
+ QVERIFY(e.isValid());
+ QObject *extra = e.value<QObject*>();
+
+ // Boolean
+ QVariant v = extra->property("x-boolean");
+ QCOMPARE(v.type(), QVariant::Bool);
+ QCOMPARE(v.toBool(), true);
+
+ // Byte
+ v = extra->property("x-byte");
+ QCOMPARE(v.typeName(), "uchar");
+ QCOMPARE(v.value<uchar>(), (uchar)42);
+
+ // Int16
+ v = extra->property("x-int16");
+ QCOMPARE(v.typeName(), "short");
+ QCOMPARE(v.value<short>(), (short)-42);
+
+ // UInt16
+ v = extra->property("x-uint16");
+ QCOMPARE(v.typeName(), "ushort");
+ QCOMPARE(v.value<ushort>(), (ushort)42);
+
+ // Int32
+ v = extra->property("x-int32");
+ QCOMPARE(v.type(), QVariant::Int);
+ QCOMPARE(v.toInt(), -42);
+
+ // UInt32
+ v = extra->property("x-uint32");
+ QCOMPARE(v.type(), QVariant::UInt);
+ QCOMPARE(v.toUInt(), (uint) 42);
+
+ // Int64
+ v = extra->property("x-int64");
+ QCOMPARE(v.typeName(), "long");
+ QCOMPARE(v.value<long>(), (long) -42);
+
+ // UInt64
+ v = extra->property("x-uint64");
+ QCOMPARE(v.typeName(), "ulong");
+ QCOMPARE(v.value<ulong>(), (ulong) 42);
+
+ // Double
+ v = extra->property("x-double");
+ QCOMPARE(v.type(), QVariant::Double);
+ QCOMPARE(v.toDouble(), 42.42);
+
+ // String
+ v = extra->property("x-string");
+ QCOMPARE(v.type(), QVariant::String);
+ QCOMPARE(v.toString(), QString("42"));
+ }
+
+};
+
+QTEST_MAIN(ModelTest)
+
+#include "modeltest.moc"
diff --git a/tests/client/script_menuchangestest.py b/tests/client/script_menuchangestest.py
new file mode 100755
index 0000000..67f2ee0
--- /dev/null
+++ b/tests/client/script_menuchangestest.py
@@ -0,0 +1,14 @@
+#!/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")
+al.removeItem(0) # remove Menu0
+al.removeItem(0) # remove Menu1
+
+t = Script.create(al)
+t.run()
+
diff --git a/tests/client/script_modeltest.py b/tests/client/script_modeltest.py
new file mode 100755
index 0000000..db4e28b
--- /dev/null
+++ b/tests/client/script_modeltest.py
@@ -0,0 +1,31 @@
+#!/usr/bin/python2.7
+
+import time
+from gi.repository import GLib
+from menuscript import Script, ActionList, MENU_OBJECT_PATH
+
+al = ActionList(MENU_OBJECT_PATH)
+al.appendItem("Menu0", "Menu0Act", None, None, {'x-boolean' : GLib.Variant('b', True),
+ 'x-byte' : GLib.Variant('y', 42),
+ 'x-int16' : GLib.Variant('n', -42),
+ 'x-uint16' : GLib.Variant('q', 42),
+ 'x-int32' : GLib.Variant('i', -42),
+ 'x-uint32' : GLib.Variant('u', 42),
+ 'x-int64' : GLib.Variant('x', -42),
+ 'x-uint64' : GLib.Variant('t', 42),
+ 'x-double' : GLib.Variant('d', 42.42),
+ 'x-string' : GLib.Variant('s', u'42')
+ })
+al.appendItem("Menu1", "Menu1Act")
+
+al.appendItem("Menu2", "Menu2Act", "section")
+al.appendItem("Menu2.1", "Menu2.1Act", None, "2")
+al.appendItem("Menu2.2", "Menu2.2Act", None, "2")
+
+al.appendItem("Menu3", "Menu3Act", "submenu")
+al.appendItem("Menu3.1", "Menu3.1Act", None, "3")
+al.appendItem("Menu3.2", "Menu3.2Act", None, "3")
+
+t = Script.create(al)
+t.run()
+
diff --git a/tests/client/script_servicetest.py b/tests/client/script_servicetest.py
new file mode 100755
index 0000000..385256c
--- /dev/null
+++ b/tests/client/script_servicetest.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()
+
diff --git a/tests/client/servicetest.cpp b/tests/client/servicetest.cpp
new file mode 100644
index 0000000..3faddd0
--- /dev/null
+++ b/tests/client/servicetest.cpp
@@ -0,0 +1,101 @@
+#include "qdbusmenumodel.h"
+#include "dbusmenuscript.h"
+
+#include <QObject>
+#include <QSignalSpy>
+#include <QtTestGui>
+#include <QDebug>
+
+class ServiceTest : public QObject
+{
+ Q_OBJECT
+private:
+ DBusMenuScript m_script;
+ QDBusMenuModel m_model;
+
+ void setupModel(QDBusMenuModel *model)
+ {
+ model->setBusType(QDBusObject::SessionBus);
+ model->setBusName(MENU_SERVICE_NAME);
+ model->setObjectPath(MENU_OBJECT_PATH);
+ }
+
+private Q_SLOTS:
+ void initTestCase()
+ {
+ Q_ASSERT(m_script.connect());
+ }
+
+ void cleanupTestCase()
+ {
+ m_script.quit();
+ }
+
+ void init()
+ {
+ qDebug() << "init>>>>>>>>>>>>>>>>>>>>>";
+ m_model.stop();
+ m_model.setBusType(QDBusObject::SessionBus);
+ m_model.setBusName(MENU_SERVICE_NAME);
+ m_model.setObjectPath(MENU_OBJECT_PATH);
+ }
+
+ void cleanup()
+ {
+ qDebug() << "cleanup>>>>>>>>>>>>>>>>>>";
+ m_script.unpublishMenu();
+ }
+
+ void testMenuStartStopWithNoService()
+ {
+ m_model.start();
+ QCOMPARE(m_model.status(), QDBusObject::Connecting);
+
+ m_model.stop();
+ QCOMPARE(m_model.status(), QDBusObject::Disconnected);
+ }
+
+ void testMenuStartStopWithService()
+ {
+ // Make menu available
+ m_script.publishMenu();
+
+ // start model
+ m_model.start();
+
+ // Wait for dbus sync
+ QTest::qWait(500);
+
+ QCOMPARE(m_model.status(), QDBusObject::Connected);
+
+ // Diconnect model
+ m_model.stop();
+ QCOMPARE(m_model.status(), QDBusObject::Disconnected);
+ }
+
+ void testMenuServiceAppearAndDissapear()
+ {
+ m_model.start();
+ QCOMPARE(m_model.status(), QDBusObject::Connecting);
+
+ QSignalSpy spy(&m_model, SIGNAL(statusChanged(QDBusObject::ConnectionStatus)));
+
+ // Make menu available
+ m_script.publishMenu();
+
+ // singal changed to connected
+ QCOMPARE(spy.count(), 1);
+ QCOMPARE(m_model.status(), QDBusObject::Connected);
+
+ // remove menu service
+ m_script.unpublishMenu();
+
+ // signal changed to connecting
+ QCOMPARE(spy.count(), 2);
+ QCOMPARE(m_model.status(), QDBusObject::Connecting);
+ }
+};
+
+QTEST_MAIN(ServiceTest)
+
+#include "servicetest.moc"