Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds development env #3294

Merged
merged 5 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ bool AdsServiceImpl::StartService() {
bat_ads_service_.set_connection_error_handler(
base::Bind(&AdsServiceImpl::MaybeStart, AsWeakPtr(), true));

UpdateIsProductionFlag();
SetEnvironment();
UpdateIsDebugFlag();
UpdateIsTestingFlag();

Expand Down Expand Up @@ -748,31 +748,32 @@ void AdsServiceImpl::OnEnsureBaseDirectoryExists(
MaybeShowMyFirstAdNotification();
}

void AdsServiceImpl::UpdateIsProductionFlag() {
auto is_production = IsProduction();
bat_ads_service_->SetProduction(is_production, base::NullCallback());
}

bool AdsServiceImpl::IsProduction() const {
#if defined(OS_ANDROID)
void AdsServiceImpl::SetEnvironment() {
ads::Environment environment;

#if defined(OFFICIAL_BUILD)
return !GetBooleanPref(brave_rewards::prefs::kUseRewardsStagingServer);
environment = ads::Environment::PRODUCTION;
#else
return false;
environment = ads::Environment::STAGING;
#endif

#if defined(OS_ANDROID)
if (GetBooleanPref(brave_rewards::prefs::kUseRewardsStagingServer)) {
environment = ads::Environment::STAGING;
}
#else

const auto& command_line = *base::CommandLine::ForCurrentProcess();

#if defined(OFFICIAL_BUILD)
return !command_line.HasSwitch(switches::kStaging);
#else
return command_line.HasSwitch(switches::kProduction);
if (command_line.HasSwitch(switches::kProduction)) {
environment = ads::Environment::PRODUCTION;
} else if (command_line.HasSwitch(switches::kStaging)) {
environment = ads::Environment::STAGING;
} else if (command_line.HasSwitch(switches::kDevelopment)) {
environment = ads::Environment::DEVELOPMENT;
}
#endif

#endif
bat_ads_service_->SetEnvironment(environment, base::NullCallback());
}

void AdsServiceImpl::UpdateIsDebugFlag() {
Expand Down
4 changes: 2 additions & 2 deletions components/brave_ads/browser/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ class AdsServiceImpl : public AdsService,
void OnEnsureBaseDirectoryExists(
const bool success);

void UpdateIsProductionFlag();
bool IsProduction() const;
void SetEnvironment();

void UpdateIsDebugFlag();
bool IsDebug() const;
void UpdateIsTestingFlag();
Expand Down
7 changes: 6 additions & 1 deletion components/brave_ads/common/switches.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
#include "brave/components/brave_ads/common/switches.h"

namespace brave_ads {

namespace switches {
const char kStaging[] = "brave-ads-staging";

const char kProduction[] = "brave-ads-production";
const char kStaging[] = "brave-ads-staging";
const char kDevelopment[] = "brave-ads-development";
const char kDebug[] = "brave-ads-debug";
const char kTesting[] = "brave-ads-testing";

} // namespace switches

} // namespace brave_ads
3 changes: 2 additions & 1 deletion components/brave_ads/common/switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ namespace brave_ads {

namespace switches {

extern const char kStaging[];
extern const char kProduction[];
extern const char kStaging[];
extern const char kDevelopment[];
extern const char kDebug[];
extern const char kTesting[];

Expand Down
88 changes: 64 additions & 24 deletions components/brave_rewards/browser/rewards_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,9 @@ class BraveRewardsBrowserTest :
base::Unretained(this)));
}

void GetProduction() {
rewards_service()->GetProduction(
base::Bind(&BraveRewardsBrowserTest::OnGetProduction,
void GetEnvironment() {
rewards_service()->GetEnvironment(
base::Bind(&BraveRewardsBrowserTest::OnGetEnvironment,
base::Unretained(this)));
}

Expand Down Expand Up @@ -1232,7 +1232,7 @@ class BraveRewardsBrowserTest :

const std::vector<double> tip_amounts_ = {1.0, 5.0, 10.0};

MOCK_METHOD1(OnGetProduction, void(bool));
MOCK_METHOD1(OnGetEnvironment, void(ledger::Environment));
MOCK_METHOD1(OnGetDebug, void(bool));
MOCK_METHOD1(OnGetReconcileTime, void(int32_t));
MOCK_METHOD1(OnGetShortRetries, void(bool));
Expand Down Expand Up @@ -1430,39 +1430,40 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, ActivateSettingsModal) {

IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, HandleFlagsSingleArg) {
testing::InSequence s;
// SetProduction(true)
EXPECT_CALL(*this, OnGetProduction(true));
// SetEnvironment(ledger::Environment::PRODUCTION)
EXPECT_CALL(*this, OnGetEnvironment(ledger::Environment::PRODUCTION));
// Staging - true and 1
EXPECT_CALL(*this, OnGetProduction(false)).Times(2);
EXPECT_CALL(*this, OnGetEnvironment(ledger::Environment::STAGING)).Times(2);
// Staging - false and random
EXPECT_CALL(*this, OnGetProduction(true)).Times(2);
EXPECT_CALL(*this, OnGetEnvironment(
ledger::Environment::PRODUCTION)).Times(2);

rewards_service()->SetProduction(true);
GetProduction();
rewards_service()->SetEnvironment(ledger::Environment::PRODUCTION);
GetEnvironment();
RunUntilIdle();

// Staging - true
rewards_service()->SetProduction(true);
rewards_service()->SetEnvironment(ledger::Environment::PRODUCTION);
rewards_service()->HandleFlags("staging=true");
GetProduction();
GetEnvironment();
RunUntilIdle();

// Staging - 1
rewards_service()->SetProduction(true);
rewards_service()->SetEnvironment(ledger::Environment::PRODUCTION);
rewards_service()->HandleFlags("staging=1");
GetProduction();
GetEnvironment();
RunUntilIdle();

// Staging - false
rewards_service()->SetProduction(false);
rewards_service()->SetEnvironment(ledger::Environment::STAGING);
rewards_service()->HandleFlags("staging=false");
GetProduction();
GetEnvironment();
RunUntilIdle();

// Staging - random
rewards_service()->SetProduction(false);
rewards_service()->SetEnvironment(ledger::Environment::STAGING);
rewards_service()->HandleFlags("staging=werwe");
GetProduction();
GetEnvironment();
RunUntilIdle();

// SetDebug(true)
Expand Down Expand Up @@ -1500,6 +1501,45 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, HandleFlagsSingleArg) {
GetDebug();
RunUntilIdle();

// SetEnvironment(ledger::Environment::PRODUCTION)
EXPECT_CALL(*this, OnGetEnvironment(ledger::Environment::PRODUCTION));
// Development - true and 1
EXPECT_CALL(
*this,
OnGetEnvironment(ledger::Environment::DEVELOPMENT)).Times(2);
// Development - false and random
EXPECT_CALL(
*this,
OnGetEnvironment(ledger::Environment::PRODUCTION)).Times(2);

rewards_service()->SetEnvironment(ledger::Environment::PRODUCTION);
GetEnvironment();
RunUntilIdle();

// Development - true
rewards_service()->SetEnvironment(ledger::Environment::PRODUCTION);
rewards_service()->HandleFlags("development=true");
GetEnvironment();
RunUntilIdle();

// Development - 1
rewards_service()->SetEnvironment(ledger::Environment::PRODUCTION);
rewards_service()->HandleFlags("development=1");
GetEnvironment();
RunUntilIdle();

// Development - false
rewards_service()->SetEnvironment(ledger::Environment::PRODUCTION);
rewards_service()->HandleFlags("development=false");
GetEnvironment();
RunUntilIdle();

// Development - random
rewards_service()->SetEnvironment(ledger::Environment::PRODUCTION);
rewards_service()->HandleFlags("development=werwe");
GetEnvironment();
RunUntilIdle();

// positive number
EXPECT_CALL(*this, OnGetReconcileTime(10));
// negative number and string
Expand Down Expand Up @@ -1540,12 +1580,12 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, HandleFlagsSingleArg) {
}

IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, HandleFlagsMultipleFlags) {
EXPECT_CALL(*this, OnGetProduction(false));
EXPECT_CALL(*this, OnGetEnvironment(ledger::Environment::STAGING));
EXPECT_CALL(*this, OnGetDebug(true));
EXPECT_CALL(*this, OnGetReconcileTime(10));
EXPECT_CALL(*this, OnGetShortRetries(true));

rewards_service()->SetProduction(true);
rewards_service()->SetEnvironment(ledger::Environment::PRODUCTION);
rewards_service()->SetDebug(true);
rewards_service()->SetReconcileTime(0);
rewards_service()->SetShortRetries(false);
Expand All @@ -1555,18 +1595,18 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, HandleFlagsMultipleFlags) {

GetReconcileTime();
GetShortRetries();
GetProduction();
GetEnvironment();
GetDebug();
RunUntilIdle();
}

IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, HandleFlagsWrongInput) {
EXPECT_CALL(*this, OnGetProduction(true));
EXPECT_CALL(*this, OnGetEnvironment(ledger::Environment::PRODUCTION));
EXPECT_CALL(*this, OnGetDebug(false));
EXPECT_CALL(*this, OnGetReconcileTime(0));
EXPECT_CALL(*this, OnGetShortRetries(false));

rewards_service()->SetProduction(true);
rewards_service()->SetEnvironment(ledger::Environment::PRODUCTION);
rewards_service()->SetDebug(false);
rewards_service()->SetReconcileTime(0);
rewards_service()->SetShortRetries(false);
Expand All @@ -1577,7 +1617,7 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, HandleFlagsWrongInput) {
GetReconcileTime();
GetShortRetries();
GetDebug();
GetProduction();
GetEnvironment();
RunUntilIdle();
}

Expand Down
62 changes: 41 additions & 21 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -502,16 +502,14 @@ void RewardsServiceImpl::StartLedger() {
bat_ledger_service_.set_connection_error_handler(
base::Bind(&RewardsServiceImpl::ConnectionClosed, AsWeakPtr()));

bool is_production = true;
ledger::Environment environment = ledger::Environment::STAGING;
// Environment
#if defined(OFFICIAL_BUILD) && defined(OS_ANDROID)
is_production = !ShouldUseStagingServerForAndroid();
environment = GetServerEnvironmentForAndroid();
#elif defined(OFFICIAL_BUILD)
is_production = true;
#else
is_production = false;
environment = ledger::Environment::PRODUCTION;
#endif
SetProduction(is_production);
SetEnvironment(environment);

SetDebug(false);

Expand Down Expand Up @@ -2861,16 +2859,16 @@ void RewardsServiceImpl::HandleFlags(const std::string& options) {
}

if (name == "staging") {
bool is_production;
ledger::Environment environment;
std::string lower = base::ToLowerASCII(value);

if (lower == "true" || lower == "1") {
is_production = false;
environment = ledger::Environment::STAGING;
} else {
is_production = true;
environment = ledger::Environment::PRODUCTION;
}

SetProduction(is_production);
SetEnvironment(environment);
continue;
}

Expand Down Expand Up @@ -2925,6 +2923,18 @@ void RewardsServiceImpl::HandleFlags(const std::string& options) {
SaveExternalWallet(ledger::kWalletUphold, std::move(uphold));
continue;
}

if (name == "development") {
ledger::Environment environment;
std::string lower = base::ToLowerASCII(value);

if (lower == "true" || lower == "1") {
environment = ledger::Environment::DEVELOPMENT;
SetEnvironment(environment);
}

continue;
}
}
}

Expand Down Expand Up @@ -2992,18 +3002,23 @@ void RewardsServiceImpl::SetLedgerEnvForTesting() {
// this is needed because we are using braveledger_bat_helper::buildURL
// directly in BraveRewardsBrowserTest
#if defined(OFFICIAL_BUILD)
ledger::is_production = true;
ledger::_environment = ledger::Environment::PRODUCTION;
#else
ledger::is_production = false;
ledger::_environment = ledger::Environment::STAGING;
#endif
}

void RewardsServiceImpl::StartMonthlyContributionForTest() {
bat_ledger_->StartMonthlyContribution();
}

void RewardsServiceImpl::GetProduction(const GetProductionCallback& callback) {
bat_ledger_service_->GetProduction(callback);
void RewardsServiceImpl::CheckInsufficientFundsForTesting() {
MaybeShowNotificationAddFunds();
}

void RewardsServiceImpl::GetEnvironment(
const GetEnvironmentCallback& callback) {
bat_ledger_service_->GetEnvironment(callback);
}

void RewardsServiceImpl::GetDebug(const GetDebugCallback& callback) {
Expand All @@ -3020,8 +3035,8 @@ void RewardsServiceImpl::GetShortRetries(
bat_ledger_service_->GetShortRetries(callback);
}

void RewardsServiceImpl::SetProduction(bool production) {
bat_ledger_service_->SetProduction(production);
void RewardsServiceImpl::SetEnvironment(ledger::Environment environment) {
bat_ledger_service_->SetEnvironment(environment);
}

void RewardsServiceImpl::SetDebug(bool debug) {
Expand Down Expand Up @@ -4016,13 +4031,18 @@ void RewardsServiceImpl::GrantAttestationResult(
#endif

#if defined(OS_ANDROID)
bool RewardsServiceImpl::ShouldUseStagingServerForAndroid() {
bool use_staging = false;
ledger::Environment RewardsServiceImpl::GetServerEnvironmentForAndroid() {
auto result = ledger::Environment::PRODUCTION;
if (profile_ && profile_->GetPrefs()) {
use_staging = profile_->GetPrefs()->
GetBoolean(prefs::kUseRewardsStagingServer);
use_staging =
profile_->GetPrefs()->GetBoolean(prefs::kUseRewardsStagingServer);
}
return use_staging;

if (use_staging) {
result = ledger::Environment::STAGING;
}

return result;
}
#endif

Expand Down
Loading