Skip to content

Commit

Permalink
Reduce autosaveDelay test time
Browse files Browse the repository at this point in the history
Minimize the test time by spying for signals and forwarding time rather then waiting the delay.
Only wait where needed to finish modified signal timer
  • Loading branch information
jNullj committed Feb 18, 2023
1 parent 7fcd3af commit 7876bfb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
6 changes: 6 additions & 0 deletions src/gui/DatabaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,12 @@ void DatabaseWidget::onAutosaveDelayTimeout()
}
}

void DatabaseWidget::earlyAutosaveTrigger()
{
m_autosaveTimer->stop();
QMetaObject::invokeMethod(m_autosaveTimer, "timeout");
}

QString DatabaseWidget::getCurrentSearch()
{
return m_lastSearchText;
Expand Down
1 change: 1 addition & 0 deletions src/gui/DatabaseWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ public slots:
int autoHideTimeout = MessageWidget::DefaultAutoHideTimeout);
void showErrorMessage(const QString& errorMessage);
void hideMessage();
void earlyAutosaveTrigger();

protected:
void closeEvent(QCloseEvent* event) override;
Expand Down
28 changes: 12 additions & 16 deletions tests/gui/TestGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1505,11 +1505,9 @@ void TestGui::testDatabaseSettings()

// test autosave delay

// 1 get current change time and init settings

QFileInfo fileInfo(m_dbFilePath);
QDateTime lastModified = fileInfo.lastModified();
// 1 init
config()->set(Config::AutoSaveAfterEveryChange, true);
QSignalSpy writeDbSignalSpy(m_db.data(), &Database::databaseSaved);

// 2 create new entries

Expand Down Expand Up @@ -1539,10 +1537,10 @@ void TestGui::testDatabaseSettings()
auto* editEntryWidgetButtonBox = editEntryWidget->findChild<QDialogButtonBox*>("buttonBox");
QTest::mouseClick(editEntryWidgetButtonBox->button(QDialogButtonBox::Ok), Qt::LeftButton);

// 2.c) Wait half the time, make sure file was not modified yet
Tools::wait(autosaveDelayTestValue * 60 * 1000 / 2);
fileInfo.refresh();
QCOMPARE(fileInfo.lastModified(), lastModified);
// 2.c) Make sure file was not modified yet
QApplication::processEvents();
Tools::wait(150); // due to modify timer
QTRY_COMPARE(writeDbSignalSpy.count(), 0);

// 2.d) Create second entry to test delay timer reset
QVERIFY(entryNewWidget->isVisible());
Expand All @@ -1561,20 +1559,18 @@ void TestGui::testDatabaseSettings()
// 2.e) Save changes
editEntryWidget->setCurrentPage(0);
editEntryWidgetButtonBox = editEntryWidget->findChild<QDialogButtonBox*>("buttonBox");
QDateTime lastEntryModifiedTime = QDateTime::currentDateTime();
QTest::mouseClick(editEntryWidgetButtonBox->button(QDialogButtonBox::Ok), Qt::LeftButton);

// 3 Double check both true negative and true positive
// 3.a) Test unmodified prior to delay timeout
Tools::wait(autosaveDelayTestValue * 60 * 1000 / 2);
fileInfo.refresh();
QCOMPARE(fileInfo.lastModified(), lastModified);
QApplication::processEvents();
Tools::wait(150); // due to modify timer
QTRY_COMPARE(writeDbSignalSpy.count(), 0);

// 3.b) Test modification time after expected
int marginDelay = 10000; // error margin to account for extra save delays (ms)
Tools::wait(autosaveDelayTestValue * 60 * 1000 / 2 + marginDelay);
fileInfo.refresh();
QVERIFY2(fileInfo.lastModified() >= lastEntryModifiedTime, "AutosaveDelay: wrong timing");
QApplication::processEvents();
m_dbWidget->earlyAutosaveTrigger();
QTRY_COMPARE(writeDbSignalSpy.count(), 1);

// 4 cleanup
config()->set(Config::AutoSaveAfterEveryChange, false);
Expand Down

0 comments on commit 7876bfb

Please sign in to comment.