~unity-team/unity8/slim-greeter

« back to all changes in this revision

Viewing changes to tests/plugins/Greeter/Unity/Launcher/launchermodelastest.cpp

  • Committer: Michael Terry
  • Date: 2015-02-12 15:45:21 UTC
  • mfrom: (1432.1.178 unity8)
  • Revision ID: michael.terry@canonical.com-20150212154521-1qpg3t501ljj88lj
MergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
16
 
 
17
// unity-api
 
18
#include <unity/shell/launcher/LauncherModelInterface.h>
 
19
#include <unity/shell/application/ApplicationInfoInterface.h>
 
20
 
 
21
#include "launcheritem.h"
 
22
#include "launchermodelas.h"
 
23
#include "AccountsServiceDBusAdaptor.h"
 
24
 
 
25
#include <QtTest>
 
26
 
 
27
QHash<QString, QList<QVariantMap>> mockProperties;
 
28
 
 
29
class LauncherModelASTest : public QObject
 
30
{
 
31
    Q_OBJECT
 
32
 
 
33
private:
 
34
    LauncherModel *launcherModel;
 
35
 
 
36
private Q_SLOTS:
 
37
 
 
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("countVisible", false);
 
47
        item.insert("pinned", true);
 
48
        list.append(item);
 
49
        item["id"] = "appId2";
 
50
        item["name"] = "Item 2";
 
51
        list.append(item);
 
52
        mockProperties["user1"] = list;
 
53
    }
 
54
 
 
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()) {
 
60
                list << map;
 
61
            }
 
62
        }
 
63
 
 
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();
 
71
        }
 
72
        return inSync;
 
73
    }
 
74
 
 
75
    void testLoadASOnStartup() {
 
76
        // Load up the model
 
77
        LauncherModel* model = new LauncherModel(this);
 
78
 
 
79
        // We didn't set a user yet. model should be empty
 
80
        QCOMPARE(model->rowCount(), 0);
 
81
 
 
82
        model->setUser("user1");
 
83
 
 
84
        QCOMPARE(isInSync(model, mockProperties["user1"]), true);
 
85
        model->deleteLater();
 
86
    }
 
87
 
 
88
    void testASChangedUpdatesModel_data() {
 
89
        QTest::addColumn<QString>("modelUser");
 
90
        QTest::addColumn<QString>("changedUser");
 
91
        QTest::addColumn<bool>("inSync");
 
92
 
 
93
        QTest::newRow("this user changed") << "user1" << "user1" << true;
 
94
        QTest::newRow("other user changed") << "user1" << "user2" << false;
 
95
    }
 
96
 
 
97
    void testASChangedUpdatesModel() {
 
98
        QFETCH(QString, modelUser);
 
99
        QFETCH(QString, changedUser);
 
100
        QFETCH(bool, inSync);
 
101
 
 
102
        LauncherModel* model = new LauncherModel(this);
 
103
        model->setUser(modelUser);
 
104
 
 
105
        int oldCount = mockProperties[modelUser].count();
 
106
        QCOMPARE(model->rowCount(), oldCount);
 
107
 
 
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));
 
118
 
 
119
        QCOMPARE(isInSync(model, mockProperties[modelUser]), true);
 
120
        QCOMPARE(isInSync(model, mockProperties[changedUser]), inSync);
 
121
 
 
122
        model->deleteLater();
 
123
    }
 
124
 
 
125
    void testUpdateCount() {
 
126
        LauncherModel* model = new LauncherModel(this);
 
127
        model->setUser("user1");
 
128
 
 
129
        QCOMPARE(model->get(0)->countVisible(), false);
 
130
        QCOMPARE(model->get(0)->count(), 0);
 
131
 
 
132
        QList<QVariantMap> newList = mockProperties["user1"];
 
133
        QVariantMap entry = newList.at(0);
 
134
        entry["countVisible"] = true;
 
135
        entry["count"] = 55;
 
136
        newList[0] = entry;
 
137
        model->m_accounts->simulatePropertyChange("user1", "LauncherItems", QVariant::fromValue(newList));
 
138
 
 
139
        QCOMPARE(isInSync(model, mockProperties["user1"]), true);
 
140
 
 
141
        model->deleteLater();
 
142
    }
 
143
 
 
144
    void testOnlyPinned() {
 
145
        LauncherModel *model = new LauncherModel(this);
 
146
        model->setUser("user1");
 
147
        model->setOnlyPinned(true);
 
148
 
 
149
        QCOMPARE(isInSync(model, mockProperties["user1"]), true);
 
150
 
 
151
        // Let's unpin one item
 
152
        QList<QVariantMap> newList = mockProperties["user1"];
 
153
        QVariantMap entry = newList.at(0);
 
154
        entry["pinned"] = false;
 
155
        newList[0] = entry;
 
156
        model->m_accounts->simulatePropertyChange("user1", "LauncherItems", QVariant::fromValue(newList));
 
157
        QCOMPARE(isInSync(model, mockProperties["user1"]), true);
 
158
 
 
159
        // Now toggle onlyPinned and make sure the model keeps up
 
160
        model->setOnlyPinned(false);
 
161
        QCOMPARE(isInSync(model, mockProperties["user1"]), true);
 
162
 
 
163
        model->setOnlyPinned(true);
 
164
        QCOMPARE(isInSync(model, mockProperties["user1"]), true);
 
165
    }
 
166
};
 
167
 
 
168
QTEST_GUILESS_MAIN(LauncherModelASTest)
 
169
#include "launchermodelastest.moc"