on 2d12d49 help sign in
134 lines 4.7 kB history raw
1/*
2 * Copyright (C) 2015-2016 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 "MockController.h"
21#include <QLightDM/SessionsModel>
22#include <QtCore/QModelIndex>
23#include <QtTest>
24#include <QString>
26class GreeterSessionsModelTest : public QObject
28 Q_OBJECT
30private Q_SLOTS:
32 void init()
33 {
34 model = new SessionsModel();
35 QVERIFY(model);
36 mock = QLightDM::MockController::instance();
37 QVERIFY(mock);
38 }
40 void cleanup()
41 {
42 delete model;
43 // mock will be a singleton, doesn't need to be cleaned
44 }
46 static QModelIndex findByKey(QAbstractItemModel *model, const QString& key)
47 {
48 for (int i = 0; i < model->rowCount(QModelIndex()); i++) {
49 if (model->data(model->index(i, 0), QLightDM::SessionsModel::KeyRole).toString() == key)
50 return model->index(i, 0);
51 }
53 return QModelIndex();
54 }
56 void testIconDirectoriesAreValid()
57 {
58 Q_FOREACH(const QUrl& searchDirectory, model->iconSearchDirectories())
59 {
60 QVERIFY(searchDirectory.isValid());
61 }
62 }
64 void testIconLookupLogic()
65 {
66 QSKIP("This test tries to search system icon directories for the "
67 "wanted icons, assuming that the greeter icons are already "
68 "installed. Fixing the test would require using "
69 "setIconSearchDirectories() and some local dummy files.");
70 const QString data[][2] = {
71 //{SESSION KEY, ICON NAME}
72 {"gnome-classic", "gnome_badge.png"},
73 {"gnome-flashback-compiz", "gnome_badge.png"},
74 {"gnome-flashback-metacity", "gnome_badge.png"},
75 {"gnome-shell", "gnome_badge.png"},
76 {"gnome-wayland", "gnome_badge.png"},
77 {"gnome", "gnome_badge.png"},
78 {"kde", "kde_badge.png"},
79 {"plasma", "kde_badge.png"},
80 {"recovery_console", "recovery_console_badge.png"},
81 {"ubuntu","ubuntu_badge.png"},
82 {"ubuntu-2d", "ubuntu_badge.png"},
83 {"xterm", "recovery_console_badge.png"},
84 {"made up session", "unknown_badge.png"},
85 {"", "unknown_badge.png"}
86 };
88 const short length = sizeof(data) / sizeof(data[0]);
89 for (int i = 0; i < length; i++) {
90 std::string key = data[i][0].toStdString();
91 std::string icon = data[i][1].toStdString();
92 std::string url = model->iconUrl(data[i][0]).toString().toStdString();
93 std::string errorMessage = "Session icon url for " + key +
94 " should contain " + icon + " but " + url + " was returned";
95 QVERIFY2(model->iconUrl(data[i][0]).toString().contains(data[i][1]),
96 errorMessage.c_str());
97 }
98 }
100 void testMultipleSessionsCountIsCorrect()
101 {
102 mock->setProperty("sessionMode", "full");
103 QVERIFY(model->rowCount(QModelIndex()) > 1);
104 }
106 void testNoSessionsCountIsCorrect()
107 {
108 mock->setProperty("sessionMode", "none");
109 QVERIFY(model->rowCount(QModelIndex()) == 0);
110 }
112 void testSingleSessionCountIsCorrect()
113 {
114 mock->setProperty("sessionMode", "single");
115 QVERIFY(model->rowCount(QModelIndex()) == 1);
116 }
118 void testSessionNameIsCorrect()
119 {
120 // This is testing the lookup, not the correctness of the strings,
121 // so one test should be sufficient
122 mock->setProperty("sessionMode", "full");
123 QVERIFY(model->data(findByKey(model, "ubuntu"),
124 Qt::DisplayRole).toString() == "Ubuntu");
125 }
127private:
128 SessionsModel *model;
129 QObject *mock;
130};
132QTEST_MAIN(GreeterSessionsModelTest)
134#include "sessionsmodel.moc"