on 955296e help sign in
unity8/tests/plugins/Greeter/Unity/Launcher/launchermodelastest.cpp
172 lines 5.9 kB history raw
1/*
2 * Copyright 2013 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 * Michael Zanetti <michael.zanetti@canonical.com>
18 */
20// unity-api
21#include <unity/shell/launcher/LauncherModelInterface.h>
22#include <unity/shell/application/ApplicationInfoInterface.h>
24#include "launcheritem.h"
25#include "launchermodelas.h"
26#include "AccountsServiceDBusAdaptor.h"
28#include <QtTest>
30QHash<QString, QList<QVariantMap>> mockProperties;
32class LauncherModelASTest : public QObject
34 Q_OBJECT
36private:
37 LauncherModel *launcherModel;
39private Q_SLOTS:
41 void init() {
42 // Prepare some fake list
43 QList<QVariantMap> list;
44 QVariantMap item;
45 item.insert("id", "appId1");
46 item.insert("name", "Item 1");
47 item.insert("icon", "fake.svg");
48 item.insert("count", 0);
49 item.insert("countVisible", false);
50 item.insert("pinned", true);
51 list.append(item);
52 item["id"] = "appId2";
53 item["name"] = "Item 2";
54 list.append(item);
55 mockProperties["user1"] = list;
56 }
58 bool isInSync(LauncherModel *model, const QList<QVariantMap> &properties) {
59 QList<QVariantMap> list;
60 // Strip unpinned if onlyPinned is set
61 Q_FOREACH (const QVariantMap &map, properties) {
62 if (!model->onlyPinned() || map.value("pinned").toBool()) {
63 list << map;
64 }
65 }
67 bool inSync = model->rowCount() == list.count();
68 for (int i = 0; inSync && i < model->rowCount(); i++) {
69 inSync &= model->get(i)->appId() == list.at(i).value("id").toString();
70 inSync &= model->get(i)->name() == list.at(i).value("name").toString();
71 inSync &= model->get(i)->icon() == list.at(i).value("icon").toString();
72 inSync &= model->get(i)->count() == list.at(i).value("count").toInt();
73 inSync &= model->get(i)->countVisible() == list.at(i).value("countVisible").toBool();
74 }
75 return inSync;
76 }
78 void testLoadASOnStartup() {
79 // Load up the model
80 LauncherModel* model = new LauncherModel(this);
82 // We didn't set a user yet. model should be empty
83 QCOMPARE(model->rowCount(), 0);
85 model->setUser("user1");
87 QCOMPARE(isInSync(model, mockProperties["user1"]), true);
88 model->deleteLater();
89 }
91 void testASChangedUpdatesModel_data() {
92 QTest::addColumn<QString>("modelUser");
93 QTest::addColumn<QString>("changedUser");
94 QTest::addColumn<bool>("inSync");
96 QTest::newRow("this user changed") << "user1" << "user1" << true;
97 QTest::newRow("other user changed") << "user1" << "user2" << false;
98 }
100 void testASChangedUpdatesModel() {
101 QFETCH(QString, modelUser);
102 QFETCH(QString, changedUser);
103 QFETCH(bool, inSync);
105 LauncherModel* model = new LauncherModel(this);
106 model->setUser(modelUser);
108 int oldCount = mockProperties[modelUser].count();
109 QCOMPARE(model->rowCount(), oldCount);
111 QList<QVariantMap> newList;
112 QVariantMap newEntry;
113 newEntry.insert("id", "newappId");
114 newEntry.insert("name", "New app");
115 newEntry.insert("icon", "some-icon.svg");
116 newEntry.insert("count", 0);
117 newEntry.insert("countVisible", false);
118 newEntry.insert("pinned", true);
119 newList.append(newEntry);
120 model->m_accounts->simulatePropertyChange(changedUser, "LauncherItems", QVariant::fromValue(newList));
122 QCOMPARE(isInSync(model, mockProperties[modelUser]), true);
123 QCOMPARE(isInSync(model, mockProperties[changedUser]), inSync);
125 model->deleteLater();
126 }
128 void testUpdateCount() {
129 LauncherModel* model = new LauncherModel(this);
130 model->setUser("user1");
132 QCOMPARE(model->get(0)->countVisible(), false);
133 QCOMPARE(model->get(0)->count(), 0);
135 QList<QVariantMap> newList = mockProperties["user1"];
136 QVariantMap entry = newList.at(0);
137 entry["countVisible"] = true;
138 entry["count"] = 55;
139 newList[0] = entry;
140 model->m_accounts->simulatePropertyChange("user1", "LauncherItems", QVariant::fromValue(newList));
142 QCOMPARE(isInSync(model, mockProperties["user1"]), true);
144 model->deleteLater();
145 }
147 void testOnlyPinned() {
148 LauncherModel *model = new LauncherModel(this);
149 model->setUser("user1");
150 model->setOnlyPinned(true);
152 QCOMPARE(isInSync(model, mockProperties["user1"]), true);
154 // Let's unpin one item
155 QList<QVariantMap> newList = mockProperties["user1"];
156 QVariantMap entry = newList.at(0);
157 entry["pinned"] = false;
158 newList[0] = entry;
159 model->m_accounts->simulatePropertyChange("user1", "LauncherItems", QVariant::fromValue(newList));
160 QCOMPARE(isInSync(model, mockProperties["user1"]), true);
162 // Now toggle onlyPinned and make sure the model keeps up
163 model->setOnlyPinned(false);
164 QCOMPARE(isInSync(model, mockProperties["user1"]), true);
166 model->setOnlyPinned(true);
167 QCOMPARE(isInSync(model, mockProperties["user1"]), true);
168 }
169};
171QTEST_GUILESS_MAIN(LauncherModelASTest)
172#include "launchermodelastest.moc"