Skip to content

Commit

Permalink
Fixes Brave Ads are not shown if the browser window did not enter the…
Browse files Browse the repository at this point in the history
… background and then become active
  • Loading branch information
tmancey committed Mar 17, 2021
1 parent c00cb7f commit 47fff12
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
9 changes: 9 additions & 0 deletions vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ void AdsImpl::Initialize(InitializeCallback callback) {
return;
}

InitializeBrowserManager();

InitializeDatabase(callback);
}

Expand Down Expand Up @@ -460,6 +462,13 @@ void AdsImpl::set(privacy::TokenGeneratorInterface* token_generator) {
user_activity_ = std::make_unique<UserActivity>();
}

void AdsImpl::InitializeBrowserManager() {
const bool is_foreground = AdsClientHelper::Get()->IsForeground();

BrowserManager::Get()->SetForegrounded(is_foreground);
BrowserManager::Get()->SetActive(is_foreground ? true : false);
}

void AdsImpl::InitializeDatabase(InitializeCallback callback) {
database_->CreateOrOpen([=](const Result result) {
if (result != SUCCESS) {
Expand Down
1 change: 1 addition & 0 deletions vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ class AdsImpl : public Ads,

void set(privacy::TokenGeneratorInterface* token_generator);

void InitializeBrowserManager();
void InitializeDatabase(InitializeCallback callback);
void MigrateConversions(InitializeCallback callback);
void LoadClientState(InitializeCallback callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ bool BrowserManager::HasInstance() {
return g_browser_manager;
}

void BrowserManager::SetActive(const bool is_active) {
is_active_ = is_active;
}

bool BrowserManager::IsActive() const {
return is_active_ && IsForegrounded();
}
Expand Down Expand Up @@ -66,6 +70,10 @@ void BrowserManager::OnInactive() {
UserActivityEventType::kBrowserWindowIsInactive);
}

void BrowserManager::SetForegrounded(const bool is_foregrounded) {
is_foregrounded_ = is_foregrounded;
}

bool BrowserManager::IsForegrounded() const {
return is_foregrounded_;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ class BrowserManager {

static bool HasInstance();

void SetActive(const bool is_active);
bool IsActive() const;
void OnActive();
void OnInactive();

void SetForegrounded(const bool is_foregrounded);
bool IsForegrounded() const;
void OnForegrounded();
void OnBackgrounded();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,22 @@ TEST_F(BatAdsBrowserManagerTest, HasInstance) {

TEST_F(BatAdsBrowserManagerTest, BrowserWindowIsActive) {
// Arrange
BrowserManager::Get()->SetForegrounded(true);
BrowserManager::Get()->SetActive(false);

// Act
BrowserManager::Get()->OnActive();
BrowserManager::Get()->OnForegrounded();

// Assert
EXPECT_TRUE(BrowserManager::Get()->IsActive());
}

TEST_F(BatAdsBrowserManagerTest, BrowserWindowIsActiveUserActivityEvent) {
// Arrange
BrowserManager::Get()->SetActive(false);

// Act
BrowserManager::Get()->OnActive();
BrowserManager::Get()->OnForegrounded();

// Assert
const UserActivityEvents events =
Expand All @@ -58,31 +59,27 @@ TEST_F(BatAdsBrowserManagerTest, BrowserWindowIsActiveUserActivityEvent) {
event.type = UserActivityEventType::kBrowserWindowIsActive;
event.time = base::Time::Now();
expected_events.push_back(event);
event.type = UserActivityEventType::kBrowserDidBecomeActive;
event.time = base::Time::Now();
expected_events.push_back(event);

EXPECT_EQ(expected_events, events);
}

TEST_F(BatAdsBrowserManagerTest, BrowserWindowIsInactive) {
// Arrange
BrowserManager::Get()->SetActive(true);

// Act
BrowserManager::Get()->OnInactive();
BrowserManager::Get()->OnForegrounded();

// Assert
EXPECT_FALSE(BrowserManager::Get()->IsActive());
}

TEST_F(BatAdsBrowserManagerTest, BrowserWindowIsInactiveUserActivityEvent) {
// Arrange
BrowserManager::Get()->OnActive();
BrowserManager::Get()->SetActive(true);

// Act
BrowserManager::Get()->OnInactive();
BrowserManager::Get()->OnForegrounded();

// Assert
const UserActivityEvents events =
Expand All @@ -91,15 +88,9 @@ TEST_F(BatAdsBrowserManagerTest, BrowserWindowIsInactiveUserActivityEvent) {

UserActivityEvents expected_events;
UserActivityEventInfo event;
event.type = UserActivityEventType::kBrowserWindowIsActive;
event.time = base::Time::Now();
expected_events.push_back(event);
event.type = UserActivityEventType::kBrowserWindowIsInactive;
event.time = base::Time::Now();
expected_events.push_back(event);
event.type = UserActivityEventType::kBrowserDidBecomeActive;
event.time = base::Time::Now();
expected_events.push_back(event);

EXPECT_EQ(expected_events, events);
}
Expand All @@ -116,10 +107,10 @@ TEST_F(BatAdsBrowserManagerTest, BrowserDidBecomeActive) {

TEST_F(BatAdsBrowserManagerTest, BrowserDidBecomeActiveUserActivityEvent) {
// Arrange
BrowserManager::Get()->SetActive(false);

// Act
BrowserManager::Get()->OnActive();
BrowserManager::Get()->OnForegrounded();

// Assert
const UserActivityEvents events =
Expand All @@ -131,15 +122,13 @@ TEST_F(BatAdsBrowserManagerTest, BrowserDidBecomeActiveUserActivityEvent) {
event.type = UserActivityEventType::kBrowserWindowIsActive;
event.time = base::Time::Now();
expected_events.push_back(event);
event.type = UserActivityEventType::kBrowserDidBecomeActive;
event.time = base::Time::Now();
expected_events.push_back(event);

EXPECT_EQ(expected_events, events);
}

TEST_F(BatAdsBrowserManagerTest, BrowserDidEnterBackground) {
// Arrange
BrowserManager::Get()->SetForegrounded(true);

// Act
BrowserManager::Get()->OnBackgrounded();
Expand All @@ -150,10 +139,9 @@ TEST_F(BatAdsBrowserManagerTest, BrowserDidEnterBackground) {

TEST_F(BatAdsBrowserManagerTest, BrowserDidEnterBackgroundUserActivityEvent) {
// Arrange
BrowserManager::Get()->OnForegrounded();
BrowserManager::Get()->SetForegrounded(true);

// Act
BrowserManager::Get()->OnActive();
BrowserManager::Get()->OnBackgrounded();

// Assert
Expand All @@ -163,12 +151,6 @@ TEST_F(BatAdsBrowserManagerTest, BrowserDidEnterBackgroundUserActivityEvent) {

UserActivityEvents expected_events;
UserActivityEventInfo event;
event.type = UserActivityEventType::kBrowserDidBecomeActive;
event.time = base::Time::Now();
expected_events.push_back(event);
event.type = UserActivityEventType::kBrowserWindowIsActive;
event.time = base::Time::Now();
expected_events.push_back(event);
event.type = UserActivityEventType::kBrowserDidEnterBackground;
event.time = base::Time::Now();
expected_events.push_back(event);
Expand Down

0 comments on commit 47fff12

Please sign in to comment.