Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Sadler committed May 14, 2019
1 parent e6a8acb commit 80cea06
Show file tree
Hide file tree
Showing 12 changed files with 618 additions and 39 deletions.
285 changes: 251 additions & 34 deletions components/brave_rewards/browser/rewards_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <memory>

#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/string_split.h"
#include "base/memory/weak_ptr.h"
#include "bat/ledger/internal/bat_helper.h"
#include "bat/ledger/internal/static_values.h"
#include "bat/ledger/ledger.h"
Expand All @@ -16,6 +19,8 @@
#include "brave/components/brave_rewards/browser/rewards_service_factory.h"
#include "brave/components/brave_rewards/browser/rewards_service_impl.h"
#include "brave/components/brave_rewards/browser/rewards_service_observer.h"
#include "brave/components/brave_rewards/browser/rewards_notification_service_impl.h" // NOLINT
#include "brave/components/brave_rewards/browser/rewards_notification_service_observer.h" // NOLINT
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/common/chrome_paths.h"
Expand Down Expand Up @@ -72,8 +77,11 @@ namespace brave_test_resp {
std::string surveyor_voting_credential_;
} // namespace brave_test_resp

class BraveRewardsBrowserTest : public InProcessBrowserTest,
public brave_rewards::RewardsServiceObserver {
class BraveRewardsBrowserTest :
public InProcessBrowserTest,
public brave_rewards::RewardsServiceObserver,
public brave_rewards::RewardsNotificationServiceObserver,
public base::SupportsWeakPtr<BraveRewardsBrowserTest> {
public:
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
Expand Down Expand Up @@ -210,6 +218,14 @@ class BraveRewardsBrowserTest : public InProcessBrowserTest,
wait_for_reconcile_completed_loop_->Run();
}

void WaitForInsufficientFundsNotification() {
if (insufficient_notification_would_have_already_shown_) {
return;
}
wait_for_insufficient_notification_loop_.reset(new base::RunLoop);
wait_for_insufficient_notification_loop_->Run();
}

void GetReconcileTime() {
rewards_service()->GetReconcileTime(
base::Bind(&BraveRewardsBrowserTest::OnGetReconcileTime,
Expand Down Expand Up @@ -444,7 +460,24 @@ class BraveRewardsBrowserTest : public InProcessBrowserTest,
EXPECT_NE(js_result.ExtractString().find("30.0 BAT"), std::string::npos);
}

void VisitPublisher(const std::string& publisher, bool verified) {
/*
* When adding sites to ac for tests other than table existence testing,
* use index to keep dom element comparisons from failing, Don't use an
* index if just adding single Publisher.
*
* example:
* For ac table testing:
* `VisitPublisher("my_publisher", true);`
*
* For testing that requires multiple publishers to be populated:
* `VisitPublisher("my_publisher1", true);` // 0 is default index
* `VisitPublisher("my_publisher2", true, 1);`
* `VisitPublisher("my_publisher3", false, 2);`
* */
void VisitPublisher(const std::string& publisher,
bool verified,
int32_t index = 0,
int32_t last_add = false) {
GURL url = embedded_test_server()->GetURL(publisher, "/index.html");
ui_test_utils::NavigateToURLWithDisposition(
browser(), url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
Expand All @@ -462,37 +495,42 @@ class BraveRewardsBrowserTest : public InProcessBrowserTest,
// Wait for publisher list normalization
WaitForPublisherListNormalized();

// Make sure site appears in auto-contribute table
content::EvalJsResult js_result = EvalJs(
contents(),
"const delay = t => new Promise(resolve => setTimeout(resolve, t));"
"delay(1000).then(() => "
" document.querySelector(\"[data-test-id='autoContribute']\")."
" getElementsByTagName('a')[0].innerText);",
content::EXECUTE_SCRIPT_DEFAULT_OPTIONS,
content::ISOLATED_WORLD_ID_CONTENT_END);
EXPECT_STREQ(js_result.ExtractString().c_str(), publisher.c_str());

if (verified) {
// A verified site has two images associated with it, the site's
// favicon and the verified icon
content::EvalJsResult js_result =
EvalJs(contents(),
"document.querySelector(\"[data-test-id='autoContribute']\")."
" getElementsByTagName('svg').length === 2;",
content::EXECUTE_SCRIPT_DEFAULT_OPTIONS,
content::ISOLATED_WORLD_ID_CONTENT_END);
EXPECT_TRUE(js_result.ExtractBool());
} else {
// An unverified site has one image associated with it, the site's
// favicon
content::EvalJsResult js_result =
EvalJs(contents(),
"document.querySelector(\"[data-test-id='autoContribute']\")."
" getElementsByTagName('svg').length === 1;",
content::EXECUTE_SCRIPT_DEFAULT_OPTIONS,
content::ISOLATED_WORLD_ID_CONTENT_END);
EXPECT_TRUE(js_result.ExtractBool());
if (index == 0) {
// Make sure site appears in auto-contribute table
content::EvalJsResult js_result = EvalJs(
contents(),
"const delay = t => new Promise(resolve => setTimeout(resolve, t));"
"delay(1000).then(() => "
" document.querySelector(\"[data-test-id='autoContribute']\")."
" getElementsByTagName('a')[0].innerText);",
content::EXECUTE_SCRIPT_DEFAULT_OPTIONS,
content::ISOLATED_WORLD_ID_CONTENT_END);
EXPECT_STREQ(js_result.ExtractString().c_str(), publisher.c_str());

if (verified) {
// A verified site has two images associated with it, the site's
// favicon and the verified icon
content::EvalJsResult js_result =
EvalJs(contents(),
"document.querySelector(\"[data-test-id='autoContribute']\")."
" getElementsByTagName('svg').length === 2;",
content::EXECUTE_SCRIPT_DEFAULT_OPTIONS,
content::ISOLATED_WORLD_ID_CONTENT_END);
EXPECT_TRUE(js_result.ExtractBool());
} else {
// An unverified site has one image associated with it, the site's
// favicon
content::EvalJsResult js_result =
EvalJs(contents(),
"document.querySelector(\"[data-test-id='autoContribute']\")."
" getElementsByTagName('svg').length === 1;",
content::EXECUTE_SCRIPT_DEFAULT_OPTIONS,
content::ISOLATED_WORLD_ID_CONTENT_END);
EXPECT_TRUE(js_result.ExtractBool());
}
}
if (last_add) {
last_publisher_added_ = true;
}
}

Expand Down Expand Up @@ -777,6 +815,46 @@ class BraveRewardsBrowserTest : public InProcessBrowserTest,
wait_for_reconcile_completed_loop_->Quit();
}

void OnNotificationAdded(
brave_rewards::RewardsNotificationService* rewards_notification_service,
const brave_rewards::RewardsNotificationService::RewardsNotification&
notification) {
const brave_rewards::RewardsNotificationService::RewardsNotificationsMap&
notifications = rewards_notification_service->GetAllNotifications();
for (const auto& notification : notifications) {
if (notification.second.type_ ==
brave_rewards::RewardsNotificationService::RewardsNotificationType
::REWARDS_NOTIFICATION_INSUFFICIENT_FUNDS) {
insufficient_notification_would_have_already_shown_ = true;
if (wait_for_insufficient_notification_loop_) {
wait_for_insufficient_notification_loop_->Quit();
}
}
}
}

/**
* When using notification observer for insufficient funds, tests will fail
* for sufficient funds because observer will never be called for
* notification. Use this as callback to know when we come back with
* sufficient funds to prevent inf loop
* */
void ShowNotificationAddFundsForTesting(bool sufficient) {
if (sufficient) {
insufficient_notification_would_have_already_shown_ = true;
if (wait_for_insufficient_notification_loop_) {
wait_for_insufficient_notification_loop_->Quit();
}
}
}

void CheckInsufficientFundsForTesting() {
rewards_service_->MaybeShowNotificationAddFundsForTesting(
base::BindOnce(
&BraveRewardsBrowserTest::ShowNotificationAddFundsForTesting,
AsWeakPtr()));
}

MOCK_METHOD1(OnGetProduction, void(bool));
MOCK_METHOD1(OnGetDebug, void(bool));
MOCK_METHOD1(OnGetReconcileTime, void(int32_t));
Expand Down Expand Up @@ -805,8 +883,12 @@ class BraveRewardsBrowserTest : public InProcessBrowserTest,
bool reconcile_completed_ = false;
unsigned int reconcile_status_ = ledger::LEDGER_ERROR;

std::unique_ptr<base::RunLoop> wait_for_insufficient_notification_loop_;
bool insufficient_notification_would_have_already_shown_ = false;

bool contribution_made_ = false;
bool tip_made = false;
bool last_publisher_added_ = false;
};

IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, RenderWelcome) {
Expand Down Expand Up @@ -1361,3 +1443,138 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest,
// Stop observing the Rewards service
rewards_service_->RemoveObserver(this);
}

IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest,
InsufficientNotificationForVerifiedsZeroAmountZeroPublishers) {
rewards_service_->GetNotificationService()->AddObserver(this);
EnableRewards();
CheckInsufficientFundsForTesting();
WaitForInsufficientFundsNotification();
const brave_rewards::RewardsNotificationService::RewardsNotificationsMap&
notifications = rewards_service_->GetAllNotifications();

if (notifications.empty()) {
SUCCEED();
}
bool notification_shown = false;
for (const auto& notification : notifications) {
if (notification.second.type_ ==
brave_rewards::RewardsNotificationService::RewardsNotificationType
::REWARDS_NOTIFICATION_INSUFFICIENT_FUNDS) {
notification_shown = true;
}
}
EXPECT_FALSE(notification_shown);
}

IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest,
InsufficientNotificationForVerifiedsDefaultAmount) {
rewards_service_->AddObserver(this);
rewards_service_->GetNotificationService()->AddObserver(this);
EnableRewards();
// Claim grant using panel
const bool use_panel = true;
ClaimGrant(use_panel);

// Visit publishers
while (!last_publisher_added_) {
const bool verified = true;
VisitPublisher("duckduckgo.com", verified);
VisitPublisher("bumpsmack.com", verified, 1);
VisitPublisher("google.com", !verified, 2, true);
}

CheckInsufficientFundsForTesting();
WaitForInsufficientFundsNotification();
const brave_rewards::RewardsNotificationService::RewardsNotificationsMap&
notifications = rewards_service_->GetAllNotifications();

if (notifications.empty()) {
SUCCEED();
}
bool notification_shown = false;
for (const auto& notification : notifications) {
if (notification.second.type_ ==
brave_rewards::RewardsNotificationService::RewardsNotificationType
::REWARDS_NOTIFICATION_INSUFFICIENT_FUNDS) {
notification_shown = true;
}
}
EXPECT_FALSE(notification_shown);
}

IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest,
InsufficientNotificationForVerifiedsSufficientAmount) {
rewards_service_->AddObserver(this);
rewards_service_->GetNotificationService()->AddObserver(this);

EnableRewards();
// Claim grant using panel
const bool use_panel = true;
ClaimGrant(use_panel);

// Visit publishers
while (!last_publisher_added_) {
const bool verified = true;
VisitPublisher("duckduckgo.com", verified);
VisitPublisher("bumpsmack.com", verified, 1);
VisitPublisher("google.com", !verified, 2, true);
}

rewards_service_->SetContributionAmount(50.0);

CheckInsufficientFundsForTesting();
WaitForInsufficientFundsNotification();
const brave_rewards::RewardsNotificationService::RewardsNotificationsMap&
notifications = rewards_service_->GetAllNotifications();
if (notifications.empty()) {
SUCCEED();
}
bool notification_shown = false;
for (const auto& notification : notifications) {
if (notification.second.type_ ==
brave_rewards::RewardsNotificationService::RewardsNotificationType
::REWARDS_NOTIFICATION_INSUFFICIENT_FUNDS) {
notification_shown = true;
}
}
EXPECT_FALSE(notification_shown);
}

IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest,
InsufficientNotificationForVerifiedsInsufficientAmount) {
rewards_service_->AddObserver(this);
rewards_service_->GetNotificationService()->AddObserver(this);
EnableRewards();
// Claim grant using panel
const bool use_panel = true;
ClaimGrant(use_panel);

// Visit publishers
while (!last_publisher_added_) {
const bool verified = true;
VisitPublisher("duckduckgo.com", verified);
VisitPublisher("bumpsmack.com", verified, 1);
VisitPublisher("google.com", !verified, 2, true);
}
rewards_service_->SetContributionAmount(100.0);

rewards_service_->CheckInsufficientFundsForTesting();
WaitForInsufficientFundsNotification();
const brave_rewards::RewardsNotificationService::RewardsNotificationsMap&
notifications = rewards_service_->GetAllNotifications();

if (notifications.empty()) {
FAIL() << "Should see Insufficient Funds notification";
}
bool notification_shown = false;
for (const auto& notification : notifications) {
if (notification.second.type_ ==
brave_rewards::RewardsNotificationService::RewardsNotificationType
::REWARDS_NOTIFICATION_INSUFFICIENT_FUNDS) {
notification_shown = true;
}
}
EXPECT_TRUE(notification_shown);
}

9 changes: 9 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2480,6 +2480,11 @@ void RewardsServiceImpl::MaybeShowNotificationAddFunds() {
AsWeakPtr()));
}

void RewardsServiceImpl::MaybeShowNotificationAddFundsForTesting(
base::OnceCallback<void(bool)> callback) {
bat_ledger_->HasSufficientBalanceToReconcile(std::move(callback));
}

bool RewardsServiceImpl::ShouldShowNotificationAddFunds() const {
base::Time next_time =
profile_->GetPrefs()->GetTime(prefs::kRewardsAddFundsNotification);
Expand Down Expand Up @@ -2669,6 +2674,10 @@ void RewardsServiceImpl::StartAutoContributeForTest() {
bat_ledger_->StartAutoContribute();
}

void RewardsServiceImpl::CheckInsufficientFundsForTesting() {
MaybeShowNotificationAddFunds();
}

void RewardsServiceImpl::GetProduction(const GetProductionCallback& callback) {
bat_ledger_service_->GetProduction(callback);
}
Expand Down
Loading

0 comments on commit 80cea06

Please sign in to comment.