Skip to content

Commit

Permalink
Updates to the latest master, restore walletProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Dec 4, 2019
1 parent c31d418 commit a7054be
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 75 deletions.
13 changes: 13 additions & 0 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,19 @@ ExtensionFunction::ResponseAction BraveRewardsGetPublisherDataFunction::Run() {
return RespondNow(NoArguments());
}

BraveRewardsGetWalletPropertiesFunction::
~BraveRewardsGetWalletPropertiesFunction() = default;

ExtensionFunction::ResponseAction
BraveRewardsGetWalletPropertiesFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
auto* rewards_service = RewardsServiceFactory::GetForProfile(profile);
if (rewards_service) {
rewards_service->FetchWalletProperties();
}
return RespondNow(NoArguments());
}

BraveRewardsGetCurrentReportFunction::~BraveRewardsGetCurrentReportFunction() {
}

Expand Down
10 changes: 10 additions & 0 deletions browser/extensions/api/brave_rewards_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ class BraveRewardsGetPublisherDataFunction : public ExtensionFunction {
ResponseAction Run() override;
};

class BraveRewardsGetWalletPropertiesFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getWalletProperties", UNKNOWN)

protected:
~BraveRewardsGetWalletPropertiesFunction() override;

ResponseAction Run() override;
};

class BraveRewardsGetCurrentReportFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getCurrentReport", UNKNOWN)
Expand Down
36 changes: 6 additions & 30 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,6 @@
"name": "properties",
"type": "object",
"properties": {
"probi": {
"type": "string",
"description": "balance represented in probis"
},
"balance": {
"type": "double",
"description": "balance"
},
"defaultTipChoices": {
"type": "array",
"description": "default tip choices for one-time tips",
Expand All @@ -179,28 +171,6 @@
"type": "number",
"minimum": 0
}
},
"rates": {
"type": "object",
"description": "rates for different currencies",
"properties": {
"BTC": {
"type": "double",
"description": "BTC rate"
},
"ETH": {
"type": "double",
"description": "ETH rate"
},
"USD": {
"type": "double",
"description": "USD rate"
},
"EUR": {
"type": "double",
"description": "EUR rate"
}
}
}
}
}
Expand Down Expand Up @@ -582,6 +552,12 @@
}
]
},
{
"name": "getWalletProperties",
"type": "function",
"description": "Get default values that we get from the server",
"parameters": []
},
{
"name": "getCurrentReport",
"type": "function",
Expand Down
88 changes: 53 additions & 35 deletions components/brave_rewards/browser/rewards_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ bool URLMatches(const std::string& url,
return (url.find(target_url) == 0);
}

enum class ContributionType { OneTimeTip, MonthlyTip };

} // namespace

namespace brave_test_resp {
Expand All @@ -146,6 +148,7 @@ namespace brave_test_resp {
std::string promotion_tokens_;
std::string captcha_;
std::string wallet_properties_;
std::string wallet_properties_defaults_;
std::string uphold_auth_resp_;
std::string uphold_transactions_resp_;
std::string uphold_commit_resp_;
Expand Down Expand Up @@ -273,8 +276,9 @@ class BraveRewardsBrowserTest
verified.c_str());
}

static std::vector<double> GetSiteBannerTipOptions(
std::vector<double> GetSiteBannerTipOptions(
content::WebContents* site_banner) {
WaitForSelector(site_banner, "[data-test-id=amount-wrapper] div span");
auto options = content::EvalJs(
site_banner,
R"(
Expand Down Expand Up @@ -336,7 +340,11 @@ class BraveRewardsBrowserTest
*response = brave_test_resp::verification_;
} else if (URLMatches(url, WALLET_PROPERTIES, PREFIX_V2,
ServerTypes::BALANCE)) {
*response = brave_test_resp::wallet_properties_;
if (show_defaults_in_properties_) {
*response = brave_test_resp::wallet_properties_defaults_;
} else {
*response = brave_test_resp::wallet_properties_;
}
} else if (URLMatches(url, "/promotions?", PREFIX_V1,
ServerTypes::kPromotion)) {
*response = brave_test_resp::promotions_;
Expand All @@ -356,7 +364,9 @@ class BraveRewardsBrowserTest
*response =
"["
"[\"bumpsmack.com\",\"publisher_verified\",false,\"address1\",{}],"
"[\"duckduckgo.com\",\"wallet_connected\",false,\"address2\",{}]"
"[\"duckduckgo.com\",\"wallet_connected\",false,\"address2\",{}],"
"[\"laurenwags.github.io\",\"wallet_connected\",false,\"address2\","
"{\"donationAmounts\": [5,10,20]}]"
"]";
} else {
*response =
Expand All @@ -366,7 +376,9 @@ class BraveRewardsBrowserTest
"[\"3zsistemi.si\",\"wallet_connected\",false,\"address3\",{}],"
"[\"site1.com\",\"wallet_connected\",false,\"address4\",{}],"
"[\"site2.com\",\"wallet_connected\",false,\"address5\",{}],"
"[\"site3.com\",\"wallet_connected\",false,\"address6\",{}]"
"[\"site3.com\",\"wallet_connected\",false,\"address6\",{}],"
"[\"laurenwags.github.io\",\"wallet_connected\",false,\"address2\","
"{\"donationAmounts\": [5,10,20]}]"
"]";
}
} else if (base::StartsWith(
Expand Down Expand Up @@ -872,6 +884,9 @@ class BraveRewardsBrowserTest
ASSERT_TRUE(
base::ReadFileToString(path.AppendASCII("wallet_properties_resp.json"),
&brave_test_resp::wallet_properties_));
ASSERT_TRUE(base::ReadFileToString(
path.AppendASCII("wallet_properties_resp_defaults.json"),
&brave_test_resp::wallet_properties_defaults_));
ASSERT_TRUE(base::ReadFileToString(
path.AppendASCII("uphold_auth_resp.json"),
&brave_test_resp::uphold_auth_resp_));
Expand Down Expand Up @@ -1225,14 +1240,14 @@ class BraveRewardsBrowserTest

content::WebContents* OpenSiteBanner(ContributionType banner_type) const {
content::WebContents* popup_contents = OpenRewardsPopup();
ASSERT_TRUE(popup_contents);

// Construct an observer to wait for the site banner to load.
content::WindowedNotificationObserver site_banner_observer(
content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
content::NotificationService::AllSources());

const std::string buttonSelector = banner_type == ContributionType::MonthlyTip
const std::string buttonSelector =
banner_type == ContributionType::MonthlyTip
? "[type='tip-monthly']"
: "[type='tip']";

Expand Down Expand Up @@ -1325,14 +1340,14 @@ class BraveRewardsBrowserTest

// Signal that direct tip was made and update wallet with new
// balance
if (!monthly && !should_contribute) {
if (type == ContributionType::OneTimeTip && !should_contribute) {
UpdateContributionBalance(amount, should_contribute);
}

// Wait for thank you banner to load
ASSERT_TRUE(WaitForLoadStop(site_banner_contents));

const std::string confirmationText = monthly
const std::string confirmationText = type == ContributionType::MonthlyTip
? "Monthly contribution has been set!"
: "Tip sent!";

Expand Down Expand Up @@ -1623,8 +1638,6 @@ class BraveRewardsBrowserTest
MOCK_METHOD1(OnGetReconcileTime, void(int32_t));
MOCK_METHOD1(OnGetShortRetries, void(bool));

enum class ContributionType { OneTimeTip, MonthlyTip };

std::unique_ptr<net::EmbeddedTestServer> https_server_;

brave_rewards::RewardsServiceImpl* rewards_service_;
Expand Down Expand Up @@ -1673,6 +1686,7 @@ class BraveRewardsBrowserTest

bool last_publisher_added_ = false;
bool alter_publisher_list_ = false;
bool show_defaults_in_properties_ = false;
bool request_made_ = false;
double balance_ = 0;
double reconciled_tip_total_ = 0;
Expand All @@ -1682,8 +1696,6 @@ class BraveRewardsBrowserTest
const std::string external_wallet_address_ =
"abe5f454-fedd-4ea9-9203-470ae7315bb3";
bool first_url_ac_called_ = false;
std::vector<double> default_tip_choices_;
std::vector<double> default_monthly_tip_choices_;
};

IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, RenderWelcome) {
Expand Down Expand Up @@ -2309,7 +2321,6 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest,
ClaimPromotion(use_panel);

// Tip verified publisher
const bool monthly = true;
TipPublisher("brave.com", ContributionType::MonthlyTip, false);

// Stop observing the Rewards service
Expand Down Expand Up @@ -2771,11 +2782,12 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest,
}

IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, RewardsPanelDefaultTipChoices) {
show_defaults_in_properties_ = true;
rewards_service_->AddObserver(this);
EnableRewards();

bool use_panel = true;
ClaimGrant(use_panel);
ClaimPromotion(use_panel);

GURL url = https_server()->GetURL("3zsistemi.si", "/index.html");
ui_test_utils::NavigateToURLWithDisposition(
Expand All @@ -2786,18 +2798,15 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, RewardsPanelDefaultTipChoices) {
bool monthly = true;
TipViaCode("3zsistemi.si", 10, monthly, ledger::PublisherStatus::VERIFIED);

// Set the default monthly tip choices, and verify that those are the options
// in the rewards popup (along with 0, which disables the contribution).
default_monthly_tip_choices_ = { 10, 20, 50 };

content::WebContents* popup = OpenRewardsPopup();
std::vector<double> tip_options = GetRewardsPopupTipOptions(popup);
const auto tip_options = GetRewardsPopupTipOptions(popup);
ASSERT_EQ(tip_options, std::vector<double>({ 0, 10, 20, 50 }));

rewards_service_->RemoveObserver(this);
}

IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, SiteBannerDefaultTipChoices) {
show_defaults_in_properties_ = true;
rewards_service_->AddObserver(this);
EnableRewards();

Expand All @@ -2806,25 +2815,34 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, SiteBannerDefaultTipChoices) {
browser(), url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);

// The UI defaults to [1, 5, 10] if no default is provided.
content::WebContents* site_banner = OpenSiteBanner(ContributionType::OneTimeTip);
std::vector<double> tip_options = GetSiteBannerTipOptions(site_banner);
ASSERT_EQ(tip_options, std::vector<double>({ 1, 5, 10 }));

default_tip_choices_ = { 5, 10, 20 };
site_banner = OpenSiteBanner(ContributionType::OneTimeTip);
tip_options = GetSiteBannerTipOptions(site_banner);
ASSERT_EQ(tip_options, default_tip_choices_);
content::WebContents* site_banner =
OpenSiteBanner(ContributionType::OneTimeTip);
auto tip_options = GetSiteBannerTipOptions(site_banner);
ASSERT_EQ(tip_options, std::vector<double>({ 5, 10, 20 }));

// The UI defaults to [1, 5, 10] if no default is provided.
site_banner = OpenSiteBanner(ContributionType::MonthlyTip);
tip_options = GetSiteBannerTipOptions(site_banner);
ASSERT_EQ(tip_options, std::vector<double>({ 1, 5, 10 }));
ASSERT_EQ(tip_options, std::vector<double>({ 10, 20, 50 }));

default_monthly_tip_choices_ = { 25, 50, 100 };
site_banner = OpenSiteBanner(ContributionType::MonthlyTip);
tip_options = GetSiteBannerTipOptions(site_banner);
ASSERT_EQ(tip_options, default_monthly_tip_choices_);
rewards_service_->RemoveObserver(this);
}

IN_PROC_BROWSER_TEST_F(
BraveRewardsBrowserTest,
SiteBannerDefaultPublisherAmounts) {
show_defaults_in_properties_ = true;
rewards_service_->AddObserver(this);
EnableRewards();

GURL url = https_server()->GetURL("laurenwags.github.io", "/index.html");
ui_test_utils::NavigateToURLWithDisposition(
browser(), url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);

content::WebContents* site_banner =
OpenSiteBanner(ContributionType::OneTimeTip);
const auto tip_options = GetSiteBannerTipOptions(site_banner);
ASSERT_EQ(tip_options, std::vector<double>({ 5, 10, 20 }));

rewards_service_->RemoveObserver(this);
}
Expand Down Expand Up @@ -2941,7 +2959,7 @@ IN_PROC_BROWSER_TEST_F(
ClaimPromotion(use_panel);

// Tip verified publisher
TipPublisher("bumpsmack.com",ContributionType::OneTimeTip, true);
TipPublisher("bumpsmack.com", ContributionType::OneTimeTip, true);

// Stop observing the Rewards service
rewards_service_->RemoveObserver(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export const onPublisherData = (windowId: number, publisher: RewardsExtension.Pu
publisher
})

export const onWalletProperties = (properties: RewardsExtension.WalletProperties) => action(types.ON_WALLET_PROPERTIES, {
properties
})

export const getCurrentReport = () => action(types.GET_CURRENT_REPORT, {})

export const onCurrentReport = (properties: RewardsExtension.Report) => action(types.ON_CURRENT_REPORT, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ chrome.braveRewards.onPromotionFinish.addListener((result: RewardsExtension.Resu
rewardsPanelActions.promotionFinished(result, promotion)
})

chrome.braveRewards.onWalletProperties.addListener((properties: RewardsExtension.WalletProperties) => {
rewardsPanelActions.onWalletProperties(properties)
})

// Fetch initial data required to refresh state, keeping in mind
// that the extension process be restarted at any time.
// TODO(petemill): Move to initializer function or single 'init' action.
Expand All @@ -126,5 +130,6 @@ chrome.braveRewards.getRewardsMainEnabled((enabledMain: boolean) => {
chrome.braveRewards.getAllNotifications((list: RewardsExtension.Notification[]) => {
rewardsPanelActions.onAllNotifications(list)
})
chrome.braveRewards.getWalletProperties()
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,18 @@ export const rewardsPanelReducer = (state: RewardsExtension.State | undefined, a
} else if (payload.result === RewardsExtension.Result.WALLET_CREATED) {
state.walletCreated = true
}
break
}
case types.ON_WALLET_PROPERTIES: {
state = {
...state,
walletCreated: true,
walletCreateFailed: false,
walletCreating: false,
walletCorrupted: false,
walletProperties: payload.properties
}
break
}
}
return state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,14 +512,15 @@ export class Panel extends React.Component<Props, State> {
publisherAmounts = tipAmounts[publisherKey]
}

const walletAmounts = walletProperties.defaultMonthlyTipChoices

// Prefer the publisher amounts, then the wallet's defaults. Fall back to defaultTipAmounts.
let initialAmounts = this.defaultTipAmounts
if (publisherAmounts) {
initialAmounts = publisherAmounts
} else if (walletAmounts.length) {
initialAmounts = walletAmounts
} else if (walletProperties) {
const walletAmounts = walletProperties.defaultMonthlyTipChoices
if (walletAmounts.length) {
initialAmounts = walletAmounts
}
}

const amounts = [0, ...initialAmounts]
Expand Down
Loading

0 comments on commit a7054be

Please sign in to comment.