Commit 2d08729e authored by Xing Liu's avatar Xing Liu Committed by Commit Bot

Notification scheduler: Add mock MockBackgroundTaskCoordinator.

This CL adds MockBackgroundTaskCoordinator and create an interface for
BackgroundTaskCoordinator, also move implementation code to cc file.

Bug: 963304
Change-Id: I5308ca58d87d1e0c7e56b4b780955d132cc98c33
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1728346Reviewed-by: default avatarHesen Zhang <hesen@chromium.org>
Commit-Queue: Xing Liu <xingliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#683240}
parent f369e8a1
...@@ -188,26 +188,57 @@ base::TimeDelta BackgroundTaskCoordinator::DefaultTimeRandomizer( ...@@ -188,26 +188,57 @@ base::TimeDelta BackgroundTaskCoordinator::DefaultTimeRandomizer(
return base::RandDouble() * time_window; return base::RandDouble() * time_window;
} }
BackgroundTaskCoordinator::BackgroundTaskCoordinator( class BackgroundTaskCoordinatorImpl : public BackgroundTaskCoordinator {
public:
BackgroundTaskCoordinatorImpl(
std::unique_ptr<NotificationBackgroundTaskScheduler> background_task,
const SchedulerConfig* config,
TimeRandomizer time_randomizer,
base::Clock* clock)
: background_task_(std::move(background_task)),
config_(config),
time_randomizer_(time_randomizer),
clock_(clock) {}
~BackgroundTaskCoordinatorImpl() override = default;
private:
// BackgroundTaskCoordinator implementation.
void ScheduleBackgroundTask(Notifications notifications,
ClientStates client_states,
SchedulerTaskTime task_start_time) override {
auto helper = std::make_unique<BackgroundTaskCoordinatorHelper>(
background_task_.get(), config_, time_randomizer_, clock_);
helper->ScheduleBackgroundTask(std::move(notifications),
std::move(client_states), task_start_time);
}
// The class that actually schedules platform background task.
std::unique_ptr<NotificationBackgroundTaskScheduler> background_task_;
// System configuration.
const SchedulerConfig* config_;
// Randomize the time to show the notification, to avoid large number of users
// to perform actions at the same time.
TimeRandomizer time_randomizer_;
// Clock to query the current timestamp.
base::Clock* clock_;
DISALLOW_COPY_AND_ASSIGN(BackgroundTaskCoordinatorImpl);
};
// static
std::unique_ptr<BackgroundTaskCoordinator> BackgroundTaskCoordinator::Create(
std::unique_ptr<NotificationBackgroundTaskScheduler> background_task, std::unique_ptr<NotificationBackgroundTaskScheduler> background_task,
const SchedulerConfig* config, const SchedulerConfig* config,
TimeRandomizer time_randomizer, TimeRandomizer time_randomizer,
base::Clock* clock) base::Clock* clock) {
: background_task_(std::move(background_task)), return std::make_unique<BackgroundTaskCoordinatorImpl>(
config_(config), std::move(background_task), config, time_randomizer, clock);
time_randomizer_(time_randomizer), }
clock_(clock) {}
BackgroundTaskCoordinator::~BackgroundTaskCoordinator() = default; BackgroundTaskCoordinator::~BackgroundTaskCoordinator() = default;
void BackgroundTaskCoordinator::ScheduleBackgroundTask(
Notifications notifications,
ClientStates client_states,
SchedulerTaskTime task_start_time) {
auto helper = std::make_unique<BackgroundTaskCoordinatorHelper>(
background_task_.get(), config_, time_randomizer_, clock_);
helper->ScheduleBackgroundTask(std::move(notifications),
std::move(client_states), task_start_time);
}
} // namespace notifications } // namespace notifications
...@@ -32,35 +32,22 @@ class BackgroundTaskCoordinator { ...@@ -32,35 +32,22 @@ class BackgroundTaskCoordinator {
std::map<SchedulerClientType, std::vector<const NotificationEntry*>>; std::map<SchedulerClientType, std::vector<const NotificationEntry*>>;
using ClientStates = std::map<SchedulerClientType, const ClientState*>; using ClientStates = std::map<SchedulerClientType, const ClientState*>;
using TimeRandomizer = base::RepeatingCallback<base::TimeDelta()>; using TimeRandomizer = base::RepeatingCallback<base::TimeDelta()>;
static base::TimeDelta DefaultTimeRandomizer( static base::TimeDelta DefaultTimeRandomizer(
const base::TimeDelta& time_window); const base::TimeDelta& time_window);
BackgroundTaskCoordinator(
static std::unique_ptr<BackgroundTaskCoordinator> Create(
std::unique_ptr<NotificationBackgroundTaskScheduler> background_task, std::unique_ptr<NotificationBackgroundTaskScheduler> background_task,
const SchedulerConfig* config, const SchedulerConfig* config,
TimeRandomizer time_randomizer, TimeRandomizer time_randomizer,
base::Clock* clock); base::Clock* clock);
virtual ~BackgroundTaskCoordinator(); virtual ~BackgroundTaskCoordinator();
// Schedule background task based on current notification in the storage. // Schedule background task based on current notification in the storage.
virtual void ScheduleBackgroundTask(Notifications notifications, virtual void ScheduleBackgroundTask(Notifications notifications,
ClientStates client_states, ClientStates client_states,
SchedulerTaskTime task_start_time); SchedulerTaskTime task_start_time) = 0;
private:
// The class that actually schedules platform background task.
std::unique_ptr<NotificationBackgroundTaskScheduler> background_task_;
// System configuration.
const SchedulerConfig* config_;
// Randomize the time to show the notification, to avoid large number of users
// to perform actions at the same time.
TimeRandomizer time_randomizer_;
// Clock to query the current timestamp.
base::Clock* clock_;
DISALLOW_COPY_AND_ASSIGN(BackgroundTaskCoordinator);
}; };
} // namespace notifications } // namespace notifications
......
...@@ -76,7 +76,7 @@ class BackgroundTaskCoordinatorTest : public testing::Test { ...@@ -76,7 +76,7 @@ class BackgroundTaskCoordinatorTest : public testing::Test {
auto background_task = auto background_task =
std::make_unique<test::MockNotificationBackgroundTaskScheduler>(); std::make_unique<test::MockNotificationBackgroundTaskScheduler>();
background_task_ = background_task.get(); background_task_ = background_task.get();
coordinator_ = std::make_unique<BackgroundTaskCoordinator>( coordinator_ = BackgroundTaskCoordinator::Create(
std::move(background_task), &config_, std::move(background_task), &config_,
base::BindRepeating(&NoopTimeRandomizer, base::TimeDelta()), &clock_); base::BindRepeating(&NoopTimeRandomizer, base::TimeDelta()), &clock_);
} }
......
...@@ -8,14 +8,12 @@ ...@@ -8,14 +8,12 @@
#include <utility> #include <utility>
#include "base/bind.h" #include "base/bind.h"
#include "base/time/default_clock.h"
#include "chrome/browser/notifications/scheduler/internal/background_task_coordinator.h" #include "chrome/browser/notifications/scheduler/internal/background_task_coordinator.h"
#include "chrome/browser/notifications/scheduler/internal/display_decider.h" #include "chrome/browser/notifications/scheduler/internal/display_decider.h"
#include "chrome/browser/notifications/scheduler/internal/impression_history_tracker.h" #include "chrome/browser/notifications/scheduler/internal/impression_history_tracker.h"
#include "chrome/browser/notifications/scheduler/internal/scheduled_notification_manager.h" #include "chrome/browser/notifications/scheduler/internal/scheduled_notification_manager.h"
#include "chrome/browser/notifications/scheduler/internal/scheduler_config.h" #include "chrome/browser/notifications/scheduler/internal/scheduler_config.h"
#include "chrome/browser/notifications/scheduler/public/display_agent.h" #include "chrome/browser/notifications/scheduler/public/display_agent.h"
#include "chrome/browser/notifications/scheduler/public/notification_background_task_scheduler.h"
#include "chrome/browser/notifications/scheduler/public/notification_scheduler_client.h" #include "chrome/browser/notifications/scheduler/public/notification_scheduler_client.h"
#include "chrome/browser/notifications/scheduler/public/notification_scheduler_client_registrar.h" #include "chrome/browser/notifications/scheduler/public/notification_scheduler_client_registrar.h"
...@@ -23,7 +21,7 @@ namespace notifications { ...@@ -23,7 +21,7 @@ namespace notifications {
NotificationSchedulerContext::NotificationSchedulerContext( NotificationSchedulerContext::NotificationSchedulerContext(
std::unique_ptr<NotificationSchedulerClientRegistrar> client_registrar, std::unique_ptr<NotificationSchedulerClientRegistrar> client_registrar,
std::unique_ptr<NotificationBackgroundTaskScheduler> background_task, std::unique_ptr<BackgroundTaskCoordinator> background_task_coordinator,
std::unique_ptr<ImpressionHistoryTracker> impression_tracker, std::unique_ptr<ImpressionHistoryTracker> impression_tracker,
std::unique_ptr<ScheduledNotificationManager> notification_manager, std::unique_ptr<ScheduledNotificationManager> notification_manager,
std::unique_ptr<DisplayAgent> display_agent, std::unique_ptr<DisplayAgent> display_agent,
...@@ -35,12 +33,7 @@ NotificationSchedulerContext::NotificationSchedulerContext( ...@@ -35,12 +33,7 @@ NotificationSchedulerContext::NotificationSchedulerContext(
display_agent_(std::move(display_agent)), display_agent_(std::move(display_agent)),
display_decider_(std::move(display_decider)), display_decider_(std::move(display_decider)),
config_(std::move(config)), config_(std::move(config)),
background_task_coordinator_(std::make_unique<BackgroundTaskCoordinator>( background_task_coordinator_(std::move(background_task_coordinator)) {}
std::move(background_task),
config_.get(),
base::BindRepeating(&BackgroundTaskCoordinator::DefaultTimeRandomizer,
config_->background_task_random_time_window),
base::DefaultClock::GetInstance())) {}
NotificationSchedulerContext::~NotificationSchedulerContext() = default; NotificationSchedulerContext::~NotificationSchedulerContext() = default;
......
...@@ -17,7 +17,6 @@ class BackgroundTaskCoordinator; ...@@ -17,7 +17,6 @@ class BackgroundTaskCoordinator;
class DisplayAgent; class DisplayAgent;
class DisplayDecider; class DisplayDecider;
class ImpressionHistoryTracker; class ImpressionHistoryTracker;
class NotificationBackgroundTaskScheduler;
class NotificationSchedulerClientRegistrar; class NotificationSchedulerClientRegistrar;
class ScheduledNotificationManager; class ScheduledNotificationManager;
struct SchedulerConfig; struct SchedulerConfig;
...@@ -28,7 +27,7 @@ class NotificationSchedulerContext { ...@@ -28,7 +27,7 @@ class NotificationSchedulerContext {
public: public:
NotificationSchedulerContext( NotificationSchedulerContext(
std::unique_ptr<NotificationSchedulerClientRegistrar> client_registrar, std::unique_ptr<NotificationSchedulerClientRegistrar> client_registrar,
std::unique_ptr<NotificationBackgroundTaskScheduler> background_task, std::unique_ptr<BackgroundTaskCoordinator> background_task_coordinator,
std::unique_ptr<ImpressionHistoryTracker> impression_tracker, std::unique_ptr<ImpressionHistoryTracker> impression_tracker,
std::unique_ptr<ScheduledNotificationManager> notification_manager, std::unique_ptr<ScheduledNotificationManager> notification_manager,
std::unique_ptr<DisplayAgent> display_agent, std::unique_ptr<DisplayAgent> display_agent,
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/sequenced_task_runner.h" #include "base/sequenced_task_runner.h"
#include "base/task/post_task.h" #include "base/task/post_task.h"
#include "base/time/default_clock.h" #include "base/time/default_clock.h"
#include "chrome/browser/notifications/scheduler/internal/background_task_coordinator.h"
#include "chrome/browser/notifications/scheduler/internal/display_decider.h" #include "chrome/browser/notifications/scheduler/internal/display_decider.h"
#include "chrome/browser/notifications/scheduler/internal/icon_store.h" #include "chrome/browser/notifications/scheduler/internal/icon_store.h"
#include "chrome/browser/notifications/scheduler/internal/impression_history_tracker.h" #include "chrome/browser/notifications/scheduler/internal/impression_history_tracker.h"
...@@ -89,8 +90,14 @@ KeyedService* CreateNotificationScheduleService( ...@@ -89,8 +90,14 @@ KeyedService* CreateNotificationScheduleService(
std::move(notification_store), std::move(icon_store), registered_clients, std::move(notification_store), std::move(icon_store), registered_clients,
*config.get()); *config.get());
auto background_task_coordinator = BackgroundTaskCoordinator::Create(
std::move(background_task_scheduler), config.get(),
base::BindRepeating(&BackgroundTaskCoordinator::DefaultTimeRandomizer,
config->background_task_random_time_window),
base::DefaultClock::GetInstance());
auto context = std::make_unique<NotificationSchedulerContext>( auto context = std::make_unique<NotificationSchedulerContext>(
std::move(client_registrar), std::move(background_task_scheduler), std::move(client_registrar), std::move(background_task_coordinator),
std::move(impression_tracker), std::move(notification_manager), std::move(impression_tracker), std::move(notification_manager),
std::move(display_agent), DisplayDecider::Create(), std::move(config)); std::move(display_agent), DisplayDecider::Create(), std::move(config));
......
...@@ -10,6 +10,8 @@ source_set("test_lib") { ...@@ -10,6 +10,8 @@ source_set("test_lib") {
sources = [ sources = [
"fake_clock.cc", "fake_clock.cc",
"fake_clock.h", "fake_clock.h",
"mock_background_task_coordinator.cc",
"mock_background_task_coordinator.h",
"mock_display_agent.cc", "mock_display_agent.cc",
"mock_display_agent.h", "mock_display_agent.h",
"mock_display_decider.cc", "mock_display_decider.cc",
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/notifications/scheduler/test/mock_background_task_coordinator.h"
namespace notifications {
namespace test {
MockBackgroundTaskCoordinator::MockBackgroundTaskCoordinator() = default;
MockBackgroundTaskCoordinator::~MockBackgroundTaskCoordinator() = default;
} // namespace test
} // namespace notifications
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_NOTIFICATIONS_SCHEDULER_TEST_MOCK_BACKGROUND_TASK_COORDINATOR_H_
#define CHROME_BROWSER_NOTIFICATIONS_SCHEDULER_TEST_MOCK_BACKGROUND_TASK_COORDINATOR_H_
#include "chrome/browser/notifications/scheduler/internal/background_task_coordinator.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace notifications {
namespace test {
class MockBackgroundTaskCoordinator : public BackgroundTaskCoordinator {
public:
MockBackgroundTaskCoordinator();
~MockBackgroundTaskCoordinator() override;
MOCK_METHOD3(ScheduleBackgroundTask,
void(BackgroundTaskCoordinator::Notifications notifications,
BackgroundTaskCoordinator::ClientStates client_states,
SchedulerTaskTime task_start_time));
};
} // namespace test
} // namespace notifications
#endif // CHROME_BROWSER_NOTIFICATIONS_SCHEDULER_TEST_MOCK_BACKGROUND_TASK_COORDINATOR_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment