on master help sign in
168 lines 5.2 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 "Greeter.h"
18#include "MockController.h"
19#include "UsersModel.h"
21#include <QLightDM/UsersModel>
22#include <QtTest>
24class GreeterUsersModelTest : public QObject
26 Q_OBJECT
28private:
29 static int findName(QAbstractItemModel *model, const QString &name)
30 {
31 for (int i = 0; i < model->rowCount(QModelIndex()); i++) {
32 if (model->data(model->index(i, 0), QLightDM::UsersModel::NameRole).toString() == name) {
33 return i;
34 }
35 }
36 return -1;
37 }
39 static QString getStringValue(QAbstractItemModel *model, const QString &name, int role)
40 {
41 int i = findName(model, name);
42 return model->data(model->index(i, 0), role).toString();
43 }
45 void recreateModel()
46 {
47 delete model;
48 model = new UsersModel();
49 QVERIFY(model);
50 }
52private Q_SLOTS:
54 void init()
55 {
56 recreateModel();
57 }
59 void cleanup()
60 {
61 QLightDM::MockController::instance()->reset();
62 }
64 void testMangleColor()
65 {
66 auto background = getStringValue(model, "color-background", QLightDM::UsersModel::BackgroundPathRole);
67 QVERIFY(background == "data:image/svg+xml,<svg><rect width='100%' height='100%' fill='#E95420'/></svg>");
68 }
70 void testMangleEmptyName()
71 {
72 auto name = getStringValue(model, "empty-name", QLightDM::UsersModel::RealNameRole);
73 QVERIFY(name == "empty-name");
74 }
76 void testHasGuest()
77 {
78 // sanity check that it doesn't start already present
79 QCOMPARE(findName(model, QStringLiteral("*guest")), -1);
81 QLightDM::MockController::instance()->setHasGuestAccountHint(true);
83 int i = findName(model, QStringLiteral("*guest"));
84 QVERIFY(i >= 0);
86 auto realName = model->data(i, QLightDM::UsersModel::RealNameRole).toString();
87 QCOMPARE(realName, QStringLiteral("Guest Session"));
89 auto loggedIn = model->data(i, QLightDM::UsersModel::LoggedInRole).toBool();
90 QVERIFY(!loggedIn);
92 auto session = model->data(i, QLightDM::UsersModel::SessionRole).toString();
93 QCOMPARE(session, Greeter::instance()->defaultSessionHint());
94 }
96 void testHasManual()
97 {
98 // sanity check that it doesn't start already present
99 QCOMPARE(findName(model, QStringLiteral("*other")), -1);
101 QLightDM::MockController::instance()->setShowManualLoginHint(true);
103 int i = findName(model, QStringLiteral("*other"));
104 QVERIFY(i >= 0);
106 auto realName = model->data(i, QLightDM::UsersModel::RealNameRole).toString();
107 QCOMPARE(realName, QStringLiteral("Login"));
109 auto loggedIn = model->data(i, QLightDM::UsersModel::LoggedInRole).toBool();
110 QVERIFY(!loggedIn);
112 auto session = model->data(i, QLightDM::UsersModel::SessionRole).toString();
113 QCOMPARE(session, Greeter::instance()->defaultSessionHint());
114 }
117 void testEmptySession()
118 {
119 int i = findName(model, QStringLiteral("no-session"));
120 QVERIFY(i >= 0);
122 // A valid test as 'no-session' is instantiated with it's
123 // SessionRole as an empty string. This ensures something,
124 // hopefully sensical, is returned.
125 auto session = model->data(i, QLightDM::UsersModel::SessionRole);
126 QCOMPARE(session.toString().isEmpty(), false);
127 }
129 void testHideUsers()
130 {
131 QLightDM::MockController::instance()->setHideUsersHint(true);
133 QCOMPARE(model->count(), 1);
134 auto name = model->data(0, QLightDM::UsersModel::NameRole).toString();
135 QCOMPARE(name, QStringLiteral("*other"));
136 }
138 void testHideUsersWithGuest()
139 {
140 QLightDM::MockController::instance()->setHideUsersHint(true);
141 QLightDM::MockController::instance()->setHasGuestAccountHint(true);
143 QCOMPARE(model->count(), 1);
144 auto name = model->data(0, QLightDM::UsersModel::NameRole).toString();
145 QCOMPARE(name, QStringLiteral("*guest"));
146 }
148 void testCustomEntriesAreLast()
149 {
150 QLightDM::MockController::instance()->setHasGuestAccountHint(true);
151 QLightDM::MockController::instance()->setShowManualLoginHint(true);
153 QVERIFY(model->count() > 2);
155 int manualIndex = findName(model, QStringLiteral("*other"));
156 QCOMPARE(manualIndex, model->count() - 2);
158 int guestIndex = findName(model, QStringLiteral("*guest"));
159 QCOMPARE(guestIndex, model->count() - 1);
160 }
162private:
163 UsersModel *model = nullptr;
164};
166QTEST_MAIN(GreeterUsersModelTest)
168#include "usersmodel.moc"