on xenial help sign in
A bit of refactoring and forgot to add the MockNotificationModel to the repository.
commitd4ebb4d16700f2c3ea1f3f1a65b8eeb7414aee17 authorMirco Müller <mirco.mueller@ubuntu.com> dateWed 14 Jan 2015, 11:48 (12 years ago) parent95fca22
6 files changed+260−6
tests/mocks/Unity/Notifications/CMakeLists.txt+1−1 tests/mocks/Unity/Notifications/MockNotificationTypes.cpp → tests/mocks/Unity/Notifications/MockNotification.cpp+1−1 tests/mocks/Unity/Notifications/MockNotificationTypes.h → tests/mocks/Unity/Notifications/MockNotification.h+3−3 tests/mocks/Unity/Notifications/MockNotificationModel.cpp+185 tests/mocks/Unity/Notifications/MockNotificationModel.h+69 tests/mocks/Unity/Notifications/plugin.cpp+1−1
tests/mocks/Unity/Notifications/CMakeLists.txt+1−1
@@ -4,7 +4,7 @@ include_directories(
44
55set(MockNotificationsPlugin_SOURCES
66 plugin.cpp
7− MockNotificationTypes.cpp
7+ MockNotification.cpp
88 MockNotificationModel.cpp
99 MockActionModel.cpp
1010)
tests/mocks/Unity/Notifications/MockNotificationTypes.cpp → tests/mocks/Unity/Notifications/MockNotification.cpprenamed+1−1
@@ -17,7 +17,7 @@
1717 * Mirco Mueller <mirco.mueller@canonical.com>
1818 */
1919
20−#include "MockNotificationTypes.h"
20+#include "MockNotification.h"
2121
2222struct MockNotificationPrivate {
2323 unsigned int id;
tests/mocks/Unity/Notifications/MockNotificationTypes.h → tests/mocks/Unity/Notifications/MockNotification.hrenamed+3−3
@@ -17,8 +17,8 @@
1717 * Mirco Mueller <mirco.mueller@canonical.com>
1818 */
1919
20−#ifndef MOCK_NOTIFICATION_TYPES_H
21−#define MOCK_NOTIFICATION_TYPES_H
20+#ifndef MOCK_NOTIFICATION_H
21+#define MOCK_NOTIFICATION_H
2222
2323#include "MockActionModel.h"
2424#include <QObject>
@@ -90,4 +90,4 @@ public:
9090 Q_INVOKABLE void close();
9191};
9292
93−#endif // MOCK_NOTIFICATION_TYPES_H
93+#endif // MOCK_NOTIFICATION_H
tests/mocks/Unity/Notifications/MockNotificationModel.cppnew file+185
@@ -0,0 +1,185 @@
1+/*
2+ * Copyright 2015 Canonical Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU Lesser General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU Lesser General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU Lesser General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ *
16+ * Authors:
17+ * Mirco Mueller <mirco.mueller@canonical.com>
18+ */
19+
20+#include "MockNotificationModel.h"
21+#include "MockNotification.h"
22+
23+#include <unity/shell/notifications/ModelInterface.h>
24+
25+#include <QTimer>
26+#include <QList>
27+#include <QVector>
28+#include <QMap>
29+#include <QStringListModel>
30+#include <QQmlEngine>
31+#include <QDebug>
32+
33+using namespace unity::shell::notifications;
34+
35+struct MockNotificationModelPrivate {
36+ QList<QSharedPointer<MockNotification> > queue;
37+};
38+
39+MockNotificationModel::MockNotificationModel(QObject *parent) : QAbstractListModel(parent), p(new MockNotificationModelPrivate) {
40+}
41+
42+MockNotificationModel::~MockNotificationModel() {
43+}
44+
45+int MockNotificationModel::rowCount(const QModelIndex &parent) const {
46+ return p->queue.size();
47+}
48+
49+QVariant MockNotificationModel::data(const QModelIndex &index, int role) const {
50+ //printf("Data %d.\n", index.row());
51+ if (!index.isValid())
52+ return QVariant();
53+
54+ switch(role) {
55+ case ModelInterface::RoleType:
56+ return QVariant(p->queue[index.row()]->getType());
57+
58+ case ModelInterface::RoleId:
59+ return QVariant(p->queue[index.row()]->getID());
60+
61+ case ModelInterface::RoleSummary:
62+ return QVariant(p->queue[index.row()]->getSummary());
63+
64+ case ModelInterface::RoleBody:
65+ return QVariant(p->queue[index.row()]->getBody());
66+
67+ case ModelInterface::RoleValue:
68+ return QVariant(p->queue[index.row()]->getValue());
69+
70+ case ModelInterface::RoleIcon:
71+ return QVariant(p->queue[index.row()]->getIcon());
72+
73+ case ModelInterface::RoleSecondaryIcon:
74+ return QVariant(p->queue[index.row()]->getSecondaryIcon());
75+
76+ case ModelInterface::RoleActions:
77+ return QVariant::fromValue(p->queue[index.row()]->getActions());
78+
79+ case ModelInterface::RoleHints:
80+ return QVariant(p->queue[index.row()]->getHints());
81+
82+ case ModelInterface::RoleNotification:
83+ return QVariant(p->queue[index.row()]);
84+
85+ default:
86+ return QVariant();
87+ }
88+}
89+
90+void MockNotificationModel::append(const QSharedPointer<MockNotification> &n) {
91+ p->queue.append(n);
92+ //qDebug() << "append() called, count:" << p->queue.size() << ", summary: " << QString(p->queue[0]->getSummary());
93+}
94+
95+QSharedPointer<MockNotification> MockNotificationModel::getNotification(unsigned int id) const {
96+ for(int i=0; i<p->queue.size(); i++) {
97+ if(p->queue[i]->getID() == id) {
98+ return p->queue[i];
99+ }
100+ }
101+
102+ QSharedPointer<MockNotification> empty;
103+ return empty;
104+}
105+
106+QSharedPointer<MockNotification> MockNotificationModel::getNotification(const QString &summary) const {
107+ for(int i=0; i<p->queue.size(); i++) {
108+ if(p->queue[i]->getSummary() == summary) {
109+ return p->queue[i];
110+ }
111+ }
112+
113+ QSharedPointer<MockNotification> empty;
114+ return empty;
115+}
116+
117+bool MockNotificationModel::hasNotification(unsigned int id) const {
118+ return !(getNotification(id) != nullptr);
119+}
120+
121+void MockNotificationModel::removeNotification(const unsigned int id) {
122+ for(int i=0; i<p->queue.size(); i++) {
123+ if(p->queue[i]->getID() == id) {
124+ deleteFromVisible(i);
125+ return;
126+ }
127+ }
128+ // The ID was not found in any queue. Should it be an error case or not?
129+}
130+
131+void MockNotificationModel::deleteFirst() {
132+ if(p->queue.empty())
133+ return;
134+ deleteFromVisible(0);
135+}
136+
137+void MockNotificationModel::deleteFromVisible(int loc) {
138+ QModelIndex deletePoint = QModelIndex();
139+ beginRemoveRows(deletePoint, loc, loc);
140+ p->queue.erase(p->queue.begin() + loc);
141+ endRemoveRows();
142+}
143+
144+MockNotification* MockNotificationModel::getRaw(const unsigned int notificationId) const {
145+ for(int i=0; i<p->queue.size(); i++) {
146+ if(p->queue[i]->getID() == notificationId) {
147+ MockNotification* n = p->queue[i].data();
148+ QQmlEngine::setObjectOwnership(n, QQmlEngine::CppOwnership);
149+ return n;
150+ }
151+ }
152+
153+ return nullptr;
154+}
155+
156+int MockNotificationModel::queued() const {
157+ return p->queue.size();
158+}
159+
160+QHash<int, QByteArray> MockNotificationModel::roleNames() const {
161+ QHash<int, QByteArray> roles;
162+
163+ roles.insert(ModelInterface::RoleType, "type");
164+ roles.insert(ModelInterface::RoleUrgency, "urgency");
165+ roles.insert(ModelInterface::RoleId, "nid");
166+ roles.insert(ModelInterface::RoleSummary, "summary");
167+ roles.insert(ModelInterface::RoleBody, "body");
168+ roles.insert(ModelInterface::RoleValue, "value");
169+ roles.insert(ModelInterface::RoleIcon, "icon");
170+ roles.insert(ModelInterface::RoleSecondaryIcon, "secondaryIcon");
171+ roles.insert(ModelInterface::RoleActions, "actions");
172+ roles.insert(ModelInterface::RoleHints, "hints");
173+ roles.insert(ModelInterface::RoleNotification, "notification");
174+
175+ return roles;
176+}
177+
178+void MockNotificationModel::onDataChanged(unsigned int id) {
179+ for(int i=0; i<p->queue.size(); i++) {
180+ if(p->queue[i]->getID() == id) {
181+ Q_EMIT dataChanged(index(i, 0), index(i, 0));
182+ break;
183+ }
184+ }
185+}
tests/mocks/Unity/Notifications/MockNotificationModel.hnew file+69
@@ -0,0 +1,69 @@
1+/*
2+ * Copyright 2015 Canonical Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU Lesser General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU Lesser General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU Lesser General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ *
16+ * Authors:
17+ * Mirco Mueller <mirco.mueller@canonical.com>
18+ */
19+
20+#ifndef MOCK_NOTIFICATION_MODEL_H
21+#define MOCK_NOTIFICATION_MODEL_H
22+
23+#include <QAbstractListModel>
24+#include <QSharedPointer>
25+#include <QScopedPointer>
26+#include "MockNotification.h"
27+
28+class MockNotification;
29+
30+struct MockNotificationModelPrivate;
31+
32+class MockNotificationModel : public QAbstractListModel {
33+ Q_OBJECT
34+
35+public:
36+ MockNotificationModel(QObject *parent=nullptr);
37+ virtual ~MockNotificationModel();
38+
39+ virtual int rowCount(const QModelIndex &parent) const;
40+ virtual QVariant data(const QModelIndex &index, int role) const;
41+ virtual QHash<int, QByteArray> roleNames() const;
42+
43+ Q_INVOKABLE void append(const QSharedPointer<MockNotification> &n);
44+ QSharedPointer<MockNotification> getNotification(unsigned int id) const;
45+ QSharedPointer<MockNotification> getNotification(const QString &summary) const;
46+ bool hasNotification(unsigned int id) const;
47+
48+ // getRaw() is only meant to be used from QML, since QML cannot handle
49+ // QSharedPointers... on C++-side only use getNotification()
50+ Q_INVOKABLE MockNotification* getRaw(const unsigned int notificationId) const;
51+
52+ Q_INVOKABLE int queued() const;
53+ Q_INVOKABLE void removeNotification(const unsigned int id);
54+
55+private Q_SLOTS:
56+ void onDataChanged(unsigned int id);
57+
58+Q_SIGNALS:
59+ void queueSizeChanged(int newSize);
60+
61+private:
62+ QScopedPointer<MockNotificationModelPrivate> p;
63+
64+ void localAppend(const MockNotification& n);
65+ void deleteFromVisible(int loc);
66+ void deleteFirst();
67+};
68+
69+#endif
tests/mocks/Unity/Notifications/plugin.cpp+1−1
@@ -19,7 +19,7 @@
1919
2020#include "plugin.h"
2121#include "MockActionModel.h"
22−#include "MockNotificationTypes.h"
22+#include "MockNotification.h"
2323#include "MockNotificationModel.h"
2424
2525#include <QtQml/qqml.h>