Skip to content

Commit

Permalink
ci: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenvukhang committed Feb 6, 2023
1 parent 805be5b commit b49352f
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 70 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ jobs:
# build canvas-sync
- run: make build

# - name: Run tests
# run: ./build/test/CanvasSyncTest
- name: Run tests
run: ./build/test/CanvasSyncTest

# package app
- run: |
Expand Down Expand Up @@ -90,7 +90,9 @@ jobs:
with:
arch: win64_msvc2019_64
version: ${{ matrix.version }}
setup-python: false
tools: tools_ifw
archives: qtbase qttools

- name: Install MSVC 2022
uses: ilammy/msvc-dev-cmd@v1
Expand All @@ -109,10 +111,8 @@ jobs:
-DCMAKE_PREFIX_PATH=${{ env.Qt6_DIR }}
cmake --build build --parallel
# - name: Run tests
# run: |
# ls build/test
# ./build/test/CanvasSyncTest.exe
- name: Run tests
run: ./build/test/CanvasSyncTest.exe -tap

- name: Use windeployqt to load libraries
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ QT_SRC_DIR ?= $(MAKEFILE_DIR)/Qt/src

QT_CFG_ARGS := -opensource -release -static -nomake examples -nomake tests -skip qt3d -skip qttranslations -skip qt5compat -skip qtactiveqt -skip qtcharts -skip qtcoap -skip qtconnectivity -skip qtdatavis3d -skip qtdeclarative -skip qtdoc -skip qthttpserver -skip qtlanguageserver -skip qtlottie -skip qtmqtt -skip qtmultimedia -skip qtnetworkauth -skip qtopcua -skip qtpositioning -skip qtquick3d -skip qtquick3dphysics -skip qtquicktimeline -skip qtremoteobjects -skip qtscxml -skip qtsensors -skip qtserialbus -skip qtserialport -skip qtshadertools -skip qtspeech -skip qtsvg -skip qttools -skip qtwebsockets
QT_INIT_ARGS := --module-subset=qtbase
QT_VERSION ?= 6.2.0
QT_VERSION ?= 6.2.4

main:
@make --no-print-directory build
Expand Down
2 changes: 2 additions & 0 deletions app/clickable_tree_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ClickableTreeView::ClickableTreeView(QWidget *parent) : QTreeView(parent)
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, &QTreeView::customContextMenuRequested, this,
[&](const QPoint &pos) { this->context_menu(pos); });
this->hideColumn(TreeIndex::FOLDER_ID);
}

TreeModel *ClickableTreeView::model() const
Expand All @@ -42,6 +43,7 @@ TreeModel *ClickableTreeView::model() const
void ClickableTreeView::setModel(TreeModel *model)
{
QTreeView::setModel(model);
this->hideColumn(TreeIndex::FOLDER_ID);
this->expand_tracked();
};

Expand Down
20 changes: 11 additions & 9 deletions app/include/csync/tree_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

class TreeIndex : public QModelIndex
{
QString data(int i) const { return siblingAtColumn(i).data().toString(); }
QString data_at(int i) const { return siblingAtColumn(i).data().toString(); }

public:
enum TreeCol { REMOTE_DIR, LOCAL_DIR, FOLDER_ID };
TreeIndex(const QModelIndex i) : QModelIndex(i){};

const QModelIndex index() { return this->index(); }
QString id() const { return data(TreeCol::FOLDER_ID); }
QString local_dir() const { return data(TreeCol::LOCAL_DIR); }
QString remote_dir() const { return data(TreeCol::REMOTE_DIR); }
const QModelIndex index() { return *this; }
QString id() const { return this->data_at(TreeCol::FOLDER_ID); }
QString local_dir() const { return this->data_at(TreeCol::LOCAL_DIR); }
QString remote_dir() const { return this->data_at(TreeCol::REMOTE_DIR); }

QString course() const
{
Expand Down Expand Up @@ -61,8 +61,10 @@ class TreeIndex : public QModelIndex
{
int i = 0;
const QAbstractItemModel *m = this->model();
while (m->index(i, 0, this->index()).isValid())
while (m->index(i, 0, this->index()).isValid()) {
qDebug() << m->index(i + 1, 0, this->index());
i++;
}
return i;
}
};
Expand Down Expand Up @@ -107,9 +109,9 @@ class TreeItem

public:
std::vector<Folder> resolve_folders();
QString get_id() const { return str_data(TreeIndex::FOLDER_ID); };
QString get_local_dir() const { return str_data(TreeIndex::LOCAL_DIR); };
QString get_remote_dir() const { return str_data(TreeIndex::REMOTE_DIR); };
QString id() const { return str_data(TreeIndex::FOLDER_ID); };
QString local_dir() const { return str_data(TreeIndex::LOCAL_DIR); };
QString remote_dir() const { return str_data(TreeIndex::REMOTE_DIR); };

private:
QVector<QVariant> itemData;
Expand Down
2 changes: 1 addition & 1 deletion app/include/csync/ui_mainwindow.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/********************************************************************************
** Form generated from reading UI file 'mainwindow.ui'
**
** Created by: Qt User Interface Compiler version 6.2.0
** Created by: Qt User Interface Compiler version 6.2.4
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion app/include/csync/ui_updates.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/********************************************************************************
** Form generated from reading UI file 'updates.ui'
**
** Created by: Qt User Interface Compiler version 6.2.0
** Created by: Qt User Interface Compiler version 6.2.4
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
Expand Down
4 changes: 2 additions & 2 deletions app/mainwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ void MainWindow::setup_ui()
ui->pushButton_changeToken->hide();
ui->pushButton_changeToken->setEnabled(false);
ui->progressBar->hide();
ui->treeView->setModel(newTreeModel());
ui->guideText->hide();
ui->label_accessTokenHelp->hide();
ui->treeView->setModel(newTreeModel());
}

void MainWindow::connect_ui()
Expand Down Expand Up @@ -370,7 +370,7 @@ std::vector<Folder> MainWindow::gather_tracked()

TreeModel *MainWindow::newTreeModel()
{
return new TreeModel({"canvas folder", "local folder"});
return new TreeModel({"canvas folder", "local folder", ""});
};

const QString MainWindow::settings_path =
Expand Down
12 changes: 6 additions & 6 deletions app/tree_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ void TreeItem::track_folder(const TreeIndex &ti, const QString &dir,
// clear children maps
on_all_children([&](TreeItem &child) {
child.setData(TreeIndex::LOCAL_DIR, "");
QString folder_id = child.get_id();
QString folder_id = child.id();
if (!folder_id.isEmpty()) settings.remove(folder_id);
});

// clear parent maps
on_all_parents([&](TreeItem &parent) {
parent.setData(TreeIndex::LOCAL_DIR, "");
QString folder_id = parent.get_id();
QString folder_id = parent.id();
if (!folder_id.isEmpty()) settings.remove(folder_id);
});

Expand All @@ -168,13 +168,13 @@ void TreeItem::track_folder(const TreeIndex &ti, const QString &dir,
void resolve_all_folders(TreeItem *item, std::filesystem::path *local_base_dir,
std::filesystem::path *cwd, std::vector<Folder> *list)
{
std::string local_dir = item->get_local_dir().toStdString();
std::string local_dir = item->local_dir().toStdString();

std::filesystem::path new_cwd;
std::filesystem::path new_base = *local_base_dir;

if (!local_base_dir->empty()) {
new_cwd = *cwd / item->get_remote_dir().toStdString();
new_cwd = *cwd / item->remote_dir().toStdString();
}

// one-time thing. on any path downwards it is guaranteed to only have
Expand All @@ -184,7 +184,7 @@ void resolve_all_folders(TreeItem *item, std::filesystem::path *local_base_dir,
}

if (!new_base.empty()) {
list->push_back(Folder(item->get_id().toInt(), new_base / new_cwd));
list->push_back(Folder(item->id().toInt(), new_base / new_cwd));
}

auto children = item->childrenItems();
Expand All @@ -205,7 +205,7 @@ std::vector<Folder> TreeItem::resolve_folders()
std::filesystem::path base = "";
resolve_all_folders(child, &base, &p, &fl);
}
int id = this->get_id().toInt();
int id = this->id().toInt();
for (size_t i = 0; i < fl.size(); i++) {
fl[i].course_id = id;
}
Expand Down
5 changes: 0 additions & 5 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
enable_testing(TRUE)

find_package(Qt6 REQUIRED COMPONENTS Widgets Network Gui Test)

Expand All @@ -27,10 +26,6 @@ target_link_libraries(${TEST_NAME} PRIVATE
csync
)

install(TARGETS ${TEST_NAME}
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

qt_finalize_target(${TEST_NAME})

add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
39 changes: 13 additions & 26 deletions test/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

QTEST_MAIN(TestGui)

void TestGui::authenticate(MainWindow *app)
void TestGui::authenticate()
{
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()
{
MainWindow *app = create_app();
QSignalSpy auth_tries(app->canvas, &ICanvas::authenticate_done);
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);
Expand All @@ -24,19 +23,18 @@ 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()
{
MainWindow *app = create_app();
authenticate(app);
authenticate();
std::string expected[] = {
"#0|Linear Algebra: {\n"
" #20010|course files: {\n"
Expand All @@ -58,38 +56,27 @@ 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()
{
MainWindow *app = create_app();
authenticate(app);
authenticate();
TreeModel *model = app.ui->treeView->model();
TreeIndex ptr(model->index(0, 0));

TreeModel *model = app->ui->treeView->model();
QCOMPARE(model->childrenCount(), 3);

TreeIndex ptr = model->index(0, 0);
// QCOMPARE(1, 2);

qDebug() << "!!!!!!!!!!!!!!!!!!! COUNT" << ptr.children_count();
QCOMPARE(ptr.course(), "Calculus");
QCOMPARE(ptr.children_count(), 1);
ptr = ptr.child(0);
QCOMPARE(ptr.remote_dir(), "course files");
QCOMPARE(ptr.child(0).remote_dir(), "Admin");
QCOMPARE(ptr.child(1).remote_dir(), "Tutorials");
qDebug() << "!!!!!!!!!!!!!!!!!!! COUNT" << ptr.children_count();

ptr = model->index(1, 0);
QCOMPARE(ptr.course(), "Geometry");
qDebug() << "!!!!!!!!!!!!!!!!!!! COUNT" << ptr.children_count();
QCOMPARE(ptr.children_count(), 10);
QCOMPARE(ptr.children_count(), 11);
ptr = ptr.child(0);
QCOMPARE(ptr.remote_dir(), "course files");
QCOMPARE(ptr.child(0).remote_dir(), "Course ntoes");
QCOMPARE(ptr.child(0).remote_dir(), "Course notes");
QCOMPARE(ptr.child(1).remote_dir(), "Lectures");
QCOMPARE(ptr.child(2).remote_dir(), "Tutorials");

Expand Down
17 changes: 4 additions & 13 deletions test/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,11 @@ class TestGui : public QObject
std::vector<QString> tmp_settings;

public:
TestGui() : app(new FakeCanvas(), "canvas-sync-test.ini"){};
MainWindow app;

// convenience routines
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;
}
void authenticate();

private slots: // tests
void access_token_entry_test();
Expand Down

0 comments on commit b49352f

Please sign in to comment.