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

Fix flaky Rewards browser tests (uplift to 1.39.x) #13430

Merged
merged 2 commits into from
May 24, 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "brave/browser/ui/views/location_bar/brave_location_bar_view.h"
#include "brave/components/brave_rewards/browser/test/common/rewards_browsertest_context_helper.h"
#include "brave/components/brave_rewards/browser/test/common/rewards_browsertest_context_util.h"
#include "brave/components/brave_rewards/browser/test/common/rewards_browsertest_util.h"
#include "brave/components/brave_rewards/common/pref_names.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/test/base/ui_test_utils.h"
Expand Down Expand Up @@ -208,6 +209,31 @@ void RewardsBrowserTestContextHelper::LoadURL(GURL url) {
WaitForLoadStop(contents);
}

void RewardsBrowserTestContextHelper::LoadRewardsPage() {
GURL url = rewards_browsertest_util::GetRewardsUrl();
auto* tab_strip = browser_->tab_strip_model();

// Activate the rewards page if it's already loaded into a tab.
bool found = false;
for (int index = 0; index < tab_strip->count(); ++index) {
auto* contents = tab_strip->GetWebContentsAt(index);
if (contents->GetLastCommittedURL() == url) {
found = true;
tab_strip->ActivateTabAt(index);
break;
}
}

// Otherwise, load the rewards page into a new tab.
if (!found) {
LoadURL(url);
}

// Wait for the content to be fully rendered before continuing.
rewards_browsertest_util::WaitForElementToAppear(
tab_strip->GetActiveWebContents(), "[data-test-id=rewards-balance-text]");
}

void RewardsBrowserTestContextHelper::ReloadCurrentSite() {
auto* contents = browser_->tab_strip_model()->GetActiveWebContents();
contents->GetController().Reload(content::ReloadType::NORMAL, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class RewardsBrowserTestContextHelper {

void LoadURL(GURL url);

void LoadRewardsPage();

void ReloadCurrentSite();

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void RewardsBrowserTestContribution::VerifyTip(
}

// Load rewards page
context_helper_->LoadURL(rewards_browsertest_util::GetRewardsUrl());
context_helper_->LoadRewardsPage();

if (should_contribute) {
// Make sure that balance is updated correctly
Expand All @@ -220,8 +220,7 @@ void RewardsBrowserTestContribution::VerifyTip(
: "[data-test-id=rewards-summary-one-time]";

rewards_browsertest_util::WaitForElementToContain(
contents(), selector,
"-" + base::StringPrintf("%.2f", amount) + " BAT");
contents(), selector, base::StringPrintf("-%.2f BAT", amount));
return;
}

Expand All @@ -245,8 +244,7 @@ void RewardsBrowserTestContribution::IsBalanceCorrect() {

void RewardsBrowserTestContribution::IsPendingBalanceCorrect() {
rewards_browsertest_util::WaitForElementToContain(
contents(),
"[data-test-id='pending-contribution-box']",
contents(), "[data-test-id=rewards-summary-pending]",
GetStringPendingBalance());
}

Expand Down Expand Up @@ -436,9 +434,7 @@ std::string RewardsBrowserTestContribution::GetStringBalance() {
}

std::string RewardsBrowserTestContribution::GetStringPendingBalance() {
const std::string balance =
rewards_browsertest_util::BalanceDoubleToString(pending_balance_);
return balance + " BAT";
return base::StringPrintf("%.2f BAT", pending_balance_);
}

ledger::type::Result RewardsBrowserTestContribution::GetACStatus() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ void WaitForLedgerStop(brave_rewards::RewardsServiceImpl* rewards_service) {

void CreateWallet(brave_rewards::RewardsServiceImpl* rewards_service) {
DCHECK(rewards_service);

// Ensure that the utility process is started before attempting to create a
// rewards payment ID.
StartProcess(rewards_service);

base::RunLoop run_loop;
bool success = false;
rewards_service->CreateWallet(
Expand Down
147 changes: 56 additions & 91 deletions components/brave_rewards/browser/test/rewards_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "content/public/browser/notification_types.h"
#include "content/public/test/browser_test.h"
#include "net/dns/mock_host_resolver.h"

Expand Down Expand Up @@ -127,6 +128,22 @@ class RewardsBrowserTest : public InProcessBrowserTest {
return total;
}

void WaitForNavigation(const std::string& url_substring) {
content::WindowedNotificationObserver window_observer(
content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
base::BindLambdaForTesting(
[&](const content::NotificationSource& source,
const content::NotificationDetails&) {
auto contents_source =
static_cast<const content::Source<content::WebContents>&>(
source);
std::string url = contents_source->GetLastCommittedURL().spec();
return url.find(url_substring) != std::string::npos;
}));

window_observer.Wait();
}

base::test::ScopedFeatureList feature_list_;
raw_ptr<brave_rewards::RewardsServiceImpl> rewards_service_ = nullptr;
std::unique_ptr<net::EmbeddedTestServer> https_server_;
Expand All @@ -136,9 +153,8 @@ class RewardsBrowserTest : public InProcessBrowserTest {
std::unique_ptr<RewardsBrowserTestContextHelper> context_helper_;
};

// https://github.com/brave/brave-browser/issues/12632
IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, DISABLED_ActivateSettingsModal) {
context_helper_->LoadURL(rewards_browsertest_util::GetRewardsUrl());
IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, ActivateSettingsModal) {
context_helper_->LoadRewardsPage();

rewards_browsertest_util::WaitForElementThenClick(
contents(), "[data-test-id=manage-wallet-button]");
Expand All @@ -147,34 +163,8 @@ IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, DISABLED_ActivateSettingsModal) {
"#modal");
}

// https://github.com/brave/brave-browser/issues/12988
IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, DISABLED_ToggleAutoContribute) {
context_helper_->LoadURL(rewards_browsertest_util::GetRewardsUrl());

// toggle auto contribute back on
rewards_browsertest_util::WaitForElementThenClick(
contents(),
"[data-test-id2='autoContribution']");
std::string value = rewards_browsertest_util::WaitForElementThenGetAttribute(
contents(),
"[data-test-id2='autoContribution']",
"data-toggled");
ASSERT_STREQ(value.c_str(), "true");

// toggle auto contribute off
rewards_browsertest_util::WaitForElementThenClick(
contents(),
"[data-test-id2='autoContribution']");
value =
rewards_browsertest_util::WaitForElementThenGetAttribute(
contents(),
"[data-test-id2='autoContribution']",
"data-toggled");
ASSERT_STREQ(value.c_str(), "false");
}

IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, SiteBannerDefaultTipChoices) {
rewards_browsertest_util::StartProcess(rewards_service_);
rewards_browsertest_util::CreateWallet(rewards_service_);
rewards_browsertest_util::NavigateToPublisherPage(
browser(),
https_server_.get(),
Expand All @@ -194,9 +184,8 @@ IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, SiteBannerDefaultTipChoices) {
ASSERT_EQ(tip_options, std::vector<double>({ 1, 10, 100 }));
}

IN_PROC_BROWSER_TEST_F(
RewardsBrowserTest,
SiteBannerDefaultPublisherAmounts) {
IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, SiteBannerDefaultPublisherAmounts) {
rewards_browsertest_util::CreateWallet(rewards_service_);
rewards_browsertest_util::NavigateToPublisherPage(
browser(),
https_server_.get(),
Expand All @@ -210,49 +199,39 @@ IN_PROC_BROWSER_TEST_F(
ASSERT_EQ(tip_options, std::vector<double>({ 5, 10, 20 }));
}

// Disabled in https://github.com/brave/brave-browser/issues/10789
IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, DISABLED_NotVerifiedWallet) {
IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, NotVerifiedWallet) {
rewards_browsertest_util::CreateWallet(rewards_service_);
context_helper_->LoadRewardsPage();
contribution_->AddBalance(promotion_->ClaimPromotionViaCode());
contribution_->IsBalanceCorrect();

// Click on verify button
rewards_browsertest_util::WaitForElementThenClick(
contents(),
"#verify-wallet-button");
contents(), "[data-test-id=verify-rewards-button]");

// Click on verify button in on boarding
rewards_browsertest_util::WaitForElementThenClick(
contents(),
"#on-boarding-verify-button");
contents(), "[data-test-id=connect-continue-button]");

rewards_browsertest_util::WaitForElementThenClick(
contents(), "[data-test-id=connect-provider-button]");

// Check if we are redirected to uphold
{
const GURL current_url = contents()->GetURL();
ASSERT_TRUE(base::StartsWith(
current_url.spec(),
ledger::uphold::GetUrl() + "/authorize/",
base::CompareCase::INSENSITIVE_ASCII));
}
WaitForNavigation(ledger::uphold::GetUrl() + "/authorize/");

response_->SetVerifiedWallet(true);

// Fake successful authentication
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
browser(),
uphold_auth_url(), 1);

// Check if we are redirected to KYC page
{
const GURL current_url = contents()->GetURL();
ASSERT_TRUE(base::StartsWith(
current_url.spec(),
ledger::uphold::GetUrl() + "/signup/step2",
base::CompareCase::INSENSITIVE_ASCII));
}
rewards_browsertest_util::WaitForElementToContain(
contents(), "[data-test-id=external-wallet-status-text]", "Verified");
}

IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, ShowACPercentInThePanel) {
rewards_browsertest_util::StartProcess(rewards_service_);
rewards_browsertest_util::CreateWallet(rewards_service_);
rewards_service_->SetAutoContributeEnabled(true);
context_helper_->LoadURL(rewards_browsertest_util::GetRewardsUrl());
context_helper_->LoadRewardsPage();
context_helper_->VisitPublisher(
rewards_browsertest_util::GetUrl(https_server_.get(), "3zsistemi.si"),
true);
Expand All @@ -276,7 +255,7 @@ IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, ShowACPercentInThePanel) {
IN_PROC_BROWSER_TEST_F(RewardsBrowserTest,
ZeroBalanceWalletClaimNotCalled_Uphold) {
response_->SetVerifiedWallet(true);
rewards_browsertest_util::StartProcess(rewards_service_);
rewards_browsertest_util::CreateWallet(rewards_service_);
contribution_->SetUpUpholdWallet(rewards_service_, 50.0);

response_->ClearRequests();
Expand Down Expand Up @@ -310,7 +289,7 @@ IN_PROC_BROWSER_TEST_F(RewardsBrowserTest,
response_->SetVerifiedWallet(true);
auto* prefs = browser()->profile()->GetPrefs();
prefs->SetBoolean(brave_rewards::prefs::kFetchOldBalance, false);
rewards_browsertest_util::StartProcess(rewards_service_);
rewards_browsertest_util::CreateWallet(rewards_service_);
contribution_->SetUpGeminiWallet(rewards_service_, 50.0);

response_->ClearRequests();
Expand Down Expand Up @@ -338,17 +317,13 @@ IN_PROC_BROWSER_TEST_F(RewardsBrowserTest,
run_loop.Run();
}

// https://github.com/brave/brave-browser/issues/12987
IN_PROC_BROWSER_TEST_F(RewardsBrowserTest,
DISABLED_BackupRestoreModalHasNotice) {
context_helper_->LoadURL(rewards_browsertest_util::GetRewardsUrl());
IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, BackupRestoreModalHasNotice) {
rewards_browsertest_util::CreateWallet(rewards_service_);
context_helper_->LoadRewardsPage();
contribution_->AddBalance(promotion_->ClaimPromotionViaCode());

rewards_browsertest_util::WaitForElementToEqual(
contents(),
"[data-test-id='balance']",
"30.000 BAT");
contents(), "[data-test-id='rewards-balance-text']", "30.000 BAT");

// Click the settings button and wait for the backup modal to appear
rewards_browsertest_util::WaitForElementThenClick(
Expand All @@ -365,9 +340,8 @@ IN_PROC_BROWSER_TEST_F(RewardsBrowserTest,

IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, BackupRestoreModalHasNoNotice) {
response_->SetUserFundsBalance(20.0);
rewards_browsertest_util::StartProcess(rewards_service_);
rewards_browsertest_util::CreateWallet(rewards_service_);
context_helper_->LoadURL(rewards_browsertest_util::GetRewardsUrl());
context_helper_->LoadRewardsPage();

rewards_browsertest_util::WaitForElementToEqual(
contents(), "[data-test-id=rewards-balance-text]", "20.000 BAT");
Expand All @@ -385,7 +359,8 @@ IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, BackupRestoreModalHasNoNotice) {
}

IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, ResetRewards) {
context_helper_->LoadURL(rewards_browsertest_util::GetRewardsUrl());
rewards_browsertest_util::CreateWallet(rewards_service_);
context_helper_->LoadRewardsPage();

rewards_browsertest_util::WaitForElementThenClick(
contents(), "[data-test-id=manage-wallet-button]");
Expand All @@ -402,10 +377,9 @@ IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, ResetRewards) {
"Your Rewards data will");
}

// https://github.com/brave/brave-browser/issues/12607
IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, DISABLED_ResetRewardsWithBAT) {
context_helper_->LoadURL(rewards_browsertest_util::GetRewardsUrl());
IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, ResetRewardsWithBAT) {
rewards_browsertest_util::CreateWallet(rewards_service_);
context_helper_->LoadRewardsPage();
contribution_->AddBalance(promotion_->ClaimPromotionViaCode());

rewards_browsertest_util::WaitForElementThenClick(
Expand All @@ -424,32 +398,24 @@ IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, DISABLED_ResetRewardsWithBAT) {
"Your 30 BAT and other Rewards");
}

// https://github.com/brave/brave-browser/issues/12704
IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, DISABLED_UpholdLimitNoBAT) {
context_helper_->LoadURL(rewards_browsertest_util::GetRewardsUrl());
IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, UpholdLimitNoBAT) {
rewards_browsertest_util::CreateWallet(rewards_service_);
context_helper_->LoadRewardsPage();

rewards_browsertest_util::WaitForElementThenClick(
contents(),
"#verify-wallet-button");
contents(), "[data-test-id=verify-rewards-button]");

rewards_browsertest_util::WaitForElementThenClick(
contents(),
"#cancel-login-button");
contents(), "[data-test-id=connect-continue-button]");

rewards_browsertest_util::WaitForElementThenClick(
contents(),
"#verify-wallet-button");
contents(), "[data-test-id=connect-provider-button]");

rewards_browsertest_util::WaitForElementThenClick(
contents(),
"#login-button");
contents(), "[data-test-id=connect-login-button]");

// Check if we are redirected to uphold
{
const GURL current_url = contents()->GetURL();

auto found = current_url.spec().find("intention=login");
ASSERT_TRUE(found != std::string::npos);
}
WaitForNavigation("intention=login");
}

IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, EnableRewardsWithBalance) {
Expand All @@ -459,7 +425,6 @@ IN_PROC_BROWSER_TEST_F(RewardsBrowserTest, EnableRewardsWithBalance) {
prefs->SetBoolean(brave_rewards::prefs::kAutoContributeEnabled, false);

// Load a balance into the user's wallet
rewards_browsertest_util::StartProcess(rewards_service_);
rewards_browsertest_util::CreateWallet(rewards_service_);
rewards_service_->FetchPromotions();
promotion_->WaitForPromotionInitialization();
Expand Down
Loading