summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortapted <tapted@chromium.org>2014-09-15 16:49:40 -0700
committerCommit bot <commit-bot@chromium.org>2014-09-15 23:51:58 +0000
commit1ab9b709cb4d691f667c843040b65b46a9c82ebd (patch)
tree21cfd5195a404967e7cadb577dccd23c3210cefb
parent24f900df2092632a68d22f06d61ac828ea6b6d63 (diff)
downloadchromium_src-1ab9b709cb4d691f667c843040b65b46a9c82ebd.zip
chromium_src-1ab9b709cb4d691f667c843040b65b46a9c82ebd.tar.gz
chromium_src-1ab9b709cb4d691f667c843040b65b46a9c82ebd.tar.bz2
Ensure incognito TestingProfiles are incognito for their whole lifetime.
Otherwise keyed services see different profiles in unit_tests at different times since the incognito->GetOriginalProfile() changes. This is causing release code to pick up unnecessary workarounds to keep unit_tests green in try runs. The fix is achieved by adding `TestingProfile::Builder::BuildIncognito`, which differs from `Build` by requiring an |original_profile| argument, which will own the incognito profile (so no need to return scoped_ptr). This simplifies a lot of call sites, since they no longer need to do a scoped_ptr dance of their own. BUG=277296 Review URL: https://codereview.chromium.org/556173002 Cr-Commit-Position: refs/heads/master@{#294931}
-rw-r--r--chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos_unittest.cc6
-rw-r--r--chrome/browser/chromeos/power/power_prefs_unittest.cc11
-rw-r--r--chrome/browser/content_settings/content_settings_pref_provider_unittest.cc4
-rw-r--r--chrome/browser/password_manager/password_manager_internals_service_unittest.cc10
-rw-r--r--chrome/browser/profiles/profile_manager_unittest.cc7
-rw-r--r--chrome/browser/renderer_context_menu/spelling_menu_observer_browsertest.cc14
-rw-r--r--chrome/browser/search/hotword_service_unittest.cc14
-rw-r--r--chrome/browser/ui/autofill/account_chooser_model_unittest.cc11
-rw-r--r--chrome/browser/ui/bookmarks/bookmark_context_menu_controller_unittest.cc10
-rw-r--r--chrome/browser/ui/browser_command_controller_unittest.cc19
-rw-r--r--chrome/browser/ui/sync/one_click_signin_helper_unittest.cc10
-rw-r--r--chrome/browser/ui/views/bookmarks/bookmark_context_menu_unittest.cc7
-rw-r--r--chrome/test/base/testing_profile.cc115
-rw-r--r--chrome/test/base/testing_profile.h25
-rw-r--r--chrome/test/base/testing_profile_manager.cc10
15 files changed, 131 insertions, 142 deletions
diff --git a/chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos_unittest.cc b/chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos_unittest.cc
index 6ddccc6..4490749 100644
--- a/chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos_unittest.cc
+++ b/chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos_unittest.cc
@@ -99,13 +99,10 @@ class UserCloudPolicyManagerChromeOSTest : public testing::Test {
0,
std::string(),
factories);
- signin_profile_ = profile_manager_->CreateTestingProfile(kSigninProfile);
- signin_profile_->ForceIncognito(true);
// Usually the signin Profile and the main Profile are separate, but since
// the signin Profile is an OTR Profile then for this test it suffices to
// attach it to the main Profile.
- profile_->SetOffTheRecordProfile(scoped_ptr<Profile>(signin_profile_));
- signin_profile_->SetOriginalProfile(profile_);
+ signin_profile_ = TestingProfile::Builder().BuildIncognito(profile_);
ASSERT_EQ(signin_profile_, chromeos::ProfileHelper::GetSigninProfile());
chrome::RegisterLocalState(prefs_.registry());
@@ -156,7 +153,6 @@ class UserCloudPolicyManagerChromeOSTest : public testing::Test {
}
signin_profile_ = NULL;
profile_ = NULL;
- profile_manager_->DeleteTestingProfile(kSigninProfile);
profile_manager_->DeleteTestingProfile(chrome::kInitialProfile);
}
diff --git a/chrome/browser/chromeos/power/power_prefs_unittest.cc b/chrome/browser/chromeos/power/power_prefs_unittest.cc
index 061dfbf..4a46b491 100644
--- a/chrome/browser/chromeos/power/power_prefs_unittest.cc
+++ b/chrome/browser/chromeos/power/power_prefs_unittest.cc
@@ -167,15 +167,8 @@ TEST_F(PowerPrefsTest, LoginScreen) {
builder.SetPath(
profile_manager_.profiles_dir().AppendASCII(chrome::kInitialProfile));
builder.SetPrefService(login_profile_prefs.PassAs<PrefServiceSyncable>());
- builder.SetIncognito();
- scoped_ptr<TestingProfile> login_profile_owner(builder.Build());
-
- TestingProfile* login_profile = login_profile_owner.get();
- TestingProfile* login_profile_parent = profile_manager_.CreateTestingProfile(
- chrome::kInitialProfile);
- login_profile_parent->SetOffTheRecordProfile(
- login_profile_owner.PassAs<Profile>());
- login_profile->SetOriginalProfile(login_profile_parent);
+ TestingProfile* login_profile = builder.BuildIncognito(
+ profile_manager_.CreateTestingProfile(chrome::kInitialProfile));
// Inform power_prefs_ that the login screen is being shown.
power_prefs_->Observe(chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
diff --git a/chrome/browser/content_settings/content_settings_pref_provider_unittest.cc b/chrome/browser/content_settings/content_settings_pref_provider_unittest.cc
index 9ad8bfb..7e32cb7 100644
--- a/chrome/browser/content_settings/content_settings_pref_provider_unittest.cc
+++ b/chrome/browser/content_settings/content_settings_pref_provider_unittest.cc
@@ -158,10 +158,8 @@ TEST_F(PrefProviderTest, Incognito) {
scoped_ptr<TestingProfile> profile = profile_builder.Build();
TestingProfile::Builder otr_profile_builder;
- otr_profile_builder.SetIncognito();
otr_profile_builder.SetPrefService(make_scoped_ptr(otr_prefs));
- scoped_ptr<TestingProfile> otr_profile(otr_profile_builder.Build());
- profile->SetOffTheRecordProfile(otr_profile.PassAs<Profile>());
+ otr_profile_builder.BuildIncognito(profile.get());
PrefProvider pref_content_settings_provider(regular_prefs, false);
PrefProvider pref_content_settings_provider_incognito(otr_prefs, true);
diff --git a/chrome/browser/password_manager/password_manager_internals_service_unittest.cc b/chrome/browser/password_manager/password_manager_internals_service_unittest.cc
index 472b117..82b7ffe 100644
--- a/chrome/browser/password_manager/password_manager_internals_service_unittest.cc
+++ b/chrome/browser/password_manager/password_manager_internals_service_unittest.cc
@@ -29,8 +29,6 @@ enum ProfileType { NORMAL_PROFILE, INCOGNITO_PROFILE };
scoped_ptr<TestingProfile> CreateProfile(ProfileType type) {
TestingProfile::Builder builder;
- if (type == INCOGNITO_PROFILE)
- builder.SetIncognito();
scoped_ptr<TestingProfile> profile(builder.Build());
#if !defined(NDEBUG)
// During the test cases, the profiles may get created on the same address. To
@@ -38,6 +36,10 @@ scoped_ptr<TestingProfile> CreateProfile(ProfileType type) {
// See declaration of MarkBrowserContextLiveForTesting for more details.
BrowserContextDependencyManager::GetInstance()
->MarkBrowserContextLiveForTesting(profile.get());
+ if (type == INCOGNITO_PROFILE) {
+ BrowserContextDependencyManager::GetInstance()
+ ->MarkBrowserContextLiveForTesting(profile->GetOffTheRecordProfile());
+ }
#endif
return profile.Pass();
}
@@ -69,9 +71,11 @@ TEST(PasswordManagerInternalsServiceTest, ServiceActiveNonIncognito) {
TEST(PasswordManagerInternalsServiceTest, ServiceNotActiveIncognito) {
scoped_ptr<TestingProfile> profile(CreateProfile(INCOGNITO_PROFILE));
ASSERT_TRUE(profile);
+
+ Profile* incognito_profile = profile->GetOffTheRecordProfile();
PasswordManagerInternalsService* service =
PasswordManagerInternalsServiceFactory::GetForBrowserContext(
- profile.get());
+ incognito_profile);
// BrowserContextKeyedBaseFactory::GetBrowserContextToUse should return NULL
// for |profile|, because |profile| is incognito. Therefore the returned
// |service| should also be NULL.
diff --git a/chrome/browser/profiles/profile_manager_unittest.cc b/chrome/browser/profiles/profile_manager_unittest.cc
index 4685581..e3bb944 100644
--- a/chrome/browser/profiles/profile_manager_unittest.cc
+++ b/chrome/browser/profiles/profile_manager_unittest.cc
@@ -760,13 +760,6 @@ TEST_F(ProfileManagerTest, LastOpenedProfilesDoesNotContainIncognito) {
static_cast<TestingProfile*>(profile_manager->GetProfile(dest_path1));
ASSERT_TRUE(profile1);
- // Incognito profiles should not be managed by the profile manager but by the
- // original profile.
- TestingProfile::Builder builder;
- builder.SetIncognito();
- scoped_ptr<TestingProfile> profile2 = builder.Build();
- profile1->SetOffTheRecordProfile(profile2.PassAs<Profile>());
-
std::vector<Profile*> last_opened_profiles =
profile_manager->GetLastOpenedProfiles();
ASSERT_EQ(0U, last_opened_profiles.size());
diff --git a/chrome/browser/renderer_context_menu/spelling_menu_observer_browsertest.cc b/chrome/browser/renderer_context_menu/spelling_menu_observer_browsertest.cc
index 943d689..3b4a841 100644
--- a/chrome/browser/renderer_context_menu/spelling_menu_observer_browsertest.cc
+++ b/chrome/browser/renderer_context_menu/spelling_menu_observer_browsertest.cc
@@ -81,7 +81,10 @@ class MockRenderViewContextMenu : public RenderViewContextMenuProxy {
// A dummy profile used in this test. Call GetPrefs() when a test needs to
// change this profile and use PrefService methods.
- scoped_ptr<TestingProfile> profile_;
+ scoped_ptr<TestingProfile> original_profile_;
+
+ // Either |original_profile_| or its incognito profile.
+ Profile* profile_;
// A list of menu items added by the SpellingMenuObserver class.
std::vector<MockMenuItem> items_;
@@ -91,10 +94,9 @@ class MockRenderViewContextMenu : public RenderViewContextMenuProxy {
MockRenderViewContextMenu::MockRenderViewContextMenu(bool incognito)
: observer_(NULL) {
- TestingProfile::Builder builder;
- if (incognito)
- builder.SetIncognito();
- profile_ = builder.Build();
+ original_profile_ = TestingProfile::Builder().Build();
+ profile_ = incognito ? original_profile_->GetOffTheRecordProfile()
+ : original_profile_.get();
}
MockRenderViewContextMenu::~MockRenderViewContextMenu() {
@@ -170,7 +172,7 @@ WebContents* MockRenderViewContextMenu::GetWebContents() const {
}
content::BrowserContext* MockRenderViewContextMenu::GetBrowserContext() const {
- return profile_.get();
+ return profile_;
}
size_t MockRenderViewContextMenu::GetMenuSize() const {
diff --git a/chrome/browser/search/hotword_service_unittest.cc b/chrome/browser/search/hotword_service_unittest.cc
index f6912a5..b878ee9 100644
--- a/chrome/browser/search/hotword_service_unittest.cc
+++ b/chrome/browser/search/hotword_service_unittest.cc
@@ -92,10 +92,7 @@ class HotwordServiceTest : public extensions::ExtensionServiceTestBase {
TEST_F(HotwordServiceTest, IsHotwordAllowedBadFieldTrial) {
TestingProfile::Builder profile_builder;
- TestingProfile::Builder otr_profile_builder;
- otr_profile_builder.SetIncognito();
scoped_ptr<TestingProfile> profile = profile_builder.Build();
- scoped_ptr<TestingProfile> otr_profile = otr_profile_builder.Build();
HotwordServiceFactory* hotword_service_factory =
HotwordServiceFactory::GetInstance();
@@ -126,15 +123,13 @@ TEST_F(HotwordServiceTest, IsHotwordAllowedBadFieldTrial) {
EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(profile.get()));
// Test that incognito returns false as well.
- EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(otr_profile.get()));
+ EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(
+ profile->GetOffTheRecordProfile()));
}
TEST_F(HotwordServiceTest, IsHotwordAllowedLocale) {
TestingProfile::Builder profile_builder;
- TestingProfile::Builder otr_profile_builder;
- otr_profile_builder.SetIncognito();
scoped_ptr<TestingProfile> profile = profile_builder.Build();
- scoped_ptr<TestingProfile> otr_profile = otr_profile_builder.Build();
HotwordServiceFactory* hotword_service_factory =
HotwordServiceFactory::GetInstance();
@@ -167,8 +162,9 @@ TEST_F(HotwordServiceTest, IsHotwordAllowedLocale) {
// Test that incognito even with a valid locale and valid field trial
// still returns false.
- SetApplicationLocale(static_cast<Profile*>(otr_profile.get()), "en");
- EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(otr_profile.get()));
+ Profile* otr_profile = profile->GetOffTheRecordProfile();
+ SetApplicationLocale(otr_profile, "en");
+ EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(otr_profile));
}
TEST_F(HotwordServiceTest, AudioLoggingPrefSetCorrectly) {
diff --git a/chrome/browser/ui/autofill/account_chooser_model_unittest.cc b/chrome/browser/ui/autofill/account_chooser_model_unittest.cc
index a4d44de..ea01c1f 100644
--- a/chrome/browser/ui/autofill/account_chooser_model_unittest.cc
+++ b/chrome/browser/ui/autofill/account_chooser_model_unittest.cc
@@ -95,19 +95,14 @@ TEST_F(AccountChooserModelTest, ObeysPref) {
}
// In incognito, use local data regardless of the pref.
{
- TestingProfile::Builder builder;
- builder.SetIncognito();
- scoped_ptr<TestingProfile> incognito = builder.Build();
- incognito->SetOriginalProfile(profile());
+ Profile* incognito = profile()->GetOffTheRecordProfile();
profile()->GetPrefs()->SetBoolean(
::prefs::kAutofillDialogPayWithoutWallet, false);
incognito->GetPrefs()->SetBoolean(
::prefs::kAutofillDialogPayWithoutWallet, false);
- TestAccountChooserModel model(delegate(),
- incognito.get(),
- false,
- metric_logger());
+ TestAccountChooserModel model(
+ delegate(), incognito, false, metric_logger());
EXPECT_FALSE(model.WalletIsSelected());
}
}
diff --git a/chrome/browser/ui/bookmarks/bookmark_context_menu_controller_unittest.cc b/chrome/browser/ui/bookmarks/bookmark_context_menu_controller_unittest.cc
index 5206a3e..2d64f64 100644
--- a/chrome/browser/ui/bookmarks/bookmark_context_menu_controller_unittest.cc
+++ b/chrome/browser/ui/bookmarks/bookmark_context_menu_controller_unittest.cc
@@ -240,14 +240,8 @@ TEST_F(BookmarkContextMenuControllerTest, MultipleFoldersWithURLs) {
// Tests the enabled state of open incognito.
TEST_F(BookmarkContextMenuControllerTest, DisableIncognito) {
- // Create an incognito Profile. It must be associated with the original
- // Profile, so that GetOriginalProfile() works as expected.
- TestingProfile::Builder builder;
- builder.SetIncognito();
- scoped_ptr<TestingProfile> testing_incognito = builder.Build();
- testing_incognito->SetOriginalProfile(profile_.get());
- TestingProfile* incognito = testing_incognito.get();
- profile_->SetOffTheRecordProfile(testing_incognito.PassAs<Profile>());
+ TestingProfile* incognito =
+ TestingProfile::Builder().BuildIncognito(profile_.get());
incognito->CreateBookmarkModel(true);
BookmarkModel* model = BookmarkModelFactory::GetForProfile(incognito);
diff --git a/chrome/browser/ui/browser_command_controller_unittest.cc b/chrome/browser/ui/browser_command_controller_unittest.cc
index 9fe9857..a796437 100644
--- a/chrome/browser/ui/browser_command_controller_unittest.cc
+++ b/chrome/browser/ui/browser_command_controller_unittest.cc
@@ -246,16 +246,19 @@ TEST_F(BrowserCommandControllerTest, NewAvatarMenuEnabledInGuestMode) {
ASSERT_TRUE(testing_profile_manager.SetUp());
// Set up guest a profile.
+ scoped_ptr<TestingProfile> original_profile =
+ TestingProfile::Builder().Build();
TestingProfile::Builder guest_builder;
- guest_builder.SetIncognito(); // Guest profiles are off the record.
guest_builder.SetGuestSession();
guest_builder.SetPath(ProfileManager::GetGuestProfilePath());
- scoped_ptr<TestingProfile>guest_profile = guest_builder.Build();
+ // Browsers in Guest mode must be off the record profiles.
+ TestingProfile* guest_profile =
+ guest_builder.BuildIncognito(original_profile.get());
ASSERT_TRUE(guest_profile->IsGuestSession());
// Create a new browser based on the guest profile.
- Browser::CreateParams profile_params(guest_profile.get(),
+ Browser::CreateParams profile_params(guest_profile,
chrome::GetActiveDesktop());
scoped_ptr<Browser> guest_browser(
chrome::CreateBrowserWithTestWindowForParams(&profile_params));
@@ -430,14 +433,10 @@ TEST_F(BrowserCommandControllerFullscreenTest,
TEST_F(BrowserCommandControllerTest, IncognitoModeOnSigninAllowedPrefChange) {
// Set up a profile with an off the record profile.
- TestingProfile::Builder builder;
- builder.SetIncognito();
- scoped_ptr<TestingProfile> profile2(builder.Build());
- TestingProfile::Builder builder2;
- scoped_ptr<TestingProfile> profile1 = builder2.Build();
- profile2->SetOriginalProfile(profile1.get());
+ scoped_ptr<TestingProfile> profile1 = TestingProfile::Builder().Build();
+ Profile* profile2 = profile1->GetOffTheRecordProfile();
+
EXPECT_EQ(profile2->GetOriginalProfile(), profile1.get());
- profile1->SetOffTheRecordProfile(profile2.PassAs<Profile>());
// Create a new browser based on the off the record profile.
Browser::CreateParams profile_params(profile1->GetOffTheRecordProfile(),
diff --git a/chrome/browser/ui/sync/one_click_signin_helper_unittest.cc b/chrome/browser/ui/sync/one_click_signin_helper_unittest.cc
index 13bfd65..d9ee9b1 100644
--- a/chrome/browser/ui/sync/one_click_signin_helper_unittest.cc
+++ b/chrome/browser/ui/sync/one_click_signin_helper_unittest.cc
@@ -392,10 +392,12 @@ class OneClickSigninHelperIncognitoTest : public OneClickSigninHelperTest {
content::BrowserContext*
OneClickSigninHelperIncognitoTest::CreateBrowserContext() {
- // Builds an incognito profile to run this test.
- TestingProfile::Builder builder;
- builder.SetIncognito();
- return builder.Build().release();
+ // Simulate an incognito profile to run this test. RenderViewHostTestHarness
+ // takes ownership of the return value, so it can't be a "proper" incognito
+ // profile, since they are owned by their parent, non-incognito profile.
+ scoped_ptr<TestingProfile> profile = TestingProfile::Builder().Build();
+ profile->ForceIncognito(true);
+ return profile.release();
}
TEST_F(OneClickSigninHelperTest, CanOfferNoContents) {
diff --git a/chrome/browser/ui/views/bookmarks/bookmark_context_menu_unittest.cc b/chrome/browser/ui/views/bookmarks/bookmark_context_menu_unittest.cc
index 0d008b4..6c01c08 100644
--- a/chrome/browser/ui/views/bookmarks/bookmark_context_menu_unittest.cc
+++ b/chrome/browser/ui/views/bookmarks/bookmark_context_menu_unittest.cc
@@ -270,12 +270,9 @@ TEST_F(BookmarkContextMenuTest, MultipleFoldersWithURLs) {
TEST_F(BookmarkContextMenuTest, DisableIncognito) {
std::vector<const BookmarkNode*> nodes;
nodes.push_back(model_->bookmark_bar_node()->GetChild(0));
- TestingProfile::Builder builder;
- builder.SetIncognito();
- scoped_ptr<TestingProfile> incognito_ = builder.Build().Pass();
- incognito_->SetOriginalProfile(profile_.get());
+ Profile* incognito = profile_->GetOffTheRecordProfile();
BookmarkContextMenu controller(
- NULL, NULL, incognito_.get(), NULL, nodes[0]->parent(), nodes, false);
+ NULL, NULL, incognito, NULL, nodes[0]->parent(), nodes, false);
EXPECT_FALSE(controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_INCOGNITO));
EXPECT_FALSE(
controller.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO));
diff --git a/chrome/test/base/testing_profile.cc b/chrome/test/base/testing_profile.cc
index 5fa8cba..e652fde 100644
--- a/chrome/test/base/testing_profile.cc
+++ b/chrome/test/base/testing_profile.cc
@@ -185,7 +185,6 @@ const char TestingProfile::kTestUserProfileDir[] = "Default";
TestingProfile::TestingProfile()
: start_time_(Time::Now()),
testing_prefs_(NULL),
- incognito_(false),
force_incognito_(false),
original_profile_(NULL),
guest_session_(false),
@@ -204,7 +203,6 @@ TestingProfile::TestingProfile()
TestingProfile::TestingProfile(const base::FilePath& path)
: start_time_(Time::Now()),
testing_prefs_(NULL),
- incognito_(false),
force_incognito_(false),
original_profile_(NULL),
guest_session_(false),
@@ -222,7 +220,6 @@ TestingProfile::TestingProfile(const base::FilePath& path,
Delegate* delegate)
: start_time_(Time::Now()),
testing_prefs_(NULL),
- incognito_(false),
force_incognito_(false),
original_profile_(NULL),
guest_session_(false),
@@ -249,7 +246,7 @@ TestingProfile::TestingProfile(
scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy,
#endif
scoped_ptr<PrefServiceSyncable> prefs,
- bool incognito,
+ TestingProfile* parent,
bool guest_session,
const std::string& supervised_user_id,
scoped_ptr<policy::PolicyService> policy_service,
@@ -257,9 +254,8 @@ TestingProfile::TestingProfile(
: start_time_(Time::Now()),
prefs_(prefs.release()),
testing_prefs_(NULL),
- incognito_(incognito),
force_incognito_(false),
- original_profile_(NULL),
+ original_profile_(parent),
guest_session_(guest_session),
supervised_user_id_(supervised_user_id),
last_session_exited_cleanly_(true),
@@ -272,6 +268,9 @@ TestingProfile::TestingProfile(
resource_context_(NULL),
delegate_(delegate),
policy_service_(policy_service.release()) {
+ if (parent)
+ parent->SetOffTheRecordProfile(scoped_ptr<Profile>(this));
+
// If no profile path was supplied, create one.
if (profile_path_.empty()) {
CreateTempProfileDir();
@@ -343,6 +342,8 @@ void TestingProfile::Init() {
if (prefs_.get())
user_prefs::UserPrefs::Set(this, prefs_.get());
+ else if (IsOffTheRecord())
+ CreateIncognitoPrefService();
else
CreateTestingPrefService();
@@ -360,10 +361,10 @@ void TestingProfile::Init() {
this, extensions::TestExtensionSystem::Build);
#endif
- // If no original profile was specified for this profile: register preferences
- // even if this is an incognito profile - this allows tests to create a
- // standalone incognito profile while still having prefs registered.
- if (!IsOffTheRecord() || !original_profile_) {
+ // Prefs for incognito profiles are set in CreateIncognitoPrefService() by
+ // simulating ProfileImpl::GetOffTheRecordPrefs().
+ if (!IsOffTheRecord()) {
+ DCHECK(!original_profile_);
user_prefs::PrefRegistrySyncable* pref_registry =
static_cast<user_prefs::PrefRegistrySyncable*>(
prefs_->DeprecatedGetPrefRegistry());
@@ -381,11 +382,13 @@ void TestingProfile::Init() {
#endif
#if defined(ENABLE_MANAGED_USERS)
- SupervisedUserSettingsService* settings_service =
- SupervisedUserSettingsServiceFactory::GetForProfile(this);
- TestingPrefStore* store = new TestingPrefStore();
- settings_service->Init(store);
- store->SetInitializationCompleted();
+ if (!IsOffTheRecord()) {
+ SupervisedUserSettingsService* settings_service =
+ SupervisedUserSettingsServiceFactory::GetForProfile(this);
+ TestingPrefStore* store = new TestingPrefStore();
+ settings_service->Init(store);
+ store->SetInitializationCompleted();
+ }
#endif
profile_name_ = "testing_profile";
@@ -410,6 +413,9 @@ TestingProfile::~TestingProfile() {
// Revert to non-incognito mode before shutdown.
force_incognito_ = false;
+ // If this profile owns an incognito profile, tear it down first.
+ incognito_profile_.reset();
+
// Any objects holding live URLFetchers should be deleted before teardown.
TemplateURLFetcherFactory::ShutdownForProfile(this);
@@ -602,8 +608,7 @@ scoped_refptr<base::SequencedTaskRunner> TestingProfile::GetIOTaskRunner() {
}
TestingPrefServiceSyncable* TestingProfile::GetTestingPrefService() {
- if (!prefs_.get())
- CreateTestingPrefService();
+ DCHECK(prefs_);
DCHECK(testing_prefs_);
return testing_prefs_;
}
@@ -619,35 +624,26 @@ std::string TestingProfile::GetProfileName() {
Profile::ProfileType TestingProfile::GetProfileType() const {
if (guest_session_)
return GUEST_PROFILE;
- if (force_incognito_ || incognito_)
+ if (force_incognito_ || original_profile_)
return INCOGNITO_PROFILE;
return REGULAR_PROFILE;
}
bool TestingProfile::IsOffTheRecord() const {
- return force_incognito_ || incognito_;
+ return force_incognito_ || original_profile_;
}
void TestingProfile::SetOffTheRecordProfile(scoped_ptr<Profile> profile) {
DCHECK(!IsOffTheRecord());
+ DCHECK_EQ(this, profile->GetOriginalProfile());
incognito_profile_ = profile.Pass();
}
-void TestingProfile::SetOriginalProfile(Profile* profile) {
- DCHECK(IsOffTheRecord());
- original_profile_ = profile;
-}
-
Profile* TestingProfile::GetOffTheRecordProfile() {
if (IsOffTheRecord())
return this;
- if (!incognito_profile_) {
- TestingProfile::Builder builder;
- builder.SetIncognito();
- scoped_ptr<TestingProfile> incognito_test_profile(builder.Build());
- incognito_test_profile->SetOriginalProfile(this);
- SetOffTheRecordProfile(incognito_test_profile.PassAs<Profile>());
- }
+ if (!incognito_profile_)
+ TestingProfile::Builder().BuildIncognito(this);
return incognito_profile_.get();
}
@@ -698,6 +694,15 @@ void TestingProfile::CreateTestingPrefService() {
chrome::RegisterUserProfilePrefs(testing_prefs_->registry());
}
+void TestingProfile::CreateIncognitoPrefService() {
+ DCHECK(original_profile_);
+ DCHECK(!testing_prefs_);
+ // Simplified version of ProfileImpl::GetOffTheRecordPrefs(). Note this
+ // leaves testing_prefs_ unset.
+ prefs_.reset(original_profile_->prefs_->CreateIncognitoPrefService(NULL));
+ user_prefs::UserPrefs::Set(this, prefs_.get());
+}
+
void TestingProfile::CreateProfilePolicyConnector() {
#if defined(ENABLE_CONFIGURATION_POLICY)
schema_registry_service_ =
@@ -724,9 +729,7 @@ if (!policy_service_) {
}
PrefService* TestingProfile::GetPrefs() {
- if (!prefs_.get()) {
- CreateTestingPrefService();
- }
+ DCHECK(prefs_);
return prefs_.get();
}
@@ -924,7 +927,6 @@ Profile::ExitType TestingProfile::GetLastSessionExitType() {
TestingProfile::Builder::Builder()
: build_called_(false),
delegate_(NULL),
- incognito_(false),
guest_session_(false) {
}
@@ -951,10 +953,6 @@ void TestingProfile::Builder::SetPrefService(
pref_service_ = prefs.Pass();
}
-void TestingProfile::Builder::SetIncognito() {
- incognito_ = true;
-}
-
void TestingProfile::Builder::SetGuestSession() {
guest_session_ = true;
}
@@ -979,16 +977,35 @@ scoped_ptr<TestingProfile> TestingProfile::Builder::Build() {
DCHECK(!build_called_);
build_called_ = true;
- return scoped_ptr<TestingProfile>(new TestingProfile(
- path_,
- delegate_,
+ return scoped_ptr<TestingProfile>(new TestingProfile(path_,
+ delegate_,
+#if defined(ENABLE_EXTENSIONS)
+ extension_policy_,
+#endif
+ pref_service_.Pass(),
+ NULL,
+ guest_session_,
+ supervised_user_id_,
+ policy_service_.Pass(),
+ testing_factories_));
+}
+
+TestingProfile* TestingProfile::Builder::BuildIncognito(
+ TestingProfile* original_profile) {
+ DCHECK(!build_called_);
+ DCHECK(original_profile);
+ build_called_ = true;
+
+ // Note: Owned by |original_profile|.
+ return new TestingProfile(path_,
+ delegate_,
#if defined(ENABLE_EXTENSIONS)
- extension_policy_,
+ extension_policy_,
#endif
- pref_service_.Pass(),
- incognito_,
- guest_session_,
- supervised_user_id_,
- policy_service_.Pass(),
- testing_factories_));
+ pref_service_.Pass(),
+ original_profile,
+ guest_session_,
+ supervised_user_id_,
+ policy_service_.Pass(),
+ testing_factories_);
}
diff --git a/chrome/test/base/testing_profile.h b/chrome/test/base/testing_profile.h
index bf55546c..6c60f06 100644
--- a/chrome/test/base/testing_profile.h
+++ b/chrome/test/base/testing_profile.h
@@ -97,9 +97,6 @@ class TestingProfile : public Profile {
// Sets the PrefService to be used by this profile.
void SetPrefService(scoped_ptr<PrefServiceSyncable> prefs);
- // Makes the Profile being built an incognito profile.
- void SetIncognito();
-
// Makes the Profile being built a guest profile.
void SetGuestSession();
@@ -113,6 +110,11 @@ class TestingProfile : public Profile {
// Creates the TestingProfile using previously-set settings.
scoped_ptr<TestingProfile> Build();
+ // Build an incognito profile, owned by |original_profile|. Note: unless you
+ // need to customize the Builder, or access TestingProfile member functions,
+ // you can use original_profile->GetOffTheRecordProfile().
+ TestingProfile* BuildIncognito(TestingProfile* original_profile);
+
private:
// If true, Build() has already been called.
bool build_called_;
@@ -124,7 +126,6 @@ class TestingProfile : public Profile {
#endif
base::FilePath path_;
Delegate* delegate_;
- bool incognito_;
bool guest_session_;
std::string supervised_user_id_;
scoped_ptr<policy::PolicyService> policy_service_;
@@ -154,7 +155,7 @@ class TestingProfile : public Profile {
scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy,
#endif
scoped_ptr<PrefServiceSyncable> prefs,
- bool incognito,
+ TestingProfile* parent,
bool guest_session,
const std::string& supervised_user_id,
scoped_ptr<policy::PolicyService> policy_service,
@@ -209,6 +210,11 @@ class TestingProfile : public Profile {
TestingPrefServiceSyncable* GetTestingPrefService();
+ // Called on the parent of an incognito |profile|. Usually called from the
+ // constructor of an incognito TestingProfile, but can also be used by tests
+ // to provide an OffTheRecordProfileImpl instance.
+ void SetOffTheRecordProfile(scoped_ptr<Profile> profile);
+
// content::BrowserContext
virtual base::FilePath GetPath() const OVERRIDE;
virtual scoped_refptr<base::SequencedTaskRunner> GetIOTaskRunner() OVERRIDE;
@@ -250,8 +256,6 @@ class TestingProfile : public Profile {
force_incognito_ = force_incognito;
}
- virtual void SetOffTheRecordProfile(scoped_ptr<Profile> profile);
- virtual void SetOriginalProfile(Profile* profile);
virtual Profile* GetOffTheRecordProfile() OVERRIDE;
virtual void DestroyOffTheRecordProfile() OVERRIDE {}
virtual bool HasOffTheRecordProfile() OVERRIDE;
@@ -349,6 +353,10 @@ class TestingProfile : public Profile {
// Creates a TestingPrefService and associates it with the TestingProfile.
void CreateTestingPrefService();
+ // Initializes |prefs_| for an incognito profile, derived from
+ // |original_profile_|.
+ void CreateIncognitoPrefService();
+
// Creates a ProfilePolicyConnector that the ProfilePolicyConnectorFactory
// maps to this profile.
void CreateProfilePolicyConnector();
@@ -357,10 +365,9 @@ class TestingProfile : public Profile {
// request context. Currently, only the CookieMonster is hooked up.
scoped_refptr<net::URLRequestContextGetter> extensions_request_context_;
- bool incognito_;
bool force_incognito_;
scoped_ptr<Profile> incognito_profile_;
- Profile* original_profile_;
+ TestingProfile* original_profile_;
bool guest_session_;
diff --git a/chrome/test/base/testing_profile_manager.cc b/chrome/test/base/testing_profile_manager.cc
index 0bbc4ee..2d49f6d 100644
--- a/chrome/test/base/testing_profile_manager.cc
+++ b/chrome/test/base/testing_profile_manager.cc
@@ -118,11 +118,6 @@ TestingProfile* TestingProfileManager::CreateTestingProfile(
TestingProfile* TestingProfileManager::CreateGuestProfile() {
DCHECK(called_set_up_);
- // Set up a profile with an off the record profile.
- TestingProfile::Builder otr_builder;
- otr_builder.SetIncognito();
- scoped_ptr<TestingProfile> otr_profile(otr_builder.Build());
-
// Create the profile and register it.
TestingProfile::Builder builder;
builder.SetGuestSession();
@@ -132,8 +127,9 @@ TestingProfile* TestingProfileManager::CreateGuestProfile() {
TestingProfile* profile = builder.Build().release();
profile->set_profile_name(kGuestProfileName);
- otr_profile->SetOriginalProfile(profile);
- profile->SetOffTheRecordProfile(otr_profile.PassAs<Profile>());
+ // Set up a profile with an off the record profile.
+ TestingProfile::Builder().BuildIncognito(profile);
+
profile_manager_->AddProfile(profile); // Takes ownership.
profile_manager_->SetGuestProfilePrefs(profile);