Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
supermassive committed May 31, 2022
1 parent dec5af0 commit 3da24ab
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
47 changes: 47 additions & 0 deletions browser/ui/webui/settings/brave_wallet_handler_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "brave/components/brave_wallet/browser/json_rpc_service.h"
#include "brave/components/brave_wallet/browser/pref_names.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
#include "brave/components/brave_wallet/common/features.h"
#include "brave/components/brave_wallet/common/value_conversion_utils.h"
#include "brave/grit/brave_generated_resources.h"
#include "chrome/test/base/testing_profile.h"
Expand Down Expand Up @@ -323,6 +324,52 @@ TEST(TestBraveWalletHandler, GetNetworkList) {
chain2);
}

// TODO(apaymyshev): temporarily copypasted from test above.
TEST(TestBraveWalletHandler, GetNetworkListWithAllKnown) {
base::test::ScopedFeatureList scoped_feature{
brave_wallet::features::kBraveWalletEditKnownNetworksFeature};
TestBraveWalletHandler handler;
std::vector<base::Value> values;
brave_wallet::mojom::NetworkInfo chain1(
"chain_id", "chain_name", {"https://url1.com"}, {"https://url1.com"},
{"https://url1.com"}, "symbol_name", "symbol", 11,
brave_wallet::mojom::CoinType::ETH,
brave_wallet::mojom::NetworkInfoData::NewEthData(
brave_wallet::mojom::NetworkInfoDataETH::New(false)));
values.push_back(brave_wallet::EthNetworkInfoToValue(chain1));

brave_wallet::mojom::NetworkInfo chain2(
"chain_id2", "chain_name2", {"https://url2.com"}, {"https://url2.com"},
{"https://url2.com"}, "symbol_name2", "symbol2", 22,
brave_wallet::mojom::CoinType::ETH,
brave_wallet::mojom::NetworkInfoData::NewEthData(
brave_wallet::mojom::NetworkInfoDataETH::New(true)));
values.push_back(brave_wallet::EthNetworkInfoToValue(chain2));
UpdateCustomNetworks(handler.prefs(), &values);
EXPECT_EQ(handler.GetAllEthCustomChains().size(), 2u);

auto args = base::ListValue();
args.Append(base::Value("id"));
handler.GetCustomNetworksList(args.GetList());
const auto& data = *handler.web_ui()->call_data()[0];
ASSERT_TRUE(data.arg1()->is_string());
EXPECT_EQ(data.arg1()->GetString(), "id");
ASSERT_TRUE(data.arg3()->is_list());
size_t index = 0u;
for (auto& known_chain :
brave_wallet::GetAllKnownEthChains(handler.prefs())) {
EXPECT_EQ(
*brave_wallet::ValueToEthNetworkInfo(data.arg3()->GetList()[index++]),
*known_chain);
}
EXPECT_EQ(
*brave_wallet::ValueToEthNetworkInfo(data.arg3()->GetList()[index++]),
chain1);
EXPECT_EQ(
*brave_wallet::ValueToEthNetworkInfo(data.arg3()->GetList()[index++]),
chain2);
}

TEST(TestBraveWalletHandler, SetActiveNetwork) {
TestBraveWalletHandler handler;

Expand Down
2 changes: 2 additions & 0 deletions components/brave_wallet/browser/brave_wallet_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ GURL AddInfuraProjectId(const GURL& url);
GURL MaybeAddInfuraProjectId(const GURL& url);
mojom::NetworkInfoPtr GetKnownEthChain(PrefService* prefs,
const std::string& chain_id);
mojom::NetworkInfoPtr GetCustomEthChain(PrefService* prefs,
const std::string& chain_id);

std::string GetSolanaSubdomainForKnownChainId(const std::string& chain_id);
std::string GetFilecoinSubdomainForKnownChainId(const std::string& chain_id);
Expand Down
81 changes: 81 additions & 0 deletions components/brave_wallet/browser/brave_wallet_utils_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,59 @@ TEST(BraveWalletUtilsUnitTest, GetAllEthCustomChainsTest) {
EXPECT_EQ(chain2, *GetAllEthCustomChains(&prefs)[1]);
}

TEST(BraveWalletUtilsUnitTest, KnownEthChainExists) {
TestingPrefServiceSimple prefs;
prefs.registry()->RegisterDictionaryPref(kBraveWalletCustomNetworks);
prefs.registry()->RegisterBooleanPref(kSupportEip1559OnLocalhostChain, false);

std::vector<base::Value> values;
mojom::NetworkInfo chain("chain_id", "chain_name", {"https://url1.com"},
{"https://url1.com"}, {"https://url1.com"},
"symbol_name", "symbol", 11, mojom::CoinType::ETH,
mojom::NetworkInfoData::NewEthData(
mojom::NetworkInfoDataETH::New(false)));
values.push_back(EthNetworkInfoToValue(chain));
UpdateCustomNetworks(&prefs, &values);

auto known_chains = GetAllKnownEthChains(&prefs);
EXPECT_EQ(known_chains.size(), 12u);
for (auto& known_chain : known_chains) {
EXPECT_TRUE(KnownEthChainExists(known_chain->chain_id));
}

EXPECT_TRUE(CustomEthChainExists(&prefs, chain.chain_id));
EXPECT_FALSE(KnownEthChainExists(chain.chain_id));
}

TEST(BraveWalletUtilsUnitTest, CustomEthChainExists) {
TestingPrefServiceSimple prefs;
prefs.registry()->RegisterDictionaryPref(kBraveWalletCustomNetworks);
prefs.registry()->RegisterBooleanPref(kSupportEip1559OnLocalhostChain, false);

std::vector<base::Value> values;
mojom::NetworkInfo chain1("chain_id", "chain_name", {"https://url1.com"},
{"https://url1.com"}, {"https://url1.com"},
"symbol_name", "symbol", 11, mojom::CoinType::ETH,
mojom::NetworkInfoData::NewEthData(
mojom::NetworkInfoDataETH::New(false)));
values.push_back(EthNetworkInfoToValue(chain1));

mojom::NetworkInfo chain2(
"chain_id2", "chain_name2", {"https://url2.com"}, {"https://url2.com"},
{"https://url2.com"}, "symbol_name2", "symbol2", 22, mojom::CoinType::ETH,
mojom::NetworkInfoData::NewEthData(mojom::NetworkInfoDataETH::New(true)));
values.push_back(EthNetworkInfoToValue(chain2));

EXPECT_FALSE(CustomEthChainExists(&prefs, chain1.chain_id));
EXPECT_FALSE(CustomEthChainExists(&prefs, chain2.chain_id));
EXPECT_EQ(GetAllEthCustomChains(&prefs).size(), 0u);
UpdateCustomNetworks(&prefs, &values);

EXPECT_TRUE(CustomEthChainExists(&prefs, chain1.chain_id));
EXPECT_TRUE(CustomEthChainExists(&prefs, chain2.chain_id));
EXPECT_EQ(GetAllEthCustomChains(&prefs).size(), 2u);
}

TEST(BraveWalletUtilsUnitTest, GetAllChainsTest) {
TestingPrefServiceSimple prefs;
prefs.registry()->RegisterDictionaryPref(kBraveWalletCustomNetworks);
Expand Down Expand Up @@ -830,6 +883,27 @@ TEST(BraveWalletUtilsUnitTest, GetKnownEthChain) {
EXPECT_EQ(network->data->get_eth_data()->is_eip1559, true);
}

TEST(BraveWalletUtilsUnitTest, GetCustomEthChain) {
TestingPrefServiceSimple prefs;
prefs.registry()->RegisterDictionaryPref(kBraveWalletCustomNetworks);
prefs.registry()->RegisterBooleanPref(kSupportEip1559OnLocalhostChain, false);

EXPECT_FALSE(GetCustomEthChain(&prefs, "0x5566"));

std::vector<base::Value> values;
mojom::NetworkInfo chain("0x5566", "chain_name", {"https://url1.com"},
{"https://url1.com"}, {"https://url1.com"},
"symbol_name", "symbol", 11, mojom::CoinType::ETH,
mojom::NetworkInfoData::NewEthData(
mojom::NetworkInfoDataETH::New(false)));
values.push_back(EthNetworkInfoToValue(chain));
UpdateCustomNetworks(&prefs, &values);

auto network = GetCustomEthChain(&prefs, chain.chain_id);
ASSERT_TRUE(network);
EXPECT_EQ(*network, chain);
}

TEST(BraveWalletUtilsUnitTest, GetChain) {
TestingPrefServiceSimple prefs;
prefs.registry()->RegisterDictionaryPref(kBraveWalletCustomNetworks);
Expand All @@ -842,6 +916,12 @@ TEST(BraveWalletUtilsUnitTest, GetChain) {
mojom::NetworkInfoData::NewEthData(
mojom::NetworkInfoDataETH::New(false)));
values.push_back(EthNetworkInfoToValue(chain1));
mojom::NetworkInfo chain2("0x89", "CustomPolygon", {"https://url1.com"},
{"https://url1.com"}, {"https://url1.com"},
"symbol_name", "symbol", 123, mojom::CoinType::ETH,
mojom::NetworkInfoData::NewEthData(
mojom::NetworkInfoDataETH::New(false)));
values.push_back(EthNetworkInfoToValue(chain2));
UpdateCustomNetworks(&prefs, &values);

EXPECT_FALSE(GetChain(&prefs, "0x123", mojom::CoinType::ETH));
Expand All @@ -851,6 +931,7 @@ TEST(BraveWalletUtilsUnitTest, GetChain) {
EXPECT_EQ(GetChain(&prefs, "0x539", mojom::CoinType::ETH),
GetKnownEthChain(&prefs, "0x539"));

EXPECT_EQ(*GetChain(&prefs, "0x89", mojom::CoinType::ETH), chain2);
// Solana

mojom::NetworkInfo sol_mainnet(
Expand Down

0 comments on commit 3da24ab

Please sign in to comment.