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

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

Issue 1845233003: SiteInstance unittest improvements (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@doghouse_now
Patch Set: Created 4 years, 8 months 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
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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_vector.h" 9 #include "base/memory/scoped_vector.h"
10 #include "base/strings/string16.h" 10 #include "base/strings/string16.h"
(...skipping 16 matching lines...) Expand all
27 #include "content/public/test/test_browser_context.h" 27 #include "content/public/test/test_browser_context.h"
28 #include "content/public/test/test_browser_thread.h" 28 #include "content/public/test/test_browser_thread.h"
29 #include "content/public/test/test_utils.h" 29 #include "content/public/test/test_utils.h"
30 #include "content/test/test_content_browser_client.h" 30 #include "content/test/test_content_browser_client.h"
31 #include "content/test/test_content_client.h" 31 #include "content/test/test_content_client.h"
32 #include "content/test/test_render_view_host.h" 32 #include "content/test/test_render_view_host.h"
33 #include "testing/gtest/include/gtest/gtest.h" 33 #include "testing/gtest/include/gtest/gtest.h"
34 #include "url/url_util.h" 34 #include "url/url_util.h"
35 35
36 namespace content { 36 namespace content {
37 namespace {
38 37
39 const char kPrivilegedScheme[] = "privileged"; 38 const char kPrivilegedScheme[] = "privileged";
40 39
41 class SiteInstanceTestBrowserClient : public TestContentBrowserClient { 40 class SiteInstanceTestBrowserClient : public TestContentBrowserClient {
42 public: 41 public:
43 SiteInstanceTestBrowserClient() 42 explicit SiteInstanceTestBrowserClient()
44 : privileged_process_id_(-1) { 43 : privileged_process_id_(-1),
44 site_instance_delete_count_(0),
45 browsing_instance_delete_count_(0) {
45 WebUIControllerFactory::RegisterFactory( 46 WebUIControllerFactory::RegisterFactory(
46 ContentWebUIControllerFactory::GetInstance()); 47 ContentWebUIControllerFactory::GetInstance());
47 } 48 }
48 49
49 ~SiteInstanceTestBrowserClient() override { 50 ~SiteInstanceTestBrowserClient() override {
50 WebUIControllerFactory::UnregisterFactoryForTesting( 51 WebUIControllerFactory::UnregisterFactoryForTesting(
51 ContentWebUIControllerFactory::GetInstance()); 52 ContentWebUIControllerFactory::GetInstance());
52 } 53 }
53 54
54 bool IsSuitableHost(RenderProcessHost* process_host, 55 bool IsSuitableHost(RenderProcessHost* process_host,
55 const GURL& site_url) override { 56 const GURL& site_url) override {
56 return (privileged_process_id_ == process_host->GetID()) == 57 return (privileged_process_id_ == process_host->GetID()) ==
57 site_url.SchemeIs(kPrivilegedScheme); 58 site_url.SchemeIs(kPrivilegedScheme);
58 } 59 }
59 60
60 void set_privileged_process_id(int process_id) { 61 void set_privileged_process_id(int process_id) {
61 privileged_process_id_ = process_id; 62 privileged_process_id_ = process_id;
62 } 63 }
63 64
65 void SiteInstanceDeleting(content::SiteInstance* site_instance) override {
Charlie Reis 2016/04/01 21:50:41 Cool! Nice use of this method, which we didn't ha
ncarter (slow) 2016/04/04 21:47:33 Done.
66 site_instance_delete_count_++;
67 // Infer deletion of the browsing instance.
68 if (static_cast<SiteInstanceImpl*>(site_instance)
69 ->browsing_instance_->HasOneRef()) {
70 browsing_instance_delete_count_++;
71 }
72 }
73
74 int GetAndClearSiteInstanceDeleteCount() {
75 int result = site_instance_delete_count_;
76 site_instance_delete_count_ = 0;
77 return result;
78 }
79 int GetAndClearBrowsingInstanceDeleteCount() {
80 int result = browsing_instance_delete_count_;
81 browsing_instance_delete_count_ = 0;
82 return result;
83 }
84
64 private: 85 private:
65 int privileged_process_id_; 86 int privileged_process_id_;
87
88 int site_instance_delete_count_;
89 int browsing_instance_delete_count_;
66 }; 90 };
67 91
92 namespace {
93
68 class SiteInstanceTest : public testing::Test { 94 class SiteInstanceTest : public testing::Test {
69 public: 95 public:
70 SiteInstanceTest() 96 SiteInstanceTest()
71 : ui_thread_(BrowserThread::UI, &message_loop_), 97 : ui_thread_(BrowserThread::UI, &message_loop_),
72 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING, 98 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING,
73 &message_loop_), 99 &message_loop_),
74 io_thread_(BrowserThread::IO, &message_loop_), 100 io_thread_(BrowserThread::IO, &message_loop_),
75 old_browser_client_(NULL) { 101 old_browser_client_(NULL) {
76 } 102 }
77 103
(...skipping 29 matching lines...) Expand all
107 } 133 }
108 134
109 void DrainMessageLoops() { 135 void DrainMessageLoops() {
110 // We don't just do this in TearDown() because we create TestBrowserContext 136 // We don't just do this in TearDown() because we create TestBrowserContext
111 // objects in each test, which will be destructed before 137 // objects in each test, which will be destructed before
112 // TearDown() is called. 138 // TearDown() is called.
113 base::MessageLoop::current()->RunUntilIdle(); 139 base::MessageLoop::current()->RunUntilIdle();
114 message_loop_.RunUntilIdle(); 140 message_loop_.RunUntilIdle();
115 } 141 }
116 142
143 SiteInstanceTestBrowserClient* browser_client() { return &browser_client_; }
144
117 private: 145 private:
118 base::MessageLoopForUI message_loop_; 146 base::MessageLoopForUI message_loop_;
119 TestBrowserThread ui_thread_; 147 TestBrowserThread ui_thread_;
120 TestBrowserThread file_user_blocking_thread_; 148 TestBrowserThread file_user_blocking_thread_;
121 TestBrowserThread io_thread_; 149 TestBrowserThread io_thread_;
122 150
123 SiteInstanceTestBrowserClient browser_client_; 151 SiteInstanceTestBrowserClient browser_client_;
124 ContentBrowserClient* old_browser_client_; 152 ContentBrowserClient* old_browser_client_;
125 MockRenderProcessHostFactory rph_factory_; 153 MockRenderProcessHostFactory rph_factory_;
126 }; 154 };
127 155
128 // Subclass of BrowsingInstance that updates a counter when deleted and
129 // returns TestSiteInstances from GetSiteInstanceForURL.
130 class TestBrowsingInstance : public BrowsingInstance {
131 public:
132 TestBrowsingInstance(BrowserContext* browser_context, int* delete_counter)
133 : BrowsingInstance(browser_context),
134 delete_counter_(delete_counter) {
135 }
136
137 // Make a few methods public for tests.
138 using BrowsingInstance::browser_context;
139 using BrowsingInstance::HasSiteInstance;
140 using BrowsingInstance::GetSiteInstanceForURL;
141 using BrowsingInstance::RegisterSiteInstance;
142 using BrowsingInstance::UnregisterSiteInstance;
143
144 private:
145 ~TestBrowsingInstance() override { (*delete_counter_)++; }
146
147 int* delete_counter_;
148 };
149
150 // Subclass of SiteInstanceImpl that updates a counter when deleted.
151 class TestSiteInstance : public SiteInstanceImpl {
152 public:
153 static TestSiteInstance* CreateTestSiteInstance(
154 BrowserContext* browser_context,
155 int* site_delete_counter,
156 int* browsing_delete_counter) {
157 TestBrowsingInstance* browsing_instance =
158 new TestBrowsingInstance(browser_context, browsing_delete_counter);
159 return new TestSiteInstance(browsing_instance, site_delete_counter);
160 }
161
162 private:
163 TestSiteInstance(BrowsingInstance* browsing_instance, int* delete_counter)
164 : SiteInstanceImpl(browsing_instance), delete_counter_(delete_counter) {}
165 ~TestSiteInstance() override { (*delete_counter_)++; }
166
167 int* delete_counter_;
168 };
169
170 } // namespace 156 } // namespace
171 157
172 // Test to ensure no memory leaks for SiteInstance objects. 158 // Test to ensure no memory leaks for SiteInstance objects.
173 TEST_F(SiteInstanceTest, SiteInstanceDestructor) { 159 TEST_F(SiteInstanceTest, SiteInstanceDestructor) {
174 // The existence of this object will cause WebContentsImpl to create our 160 // The existence of this object will cause WebContentsImpl to create our
175 // test one instead of the real one. 161 // test one instead of the real one.
176 RenderViewHostTestEnabler rvh_test_enabler; 162 RenderViewHostTestEnabler rvh_test_enabler;
177 int site_delete_counter = 0;
178 int browsing_delete_counter = 0;
179 const GURL url("test:foo"); 163 const GURL url("test:foo");
180 164
181 // Ensure that instances are deleted when their NavigationEntries are gone. 165 // Ensure that instances are deleted when their NavigationEntries are gone.
182 TestSiteInstance* instance = 166 scoped_refptr<SiteInstanceImpl> instance = SiteInstanceImpl::Create(NULL);
Charlie Reis 2016/04/01 21:50:41 nit: nullptr while we're at it? (Same below.)
ncarter (slow) 2016/04/04 21:47:33 NULL->nullptr replaced globally in the file.
183 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter, 167 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount());
184 &browsing_delete_counter);
185 EXPECT_EQ(0, site_delete_counter);
186 168
187 NavigationEntryImpl* e1 = new NavigationEntryImpl( 169 NavigationEntryImpl* e1 = new NavigationEntryImpl(
188 instance, 0, url, Referrer(), base::string16(), ui::PAGE_TRANSITION_LINK, 170 instance, 0, url, Referrer(), base::string16(), ui::PAGE_TRANSITION_LINK,
189 false); 171 false);
190 172
191 // Redundantly setting e1's SiteInstance shouldn't affect the ref count. 173 // Redundantly setting e1's SiteInstance shouldn't affect the ref count.
192 e1->set_site_instance(instance); 174 e1->set_site_instance(instance);
193 EXPECT_EQ(0, site_delete_counter); 175 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount());
176 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount());
194 177
195 // Add a second reference 178 // Add a second reference
196 NavigationEntryImpl* e2 = new NavigationEntryImpl( 179 NavigationEntryImpl* e2 = new NavigationEntryImpl(
197 instance, 0, url, Referrer(), base::string16(), ui::PAGE_TRANSITION_LINK, 180 instance, 0, url, Referrer(), base::string16(), ui::PAGE_TRANSITION_LINK,
198 false); 181 false);
199 182
183 instance = nullptr;
184 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount());
185 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount());
186
200 // Now delete both entries and be sure the SiteInstance goes away. 187 // Now delete both entries and be sure the SiteInstance goes away.
201 delete e1; 188 delete e1;
202 EXPECT_EQ(0, site_delete_counter); 189 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount());
203 EXPECT_EQ(0, browsing_delete_counter); 190 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount());
204 delete e2; 191 delete e2;
205 EXPECT_EQ(1, site_delete_counter);
206 // instance is now deleted 192 // instance is now deleted
207 EXPECT_EQ(1, browsing_delete_counter); 193 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount());
194 EXPECT_EQ(1, browser_client()->GetAndClearBrowsingInstanceDeleteCount());
208 // browsing_instance is now deleted 195 // browsing_instance is now deleted
209 196
210 // Ensure that instances are deleted when their RenderViewHosts are gone. 197 // Ensure that instances are deleted when their RenderViewHosts are gone.
211 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); 198 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
212 instance =
213 TestSiteInstance::CreateTestSiteInstance(browser_context.get(),
214 &site_delete_counter,
215 &browsing_delete_counter);
216 { 199 {
217 scoped_ptr<WebContentsImpl> web_contents(static_cast<WebContentsImpl*>( 200 scoped_ptr<WebContentsImpl> web_contents(static_cast<WebContentsImpl*>(
218 WebContents::Create(WebContents::CreateParams( 201 WebContents::Create(WebContents::CreateParams(
219 browser_context.get(), instance)))); 202 browser_context.get(),
220 EXPECT_EQ(1, site_delete_counter); 203 SiteInstance::Create(browser_context.get())))));
221 EXPECT_EQ(1, browsing_delete_counter); 204 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount());
205 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount());
222 } 206 }
223 207
224 // Make sure that we flush any messages related to the above WebContentsImpl 208 // Make sure that we flush any messages related to the above WebContentsImpl
225 // destruction. 209 // destruction.
226 DrainMessageLoops(); 210 DrainMessageLoops();
227 211
228 EXPECT_EQ(2, site_delete_counter); 212 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount());
229 EXPECT_EQ(2, browsing_delete_counter); 213 EXPECT_EQ(1, browser_client()->GetAndClearBrowsingInstanceDeleteCount());
230 // contents is now deleted, along with instance and browsing_instance 214 // contents is now deleted, along with instance and browsing_instance
231 } 215 }
232 216
233 // Test that NavigationEntries with SiteInstances can be cloned, but that their 217 // Test that NavigationEntries with SiteInstances can be cloned, but that their
234 // SiteInstances can be changed afterwards. Also tests that the ref counts are 218 // SiteInstances can be changed afterwards. Also tests that the ref counts are
235 // updated properly after the change. 219 // updated properly after the change.
236 TEST_F(SiteInstanceTest, CloneNavigationEntry) { 220 TEST_F(SiteInstanceTest, CloneNavigationEntry) {
237 int site_delete_counter1 = 0;
238 int site_delete_counter2 = 0;
239 int browsing_delete_counter = 0;
240 const GURL url("test:foo"); 221 const GURL url("test:foo");
241 222
242 SiteInstanceImpl* instance1 = 223 scoped_ptr<NavigationEntryImpl> e1 = make_scoped_ptr(new NavigationEntryImpl(
243 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter1, 224 SiteInstanceImpl::Create(NULL), 0, url, Referrer(), base::string16(),
244 &browsing_delete_counter); 225 ui::PAGE_TRANSITION_LINK, false));
245 SiteInstanceImpl* instance2 =
246 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter2,
247 &browsing_delete_counter);
248 226
249 scoped_ptr<NavigationEntryImpl> e1 = make_scoped_ptr(new NavigationEntryImpl(
250 instance1, 0, url, Referrer(), base::string16(), ui::PAGE_TRANSITION_LINK,
251 false));
252 // Clone the entry. 227 // Clone the entry.
253 scoped_ptr<NavigationEntryImpl> e2 = e1->Clone(); 228 scoped_ptr<NavigationEntryImpl> e2 = e1->Clone();
254 229
255 // Should be able to change the SiteInstance of the cloned entry. 230 // Should be able to change the SiteInstance of the cloned entry.
256 e2->set_site_instance(instance2); 231 e2->set_site_instance(SiteInstanceImpl::Create(NULL));
257 232
258 // The first SiteInstance should go away after resetting e1, since e2 should 233 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount());
259 // no longer be referencing it. 234 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount());
235
236 // The first SiteInstance and BrowsingInstance should go away after resetting
237 // e1, since e2 should no longer be referencing it.
260 e1.reset(); 238 e1.reset();
261 EXPECT_EQ(1, site_delete_counter1); 239 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount());
262 EXPECT_EQ(0, site_delete_counter2); 240 EXPECT_EQ(1, browser_client()->GetAndClearBrowsingInstanceDeleteCount());
263 241
264 // The second SiteInstance should go away after resetting e2. 242 // The second SiteInstance should go away after resetting e2.
265 e2.reset(); 243 e2.reset();
266 EXPECT_EQ(1, site_delete_counter1); 244 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount());
267 EXPECT_EQ(1, site_delete_counter2); 245 EXPECT_EQ(1, browser_client()->GetAndClearBrowsingInstanceDeleteCount());
268
269 // Both BrowsingInstances are also now deleted.
270 EXPECT_EQ(2, browsing_delete_counter);
271 246
272 DrainMessageLoops(); 247 DrainMessageLoops();
273 } 248 }
274 249
275 // Test to ensure GetProcess returns and creates processes correctly. 250 // Test to ensure GetProcess returns and creates processes correctly.
276 TEST_F(SiteInstanceTest, GetProcess) { 251 TEST_F(SiteInstanceTest, GetProcess) {
277 // Ensure that GetProcess returns a process. 252 // Ensure that GetProcess returns a process.
278 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); 253 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
279 scoped_ptr<RenderProcessHost> host1; 254 scoped_ptr<RenderProcessHost> host1;
280 scoped_refptr<SiteInstanceImpl> instance( 255 scoped_refptr<SiteInstanceImpl> instance(
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 EXPECT_FALSE(SiteInstance::IsSameWebSite(NULL, url_blank, url_foo_port)); 376 EXPECT_FALSE(SiteInstance::IsSameWebSite(NULL, url_blank, url_foo_port));
402 377
403 DrainMessageLoops(); 378 DrainMessageLoops();
404 } 379 }
405 380
406 // Test to ensure that there is only one SiteInstance per site in a given 381 // Test to ensure that there is only one SiteInstance per site in a given
407 // BrowsingInstance, when process-per-site is not in use. 382 // BrowsingInstance, when process-per-site is not in use.
408 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) { 383 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) {
409 ASSERT_FALSE(base::CommandLine::ForCurrentProcess()->HasSwitch( 384 ASSERT_FALSE(base::CommandLine::ForCurrentProcess()->HasSwitch(
410 switches::kProcessPerSite)); 385 switches::kProcessPerSite));
411 int delete_counter = 0;
412 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); 386 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
413 TestBrowsingInstance* browsing_instance = 387 BrowsingInstance* browsing_instance =
414 new TestBrowsingInstance(browser_context.get(), &delete_counter); 388 new BrowsingInstance(browser_context.get());
415 389
416 const GURL url_a1("http://www.google.com/1.html"); 390 const GURL url_a1("http://www.google.com/1.html");
417 scoped_refptr<SiteInstanceImpl> site_instance_a1( 391 scoped_refptr<SiteInstanceImpl> site_instance_a1(
418 browsing_instance->GetSiteInstanceForURL(url_a1)); 392 browsing_instance->GetSiteInstanceForURL(url_a1));
419 EXPECT_TRUE(site_instance_a1.get() != NULL); 393 EXPECT_TRUE(site_instance_a1.get() != NULL);
420 394
421 // A separate site should create a separate SiteInstance. 395 // A separate site should create a separate SiteInstance.
422 const GURL url_b1("http://www.yahoo.com/"); 396 const GURL url_b1("http://www.yahoo.com/");
423 scoped_refptr<SiteInstanceImpl> site_instance_b1( 397 scoped_refptr<SiteInstanceImpl> site_instance_b1(
424 398
425 browsing_instance->GetSiteInstanceForURL(url_b1)); 399 browsing_instance->GetSiteInstanceForURL(url_b1));
426 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); 400 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get());
427 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1.get())); 401 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1.get()));
428 402
429 // Getting the new SiteInstance from the BrowsingInstance and from another 403 // Getting the new SiteInstance from the BrowsingInstance and from another
430 // SiteInstance in the BrowsingInstance should give the same result. 404 // SiteInstance in the BrowsingInstance should give the same result.
431 EXPECT_EQ(site_instance_b1.get(), 405 EXPECT_EQ(site_instance_b1.get(),
432 site_instance_a1->GetRelatedSiteInstance(url_b1)); 406 site_instance_a1->GetRelatedSiteInstance(url_b1));
433 407
434 // A second visit to the original site should return the same SiteInstance. 408 // A second visit to the original site should return the same SiteInstance.
435 const GURL url_a2("http://www.google.com/2.html"); 409 const GURL url_a2("http://www.google.com/2.html");
436 EXPECT_EQ(site_instance_a1.get(), 410 EXPECT_EQ(site_instance_a1.get(),
437 browsing_instance->GetSiteInstanceForURL(url_a2)); 411 browsing_instance->GetSiteInstanceForURL(url_a2));
438 EXPECT_EQ(site_instance_a1.get(), 412 EXPECT_EQ(site_instance_a1.get(),
439 site_instance_a1->GetRelatedSiteInstance(url_a2)); 413 site_instance_a1->GetRelatedSiteInstance(url_a2));
440 414
441 // A visit to the original site in a new BrowsingInstance (same or different 415 // A visit to the original site in a new BrowsingInstance (same or different
442 // browser context) should return a different SiteInstance. 416 // browser context) should return a different SiteInstance.
443 TestBrowsingInstance* browsing_instance2 = 417 BrowsingInstance* browsing_instance2 =
444 new TestBrowsingInstance(browser_context.get(), &delete_counter); 418 new BrowsingInstance(browser_context.get());
445 // Ensure the new SiteInstance is ref counted so that it gets deleted. 419 // Ensure the new SiteInstance is ref counted so that it gets deleted.
446 scoped_refptr<SiteInstanceImpl> site_instance_a2_2( 420 scoped_refptr<SiteInstanceImpl> site_instance_a2_2(
447 browsing_instance2->GetSiteInstanceForURL(url_a2)); 421 browsing_instance2->GetSiteInstanceForURL(url_a2));
448 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get()); 422 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get());
449 EXPECT_FALSE( 423 EXPECT_FALSE(
450 site_instance_a1->IsRelatedSiteInstance(site_instance_a2_2.get())); 424 site_instance_a1->IsRelatedSiteInstance(site_instance_a2_2.get()));
451 425
452 // The two SiteInstances for http://google.com should not use the same process 426 // The two SiteInstances for http://google.com should not use the same process
453 // if process-per-site is not enabled. 427 // if process-per-site is not enabled.
454 scoped_ptr<RenderProcessHost> process_a1(site_instance_a1->GetProcess()); 428 scoped_ptr<RenderProcessHost> process_a1(site_instance_a1->GetProcess());
(...skipping 18 matching lines...) Expand all
473 // The processes will be unregistered when the RPH scoped_ptrs go away. 447 // The processes will be unregistered when the RPH scoped_ptrs go away.
474 448
475 DrainMessageLoops(); 449 DrainMessageLoops();
476 } 450 }
477 451
478 // Test to ensure that there is only one RenderProcessHost per site for an 452 // Test to ensure that there is only one RenderProcessHost per site for an
479 // entire BrowserContext, if process-per-site is in use. 453 // entire BrowserContext, if process-per-site is in use.
480 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) { 454 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) {
481 base::CommandLine::ForCurrentProcess()->AppendSwitch( 455 base::CommandLine::ForCurrentProcess()->AppendSwitch(
482 switches::kProcessPerSite); 456 switches::kProcessPerSite);
483 int delete_counter = 0;
484 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); 457 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
485 TestBrowsingInstance* browsing_instance = 458 scoped_refptr<BrowsingInstance> browsing_instance =
486 new TestBrowsingInstance(browser_context.get(), &delete_counter); 459 new BrowsingInstance(browser_context.get());
487 460
488 const GURL url_a1("http://www.google.com/1.html"); 461 const GURL url_a1("http://www.google.com/1.html");
489 scoped_refptr<SiteInstanceImpl> site_instance_a1( 462 scoped_refptr<SiteInstanceImpl> site_instance_a1(
490 browsing_instance->GetSiteInstanceForURL(url_a1)); 463 browsing_instance->GetSiteInstanceForURL(url_a1));
491 EXPECT_TRUE(site_instance_a1.get() != NULL); 464 EXPECT_TRUE(site_instance_a1.get() != NULL);
492 scoped_ptr<RenderProcessHost> process_a1(site_instance_a1->GetProcess()); 465 scoped_ptr<RenderProcessHost> process_a1(site_instance_a1->GetProcess());
493 466
494 // A separate site should create a separate SiteInstance. 467 // A separate site should create a separate SiteInstance.
495 const GURL url_b1("http://www.yahoo.com/"); 468 const GURL url_b1("http://www.yahoo.com/");
496 scoped_refptr<SiteInstanceImpl> site_instance_b1( 469 scoped_refptr<SiteInstanceImpl> site_instance_b1(
497 browsing_instance->GetSiteInstanceForURL(url_b1)); 470 browsing_instance->GetSiteInstanceForURL(url_b1));
498 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); 471 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get());
499 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1.get())); 472 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1.get()));
500 473
501 // Getting the new SiteInstance from the BrowsingInstance and from another 474 // Getting the new SiteInstance from the BrowsingInstance and from another
502 // SiteInstance in the BrowsingInstance should give the same result. 475 // SiteInstance in the BrowsingInstance should give the same result.
503 EXPECT_EQ(site_instance_b1.get(), 476 EXPECT_EQ(site_instance_b1.get(),
504 site_instance_a1->GetRelatedSiteInstance(url_b1)); 477 site_instance_a1->GetRelatedSiteInstance(url_b1));
505 478
506 // A second visit to the original site should return the same SiteInstance. 479 // A second visit to the original site should return the same SiteInstance.
507 const GURL url_a2("http://www.google.com/2.html"); 480 const GURL url_a2("http://www.google.com/2.html");
508 EXPECT_EQ(site_instance_a1.get(), 481 EXPECT_EQ(site_instance_a1.get(),
509 browsing_instance->GetSiteInstanceForURL(url_a2)); 482 browsing_instance->GetSiteInstanceForURL(url_a2));
510 EXPECT_EQ(site_instance_a1.get(), 483 EXPECT_EQ(site_instance_a1.get(),
511 site_instance_a1->GetRelatedSiteInstance(url_a2)); 484 site_instance_a1->GetRelatedSiteInstance(url_a2));
512 485
513 // A visit to the original site in a new BrowsingInstance (same browser 486 // A visit to the original site in a new BrowsingInstance (same browser
514 // context) should return a different SiteInstance with the same process. 487 // context) should return a different SiteInstance with the same process.
515 TestBrowsingInstance* browsing_instance2 = 488 BrowsingInstance* browsing_instance2 =
516 new TestBrowsingInstance(browser_context.get(), &delete_counter); 489 new BrowsingInstance(browser_context.get());
517 scoped_refptr<SiteInstanceImpl> site_instance_a1_2( 490 scoped_refptr<SiteInstanceImpl> site_instance_a1_2(
518 browsing_instance2->GetSiteInstanceForURL(url_a1)); 491 browsing_instance2->GetSiteInstanceForURL(url_a1));
519 EXPECT_TRUE(site_instance_a1.get() != NULL); 492 EXPECT_TRUE(site_instance_a1.get() != NULL);
520 EXPECT_NE(site_instance_a1.get(), site_instance_a1_2.get()); 493 EXPECT_NE(site_instance_a1.get(), site_instance_a1_2.get());
521 EXPECT_EQ(process_a1.get(), site_instance_a1_2->GetProcess()); 494 EXPECT_EQ(process_a1.get(), site_instance_a1_2->GetProcess());
522 495
523 // A visit to the original site in a new BrowsingInstance (different browser 496 // A visit to the original site in a new BrowsingInstance (different browser
524 // context) should return a different SiteInstance with a different process. 497 // context) should return a different SiteInstance with a different process.
525 scoped_ptr<TestBrowserContext> browser_context2(new TestBrowserContext()); 498 scoped_ptr<TestBrowserContext> browser_context2(new TestBrowserContext());
526 TestBrowsingInstance* browsing_instance3 = 499 BrowsingInstance* browsing_instance3 =
527 new TestBrowsingInstance(browser_context2.get(), &delete_counter); 500 new BrowsingInstance(browser_context2.get());
528 scoped_refptr<SiteInstanceImpl> site_instance_a2_3( 501 scoped_refptr<SiteInstanceImpl> site_instance_a2_3(
529 browsing_instance3->GetSiteInstanceForURL(url_a2)); 502 browsing_instance3->GetSiteInstanceForURL(url_a2));
530 EXPECT_TRUE(site_instance_a2_3.get() != NULL); 503 EXPECT_TRUE(site_instance_a2_3.get() != NULL);
531 scoped_ptr<RenderProcessHost> process_a2_3(site_instance_a2_3->GetProcess()); 504 scoped_ptr<RenderProcessHost> process_a2_3(site_instance_a2_3->GetProcess());
532 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get()); 505 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get());
533 EXPECT_NE(process_a1.get(), process_a2_3.get()); 506 EXPECT_NE(process_a1.get(), process_a2_3.get());
534 507
535 // Should be able to see that we do have SiteInstances. 508 // Should be able to see that we do have SiteInstances.
536 EXPECT_TRUE(browsing_instance->HasSiteInstance( 509 EXPECT_TRUE(browsing_instance->HasSiteInstance(
537 GURL("http://mail.google.com"))); // visited before 510 GURL("http://mail.google.com"))); // visited before
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
768 EXPECT_TRUE(instance->HasSite()); 741 EXPECT_TRUE(instance->HasSite());
769 EXPECT_TRUE(instance->GetSiteURL().is_empty()); 742 EXPECT_TRUE(instance->GetSiteURL().is_empty());
770 host.reset(instance->GetProcess()); 743 host.reset(instance->GetProcess());
771 744
772 EXPECT_FALSE(RenderProcessHostImpl::GetProcessHostForSite( 745 EXPECT_FALSE(RenderProcessHostImpl::GetProcessHostForSite(
773 browser_context.get(), GURL())); 746 browser_context.get(), GURL()));
774 747
775 DrainMessageLoops(); 748 DrainMessageLoops();
776 } 749 }
777 750
751 TEST_F(SiteInstanceTest, DefaultSubframeSiteInstance) {
752 base::CommandLine::ForCurrentProcess()->AppendSwitch(
753 switches::kTopDocumentIsolation);
Charlie Reis 2016/04/01 21:50:41 Does this need to return early for --site-per-proc
ncarter (slow) 2016/04/04 21:47:33 Good catch. Done.
754
755 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
756 scoped_refptr<SiteInstanceImpl> main_instance =
757 SiteInstanceImpl::Create(browser_context.get());
758 scoped_refptr<SiteInstanceImpl> subframe_instance =
759 main_instance->GetDefaultSubframeSiteInstance();
760 int subframe_instance_id = subframe_instance->GetId();
761
762 EXPECT_NE(main_instance, subframe_instance);
763 EXPECT_EQ(subframe_instance, main_instance->GetDefaultSubframeSiteInstance());
764 EXPECT_FALSE(main_instance->is_default_subframe_site_instance());
765 EXPECT_TRUE(subframe_instance->is_default_subframe_site_instance());
766
767 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount());
768 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount());
769
770 // Free the subframe instance.
771 subframe_instance = nullptr;
772 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount());
773 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount());
774
775 // Calling GetDefaultSubframeSiteInstance again should return a new
776 // SiteInstance with a different ID from the original.
777 subframe_instance = main_instance->GetDefaultSubframeSiteInstance();
778 EXPECT_NE(subframe_instance->GetId(), subframe_instance_id);
779 EXPECT_FALSE(main_instance->is_default_subframe_site_instance());
780 EXPECT_TRUE(subframe_instance->is_default_subframe_site_instance());
781 EXPECT_EQ(subframe_instance->GetDefaultSubframeSiteInstance(),
782 subframe_instance);
783 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount());
784 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount());
785
786 // Free the main instance.
787 main_instance = nullptr;
788 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount());
789 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount());
790
791 // Free the subframe instance, which should free the browsing instance.
792 subframe_instance = nullptr;
793 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount());
794 EXPECT_EQ(1, browser_client()->GetAndClearBrowsingInstanceDeleteCount());
795 }
796
778 } // namespace content 797 } // namespace content
OLDNEW
« content/browser/site_instance_impl.h ('K') | « content/browser/site_instance_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698