|
| 1 | +#include <gtest/gtest.h> |
| 2 | + |
| 3 | +#include <QTest> |
| 4 | +#include <QLineEdit> |
| 5 | +#include <QPushButton> |
| 6 | +#include <QLabel> |
| 7 | + |
| 8 | +#include "windows/register_window.h" |
| 9 | + |
| 10 | +class RegisterWindowTest : public ::testing::Test { |
| 11 | +protected: |
| 12 | + void SetUp() override { |
| 13 | + qputenv("REGISTER_URL", "itdoesntexist"); |
| 14 | + |
| 15 | + window = std::make_unique<RegisterWindow>(); |
| 16 | + |
| 17 | + emailLineEdit = window->findChild<QLineEdit*>("emailLineEdit"); |
| 18 | + usernameLineEdit = window->findChild<QLineEdit*>("usernameLineEdit"); |
| 19 | + passwordLineEdit = window->findChild<QLineEdit*>("passwordLineEdit"); |
| 20 | + passwordRepeatLineEdit = window->findChild<QLineEdit*>("passwordRepeatLineEdit"); |
| 21 | + errorLabel = window->findChild<QLabel*>("errorLabel"); |
| 22 | + registerButton = window->findChild<QPushButton*>("registerButton"); |
| 23 | + } |
| 24 | + |
| 25 | + std::unique_ptr<RegisterWindow> window; |
| 26 | + |
| 27 | + QLineEdit *emailLineEdit, *usernameLineEdit, *passwordLineEdit, *passwordRepeatLineEdit; |
| 28 | + QPushButton *loginButton, *registerButton; |
| 29 | + QLabel* errorLabel; |
| 30 | +}; |
| 31 | + |
| 32 | +TEST_F(RegisterWindowTest, HasControls) { |
| 33 | + EXPECT_NE(emailLineEdit, nullptr); |
| 34 | + EXPECT_NE(usernameLineEdit, nullptr); |
| 35 | + EXPECT_NE(passwordLineEdit, nullptr); |
| 36 | + EXPECT_NE(passwordRepeatLineEdit, nullptr); |
| 37 | + EXPECT_NE(errorLabel, nullptr); |
| 38 | + EXPECT_NE(registerButton, nullptr); |
| 39 | +} |
| 40 | + |
| 41 | +TEST_F(RegisterWindowTest, HasPlaceholderText) { |
| 42 | + EXPECT_EQ(emailLineEdit->placeholderText().toStdString(), "Email"); |
| 43 | + EXPECT_EQ(usernameLineEdit->placeholderText().toStdString(), "Username"); |
| 44 | + EXPECT_EQ(passwordLineEdit->placeholderText().toStdString(), "Password"); |
| 45 | + EXPECT_EQ(passwordRepeatLineEdit->placeholderText().toStdString(), "Repeat password"); |
| 46 | +} |
| 47 | + |
| 48 | +TEST_F(RegisterWindowTest, PasswordHidden) { |
| 49 | + EXPECT_EQ(passwordLineEdit->echoMode(), QLineEdit::EchoMode::Password); |
| 50 | + EXPECT_EQ(passwordRepeatLineEdit->echoMode(), QLineEdit::EchoMode::Password); |
| 51 | +} |
| 52 | + |
| 53 | +TEST_F(RegisterWindowTest, TimeoutMessageShows) { |
| 54 | + emailLineEdit->setText("test@test.com"); |
| 55 | + usernameLineEdit->setText("test"); |
| 56 | + passwordLineEdit->setText("strong_password"); |
| 57 | + passwordRepeatLineEdit->setText("strong_password"); |
| 58 | + |
| 59 | + |
| 60 | + QTest::mouseClick(registerButton, Qt::LeftButton); |
| 61 | + QTest::qWait(100); // wait for request to fail |
| 62 | + EXPECT_EQ(errorLabel->text().toStdString(), "Request timed out"); |
| 63 | +} |
| 64 | + |
| 65 | +TEST_F(RegisterWindowTest, PasswordsDontMatch) { |
| 66 | + emailLineEdit->setText("test@test.com"); |
| 67 | + usernameLineEdit->setText("test"); |
| 68 | + passwordLineEdit->setText("strong_password"); |
| 69 | + passwordRepeatLineEdit->setText("other_password"); |
| 70 | + |
| 71 | + QTest::mouseClick(registerButton, Qt::LeftButton); |
| 72 | + QTest::qWait(100); // wait for request to fail |
| 73 | + EXPECT_EQ(errorLabel->text().toStdString(), "Passwords don't match"); |
| 74 | +} |
| 75 | + |
| 76 | +TEST_F(RegisterWindowTest, EmptyFieldsError) { |
| 77 | + QTest::mouseClick(registerButton, Qt::LeftButton); |
| 78 | + QTest::qWait(100); // wait for request to fail |
| 79 | + EXPECT_EQ(errorLabel->text().toStdString(), "Please fill the fields below"); |
| 80 | +} |
| 81 | + |
| 82 | +TEST_F(RegisterWindowTest, RegisterButtonResets) { |
| 83 | + emailLineEdit->setText("test@test.com"); |
| 84 | + usernameLineEdit->setText("test"); |
| 85 | + passwordLineEdit->setText("strong_password"); |
| 86 | + passwordRepeatLineEdit->setText("strong_password"); |
| 87 | + |
| 88 | + registerButton->click(); |
| 89 | + EXPECT_TRUE(registerButton->text().isEmpty()); |
| 90 | + |
| 91 | + QTest::qWait(200); |
| 92 | + EXPECT_EQ(registerButton->text().toStdString(), "Register"); |
| 93 | +} |
0 commit comments