Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wallet): Added Show Test Networks Pref #13098

Merged
merged 1 commit into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,9 @@ Are you sure you want to do this?
<message name="IDS_SETTINGS_SHOW_BRAVE_WALLET_ICON_ON_TOOLBAR" desc="The description for showing the Brave wallet panel icon on the toolbar">
Show Brave Wallet icon on toolbar
</message>
<message name="IDS_SETTINGS_SHOW_BRAVE_WALLET_TEST_NETWORKS" desc="The description for showing the Brave wallet test networks">
Show test networks
</message>
<message name="IDS_SETTINGS_HIDE_BRAVE_WALLET_ICON_ON_TOOLBAR" desc="The description for hiding the Brave wallet panel icon on the toolbar">
Hide Brave Wallet icon on toolbar
</message>
Expand Down
2 changes: 2 additions & 0 deletions browser/brave_prefs_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ IN_PROC_BROWSER_TEST_F(BraveProfilePrefsBrowserTest, MiscBravePrefs) {
brave_wallet::mojom::DefaultWallet::BraveWalletPreferExtension);
EXPECT_TRUE(chrome_test_utils::GetProfile(this)->GetPrefs()->GetBoolean(
kShowWalletIconOnToolbar));
EXPECT_FALSE(chrome_test_utils::GetProfile(this)->GetPrefs()->GetBoolean(
kShowWalletTestNetworks));
EXPECT_FALSE(chrome_test_utils::GetProfile(this)->GetPrefs()->GetBoolean(
kBraveWalletBackupComplete));
EXPECT_FALSE(chrome_test_utils::GetProfile(this)->GetPrefs()->GetBoolean(
Expand Down
20 changes: 20 additions & 0 deletions browser/brave_wallet/brave_wallet_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,17 @@ class BraveWalletServiceUnitTest : public testing::Test {
return default_cryptocurrency;
}

bool GetShowWalletTestNetworks() {
base::RunLoop run_loop;
bool show_wallet_test_networks;
service_->GetShowWalletTestNetworks(base::BindLambdaForTesting([&](bool b) {
show_wallet_test_networks = b;
run_loop.Quit();
}));
run_loop.Run();
return show_wallet_test_networks;
}

void SimulateOnGetImportInfo(const std::string& new_password,
bool result,
const ImportInfo& info,
Expand Down Expand Up @@ -1081,6 +1092,15 @@ TEST_F(BraveWalletServiceUnitTest, GetAndSetDefaultBaseCryptocurrency) {
EXPECT_EQ(GetDefaultBaseCryptocurrency(), "ETH");
}

TEST_F(BraveWalletServiceUnitTest, GetShowWalletTestNetworks) {
// Default value for kShowWalletTestNetworks should be false
EXPECT_FALSE(GetPrefs()->GetBoolean(kShowWalletTestNetworks));
EXPECT_FALSE(GetShowWalletTestNetworks());

GetPrefs()->SetBoolean(kShowWalletTestNetworks, true);
EXPECT_TRUE(GetShowWalletTestNetworks());
}

TEST_F(BraveWalletServiceUnitTest, EthAddRemoveSetUserAssetVisible) {
mojom::BlockchainTokenPtr eth_0x4_token = GetEthToken();
eth_0x4_token->chain_id = "0x4";
Expand Down
2 changes: 2 additions & 0 deletions browser/extensions/api/settings_private/brave_prefs_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ const PrefsUtil::TypedPrefMap& BravePrefsUtil::GetAllowlistedKeys() {
settings_api::PrefType::PREF_TYPE_STRING;
(*s_brave_allowlist)[kShowWalletIconOnToolbar] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_allowlist)[kShowWalletTestNetworks] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_allowlist)[kBraveWalletAutoLockMinutes] =
settings_api::PrefType::PREF_TYPE_NUMBER;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
</template>
</iron-list>
</div>
<settings-toggle-button id="showBravewalletTestNetworks"
class="cr-row"
pref="{{prefs.brave.wallet.show_wallet_test_networks}}"
label="$i18n{showBravewalletTestNetworks}">
</settings-toggle-button>
</div>
</div>
<template is="dom-if" if="[[showAddWalletNetworkDialog_]]" restamp>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source,
IDS_SETTINGS_DEFAULT_BASE_CRYPTOCURRENCY_DESC},
{"showBravewalletIconOnToolbar",
IDS_SETTINGS_SHOW_BRAVE_WALLET_ICON_ON_TOOLBAR},
{"showBravewalletTestNetworks",
IDS_SETTINGS_SHOW_BRAVE_WALLET_TEST_NETWORKS},
{"autoLockMinutes", IDS_SETTINGS_AUTO_LOCK_MINUTES},
{"autoLockMinutesDesc", IDS_SETTINGS_AUTO_LOCK_MINUTES_DESC},
{"googleLoginForExtensionsDesc", IDS_SETTINGS_GOOGLE_LOGIN_FOR_EXTENSIONS},
Expand Down
1 change: 1 addition & 0 deletions components/brave_wallet/browser/brave_wallet_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterStringPref(kDefaultBaseCurrency, "USD");
registry->RegisterStringPref(kDefaultBaseCryptocurrency, "BTC");
registry->RegisterBooleanPref(kShowWalletIconOnToolbar, true);
registry->RegisterBooleanPref(kShowWalletTestNetworks, false);
registry->RegisterDictionaryPref(kBraveWalletTransactions);
registry->RegisterTimePref(kBraveWalletLastUnlockTime, base::Time());
registry->RegisterTimePref(kBraveWalletP3ALastReportTime, base::Time());
Expand Down
5 changes: 5 additions & 0 deletions components/brave_wallet/browser/brave_wallet_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,11 @@ void BraveWalletService::SetDefaultBaseCryptocurrency(
}
}

void BraveWalletService::GetShowWalletTestNetworks(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for this method being backed by a pref.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant it should cover case when changing pref changes service method outcome;)

So please add smth like this
diff --git a/browser/brave_wallet/brave_wallet_service_unittest.cc b/browser/brave_wallet/brave_wallet_service_unittest.cc
index 93c5311959..7e686898b5 100644
--- a/browser/brave_wallet/brave_wallet_service_unittest.cc
+++ b/browser/brave_wallet/brave_wallet_service_unittest.cc
@@ -459,6 +459,18 @@ class BraveWalletServiceUnitTest : public testing::Test {
     return default_cryptocurrency;
   }
 
+  bool GetShowWalletTestNetworks() {
+    base::RunLoop run_loop;
+    bool show_wallet_test_networks;
+    service_->GetShowWalletTestNetworks(
+        base::BindLambdaForTesting([&](bool b) {
+          show_wallet_test_networks = b;
+          run_loop.Quit();
+        }));
+    run_loop.Run();
+    return show_wallet_test_networks;
+  }
+
   void SimulateOnGetImportInfo(const std::string& new_password,
                                bool result,
                                const ImportInfo& info,
@@ -1084,7 +1096,10 @@ TEST_F(BraveWalletServiceUnitTest, GetAndSetDefaultBaseCryptocurrency) {
 TEST_F(BraveWalletServiceUnitTest, GetShowWalletTestNetworks) {
   // Default value for kShowWalletTestNetworks should be false
   EXPECT_FALSE(GetPrefs()->GetBoolean(kShowWalletTestNetworks));
-  EXPECT_FALSE(GetShowWalletTestNetworks(GetPrefs()));
+  EXPECT_FALSE(GetShowWalletTestNetworks());
+
+  GetPrefs()->SetBoolean(kShowWalletTestNetworks, true);
+  EXPECT_TRUE(GetShowWalletTestNetworks());
 }
 
 TEST_F(BraveWalletServiceUnitTest, EthAddRemoveSetUserAssetVisible) {

Marking as approved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha!
Updated, thank you for the help! 😎

GetShowWalletTestNetworksCallback callback) {
std::move(callback).Run(::brave_wallet::GetShowWalletTestNetworks(prefs_));
}

void BraveWalletService::OnDefaultWalletChanged() {
auto default_wallet = ::brave_wallet::GetDefaultWallet(prefs_);
for (const auto& observer : observers_) {
Expand Down
2 changes: 2 additions & 0 deletions components/brave_wallet/browser/brave_wallet_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class BraveWalletService : public KeyedService,
void GetDefaultBaseCryptocurrency(
GetDefaultBaseCryptocurrencyCallback callback) override;
void SetDefaultBaseCryptocurrency(const std::string& cryptocurrency) override;
void GetShowWalletTestNetworks(
GetShowWalletTestNetworksCallback callback) override;
void AddPermission(mojom::CoinType coin,
const url::Origin& origin,
const std::string& account,
Expand Down
4 changes: 4 additions & 0 deletions components/brave_wallet/browser/brave_wallet_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,10 @@ void SetDefaultBaseCryptocurrency(PrefService* prefs,
prefs->SetString(kDefaultBaseCryptocurrency, cryptocurrency);
}

bool GetShowWalletTestNetworks(PrefService* prefs) {
return prefs->GetBoolean(kShowWalletTestNetworks);
}

std::string GetDefaultBaseCryptocurrency(PrefService* prefs) {
return prefs->GetString(kDefaultBaseCryptocurrency);
}
Expand Down
1 change: 1 addition & 0 deletions components/brave_wallet/browser/brave_wallet_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ void SetDefaultBaseCurrency(PrefService* prefs, const std::string& currency);
std::string GetDefaultBaseCurrency(PrefService* prefs);
void SetDefaultBaseCryptocurrency(PrefService* prefs,
const std::string& cryptocurrency);
bool GetShowWalletTestNetworks(PrefService* prefs);
std::string GetDefaultBaseCryptocurrency(PrefService* prefs);
std::vector<std::string> GetAllKnownEthNetworkIds();
std::vector<std::string> GetAllKnownSolNetworkIds();
Expand Down
1 change: 1 addition & 0 deletions components/brave_wallet/browser/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const char kDefaultBaseCryptocurrency[] =
const char kBraveWalletTransactions[] = "brave.wallet.transactions";
const char kShowWalletIconOnToolbar[] =
"brave.wallet.show_wallet_icon_on_toolbar";
const char kShowWalletTestNetworks[] = "brave.wallet.show_wallet_test_networks";
const char kBraveWalletLastUnlockTime[] =
"brave.wallet.wallet_last_unlock_time_v2";
const char kBraveWalletPingReportedUnlockTime[] =
Expand Down
1 change: 1 addition & 0 deletions components/brave_wallet/browser/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern const char kDefaultBaseCurrency[];
extern const char kDefaultBaseCryptocurrency[];
extern const char kBraveWalletTransactions[];
extern const char kShowWalletIconOnToolbar[];
extern const char kShowWalletTestNetworks[];
extern const char kBraveWalletLastUnlockTime[];
extern const char kBraveWalletPingReportedUnlockTime[];
extern const char kBraveWalletP3ALastReportTime[];
Expand Down
3 changes: 3 additions & 0 deletions components/brave_wallet/common/brave_wallet.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,9 @@ interface BraveWalletService {
// Sets the default base cryptocurrency
SetDefaultBaseCryptocurrency(string cryptocurrency);

// Obtains the show test networks user pref from settings
GetShowWalletTestNetworks() => (bool isEnabled);

// Adds the permission of coin type for the account
AddPermission(CoinType coin, url.mojom.Origin origin, string account)
=> (bool success);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,4 @@ export const setSelectedCoin = createAction<BraveWallet.CoinType>('setSelectedCo
export const setDefaultNetworks = createAction<BraveWallet.NetworkInfo[]>('setDefaultNetworks')
export const setSelectedNetworkFilter = createAction<BraveWallet.NetworkInfo>('setSelectedNetworkFilter')
export const setDefaultAccounts = createAction<BraveWallet.AccountInfo[]>('setDefaultAccounts')
export const setShowTestNetworks = createAction<boolean>('setShowTestNetworks')
2 changes: 2 additions & 0 deletions components/brave_wallet_ui/common/async/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ handler.on(WalletActions.initialized.getType(), async (store: Store, payload: Wa
crypto: defualtCrypo.cryptocurrency
}
store.dispatch(WalletActions.defaultCurrenciesUpdated(defaultCurrencies))
const showTestNetworks = await braveWalletService.getShowWalletTestNetworks()
store.dispatch(WalletActions.setShowTestNetworks(showTestNetworks.isEnabled))
// Fetch Balances and Prices
if (!state.isWalletLocked && state.isWalletCreated) {
const currentNetwork = await store.dispatch(refreshNetworkInfo())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,13 @@ export const createWalletReducer = (initialState: WalletState) => {
}
})

reducer.on(WalletActions.setShowTestNetworks, (state: WalletState, payload: boolean): WalletState => {
return {
...state,
isTestNetworksEnabled: payload
}
})

return reducer
}

Expand Down