|
| 1 | +// Copyright (c) 2026 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <qml/models/walletlistmodel.h> |
| 6 | + |
| 7 | +#include <QtTest/QtTest> |
| 8 | + |
| 9 | +#include <memory> |
| 10 | + |
| 11 | +class FakeWalletListDataSource final : public WalletListModel::DataSource |
| 12 | +{ |
| 13 | +public: |
| 14 | + WalletListModel::WalletDirEntries m_entries{}; |
| 15 | + |
| 16 | + WalletListModel::WalletDirEntries listWalletDir() override |
| 17 | + { |
| 18 | + return m_entries; |
| 19 | + } |
| 20 | +}; |
| 21 | + |
| 22 | +class WalletListModelTests : public QObject |
| 23 | +{ |
| 24 | + Q_OBJECT |
| 25 | + |
| 26 | +private Q_SLOTS: |
| 27 | + void role_names_expose_name_key(); |
| 28 | + void list_wallet_dir_adds_entries_and_filters_duplicates(); |
| 29 | + void subsequent_list_wallet_dir_only_appends_new_wallets(); |
| 30 | + void invalid_index_returns_empty_variant(); |
| 31 | +}; |
| 32 | + |
| 33 | +void WalletListModelTests::role_names_expose_name_key() |
| 34 | +{ |
| 35 | + auto source = std::make_unique<FakeWalletListDataSource>(); |
| 36 | + WalletListModel model(std::move(source)); |
| 37 | + |
| 38 | + const QHash<int, QByteArray> roles = model.roleNames(); |
| 39 | + QVERIFY(roles.contains(WalletListModel::NameRole)); |
| 40 | + QCOMPARE(roles.value(WalletListModel::NameRole), QByteArrayLiteral("name")); |
| 41 | +} |
| 42 | + |
| 43 | +void WalletListModelTests::list_wallet_dir_adds_entries_and_filters_duplicates() |
| 44 | +{ |
| 45 | + auto source = std::make_unique<FakeWalletListDataSource>(); |
| 46 | + source->m_entries = { |
| 47 | + {"wallet-alpha", "sqlite"}, |
| 48 | + {"wallet-beta", "sqlite"}, |
| 49 | + {"wallet-alpha", "sqlite"}, |
| 50 | + }; |
| 51 | + |
| 52 | + WalletListModel model(std::move(source)); |
| 53 | + model.listWalletDir(); |
| 54 | + |
| 55 | + QCOMPARE(model.rowCount(), 2); |
| 56 | + QCOMPARE(model.data(model.index(0, 0), WalletListModel::NameRole).toString(), QStringLiteral("wallet-alpha")); |
| 57 | + QCOMPARE(model.data(model.index(1, 0), WalletListModel::NameRole).toString(), QStringLiteral("wallet-beta")); |
| 58 | +} |
| 59 | + |
| 60 | +void WalletListModelTests::subsequent_list_wallet_dir_only_appends_new_wallets() |
| 61 | +{ |
| 62 | + auto source = std::make_unique<FakeWalletListDataSource>(); |
| 63 | + auto* source_ptr = source.get(); |
| 64 | + source_ptr->m_entries = { |
| 65 | + {"wallet-alpha", "sqlite"}, |
| 66 | + {"wallet-beta", "sqlite"}, |
| 67 | + }; |
| 68 | + |
| 69 | + WalletListModel model(std::move(source)); |
| 70 | + model.listWalletDir(); |
| 71 | + |
| 72 | + source_ptr->m_entries = { |
| 73 | + {"wallet-beta", "sqlite"}, |
| 74 | + {"wallet-gamma", "sqlite"}, |
| 75 | + }; |
| 76 | + model.listWalletDir(); |
| 77 | + |
| 78 | + QCOMPARE(model.rowCount(), 3); |
| 79 | + QCOMPARE(model.data(model.index(2, 0), WalletListModel::NameRole).toString(), QStringLiteral("wallet-gamma")); |
| 80 | +} |
| 81 | + |
| 82 | +void WalletListModelTests::invalid_index_returns_empty_variant() |
| 83 | +{ |
| 84 | + auto source = std::make_unique<FakeWalletListDataSource>(); |
| 85 | + source->m_entries = {{"wallet-alpha", "sqlite"}}; |
| 86 | + |
| 87 | + WalletListModel model(std::move(source)); |
| 88 | + model.listWalletDir(); |
| 89 | + |
| 90 | + QVERIFY(!model.data(QModelIndex{}, WalletListModel::NameRole).isValid()); |
| 91 | + QVERIFY(!model.data(model.index(5, 0), WalletListModel::NameRole).isValid()); |
| 92 | +} |
| 93 | + |
| 94 | +int RunWalletListModelTests(int argc, char* argv[]) |
| 95 | +{ |
| 96 | + WalletListModelTests tests; |
| 97 | + return QTest::qExec(&tests, argc, argv); |
| 98 | +} |
| 99 | + |
| 100 | +#ifndef BITCOINQML_NO_TEST_MAIN |
| 101 | +QTEST_MAIN(WalletListModelTests) |
| 102 | +#endif |
| 103 | +#include "test_walletlistmodel.moc" |
0 commit comments