|
| 1 | +#include <qt/test/addressbooktests.h> |
| 2 | +#include <qt/test/util.h> |
| 3 | +#include <test/test_bitcoin.h> |
| 4 | + |
| 5 | +#include <interfaces/node.h> |
| 6 | +#include <qt/addressbookpage.h> |
| 7 | +#include <qt/addresstablemodel.h> |
| 8 | +#include <qt/editaddressdialog.h> |
| 9 | +#include <qt/callback.h> |
| 10 | +#include <qt/optionsmodel.h> |
| 11 | +#include <qt/platformstyle.h> |
| 12 | +#include <qt/qvalidatedlineedit.h> |
| 13 | +#include <qt/walletmodel.h> |
| 14 | + |
| 15 | +#include <key.h> |
| 16 | +#include <pubkey.h> |
| 17 | +#include <key_io.h> |
| 18 | +#include <wallet/wallet.h> |
| 19 | + |
| 20 | +#include <QTimer> |
| 21 | +#include <QMessageBox> |
| 22 | + |
| 23 | +namespace |
| 24 | +{ |
| 25 | + |
| 26 | +/** |
| 27 | + * Fill the edit address dialog box with data, submit it, and ensure that |
| 28 | + * the resulting message meets expectations. |
| 29 | + */ |
| 30 | +void EditAddressAndSubmit( |
| 31 | + EditAddressDialog* dialog, |
| 32 | + const QString& label, const QString& address, QString expected_msg) |
| 33 | +{ |
| 34 | + QString warning_text; |
| 35 | + |
| 36 | + dialog->findChild<QLineEdit*>("labelEdit")->setText(label); |
| 37 | + dialog->findChild<QValidatedLineEdit*>("addressEdit")->setText(address); |
| 38 | + |
| 39 | + ConfirmMessage(&warning_text, 5); |
| 40 | + dialog->accept(); |
| 41 | + QCOMPARE(warning_text, expected_msg); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Test adding various send addresses to the address book. |
| 46 | + * |
| 47 | + * There are three cases tested: |
| 48 | + * |
| 49 | + * - new_address: a new address which should add as a send address successfully. |
| 50 | + * - existing_s_address: an existing sending address which won't add successfully. |
| 51 | + * - existing_r_address: an existing receiving address which won't add successfully. |
| 52 | + * |
| 53 | + * In each case, verify the resulting state of the address book and optionally |
| 54 | + * the warning message presented to the user. |
| 55 | + */ |
| 56 | +void TestAddAddressesToSendBook() |
| 57 | +{ |
| 58 | + TestChain100Setup test; |
| 59 | + CWallet wallet("mock", WalletDatabase::CreateMock()); |
| 60 | + bool firstRun; |
| 61 | + wallet.LoadWallet(firstRun); |
| 62 | + |
| 63 | + auto build_address = [&wallet]() { |
| 64 | + CKey key; |
| 65 | + key.MakeNewKey(true); |
| 66 | + CTxDestination dest(GetDestinationForKey( |
| 67 | + key.GetPubKey(), wallet.m_default_address_type)); |
| 68 | + |
| 69 | + return std::make_pair(dest, QString::fromStdString(EncodeDestination(dest))); |
| 70 | + }; |
| 71 | + |
| 72 | + CTxDestination r_key_dest, s_key_dest; |
| 73 | + |
| 74 | + // Add a preexisting "receive" entry in the address book. |
| 75 | + QString preexisting_r_address; |
| 76 | + QString r_label("already here (r)"); |
| 77 | + |
| 78 | + // Add a preexisting "send" entry in the address book. |
| 79 | + QString preexisting_s_address; |
| 80 | + QString s_label("already here (s)"); |
| 81 | + |
| 82 | + // Define a new address (which should add to the address book successfully). |
| 83 | + QString new_address; |
| 84 | + |
| 85 | + std::tie(r_key_dest, preexisting_r_address) = build_address(); |
| 86 | + std::tie(s_key_dest, preexisting_s_address) = build_address(); |
| 87 | + std::tie(std::ignore, new_address) = build_address(); |
| 88 | + |
| 89 | + { |
| 90 | + LOCK(wallet.cs_wallet); |
| 91 | + wallet.SetAddressBook(r_key_dest, r_label.toStdString(), "receive"); |
| 92 | + wallet.SetAddressBook(s_key_dest, s_label.toStdString(), "send"); |
| 93 | + } |
| 94 | + |
| 95 | + auto check_addbook_size = [&wallet](int expected_size) { |
| 96 | + QCOMPARE(static_cast<int>(wallet.mapAddressBook.size()), expected_size); |
| 97 | + }; |
| 98 | + |
| 99 | + // We should start with the two addresses we added earlier and nothing else. |
| 100 | + check_addbook_size(2); |
| 101 | + |
| 102 | + // Initialize relevant QT models. |
| 103 | + std::unique_ptr<const PlatformStyle> platformStyle(PlatformStyle::instantiate("other")); |
| 104 | + auto node = interfaces::MakeNode(); |
| 105 | + OptionsModel optionsModel(*node); |
| 106 | + AddWallet(&wallet); |
| 107 | + WalletModel walletModel(std::move(node->getWallets()[0]), *node, platformStyle.get(), &optionsModel); |
| 108 | + RemoveWallet(&wallet); |
| 109 | + EditAddressDialog editAddressDialog(EditAddressDialog::NewSendingAddress); |
| 110 | + editAddressDialog.setModel(walletModel.getAddressTableModel()); |
| 111 | + |
| 112 | + EditAddressAndSubmit( |
| 113 | + &editAddressDialog, QString("uhoh"), preexisting_r_address, |
| 114 | + QString( |
| 115 | + "Address \"%1\" already exists as a receiving address with label " |
| 116 | + "\"%2\" and so cannot be added as a sending address." |
| 117 | + ).arg(preexisting_r_address).arg(r_label)); |
| 118 | + |
| 119 | + check_addbook_size(2); |
| 120 | + |
| 121 | + EditAddressAndSubmit( |
| 122 | + &editAddressDialog, QString("uhoh, different"), preexisting_s_address, |
| 123 | + QString( |
| 124 | + "The entered address \"%1\" is already in the address book with " |
| 125 | + "label \"%2\"." |
| 126 | + ).arg(preexisting_s_address).arg(s_label)); |
| 127 | + |
| 128 | + check_addbook_size(2); |
| 129 | + |
| 130 | + // Submit a new address which should add successfully - we expect the |
| 131 | + // warning message to be blank. |
| 132 | + EditAddressAndSubmit( |
| 133 | + &editAddressDialog, QString("new"), new_address, QString("")); |
| 134 | + |
| 135 | + check_addbook_size(3); |
| 136 | +} |
| 137 | + |
| 138 | +} // namespace |
| 139 | + |
| 140 | +void AddressBookTests::addressBookTests() |
| 141 | +{ |
| 142 | + TestAddAddressesToSendBook(); |
| 143 | +} |
0 commit comments