Skip to content

Commit

Permalink
fix: edge case - disable autosave delay after triggering a save
Browse files Browse the repository at this point in the history
User might trigger the autosave delay timer and disable autosave delay or autosave.
This commmit fixes potential issues from the timer triggering after the feature is disabled.
The timer now checks if the feature is still enabled when triggered.
Adds tests for those edge cases as well
  • Loading branch information
jNullj committed Jul 6, 2023
1 parent 1c012a2 commit a8e430d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/gui/DatabaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1535,7 +1535,7 @@ void DatabaseWidget::onDatabaseModified()
refreshSearch();
int autosaveDelayMs = m_db->metadata()->autosaveDelayMin() * 60 * 1000; // min to msec for QTimer
bool autosaveAfterEveryChangeConfig = config()->get(Config::AutoSaveAfterEveryChange).toBool();
if (m_autosaveTimer->isActive() || (autosaveDelayMs > 0 && autosaveAfterEveryChangeConfig)) {
if (autosaveDelayMs > 0 && autosaveAfterEveryChangeConfig) {
// reset delay when modified
m_autosaveTimer->start(autosaveDelayMs);
return;
Expand All @@ -1550,6 +1550,12 @@ void DatabaseWidget::onDatabaseModified()

void DatabaseWidget::onAutosaveDelayTimeout()
{
const bool isAutosaveDelayEnabled = m_db->metadata()->autosaveDelayMin() > 0;
const bool autosaveAfterEveryChangeConfig = config()->get(Config::AutoSaveAfterEveryChange).toBool();
if (!(isAutosaveDelayEnabled && autosaveAfterEveryChangeConfig)) {
// User might disable the delay/autosave while the timer is running
return;
}
if (!m_blockAutoSave) {
save();
} else {
Expand Down
38 changes: 37 additions & 1 deletion tests/gui/TestGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,43 @@ void TestGui::testDatabaseSettings()
m_dbWidget->triggerAutosaveTimer();
QTRY_COMPARE(writeDbSignalSpy.count(), 1);

// 4 cleanup
// 4 Test no delay when disabled autosave or autosaveDelay
// 4.a) create new entry
QTest::mouseClick(entryNewWidget, Qt::LeftButton);
QCOMPARE(m_dbWidget->currentMode(), DatabaseWidget::Mode::EditMode);
QTest::keyClicks(titleEdit, "Test autosaveDelay 3");

// 4.b) Save changes
editEntryWidget->setCurrentPage(0);
editEntryWidgetButtonBox = editEntryWidget->findChild<QDialogButtonBox*>("buttonBox");
QTest::mouseClick(editEntryWidgetButtonBox->button(QDialogButtonBox::Ok), Qt::LeftButton);

// 4.c) Start timer
Tools::wait(150); // due to modify timer

// 4.d) Disable autosave
config()->set(Config::AutoSaveAfterEveryChange, false);

// 4.e) Make sure changes are not saved
m_dbWidget->triggerAutosaveTimer();
QTRY_COMPARE(writeDbSignalSpy.count(), 1);

// 4.f) Repeat for autosaveDelay
config()->set(Config::AutoSaveAfterEveryChange, true);
QTest::mouseClick(entryNewWidget, Qt::LeftButton);
QCOMPARE(m_dbWidget->currentMode(), DatabaseWidget::Mode::EditMode);
QTest::keyClicks(titleEdit, "Test autosaveDelay 4");
editEntryWidget->setCurrentPage(0);
editEntryWidgetButtonBox = editEntryWidget->findChild<QDialogButtonBox*>("buttonBox");
QTest::mouseClick(editEntryWidgetButtonBox->button(QDialogButtonBox::Ok), Qt::LeftButton);
Tools::wait(150); // due to modify timer
m_db->metadat()->setAutosaveDelayMin(0);

// 4.g) Make sure changes are not saved
m_dbWidget->triggerAutosaveTimer();
QTRY_COMPARE(writeDbSignalSpy.count(), 1);

// 5 Cleanup
config()->set(Config::AutoSaveAfterEveryChange, false);
}

Expand Down

0 comments on commit a8e430d

Please sign in to comment.