2
* Copyright 2014-2015 Canonical Ltd.
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.
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.
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/>.
18
#include <unity/shell/launcher/LauncherModelInterface.h>
19
#include <unity/shell/application/ApplicationInfoInterface.h>
21
#include "launcheritem.h"
22
#include "launchermodelas.h"
23
#include "AccountsServiceDBusAdaptor.h"
27
QHash<QString, QList<QVariantMap>> mockProperties;
29
class LauncherModelASTest : public QObject
34
LauncherModel *launcherModel;
39
// Prepare some fake list
40
QList<QVariantMap> list;
42
item.insert("id", "appId1");
43
item.insert("name", "Item 1");
44
item.insert("icon", "fake.svg");
45
item.insert("count", 0);
46
item.insert("countVisible", false);
47
item.insert("pinned", true);
49
item["id"] = "appId2";
50
item["name"] = "Item 2";
52
mockProperties["user1"] = list;
55
bool isInSync(LauncherModel *model, const QList<QVariantMap> &properties) {
56
QList<QVariantMap> list;
57
// Strip unpinned if onlyPinned is set
58
Q_FOREACH (const QVariantMap &map, properties) {
59
if (!model->onlyPinned() || map.value("pinned").toBool()) {
64
bool inSync = model->rowCount() == list.count();
65
for (int i = 0; inSync && i < model->rowCount(); i++) {
66
inSync &= model->get(i)->appId() == list.at(i).value("id").toString();
67
inSync &= model->get(i)->name() == list.at(i).value("name").toString();
68
inSync &= model->get(i)->icon() == list.at(i).value("icon").toString();
69
inSync &= model->get(i)->count() == list.at(i).value("count").toInt();
70
inSync &= model->get(i)->countVisible() == list.at(i).value("countVisible").toBool();
75
void testLoadASOnStartup() {
77
LauncherModel* model = new LauncherModel(this);
79
// We didn't set a user yet. model should be empty
80
QCOMPARE(model->rowCount(), 0);
82
model->setUser("user1");
84
QCOMPARE(isInSync(model, mockProperties["user1"]), true);
88
void testASChangedUpdatesModel_data() {
89
QTest::addColumn<QString>("modelUser");
90
QTest::addColumn<QString>("changedUser");
91
QTest::addColumn<bool>("inSync");
93
QTest::newRow("this user changed") << "user1" << "user1" << true;
94
QTest::newRow("other user changed") << "user1" << "user2" << false;
97
void testASChangedUpdatesModel() {
98
QFETCH(QString, modelUser);
99
QFETCH(QString, changedUser);
100
QFETCH(bool, inSync);
102
LauncherModel* model = new LauncherModel(this);
103
model->setUser(modelUser);
105
int oldCount = mockProperties[modelUser].count();
106
QCOMPARE(model->rowCount(), oldCount);
108
QList<QVariantMap> newList;
109
QVariantMap newEntry;
110
newEntry.insert("id", "newappId");
111
newEntry.insert("name", "New app");
112
newEntry.insert("icon", "some-icon.svg");
113
newEntry.insert("count", 0);
114
newEntry.insert("countVisible", false);
115
newEntry.insert("pinned", true);
116
newList.append(newEntry);
117
model->m_accounts->simulatePropertyChange(changedUser, "LauncherItems", QVariant::fromValue(newList));
119
QCOMPARE(isInSync(model, mockProperties[modelUser]), true);
120
QCOMPARE(isInSync(model, mockProperties[changedUser]), inSync);
122
model->deleteLater();
125
void testUpdateCount() {
126
LauncherModel* model = new LauncherModel(this);
127
model->setUser("user1");
129
QCOMPARE(model->get(0)->countVisible(), false);
130
QCOMPARE(model->get(0)->count(), 0);
132
QList<QVariantMap> newList = mockProperties["user1"];
133
QVariantMap entry = newList.at(0);
134
entry["countVisible"] = true;
137
model->m_accounts->simulatePropertyChange("user1", "LauncherItems", QVariant::fromValue(newList));
139
QCOMPARE(isInSync(model, mockProperties["user1"]), true);
141
model->deleteLater();
144
void testOnlyPinned() {
145
LauncherModel *model = new LauncherModel(this);
146
model->setUser("user1");
147
model->setOnlyPinned(true);
149
QCOMPARE(isInSync(model, mockProperties["user1"]), true);
151
// Let's unpin one item
152
QList<QVariantMap> newList = mockProperties["user1"];
153
QVariantMap entry = newList.at(0);
154
entry["pinned"] = false;
156
model->m_accounts->simulatePropertyChange("user1", "LauncherItems", QVariant::fromValue(newList));
157
QCOMPARE(isInSync(model, mockProperties["user1"]), true);
159
// Now toggle onlyPinned and make sure the model keeps up
160
model->setOnlyPinned(false);
161
QCOMPARE(isInSync(model, mockProperties["user1"]), true);
163
model->setOnlyPinned(true);
164
QCOMPARE(isInSync(model, mockProperties["user1"]), true);
168
QTEST_GUILESS_MAIN(LauncherModelASTest)
169
#include "launchermodelastest.moc"