bitcoin/src/qt/test/wallettests.cpp

108 lines
4.0 KiB
C++
Raw Normal View History

#include "wallettests.h"
#include "qt/bitcoinamountfield.h"
#include "qt/optionsmodel.h"
#include "qt/platformstyle.h"
#include "qt/qvalidatedlineedit.h"
#include "qt/sendcoinsdialog.h"
#include "qt/sendcoinsentry.h"
#include "qt/transactiontablemodel.h"
#include "qt/walletmodel.h"
#include "test/test_bitcoin.h"
#include "validation.h"
#include "wallet/wallet.h"
#include <QAbstractButton>
#include <QApplication>
#include <QTimer>
#include <QVBoxLayout>
namespace
{
//! Press "Yes" button in modal send confirmation dialog.
void ConfirmSend()
{
Avoid QTimer::singleShot compile error with Qt 5.3.2 Construct QTimer object directly, instead of relying on QTimer::singleShot overloads accepting lambdas, which weren't introduced until Qt 5.4. Avoids the following compile error in debian jessie: ``` qt/test/wallettests.cpp: In function ‘void {anonymous}::ConfirmSend()’: qt/test/wallettests.cpp:34:6: error: no matching function for call to ‘QTimer::singleShot(int, Qt::TimerType, {anonymous}::ConfirmSend()::<lambda()>)’ }); ^ qt/test/wallettests.cpp:34:6: note: candidates are: In file included from /usr/include/x86_64-linux-gnu/qt5/QtCore/QTimer:1:0, from ./qt/sendcoinsdialog.h:13, from qt/test/wallettests.cpp:7: /usr/include/x86_64-linux-gnu/qt5/QtCore/qtimer.h:81:17: note: static void QTimer::singleShot(int, const QObject*, const char*) static void singleShot(int msec, const QObject *receiver, const char *member); ^ /usr/include/x86_64-linux-gnu/qt5/QtCore/qtimer.h:81:17: note: no known conversion for argument 2 from ‘Qt::TimerType’ to ‘const QObject*’ /usr/include/x86_64-linux-gnu/qt5/QtCore/qtimer.h:82:17: note: static void QTimer::singleShot(int, Qt::TimerType, const QObject*, const char*) static void singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member); ^ /usr/include/x86_64-linux-gnu/qt5/QtCore/qtimer.h:82:17: note: candidate expects 4 arguments, 3 provided ``` Error reported by Pavel Janík <Pavel@Janik.cz> in https://github.com/bitcoin/bitcoin/pull/9974#issuecomment-287574436
2017-03-20 09:35:37 -07:00
QTimer* timer = new QTimer;
timer->setSingleShot(true);
QObject::connect(timer, &QTimer::timeout, []() {
for (QWidget* widget : QApplication::topLevelWidgets()) {
if (widget->inherits("SendConfirmationDialog")) {
SendConfirmationDialog* dialog = qobject_cast<SendConfirmationDialog*>(widget);
QAbstractButton* button = dialog->button(QMessageBox::Yes);
button->setEnabled(true);
button->click();
}
}
});
Avoid QTimer::singleShot compile error with Qt 5.3.2 Construct QTimer object directly, instead of relying on QTimer::singleShot overloads accepting lambdas, which weren't introduced until Qt 5.4. Avoids the following compile error in debian jessie: ``` qt/test/wallettests.cpp: In function ‘void {anonymous}::ConfirmSend()’: qt/test/wallettests.cpp:34:6: error: no matching function for call to ‘QTimer::singleShot(int, Qt::TimerType, {anonymous}::ConfirmSend()::<lambda()>)’ }); ^ qt/test/wallettests.cpp:34:6: note: candidates are: In file included from /usr/include/x86_64-linux-gnu/qt5/QtCore/QTimer:1:0, from ./qt/sendcoinsdialog.h:13, from qt/test/wallettests.cpp:7: /usr/include/x86_64-linux-gnu/qt5/QtCore/qtimer.h:81:17: note: static void QTimer::singleShot(int, const QObject*, const char*) static void singleShot(int msec, const QObject *receiver, const char *member); ^ /usr/include/x86_64-linux-gnu/qt5/QtCore/qtimer.h:81:17: note: no known conversion for argument 2 from ‘Qt::TimerType’ to ‘const QObject*’ /usr/include/x86_64-linux-gnu/qt5/QtCore/qtimer.h:82:17: note: static void QTimer::singleShot(int, Qt::TimerType, const QObject*, const char*) static void singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member); ^ /usr/include/x86_64-linux-gnu/qt5/QtCore/qtimer.h:82:17: note: candidate expects 4 arguments, 3 provided ``` Error reported by Pavel Janík <Pavel@Janik.cz> in https://github.com/bitcoin/bitcoin/pull/9974#issuecomment-287574436
2017-03-20 09:35:37 -07:00
timer->start(0);
}
//! Send coins to address and return txid.
uint256 SendCoins(CWallet& wallet, SendCoinsDialog& sendCoinsDialog, const CBitcoinAddress& address, CAmount amount)
{
QVBoxLayout* entries = sendCoinsDialog.findChild<QVBoxLayout*>("entries");
SendCoinsEntry* entry = qobject_cast<SendCoinsEntry*>(entries->itemAt(0)->widget());
entry->findChild<QValidatedLineEdit*>("payTo")->setText(QString::fromStdString(address.ToString()));
entry->findChild<BitcoinAmountField*>("payAmount")->setValue(amount);
uint256 txid;
boost::signals2::scoped_connection c(wallet.NotifyTransactionChanged.connect([&txid](CWallet*, const uint256& hash, ChangeType status) {
if (status == CT_NEW) txid = hash;
}));
ConfirmSend();
QMetaObject::invokeMethod(&sendCoinsDialog, "on_sendButton_clicked");
return txid;
}
//! Find index of txid in transaction list.
QModelIndex FindTx(const QAbstractItemModel& model, const uint256& txid)
{
QString hash = QString::fromStdString(txid.ToString());
int rows = model.rowCount({});
for (int row = 0; row < rows; ++row) {
QModelIndex index = model.index(row, 0, {});
if (model.data(index, TransactionTableModel::TxHashRole) == hash) {
return index;
}
}
return {};
}
}
//! Simple qt wallet tests.
void WalletTests::walletTests()
{
// Set up wallet and chain with 101 blocks (1 mature block for spending).
TestChain100Setup test;
test.CreateAndProcessBlock({}, GetScriptForRawPubKey(test.coinbaseKey.GetPubKey()));
bitdb.MakeMock();
CWallet wallet("wallet_test.dat");
bool firstRun;
wallet.LoadWallet(firstRun);
{
LOCK(wallet.cs_wallet);
wallet.SetAddressBook(test.coinbaseKey.GetPubKey().GetID(), "", "receive");
wallet.AddKeyPubKey(test.coinbaseKey, test.coinbaseKey.GetPubKey());
}
wallet.ScanForWalletTransactions(chainActive.Genesis(), true);
wallet.SetBroadcastTransactions(true);
// Create widgets for sending coins and listing transactions.
std::unique_ptr<const PlatformStyle> platformStyle(PlatformStyle::instantiate("other"));
SendCoinsDialog sendCoinsDialog(platformStyle.get());
OptionsModel optionsModel;
WalletModel walletModel(platformStyle.get(), &wallet, &optionsModel);
sendCoinsDialog.setModel(&walletModel);
// Send two transactions, and verify they are added to transaction list.
TransactionTableModel* transactionTableModel = walletModel.getTransactionTableModel();
QCOMPARE(transactionTableModel->rowCount({}), 101);
uint256 txid1 = SendCoins(wallet, sendCoinsDialog, CBitcoinAddress(CKeyID()), 5 * COIN);
uint256 txid2 = SendCoins(wallet, sendCoinsDialog, CBitcoinAddress(CKeyID()), 10 * COIN);
QCOMPARE(transactionTableModel->rowCount({}), 103);
QVERIFY(FindTx(*transactionTableModel, txid1).isValid());
QVERIFY(FindTx(*transactionTableModel, txid2).isValid());
bitdb.Flush(true);
bitdb.Reset();
}