Skip to content

Commit ce5c37d

Browse files
authored
Merge pull request #28 from hackpulsar/dev
add register_window tests & update login_window tests
2 parents e3b22a1 + 60f11e6 commit ce5c37d

2 files changed

Lines changed: 109 additions & 3 deletions

File tree

tests/ui/login_window_test.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <QLineEdit>
55
#include <QPushButton>
66
#include <QLabel>
7+
#include <QPointer>
78

89
#include "windows/login_window.h"
910

@@ -12,18 +13,20 @@ class LoginWindowTest : public ::testing::Test {
1213
void SetUp() override {
1314
qputenv("TOKEN_OBTAIN_URL", "itdoesntexist");
1415

15-
window = std::make_unique<LoginWindow>();
16+
window = new LoginWindow;
17+
window->setAttribute(Qt::WA_DeleteOnClose);
1618

1719
emailLineEdit = window->findChild<QLineEdit*>("emailLineEdit");
1820
passwordLineEdit = window->findChild<QLineEdit*>("passwordLineEdit");
1921
loginButton = window->findChild<QPushButton*>("loginButton");
2022
errorLabel = window->findChild<QLabel*>("errorLabel");
23+
registerButton = window->findChild<QPushButton*>("registerButton");
2124
}
2225

23-
std::unique_ptr<LoginWindow> window;
26+
QPointer<LoginWindow> window;
2427

2528
QLineEdit *emailLineEdit, *passwordLineEdit;
26-
QPushButton* loginButton;
29+
QPushButton *loginButton, *registerButton;
2730
QLabel* errorLabel;
2831

2932
};
@@ -33,6 +36,7 @@ TEST_F(LoginWindowTest, HasControls) {
3336
EXPECT_NE(passwordLineEdit, nullptr);
3437
EXPECT_NE(loginButton, nullptr);
3538
EXPECT_NE(errorLabel, nullptr);
39+
EXPECT_NE(registerButton, nullptr);
3640
}
3741

3842
TEST_F(LoginWindowTest, PasswordHidden) {
@@ -48,6 +52,9 @@ TEST_F(LoginWindowTest, CorrectTitle) {
4852
}
4953

5054
TEST_F(LoginWindowTest, TimeoutMessageShows) {
55+
emailLineEdit->setText("test@test.com");
56+
passwordLineEdit->setText("strong_password");
57+
5158
QTest::mouseClick(loginButton, Qt::LeftButton);
5259
QTest::qWait(100); // wait for request to fail
5360
EXPECT_EQ(errorLabel->text().toStdString(), "Request timed out");
@@ -68,3 +75,9 @@ TEST_F(LoginWindowTest, LoginButtonResets) {
6875
QTest::qWait(100); // wait for request to fail
6976
EXPECT_EQ(loginButton->text().toStdString(), "Login");
7077
}
78+
79+
TEST_F(LoginWindowTest, RegisterButtonClosesCurrent) {
80+
QTest::mouseClick(registerButton, Qt::LeftButton);
81+
QTest::qWait(100); // wait for event to process
82+
EXPECT_TRUE(window.isNull());
83+
}

tests/ui/register_window_test.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)