diff --git a/src/gui/EditEntryWidget.cpp b/src/gui/EditEntryWidget.cpp index fe09b1549..163912e0a 100644 --- a/src/gui/EditEntryWidget.cpp +++ b/src/gui/EditEntryWidget.cpp @@ -134,6 +134,7 @@ void EditEntryWidget::loadEntry(Entry* entry, bool create, const QString& groupN { m_entry = entry; m_metadata = metadata; + m_create = create; if (create) { m_ui->headerLabel->setText(groupName+" > "+tr("Add entry")); @@ -212,7 +213,9 @@ void EditEntryWidget::saveEntry() m_currentAttribute = QPersistentModelIndex(); - m_entry->beginUpdate(); + if (!m_create) { + m_entry->beginUpdate(); + } m_entry->setTitle(m_mainUi->titleEdit->text()); m_entry->setUsername(m_mainUi->usernameEdit->text()); @@ -245,7 +248,9 @@ void EditEntryWidget::saveEntry() } } - m_entry->endUpdate(); + if (!m_create) { + m_entry->endUpdate(); + } m_entry = 0; m_entryAttributes->clear(); diff --git a/src/gui/EditEntryWidget.h b/src/gui/EditEntryWidget.h index dee356c68..0de20f4f5 100644 --- a/src/gui/EditEntryWidget.h +++ b/src/gui/EditEntryWidget.h @@ -83,6 +83,7 @@ private: Entry* m_entry; Metadata* m_metadata; + bool m_create; const QScopedPointer m_ui; const QScopedPointer m_mainUi; const QScopedPointer m_notesUi; diff --git a/tests/gui/TestGui.cpp b/tests/gui/TestGui.cpp index d28400672..8001c86f5 100644 --- a/tests/gui/TestGui.cpp +++ b/tests/gui/TestGui.cpp @@ -28,10 +28,12 @@ #include "config-keepassx-tests.h" #include "tests.h" #include "crypto/Crypto.h" +#include "core/Entry.h" #include "gui/DatabaseTabWidget.h" #include "gui/DatabaseWidget.h" #include "gui/EditEntryWidget.h" #include "gui/EntryView.h" +#include "gui/EntryModel.h" #include "gui/FileDialog.h" #include "gui/MainWindow.h" @@ -97,6 +99,58 @@ void TestGui::testEditEntry() QCOMPARE(tabWidget->tabText(tabWidget->currentIndex()), QString("NewDatabase.kdbx")); } +void TestGui::testAddEntry() +{ + DatabaseTabWidget* tabWidget = m_mainWindow->findChild("tabWidget"); + DatabaseWidget* dbWidget = tabWidget->currentDatabaseWidget(); + + EntryView* entryView = dbWidget->findChild("entryView"); + QAction* entryNewAction = m_mainWindow->findChild("actionEntryNew"); + QVERIFY(entryNewAction->isEnabled()); + QToolBar* toolBar = m_mainWindow->findChild("toolBar"); + QWidget* entryNewWidget = toolBar->widgetForAction(entryNewAction); + QVERIFY(entryNewWidget->isVisible()); + QVERIFY(entryNewWidget->isEnabled()); + + QTest::mouseClick(entryNewWidget, Qt::LeftButton); + QTest::qWait(20); + + QCOMPARE(dbWidget->currentMode(), DatabaseWidget::EditMode); + + EditEntryWidget* editEntryWidget = dbWidget->findChild("editEntryWidget"); + QLineEdit* titleEdit = editEntryWidget->findChild("titleEdit"); + QTest::keyClicks(titleEdit, "test"); + QTest::qWait(20); + + QDialogButtonBox* editEntryWidgetButtonBox = editEntryWidget->findChild("buttonBox"); + QTest::mouseClick(editEntryWidgetButtonBox->button(QDialogButtonBox::Ok), Qt::LeftButton); + QTest::qWait(20); + + QCOMPARE(dbWidget->currentMode(), DatabaseWidget::ViewMode); + QModelIndex item = entryView->model()->index(1, 0); + Entry* entry = static_cast(entryView->model())->entryFromIndex(item); + + QCOMPARE(entry->title(), QString("test")); + QCOMPARE(entry->historyItems().size(), 0); + QCOMPARE(tabWidget->tabText(tabWidget->currentIndex()), QString("NewDatabase.kdbx*")); + + QAction* entryEditAction = m_mainWindow->findChild("actionEntryEdit"); + QVERIFY(entryEditAction->isEnabled()); + QWidget* entryEditWidget = toolBar->widgetForAction(entryEditAction); + QVERIFY(entryEditWidget->isVisible()); + QVERIFY(entryEditWidget->isEnabled()); + QTest::mouseClick(entryEditWidget, Qt::LeftButton); + QTest::qWait(20); + + QCOMPARE(dbWidget->currentMode(), DatabaseWidget::EditMode); + QTest::keyClicks(titleEdit, "something"); + QTest::mouseClick(editEntryWidgetButtonBox->button(QDialogButtonBox::Ok), Qt::LeftButton); + QTest::qWait(20); + + QCOMPARE(entry->title(), QString("testsomething")); + QCOMPARE(entry->historyItems().size(), 1); +} + void TestGui::cleanupTestCase() { delete m_mainWindow; diff --git a/tests/gui/TestGui.h b/tests/gui/TestGui.h index 70facdc57..aedaebf88 100644 --- a/tests/gui/TestGui.h +++ b/tests/gui/TestGui.h @@ -31,6 +31,7 @@ private Q_SLOTS: void testOpenDatabase(); void testTabs(); void testEditEntry(); + void testAddEntry(); void cleanupTestCase(); private: