Skip to content

Commit

Permalink
Tests: add unit tests for app/DocumentFilesWatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
HuguesDelorme committed Apr 3, 2024
1 parent 03b944f commit 33b09e0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
58 changes: 58 additions & 0 deletions tests/test_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
#include "test_app.h"

#include "../src/app/app_module.h"
#include "../src/app/document_files_watcher.h"
#include "../src/app/qstring_utils.h"
#include "../src/app/qtgui_utils.h"
#include "../src/app/recent_files.h"
#include "../src/app/theme.h"
#include "../src/base/application.h"
#include "../src/base/document.h"
#include "../src/base/io_system.h"
#include "../src/io_occ/io_occ.h"
#include "../src/io_ply/io_ply_reader.h"
#include "../src/qtcommon/filepath_conv.h"
#include "../src/qtcommon/qstring_conv.h"

Expand Down Expand Up @@ -62,6 +67,59 @@ RecentFile createRecentFile(const QPixmap& thumbnail)

} // namespace

void TestApp::DocumentFilesWatcher_test()
{
IO::System ioSystem;
ioSystem.addFactoryReader(std::make_unique<IO::PlyFactoryReader>());

auto app = Application::instance();

DocumentFilesWatcher docFilesWatcher(app);
docFilesWatcher.enable(true);
Document::Identifier changedDocId = -1;
docFilesWatcher.signalDocumentFileChanged.connectSlot([&](DocumentPtr changedDoc) {
changedDocId = changedDoc->identifier();
});

const FilePath cadFilePath = "tests/outputs/temp-cube.ply";
auto fnCopyCadFile = [=]{
std_filesystem::copy_file(
"tests/inputs/cube.ply",
cadFilePath,
std_filesystem::copy_options::overwrite_existing
);
};

DocumentPtr doc = app->newDocument();
doc->setFilePath(cadFilePath);
fnCopyCadFile();
auto _ = gsl::finally([=]{ app->closeDocument(doc); });
const bool okImport = ioSystem.importInDocument()
.targetDocument(doc)
.withFilepath(cadFilePath)
.execute();
QVERIFY(okImport);

// Check file change on document is caught
fnCopyCadFile();
const bool okWait = QTest::qWaitFor([&]{ return changedDocId != -1; });
QVERIFY(okWait);
QCOMPARE(changedDocId, doc->identifier());

// Check further file changes are not monitored until last one is acknowledged
changedDocId = -1;
fnCopyCadFile();
QTest::qWait(125/*ms*/);
QCOMPARE(changedDocId, -1);

// Check closing document unmonitors file in DocumentFilesWatcher
docFilesWatcher.acknowledgeDocumentFileChange(doc);
app->closeDocument(doc);
fnCopyCadFile();
QTest::qWait(125/*ms*/);
QCOMPARE(changedDocId, -1);
}

void TestApp::FilePathConv_test()
{
const char strTestPath[] = "../as1-oc-214 - 測試文件.stp";
Expand Down
2 changes: 2 additions & 0 deletions tests/test_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace Mayo {
class TestApp : public QObject {
Q_OBJECT
private slots:
void DocumentFilesWatcher_test();

void FilePathConv_test();

void QStringUtils_append_test();
Expand Down

0 comments on commit 33b09e0

Please sign in to comment.