on ee2f4bd help sign in
unity8/tests/plugins/Greeter/Unity/Launcher/launchermodelastest.cpp
172 lines 6.0 kB history raw
1/*
2 * Copyright 2014-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 */
17// unity-api
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"
25#include <QtTest>
27QHash<QString, QList<QVariantMap>> mockProperties;
29class LauncherModelASTest : public QObject
31 Q_OBJECT
33private:
34 LauncherModel *launcherModel;
36private Q_SLOTS:
38 void init() {
39 // Prepare some fake list
40 QList<QVariantMap> list;
41 QVariantMap item;
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("progress", 0);
47 item.insert("countVisible", false);
48 item.insert("pinned", true);
49 list.append(item);
50 item["id"] = "appId2";
51 item["name"] = "Item 2";
52 list.append(item);
53 mockProperties["user1"] = list;
54 }
56 bool isInSync(LauncherModel *model, const QList<QVariantMap> &properties) {
57 QList<QVariantMap> list;
58 // Strip unpinned if onlyPinned is set
59 Q_FOREACH (const QVariantMap &map, properties) {
60 if (!model->onlyPinned() || map.value("pinned").toBool()) {
61 list << map;
62 }
63 }
65 bool inSync = model->rowCount() == list.count();
66 for (int i = 0; inSync && i < model->rowCount(); i++) {
67 inSync &= model->get(i)->appId() == list.at(i).value("id").toString();
68 inSync &= model->get(i)->name() == list.at(i).value("name").toString();
69 inSync &= model->get(i)->icon() == list.at(i).value("icon").toString();
70 inSync &= model->get(i)->count() == list.at(i).value("count").toInt();
71 inSync &= model->get(i)->progress() == list.at(i).value("progress").toInt();
72 inSync &= model->get(i)->countVisible() == list.at(i).value("countVisible").toBool();
73 }
74 return inSync;
75 }
77 void testLoadASOnStartup() {
78 // Load up the model
79 LauncherModel* model = new LauncherModel(this);
81 // We didn't set a user yet. model should be empty
82 QCOMPARE(model->rowCount(), 0);
84 model->setUser("user1");
86 QCOMPARE(isInSync(model, mockProperties["user1"]), true);
87 model->deleteLater();
88 }
90 void testASChangedUpdatesModel_data() {
91 QTest::addColumn<QString>("modelUser");
92 QTest::addColumn<QString>("changedUser");
93 QTest::addColumn<bool>("inSync");
95 QTest::newRow("this user changed") << "user1" << "user1" << true;
96 QTest::newRow("other user changed") << "user1" << "user2" << false;
97 }
99 void testASChangedUpdatesModel() {
100 QFETCH(QString, modelUser);
101 QFETCH(QString, changedUser);
102 QFETCH(bool, inSync);
104 LauncherModel* model = new LauncherModel(this);
105 model->setUser(modelUser);
107 int oldCount = mockProperties[modelUser].count();
108 QCOMPARE(model->rowCount(), oldCount);
110 QList<QVariantMap> newList;
111 QVariantMap newEntry;
112 newEntry.insert("id", "newappId");
113 newEntry.insert("name", "New app");
114 newEntry.insert("icon", "some-icon.svg");
115 newEntry.insert("count", 0);
116 newEntry.insert("progress", 42);
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"