Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Side by Side Diff: content/browser/site_instance_impl_unittest.cc

Issue 11346016: Move remaining content test code into the content namespace. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: fix compile Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/command_line.h" 5 #include "base/command_line.h"
6 #include "base/compiler_specific.h" 6 #include "base/compiler_specific.h"
7 #include "base/stl_util.h" 7 #include "base/stl_util.h"
8 #include "base/string16.h" 8 #include "base/string16.h"
9 #include "content/browser/browser_thread_impl.h" 9 #include "content/browser/browser_thread_impl.h"
10 #include "content/browser/browsing_instance.h" 10 #include "content/browser/browsing_instance.h"
(...skipping 10 matching lines...) Expand all
21 #include "content/public/common/content_switches.h" 21 #include "content/public/common/content_switches.h"
22 #include "content/public/common/url_constants.h" 22 #include "content/public/common/url_constants.h"
23 #include "content/public/test/mock_render_process_host.h" 23 #include "content/public/test/mock_render_process_host.h"
24 #include "content/public/test/test_browser_context.h" 24 #include "content/public/test/test_browser_context.h"
25 #include "content/public/test/test_browser_thread.h" 25 #include "content/public/test/test_browser_thread.h"
26 #include "content/test/test_content_browser_client.h" 26 #include "content/test/test_content_browser_client.h"
27 #include "content/test/test_content_client.h" 27 #include "content/test/test_content_client.h"
28 #include "googleurl/src/url_util.h" 28 #include "googleurl/src/url_util.h"
29 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
30 30
31 using content::BrowserContext; 31 namespace content {
32 using content::BrowserThread;
33 using content::BrowserThreadImpl;
34 using content::MockRenderProcessHost;
35 using content::MockRenderProcessHostFactory;
36 using content::NavigationEntry;
37 using content::NavigationEntryImpl;
38 using content::SiteInstance;
39 using content::WebContentsImpl;
40 using content::WebUI;
41 using content::WebUIController;
42
43 namespace { 32 namespace {
44 33
45 const char kSameAsAnyInstanceURL[] = "about:internets"; 34 const char kSameAsAnyInstanceURL[] = "about:internets";
46 35
47 const char kPrivilegedScheme[] = "privileged"; 36 const char kPrivilegedScheme[] = "privileged";
48 37
49 class SiteInstanceTestWebUIControllerFactory 38 class SiteInstanceTestWebUIControllerFactory
50 : public content::WebUIControllerFactory { 39 : public WebUIControllerFactory {
tfarina 2012/10/29 21:01:30 it fits above.
jam 2012/10/29 23:48:07 Done.
51 public: 40 public:
52 virtual WebUIController* CreateWebUIControllerForURL( 41 virtual WebUIController* CreateWebUIControllerForURL(
53 WebUI* web_ui, const GURL& url) const OVERRIDE { 42 WebUI* web_ui, const GURL& url) const OVERRIDE {
54 return NULL; 43 return NULL;
55 } 44 }
56 virtual WebUI::TypeID GetWebUIType(BrowserContext* browser_context, 45 virtual WebUI::TypeID GetWebUIType(BrowserContext* browser_context,
57 const GURL& url) const OVERRIDE { 46 const GURL& url) const OVERRIDE {
58 return WebUI::kNoWebUI; 47 return WebUI::kNoWebUI;
59 } 48 }
60 virtual bool UseWebUIForURL(BrowserContext* browser_context, 49 virtual bool UseWebUIForURL(BrowserContext* browser_context,
61 const GURL& url) const OVERRIDE { 50 const GURL& url) const OVERRIDE {
62 return content::GetContentClient()->HasWebUIScheme(url); 51 return GetContentClient()->HasWebUIScheme(url);
63 } 52 }
64 virtual bool UseWebUIBindingsForURL(BrowserContext* browser_context, 53 virtual bool UseWebUIBindingsForURL(BrowserContext* browser_context,
65 const GURL& url) const OVERRIDE { 54 const GURL& url) const OVERRIDE {
66 return content::GetContentClient()->HasWebUIScheme(url); 55 return GetContentClient()->HasWebUIScheme(url);
67 } 56 }
68 virtual bool IsURLAcceptableForWebUI( 57 virtual bool IsURLAcceptableForWebUI(
69 BrowserContext* browser_context, 58 BrowserContext* browser_context,
70 const GURL& url, 59 const GURL& url,
71 bool data_urls_allowed) const OVERRIDE { 60 bool data_urls_allowed) const OVERRIDE {
72 return false; 61 return false;
73 } 62 }
74 }; 63 };
75 64
76 class SiteInstanceTestClient : public TestContentClient { 65 class SiteInstanceTestClient : public TestContentClient {
77 public: 66 public:
78 SiteInstanceTestClient() { 67 SiteInstanceTestClient() {
79 } 68 }
80 69
81 virtual bool HasWebUIScheme(const GURL& url) const OVERRIDE { 70 virtual bool HasWebUIScheme(const GURL& url) const OVERRIDE {
82 return url.SchemeIs(chrome::kChromeUIScheme); 71 return url.SchemeIs(chrome::kChromeUIScheme);
83 } 72 }
84 }; 73 };
85 74
86 class SiteInstanceTestBrowserClient : public content::TestContentBrowserClient { 75 class SiteInstanceTestBrowserClient : public TestContentBrowserClient {
87 public: 76 public:
88 SiteInstanceTestBrowserClient() 77 SiteInstanceTestBrowserClient()
89 : privileged_process_id_(-1) { 78 : privileged_process_id_(-1) {
90 } 79 }
91 80
92 virtual content::WebUIControllerFactory* 81 virtual WebUIControllerFactory* GetWebUIControllerFactory() OVERRIDE {
93 GetWebUIControllerFactory() OVERRIDE {
94 return &factory_; 82 return &factory_;
95 } 83 }
96 84
97 virtual bool IsSuitableHost(content::RenderProcessHost* process_host, 85 virtual bool IsSuitableHost(RenderProcessHost* process_host,
98 const GURL& site_url) OVERRIDE { 86 const GURL& site_url) OVERRIDE {
99 return (privileged_process_id_ == process_host->GetID()) == 87 return (privileged_process_id_ == process_host->GetID()) ==
100 site_url.SchemeIs(kPrivilegedScheme); 88 site_url.SchemeIs(kPrivilegedScheme);
101 } 89 }
102 90
103 void set_privileged_process_id(int process_id) { 91 void set_privileged_process_id(int process_id) {
104 privileged_process_id_ = process_id; 92 privileged_process_id_ = process_id;
105 } 93 }
106 94
107 private: 95 private:
108 SiteInstanceTestWebUIControllerFactory factory_; 96 SiteInstanceTestWebUIControllerFactory factory_;
109 int privileged_process_id_; 97 int privileged_process_id_;
110 }; 98 };
111 99
112 class SiteInstanceTest : public testing::Test { 100 class SiteInstanceTest : public testing::Test {
113 public: 101 public:
114 SiteInstanceTest() 102 SiteInstanceTest()
115 : ui_thread_(BrowserThread::UI, &message_loop_), 103 : ui_thread_(BrowserThread::UI, &message_loop_),
116 file_user_blocking_thread_(content::BrowserThread::FILE_USER_BLOCKING, 104 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING,
117 &message_loop_), 105 &message_loop_),
118 io_thread_(content::BrowserThread::IO, &message_loop_), 106 io_thread_(BrowserThread::IO, &message_loop_),
119 old_client_(NULL), 107 old_client_(NULL),
120 old_browser_client_(NULL) { 108 old_browser_client_(NULL) {
121 } 109 }
122 110
123 virtual void SetUp() { 111 virtual void SetUp() {
124 old_client_ = content::GetContentClient(); 112 old_client_ = GetContentClient();
125 old_browser_client_ = content::GetContentClient()->browser(); 113 old_browser_client_ = GetContentClient()->browser();
126 content::SetContentClient(&client_); 114 SetContentClient(&client_);
127 content::GetContentClient()->set_browser_for_testing(&browser_client_); 115 GetContentClient()->set_browser_for_testing(&browser_client_);
128 url_util::AddStandardScheme(kPrivilegedScheme); 116 url_util::AddStandardScheme(kPrivilegedScheme);
129 url_util::AddStandardScheme(chrome::kChromeUIScheme); 117 url_util::AddStandardScheme(chrome::kChromeUIScheme);
130 } 118 }
131 119
132 virtual void TearDown() { 120 virtual void TearDown() {
133 // Ensure that no RenderProcessHosts are left over after the tests. 121 // Ensure that no RenderProcessHosts are left over after the tests.
134 EXPECT_TRUE(content::RenderProcessHost::AllHostsIterator().IsAtEnd()); 122 EXPECT_TRUE(RenderProcessHost::AllHostsIterator().IsAtEnd());
135 123
136 content::GetContentClient()->set_browser_for_testing(old_browser_client_); 124 GetContentClient()->set_browser_for_testing(old_browser_client_);
137 content::SetContentClient(old_client_); 125 SetContentClient(old_client_);
138 126
139 // http://crbug.com/143565 found SiteInstanceTest leaking an 127 // http://crbug.com/143565 found SiteInstanceTest leaking an
140 // AppCacheDatabase. This happens because some part of the test indirectly 128 // AppCacheDatabase. This happens because some part of the test indirectly
141 // calls StoragePartitionImplMap::PostCreateInitialization(), which posts 129 // calls StoragePartitionImplMap::PostCreateInitialization(), which posts
142 // a task to the IO thread to create the AppCacheDatabase. Since the 130 // a task to the IO thread to create the AppCacheDatabase. Since the
143 // message loop is not running, the AppCacheDatabase ends up getting 131 // message loop is not running, the AppCacheDatabase ends up getting
144 // created when DrainMessageLoops() gets called at the end of a test case. 132 // created when DrainMessageLoops() gets called at the end of a test case.
145 // Immediately after, the test case ends and the AppCacheDatabase gets 133 // Immediately after, the test case ends and the AppCacheDatabase gets
146 // scheduled for deletion. Here, call DrainMessageLoops() again so the 134 // scheduled for deletion. Here, call DrainMessageLoops() again so the
147 // AppCacheDatabase actually gets deleted. 135 // AppCacheDatabase actually gets deleted.
148 DrainMessageLoops(); 136 DrainMessageLoops();
149 } 137 }
150 138
151 void set_privileged_process_id(int process_id) { 139 void set_privileged_process_id(int process_id) {
152 browser_client_.set_privileged_process_id(process_id); 140 browser_client_.set_privileged_process_id(process_id);
153 } 141 }
154 142
155 void DrainMessageLoops() { 143 void DrainMessageLoops() {
156 // We don't just do this in TearDown() because we create TestBrowserContext 144 // We don't just do this in TearDown() because we create TestBrowserContext
157 // objects in each test, which will be destructed before 145 // objects in each test, which will be destructed before
158 // TearDown() is called. 146 // TearDown() is called.
159 MessageLoop::current()->RunAllPending(); 147 MessageLoop::current()->RunAllPending();
160 message_loop_.RunAllPending(); 148 message_loop_.RunAllPending();
161 } 149 }
162 150
163 private: 151 private:
164 MessageLoopForUI message_loop_; 152 MessageLoopForUI message_loop_;
165 content::TestBrowserThread ui_thread_; 153 TestBrowserThread ui_thread_;
166 content::TestBrowserThread file_user_blocking_thread_; 154 TestBrowserThread file_user_blocking_thread_;
167 content::TestBrowserThread io_thread_; 155 TestBrowserThread io_thread_;
168 156
169 SiteInstanceTestClient client_; 157 SiteInstanceTestClient client_;
170 SiteInstanceTestBrowserClient browser_client_; 158 SiteInstanceTestBrowserClient browser_client_;
171 content::ContentClient* old_client_; 159 ContentClient* old_client_;
172 content::ContentBrowserClient* old_browser_client_; 160 ContentBrowserClient* old_browser_client_;
173 }; 161 };
174 162
175 // Subclass of BrowsingInstance that updates a counter when deleted and 163 // Subclass of BrowsingInstance that updates a counter when deleted and
176 // returns TestSiteInstances from GetSiteInstanceForURL. 164 // returns TestSiteInstances from GetSiteInstanceForURL.
177 class TestBrowsingInstance : public BrowsingInstance { 165 class TestBrowsingInstance : public BrowsingInstance {
178 public: 166 public:
179 TestBrowsingInstance(BrowserContext* browser_context, int* delete_counter) 167 TestBrowsingInstance(BrowserContext* browser_context, int* delete_counter)
180 : BrowsingInstance(browser_context), 168 : BrowsingInstance(browser_context),
181 delete_counter_(delete_counter) { 169 delete_counter_(delete_counter) {
182 } 170 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 205
218 int* delete_counter_; 206 int* delete_counter_;
219 }; 207 };
220 208
221 } // namespace 209 } // namespace
222 210
223 // Test to ensure no memory leaks for SiteInstance objects. 211 // Test to ensure no memory leaks for SiteInstance objects.
224 TEST_F(SiteInstanceTest, SiteInstanceDestructor) { 212 TEST_F(SiteInstanceTest, SiteInstanceDestructor) {
225 // The existence of this object will cause WebContentsImpl to create our 213 // The existence of this object will cause WebContentsImpl to create our
226 // test one instead of the real one. 214 // test one instead of the real one.
227 content::RenderViewHostTestEnabler rvh_test_enabler; 215 RenderViewHostTestEnabler rvh_test_enabler;
228 int site_delete_counter = 0; 216 int site_delete_counter = 0;
229 int browsing_delete_counter = 0; 217 int browsing_delete_counter = 0;
230 const GURL url("test:foo"); 218 const GURL url("test:foo");
231 219
232 // Ensure that instances are deleted when their NavigationEntries are gone. 220 // Ensure that instances are deleted when their NavigationEntries are gone.
233 TestSiteInstance* instance = 221 TestSiteInstance* instance =
234 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter, 222 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter,
235 &browsing_delete_counter); 223 &browsing_delete_counter);
236 EXPECT_EQ(0, site_delete_counter); 224 EXPECT_EQ(0, site_delete_counter);
237 225
238 NavigationEntryImpl* e1 = new NavigationEntryImpl( 226 NavigationEntryImpl* e1 = new NavigationEntryImpl(
239 instance, 0, url, content::Referrer(), string16(), 227 instance, 0, url, Referrer(), string16(), PAGE_TRANSITION_LINK, false);
240 content::PAGE_TRANSITION_LINK, false);
241 228
242 // Redundantly setting e1's SiteInstance shouldn't affect the ref count. 229 // Redundantly setting e1's SiteInstance shouldn't affect the ref count.
243 e1->set_site_instance(instance); 230 e1->set_site_instance(instance);
244 EXPECT_EQ(0, site_delete_counter); 231 EXPECT_EQ(0, site_delete_counter);
245 232
246 // Add a second reference 233 // Add a second reference
247 NavigationEntryImpl* e2 = new NavigationEntryImpl( 234 NavigationEntryImpl* e2 = new NavigationEntryImpl(
248 instance, 0, url, content::Referrer(), string16(), 235 instance, 0, url, Referrer(), string16(), PAGE_TRANSITION_LINK, false);
249 content::PAGE_TRANSITION_LINK, false);
250 236
251 // Now delete both entries and be sure the SiteInstance goes away. 237 // Now delete both entries and be sure the SiteInstance goes away.
252 delete e1; 238 delete e1;
253 EXPECT_EQ(0, site_delete_counter); 239 EXPECT_EQ(0, site_delete_counter);
254 EXPECT_EQ(0, browsing_delete_counter); 240 EXPECT_EQ(0, browsing_delete_counter);
255 delete e2; 241 delete e2;
256 EXPECT_EQ(1, site_delete_counter); 242 EXPECT_EQ(1, site_delete_counter);
257 // instance is now deleted 243 // instance is now deleted
258 EXPECT_EQ(1, browsing_delete_counter); 244 EXPECT_EQ(1, browsing_delete_counter);
259 // browsing_instance is now deleted 245 // browsing_instance is now deleted
260 246
261 // Ensure that instances are deleted when their RenderViewHosts are gone. 247 // Ensure that instances are deleted when their RenderViewHosts are gone.
262 scoped_ptr<content::TestBrowserContext> browser_context( 248 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
263 new content::TestBrowserContext());
264 instance = 249 instance =
265 TestSiteInstance::CreateTestSiteInstance(browser_context.get(), 250 TestSiteInstance::CreateTestSiteInstance(browser_context.get(),
266 &site_delete_counter, 251 &site_delete_counter,
267 &browsing_delete_counter); 252 &browsing_delete_counter);
268 { 253 {
269 scoped_ptr<WebContentsImpl> web_contents( 254 scoped_ptr<WebContentsImpl> web_contents(
270 WebContentsImpl::Create(browser_context.get(), instance, 255 WebContentsImpl::Create(browser_context.get(), instance,
271 MSG_ROUTING_NONE, NULL)); 256 MSG_ROUTING_NONE, NULL));
272 EXPECT_EQ(1, site_delete_counter); 257 EXPECT_EQ(1, site_delete_counter);
273 EXPECT_EQ(1, browsing_delete_counter); 258 EXPECT_EQ(1, browsing_delete_counter);
(...skipping 18 matching lines...) Expand all
292 const GURL url("test:foo"); 277 const GURL url("test:foo");
293 278
294 SiteInstanceImpl* instance1 = 279 SiteInstanceImpl* instance1 =
295 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter1, 280 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter1,
296 &browsing_delete_counter); 281 &browsing_delete_counter);
297 SiteInstanceImpl* instance2 = 282 SiteInstanceImpl* instance2 =
298 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter2, 283 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter2,
299 &browsing_delete_counter); 284 &browsing_delete_counter);
300 285
301 NavigationEntryImpl* e1 = new NavigationEntryImpl( 286 NavigationEntryImpl* e1 = new NavigationEntryImpl(
302 instance1, 0, url, content::Referrer(), string16(), 287 instance1, 0, url, Referrer(), string16(), PAGE_TRANSITION_LINK, false);
303 content::PAGE_TRANSITION_LINK, false);
304 // Clone the entry 288 // Clone the entry
305 NavigationEntryImpl* e2 = new NavigationEntryImpl(*e1); 289 NavigationEntryImpl* e2 = new NavigationEntryImpl(*e1);
306 290
307 // Should be able to change the SiteInstance of the cloned entry. 291 // Should be able to change the SiteInstance of the cloned entry.
308 e2->set_site_instance(instance2); 292 e2->set_site_instance(instance2);
309 293
310 // The first SiteInstance should go away after deleting e1, since e2 should 294 // The first SiteInstance should go away after deleting e1, since e2 should
311 // no longer be referencing it. 295 // no longer be referencing it.
312 delete e1; 296 delete e1;
313 EXPECT_EQ(1, site_delete_counter1); 297 EXPECT_EQ(1, site_delete_counter1);
314 EXPECT_EQ(0, site_delete_counter2); 298 EXPECT_EQ(0, site_delete_counter2);
315 299
316 // The second SiteInstance should go away after deleting e2. 300 // The second SiteInstance should go away after deleting e2.
317 delete e2; 301 delete e2;
318 EXPECT_EQ(1, site_delete_counter1); 302 EXPECT_EQ(1, site_delete_counter1);
319 EXPECT_EQ(1, site_delete_counter2); 303 EXPECT_EQ(1, site_delete_counter2);
320 304
321 // Both BrowsingInstances are also now deleted 305 // Both BrowsingInstances are also now deleted
322 EXPECT_EQ(2, browsing_delete_counter); 306 EXPECT_EQ(2, browsing_delete_counter);
323 307
324 DrainMessageLoops(); 308 DrainMessageLoops();
325 } 309 }
326 310
327 // Test to ensure GetProcess returns and creates processes correctly. 311 // Test to ensure GetProcess returns and creates processes correctly.
328 TEST_F(SiteInstanceTest, GetProcess) { 312 TEST_F(SiteInstanceTest, GetProcess) {
329 // Ensure that GetProcess returns a process. 313 // Ensure that GetProcess returns a process.
330 scoped_ptr<content::TestBrowserContext> browser_context( 314 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
331 new content::TestBrowserContext()); 315 scoped_ptr<RenderProcessHost> host1;
332 scoped_ptr<content::RenderProcessHost> host1;
333 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>( 316 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>(
334 SiteInstance::Create(browser_context.get()))); 317 SiteInstance::Create(browser_context.get())));
335 host1.reset(instance->GetProcess()); 318 host1.reset(instance->GetProcess());
336 EXPECT_TRUE(host1.get() != NULL); 319 EXPECT_TRUE(host1.get() != NULL);
337 320
338 // Ensure that GetProcess creates a new process. 321 // Ensure that GetProcess creates a new process.
339 scoped_refptr<SiteInstanceImpl> instance2(static_cast<SiteInstanceImpl*>( 322 scoped_refptr<SiteInstanceImpl> instance2(static_cast<SiteInstanceImpl*>(
340 SiteInstance::Create(browser_context.get()))); 323 SiteInstance::Create(browser_context.get())));
341 scoped_ptr<content::RenderProcessHost> host2(instance2->GetProcess()); 324 scoped_ptr<RenderProcessHost> host2(instance2->GetProcess());
342 EXPECT_TRUE(host2.get() != NULL); 325 EXPECT_TRUE(host2.get() != NULL);
343 EXPECT_NE(host1.get(), host2.get()); 326 EXPECT_NE(host1.get(), host2.get());
344 327
345 DrainMessageLoops(); 328 DrainMessageLoops();
346 } 329 }
347 330
348 // Test to ensure SetSite and site() work properly. 331 // Test to ensure SetSite and site() work properly.
349 TEST_F(SiteInstanceTest, SetSite) { 332 TEST_F(SiteInstanceTest, SetSite) {
350 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>( 333 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>(
351 SiteInstance::Create(NULL))); 334 SiteInstance::Create(NULL)));
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 405
423 DrainMessageLoops(); 406 DrainMessageLoops();
424 } 407 }
425 408
426 // Test to ensure that there is only one SiteInstance per site in a given 409 // Test to ensure that there is only one SiteInstance per site in a given
427 // BrowsingInstance, when process-per-site is not in use. 410 // BrowsingInstance, when process-per-site is not in use.
428 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) { 411 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) {
429 ASSERT_FALSE(CommandLine::ForCurrentProcess()->HasSwitch( 412 ASSERT_FALSE(CommandLine::ForCurrentProcess()->HasSwitch(
430 switches::kProcessPerSite)); 413 switches::kProcessPerSite));
431 int delete_counter = 0; 414 int delete_counter = 0;
432 scoped_ptr<content::TestBrowserContext> browser_context( 415 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
433 new content::TestBrowserContext());
434 TestBrowsingInstance* browsing_instance = 416 TestBrowsingInstance* browsing_instance =
435 new TestBrowsingInstance(browser_context.get(), &delete_counter); 417 new TestBrowsingInstance(browser_context.get(), &delete_counter);
436 418
437 const GURL url_a1("http://www.google.com/1.html"); 419 const GURL url_a1("http://www.google.com/1.html");
438 scoped_refptr<SiteInstanceImpl> site_instance_a1( 420 scoped_refptr<SiteInstanceImpl> site_instance_a1(
439 static_cast<SiteInstanceImpl*>( 421 static_cast<SiteInstanceImpl*>(
440 browsing_instance->GetSiteInstanceForURL(url_a1))); 422 browsing_instance->GetSiteInstanceForURL(url_a1)));
441 EXPECT_TRUE(site_instance_a1.get() != NULL); 423 EXPECT_TRUE(site_instance_a1.get() != NULL);
442 424
443 // A separate site should create a separate SiteInstance. 425 // A separate site should create a separate SiteInstance.
(...skipping 22 matching lines...) Expand all
466 new TestBrowsingInstance(browser_context.get(), &delete_counter); 448 new TestBrowsingInstance(browser_context.get(), &delete_counter);
467 // Ensure the new SiteInstance is ref counted so that it gets deleted. 449 // Ensure the new SiteInstance is ref counted so that it gets deleted.
468 scoped_refptr<SiteInstanceImpl> site_instance_a2_2( 450 scoped_refptr<SiteInstanceImpl> site_instance_a2_2(
469 static_cast<SiteInstanceImpl*>( 451 static_cast<SiteInstanceImpl*>(
470 browsing_instance2->GetSiteInstanceForURL(url_a2))); 452 browsing_instance2->GetSiteInstanceForURL(url_a2)));
471 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get()); 453 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get());
472 EXPECT_FALSE(site_instance_a1->IsRelatedSiteInstance(site_instance_a2_2)); 454 EXPECT_FALSE(site_instance_a1->IsRelatedSiteInstance(site_instance_a2_2));
473 455
474 // The two SiteInstances for http://google.com should not use the same process 456 // The two SiteInstances for http://google.com should not use the same process
475 // if process-per-site is not enabled. 457 // if process-per-site is not enabled.
476 scoped_ptr<content::RenderProcessHost> process_a1( 458 scoped_ptr<RenderProcessHost> process_a1(site_instance_a1->GetProcess());
477 site_instance_a1->GetProcess()); 459 scoped_ptr<RenderProcessHost> process_a2_2(site_instance_a2_2->GetProcess());
478 scoped_ptr<content::RenderProcessHost> process_a2_2(
479 site_instance_a2_2->GetProcess());
480 EXPECT_NE(process_a1.get(), process_a2_2.get()); 460 EXPECT_NE(process_a1.get(), process_a2_2.get());
481 461
482 // Should be able to see that we do have SiteInstances. 462 // Should be able to see that we do have SiteInstances.
483 EXPECT_TRUE(browsing_instance->HasSiteInstance( 463 EXPECT_TRUE(browsing_instance->HasSiteInstance(
484 GURL("http://mail.google.com"))); 464 GURL("http://mail.google.com")));
485 EXPECT_TRUE(browsing_instance2->HasSiteInstance( 465 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
486 GURL("http://mail.google.com"))); 466 GURL("http://mail.google.com")));
487 EXPECT_TRUE(browsing_instance->HasSiteInstance( 467 EXPECT_TRUE(browsing_instance->HasSiteInstance(
488 GURL("http://mail.yahoo.com"))); 468 GURL("http://mail.yahoo.com")));
489 469
490 // Should be able to see that we don't have SiteInstances. 470 // Should be able to see that we don't have SiteInstances.
491 EXPECT_FALSE(browsing_instance->HasSiteInstance( 471 EXPECT_FALSE(browsing_instance->HasSiteInstance(
492 GURL("https://www.google.com"))); 472 GURL("https://www.google.com")));
493 EXPECT_FALSE(browsing_instance2->HasSiteInstance( 473 EXPECT_FALSE(browsing_instance2->HasSiteInstance(
494 GURL("http://www.yahoo.com"))); 474 GURL("http://www.yahoo.com")));
495 475
496 // browsing_instances will be deleted when their SiteInstances are deleted. 476 // browsing_instances will be deleted when their SiteInstances are deleted.
497 // The processes will be unregistered when the RPH scoped_ptrs go away. 477 // The processes will be unregistered when the RPH scoped_ptrs go away.
498 478
499 DrainMessageLoops(); 479 DrainMessageLoops();
500 } 480 }
501 481
502 // Test to ensure that there is only one RenderProcessHost per site for an 482 // Test to ensure that there is only one RenderProcessHost per site for an
503 // entire BrowserContext, if process-per-site is in use. 483 // entire BrowserContext, if process-per-site is in use.
504 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) { 484 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) {
505 CommandLine::ForCurrentProcess()->AppendSwitch( 485 CommandLine::ForCurrentProcess()->AppendSwitch(
506 switches::kProcessPerSite); 486 switches::kProcessPerSite);
507 int delete_counter = 0; 487 int delete_counter = 0;
508 scoped_ptr<content::TestBrowserContext> browser_context( 488 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
509 new content::TestBrowserContext());
510 TestBrowsingInstance* browsing_instance = 489 TestBrowsingInstance* browsing_instance =
511 new TestBrowsingInstance(browser_context.get(), &delete_counter); 490 new TestBrowsingInstance(browser_context.get(), &delete_counter);
512 491
513 const GURL url_a1("http://www.google.com/1.html"); 492 const GURL url_a1("http://www.google.com/1.html");
514 scoped_refptr<SiteInstanceImpl> site_instance_a1( 493 scoped_refptr<SiteInstanceImpl> site_instance_a1(
515 static_cast<SiteInstanceImpl*>( 494 static_cast<SiteInstanceImpl*>(
516 browsing_instance->GetSiteInstanceForURL(url_a1))); 495 browsing_instance->GetSiteInstanceForURL(url_a1)));
517 EXPECT_TRUE(site_instance_a1.get() != NULL); 496 EXPECT_TRUE(site_instance_a1.get() != NULL);
518 scoped_ptr<content::RenderProcessHost> process_a1( 497 scoped_ptr<RenderProcessHost> process_a1(site_instance_a1->GetProcess());
519 site_instance_a1->GetProcess());
520 498
521 // A separate site should create a separate SiteInstance. 499 // A separate site should create a separate SiteInstance.
522 const GURL url_b1("http://www.yahoo.com/"); 500 const GURL url_b1("http://www.yahoo.com/");
523 scoped_refptr<SiteInstanceImpl> site_instance_b1( 501 scoped_refptr<SiteInstanceImpl> site_instance_b1(
524 static_cast<SiteInstanceImpl*>( 502 static_cast<SiteInstanceImpl*>(
525 browsing_instance->GetSiteInstanceForURL(url_b1))); 503 browsing_instance->GetSiteInstanceForURL(url_b1)));
526 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); 504 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get());
527 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1)); 505 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1));
528 506
529 // Getting the new SiteInstance from the BrowsingInstance and from another 507 // Getting the new SiteInstance from the BrowsingInstance and from another
(...skipping 14 matching lines...) Expand all
544 new TestBrowsingInstance(browser_context.get(), &delete_counter); 522 new TestBrowsingInstance(browser_context.get(), &delete_counter);
545 scoped_refptr<SiteInstanceImpl> site_instance_a1_2( 523 scoped_refptr<SiteInstanceImpl> site_instance_a1_2(
546 static_cast<SiteInstanceImpl*>( 524 static_cast<SiteInstanceImpl*>(
547 browsing_instance2->GetSiteInstanceForURL(url_a1))); 525 browsing_instance2->GetSiteInstanceForURL(url_a1)));
548 EXPECT_TRUE(site_instance_a1.get() != NULL); 526 EXPECT_TRUE(site_instance_a1.get() != NULL);
549 EXPECT_NE(site_instance_a1.get(), site_instance_a1_2.get()); 527 EXPECT_NE(site_instance_a1.get(), site_instance_a1_2.get());
550 EXPECT_EQ(process_a1.get(), site_instance_a1_2->GetProcess()); 528 EXPECT_EQ(process_a1.get(), site_instance_a1_2->GetProcess());
551 529
552 // A visit to the original site in a new BrowsingInstance (different browser 530 // A visit to the original site in a new BrowsingInstance (different browser
553 // context) should return a different SiteInstance with a different process. 531 // context) should return a different SiteInstance with a different process.
554 scoped_ptr<content::TestBrowserContext> browser_context2( 532 scoped_ptr<TestBrowserContext> browser_context2(new TestBrowserContext());
555 new content::TestBrowserContext());
556 TestBrowsingInstance* browsing_instance3 = 533 TestBrowsingInstance* browsing_instance3 =
557 new TestBrowsingInstance(browser_context2.get(), &delete_counter); 534 new TestBrowsingInstance(browser_context2.get(), &delete_counter);
558 scoped_refptr<SiteInstanceImpl> site_instance_a2_3( 535 scoped_refptr<SiteInstanceImpl> site_instance_a2_3(
559 static_cast<SiteInstanceImpl*>( 536 static_cast<SiteInstanceImpl*>(
560 browsing_instance3->GetSiteInstanceForURL(url_a2))); 537 browsing_instance3->GetSiteInstanceForURL(url_a2)));
561 EXPECT_TRUE(site_instance_a2_3.get() != NULL); 538 EXPECT_TRUE(site_instance_a2_3.get() != NULL);
562 scoped_ptr<content::RenderProcessHost> process_a2_3( 539 scoped_ptr<RenderProcessHost> process_a2_3(site_instance_a2_3->GetProcess());
563 site_instance_a2_3->GetProcess());
564 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get()); 540 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get());
565 EXPECT_NE(process_a1.get(), process_a2_3.get()); 541 EXPECT_NE(process_a1.get(), process_a2_3.get());
566 542
567 // Should be able to see that we do have SiteInstances. 543 // Should be able to see that we do have SiteInstances.
568 EXPECT_TRUE(browsing_instance->HasSiteInstance( 544 EXPECT_TRUE(browsing_instance->HasSiteInstance(
569 GURL("http://mail.google.com"))); // visited before 545 GURL("http://mail.google.com"))); // visited before
570 EXPECT_TRUE(browsing_instance2->HasSiteInstance( 546 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
571 GURL("http://mail.google.com"))); // visited before 547 GURL("http://mail.google.com"))); // visited before
572 EXPECT_TRUE(browsing_instance->HasSiteInstance( 548 EXPECT_TRUE(browsing_instance->HasSiteInstance(
573 GURL("http://mail.yahoo.com"))); // visited before 549 GURL("http://mail.yahoo.com"))); // visited before
574 550
575 // Should be able to see that we don't have SiteInstances. 551 // Should be able to see that we don't have SiteInstances.
576 EXPECT_FALSE(browsing_instance2->HasSiteInstance( 552 EXPECT_FALSE(browsing_instance2->HasSiteInstance(
577 GURL("http://www.yahoo.com"))); // different BI, same browser context 553 GURL("http://www.yahoo.com"))); // different BI, same browser context
578 EXPECT_FALSE(browsing_instance->HasSiteInstance( 554 EXPECT_FALSE(browsing_instance->HasSiteInstance(
579 GURL("https://www.google.com"))); // not visited before 555 GURL("https://www.google.com"))); // not visited before
580 EXPECT_FALSE(browsing_instance3->HasSiteInstance( 556 EXPECT_FALSE(browsing_instance3->HasSiteInstance(
581 GURL("http://www.yahoo.com"))); // different BI, different context 557 GURL("http://www.yahoo.com"))); // different BI, different context
582 558
583 // browsing_instances will be deleted when their SiteInstances are deleted. 559 // browsing_instances will be deleted when their SiteInstances are deleted.
584 // The processes will be unregistered when the RPH scoped_ptrs go away. 560 // The processes will be unregistered when the RPH scoped_ptrs go away.
585 561
586 DrainMessageLoops(); 562 DrainMessageLoops();
587 } 563 }
588 564
589 static SiteInstanceImpl* CreateSiteInstance( 565 static SiteInstanceImpl* CreateSiteInstance(
590 content::BrowserContext* browser_context, 566 BrowserContext* browser_context,
591 content::RenderProcessHostFactory* factory, 567 RenderProcessHostFactory* factory,
592 const GURL& url) { 568 const GURL& url) {
593 SiteInstanceImpl* instance = 569 SiteInstanceImpl* instance =
594 reinterpret_cast<SiteInstanceImpl*>( 570 reinterpret_cast<SiteInstanceImpl*>(
595 SiteInstance::CreateForURL(browser_context, url)); 571 SiteInstance::CreateForURL(browser_context, url));
596 instance->set_render_process_host_factory(factory); 572 instance->set_render_process_host_factory(factory);
597 return instance; 573 return instance;
598 } 574 }
599 575
600 // Test to ensure that pages that require certain privileges are grouped 576 // Test to ensure that pages that require certain privileges are grouped
601 // in processes with similar pages. 577 // in processes with similar pages.
602 TEST_F(SiteInstanceTest, ProcessSharingByType) { 578 TEST_F(SiteInstanceTest, ProcessSharingByType) {
603 MockRenderProcessHostFactory rph_factory; 579 MockRenderProcessHostFactory rph_factory;
604 ChildProcessSecurityPolicyImpl* policy = 580 ChildProcessSecurityPolicyImpl* policy =
605 ChildProcessSecurityPolicyImpl::GetInstance(); 581 ChildProcessSecurityPolicyImpl::GetInstance();
606 582
607 // Make a bunch of mock renderers so that we hit the limit. 583 // Make a bunch of mock renderers so that we hit the limit.
608 scoped_ptr<content::TestBrowserContext> browser_context( 584 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
609 new content::TestBrowserContext());
610 std::vector<MockRenderProcessHost*> hosts; 585 std::vector<MockRenderProcessHost*> hosts;
611 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) 586 for (size_t i = 0; i < kMaxRendererProcessCount; ++i)
612 hosts.push_back(new MockRenderProcessHost(browser_context.get())); 587 hosts.push_back(new MockRenderProcessHost(browser_context.get()));
613 588
614 // Create some extension instances and make sure they share a process. 589 // Create some extension instances and make sure they share a process.
615 scoped_refptr<SiteInstanceImpl> extension1_instance( 590 scoped_refptr<SiteInstanceImpl> extension1_instance(
616 CreateSiteInstance(browser_context.get(), &rph_factory, 591 CreateSiteInstance(browser_context.get(), &rph_factory,
617 GURL(kPrivilegedScheme + std::string("://foo/bar")))); 592 GURL(kPrivilegedScheme + std::string("://foo/bar"))));
618 set_privileged_process_id(extension1_instance->GetProcess()->GetID()); 593 set_privileged_process_id(extension1_instance->GetProcess()->GetID());
619 594
620 scoped_refptr<SiteInstanceImpl> extension2_instance( 595 scoped_refptr<SiteInstanceImpl> extension2_instance(
621 CreateSiteInstance(browser_context.get(), &rph_factory, 596 CreateSiteInstance(browser_context.get(), &rph_factory,
622 GURL(kPrivilegedScheme + std::string("://baz/bar")))); 597 GURL(kPrivilegedScheme + std::string("://baz/bar"))));
623 598
624 scoped_ptr<content::RenderProcessHost> extension_host( 599 scoped_ptr<RenderProcessHost> extension_host(
625 extension1_instance->GetProcess()); 600 extension1_instance->GetProcess());
626 EXPECT_EQ(extension1_instance->GetProcess(), 601 EXPECT_EQ(extension1_instance->GetProcess(),
627 extension2_instance->GetProcess()); 602 extension2_instance->GetProcess());
628 603
629 // Create some WebUI instances and make sure they share a process. 604 // Create some WebUI instances and make sure they share a process.
630 scoped_refptr<SiteInstanceImpl> webui1_instance(CreateSiteInstance( 605 scoped_refptr<SiteInstanceImpl> webui1_instance(CreateSiteInstance(
631 browser_context.get(), &rph_factory, 606 browser_context.get(), &rph_factory,
632 GURL(chrome::kChromeUIScheme + std::string("://newtab")))); 607 GURL(chrome::kChromeUIScheme + std::string("://newtab"))));
633 policy->GrantWebUIBindings(webui1_instance->GetProcess()->GetID()); 608 policy->GrantWebUIBindings(webui1_instance->GetProcess()->GetID());
634 609
635 scoped_refptr<SiteInstanceImpl> webui2_instance(CreateSiteInstance( 610 scoped_refptr<SiteInstanceImpl> webui2_instance(CreateSiteInstance(
636 browser_context.get(), &rph_factory, 611 browser_context.get(), &rph_factory,
637 GURL(chrome::kChromeUIScheme + std::string("://history")))); 612 GURL(chrome::kChromeUIScheme + std::string("://history"))));
638 613
639 scoped_ptr<content::RenderProcessHost> dom_host( 614 scoped_ptr<RenderProcessHost> dom_host(webui1_instance->GetProcess());
640 webui1_instance->GetProcess());
641 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess()); 615 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess());
642 616
643 // Make sure none of differing privilege processes are mixed. 617 // Make sure none of differing privilege processes are mixed.
644 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess()); 618 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess());
645 619
646 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) { 620 for (size_t i = 0; i < kMaxRendererProcessCount; ++i) {
647 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]); 621 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]);
648 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]); 622 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]);
649 } 623 }
650 624
651 STLDeleteContainerPointers(hosts.begin(), hosts.end()); 625 STLDeleteContainerPointers(hosts.begin(), hosts.end());
652 626
653 DrainMessageLoops(); 627 DrainMessageLoops();
654 } 628 }
655 629
656 // Test to ensure that HasWrongProcessForURL behaves properly for different 630 // Test to ensure that HasWrongProcessForURL behaves properly for different
657 // types of URLs. 631 // types of URLs.
658 TEST_F(SiteInstanceTest, HasWrongProcessForURL) { 632 TEST_F(SiteInstanceTest, HasWrongProcessForURL) {
659 scoped_ptr<content::TestBrowserContext> browser_context( 633 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
660 new content::TestBrowserContext()); 634 scoped_ptr<RenderProcessHost> host;
661 scoped_ptr<content::RenderProcessHost> host;
662 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>( 635 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>(
663 SiteInstance::Create(browser_context.get()))); 636 SiteInstance::Create(browser_context.get())));
664 637
665 EXPECT_FALSE(instance->HasSite()); 638 EXPECT_FALSE(instance->HasSite());
666 EXPECT_TRUE(instance->GetSiteURL().is_empty()); 639 EXPECT_TRUE(instance->GetSiteURL().is_empty());
667 640
668 instance->SetSite(GURL("http://evernote.com/")); 641 instance->SetSite(GURL("http://evernote.com/"));
669 EXPECT_TRUE(instance->HasSite()); 642 EXPECT_TRUE(instance->HasSite());
670 643
671 // Check prior to "assigning" a process to the instance, which is expected 644 // Check prior to "assigning" a process to the instance, which is expected
672 // to return false due to not being attached to any process yet. 645 // to return false due to not being attached to any process yet.
673 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com"))); 646 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com")));
674 647
675 // The call to GetProcess actually creates a new real process, which works 648 // The call to GetProcess actually creates a new real process, which works
676 // fine, but might be a cause for problems in different contexts. 649 // fine, but might be a cause for problems in different contexts.
677 host.reset(instance->GetProcess()); 650 host.reset(instance->GetProcess());
678 EXPECT_TRUE(host.get() != NULL); 651 EXPECT_TRUE(host.get() != NULL);
679 EXPECT_TRUE(instance->HasProcess()); 652 EXPECT_TRUE(instance->HasProcess());
680 653
681 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com"))); 654 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com")));
682 EXPECT_FALSE(instance->HasWrongProcessForURL( 655 EXPECT_FALSE(instance->HasWrongProcessForURL(
683 GURL("javascript:alert(document.location.href);"))); 656 GURL("javascript:alert(document.location.href);")));
684 657
685 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://settings"))); 658 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://settings")));
686 659
687 DrainMessageLoops(); 660 DrainMessageLoops();
688 } 661 }
662
663 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698