on a213859 help sign in
unity8/tests/plugins/LightDM/sessionsmodel.cpp
127 lines 4.3 kB history raw
1/*
2 * Copyright (C) 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 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 General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "SessionsModel.h"
19#include <QModelIndex>
20#include <QString>
21#include <QtTest>
23class GreeterSessionsModelTest : public QObject
25 Q_OBJECT
27private Q_SLOTS:
29 void init()
30 {
31 model = new SessionsModel();
32 QVERIFY(model);
33 mock = model->mock();
34 QVERIFY(mock);
35 }
37 void cleanup()
38 {
39 delete model;
40 // mock will be a singleton, doesn't need to be cleaned
41 }
43 static QModelIndex findByKey(QAbstractItemModel *model, const QString& key)
44 {
45 for (int i = 0; i < model->rowCount(QModelIndex()); i++) {
46 if (model->data(model->index(i, 0), QLightDM::SessionsModel::KeyRole).toString() == key)
47 return model->index(i, 0);
48 }
50 return QModelIndex();
51 }
53 void testIconDirectoriesAreValid()
54 {
55 Q_FOREACH(const QUrl& searchDirectory, model->iconSearchDirectories())
56 {
57 QVERIFY(searchDirectory.isValid());
58 }
59 }
61 void testIconLookupLogic()
62 {
63 const QString data[][2] = {
64 //{SESSION KEY, ICON NAME}
65 {"gnome-classic", "gnome_badge.png"},
66 {"gnome-flashback-compiz", "gnome_badge.png"},
67 {"gnome-flashback-metacity", "gnome_badge.png"},
68 {"gnome-shell", "gnome_badge.png"},
69 {"gnome-wayland", "gnome_badge.png"},
70 {"gnome", "gnome_badge.png"},
71 {"kde", "kde_badge.png"},
72 {"plasma", "kde_badge.png"},
73 {"recovery_console", "recovery_console_badge.png"},
74 {"ubuntu","ubuntu_badge.png"},
75 {"ubuntu-2d", "ubuntu_badge.png"},
76 {"xterm", "recovery_console_badge.png"},
77 {"made up session", "unknown_badge.png"},
78 {"", "unknown_badge.png"}
79 };
81 const short length = sizeof(data) / sizeof(data[0]);
82 for (int i = 0; i < length; i++) {
83 std::string key = data[i][0].toStdString();
84 std::string icon = data[i][1].toStdString();
85 std::string url = model->iconUrl(data[i][0]).toString().toStdString();
86 std::string errorMessage = "Session icon url for " + key +
87 " should contain " + icon + " but " + url + " was returned";
88 QVERIFY2(model->iconUrl(data[i][0]).toString().contains(data[i][1]),
89 errorMessage.c_str());
90 }
91 }
93 void testMultipleSessionsCountIsCorrect()
94 {
95 mock->setProperty("sessionMode", "full");
96 QVERIFY(model->rowCount(QModelIndex()) > 1);
97 }
99 void testNoSessionsCountIsCorrect()
100 {
101 mock->setProperty("sessionMode", "none");
102 QVERIFY(model->rowCount(QModelIndex()) == 0);
103 }
105 void testSingleSessionCountIsCorrect()
106 {
107 mock->setProperty("sessionMode", "single");
108 QVERIFY(model->rowCount(QModelIndex()) == 1);
109 }
111 void testSessionNameIsCorrect()
112 {
113 // This is testing the lookup, not the correctness of the strings,
114 // so one test should be sufficient
115 mock->setProperty("sessionMode", "full");
116 QVERIFY(model->data(findByKey(model, "ubuntu"),
117 Qt::DisplayRole).toString() == "Ubuntu");
118 }
120private:
121 SessionsModel *model;
122 QObject *mock;
123};
125QTEST_MAIN(GreeterSessionsModelTest)
127#include "sessionsmodel.moc"