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 3, 2019
1 parent 38690c4 commit d2f1177
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 47 deletions.
145 changes: 121 additions & 24 deletions components/brave_rewards/browser/rewards_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,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 @@ -1271,6 +1279,7 @@ class BraveRewardsBrowserTest
// Signal that direct tip was made and update wallet with new
// balance
if (!monthly && !should_contribute) {
WaitForPendingTipToBeSaved();
UpdateContributionBalance(amount, should_contribute);
}

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

VerifyTip(amount, should_contribute, monthly);
}

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 Down Expand Up @@ -1489,6 +1510,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 @@ -1550,9 +1584,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 @@ -1561,6 +1596,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);
}
}

const std::vector<double> tip_amounts_ = {1.0, 5.0, 10.0};
Expand Down Expand Up @@ -1614,6 +1661,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 @@ -2638,17 +2688,13 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest,
ClaimPromotion(true);

// Tip unverified publisher
TipPublisher("brave.com");
rewards_service_->OnTip("brave.com", 5.0, false);
UpdateContributionBalance(5.0, false); // update pending balance
TipPublisher("3zsistemi.si", false, false, 2);
TipPublisher("3zsistemi.si", false, false, 1);
TipPublisher("3zsistemi.si", false, false, 2);
TipPublisher("3zsistemi.si", false, 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 @@ -2776,8 +2822,14 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest,
// Enable Rewards
EnableRewards();

// Tip verified publisher
TipPublisher("duckduckgo.com", 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 @@ -2795,7 +2847,14 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, TipConnectedPublisherAnon) {
ClaimPromotion(use_panel);

// Tip verified publisher
TipPublisher("bumpsmack.com", 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 @@ -2826,7 +2885,14 @@ IN_PROC_BROWSER_TEST_F(
ClaimPromotion(use_panel);

// Tip verified publisher
TipPublisher("bumpsmack.com", 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 @@ -2851,9 +2917,17 @@ IN_PROC_BROWSER_TEST_F(

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

// Tip verified publisher
TipPublisher("bumpsmack.com", 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 @@ -2878,9 +2952,17 @@ IN_PROC_BROWSER_TEST_F(

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

// Tip verified publisher
TipPublisher("bumpsmack.com", 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 @@ -2993,9 +3075,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
38 changes: 25 additions & 13 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,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 @@ -2397,29 +2397,41 @@ 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),
if (publisher_info) {
SavePublisherInfo(std::move(publisher_info),
std::bind(&RewardsServiceImpl::OnTipPublisherInfoSaved,
this, _1, _2));
}

SaveRecurringTipUI(publisher_key, amount, base::DoNothing());
this,
_1,
_2,
recurring,
amount));
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 @@ -356,8 +356,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 d2f1177

Please sign in to comment.