Commit ed149212 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/android/signin.

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

Bug: 1106699
Change-Id: Ifa14602b1dab9f9c965e7e4dd55fac20f5cd68d2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2315154
Commit-Queue: Mikel Astiz <mastiz@chromium.org>
Auto-Submit: Mikel Astiz <mastiz@chromium.org>
Reviewed-by: default avatarBoris Sazonov <bsazonov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#792187}
parent 7191df2b
......@@ -19,8 +19,7 @@
#include "chrome/browser/download/download_core_service_impl.h"
#include "chrome/browser/offline_pages/offline_page_model_factory.h"
#include "chrome/browser/profiles/profile_key.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "chrome/test/base/testing_profile.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/test/bookmark_test_helpers.h"
#include "components/browsing_data/content/cache_storage_helper.h"
......@@ -53,31 +52,34 @@ std::unique_ptr<KeyedService> BuildOfflinePageModel(SimpleFactoryKey* key) {
class SigninManagerAndroidTest : public ::testing::Test {
public:
SigninManagerAndroidTest()
: profile_manager_(TestingBrowserProcess::GetGlobal()) {}
~SigninManagerAndroidTest() override {}
SigninManagerAndroidTest() = default;
~SigninManagerAndroidTest() override = default;
void SetUp() override {
ASSERT_TRUE(profile_manager_.SetUp());
profile_ = profile_manager_.CreateTestingProfile("Testing Profile");
TestingProfile::Builder profile_builder;
profile_builder.AddTestingFactory(
BookmarkModelFactory::GetInstance(),
BookmarkModelFactory::GetDefaultFactory());
profile_ = profile_builder.Build();
// Creating a BookmarkModel also a creates a StubOfflinePageModel.
// We need to replace this with a mock that responds to deletions.
offline_pages::OfflinePageModelFactory::GetInstance()->SetTestingFactory(
profile_->GetProfileKey(), base::BindRepeating(&BuildOfflinePageModel));
// TODO(crbug.com/748484): Remove requirement for this delegate in
// unit_tests.
DownloadCoreServiceFactory::GetForBrowserContext(profile_)
DownloadCoreServiceFactory::GetForBrowserContext(profile_.get())
->SetDownloadManagerDelegateForTesting(
std::make_unique<ChromeDownloadManagerDelegate>(profile_));
std::make_unique<ChromeDownloadManagerDelegate>(profile_.get()));
}
TestingProfile* profile() { return profile_; }
TestingProfile* profile() { return profile_.get(); }
// Adds two testing bookmarks to |profile_|.
bookmarks::BookmarkModel* AddTestBookmarks() {
profile_->CreateBookmarkModel(true);
// Creating a BookmarkModel also a creates a StubOfflinePageModel.
// We need to replace this with a mock that responds to deletions.
offline_pages::OfflinePageModelFactory::GetInstance()->SetTestingFactory(
profile_->GetProfileKey(), base::BindRepeating(&BuildOfflinePageModel));
bookmarks::BookmarkModel* bookmark_model =
BookmarkModelFactory::GetForBrowserContext(profile_);
BookmarkModelFactory::GetForBrowserContext(profile());
bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model);
bookmark_model->AddURL(bookmark_model->bookmark_bar_node(), 0,
......@@ -100,8 +102,7 @@ class SigninManagerAndroidTest : public ::testing::Test {
private:
content::BrowserTaskEnvironment task_environment_;
TestingProfileManager profile_manager_;
TestingProfile* profile_; // Owned by |profile_manager_|.
std::unique_ptr<TestingProfile> profile_;
DISALLOW_COPY_AND_ASSIGN(SigninManagerAndroidTest);
};
......
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