1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include "src/widget/loginscreen.h"
#include "src/persistence/paths.h"
#include "src/widget/style.h"
#include <QDebug>
#include <QLineEdit>
#include <QProcessEnvironment>
#include <QPushButton>
#include <QTest>
namespace {
std::pair<QPixmap, QPixmap> compareScreenshot(QWidget* widget, const QString& path)
{
QPixmap actual = widget->grab();
const auto env = QProcessEnvironment::systemEnvironment();
if (env.contains("BUILD_WORKSPACE_DIRECTORY")) {
const QString actualPath = QStringLiteral("%1/qtox/test/resources/images/%2")
.arg(env.value("BUILD_WORKSPACE_DIRECTORY"), path);
qDebug() << "Saving actual image to" << actualPath;
actual.save(actualPath);
}
QPixmap expected = QPixmap(QStringLiteral(":/test/images/%1").arg(path));
return {std::move(actual), std::move(expected)};
}
#define COMPARE_GRAB(widget, path) \
do { \
const auto [actual, expected] = compareScreenshot(widget, path); \
if (!QTest::qCompare(actual, expected, #widget, path, __FILE__, __LINE__)) \
QTEST_FAIL_ACTION; \
} while (false)
} // namespace
class TestLoginScreen : public QObject
{
Q_OBJECT
private slots:
void testLoginScreen();
void testCreateProfile();
void testCreateProfileBadPassword();
private:
Paths paths{Paths::Portable::Portable};
Style style;
int themeColor = 0; // Default
QString profileName = "test-user";
};
void TestLoginScreen::testLoginScreen()
{
LoginScreen loginScreen(paths, style, themeColor, profileName);
COMPARE_GRAB(&loginScreen, "loginscreen_empty.png");
}
void TestLoginScreen::testCreateProfile()
{
LoginScreen loginScreen(paths, style, themeColor, profileName);
bool created = false;
QObject::connect(&loginScreen, &LoginScreen::createNewProfile, this,
[&created]() { created = true; });
loginScreen.findChild<QLineEdit*>("newUsername")->setText("test-user");
loginScreen.findChild<QLineEdit*>("newPass")->setText("password");
loginScreen.findChild<QLineEdit*>("newPassConfirm")->setText("password");
loginScreen.findChild<QPushButton*>("createAccountButton")->click();
QVERIFY(created);
COMPARE_GRAB(&loginScreen, "loginscreen_ok.png");
}
void TestLoginScreen::testCreateProfileBadPassword()
{
LoginScreen loginScreen(paths, style, themeColor, profileName);
bool created = false;
connect(&loginScreen, &LoginScreen::createNewProfile, this, [&created]() { created = true; });
QString error;
connect(&loginScreen, &LoginScreen::failure, this,
[&error](const QString& title, const QString& message) {
std::ignore = title;
error = message;
});
loginScreen.findChild<QLineEdit*>("newUsername")->setText("test-user");
loginScreen.findChild<QLineEdit*>("newPass")->setText("password");
loginScreen.findChild<QLineEdit*>("newPassConfirm")->setText("password2");
QTest::mouseClick(loginScreen.findChild<QPushButton*>("createAccountButton"),
Qt::MouseButton::LeftButton);
QVERIFY(!created);
QVERIFY(error.contains("different"));
}
QTEST_MAIN(TestLoginScreen)
#include "loginscreen_test.moc"
|