TestGuiBrowser.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
  3. * Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 or (at your option)
  8. * version 3 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "TestGuiBrowser.h"
  19. #include "gui/Application.h"
  20. #include <QJsonArray>
  21. #include <QJsonObject>
  22. #include <QLineEdit>
  23. #include <QListView>
  24. #include <QPlainTextEdit>
  25. #include <QPushButton>
  26. #include <QTableView>
  27. #include <QTest>
  28. #include <QToolBar>
  29. #include "browser/BrowserService.h"
  30. #include "config-keepassx-tests.h"
  31. #include "crypto/Crypto.h"
  32. #include "gui/DatabaseTabWidget.h"
  33. #include "gui/FileDialog.h"
  34. #include "gui/MessageBox.h"
  35. #include "gui/PasswordWidget.h"
  36. #include "gui/entry/EditEntryWidget.h"
  37. #include "gui/entry/EntryView.h"
  38. int main(int argc, char* argv[])
  39. {
  40. #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
  41. QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  42. QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
  43. #endif
  44. Application app(argc, argv);
  45. app.setApplicationName("KeePassXC");
  46. app.setApplicationVersion(KEEPASSXC_VERSION);
  47. app.setQuitOnLastWindowClosed(false);
  48. app.setAttribute(Qt::AA_Use96Dpi, true);
  49. app.applyTheme();
  50. QTEST_DISABLE_KEYPAD_NAVIGATION
  51. TestGuiBrowser tc;
  52. QTEST_SET_MAIN_SOURCE_PATH
  53. return QTest::qExec(&tc, argc, argv);
  54. }
  55. void TestGuiBrowser::initTestCase()
  56. {
  57. QVERIFY(Crypto::init());
  58. Config::createTempFileInstance();
  59. // Disable autosave so we can test the modified file indicator
  60. config()->set(Config::AutoSaveAfterEveryChange, false);
  61. config()->set(Config::AutoSaveOnExit, false);
  62. // Enable the tray icon so we can test hiding/restoring the windowQByteArray
  63. config()->set(Config::GUI_ShowTrayIcon, true);
  64. // Disable the update check first time alert
  65. config()->set(Config::UpdateCheckMessageShown, true);
  66. // Disable quick unlock
  67. config()->set(Config::Security_QuickUnlock, false);
  68. m_mainWindow.reset(new MainWindow());
  69. m_tabWidget = m_mainWindow->findChild<DatabaseTabWidget*>("tabWidget");
  70. m_mainWindow->show();
  71. }
  72. // Every test starts with opening the temp database
  73. void TestGuiBrowser::init()
  74. {
  75. m_dbFile.reset(new TemporaryFile());
  76. m_dbFile->copyFromFile(QString(KEEPASSX_TEST_DATA_DIR).append("/NewDatabaseBrowser.kdbx"));
  77. // make sure window is activated or focus tests may fail
  78. m_mainWindow->activateWindow();
  79. QApplication::processEvents();
  80. fileDialog()->setNextFileName(m_dbFile->fileName());
  81. triggerAction("actionDatabaseOpen");
  82. auto* databaseOpenWidget = m_tabWidget->currentDatabaseWidget()->findChild<QWidget*>("databaseOpenWidget");
  83. QVERIFY(databaseOpenWidget);
  84. auto* editPassword =
  85. databaseOpenWidget->findChild<PasswordWidget*>("editPassword")->findChild<QLineEdit*>("passwordEdit");
  86. QVERIFY(editPassword);
  87. editPassword->setFocus();
  88. QTest::keyClicks(editPassword, "a");
  89. QTest::keyClick(editPassword, Qt::Key_Enter);
  90. m_dbWidget = m_tabWidget->currentDatabaseWidget();
  91. m_db = m_dbWidget->database();
  92. }
  93. // Every test ends with closing the temp database without saving
  94. void TestGuiBrowser::cleanup()
  95. {
  96. // DO NOT save the database
  97. m_db->markAsClean();
  98. MessageBox::setNextAnswer(MessageBox::No);
  99. triggerAction("actionDatabaseClose");
  100. QApplication::processEvents();
  101. MessageBox::setNextAnswer(MessageBox::NoButton);
  102. if (m_dbWidget) {
  103. delete m_dbWidget;
  104. }
  105. m_dbFile->remove();
  106. }
  107. void TestGuiBrowser::cleanupTestCase()
  108. {
  109. m_dbFile->remove();
  110. }
  111. void TestGuiBrowser::testEntrySettings()
  112. {
  113. // Enable the Browser Integration
  114. config()->set(Config::Browser_Enabled, true);
  115. auto* toolBar = m_mainWindow->findChild<QToolBar*>("toolBar");
  116. auto* entryView = m_dbWidget->findChild<EntryView*>("entryView");
  117. entryView->setFocus();
  118. QVERIFY(entryView->hasFocus());
  119. // Select the first entry in the database
  120. QModelIndex entryItem = entryView->model()->index(0, 1);
  121. Entry* entry = entryView->entryFromIndex(entryItem);
  122. clickIndex(entryItem, entryView, Qt::LeftButton);
  123. auto* entryEditAction = m_mainWindow->findChild<QAction*>("actionEntryEdit");
  124. QWidget* entryEditWidget = toolBar->widgetForAction(entryEditAction);
  125. QTest::mouseClick(entryEditWidget, Qt::LeftButton);
  126. QCOMPARE(m_dbWidget->currentMode(), DatabaseWidget::Mode::EditEntryMode);
  127. auto* editEntryWidget = m_dbWidget->findChild<EditEntryWidget*>("editEntryWidget");
  128. // Switch to Properties page and select all rows from the custom data table
  129. editEntryWidget->setCurrentPage(5);
  130. auto customDataTableView = editEntryWidget->findChild<QTableView*>("customDataTable");
  131. QVERIFY(customDataTableView);
  132. QTest::mouseClick(customDataTableView, Qt::LeftButton);
  133. QTest::keyClick(customDataTableView, 'a', Qt::ControlModifier);
  134. // Remove the data
  135. QCOMPARE(entry->customData()->size(), 2);
  136. auto* removeButton = editEntryWidget->findChild<QPushButton*>("removeCustomDataButton");
  137. QVERIFY(removeButton);
  138. MessageBox::setNextAnswer(MessageBox::Delete);
  139. QTest::mouseClick(removeButton, Qt::LeftButton);
  140. // Apply the removal
  141. auto* editEntryWidgetButtonBox = editEntryWidget->findChild<QDialogButtonBox*>("buttonBox");
  142. QVERIFY(editEntryWidgetButtonBox);
  143. auto* okButton = editEntryWidgetButtonBox->button(QDialogButtonBox::Ok);
  144. QVERIFY(okButton);
  145. QTRY_VERIFY(okButton->isEnabled());
  146. QTest::mouseClick(okButton, Qt::LeftButton);
  147. QApplication::processEvents();
  148. QCOMPARE(entry->customData()->size(), 0);
  149. }
  150. void TestGuiBrowser::testAdditionalURLs()
  151. {
  152. auto* toolBar = m_mainWindow->findChild<QToolBar*>("toolBar");
  153. auto* entryView = m_dbWidget->findChild<EntryView*>("entryView");
  154. entryView->setFocus();
  155. QVERIFY(entryView->hasFocus());
  156. // Select the first entry in the database
  157. QModelIndex entryItem = entryView->model()->index(0, 1);
  158. clickIndex(entryItem, entryView, Qt::LeftButton);
  159. auto* entryEditAction = m_mainWindow->findChild<QAction*>("actionEntryEdit");
  160. QWidget* entryEditWidget = toolBar->widgetForAction(entryEditAction);
  161. QTest::mouseClick(entryEditWidget, Qt::LeftButton);
  162. QCOMPARE(m_dbWidget->currentMode(), DatabaseWidget::Mode::EditEntryMode);
  163. auto* editEntryWidget = m_dbWidget->findChild<EditEntryWidget*>("editEntryWidget");
  164. // Switch to Browser Integration page and add three URL's
  165. editEntryWidget->setCurrentPage(4);
  166. auto* addURLButton = editEntryWidget->findChild<QPushButton*>("addURLButton");
  167. QVERIFY(addURLButton);
  168. auto* urlList = editEntryWidget->findChild<QListView*>("additionalURLsView");
  169. QVERIFY(urlList);
  170. QStringList testURLs = {"https://example1.com", "https://example2.com", "https://example3.com"};
  171. for (const auto& url : testURLs) {
  172. QTest::mouseClick(addURLButton, Qt::LeftButton);
  173. QApplication::processEvents();
  174. QTest::keyClicks(urlList->focusWidget(), url);
  175. QTest::keyClick(urlList->focusWidget(), Qt::Key_Enter);
  176. }
  177. // Check the values from attributesEdit
  178. editEntryWidget->setCurrentPage(1);
  179. auto* attributesView = editEntryWidget->findChild<QListView*>("attributesView");
  180. auto* attrTextEdit = editEntryWidget->findChild<QPlainTextEdit*>("attributesEdit");
  181. // Go top of the list
  182. attributesView->setFocus();
  183. QTest::keyClick(attributesView->focusWidget(), Qt::Key_PageUp);
  184. for (const auto& url : testURLs) {
  185. QCOMPARE(attrTextEdit->toPlainText(), url);
  186. QTest::keyClick(attributesView->focusWidget(), Qt::Key_Down);
  187. }
  188. }
  189. void TestGuiBrowser::testGetDatabaseGroups()
  190. {
  191. auto result = browserService()->getDatabaseGroups();
  192. QCOMPARE(result.length(), 1);
  193. auto groups = result["groups"].toArray();
  194. auto first = groups.at(0);
  195. auto children = first.toObject()["children"].toArray();
  196. QCOMPARE(first.toObject()["name"].toString(), QString("NewDatabase"));
  197. QCOMPARE(children.size(), 6);
  198. auto firstChild = children.at(0).toObject();
  199. auto secondChild = children.at(1).toObject();
  200. QCOMPARE(firstChild["name"].toString(), QString("General"));
  201. QCOMPARE(secondChild["name"].toString(), QString("Windows"));
  202. auto subGroups = firstChild["children"].toArray();
  203. QCOMPARE(subGroups.count(), 1);
  204. auto subGroupObj = subGroups.at(0).toObject();
  205. QCOMPARE(subGroupObj["name"].toString(), QString("SubGroup"));
  206. }
  207. void TestGuiBrowser::triggerAction(const QString& name)
  208. {
  209. auto* action = m_mainWindow->findChild<QAction*>(name);
  210. QVERIFY(action);
  211. QVERIFY(action->isEnabled());
  212. action->trigger();
  213. QApplication::processEvents();
  214. }
  215. void TestGuiBrowser::clickIndex(const QModelIndex& index,
  216. QAbstractItemView* view,
  217. Qt::MouseButton button,
  218. Qt::KeyboardModifiers stateKey)
  219. {
  220. QTest::mouseClick(view->viewport(), button, stateKey, view->visualRect(index).center());
  221. }