Skip to content

Commit

Permalink
Fix status bar update when switching to other DB (#9073)
Browse files Browse the repository at this point in the history
* Gui tests: add validation of StatusBarLabel in some tests
  • Loading branch information
dmaslenko committed Feb 8, 2023
1 parent bba0c09 commit 5bd8715
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ MainWindow::MainWindow()
statusBar()->addPermanentWidget(m_progressBar);
connect(clipboard(), SIGNAL(updateCountdown(int, QString)), this, SLOT(updateProgressBar(int, QString)));
m_statusBarLabel = new QLabel(statusBar());
m_statusBarLabel->setObjectName("statusBarLabel");
statusBar()->addPermanentWidget(m_statusBarLabel);

restoreConfigState();
Expand Down Expand Up @@ -1352,6 +1353,7 @@ void MainWindow::databaseTabChanged(int tabIndex)
}

m_actionMultiplexer.setCurrentObject(m_ui->tabWidget->currentDatabaseWidget());
updateEntryCountLabel();
}

void MainWindow::closeEvent(QCloseEvent* event)
Expand Down
42 changes: 41 additions & 1 deletion tests/gui/TestGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ void TestGui::testCreateDatabase()

triggerAction("actionDatabaseNew");

QCOMPARE(m_tabWidget->count(), 2);

statusBarLabelShouldBe("0 Entry(s)");

// there is a new empty db
m_db = m_tabWidget->currentDatabaseWidget()->database();
QCOMPARE(m_db->rootGroup()->children().size(), 0);
Expand All @@ -306,6 +310,15 @@ void TestGui::testCreateDatabase()
compositeKey->addKey(fileKey);
QCOMPARE(m_db->key()->rawKey(), compositeKey->rawKey());

statusBarLabelShouldBe("0 Entry(s)");

// Test the switching to other DB tab
m_tabWidget->setCurrentIndex(0);
statusBarLabelShouldBe("1 Entry(s)");

m_tabWidget->setCurrentIndex(1);
statusBarLabelShouldBe("0 Entry(s)");

// close the new database
MessageBox::setNextAnswer(MessageBox::No);
triggerAction("actionDatabaseClose");
Expand Down Expand Up @@ -592,6 +605,9 @@ void TestGui::testAddEntry()
auto* toolBar = m_mainWindow->findChild<QToolBar*>("toolBar");
auto* entryView = m_dbWidget->findChild<EntryView*>("entryView");

// Given the status bar label with initial number of entries.
statusBarLabelShouldBe("1 Entry(s)");

// Find the new entry action
auto* entryNewAction = m_mainWindow->findChild<QAction*>("actionEntryNew");
QVERIFY(entryNewAction->isEnabled());
Expand Down Expand Up @@ -626,6 +642,9 @@ void TestGui::testAddEntry()

m_db->updateCommonUsernames();

// Then the status bar label should be updated with incremented number of entries.
statusBarLabelShouldBe("2 Entry(s)");

// Add entry "something 2"
QTest::mouseClick(entryNewWidget, Qt::LeftButton);
QTest::keyClicks(titleEdit, "something 2");
Expand All @@ -645,6 +664,9 @@ void TestGui::testAddEntry()
QCOMPARE(entry->username(), QString("AutocompletionUsername"));
QCOMPARE(entry->historyItems().size(), 0);

// Then the status bar label should be updated with incremented number of entries.
statusBarLabelShouldBe("3 Entry(s)");

// Add entry "something 5" but click cancel button (does NOT add entry)
QTest::mouseClick(entryNewWidget, Qt::LeftButton);
QTest::keyClicks(titleEdit, "something 5");
Expand All @@ -653,8 +675,9 @@ void TestGui::testAddEntry()

QApplication::processEvents();

// Confirm entry count
// Confirm no changed entry count
QTRY_COMPARE(entryView->model()->rowCount(), 3);
statusBarLabelShouldBe("3 Entry(s)");
}

void TestGui::testPasswordEntryEntropy_data()
Expand Down Expand Up @@ -1087,6 +1110,7 @@ void TestGui::testDeleteEntry()
{
// Add canned entries for consistent testing
addCannedEntries();
statusBarLabelShouldBe("4 Entry(s)");

auto* groupView = m_dbWidget->findChild<GroupView*>("groupView");
auto* entryView = m_dbWidget->findChild<EntryView*>("entryView");
Expand Down Expand Up @@ -1116,6 +1140,8 @@ void TestGui::testDeleteEntry()
QCOMPARE(m_db->metadata()->recycleBin()->entries().size(), 1);
}

statusBarLabelShouldBe("3 Entry(s)");

// Select multiple entries and move them to the recycling bin
clickIndex(entryView->model()->index(1, 1), entryView, Qt::LeftButton);
clickIndex(entryView->model()->index(2, 1), entryView, Qt::LeftButton, Qt::ControlModifier);
Expand All @@ -1136,6 +1162,7 @@ void TestGui::testDeleteEntry()
QCOMPARE(entryView->model()->rowCount(), 1);
QCOMPARE(m_db->metadata()->recycleBin()->entries().size(), 3);
}
statusBarLabelShouldBe("1 Entry(s)");

// Go to the recycling bin
QCOMPARE(groupView->currentGroup(), m_db->rootGroup());
Expand All @@ -1144,13 +1171,15 @@ void TestGui::testDeleteEntry()
groupView,
Qt::LeftButton);
QCOMPARE(groupView->currentGroup()->name(), m_db->metadata()->recycleBin()->name());
statusBarLabelShouldBe("3 Entry(s)");

// Delete one entry from the bin
clickIndex(entryView->model()->index(0, 1), entryView, Qt::LeftButton);
MessageBox::setNextAnswer(MessageBox::Cancel);
QTest::mouseClick(entryDeleteWidget, Qt::LeftButton);
QCOMPARE(entryView->model()->rowCount(), 3);
QCOMPARE(m_db->metadata()->recycleBin()->entries().size(), 3);
statusBarLabelShouldBe("3 Entry(s)");

MessageBox::setNextAnswer(MessageBox::Delete);
QTest::mouseClick(entryDeleteWidget, Qt::LeftButton);
Expand All @@ -1164,6 +1193,7 @@ void TestGui::testDeleteEntry()
QTest::mouseClick(entryDeleteWidget, Qt::LeftButton);
QCOMPARE(entryView->model()->rowCount(), 0);
QCOMPARE(m_db->metadata()->recycleBin()->entries().size(), 0);
statusBarLabelShouldBe("0 Entry(s)");

// Ensure the entry preview widget shows the recycling group since all entries are deleted
auto* previewWidget = m_dbWidget->findChild<EntryPreviewWidget*>("previewWidget");
Expand Down Expand Up @@ -1883,6 +1913,16 @@ void TestGui::checkSaveDatabase()
QFAIL("Could not save database.");
}

void TestGui::statusBarLabelShouldBe(const char* expectedText)
{
// Wait a little to have updated status bar text.
// It fails with 150ms on i7 2.5Ghz, let's have double more time: 300ms.
Tools::wait(300);
auto* statusBarLabel = m_mainWindow->findChild<QLabel*>("statusBarLabel");
QVERIFY2(statusBarLabel->isVisible(), "StatusBarLabel is to be visible");
QCOMPARE(statusBarLabel->text(), QString(expectedText));
}

void TestGui::triggerAction(const QString& name)
{
auto* action = m_mainWindow->findChild<QAction*>(name);
Expand Down
1 change: 1 addition & 0 deletions tests/gui/TestGui.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ private slots:
Qt::MouseButton button,
Qt::KeyboardModifiers stateKey = 0);
void checkSaveDatabase();
void statusBarLabelShouldBe(const char* expectedText);

QScopedPointer<MainWindow> m_mainWindow;
QPointer<DatabaseTabWidget> m_tabWidget;
Expand Down

0 comments on commit 5bd8715

Please sign in to comment.