This source file includes following definitions.
- destroyed_otr_profile_
- DestroyOffTheRecordProfile
- DestroyOffTheRecordProfile
- CreateProfile
- TEST_F
- TEST_F
#include "chrome/browser/profiles/profile_destroyer.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/site_instance.h"
class TestingOffTheRecordDestructionProfile : public TestingProfile {
public:
TestingOffTheRecordDestructionProfile()
: TestingProfile(base::FilePath(),
NULL,
scoped_refptr<ExtensionSpecialStoragePolicy>()
scoped_ptr<PrefServiceSyncable>(),
true,
TestingFactories()),
destroyed_otr_profile_(false) {
set_incognito(true);
}
virtual void DestroyOffTheRecordProfile() OVERRIDE {
destroyed_otr_profile_ = true;
}
bool destroyed_otr_profile_;
DISALLOW_COPY_AND_ASSIGN(TestingOffTheRecordDestructionProfile);
};
class TestingOriginalDestructionProfile : public TestingProfile {
public:
TestingOriginalDestructionProfile() : destroyed_otr_profile_(false) {
DCHECK_EQ(kNull, living_instance_);
living_instance_ = this;
}
virtual ~TestingOriginalDestructionProfile() {
DCHECK_EQ(this, living_instance_);
living_instance_ = NULL;
}
virtual void DestroyOffTheRecordProfile() OVERRIDE {
SetOffTheRecordProfile(NULL);
destroyed_otr_profile_ = true;
}
bool destroyed_otr_profile_;
static TestingOriginalDestructionProfile* living_instance_;
static const TestingOriginalDestructionProfile* kNull;
DISALLOW_COPY_AND_ASSIGN(TestingOriginalDestructionProfile);
};
const TestingOriginalDestructionProfile*
TestingOriginalDestructionProfile::kNull = NULL;
TestingOriginalDestructionProfile*
TestingOriginalDestructionProfile::living_instance_ = NULL;
class ProfileDestroyerTest : public BrowserWithTestWindowTest {
public:
ProfileDestroyerTest() : off_the_record_profile_(NULL) {}
protected:
virtual TestingProfile* CreateProfile() OVERRIDE {
if (off_the_record_profile_ == NULL)
off_the_record_profile_ = new TestingOffTheRecordDestructionProfile();
return off_the_record_profile_;
}
TestingOffTheRecordDestructionProfile* off_the_record_profile_;
DISALLOW_COPY_AND_ASSIGN(ProfileDestroyerTest);
};
TEST_F(ProfileDestroyerTest, DelayProfileDestruction) {
scoped_refptr<content::SiteInstance> instance1(
content::SiteInstance::Create(off_the_record_profile_));
scoped_ptr<content::RenderProcessHost> render_process_host1;
render_process_host1.reset(instance1->GetProcess());
ASSERT_TRUE(render_process_host1.get() != NULL);
scoped_refptr<content::SiteInstance> instance2(
content::SiteInstance::Create(off_the_record_profile_));
scoped_ptr<content::RenderProcessHost> render_process_host2;
render_process_host2.reset(instance2->GetProcess());
ASSERT_TRUE(render_process_host2.get() != NULL);
set_browser(NULL);
EXPECT_FALSE(off_the_record_profile_->destroyed_otr_profile_);
render_process_host1.release()->Cleanup();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_FALSE(off_the_record_profile_->destroyed_otr_profile_);
render_process_host2.release()->Cleanup();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_TRUE(off_the_record_profile_->destroyed_otr_profile_);
}
TEST_F(ProfileDestroyerTest, DelayOriginalProfileDestruction) {
TestingOriginalDestructionProfile* original_profile =
new TestingOriginalDestructionProfile;
TestingOffTheRecordDestructionProfile* off_the_record_profile =
new TestingOffTheRecordDestructionProfile;
original_profile->SetOffTheRecordProfile(off_the_record_profile);
scoped_refptr<content::SiteInstance> instance1(
content::SiteInstance::Create(off_the_record_profile));
scoped_ptr<content::RenderProcessHost> render_process_host1;
render_process_host1.reset(instance1->GetProcess());
ASSERT_TRUE(render_process_host1.get() != NULL);
ProfileDestroyer::DestroyProfileWhenAppropriate(original_profile);
EXPECT_NE(TestingOriginalDestructionProfile::kNull,
TestingOriginalDestructionProfile::living_instance_);
EXPECT_FALSE(original_profile->destroyed_otr_profile_);
render_process_host1.release()->Cleanup();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(NULL, TestingOriginalDestructionProfile::living_instance_);
TestingOriginalDestructionProfile* main_profile =
new TestingOriginalDestructionProfile;
scoped_refptr<content::SiteInstance> instance2(
content::SiteInstance::Create(main_profile));
scoped_ptr<content::RenderProcessHost> render_process_host2;
render_process_host2.reset(instance2->GetProcess());
ASSERT_TRUE(render_process_host2.get() != NULL);
ProfileDestroyer::DestroyProfileWhenAppropriate(main_profile);
EXPECT_EQ(main_profile, TestingOriginalDestructionProfile::living_instance_);
render_process_host2.release()->Cleanup();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(NULL, TestingOriginalDestructionProfile::living_instance_);
}