Skip to content

Commit

Permalink
Switch tip where possible to tip via code
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Dec 5, 2019
1 parent 9c6bdf3 commit d4ca210
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 49 deletions.
157 changes: 130 additions & 27 deletions components/brave_rewards/browser/rewards_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,14 @@ class BraveRewardsBrowserTest
wait_for_tip_completed_loop_->Run();
}

void WaitForPendingTipToBeSaved() {
if (pending_tip_saved_) {
return;
}
wait_for_pending_tip_saved_loop_.reset(new base::RunLoop);
wait_for_pending_tip_saved_loop_->Run();
}

void WaitForMultipleTipReconcileCompleted(int32_t needed) {
multiple_tip_reconcile_needed_ = needed;
if (multiple_tip_reconcile_completed_) {
Expand Down Expand Up @@ -1341,6 +1349,7 @@ class BraveRewardsBrowserTest
// Signal that direct tip was made and update wallet with new
// balance
if (type == ContributionType::OneTimeTip && !should_contribute) {
WaitForPendingTipToBeSaved();
UpdateContributionBalance(amount, should_contribute);
}

Expand Down Expand Up @@ -1396,6 +1405,18 @@ class BraveRewardsBrowserTest
"" + GetBalance() + " BAT"), std::string::npos);
}

VerifyTip(amount, should_contribute, type == ContributionType::MonthlyTip);
}

void VerifyTip(
const double amount,
const bool should_contribute,
const bool monthly,
const bool via_code = false) {
if (via_code && monthly) {
return;
}

// Activate the Rewards settings page tab
ActivateTabAtIndex(0);

Expand All @@ -1404,7 +1425,7 @@ class BraveRewardsBrowserTest
ASSERT_EQ(RewardsPageBalance(), ExpectedBalanceString());

// Check that tip table shows the appropriate tip amount
const std::string selector = type == ContributionType::MonthlyTip
const std::string selector = monthly
? "[data-test-id='summary-donation']"
: "[data-test-id='summary-tips']";

Expand Down Expand Up @@ -1559,6 +1580,19 @@ class BraveRewardsBrowserTest
}
}

void OnPendingContributionSaved(
brave_rewards::RewardsService* rewards_service,
int result) {
if (result != 0) {
return;
}

pending_tip_saved_ = true;
if (wait_for_pending_tip_saved_loop_) {
wait_for_pending_tip_saved_loop_->Quit();
}
}

void OnNotificationAdded(
brave_rewards::RewardsNotificationService* rewards_notification_service,
const brave_rewards::RewardsNotificationService::RewardsNotification&
Expand Down Expand Up @@ -1620,9 +1654,10 @@ class BraveRewardsBrowserTest

void TipViaCode(
const std::string publisher_key,
int amount,
bool recurring,
ledger::PublisherStatus status) {
const double amount,
const ledger::PublisherStatus status,
const bool should_contribute = false,
const bool recurring = false) {
auto site = std::make_unique<brave_rewards::ContentSite>();
site->id = publisher_key;
site->name = publisher_key;
Expand All @@ -1631,6 +1666,18 @@ class BraveRewardsBrowserTest
site->provider = "";
site->favicon_url = "";
rewards_service_->OnTip(publisher_key, amount, recurring, std::move(site));

if (!recurring && should_contribute) {
// Wait for reconciliation to complete
WaitForTipReconcileCompleted();
ASSERT_EQ(tip_reconcile_status_, ledger::Result::LEDGER_OK);
}

// Signal to update pending contribution balance
if (!should_contribute) {
WaitForPendingTipToBeSaved();
UpdateContributionBalance(amount, should_contribute);
}
}

MOCK_METHOD1(OnGetEnvironment, void(ledger::Environment));
Expand Down Expand Up @@ -1682,6 +1729,9 @@ class BraveRewardsBrowserTest
std::unique_ptr<base::RunLoop> wait_for_recurring_tip_saved_loop_;
bool recurring_tip_saved_ = false;

std::unique_ptr<base::RunLoop> wait_for_pending_tip_saved_loop_;
bool pending_tip_saved_ = false;

std::unique_ptr<base::RunLoop> wait_for_attestation_loop_;

bool last_publisher_added_ = false;
Expand Down Expand Up @@ -2705,17 +2755,13 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest,
ClaimPromotion(true);

// Tip unverified publisher
TipPublisher("brave.com", ContributionType::OneTimeTip);
rewards_service_->OnTip("brave.com", 5.0, false);
UpdateContributionBalance(5.0, false); // update pending balance
TipPublisher("3zsistemi.si", ContributionType::OneTimeTip, false, 2);
TipPublisher("3zsistemi.si", ContributionType::OneTimeTip, false, 1);
TipPublisher("3zsistemi.si", ContributionType::OneTimeTip, false, 2);
TipPublisher("3zsistemi.si", ContributionType::OneTimeTip, false, 2);

// Make sure that pending contribution box shows the correct
// amount
ASSERT_EQ(RewardsPagePendingContributions(), ExpectedPendingBalanceString());
TipViaCode("brave.com", 5.0, ledger::PublisherStatus::NOT_VERIFIED);
TipViaCode("brave.com", 5.0, ledger::PublisherStatus::NOT_VERIFIED);
TipViaCode("3zsistemi.si", 10.0, ledger::PublisherStatus::NOT_VERIFIED);
TipViaCode("3zsistemi.si", 1.0, ledger::PublisherStatus::NOT_VERIFIED);
TipViaCode("3zsistemi.si", 10.0, ledger::PublisherStatus::NOT_VERIFIED);
TipViaCode("3zsistemi.si", 10.0, ledger::PublisherStatus::NOT_VERIFIED);
VerifyTip(41.0, false, false, true);

alter_publisher_list_ = false;

Expand Down Expand Up @@ -2795,8 +2841,12 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, RewardsPanelDefaultTipChoices) {
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);

// Add a recurring tip of 10 BAT.
bool monthly = true;
TipViaCode("3zsistemi.si", 10, monthly, ledger::PublisherStatus::VERIFIED);
TipViaCode(
"3zsistemi.si",
10.0,
ledger::PublisherStatus::VERIFIED,
false,
true);

content::WebContents* popup = OpenRewardsPopup();
const auto tip_options = GetRewardsPopupTipOptions(popup);
Expand Down Expand Up @@ -2909,8 +2959,14 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest,
// Enable Rewards
EnableRewards();

// Tip verified publisher
TipPublisher("duckduckgo.com", ContributionType::OneTimeTip, true);
const double amount = 5.0;
const bool should_contribute = true;
TipViaCode(
"duckduckgo.com",
amount,
ledger::PublisherStatus::VERIFIED,
should_contribute);
VerifyTip(amount, should_contribute, false, true);

// Stop observing the Rewards service
rewards_service()->RemoveObserver(this);
Expand All @@ -2928,7 +2984,14 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, TipConnectedPublisherAnon) {
ClaimPromotion(use_panel);

// Tip verified publisher
TipPublisher("bumpsmack.com", ContributionType::OneTimeTip, true);
const double amount = 5.0;
const bool should_contribute = true;
TipViaCode(
"bumpsmack.com",
amount,
ledger::PublisherStatus::CONNECTED,
should_contribute);
VerifyTip(amount, should_contribute, false, true);

// Stop observing the Rewards service
rewards_service_->RemoveObserver(this);
Expand Down Expand Up @@ -2959,7 +3022,14 @@ IN_PROC_BROWSER_TEST_F(
ClaimPromotion(use_panel);

// Tip verified publisher
TipPublisher("bumpsmack.com", ContributionType::OneTimeTip, true);
const double amount = 5.0;
const bool should_contribute = true;
TipViaCode(
"bumpsmack.com",
amount,
ledger::PublisherStatus::CONNECTED,
should_contribute);
VerifyTip(amount, should_contribute, false, true);

// Stop observing the Rewards service
rewards_service_->RemoveObserver(this);
Expand All @@ -2984,9 +3054,18 @@ IN_PROC_BROWSER_TEST_F(

// Enable Rewards
EnableRewards();
contents()->GetController().Reload(content::ReloadType::NORMAL, true);
EXPECT_TRUE(WaitForLoadStop(contents()));

// Tip verified publisher
TipPublisher("bumpsmack.com", ContributionType::OneTimeTip, false);
// Tip connected publisher
const double amount = 5.0;
const bool should_contribute = false;
TipViaCode(
"bumpsmack.com",
amount,
ledger::PublisherStatus::CONNECTED,
should_contribute);
VerifyTip(amount, should_contribute, false, true);

// Stop observing the Rewards service
rewards_service_->RemoveObserver(this);
Expand All @@ -3011,9 +3090,18 @@ IN_PROC_BROWSER_TEST_F(

// Enable Rewards
EnableRewards();
contents()->GetController().Reload(content::ReloadType::NORMAL, true);
EXPECT_TRUE(WaitForLoadStop(contents()));

// Tip verified publisher
TipPublisher("bumpsmack.com", ContributionType::OneTimeTip, false);
const double amount = 5.0;
const bool should_contribute = false;
TipViaCode(
"bumpsmack.com",
amount,
ledger::PublisherStatus::CONNECTED,
should_contribute);
VerifyTip(amount, should_contribute, false, true);

// Stop observing the Rewards service
rewards_service_->RemoveObserver(this);
Expand Down Expand Up @@ -3126,9 +3214,24 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest,
// Set monthly recurring
rewards_service_->OnTip("duckduckgo.com", 5, true);

TipViaCode("site1.com", 10, true, ledger::PublisherStatus::VERIFIED);
TipViaCode("site2.com", 10, true, ledger::PublisherStatus::VERIFIED);
TipViaCode("site3.com", 10, true, ledger::PublisherStatus::VERIFIED);
TipViaCode(
"site1.com",
10.0,
ledger::PublisherStatus::VERIFIED,
true,
true);
TipViaCode(
"site2.com",
10.0,
ledger::PublisherStatus::VERIFIED,
true,
true);
TipViaCode(
"site3.com",
10.0,
ledger::PublisherStatus::VERIFIED,
true,
true);

// Trigger contribution process
rewards_service()->StartMonthlyContributionForTest();
Expand Down
41 changes: 29 additions & 12 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ void RewardsServiceImpl::SavePublisherInfo(
ledger::PublisherInfoCallback callback) {
base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE,
base::BindOnce(&SavePublisherInfoOnFileTaskRunner,
std::move(publisher_info),
publisher_info->Clone(),
publisher_info_backend_.get()),
base::BindOnce(&RewardsServiceImpl::OnPublisherInfoSaved,
AsWeakPtr(),
Expand Down Expand Up @@ -2400,29 +2400,46 @@ void RewardsServiceImpl::OnPublisherBanner(
std::move(callback).Run(std::move(new_banner));
}

void RewardsServiceImpl::OnTipPublisherInfoSaved(const ledger::Result result,
ledger::PublisherInfoPtr info) {
void RewardsServiceImpl::OnTipPublisherInfoSaved(
const ledger::Result result,
ledger::PublisherInfoPtr info,
const bool recurring,
const double amount) {
if (!info) {
return;
}

if (recurring) {
SaveRecurringTipUI(info->id, amount, base::DoNothing());
return;
}

bat_ledger_->DoDirectTip(info->id, amount, "BAT", base::DoNothing());
}

void RewardsServiceImpl::OnTip(const std::string& publisher_key,
double amount,
bool recurring,
ledger::PublisherInfoPtr publisher_info) {
if (recurring) {
// TODO(nejczdovc): this needs to be wired through ledger code
// If caller provided publisher info, save it to `publisher_info` table
if (publisher_info) {
SavePublisherInfo(std::move(publisher_info),
// TODO(https://github.com/brave/brave-browser/issues/7217):
// this needs to be wired through ledger code
if (publisher_info) {
SavePublisherInfo(std::move(publisher_info),
std::bind(&RewardsServiceImpl::OnTipPublisherInfoSaved,
this, _1, _2));
}

this,
_1,
_2,
recurring,
amount));
return;
} else if (recurring) {
SaveRecurringTipUI(publisher_key, amount, base::DoNothing());
return;
}

if (!Connected())
if (!Connected()) {
return;
}

bat_ledger_->DoDirectTip(publisher_key, amount, "BAT", base::DoNothing());
}
Expand Down
7 changes: 5 additions & 2 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,11 @@ class RewardsServiceImpl : public RewardsService,
const std::string& value);
void OnResetState(ledger::OnResetCallback callback,
bool success);
void OnTipPublisherInfoSaved(const ledger::Result result,
ledger::PublisherInfoPtr info);
void OnTipPublisherInfoSaved(
const ledger::Result result,
ledger::PublisherInfoPtr info,
const bool recurring,
const double amount);
void OnTip(const std::string& publisher_key,
double amount,
bool recurring,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,6 @@ chrome.braveRewards.onRecurringTipRemoved.addListener((success: boolean) => {
}
})

chrome.braveRewards.onPendingContributionSaved.addListener((result: number) => {
if (result === 0) {
chrome.braveRewards.getPendingContributionsTotal(((amount: number) => {
rewardsPanelActions.OnPendingContributionsTotal(amount)
}))
}
})

chrome.braveRewards.onReconcileComplete.addListener((result: number, type: number) => {
if (result === 0) {
chrome.braveRewards.fetchBalance((balance: RewardsExtension.Balance) => {
Expand Down

0 comments on commit d4ca210

Please sign in to comment.