on d19090f help sign in
unity8/tests/mocks/Unity/Notifications/MockNotificationModel.cpp
189 lines 5.6 kB history raw
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 */
20#include "MockNotificationModel.h"
21#include "MockNotification.h"
23#include <unity/shell/notifications/ModelInterface.h>
25#include <QTimer>
26#include <QList>
27#include <QVector>
28#include <QMap>
29#include <QStringListModel>
30#include <QQmlEngine>
31#include <QDebug>
33using namespace unity::shell::notifications;
35MockNotificationModel::MockNotificationModel(QObject *parent) : QAbstractListModel(parent) {
38MockNotificationModel::~MockNotificationModel() {
41int MockNotificationModel::rowCount(const QModelIndex &parent) const {
42 return m_queue.size();
45int MockNotificationModel::getCount() const {
46 return m_queue.size();
49QVariant MockNotificationModel::data(const QModelIndex &index, int role) const {
50 if (!index.isValid())
51 return QVariant();
53 switch(role) {
54 case ModelInterface::RoleType:
55 return QVariant(m_queue[index.row()]->getType());
57 case ModelInterface::RoleId:
58 return QVariant(m_queue[index.row()]->getID());
60 case ModelInterface::RoleSummary:
61 return QVariant(m_queue[index.row()]->getSummary());
63 case ModelInterface::RoleBody:
64 return QVariant(m_queue[index.row()]->getBody());
66 case ModelInterface::RoleValue:
67 return QVariant(m_queue[index.row()]->getValue());
69 case ModelInterface::RoleIcon:
70 return QVariant(m_queue[index.row()]->getIcon());
72 case ModelInterface::RoleSecondaryIcon:
73 return QVariant(m_queue[index.row()]->getSecondaryIcon());
75 case ModelInterface::RoleActions:
76 return QVariant::fromValue(m_queue[index.row()]->getActions());
78 case ModelInterface::RoleHints:
79 return QVariant(m_queue[index.row()]->getHints());
81 case ModelInterface::RoleNotification:
82 return QVariant::fromValue(m_queue[index.row()]);
84 default:
85 return QVariant();
86 }
89void MockNotificationModel::append(MockNotification* n) {
90 int location = m_queue.size();
91 QModelIndex insertionPoint = QModelIndex();
92 beginInsertRows(insertionPoint, location, location);
93 m_queue.insert(location, n);
94 endInsertRows();
97MockNotification* MockNotificationModel::getNotification(unsigned int id) const {
98 for(int i=0; i < m_queue.size(); i++) {
99 if(m_queue[i]->getID() == id) {
100 return m_queue[i];
101 }
102 }
104 return nullptr;
107MockNotification* MockNotificationModel::getNotification(const QString &summary) const {
108 for(int i=0; i < m_queue.size(); i++) {
109 if(m_queue[i]->getSummary() == summary) {
110 return m_queue[i];
111 }
112 }
114 return nullptr;
117bool MockNotificationModel::hasNotification(unsigned int id) const {
118 return !(getNotification(id) != nullptr);
121void MockNotificationModel::remove(const unsigned int id) {
122 for(int i = 0; i < m_queue.size(); i++) {
123 if(m_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?
131void MockNotificationModel::deleteFirst() {
132 if(m_queue.empty())
133 return;
134 deleteFromVisible(0);
137void MockNotificationModel::deleteFromVisible(int loc) {
138 QModelIndex deletePoint = QModelIndex();
139 beginRemoveRows(deletePoint, loc, loc);
140 m_queue.erase(m_queue.begin() + loc);
141 endRemoveRows();
144MockNotification* MockNotificationModel::getRaw(const unsigned int notificationId) const {
145 for(int i = 0; i < m_queue.size(); i++) {
146 if(m_queue[i]->getID() == notificationId) {
147 MockNotification* n = m_queue[i];
148 return n;
149 }
150 }
152 return nullptr;
155int MockNotificationModel::queued() const {
156 return m_queue.size();
159QHash<int, QByteArray> MockNotificationModel::roleNames() const {
160 QHash<int, QByteArray> roles;
162 roles.insert(ModelInterface::RoleType, "type");
163 roles.insert(ModelInterface::RoleUrgency, "urgency");
164 roles.insert(ModelInterface::RoleId, "nid");
165 roles.insert(ModelInterface::RoleSummary, "summary");
166 roles.insert(ModelInterface::RoleBody, "body");
167 roles.insert(ModelInterface::RoleValue, "value");
168 roles.insert(ModelInterface::RoleIcon, "icon");
169 roles.insert(ModelInterface::RoleSecondaryIcon, "secondaryIcon");
170 roles.insert(ModelInterface::RoleActions, "actions");
171 roles.insert(ModelInterface::RoleHints, "hints");
172 roles.insert(ModelInterface::RoleNotification, "notification");
174 return roles;
177void MockNotificationModel::onCompleted(unsigned int id) {
178 qDebug() << "onCompleted(" << id << ")";
179 remove(id);
182void MockNotificationModel::onDataChanged(unsigned int id) {
183 for(int i = 0; i < m_queue.size(); i++) {
184 if(m_queue[i]->getID() == id) {
185 Q_EMIT dataChanged(index(i, 0), index(i, 0));
186 break;
187 }
188 }