Commit f2d15dab 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/ui.

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=tapted@chromium.org

Bug: 1106699
Change-Id: Id83d2e9bb3cb147da6363981cd7ac20a6daa956a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2316207
Commit-Queue: Mikel Astiz <mastiz@chromium.org>
Auto-Submit: Mikel Astiz <mastiz@chromium.org>
Reviewed-by: default avatarTrent Apted <tapted@chromium.org>
Cr-Commit-Position: refs/heads/master@{#791645}
parent c0bd3b98
......@@ -52,9 +52,11 @@ class BookmarkContextMenuControllerTest : public testing::Test {
BookmarkContextMenuControllerTest() : model_(nullptr) {}
void SetUp() override {
TestingProfile::Builder builder;
profile_ = builder.Build();
profile_->CreateBookmarkModel(true);
TestingProfile::Builder profile_builder;
profile_builder.AddTestingFactory(
BookmarkModelFactory::GetInstance(),
BookmarkModelFactory::GetDefaultFactory());
profile_ = profile_builder.Build();
model_ = BookmarkModelFactory::GetForBrowserContext(profile_.get());
bookmarks::test::WaitForBookmarkModelToLoad(model_);
AddTestData(model_);
......@@ -251,10 +253,11 @@ TEST_F(BookmarkContextMenuControllerTest, MultipleFoldersWithURLs) {
// Tests the enabled state of open incognito.
TEST_F(BookmarkContextMenuControllerTest, DisableIncognito) {
TestingProfile* incognito =
TestingProfile::Builder().BuildIncognito(profile_.get());
TestingProfile::Builder profile_builder;
profile_builder.AddTestingFactory(BookmarkModelFactory::GetInstance(),
BookmarkModelFactory::GetDefaultFactory());
TestingProfile* incognito = profile_builder.BuildIncognito(profile_.get());
incognito->CreateBookmarkModel(true);
BookmarkModel* model = BookmarkModelFactory::GetForBrowserContext(incognito);
bookmarks::test::WaitForBookmarkModelToLoad(model);
AddTestData(model);
......
......@@ -14,13 +14,19 @@
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
typedef BrowserWithTestWindowTest BookmarkTest;
class BookmarkTest : public BrowserWithTestWindowTest {
public:
TestingProfile::TestingFactories GetTestingFactories() override {
return {{BookmarkModelFactory::GetInstance(),
BookmarkModelFactory::GetDefaultFactory()}};
}
};
TEST_F(BookmarkTest, NonEmptyBookmarkBarShownOnNTP) {
profile()->CreateBookmarkModel(true);
bookmarks::BookmarkModel* bookmark_model =
BookmarkModelFactory::GetForBrowserContext(profile());
bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model);
bookmarks::AddIfNotBookmarked(bookmark_model, GURL("https://www.test.com"),
base::string16());
......@@ -29,6 +35,10 @@ TEST_F(BookmarkTest, NonEmptyBookmarkBarShownOnNTP) {
}
TEST_F(BookmarkTest, EmptyBookmarkBarNotShownOnNTP) {
bookmarks::BookmarkModel* bookmark_model =
BookmarkModelFactory::GetForBrowserContext(profile());
bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model);
AddTab(browser(), GURL(chrome::kChromeUINewTabURL));
EXPECT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state());
}
......
......@@ -67,6 +67,13 @@ class FaviconDelegate : public ui::MenuModelDelegate {
class BackFwdMenuModelTest : public ChromeRenderViewHostTestHarness {
public:
TestingProfile::TestingFactories GetTestingFactories() const override {
return {{HistoryServiceFactory::GetInstance(),
HistoryServiceFactory::GetDefaultFactory()},
{FaviconServiceFactory::GetInstance(),
FaviconServiceFactory::GetDefaultFactory()}};
}
void ValidateModel(BackForwardMenuModel* model, int history_items,
int chapter_stops) {
int h = std::min(BackForwardMenuModel::kMaxHistoryItems, history_items);
......@@ -494,8 +501,6 @@ TEST_F(BackFwdMenuModelTest, EscapeLabel) {
// Test asynchronous loading of favicon from history service.
TEST_F(BackFwdMenuModelTest, FaviconLoadTest) {
ASSERT_TRUE(profile()->CreateHistoryService(true, false));
profile()->CreateFaviconService();
Browser::CreateParams native_params(profile(), true);
std::unique_ptr<Browser> browser(
CreateBrowserWithTestWindowForParams(&native_params));
......
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