diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 8c036b9c26ae..5c366d5d04c9 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -2785,11 +2785,10 @@ void RewardsServiceImpl::OnFetchBalance( if (IsRewardsEnabled()) { const bool grants_claimed = pref_service->GetBoolean(prefs::kUserHasClaimedGrant); - p3a::RecordWalletState( - {.wallet_created = true, - .rewards_enabled = true, - .grants_claimed = grants_claimed, - .funds_added = balance && balance->user_funds > 0}); + p3a::RecordWalletState({.wallet_created = true, + .rewards_enabled = true, + .grants_claimed = grants_claimed, + .funds_added = balance && balance->user_funds > 0}); } if (balance) { diff --git a/components/brave_rewards/browser/test/common/rewards_browsertest_contribution.h b/components/brave_rewards/browser/test/common/rewards_browsertest_contribution.h index cade306b21e3..72bb5e20b2ca 100644 --- a/components/brave_rewards/browser/test/common/rewards_browsertest_contribution.h +++ b/components/brave_rewards/browser/test/common/rewards_browsertest_contribution.h @@ -14,6 +14,7 @@ #include "bat/ledger/mojom_structs.h" #include "brave/components/brave_rewards/browser/rewards_service_impl.h" #include "brave/components/brave_rewards/browser/rewards_service_observer.h" +#include "brave/components/brave_rewards/browser/test/common/rewards_browsertest_context_helper.h" #include "brave/components/brave_rewards/browser/test/common/rewards_browsertest_util.h" #include "chrome/browser/ui/browser.h" diff --git a/components/brave_rewards/browser/test/rewards_p3a_browsertest.cc b/components/brave_rewards/browser/test/rewards_p3a_browsertest.cc new file mode 100644 index 000000000000..e2467dfed7c1 --- /dev/null +++ b/components/brave_rewards/browser/test/rewards_p3a_browsertest.cc @@ -0,0 +1,222 @@ +/* Copyright (c) 2021 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include +#include + +#include "base/containers/flat_map.h" +#include "base/test/bind.h" +#include "base/test/metrics/histogram_tester.h" +#include "brave/browser/brave_rewards/rewards_service_factory.h" +#include "brave/common/brave_paths.h" +#include "brave/components/brave_rewards/browser/rewards_service_impl.h" +#include "brave/components/brave_rewards/browser/rewards_service_observer.h" +#include "brave/components/brave_rewards/browser/test/common/rewards_browsertest_context_util.h" +#include "brave/components/brave_rewards/browser/test/common/rewards_browsertest_contribution.h" +#include "brave/components/brave_rewards/browser/test/common/rewards_browsertest_network_util.h" +#include "brave/components/brave_rewards/browser/test/common/rewards_browsertest_promotion.h" +#include "brave/components/brave_rewards/browser/test/common/rewards_browsertest_response.h" +#include "brave/components/brave_rewards/browser/test/common/rewards_browsertest_util.h" +#include "chrome/test/base/in_process_browser_test.h" +#include "components/network_session_configurator/common/network_switches.h" +#include "content/public/test/browser_test.h" +#include "content/public/test/browser_test_utils.h" +#include "net/dns/mock_host_resolver.h" + +// npm run test -- brave_browser_tests --filter=RewardsP3ABrowserTest.* + +namespace rewards_browsertest { + +class RewardsP3ABrowserTest : public InProcessBrowserTest, + public brave_rewards::RewardsServiceObserver { + public: + RewardsP3ABrowserTest() { + contribution_ = std::make_unique(); + promotion_ = std::make_unique(); + response_ = std::make_unique(); + histogram_tester_.reset(new base::HistogramTester); + } + + void SetUpOnMainThread() override { + InProcessBrowserTest::SetUpOnMainThread(); + + context_helper_ = + std::make_unique(browser()); + + // HTTP resolver + host_resolver()->AddRule("*", "127.0.0.1"); + https_server_.reset(new net::EmbeddedTestServer( + net::test_server::EmbeddedTestServer::TYPE_HTTPS)); + https_server_->SetSSLConfig(net::EmbeddedTestServer::CERT_OK); + https_server_->RegisterRequestHandler( + base::BindRepeating(&rewards_browsertest_util::HandleRequest)); + ASSERT_TRUE(https_server_->Start()); + + // Rewards service + brave::RegisterPathProvider(); + auto* profile = browser()->profile(); + rewards_service_ = static_cast( + brave_rewards::RewardsServiceFactory::GetForProfile(profile)); + rewards_service_->AddObserver(this); + + // Response mock + base::ScopedAllowBlockingForTesting allow_blocking; + response_->LoadMocks(); + rewards_service_->ForTestingSetTestResponseCallback(base::BindRepeating( + &RewardsP3ABrowserTest::GetTestResponse, base::Unretained(this))); + rewards_service_->SetLedgerEnvForTesting(); + + // Other + promotion_->Initialize(browser(), rewards_service_); + contribution_->Initialize(browser(), rewards_service_); + + rewards_browsertest_util::SetOnboardingBypassed(browser()); + } + + void TearDown() override { InProcessBrowserTest::TearDown(); } + + void SetUpCommandLine(base::CommandLine* command_line) override { + // HTTPS server only serves a valid cert for localhost, so this is needed + // to load pages from other hosts without an error + command_line->AppendSwitch(switches::kIgnoreCertificateErrors); + } + + void GetTestResponse(const std::string& url, + int32_t method, + int* response_status_code, + std::string* response, + base::flat_map* headers) { + response_->SetExternalBalance(contribution_->GetExternalBalance()); + response_->Get(url, method, response_status_code, response); + } + + void FetchBalance() { + base::RunLoop run_loop; + rewards_service_->FetchBalance(base::BindLambdaForTesting( + [&](ledger::type::Result result, ledger::type::BalancePtr balance) { + run_loop.Quit(); + })); + run_loop.Run(); + } + + void WaitForRewardsInitialization() { + if (rewards_initialized_) { + return; + } + + wait_for_rewards_initialization_loop_.reset(new base::RunLoop); + wait_for_rewards_initialization_loop_->Run(); + } + + content::WebContents* contents() { + return browser()->tab_strip_model()->GetActiveWebContents(); + } + + void OnRewardsInitialized( + brave_rewards::RewardsService* rewards_service) override { + rewards_initialized_ = true; + } + + brave_rewards::RewardsServiceImpl* rewards_service_; + std::unique_ptr https_server_; + std::unique_ptr contribution_; + std::unique_ptr promotion_; + std::unique_ptr response_; + std::unique_ptr context_helper_; + std::unique_ptr histogram_tester_; + + bool rewards_initialized_ = false; + std::unique_ptr wait_for_rewards_initialization_loop_; +}; + +IN_PROC_BROWSER_TEST_F(RewardsP3ABrowserTest, RewardsDisabled) { + rewards_browsertest_util::StartProcess(rewards_service_); + + WaitForRewardsInitialization(); + + histogram_tester_->ExpectBucketCount("Brave.Rewards.WalletBalance.2", 1, 1); + histogram_tester_->ExpectBucketCount("Brave.Rewards.AutoContributionsState.2", + 1, 1); + histogram_tester_->ExpectBucketCount("Brave.Rewards.TipsState.2", 1, 1); +} + +IN_PROC_BROWSER_TEST_F(RewardsP3ABrowserTest, + WalletStateWalletCreatedNoGrantsClaimedNoFundsAdded) { + rewards_browsertest_util::StartProcess(rewards_service_); + rewards_browsertest_util::CreateWallet(rewards_service_); + + rewards_service_->SetAutoContributeEnabled(true); + rewards_service_->SetAdsEnabled(true); + + FetchBalance(); + + histogram_tester_->ExpectBucketCount("Brave.Rewards.WalletState", 1, 1); +} + +IN_PROC_BROWSER_TEST_F(RewardsP3ABrowserTest, + WalletStateWalletCreatedGrantsClaimedNoFundsAdded) { + rewards_browsertest_util::StartProcess(rewards_service_); + rewards_browsertest_util::CreateWallet(rewards_service_); + + context_helper_->LoadURL(rewards_browsertest_util::GetRewardsUrl()); + + rewards_service_->SetAutoContributeEnabled(true); + rewards_service_->SetAdsEnabled(true); + + contribution_->AddBalance(promotion_->ClaimPromotionViaCode()); + + FetchBalance(); + + histogram_tester_->ExpectBucketCount("Brave.Rewards.WalletState", 2, 1); +} + +IN_PROC_BROWSER_TEST_F(RewardsP3ABrowserTest, + WalletStateWalletCreatedNoGrantsClaimedFundsAdded) { + response_->SetUserFundsBalance(true); + + rewards_browsertest_util::StartProcess(rewards_service_); + rewards_browsertest_util::CreateWallet(rewards_service_); + + context_helper_->LoadURL(rewards_browsertest_util::GetRewardsUrl()); + + rewards_service_->SetAutoContributeEnabled(true); + rewards_service_->SetAdsEnabled(true); + + FetchBalance(); + + histogram_tester_->ExpectBucketCount("Brave.Rewards.WalletState", 3, 1); +} + +IN_PROC_BROWSER_TEST_F(RewardsP3ABrowserTest, + WalletStateWalletCreatedGrantsClaimedFundsAdded) { + response_->SetUserFundsBalance(true); + + rewards_browsertest_util::StartProcess(rewards_service_); + rewards_browsertest_util::CreateWallet(rewards_service_); + + context_helper_->LoadURL(rewards_browsertest_util::GetRewardsUrl()); + + rewards_service_->SetAutoContributeEnabled(true); + rewards_service_->SetAdsEnabled(true); + + contribution_->AddBalance(promotion_->ClaimPromotionViaCode()); + + FetchBalance(); + + histogram_tester_->ExpectBucketCount("Brave.Rewards.WalletState", 4, 1); +} + +IN_PROC_BROWSER_TEST_F(RewardsP3ABrowserTest, + WalletStateWalletDisabledAfterCreation) { + rewards_browsertest_util::StartProcess(rewards_service_); + rewards_browsertest_util::CreateWallet(rewards_service_); + + rewards_service_->SetAdsEnabled(false); + rewards_service_->SetAutoContributeEnabled(false); + + histogram_tester_->ExpectBucketCount("Brave.Rewards.WalletState", 5, 1); +} + +} // namespace rewards_browsertest diff --git a/test/BUILD.gn b/test/BUILD.gn index ec72d003f093..df10e5262f61 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -726,6 +726,7 @@ if (!is_android) { "//brave/components/brave_rewards/browser/test/rewards_contribution_browsertest.cc", "//brave/components/brave_rewards/browser/test/rewards_flag_browsertest.cc", "//brave/components/brave_rewards/browser/test/rewards_notification_browsertest.cc", + "//brave/components/brave_rewards/browser/test/rewards_p3a_browsertest.cc", "//brave/components/brave_rewards/browser/test/rewards_promotion_browsertest.cc", "//brave/components/brave_rewards/browser/test/rewards_publisher_browsertest.cc", "//brave/components/brave_rewards/browser/test/rewards_state_browsertest.cc",