Commit 2c398c33 authored by Ramin Halavati's avatar Ramin Halavati Committed by Commit Bot

Update Profile Destroyer test to cover non-primary OTR profiles.

Expands the tests in profile destroyer unittest to cover both primary
and non-primary OTR profiles.

Bug: 1033903
Change-Id: I53244db5e236a60bb7ea5d777438671b6d94d0fa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2529107
Commit-Queue: Ramin Halavati <rhalavati@chromium.org>
Reviewed-by: default avatarMihai Sardarescu <msarda@chromium.org>
Cr-Commit-Position: refs/heads/master@{#827240}
parent 2370de33
...@@ -111,8 +111,8 @@ class Profile : public content::BrowserContext { ...@@ -111,8 +111,8 @@ class Profile : public content::BrowserContext {
explicit OTRProfileID(const std::string& profile_id); explicit OTRProfileID(const std::string& profile_id);
// ID used by the incognito and guest profiles. // ID used by the incognito and guest profiles.
// TODO(https://crbug.com/1033903): To be replaced with |IncognitoID| and // TODO(https://crbug.com/1125474): To be replaced with |IncognitoID| when
// |GuestID| when the use cases are reduced. // OTR Guest profiles are deprecated.
static const OTRProfileID PrimaryID(); static const OTRProfileID PrimaryID();
// Creates a unique OTR profile id with the given profile id prefix. // Creates a unique OTR profile id with the given profile id prefix.
......
...@@ -11,9 +11,10 @@ ...@@ -11,9 +11,10 @@
#include "content/public/browser/render_process_host.h" #include "content/public/browser/render_process_host.h"
#include "content/public/browser/site_instance.h" #include "content/public/browser/site_instance.h"
class ProfileDestroyerTest : public BrowserWithTestWindowTest { class ProfileDestroyerTest : public BrowserWithTestWindowTest,
public testing::WithParamInterface<bool> {
public: public:
ProfileDestroyerTest() = default; ProfileDestroyerTest() : is_primary_otr_(GetParam()) {}
void SetUp() override { void SetUp() override {
BrowserWithTestWindowTest::SetUp(); BrowserWithTestWindowTest::SetUp();
...@@ -24,22 +25,26 @@ class ProfileDestroyerTest : public BrowserWithTestWindowTest { ...@@ -24,22 +25,26 @@ class ProfileDestroyerTest : public BrowserWithTestWindowTest {
TestingProfile* GetOriginalProfile() { return GetProfile(); } TestingProfile* GetOriginalProfile() { return GetProfile(); }
TestingProfile* GetIncognitoProfile() { TestingProfile* GetOffTheRecordProfile() {
if (!incognito_profile_) { if (!otr_profile_) {
TestingProfile::Builder builder; TestingProfile::Builder builder;
incognito_profile_ = builder.BuildIncognito(GetOriginalProfile()); Profile::OTRProfileID profile_id =
incognito_profile_->SetProfileDestructionObserver( is_primary_otr_ ? Profile::OTRProfileID::PrimaryID()
base::BindOnce(&ProfileDestroyerTest::SetIncognitoProfileDestroyed, : Profile::OTRProfileID("Test::ProfileDestroyer");
otr_profile_ =
builder.BuildOffTheRecord(GetOriginalProfile(), profile_id);
otr_profile_->SetProfileDestructionObserver(
base::BindOnce(&ProfileDestroyerTest::SetOTRProfileDestroyed,
base::Unretained(this))); base::Unretained(this)));
} }
return incognito_profile_; return otr_profile_;
} }
void SetOriginalProfileDestroyed() { original_profile_destroyed_ = true; } void SetOriginalProfileDestroyed() { original_profile_destroyed_ = true; }
void SetIncognitoProfileDestroyed() { incognito_profile_destroyed_ = true; } void SetOTRProfileDestroyed() { otr_profile_destroyed_ = true; }
bool IsOriginalProfileDestroyed() { return original_profile_destroyed_; } bool IsOriginalProfileDestroyed() { return original_profile_destroyed_; }
bool IsIncognitoProfileDestroyed() { return incognito_profile_destroyed_; } bool IsOTRProfileDestroyed() { return otr_profile_destroyed_; }
// Creates a render process host based on a new site instance given the // Creates a render process host based on a new site instance given the
// |profile| and mark it as used. // |profile| and mark it as used.
...@@ -56,56 +61,59 @@ class ProfileDestroyerTest : public BrowserWithTestWindowTest { ...@@ -56,56 +61,59 @@ class ProfileDestroyerTest : public BrowserWithTestWindowTest {
} }
protected: protected:
bool is_primary_otr_;
bool original_profile_destroyed_{false}; bool original_profile_destroyed_{false};
bool incognito_profile_destroyed_{false}; bool otr_profile_destroyed_{false};
TestingProfile* incognito_profile_{nullptr}; TestingProfile* otr_profile_{nullptr};
std::vector<scoped_refptr<content::SiteInstance>> site_instances_; std::vector<scoped_refptr<content::SiteInstance>> site_instances_;
DISALLOW_COPY_AND_ASSIGN(ProfileDestroyerTest); DISALLOW_COPY_AND_ASSIGN(ProfileDestroyerTest);
}; };
// Expect immediate incognito profile destruction when no pending renderer // Expect immediate OTR profile destruction when no pending renderer
// process host exists. // process host exists.
TEST_F(ProfileDestroyerTest, ImmediateIncognitoProfileDestruction) { TEST_P(ProfileDestroyerTest, ImmediateOTRProfileDestruction) {
TestingProfile* incognito_profile = GetIncognitoProfile(); TestingProfile* otr_profile = GetOffTheRecordProfile();
// Destroying the regular browser does not result in destruction of regular // Destroying the regular browser does not result in destruction of regular
// profile and hence should not destroy the incognito profile. // profile and hence should not destroy the OTR profile.
set_browser(nullptr); set_browser(nullptr);
EXPECT_FALSE(IsOriginalProfileDestroyed()); EXPECT_FALSE(IsOriginalProfileDestroyed());
EXPECT_FALSE(IsIncognitoProfileDestroyed()); EXPECT_FALSE(IsOTRProfileDestroyed());
// Ask for destruction of Incognito profile, and expect immediate destruction. // Ask for destruction of OTR profile, and expect immediate destruction.
ProfileDestroyer::DestroyProfileWhenAppropriate(incognito_profile); ProfileDestroyer::DestroyProfileWhenAppropriate(otr_profile);
EXPECT_TRUE(IsIncognitoProfileDestroyed()); EXPECT_TRUE(IsOTRProfileDestroyed());
} }
// Expect pending renderer process hosts delay incognito profile destruction. // Expect pending renderer process hosts delay OTR profile destruction.
TEST_F(ProfileDestroyerTest, DelayedIncognitoProfileDestruction) { TEST_P(ProfileDestroyerTest, DelayedOTRProfileDestruction) {
TestingProfile* incognito_profile = GetIncognitoProfile(); TestingProfile* otr_profile = GetOffTheRecordProfile();
// Create two render process hosts. // Create two render process hosts.
std::unique_ptr<content::RenderProcessHost> render_process_host1 = std::unique_ptr<content::RenderProcessHost> render_process_host1 =
CreatedRendererProcessHost(incognito_profile); CreatedRendererProcessHost(otr_profile);
std::unique_ptr<content::RenderProcessHost> render_process_host2 = std::unique_ptr<content::RenderProcessHost> render_process_host2 =
CreatedRendererProcessHost(incognito_profile); CreatedRendererProcessHost(otr_profile);
// Ask for destruction of Incognito profile, but expect it to be delayed. // Ask for destruction of OTR profile, but expect it to be delayed.
ProfileDestroyer::DestroyProfileWhenAppropriate(incognito_profile); ProfileDestroyer::DestroyProfileWhenAppropriate(otr_profile);
EXPECT_FALSE(IsIncognitoProfileDestroyed()); EXPECT_FALSE(IsOTRProfileDestroyed());
// Destroy the first pending render process host, and expect it not to destroy // Destroy the first pending render process host, and expect it not to destroy
// the incognito profile. // the OTR profile.
render_process_host1.release()->Cleanup(); render_process_host1.release()->Cleanup();
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_FALSE(IsIncognitoProfileDestroyed()); EXPECT_FALSE(IsOTRProfileDestroyed());
// Destroy the other renderer process, and expect destruction of incognito // Destroy the other renderer process, and expect destruction of OTR
// profile. // profile.
render_process_host2.release()->Cleanup(); render_process_host2.release()->Cleanup();
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_TRUE(IsIncognitoProfileDestroyed()); EXPECT_TRUE(IsOTRProfileDestroyed());
} }
// TODO(https://crbug.com/1033903): Add tests for non-primary OTRs. INSTANTIATE_TEST_SUITE_P(AllOTRProfileTypes,
ProfileDestroyerTest,
/*is_primary_otr=*/testing::Bool());
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