on 44dae67 help sign in
unity8/tests/mocks/Unity/Notifications/MockNotificationModel.cpp
183 lines 5.5 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>
32using namespace unity::shell::notifications;
34MockNotificationModel::MockNotificationModel(QObject *parent) : QAbstractListModel(parent) {
37MockNotificationModel::~MockNotificationModel() {
40int MockNotificationModel::rowCount(const QModelIndex &parent) const {
41 return m_queue.size();
44int MockNotificationModel::getCount() const {
45 return m_queue.size();
48QVariant MockNotificationModel::data(const QModelIndex &index, int role) const {
49 if (!index.isValid())
50 return QVariant();
52 switch(role) {
53 case ModelInterface::RoleType:
54 return QVariant(m_queue[index.row()]->getType());
56 case ModelInterface::RoleId:
57 return QVariant(m_queue[index.row()]->getID());
59 case ModelInterface::RoleSummary:
60 return QVariant(m_queue[index.row()]->getSummary());
62 case ModelInterface::RoleBody:
63 return QVariant(m_queue[index.row()]->getBody());
65 case ModelInterface::RoleValue:
66 return QVariant(m_queue[index.row()]->getValue());
68 case ModelInterface::RoleIcon:
69 return QVariant(m_queue[index.row()]->getIcon());
71 case ModelInterface::RoleSecondaryIcon:
72 return QVariant(m_queue[index.row()]->getSecondaryIcon());
74 case ModelInterface::RoleActions:
75 return QVariant::fromValue(m_queue[index.row()]->getActions());
77 case ModelInterface::RoleHints:
78 return QVariant(m_queue[index.row()]->getHints());
80 case ModelInterface::RoleNotification:
81 return QVariant::fromValue(m_queue[index.row()]);
83 default:
84 return QVariant();
85 }
88void MockNotificationModel::append(MockNotification* n) {
89 int location = m_queue.size();
90 QModelIndex insertionPoint = QModelIndex();
91 beginInsertRows(insertionPoint, location, location);
92 m_queue.insert(location, n);
93 endInsertRows();
96MockNotification* MockNotificationModel::getNotification(unsigned int id) const {
97 for(int i=0; i < m_queue.size(); i++) {
98 if(m_queue[i]->getID() == id) {
99 return m_queue[i];
100 }
101 }
103 return nullptr;
106MockNotification* MockNotificationModel::getNotification(const QString &summary) const {
107 for(int i=0; i < m_queue.size(); i++) {
108 if(m_queue[i]->getSummary() == summary) {
109 return m_queue[i];
110 }
111 }
113 return nullptr;
116bool MockNotificationModel::hasNotification(unsigned int id) const {
117 return !(getNotification(id) != nullptr);
120void MockNotificationModel::remove(const unsigned int id) {
121 for(int i = 0; i < m_queue.size(); i++) {
122 if(m_queue[i]->getID() == id) {
123 deleteFromVisible(i);
124 return;
125 }
126 }
127 // The ID was not found in any queue. Should it be an error case or not?
130void MockNotificationModel::deleteFirst() {
131 if(m_queue.empty())
132 return;
133 deleteFromVisible(0);
136void MockNotificationModel::deleteFromVisible(int loc) {
137 QModelIndex deletePoint = QModelIndex();
138 beginRemoveRows(deletePoint, loc, loc);
139 m_queue.erase(m_queue.begin() + loc);
140 endRemoveRows();
143MockNotification* MockNotificationModel::getRaw(const unsigned int notificationId) const {
144 for(int i = 0; i < m_queue.size(); i++) {
145 if(m_queue[i]->getID() == notificationId) {
146 MockNotification* n = m_queue[i];
147 return n;
148 }
149 }
151 return nullptr;
154int MockNotificationModel::queued() const {
155 return m_queue.size();
158QHash<int, QByteArray> MockNotificationModel::roleNames() const {
159 QHash<int, QByteArray> roles;
161 roles.insert(ModelInterface::RoleType, "type");
162 roles.insert(ModelInterface::RoleUrgency, "urgency");
163 roles.insert(ModelInterface::RoleId, "nid");
164 roles.insert(ModelInterface::RoleSummary, "summary");
165 roles.insert(ModelInterface::RoleBody, "body");
166 roles.insert(ModelInterface::RoleValue, "value");
167 roles.insert(ModelInterface::RoleIcon, "icon");
168 roles.insert(ModelInterface::RoleSecondaryIcon, "secondaryIcon");
169 roles.insert(ModelInterface::RoleActions, "actions");
170 roles.insert(ModelInterface::RoleHints, "hints");
171 roles.insert(ModelInterface::RoleNotification, "notification");
173 return roles;
176void MockNotificationModel::onDataChanged(unsigned int id) {
177 for(int i = 0; i < m_queue.size(); i++) {
178 if(m_queue[i]->getID() == id) {
179 Q_EMIT dataChanged(index(i, 0), index(i, 0));
180 break;
181 }
182 }