diff --git a/test/main.cc b/test/main.cc index 64310bf..7fcee67 100644 --- a/test/main.cc +++ b/test/main.cc @@ -2,18 +2,19 @@ QTEST_MAIN(TestGui) -void TestGui::authenticate() +void TestGui::authenticate(MainWindow *app) { - QPushButton *change_token = app.ui->pushButton_changeToken; + QPushButton *change_token = app->ui->pushButton_changeToken; QTest::mouseClick(change_token, Qt::MouseButton::LeftButton); - QTest::keyClicks(app.ui->lineEdit_accessToken, "valid"); + QTest::keyClicks(app->ui->lineEdit_accessToken, "valid"); } void TestGui::access_token_entry_test() { - QSignalSpy auth_tries(app.canvas, &ICanvas::authenticate_done); + MainWindow *app = create_app(); + QSignalSpy auth_tries(app->canvas, &ICanvas::authenticate_done); - QPushButton *change_token = app.ui->pushButton_changeToken; + QPushButton *change_token = app->ui->pushButton_changeToken; // click on "Change Token" QTest::mouseClick(change_token, Qt::MouseButton::LeftButton); @@ -23,18 +24,19 @@ void TestGui::access_token_entry_test() QVERIFY(change_token->isHidden()); // enter a valid token - QTest::keyClicks(app.ui->lineEdit_accessToken, "valid"); + QTest::keyClicks(app->ui->lineEdit_accessToken, "valid"); QCOMPARE(auth_tries.count(), 5); - QCOMPARE(app.ui->label_authenticationStatus->text(), "authenticated!"); + QCOMPARE(app->ui->label_authenticationStatus->text(), "authenticated!"); QVERIFY(change_token->isEnabled()); QVERIFY(!change_token->isHidden()); } void TestGui::fetch_courses_test() { - authenticate(); + MainWindow *app = create_app(); + authenticate(app); std::string expected[] = { "#0|Linear Algebra: {\n" " #20010|course files: {\n" @@ -56,15 +58,16 @@ void TestGui::fetch_courses_test() " }\n" "}", }; - for (auto t : app.course_trees) + for (auto t : app->course_trees) QCOMPARE(QString(t.to_string().c_str()), QString(expected[t.id].c_str())); } void TestGui::fetch_courses_ui_test() { - authenticate(); + MainWindow *app = create_app(); + authenticate(app); - TreeModel *model = app.ui->treeView->model(); + TreeModel *model = app->ui->treeView->model(); QCOMPARE(model->childrenCount(), 3); TreeIndex ptr = model->index(0, 0); diff --git a/test/main.h b/test/main.h index d443412..1a66075 100644 --- a/test/main.h +++ b/test/main.h @@ -10,23 +10,44 @@ #include "fake_canvas.h" #include +#include +#include +#include class TestGui : public QObject { Q_OBJECT + int id = 1; + std::mutex id_mtx; + std::vector tmp_settings; public: - TestGui() : app(new FakeCanvas(), "canvas-sync-test.ini"){}; - - MainWindow app; - // convenience routines - void authenticate(); + void authenticate(MainWindow *app); + MainWindow *create_app() + { + return new MainWindow(new FakeCanvas(), settings_file()); + } + QString settings_file() + { + this->id_mtx.lock(); + QString result = QString("canvas-sync-test-%1.ini").arg(this->id++); + tmp_settings.push_back(MainWindow::settings_path + '/' + result); + this->id_mtx.unlock(); + return result; + } private slots: // tests void access_token_entry_test(); void fetch_courses_test(); void fetch_courses_ui_test(); + void cleanupTestCase() + { + this->id_mtx.lock(); + for (auto i : this->tmp_settings) + std::filesystem::remove(i.toStdString()); + this->id_mtx.unlock(); + }; }; #endif