Commit a862f943 authored by Mikel Astiz's avatar Mikel Astiz Committed by Commit Bot

Avoid late keyed services with TestingProfile

This patch migrates tests away from deprecated APIs and adopts
TestingProfile::Builder for tests in
/chrome/browser/engagement.

Rationale: creating or overriding keyed services after the profile has
been created is problematic and known to cause hard-to-debug test
flakiness, because it bypasses BrowserContextDependencyManager and often
leading to use-after-free.

This CL was uploaded by git cl split.

R=calamity@chromium.org

Bug: 1106699
Change-Id: Ia256324372fffe91b7dd7bda603faea66fbbb666
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2316361
Auto-Submit: Mikel Astiz <mastiz@chromium.org>
Reviewed-by: default avatarcalamity <calamity@chromium.org>
Commit-Queue: calamity <calamity@chromium.org>
Cr-Commit-Position: refs/heads/master@{#792498}
parent 1470d237
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include <utility> #include <utility>
#include "base/bind.h" #include "base/bind.h"
#include "base/files/scoped_temp_dir.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/metrics/sample_vector.h" #include "base/metrics/sample_vector.h"
...@@ -27,9 +26,6 @@ ...@@ -27,9 +26,6 @@
#include "components/content_settings/core/browser/host_content_settings_map.h" #include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h" #include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_pattern.h" #include "components/content_settings/core/common/content_settings_pattern.h"
#include "components/history/core/browser/history_database_params.h"
#include "components/history/core/browser/history_service.h"
#include "components/history/core/test/test_history_database.h"
#include "components/keyed_service/core/keyed_service.h" #include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
...@@ -44,15 +40,6 @@ using BookmarkModel = bookmarks::BookmarkModel; ...@@ -44,15 +40,6 @@ using BookmarkModel = bookmarks::BookmarkModel;
using ImportantDomainInfo = ImportantSitesUtil::ImportantDomainInfo; using ImportantDomainInfo = ImportantSitesUtil::ImportantDomainInfo;
const size_t kNumImportantSites = 5; const size_t kNumImportantSites = 5;
base::FilePath g_temp_history_dir;
std::unique_ptr<KeyedService> BuildTestHistoryService(
content::BrowserContext* context) {
std::unique_ptr<history::HistoryService> service(
new history::HistoryService());
service->Init(history::TestHistoryDatabaseParamsForPath(g_temp_history_dir));
return std::move(service);
}
// We only need to reproduce the values that we are testing. The values here // We only need to reproduce the values that we are testing. The values here
// need to match the values in important_sites_util. // need to match the values in important_sites_util.
...@@ -76,10 +63,13 @@ class ImportantSitesUtilTest : public ChromeRenderViewHostTestHarness { ...@@ -76,10 +63,13 @@ class ImportantSitesUtilTest : public ChromeRenderViewHostTestHarness {
void SetUp() override { void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp(); ChromeRenderViewHostTestHarness::SetUp();
SiteEngagementScore::SetParamValuesForTesting(); SiteEngagementScore::SetParamValuesForTesting();
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
g_temp_history_dir = temp_dir_.GetPath();
HistoryServiceFactory::GetInstance()->SetTestingFactory( TestingProfile::TestingFactories GetTestingFactories() const override {
profile(), base::BindRepeating(&BuildTestHistoryService)); return {{BookmarkModelFactory::GetInstance(),
BookmarkModelFactory::GetDefaultFactory()},
{HistoryServiceFactory::GetInstance(),
HistoryServiceFactory::GetDefaultFactory()}};
} }
void AddContentSetting(ContentSettingsType type, void AddContentSetting(ContentSettingsType type,
...@@ -98,7 +88,6 @@ class ImportantSitesUtilTest : public ChromeRenderViewHostTestHarness { ...@@ -98,7 +88,6 @@ class ImportantSitesUtilTest : public ChromeRenderViewHostTestHarness {
void AddBookmark(const GURL& origin) { void AddBookmark(const GURL& origin) {
if (!model_) { if (!model_) {
profile()->CreateBookmarkModel(true);
model_ = BookmarkModelFactory::GetForBrowserContext(profile()); model_ = BookmarkModelFactory::GetForBrowserContext(profile());
bookmarks::test::WaitForBookmarkModelToLoad(model_); bookmarks::test::WaitForBookmarkModelToLoad(model_);
} }
...@@ -138,7 +127,6 @@ class ImportantSitesUtilTest : public ChromeRenderViewHostTestHarness { ...@@ -138,7 +127,6 @@ class ImportantSitesUtilTest : public ChromeRenderViewHostTestHarness {
} }
private: private:
base::ScopedTempDir temp_dir_;
BookmarkModel* model_ = nullptr; BookmarkModel* model_ = nullptr;
}; };
......
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