diff options
author | Dominik Holland <dominik.holland@pelagicore.com> | 2019-02-28 16:02:01 +0100 |
---|---|---|
committer | Robert Griebl <robert.griebl@pelagicore.com> | 2019-03-14 10:10:27 +0000 |
commit | 3611624f97c0b11afb2ddfd6c28ee75b63e26cff (patch) | |
tree | bf69f334fbb892265642ee4bfdc4475aa2f864c0 /tests/auto/core | |
parent | 8bc29da8b94d929b9c59f2516c1013372263b48f (diff) | |
download | qtivi-3611624f97c0b11afb2ddfd6c28ee75b63e26cff.tar.gz |
ivigenerator: Add model support to the "backend_qtro" template
Fixes: AUTOSUITE-599
Change-Id: I45e8a00146556b0df159479f703e4bf74cf0c9c6
Reviewed-by: Robert Griebl <robert.griebl@pelagicore.com>
Diffstat (limited to 'tests/auto/core')
8 files changed, 214 insertions, 2 deletions
diff --git a/tests/auto/core/ivigenerator/org.example.echo.qtro.qface b/tests/auto/core/ivigenerator/org.example.echo.qtro.qface index 340fabb..edbcd24 100644 --- a/tests/auto/core/ivigenerator/org.example.echo.qtro.qface +++ b/tests/auto/core/ivigenerator/org.example.echo.qtro.qface @@ -38,6 +38,8 @@ interface Echo { signal foobar(string foo); signal somethingHappened(); signal newValueAvailable(var newValue); + + model<Contact> testList; } enum WeekDay { diff --git a/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/contactsmodelservice.cpp b/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/contactsmodelservice.cpp new file mode 100644 index 0000000..2e7d542 --- /dev/null +++ b/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/contactsmodelservice.cpp @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Copyright (C) 2019 Luxoft Sweden AB +** Copyright (C) 2018 Pelagicore AG +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtIvi module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT-QTAS$ +** Commercial License Usage +** Licensees holding valid commercial Qt Automotive Suite licenses may use +** this file in accordance with the commercial license agreement provided +** with the Software or, alternatively, in accordance with the terms +** contained in a written agreement between you and The Qt Company. For +** licensing terms and conditions see https://www.qt.io/terms-conditions. +** For further information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "contactsmodelservice.h" + +ContactsModelService::ContactsModelService(QObject* parent) + : PagingModelSimpleSource(parent) +{ +} + +void ContactsModelService::registerInstance(const QUuid &identifier) +{ + Q_UNUSED(identifier) +} + +void ContactsModelService::unregisterInstance(const QUuid &identifier) +{ + Q_UNUSED(identifier) +} + +void ContactsModelService::fetchData(const QUuid &identifier, int start, int count) +{ + QVariantList list; + int max = qMin(start + count, m_list.count()); + for (int i=start; i < max; i++) + list.append(QVariant::fromValue(m_list.at(i))); + + emit dataFetched(identifier, list, start, max < m_list.count()); +} + +void ContactsModelService::insert(int index, const Contact &item) +{ + m_list.insert(index, item); + + emit dataChanged(QUuid(), { QVariant::fromValue(item) }, index, 0); +} + +void ContactsModelService::remove(int index) +{ + m_list.removeAt(index); + + emit dataChanged(QUuid(), QVariantList(), index, 1); +} + +void ContactsModelService::move(int currentIndex, int newIndex) +{ + int min = qMin(currentIndex, newIndex); + int max = qMax(currentIndex, newIndex); + + m_list.move(currentIndex, newIndex); + QVariantList variantList; + for (int i = min; i <= max; i++) + variantList.append(QVariant::fromValue(m_list.at(i))); + + emit dataChanged(QUuid(), variantList, min, max - min + 1); +} + +void ContactsModelService::reset() +{ + emit dataChanged(QUuid(), QVariantList(), 0, m_list.count()); + m_list.clear(); +} + +void ContactsModelService::update(int index, const Contact &item) +{ + m_list[index] = item; + emit dataChanged(QUuid(), { QVariant::fromValue(item) }, index, 1); +} + +const Contact &ContactsModelService::at(int index) const +{ + return m_list.at(index); +} diff --git a/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/contactsmodelservice.h b/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/contactsmodelservice.h new file mode 100644 index 0000000..5a257f5 --- /dev/null +++ b/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/contactsmodelservice.h @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2019 Luxoft Sweden AB +** Copyright (C) 2018 Pelagicore AG +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtIvi module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT-QTAS$ +** Commercial License Usage +** Licensees holding valid commercial Qt Automotive Suite licenses may use +** this file in accordance with the commercial license agreement provided +** with the Software or, alternatively, in accordance with the terms +** contained in a written agreement between you and The Qt Company. For +** licensing terms and conditions see https://www.qt.io/terms-conditions. +** For further information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef CONTACTSMODELSERVICE_H +#define CONTACTSMODELSERVICE_H + +#include "contact.h" +#include "rep_pagingmodel_source.h" + +class ContactsModelService : public PagingModelSimpleSource +{ +public: + ContactsModelService(QObject* parent = nullptr); + + void registerInstance(const QUuid &identifier) override; + void unregisterInstance(const QUuid &identifier) override; + + void fetchData(const QUuid &identifier, int start, int count) override; + +public Q_SLOTS: + void insert(int index, const Contact &item); + void remove(int index); + void move(int currentIndex, int newIndex); + void reset(); + void update(int index, const Contact &item); + const Contact &at(int index) const; + +private: + QList<Contact> m_list; +}; + +#endif // CONTACTSMODELSERVICE_H diff --git a/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/server.cpp b/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/server.cpp index 192ebca..a1e5083 100644 --- a/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/server.cpp +++ b/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/server.cpp @@ -35,6 +35,7 @@ bool Server::start() { bool val = true; val = val && Core::instance()->host()->enableRemoting(&m_echoService, QStringLiteral("org.example.echo.Echo")); + val = val && Core::instance()->host()->enableRemoting(&m_contactsModelService, QStringLiteral("org.example.echo.Echo.testList")); val = val && Core::instance()->host()->enableRemoting(&m_echoZonedService, QStringLiteral("org.example.echo.EchoZoned")); //Give QtRO time to announce the service QTest::qWait(200); @@ -45,6 +46,7 @@ bool Server::stop() { bool val = true; val = val && Core::instance()->host()->disableRemoting(&m_echoService); + val = val && Core::instance()->host()->disableRemoting(&m_contactsModelService); val = val && Core::instance()->host()->disableRemoting(&m_echoZonedService); //Give QtRO time to send the disconnect message to the Replica QTest::qWait(200); diff --git a/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/server.h b/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/server.h index 7c1397b..d5763be 100644 --- a/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/server.h +++ b/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/server.h @@ -32,6 +32,7 @@ #include "echoservice.h" #include "echozonedservice.h" +#include "contactsmodelservice.h" class Server : public QObject { @@ -44,6 +45,7 @@ public Q_SLOTS: public: EchoService m_echoService; EchoZonedService m_echoZonedService; + ContactsModelService m_contactsModelService; ~Server(); diff --git a/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/server_qtro_test.pro b/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/server_qtro_test.pro index 92f8402..ba49e98 100644 --- a/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/server_qtro_test.pro +++ b/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/server_qtro_test.pro @@ -16,7 +16,8 @@ SOURCES += main.cpp \ server.cpp \ echoservice.cpp \ tst_echoqtro.cpp \ - echozonedservice.cpp + echozonedservice.cpp \ + contactsmodelservice.cpp QFACE_FORMAT = server_qtro QFACE_SOURCES = ../../../org.example.echo.qtro.qface @@ -25,7 +26,8 @@ HEADERS += \ server.h \ echoservice.h \ tst_echoqtro.h \ - echozonedservice.h + echozonedservice.h \ + contactsmodelservice.h QMAKE_RPATHDIR += $$OUT_PWD/.. QMAKE_RPATHDIR += $$OUT_PWD/../qtivi diff --git a/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/tst_echoqtro.cpp b/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/tst_echoqtro.cpp index 58cd8c6..c1f520a 100644 --- a/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/tst_echoqtro.cpp +++ b/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/tst_echoqtro.cpp @@ -867,3 +867,49 @@ void EchoQtroTest::testSignals() server.m_echoZonedService.somethingHappened(frontLeftZone); WAIT_AND_COMPARE(zonedSomethingSpy, 1); } + +void EchoQtroTest::testModel() +{ + Server server; + server.start(); + + Echo client; + QSignalSpy initSpy(&client, SIGNAL(isInitializedChanged(bool))); + QVERIFY(initSpy.isValid()); + QVERIFY(client.startAutoDiscovery() == QIviAbstractFeature::ProductionBackendLoaded); + + //wait until the client has connected and initial values are set + WAIT_AND_COMPARE(initSpy, 1); + QVERIFY(client.isInitialized()); + + //Give QtRO time to actually call our server side + QTest::qWait(200); + + QIviPagingModel* model = client.testList(); + QVERIFY(model->isInitialized()); + QCOMPARE(model->rowCount(), 0); + + //Test inserting a row + Contact testContact(QStringLiteral("Mr A."), 20, false, "foo"); + QSignalSpy countSpy(model, SIGNAL(countChanged())); + server.m_contactsModelService.insert(0, testContact); + + WAIT_AND_COMPARE(countSpy, 1); + QCOMPARE(model->rowCount(), 1); + QCOMPARE(model->at<Contact>(0), testContact); + countSpy.clear(); + + //test updating a row + QSignalSpy changedSpy(model, SIGNAL(dataChanged( QModelIndex, QModelIndex, QVector<int>))); + Contact updatedContact(QStringLiteral("Mr B."), 30, true, QVariant()); + server.m_contactsModelService.update(0, updatedContact); + + WAIT_AND_COMPARE(changedSpy, 1); + QCOMPARE(model->rowCount(), 1); + QCOMPARE(model->at<Contact>(0), updatedContact); + + //Test removing a row + server.m_contactsModelService.remove(0); + WAIT_AND_COMPARE(countSpy, 1); + QCOMPARE(model->rowCount(), 0); +} diff --git a/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/tst_echoqtro.h b/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/tst_echoqtro.h index 9c42b3b..84aabcd 100644 --- a/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/tst_echoqtro.h +++ b/tests/auto/core/ivigenerator/projects/org-example-echo-qtro/server_qtro_test/tst_echoqtro.h @@ -53,6 +53,7 @@ private slots: void testZonedSlots(); void testMultipleSlotCalls(); void testSignals(); + void testModel(); }; #endif // ECHOQTROTEST_H |