Allow copying passwords directly from searching

* Reverts removal of previously implemented feature
* Fix #2630
* Make gui search tests more robust
This commit is contained in:
Jonathan White 2019-04-07 12:00:02 -04:00
parent 88c8cdd800
commit 71e375aff0
2 changed files with 25 additions and 7 deletions

View File

@ -1,5 +1,4 @@
/* /*
* Copyright (C) 2016 Jonathan White <support@dmapps.us>
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org> * Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -97,6 +96,13 @@ bool SearchWidget::eventFilter(QObject* obj, QEvent* event)
if (keyEvent->key() == Qt::Key_Escape) { if (keyEvent->key() == Qt::Key_Escape) {
emit escapePressed(); emit escapePressed();
return true; return true;
} else if (keyEvent->matches(QKeySequence::Copy)) {
// If Control+C is pressed in the search edit when no text
// is selected, copy the password of the current entry.
if (!m_ui->searchEdit->hasSelectedText()) {
emit copyPressed();
return true;
}
} else if (keyEvent->matches(QKeySequence::MoveToNextLine)) { } else if (keyEvent->matches(QKeySequence::MoveToNextLine)) {
if (m_ui->searchEdit->cursorPosition() == m_ui->searchEdit->text().length()) { if (m_ui->searchEdit->cursorPosition() == m_ui->searchEdit->text().length()) {
// If down is pressed at EOL, move the focus to the entry view // If down is pressed at EOL, move the focus to the entry view

View File

@ -849,19 +849,31 @@ void TestGui::testSearch()
QTRY_VERIFY(searchTextEdit->hasFocus()); QTRY_VERIFY(searchTextEdit->hasFocus());
QTest::keyClick(searchTextEdit, Qt::Key_Down); QTest::keyClick(searchTextEdit, Qt::Key_Down);
QTRY_VERIFY(entryView->hasFocus()); QTRY_VERIFY(entryView->hasFocus());
auto* searchedEntry = entryView->currentEntry();
// Restore focus and search text selection // Restore focus and search text selection
QTest::keyClick(m_mainWindow.data(), Qt::Key_F, Qt::ControlModifier); QTest::keyClick(m_mainWindow.data(), Qt::Key_F, Qt::ControlModifier);
QTRY_COMPARE(searchTextEdit->selectedText(), QString("someTHING")); QTRY_COMPARE(searchTextEdit->selectedText(), QString("someTHING"));
QTRY_VERIFY(searchTextEdit->hasFocus());
searchedEntry->setPassword("password");
QClipboard* clipboard = QApplication::clipboard();
// Attempt password copy with selected test (should fail)
QTest::keyClick(searchTextEdit, Qt::Key_C, Qt::ControlModifier);
QVERIFY(clipboard->text() != searchedEntry->password());
// Deselect text and confirm password copies
QTest::mouseClick(searchTextEdit, Qt::LeftButton);
QTRY_VERIFY(searchTextEdit->selectedText().isEmpty());
QTRY_VERIFY(searchTextEdit->hasFocus());
QTest::keyClick(searchTextEdit, Qt::Key_C, Qt::ControlModifier);
QCOMPARE(searchedEntry->password(), clipboard->text());
// Ensure Down focuses on entry view when search text is selected // Ensure Down focuses on entry view when search text is selected
QTest::keyClick(searchTextEdit, Qt::Key_Down); QTest::keyClick(searchTextEdit, Qt::Key_Down);
QTRY_VERIFY(entryView->hasFocus()); QTRY_VERIFY(entryView->hasFocus());
QCOMPARE(entryView->selectionModel()->currentIndex().row(), 0); QCOMPARE(entryView->currentEntry(), searchedEntry);
// Test that password copies (entry has focus) // Test that password copies with entry focused
QClipboard* clipboard = QApplication::clipboard();
QTest::keyClick(entryView, Qt::Key_C, Qt::ControlModifier); QTest::keyClick(entryView, Qt::Key_C, Qt::ControlModifier);
QModelIndex searchedItem = entryView->model()->index(0, 1); QCOMPARE(searchedEntry->password(), clipboard->text());
Entry* searchedEntry = entryView->entryFromIndex(searchedItem);
QTRY_COMPARE(searchedEntry->password(), clipboard->text());
// Refocus back to search edit // Refocus back to search edit
QTest::mouseClick(searchTextEdit, Qt::LeftButton); QTest::mouseClick(searchTextEdit, Qt::LeftButton);
QTRY_VERIFY(searchTextEdit->hasFocus()); QTRY_VERIFY(searchTextEdit->hasFocus());