on c488c56 help sign in
unity8/tests/mocks/liblightdm/MockUsersModel.cpp
191 lines 6.7 kB history raw
1/*
2 * Copyright (C) 2013-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 *
16 * Author: Michael Terry <michael.terry@canonical.com>
17 */
19// LightDM currently is Qt4 compatible, and so doesn't define setRoleNames.
20// To use the same method of setting role name that it does, we
21// set our compatibility to Qt4 here too.
22#define QT_DISABLE_DEPRECATED_BEFORE QT_VERSION_CHECK(4, 0, 0)
24#include "MockController.h"
25#include "MockUsersModel.h"
26#include <QDir>
27#include <QIcon>
29namespace QLightDM
32class Entry
34public:
35 QString username;
36 QString real_name;
37 QString background;
38 QString layouts;
39 bool is_active;
40 bool has_messages;
41 QString session;
42 uid_t uid;
43};
45class UsersModelPrivate
47public:
48 QList<Entry> entries;
49};
51UsersModel::UsersModel(QObject *parent)
52 : QAbstractListModel(parent)
53 , d_ptr(new UsersModelPrivate)
55 // Extend roleNames (we want to keep the "display" role)
56 QHash<int, QByteArray> roles = roleNames();
57 roles[NameRole] = "name";
58 roles[RealNameRole] = "realName";
59 roles[LoggedInRole] = "loggedIn";
60 roles[BackgroundRole] = "background";
61 roles[BackgroundPathRole] = "backgroundPath";
62 roles[SessionRole] = "session";
63 roles[HasMessagesRole] = "hasMessages";
64 roles[ImagePathRole] = "imagePath";
65 roles[UidRole] = "uid";
66 setRoleNames(roles);
68 connect(MockController::instance(), SIGNAL(userModeChanged()),
69 this, SLOT(resetEntries()));
70 resetEntries();
73UsersModel::~UsersModel()
75 delete d_ptr;
78int UsersModel::rowCount(const QModelIndex &parent) const
80 Q_D(const UsersModel);
82 if (parent.isValid()) {
83 return 0;
84 } else { // parent is root
85 return d->entries.size();
86 }
89QVariant UsersModel::data(const QModelIndex &index, int role) const
91 Q_D(const UsersModel);
93 if (!index.isValid()) {
94 return QVariant();
95 }
97 int row = index.row();
98 switch (role) {
99 case Qt::DisplayRole:
100 return d->entries[row].real_name;
101 case Qt::DecorationRole:
102 return QIcon();
103 case UsersModel::NameRole:
104 return d->entries[row].username;
105 case UsersModel::RealNameRole:
106 return d->entries[row].real_name;
107 case UsersModel::SessionRole:
108 return d->entries[row].session;
109 case UsersModel::LoggedInRole:
110 return d->entries[row].is_active;
111 case UsersModel::BackgroundRole:
112 return QPixmap(d->entries[row].background);
113 case UsersModel::BackgroundPathRole:
114 return d->entries[row].background;
115 case UsersModel::HasMessagesRole:
116 return d->entries[row].has_messages;
117 case UsersModel::ImagePathRole:
118 return "";
119 case UsersModel::UidRole:
120 return d->entries[row].uid;
121 default:
122 return QVariant();
123 }
126void UsersModel::resetEntries()
128 Q_D(UsersModel);
130 beginResetModel();
132 QString userMode = MockController::instance()->userMode();
134 if (userMode == "single") {
135 d->entries = {{"no-password", "No Password", 0, 0, false, false, "ubuntu", 0}};
136 } else if (userMode == "single-passphrase") {
137 d->entries = {{"has-password", "Has Password", 0, 0, false, false, "ubuntu", 0}};
138 } else if (userMode == "single-pin") {
139 d->entries = {{"has-pin", "Has PIN", 0, 0, false, false, "ubuntu", 0}};
140 } else if (userMode == "full") {
141 d->entries = {
142 { "has-password", "Has Password", 0, 0, false, false, "ubuntu", 0 },
143 { "has-pin", "Has PIN", 0, 0, false, false, "ubuntu", 0 },
144 { "different-prompt", "Different Prompt", 0, 0, false, false, "ubuntu", 0 },
145 { "no-password", "No Password", 0, 0, false, false, "ubuntu", 0 },
146 { "auth-error", "Auth Error", 0, 0, false, false, "ubuntu", 0 },
147 { "two-factor", "Two Factor", 0, 0, false, false, "ubuntu", 0 },
148 { "info-prompt", "Info Prompt", 0, 0, false, false, "ubuntu", 0 },
149 { "html-info-prompt", "HTML Info Prompt", 0, 0, false, false, "ubuntu", 0 },
150 { "long-info-prompt", "Long Info Prompt", 0, 0, false, false, "ubuntu", 0 },
151 { "wide-info-prompt", "Wide Info Prompt", 0, 0, false, false, "ubuntu", 0 },
152 { "multi-info-prompt", "Multi Info Prompt", 0, 0, false, false, "ubuntu", 0 },
153 { "long-name", "Long name (far far too long to fit, seriously this would never fit on the screen, you will never see this part of the name)", 0, 0, false, false, "ubuntu", 0 },
154 { "color-background", "Color Background", "#E95420", 0, false, false, "ubuntu", 0 },
155 // white and black are a bit redundant, but useful for manually testing if UI is still readable
156 { "white-background", "White Background", "#ffffff", 0, false, false, "ubuntu", 0 },
157 { "black-background", "Black Background", "#000000", 0, false, false, "ubuntu", 0 },
158 { "no-background", "No Background", "", 0, false, false, "ubuntu", 0 },
159 { "unicode", "가나다라마", 0, 0, false, false, "ubuntu", 0 },
160 { "no-response", "No Response", 0, 0, false, false, "ubuntu", 0 },
161 { "empty-name", "", 0, 0, false, false, "ubuntu", 0 },
162 { "active", "Active Account", 0, 0, true, false, "ubuntu", 0 },
163 };
164 }
166 // Assign uids in a loop, just to avoid having to muck with them when
167 // adding or removing test users.
168 for (int i = 0; i < d->entries.size(); i++) {
169 d->entries[i].uid = i + 1;
170 }
172 // Assign backgrounds
173 QDir backgroundDir("/usr/share/backgrounds");
174 QStringList backgrounds = backgroundDir.entryList(QDir::Files);
175 if (!backgrounds.empty()) {
176 for (int i = 0; i < d->entries.size(); i++) {
177 if (d->entries[i].background.isNull()) {
178 d->entries[i].background = backgroundDir.filePath(backgrounds[i % backgrounds.size()]);
179 }
180 }
181 }
183 endResetModel();
186QObject *UsersModel::mock()
188 return MockController::instance();