Commit a6dffe5e authored by Hesen Zhang's avatar Hesen Zhang Committed by Commit Bot

[Query Tiles]: Register pref service and add unit test for scheduler.

Bug: 1073550
Change-Id: I2a9d0d10839007a1a170a44014a3f9dbbe1f1904
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2198719
Commit-Queue: Hesen Zhang <hesen@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Reviewed-by: default avatarMin Qin <qinmin@chromium.org>
Reviewed-by: default avatarXing Liu <xingliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#768760}
parent 1b943e22
......@@ -252,6 +252,7 @@
#include "chrome/browser/ui/webui/settings/settings_ui.h"
#include "chrome/browser/upgrade_detector/upgrade_detector.h"
#include "components/ntp_tiles/custom_links_manager_impl.h"
#include "components/query_tiles/tile_service_prefs.h"
#endif // defined(OS_ANDROID)
#if defined(OS_CHROMEOS)
......@@ -985,6 +986,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry,
ntp_tiles::CustomLinksManagerImpl::RegisterProfilePrefs(registry);
PinnedTabCodec::RegisterProfilePrefs(registry);
PromoService::RegisterProfilePrefs(registry);
query_tiles::RegisterPrefs(registry);
SearchSuggestService::RegisterProfilePrefs(registry);
settings::SettingsUI::RegisterProfilePrefs(registry);
send_tab_to_self::SendTabToSelfBubbleController::RegisterProfilePrefs(
......
......@@ -29,6 +29,8 @@ source_set("public") {
"tile.cc",
"tile.h",
"tile_service.h",
"tile_service_prefs.cc",
"tile_service_prefs.h",
]
deps = [ "//ui/gfx" ]
......@@ -36,6 +38,7 @@ source_set("public") {
public_deps = [
"//base",
"//components/keyed_service/core",
"//components/prefs",
"//url:url",
]
......
......@@ -70,6 +70,7 @@ source_set("unit_tests") {
"tile_iterator_unittest.cc",
"tile_manager_unittest.cc",
"tile_service_impl_unittest.cc",
"tile_service_scheduler_unittest.cc",
"tile_store_unittest.cc",
]
......@@ -80,6 +81,7 @@ source_set("unit_tests") {
"//components/image_fetcher/core:test_support",
"//components/leveldb_proto",
"//components/leveldb_proto:test_support",
"//components/prefs:test_support",
"//components/query_tiles:public",
"//components/query_tiles/proto",
"//components/query_tiles/test:test_lib",
......
......@@ -5,6 +5,7 @@
#include "components/query_tiles/internal/tile_service_scheduler.h"
#include <memory>
#include <utility>
#include "base/rand_util.h"
#include "base/time/default_tick_clock.h"
......@@ -15,9 +16,6 @@
namespace query_tiles {
namespace {
// Key for query tiles backoff entry stored in pref service.
constexpr char kBackoffEntryKey[] = "query_tiles.backoff_entry_key";
class TileServiceSchedulerImpl : public TileServiceScheduler {
public:
TileServiceSchedulerImpl(
......
......@@ -12,6 +12,7 @@
#include "components/background_task_scheduler/background_task_scheduler.h"
#include "components/query_tiles/internal/tile_config.h"
#include "components/query_tiles/internal/tile_types.h"
#include "components/query_tiles/tile_service_prefs.h"
#include "net/base/backoff_entry_serializer.h"
class PrefService;
......
// Copyright 2020 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 "components/query_tiles/internal/tile_service_scheduler.h"
#include <utility>
#include <vector>
#include "base/bind.h"
#include "base/run_loop.h"
#include "base/test/simple_test_clock.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/test/task_environment.h"
#include "base/test/test_mock_time_task_runner.h"
#include "components/prefs/testing_pref_service.h"
#include "components/query_tiles/internal/tile_config.h"
#include "components/query_tiles/internal/tile_store.h"
#include "components/query_tiles/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using ::testing::Invoke;
namespace query_tiles {
namespace {
class MockBackgroundTaskScheduler
: public background_task::BackgroundTaskScheduler {
public:
MockBackgroundTaskScheduler() = default;
~MockBackgroundTaskScheduler() override = default;
MOCK_METHOD1(Schedule, bool(const background_task::TaskInfo& task_info));
MOCK_METHOD1(Cancel, void(int));
};
// TODO(crbug.com/1082529): Verify the schedule timing range matches task_info,
// also add more cross status situation tests.
class TileServiceSchedulerTest : public testing::Test {
public:
TileServiceSchedulerTest() = default;
~TileServiceSchedulerTest() override = default;
void SetUp() override {
base::Time fake_now;
EXPECT_TRUE(base::Time::FromString("05/18/20 01:00:00 AM", &fake_now));
clock_.SetNow(fake_now);
query_tiles::RegisterPrefs(prefs()->registry());
auto policy = std::make_unique<net::BackoffEntry::Policy>();
policy->num_errors_to_ignore = 0;
policy->initial_delay_ms = 1 * 1000;
policy->multiply_factor = 2;
policy->jitter_factor = 0;
policy->maximum_backoff_ms = 4 * 1000;
policy->always_use_initial_delay = false;
policy->entry_lifetime_ms = -1;
tile_service_scheduler_ =
TileServiceScheduler::Create(&mocked_native_scheduler_, &prefs_,
&clock_, &tick_clock_, std::move(policy));
}
protected:
base::Clock* clock() { return &clock_; }
base::TickClock* tick_clock() { return &tick_clock_; }
MockBackgroundTaskScheduler* native_scheduler() {
return &mocked_native_scheduler_;
}
TileServiceScheduler* tile_service_scheduler() {
return tile_service_scheduler_.get();
}
TestingPrefServiceSimple* prefs() { return &prefs_; }
private:
base::test::TaskEnvironment task_environment_;
base::SimpleTestClock clock_;
base::SimpleTestTickClock tick_clock_;
TestingPrefServiceSimple prefs_;
MockBackgroundTaskScheduler mocked_native_scheduler_;
std::unique_ptr<TileServiceScheduler> tile_service_scheduler_;
};
TEST_F(TileServiceSchedulerTest, CancelTask) {
EXPECT_CALL(
*native_scheduler(),
Cancel(static_cast<int>(background_task::TaskIds::QUERY_TILE_JOB_ID)));
tile_service_scheduler()->CancelTask();
}
TEST_F(TileServiceSchedulerTest, OnFetchCompletedSuccess) {
EXPECT_CALL(*native_scheduler(), Schedule(_));
tile_service_scheduler()->OnFetchCompleted(TileInfoRequestStatus::kSuccess);
}
TEST_F(TileServiceSchedulerTest, OnFetchCompletedSuspend) {
EXPECT_CALL(*native_scheduler(), Schedule(_));
tile_service_scheduler()->OnFetchCompleted(
TileInfoRequestStatus::kShouldSuspend);
}
TEST_F(TileServiceSchedulerTest, OnFetchCompletedFailure) {
EXPECT_CALL(*native_scheduler(), Schedule(_));
tile_service_scheduler()->OnFetchCompleted(TileInfoRequestStatus::kFailure);
}
TEST_F(TileServiceSchedulerTest, OnFetchCompletedOtherStatus) {
std::vector<TileInfoRequestStatus> other_status = {
TileInfoRequestStatus::kInit};
EXPECT_CALL(*native_scheduler(), Schedule(_)).Times(0);
for (const auto& status : other_status) {
tile_service_scheduler()->OnFetchCompleted(status);
}
}
TEST_F(TileServiceSchedulerTest, OnTileGroupLoadedWithNoTiles) {
EXPECT_CALL(*native_scheduler(), Schedule(_));
tile_service_scheduler()->OnTileManagerInitialized(TileGroupStatus::kNoTiles);
}
TEST_F(TileServiceSchedulerTest, OnTileGroupLoadedWithFailure) {
EXPECT_CALL(*native_scheduler(), Schedule(_));
tile_service_scheduler()->OnTileManagerInitialized(
TileGroupStatus::kFailureDbOperation);
}
TEST_F(TileServiceSchedulerTest, OnTileGroupLoadedWithOtherStatus) {
std::vector<TileGroupStatus> other_status = {TileGroupStatus::kUninitialized,
TileGroupStatus ::kSuccess};
EXPECT_CALL(*native_scheduler(), Schedule(_)).Times(0);
for (const auto status : other_status) {
tile_service_scheduler()->OnTileManagerInitialized(status);
}
}
} // namespace
} // namespace query_tiles
// Copyright 2020 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 "components/query_tiles/tile_service_prefs.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
namespace query_tiles {
constexpr char kBackoffEntryKey[] = "query_tiles.backoff_entry_key";
void RegisterPrefs(PrefRegistrySimple* registry) {
registry->RegisterListPref(kBackoffEntryKey);
}
} // namespace query_tiles
// Copyright 2020 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 COMPONENTS_QUERY_TILES_TILE_SERVICE_PREFS_H_
#define COMPONENTS_QUERY_TILES_TILE_SERVICE_PREFS_H_
class PrefRegistrySimple;
namespace query_tiles {
// Key for query tiles backoff entry stored in pref service.
extern const char kBackoffEntryKey[];
// Register to prefs service.
void RegisterPrefs(PrefRegistrySimple* registry);
} // namespace query_tiles
#endif // COMPONENTS_QUERY_TILES_TILE_SERVICE_PREFS_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