Skip to content

Commit 244ed42

Browse files
committed
Improve remote sync tests
1 parent af2d0b1 commit 244ed42

File tree

4 files changed

+36
-35
lines changed

4 files changed

+36
-35
lines changed

tests/gui/TestGui.cpp

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -375,26 +375,27 @@ void TestGui::testMergeDatabase()
375375
QCOMPARE(m_db->rootGroup()->findChildByName("General")->entries().size(), 1);
376376
}
377377

378-
void TestGui::prepareAndTriggerRemoteSync(const QString& sourceToSync)
378+
void TestGui::prepareAndTriggerRemoteSync()
379379
{
380-
auto* menuRemoteSync = m_mainWindow->findChild<QMenu*>("menuRemoteSync");
381-
QSignalSpy remoteAboutToShow(menuRemoteSync, &QMenu::aboutToShow);
382-
QApplication::processEvents();
383-
384380
// create remote settings in settings dialog
385381
triggerAction("actionDatabaseSettings");
386-
auto* dbSettingsDialog = m_dbWidget->findChild<QWidget*>("databaseSettingsDialog");
387-
auto* dbSettingsCategoryList = dbSettingsDialog->findChild<CategoryListWidget*>("categoryList");
388-
auto* dbSettingsStackedWidget = dbSettingsDialog->findChild<QStackedWidget*>("stackedWidget");
389-
dbSettingsCategoryList->setCurrentCategory(2); // go into remote category
382+
383+
auto dbSettingsDialog = m_dbWidget->findChild<DatabaseSettingsDialog*>("databaseSettingsDialog");
384+
QVERIFY(dbSettingsDialog);
385+
dbSettingsDialog->showRemoteSettings();
386+
390387
auto name = "testCommand";
391-
auto* nameEdit = dbSettingsStackedWidget->findChild<QLineEdit*>("nameLineEdit");
392-
auto* downloadCommandEdit = dbSettingsStackedWidget->findChild<QLineEdit*>("downloadCommand");
393-
QVERIFY(downloadCommandEdit != nullptr);
394-
downloadCommandEdit->setText(sourceToSync);
388+
auto nameEdit = dbSettingsDialog->findChild<QLineEdit*>("nameLineEdit");
389+
QVERIFY(nameEdit);
390+
QVERIFY(nameEdit->isVisible());
395391
nameEdit->setText(name);
396-
auto* saveSettingsButton = dbSettingsStackedWidget->findChild<QPushButton*>("saveSettingsButton");
397-
QVERIFY(saveSettingsButton != nullptr);
392+
393+
auto downloadCommandEdit = dbSettingsDialog->findChild<QLineEdit*>("downloadCommand");
394+
QVERIFY(downloadCommandEdit);
395+
downloadCommandEdit->setText("sftp user@server:Database.kdbx");
396+
397+
auto saveSettingsButton = dbSettingsDialog->findChild<QPushButton*>("saveSettingsButton");
398+
QVERIFY(saveSettingsButton);
398399
QTest::mouseClick(saveSettingsButton, Qt::LeftButton);
399400

400401
auto okButton = dbSettingsDialog->findChild<QDialogButtonBox*>("buttonBox")->button(QDialogButtonBox::Ok);
@@ -403,32 +404,33 @@ void TestGui::prepareAndTriggerRemoteSync(const QString& sourceToSync)
403404

404405
QTRY_COMPARE(m_dbWidget->getRemoteParams().size(), 1);
405406

406-
// trigger aboutToShow to create remote actions
407-
menuRemoteSync->popup(QPoint(0, 0));
407+
// Show menu to trigger populating with remote sync action
408+
auto menuRemoteSync = m_mainWindow->findChild<QMenu*>("menuRemoteSync");
409+
QVERIFY(menuRemoteSync);
410+
menuRemoteSync->popup({0, 0});
408411
QApplication::processEvents();
409-
QTRY_COMPARE(remoteAboutToShow.count(), 1);
410-
// close the opened menu
411-
QTest::keyClick(menuRemoteSync, Qt::Key::Key_Escape);
412+
menuRemoteSync->close();
412413

413-
// trigger remote sync action
414-
for (auto* remoteAction : menuRemoteSync->actions()) {
414+
// Trigger the remote sync action
415+
for (const auto remoteAction : menuRemoteSync->actions()) {
415416
if (remoteAction->text() == name) {
416417
remoteAction->trigger();
417-
break;
418+
return;
418419
}
419420
}
420-
QApplication::processEvents();
421+
422+
// If we get here then something didn't work properly
423+
QFAIL("Remote sync action not present in menu.");
421424
}
422425

423426
void TestGui::testRemoteSyncDatabaseSameKey()
424427
{
425-
QString sourceToSync = "sftp user@server:Database.kdbx";
426-
RemoteHandler::setRemoteProcessFunc([sourceToSync](QObject* parent) {
428+
RemoteHandler::setRemoteProcessFunc([](QObject* parent) {
427429
return QScopedPointer<RemoteProcess>(
428430
new MockRemoteProcess(parent, QString(KEEPASSX_TEST_DATA_DIR).append("/SyncDatabase.kdbx")));
429431
});
430432
QSignalSpy dbSyncSpy(m_dbWidget.data(), &DatabaseWidget::databaseSyncCompleted);
431-
prepareAndTriggerRemoteSync(sourceToSync);
433+
prepareAndTriggerRemoteSync();
432434
QTRY_COMPARE(dbSyncSpy.count(), 1);
433435

434436
m_db = m_tabWidget->currentDatabaseWidget()->database();
@@ -443,13 +445,12 @@ void TestGui::testRemoteSyncDatabaseSameKey()
443445

444446
void TestGui::testRemoteSyncDatabaseRequiresPassword()
445447
{
446-
QString sourceToSync = "sftp user@server:Database.kdbx";
447-
RemoteHandler::setRemoteProcessFunc([sourceToSync](QObject* parent) {
448+
RemoteHandler::setRemoteProcessFunc([](QObject* parent) {
448449
return QScopedPointer<RemoteProcess>(new MockRemoteProcess(
449450
parent, QString(KEEPASSX_TEST_DATA_DIR).append("/SyncDatabaseDifferentPassword.kdbx")));
450451
});
451452
QSignalSpy dbSyncSpy(m_dbWidget.data(), &DatabaseWidget::databaseSyncCompleted);
452-
prepareAndTriggerRemoteSync(sourceToSync);
453+
prepareAndTriggerRemoteSync();
453454

454455
// need to process more events as opening with the same key did not work and more events have been fired
455456
QApplication::processEvents(QEventLoop::WaitForMoreEvents);

tests/gui/TestGui.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private slots:
8989
Qt::KeyboardModifiers stateKey = {});
9090
void checkSaveDatabase();
9191
void checkStatusBarText(const QString& textFragment);
92-
void prepareAndTriggerRemoteSync(const QString& sourceToSync);
92+
void prepareAndTriggerRemoteSync();
9393

9494
QScopedPointer<MainWindow> m_mainWindow;
9595
QPointer<QLabel> m_statusBarLabel;

tests/mock/MockRemoteProcess.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919

2020
#include "MockRemoteProcess.h"
2121

22-
MockRemoteProcess::MockRemoteProcess(QObject* parent, const QString& dbPath)
22+
MockRemoteProcess::MockRemoteProcess(QObject* parent, QString dbPath)
2323
: RemoteProcess(parent)
24-
, m_dbPath(dbPath)
24+
, m_dbPath(std::move(dbPath))
2525
{
2626
}
2727

2828
void MockRemoteProcess::start(const QString&)
2929
{
30-
QFile ::copy(m_dbPath, m_tempFileLocation);
30+
QFile::copy(m_dbPath, m_tempFileLocation);
3131
}
3232

3333
qint64 MockRemoteProcess::write(const QString& data)

tests/mock/MockRemoteProcess.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
class MockRemoteProcess : public RemoteProcess
2424
{
2525
public:
26-
explicit MockRemoteProcess(QObject* parent, const QString& dbPath);
26+
explicit MockRemoteProcess(QObject* parent, QString dbPath);
2727
~MockRemoteProcess() override = default;
2828

2929
void start(const QString& program) override;

0 commit comments

Comments
 (0)