Skip to content

Commit

Permalink
Do not start process if rewards is off
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Jun 25, 2020
1 parent 7023925 commit 8c275a4
Show file tree
Hide file tree
Showing 71 changed files with 1,285 additions and 655 deletions.
54 changes: 48 additions & 6 deletions browser/greaselion/greaselion_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#include "brave/components/brave_component_updater/browser/local_data_files_service.h"
#include "brave/browser/brave_rewards/rewards_service_factory.h"
#include "brave/components/greaselion/browser/greaselion_download_service.h"
#include "brave/components/brave_rewards/browser/test/common/rewards_browsertest_response.h"
#include "brave/components/brave_rewards/browser/test/common/rewards_browsertest_util.h"
#include "brave/components/brave_rewards/browser/test/common/rewards_browsertest_network_util.h"
#include "brave/components/greaselion/browser/greaselion_service.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/test/base/ui_test_utils.h"
Expand Down Expand Up @@ -87,7 +90,13 @@ class GreaselionServiceWaiter : public GreaselionService::Observer {

class GreaselionServiceTest : public BaseLocalDataFilesBrowserTest {
public:
GreaselionServiceTest() {}
GreaselionServiceTest(): https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {
brave::RegisterPathProvider();
response_ =
std::make_unique<rewards_browsertest::RewardsBrowserTestResponse>();
base::ScopedAllowBlockingForTesting allow_blocking;
response_->LoadMocks();
}

// BaseLocalDataFilesBrowserTest overrides
const char* test_data_directory() override { return kTestDataDirectory; }
Expand Down Expand Up @@ -120,16 +129,49 @@ class GreaselionServiceTest : public BaseLocalDataFilesBrowserTest {
g_brave_browser_process->greaselion_download_service()->rules()->clear();
}

void SetRewardsEnabled(bool enabled) {
RewardsService* rewards_service =
RewardsServiceFactory::GetForProfile(profile());
rewards_service->SetRewardsMainEnabled(enabled);
void StartRewards() {
// HTTP resolver
https_server_.SetSSLConfig(net::EmbeddedTestServer::CERT_OK);
https_server_.RegisterRequestHandler(
base::BindRepeating(&rewards_browsertest_util::HandleRequest));
ASSERT_TRUE(https_server_.Start());

// Rewards service
brave::RegisterPathProvider();
rewards_service_ = static_cast<brave_rewards::RewardsServiceImpl*>(
brave_rewards::RewardsServiceFactory::GetForProfile(profile()));

// Response mock
rewards_service_->ForTestingSetTestResponseCallback(
base::BindRepeating(
&GreaselionServiceTest::GetTestResponse,
base::Unretained(this)));
rewards_service_->SetLedgerEnvForTesting();

rewards_browsertest_util::EnableRewardsViaCode(browser(), rewards_service_);
GreaselionService* greaselion_service =
GreaselionServiceFactory::GetForBrowserContext(profile());
// wait for the Greaselion service to install all the extensions it creates
// after the rewards service is turned off or on
GreaselionServiceWaiter(greaselion_service).Wait();
}

void GetTestResponse(
const std::string& url,
int32_t method,
int* response_status_code,
std::string* response,
std::map<std::string, std::string>* headers) {
response_->Get(
url,
method,
response_status_code,
response);
}

std::unique_ptr<rewards_browsertest::RewardsBrowserTestResponse> response_;
net::test_server::EmbeddedTestServer https_server_;
brave_rewards::RewardsServiceImpl* rewards_service_;
};

// Ensure the site specific script service properly clears its cache of
Expand Down Expand Up @@ -236,7 +278,7 @@ IN_PROC_BROWSER_TEST_F(GreaselionServiceTest, ScriptInjectionWithPrecondition) {
// rules are active
EXPECT_EQ(title, "OK");

SetRewardsEnabled(true);
StartRewards();
ui_test_utils::NavigateToURL(browser(), url);
contents = browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(content::WaitForLoadStop(contents));
Expand Down
10 changes: 9 additions & 1 deletion browser/ui/webui/brave_rewards_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -543,10 +543,18 @@ void RewardsDOMHandler::OnWalletInitialized(
// ledger::Result::WALLET_CREATED
if (result == 12) {
web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.walletCreated");
} else if (result != 3 && result != 0) {
return;
}

if (result != 3 && result != 0) {
// Report back all errors except when ledger_state is missing
web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.walletCreateFailed");
return;
}

web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards.initialized",
base::Value(result));
}

void RewardsDOMHandler::GetAutoContributeProperties(
Expand Down
27 changes: 0 additions & 27 deletions components/brave_ads/browser/ads_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "brave/common/brave_paths.h"
#include "brave/browser/brave_rewards/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 "brave/components/brave_rewards/common/pref_names.h"
Expand Down Expand Up @@ -115,7 +114,6 @@ bool URLMatches(const std::string& url,

class BraveAdsBrowserTest
: public InProcessBrowserTest,
public brave_rewards::RewardsServiceObserver,
public brave_rewards::RewardsNotificationServiceObserver,
public base::SupportsWeakPtr<BraveAdsBrowserTest> {
public:
Expand Down Expand Up @@ -159,10 +157,6 @@ class BraveAdsBrowserTest
ads_service_ = static_cast<brave_ads::AdsServiceImpl*>(
brave_ads::AdsServiceFactory::GetForProfile(browser_profile));
ASSERT_NE(nullptr, ads_service_);
rewards_service_->AddObserver(this);
if (!rewards_service_->IsWalletInitialized()) {
WaitForWalletInitialization();
}
rewards_service_->SetLedgerEnvForTesting();
}

Expand All @@ -173,24 +167,6 @@ class BraveAdsBrowserTest
InProcessBrowserTest::TearDown();
}

void WaitForWalletInitialization() {
if (wallet_initialized_)
return;
wait_for_wallet_initialization_loop_.reset(new base::RunLoop);
wait_for_wallet_initialization_loop_->Run();
}

void OnWalletInitialized(
brave_rewards::RewardsService* rewards_service,
int32_t result) {
const auto converted_result = static_cast<ledger::Result>(result);
ASSERT_TRUE(converted_result == ledger::Result::WALLET_CREATED ||
converted_result == ledger::Result::LEDGER_OK);
wallet_initialized_ = true;
if (wait_for_wallet_initialization_loop_)
wait_for_wallet_initialization_loop_->Quit();
}

void GetTestDataDir(base::FilePath* test_data_dir) {
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_TRUE(base::PathService::Get(brave::DIR_TEST_DATA, test_data_dir));
Expand Down Expand Up @@ -543,9 +519,6 @@ class BraveAdsBrowserTest
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_wallet_initialization_loop_;
bool wallet_initialized_ = false;

std::string registrarVK_;
std::string verification_;
std::string parameters_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ void RewardsNotificationServiceImpl::DeleteAllNotifications() {
rewards_notifications_.clear();
#if defined(OS_ANDROID)
rewards_notifications_displayed_.clear();
StoreRewardsNotifications();
#endif
StoreRewardsNotifications();
OnAllNotificationsDeleted();
}

Expand Down
Loading

0 comments on commit 8c275a4

Please sign in to comment.