src/qt/test/addressbooktests.cpp
void TestAddAddressesToSendBook(interfaces::Node& node)
{
    TestChain100Setup test;
    std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), WalletLocation(), WalletDatabase::CreateMock());
    wallet->SetupLegacyScriptPubKeyMan();
    bool firstRun;
    wallet->LoadWallet(firstRun);

    auto build_address = [&wallet]() {
        CKey key;
        key.MakeNewKey(true);
        CTxDestination dest(GetDestinationForKey(
            key.GetPubKey(), wallet->m_default_address_type));

        return std::make_pair(dest, QString::fromStdString(EncodeDestination(dest)));
    };

    CTxDestination r_key_dest, s_key_dest;

    // Add a preexisting "receive" entry in the address book.
    QString preexisting_r_address;
    QString r_label("already here (r)");

    // Add a preexisting "send" entry in the address book.
    QString preexisting_s_address;
    QString s_label("already here (s)");

    // Define a new address (which should add to the address book successfully).
    QString new_address;

    std::tie(r_key_dest, preexisting_r_address) = build_address();
    std::tie(s_key_dest, preexisting_s_address) = build_address();
    std::tie(std::ignore, new_address) = build_address();

    {
        LOCK(wallet->cs_wallet);
        wallet->SetAddressBook(r_key_dest, r_label.toStdString(), "receive");
        wallet->SetAddressBook(s_key_dest, s_label.toStdString(), "send");
    }

    auto check_addbook_size = [&wallet](int expected_size) {
        LOCK(wallet->cs_wallet);
        QCOMPARE(static_cast<int>(wallet->m_address_book.size()), expected_size);
    };

    // We should start with the two addresses we added earlier and nothing else.
    check_addbook_size(2);

    // Initialize relevant QT models.
    std::unique_ptr<const PlatformStyle> platformStyle(PlatformStyle::instantiate("other"));
    OptionsModel optionsModel(node);
    AddWallet(wallet);
    WalletModel walletModel(interfaces::MakeWallet(wallet), node, platformStyle.get(), &optionsModel);
    RemoveWallet(wallet);
    EditAddressDialog editAddressDialog(EditAddressDialog::NewSendingAddress);
    editAddressDialog.setModel(walletModel.getAddressTableModel());

    EditAddressAndSubmit(
        &editAddressDialog, QString("uhoh"), preexisting_r_address,
        QString(
            "Address \"%1\" already exists as a receiving address with label "
            "\"%2\" and so cannot be added as a sending address."
            ).arg(preexisting_r_address).arg(r_label));

    check_addbook_size(2);

    EditAddressAndSubmit(
        &editAddressDialog, QString("uhoh, different"), preexisting_s_address,
        QString(
            "The entered address \"%1\" is already in the address book with "
            "label \"%2\"."
            ).arg(preexisting_s_address).arg(s_label));

    check_addbook_size(2);

    // Submit a new address which should add successfully - we expect the
    // warning message to be blank.
    EditAddressAndSubmit(
        &editAddressDialog, QString("new"), new_address, QString(""));

    check_addbook_size(3);
}
/**
 * Test adding various send addresses to the address book.
 *
 * There are three cases tested:
 *
 *   - new_address: a new address which should add as a send address successfully.
 *   - existing_s_address: an existing sending address which won't add successfully.
 *   - existing_r_address: an existing receiving address which won't add successfully.
 *
 * In each case, verify the resulting state of the address book and optionally
 * the warning message presented to the user.
 */
Test adding various send addresses to the address book.
There are three cases tested.

- new_address: a new address which should add as a send address successfully.
- existing_s_address: an existing sending address which won't add successfully.
- existing_r_address: an existing receiving address which won't add successfully.

In each case, verify the resulting state of the address book and optionally
the warning message presented to the user.
Test adding various send addresses to the address book . There are three cases tested . - new_address : a new address which should add as a send address successfully . - existing_s_address : an existing sending address which won ' t add successfully . - existing_r_address : an existing receiving address which won ' t add successfully . In each case verify the resulting state of the address book and optionally the warning message presented to the user .
void TestAddAddressesToSendBook(interfaces::Node& node)
{
    TestChain100Setup test;
    std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), WalletLocation(), WalletDatabase::CreateMock());
    wallet->SetupLegacyScriptPubKeyMan();
    bool firstRun;
    wallet->LoadWallet(firstRun);
    auto build_address = [&wallet]() {
        CKey key;
        key.MakeNewKey(true);
        CTxDestination dest(GetDestinationForKey(
            key.GetPubKey(), wallet->m_default_address_type));
        return std::make_pair(dest, QString::fromStdString(EncodeDestination(dest)));
    };
    CTxDestination r_key_dest, s_key_dest;
    QString preexisting_r_address;
    QString r_label("already here (r)");
    QString preexisting_s_address;
    QString s_label("already here (s)");
    QString new_address;
    std::tie(r_key_dest, preexisting_r_address) = build_address();
    std::tie(s_key_dest, preexisting_s_address) = build_address();
    std::tie(std::ignore, new_address) = build_address();
    {
        LOCK(wallet->cs_wallet);
        wallet->SetAddressBook(r_key_dest, r_label.toStdString(), "receive");
        wallet->SetAddressBook(s_key_dest, s_label.toStdString(), "send");
    }
    auto check_addbook_size = [&wallet](int expected_size) {
        LOCK(wallet->cs_wallet);
        QCOMPARE(static_cast<int>(wallet->m_address_book.size()), expected_size);
    };
    check_addbook_size(2);
    std::unique_ptr<const PlatformStyle> platformStyle(PlatformStyle::instantiate("other"));
    OptionsModel optionsModel(node);
    AddWallet(wallet);
    WalletModel walletModel(interfaces::MakeWallet(wallet), node, platformStyle.get(), &optionsModel);
    RemoveWallet(wallet);
    EditAddressDialog editAddressDialog(EditAddressDialog::NewSendingAddress);
    editAddressDialog.setModel(walletModel.getAddressTableModel());
    EditAddressAndSubmit(
        &editAddressDialog, QString("uhoh"), preexisting_r_address,
        QString(
            "Address \"%1\" already exists as a receiving address with label "
            "\"%2\" and so cannot be added as a sending address."
            ).arg(preexisting_r_address).arg(r_label));
    check_addbook_size(2);
    EditAddressAndSubmit(
        &editAddressDialog, QString("uhoh, different"), preexisting_s_address,
        QString(
            "The entered address \"%1\" is already in the address book with "
            "label \"%2\"."
            ).arg(preexisting_s_address).arg(s_label));
    check_addbook_size(2);
    EditAddressAndSubmit(
        &editAddressDialog, QString("new"), new_address, QString(""));
    check_addbook_size(3);
}
void TestAddAddressesToSendBook ( interfaces :: Node & node ) { TestChain100Setup test ; std :: shared_ptr < CWallet > wallet = std :: make_shared < CWallet > ( node . context ( ) -> chain . get ( ) , WalletLocation ( ) , WalletDatabase :: CreateMock ( ) ) ; wallet -> SetupLegacyScriptPubKeyMan ( ) ; bool firstRun ; wallet -> LoadWallet ( firstRun ) ; auto build_address = [ & wallet ] ( ) { CKey key ; key . MakeNewKey ( true ) ; CTxDestination dest ( GetDestinationForKey ( key . GetPubKey ( ) , wallet -> m_default_address_type ) ) ; return std :: make_pair ( dest , QString :: fromStdString ( EncodeDestination ( dest ) ) ) ; } ; CTxDestination r_key_dest , s_key_dest ; QString preexisting_r_address ; QString r_label ( " " ) ; QString preexisting_s_address ; QString s_label ( " " ) ; QString new_address ; std :: tie ( r_key_dest , preexisting_r_address ) = build_address ( ) ; std :: tie ( s_key_dest , preexisting_s_address ) = build_address ( ) ; std :: tie ( std :: ignore , new_address ) = build_address ( ) ; { LOCK ( wallet -> cs_wallet ) ; wallet -> SetAddressBook ( r_key_dest , r_label . toStdString ( ) , " " ) ; wallet -> SetAddressBook ( s_key_dest , s_label . toStdString ( ) , " " ) ; } auto check_addbook_size = [ & wallet ] ( int expected_size ) { LOCK ( wallet -> cs_wallet ) ; QCOMPARE ( static_cast < int > ( wallet -> m_address_book . size ( ) ) , expected_size ) ; } ; check_addbook_size ( 2 ) ; std :: unique_ptr < const PlatformStyle > platformStyle ( PlatformStyle :: instantiate ( " " ) ) ; OptionsModel optionsModel ( node ) ; AddWallet ( wallet ) ; WalletModel walletModel ( interfaces :: MakeWallet ( wallet ) , node , platformStyle . get ( ) , & optionsModel ) ; RemoveWallet ( wallet ) ; EditAddressDialog editAddressDialog ( EditAddressDialog :: NewSendingAddress ) ; editAddressDialog . setModel ( walletModel . getAddressTableModel ( ) ) ; EditAddressAndSubmit ( & editAddressDialog , QString ( " " ) , preexisting_r_address , QString ( " \" \" " " \" \" " ) . arg ( preexisting_r_address ) . arg ( r_label ) ) ; check_addbook_size ( 2 ) ; EditAddressAndSubmit ( & editAddressDialog , QString ( " " ) , preexisting_s_address , QString ( " \" \" " " \" \" " ) . arg ( preexisting_s_address ) . arg ( s_label ) ) ; check_addbook_size ( 2 ) ; EditAddressAndSubmit ( & editAddressDialog , QString ( " " ) , new_address , QString ( " " ) ) ; check_addbook_size ( 3 ) ; }
Test adding various send addresses to the address book.
Test adding various send addresses to the address book .
// Add a preexisting "receive" entry in the address book. // Add a preexisting "send" entry in the address book. // Define a new address (which should add to the address book successfully). // We should start with the two addresses we added earlier and nothing else. // Initialize relevant QT models. // Submit a new address which should add successfully - we expect the // warning message to be blank.