Skip to content

Commit 5109fc4

Browse files
committed
[tests] [qt] Add tests for address book manipulation via EditAddressDialog
Also modifies corresponding QT code to allow for use within test cases.
1 parent 9c01be1 commit 5109fc4

File tree

7 files changed

+170
-2
lines changed

7 files changed

+170
-2
lines changed

src/Makefile.qttest.include

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ TEST_QT_MOC_CPP = \
1212

1313
if ENABLE_WALLET
1414
TEST_QT_MOC_CPP += \
15+
qt/test/moc_addressbooktests.cpp \
1516
qt/test/moc_paymentservertests.cpp \
1617
qt/test/moc_wallettests.cpp
1718
endif
1819

1920
TEST_QT_H = \
21+
qt/test/addressbooktests.h \
2022
qt/test/compattests.h \
2123
qt/test/rpcnestedtests.h \
2224
qt/test/uritests.h \
@@ -45,6 +47,7 @@ qt_test_test_bitcoin_qt_SOURCES = \
4547
$(TEST_BITCOIN_H)
4648
if ENABLE_WALLET
4749
qt_test_test_bitcoin_qt_SOURCES += \
50+
qt/test/addressbooktests.cpp \
4851
qt/test/paymentservertests.cpp \
4952
qt/test/wallettests.cpp \
5053
wallet/test/wallet_test_fixture.cpp

src/qt/addressbookpage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class AddressBookPage : public QDialog
3838
ForEditing /**< Open address book for editing */
3939
};
4040

41-
explicit AddressBookPage(const PlatformStyle *platformStyle, Mode mode, Tabs tab, QWidget *parent);
41+
explicit AddressBookPage(const PlatformStyle *platformStyle, Mode mode, Tabs tab, QWidget *parent = 0);
4242
~AddressBookPage();
4343

4444
void setModel(AddressTableModel *model);

src/qt/editaddressdialog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class EditAddressDialog : public QDialog
3030
EditSendingAddress
3131
};
3232

33-
explicit EditAddressDialog(Mode mode, QWidget *parent);
33+
explicit EditAddressDialog(Mode mode, QWidget *parent = 0);
3434
~EditAddressDialog();
3535

3636
void setModel(AddressTableModel *model);

src/qt/test/addressbooktests.cpp

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
}

src/qt/test/addressbooktests.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef BITCOIN_QT_TEST_ADDRESSBOOKTESTS_H
2+
#define BITCOIN_QT_TEST_ADDRESSBOOKTESTS_H
3+
4+
#include <QObject>
5+
#include <QTest>
6+
7+
class AddressBookTests : public QObject
8+
{
9+
Q_OBJECT
10+
11+
private Q_SLOTS:
12+
void addressBookTests();
13+
};
14+
15+
#endif // BITCOIN_QT_TEST_ADDRESSBOOKTESTS_H

src/qt/test/test_main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <qt/test/compattests.h>
1414

1515
#ifdef ENABLE_WALLET
16+
#include <qt/test/addressbooktests.h>
1617
#include <qt/test/paymentservertests.h>
1718
#include <qt/test/wallettests.h>
1819
#endif
@@ -99,6 +100,10 @@ int main(int argc, char *argv[])
99100
if (QTest::qExec(&test5) != 0) {
100101
fInvalid = true;
101102
}
103+
AddressBookTests test6;
104+
if (QTest::qExec(&test6) != 0) {
105+
fInvalid = true;
106+
}
102107
#endif
103108

104109
fs::remove_all(pathTemp);

src/qt/walletmodel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ class WalletModel : public QObject
204204
QString getWalletName() const;
205205

206206
bool isMultiwallet();
207+
208+
AddressTableModel* getAddressTableModel() const { return addressTableModel; }
207209
private:
208210
std::unique_ptr<interfaces::Wallet> m_wallet;
209211
std::unique_ptr<interfaces::Handler> m_handler_status_changed;

0 commit comments

Comments
 (0)