Skip to content

Commit

Permalink
Updates balance in tip banner
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Nov 25, 2019
1 parent b60900a commit 67df180
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 35 deletions.
26 changes: 26 additions & 0 deletions browser/ui/webui/brave_tip_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ class RewardsTipDOMHandler : public WebUIMessageHandler,
void OnRecurringTipRemoved(brave_rewards::RewardsService* rewards_service,
bool success) override;

void OnReconcileComplete(
brave_rewards::RewardsService* rewards_service,
unsigned int result,
const std::string& viewing_id,
const std::string& probi,
const int32_t type) override;

brave_rewards::RewardsService* rewards_service_; // NOT OWNED
base::WeakPtrFactory<RewardsTipDOMHandler> weak_factory_;

Expand Down Expand Up @@ -477,3 +484,22 @@ void RewardsTipDOMHandler::OnlyAnonWallet(const base::ListValue* args) {
"brave_rewards_tip.onlyAnonWallet",
base::Value(allow));
}

void RewardsTipDOMHandler::OnReconcileComplete(
brave_rewards::RewardsService* rewards_service,
unsigned int result,
const std::string& viewing_id,
const std::string& probi,
const int32_t type) {
if (!web_ui()->CanCallJavascript()) {
return;
}

base::DictionaryValue complete;
complete.SetKey("result", base::Value(static_cast<int>(result)));
complete.SetKey("type", base::Value(type));

web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards_tip.reconcileComplete",
complete);
}
70 changes: 49 additions & 21 deletions components/brave_rewards/browser/rewards_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,14 @@ class BraveRewardsBrowserTest
wait_for_insufficient_notification_loop_->Run();
}

void WaitForRecurringTipToBeSaved() {
if (recurring_tip_saved_) {
return;
}
wait_for_recurring_tip_saved_loop_.reset(new base::RunLoop);
wait_for_recurring_tip_saved_loop_->Run();
}

void WaitForSelector(content::WebContents* contents,
const std::string& selector) const {
auto script = content::JsReplace(
Expand Down Expand Up @@ -1236,7 +1244,7 @@ class BraveRewardsBrowserTest
ASSERT_TRUE(site_banner_contents);

const double amount = tip_amounts_.at(selection);
const std::string amount_str = std::to_string(static_cast<int32_t>(amount));
const std::string amount_str = base::StringPrintf("%2.1f", amount);

// Select the tip amount (default is 1.0 BAT)
ASSERT_TRUE(ExecJs(
Expand Down Expand Up @@ -1272,28 +1280,9 @@ class BraveRewardsBrowserTest
const std::string confirmationText = monthly
? "Monthly contribution has been set!"
: "Tip sent!";
// Make sure that thank you banner shows correct publisher data
// (domain and amount)
{
content::EvalJsResult js_result = EvalJs(
site_banner_contents,
"const delay = t => new Promise(resolve => setTimeout(resolve, t));"
"delay(0).then(() => "
" document.documentElement.innerText);",
content::EXECUTE_SCRIPT_DEFAULT_OPTIONS,
content::ISOLATED_WORLD_ID_CONTENT_END);
EXPECT_NE(js_result.ExtractString().find(
confirmationText), std::string::npos);
EXPECT_NE(js_result.ExtractString().find(
"" + amount_str + ".0 BAT"), std::string::npos);
EXPECT_NE(js_result.ExtractString().find(
"Share the good news:"), std::string::npos);
}

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

if (monthly) {
WaitForRecurringTipToBeSaved();
// Trigger contribution process
rewards_service()->StartMonthlyContributionForTest();

Expand All @@ -1317,6 +1306,29 @@ class BraveRewardsBrowserTest
ASSERT_EQ(tip_reconcile_status_, ledger::Result::LEDGER_OK);
}

// Make sure that thank you banner shows correct publisher data
// (domain and amount)
{
content::EvalJsResult js_result = EvalJs(
site_banner_contents,
"const delay = t => new Promise(resolve => setTimeout(resolve, t));"
"delay(1000).then(() => "
" document.documentElement.innerText);",
content::EXECUTE_SCRIPT_DEFAULT_OPTIONS,
content::ISOLATED_WORLD_ID_CONTENT_END);
EXPECT_NE(js_result.ExtractString().find(
confirmationText), std::string::npos);
EXPECT_NE(js_result.ExtractString().find(
"" + amount_str + " BAT"), std::string::npos);
EXPECT_NE(js_result.ExtractString().find(
"Share the good news:"), std::string::npos);
EXPECT_NE(js_result.ExtractString().find(
"" + GetBalance() + " BAT"), std::string::npos);
}

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

if (should_contribute) {
// Make sure that balance is updated correctly
ASSERT_EQ(RewardsPageBalance(), ExpectedBalanceString());
Expand Down Expand Up @@ -1471,6 +1483,19 @@ class BraveRewardsBrowserTest
}
}

void OnRecurringTipSaved(
brave_rewards::RewardsService* rewards_service,
bool success) {
if (!success) {
return;
}

recurring_tip_saved_ = true;
if (wait_for_recurring_tip_saved_loop_) {
wait_for_recurring_tip_saved_loop_->Quit();
}
}

void OnNotificationAdded(
brave_rewards::RewardsNotificationService* rewards_notification_service,
const brave_rewards::RewardsNotificationService::RewardsNotification&
Expand Down Expand Up @@ -1593,6 +1618,9 @@ class BraveRewardsBrowserTest
std::unique_ptr<base::RunLoop> brave_ads_have_arrived_notification_run_loop_;
bool brave_ads_have_arrived_notification_was_already_shown_ = false;

std::unique_ptr<base::RunLoop> wait_for_recurring_tip_saved_loop_;
bool recurring_tip_saved_ = false;

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

bool last_publisher_added_ = false;
Expand Down
9 changes: 8 additions & 1 deletion components/brave_rewards/resources/tip/brave_rewards_tip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ window.cr.define('brave_rewards_tip', function () {
getActions().onOnlyAnonWallet(only)
}

function reconcileComplete (properties: {type: number, result: number}) {
if (properties.result === 0) {
getActions().getBalance()
}
}

return {
initialize,
publisherBanner,
Expand All @@ -104,7 +110,8 @@ window.cr.define('brave_rewards_tip', function () {
recurringTipSaved,
balance,
externalWallet,
onlyAnonWallet
onlyAnonWallet,
reconcileComplete
}
})

Expand Down
27 changes: 14 additions & 13 deletions components/brave_rewards/resources/tip/reducers/tip_reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,21 @@ const publishersReducer: Reducer<RewardsTip.State> = (state: RewardsTip.State =
break
}
case types.ON_TIP: {
if (payload.publisherKey && payload.amount > 0) {
let amount = parseFloat(payload.amount)
chrome.send('brave_rewards_tip.onTip', [
payload.publisherKey,
amount,
payload.recurring
])
state = { ...state }
state.finished = true
state.currentTipAmount = amount.toFixed(1)
state.currentTipRecurring = payload.recurring
} else {
// TODO return error
if (!payload.publisherKey || payload.amount <= 0) {
break
}

let amount = parseFloat(payload.amount)
chrome.send('brave_rewards_tip.onTip', [
payload.publisherKey,
amount,
payload.recurring
])
state = { ...state }
state.finished = true
state.currentTipAmount = amount.toFixed(1)
state.currentTipRecurring = payload.recurring

break
}
case types.GET_RECURRING_TIPS:
Expand Down

0 comments on commit 67df180

Please sign in to comment.