on bionic help sign in
unity8/tests/mocks/Unity/Notifications/MockNotificationModel.cpp
169 lines 5.0 kB history raw
1/*
2 * Copyright 2015-2016 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 */
18#include "MockNotificationModel.h"
19#include "MockNotification.h"
21#include <unity/shell/notifications/ModelInterface.h>
23#include <QTimer>
24#include <QList>
25#include <QVector>
26#include <QMap>
27#include <QStringListModel>
28#include <QQmlEngine>
30using namespace unity::shell::notifications;
32MockNotificationModel::MockNotificationModel(QObject *parent) : QAbstractListModel(parent) {
35MockNotificationModel::~MockNotificationModel() {
36 Q_FOREACH(MockNotification *n, m_queue) {
37 n->deleteLater();
38 }
39 m_queue.clear();
42int MockNotificationModel::rowCount(const QModelIndex &) const {
43 return m_queue.size();
46int MockNotificationModel::getCount() const {
47 return m_queue.size();
50QVariant MockNotificationModel::data(const QModelIndex &index, int role) const {
51 if (!index.isValid())
52 return QVariant();
54 switch(role) {
55 case ModelInterface::RoleType:
56 return QVariant(m_queue[index.row()]->getType());
58 case ModelInterface::RoleId:
59 return QVariant(m_queue[index.row()]->getID());
61 case ModelInterface::RoleSummary:
62 return QVariant(m_queue[index.row()]->getSummary());
64 case ModelInterface::RoleBody:
65 return QVariant(m_queue[index.row()]->getBody());
67 case ModelInterface::RoleValue:
68 return QVariant(m_queue[index.row()]->getValue());
70 case ModelInterface::RoleIcon:
71 return QVariant(m_queue[index.row()]->getIcon());
73 case ModelInterface::RoleSecondaryIcon:
74 return QVariant(m_queue[index.row()]->getSecondaryIcon());
76 case ModelInterface::RoleActions:
77 return QVariant::fromValue(m_queue[index.row()]->getActions());
79 case ModelInterface::RoleHints:
80 return QVariant(m_queue[index.row()]->getHints());
82 case ModelInterface::RoleNotification:
83 return QVariant::fromValue(m_queue[index.row()]);
85 default:
86 return QVariant();
87 }
90void MockNotificationModel::append(MockNotification* n) {
91 QQmlEngine::setObjectOwnership(n, QQmlEngine::CppOwnership);
92 int location = m_queue.size();
93 QModelIndex insertionPoint = QModelIndex();
94 beginInsertRows(insertionPoint, location, location);
95 m_queue.insert(location, n);
96 endInsertRows();
99MockNotification* MockNotificationModel::getNotification(int id) const {
100 for(int i = 0; i < m_queue.size(); i++) {
101 MockNotification* n = m_queue.at(i);
102 if(n && n->getID() == id) {
103 return n;
104 }
105 }
107 return nullptr;
110void MockNotificationModel::remove(const int id) {
111 for(int i = 0; i < m_queue.size(); i++) {
112 if(m_queue[i]->getID() == id) {
113 removeInternal(i);
114 return;
115 }
116 }
119void MockNotificationModel::removeFirst() {
120 if(m_queue.isEmpty())
121 return;
122 removeInternal(0);
125void MockNotificationModel::removeInternal(int loc) {
126 QModelIndex deletePoint = QModelIndex();
127 beginRemoveRows(deletePoint, loc, loc);
128 m_queue.erase(m_queue.begin() + loc);
129 endRemoveRows();
132MockNotification* MockNotificationModel::getRaw(const int notificationId) const {
133 return getNotification(notificationId);
136int MockNotificationModel::queued() const {
137 return m_queue.size();
140QHash<int, QByteArray> MockNotificationModel::roleNames() const {
141 QHash<int, QByteArray> roles;
143 roles.insert(ModelInterface::RoleType, "type");
144 roles.insert(ModelInterface::RoleUrgency, "urgency");
145 roles.insert(ModelInterface::RoleId, "id");
146 roles.insert(ModelInterface::RoleSummary, "summary");
147 roles.insert(ModelInterface::RoleBody, "body");
148 roles.insert(ModelInterface::RoleValue, "value");
149 roles.insert(ModelInterface::RoleIcon, "icon");
150 roles.insert(ModelInterface::RoleSecondaryIcon, "secondaryIcon");
151 roles.insert(ModelInterface::RoleActions, "actions");
152 roles.insert(ModelInterface::RoleHints, "hints");
153 roles.insert(ModelInterface::RoleNotification, "notification");
155 return roles;
158void MockNotificationModel::onCompleted(int id) {
159 remove(id);
162void MockNotificationModel::onDataChanged(int id) {
163 for(int i = 0; i < m_queue.size(); i++) {
164 if(m_queue[i]->getID() == id) {
165 Q_EMIT dataChanged(index(i, 0), index(i, 0));
166 break;
167 }
168 }