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

Side by Side Diff: chrome/browser/tab_contents/render_view_host_manager_unittest.cc

Issue 6537015: Start moving core pieces of Chrome multi-process code to src\content. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 10 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/browser_thread.h"
6 #include "chrome/browser/browser_url_handler.h"
7 #include "chrome/browser/renderer_host/site_instance.h"
8 #include "chrome/browser/renderer_host/test/test_render_view_host.h"
9 #include "chrome/browser/tab_contents/navigation_controller.h"
10 #include "chrome/browser/tab_contents/navigation_entry.h"
11 #include "chrome/browser/tab_contents/render_view_host_manager.h"
12 #include "chrome/browser/tab_contents/test_tab_contents.h"
13 #include "chrome/common/notification_details.h"
14 #include "chrome/common/notification_source.h"
15 #include "chrome/common/page_transition_types.h"
16 #include "chrome/common/render_messages.h"
17 #include "chrome/common/render_messages_params.h"
18 #include "chrome/common/url_constants.h"
19 #include "chrome/test/test_notification_tracker.h"
20 #include "chrome/test/testing_profile.h"
21 #include "ipc/ipc_message.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 class RenderViewHostManagerTest : public RenderViewHostTestHarness {
25 public:
26 void NavigateActiveAndCommit(const GURL& url) {
27 // Note: we navigate the active RenderViewHost because previous navigations
28 // won't have committed yet, so NavigateAndCommit does the wrong thing
29 // for us.
30 controller().LoadURL(url, GURL(), PageTransition::LINK);
31 active_rvh()->SendNavigate(
32 static_cast<MockRenderProcessHost*>(active_rvh()->process())->
33 max_page_id() + 1,
34 url);
35 }
36
37 bool ShouldSwapProcesses(RenderViewHostManager* manager,
38 const NavigationEntry* cur_entry,
39 const NavigationEntry* new_entry) const {
40 return manager->ShouldSwapProcessesForNavigation(cur_entry, new_entry);
41 }
42 };
43
44 // Tests that when you navigate from the New TabPage to another page, and
45 // then do that same thing in another tab, that the two resulting pages have
46 // different SiteInstances, BrowsingInstances, and RenderProcessHosts. This is
47 // a regression test for bug 9364.
48 TEST_F(RenderViewHostManagerTest, NewTabPageProcesses) {
49 BrowserThread ui_thread(BrowserThread::UI, MessageLoop::current());
50 GURL ntp(chrome::kChromeUINewTabURL);
51 GURL dest("http://www.google.com/");
52
53 // Navigate our first tab to the new tab page and then to the destination.
54 NavigateActiveAndCommit(ntp);
55 NavigateActiveAndCommit(dest);
56
57 // Make a second tab.
58 TestTabContents contents2(profile_.get(), NULL);
59
60 // Load the two URLs in the second tab. Note that the first navigation creates
61 // a RVH that's not pending (since there is no cross-site transition), so
62 // we use the committed one, but the second one is the opposite.
63 contents2.controller().LoadURL(ntp, GURL(), PageTransition::LINK);
64 static_cast<TestRenderViewHost*>(contents2.render_manager()->
65 current_host())->SendNavigate(100, ntp);
66 contents2.controller().LoadURL(dest, GURL(), PageTransition::LINK);
67 static_cast<TestRenderViewHost*>(contents2.render_manager()->
68 pending_render_view_host())->SendNavigate(101, dest);
69
70 // The two RVH's should be different in every way.
71 EXPECT_NE(active_rvh()->process(), contents2.render_view_host()->process());
72 EXPECT_NE(active_rvh()->site_instance(),
73 contents2.render_view_host()->site_instance());
74 EXPECT_NE(active_rvh()->site_instance()->browsing_instance(),
75 contents2.render_view_host()->site_instance()->browsing_instance());
76
77 // Navigate both to the new tab page, and verify that they share a
78 // SiteInstance.
79 NavigateActiveAndCommit(ntp);
80
81 contents2.controller().LoadURL(ntp, GURL(), PageTransition::LINK);
82 static_cast<TestRenderViewHost*>(contents2.render_manager()->
83 pending_render_view_host())->SendNavigate(102, ntp);
84
85 EXPECT_EQ(active_rvh()->site_instance(),
86 contents2.render_view_host()->site_instance());
87 }
88
89 // When there is an error with the specified page, renderer exits view-source
90 // mode. See WebFrameImpl::DidFail(). We check by this test that
91 // EnableViewSourceMode message is sent on every navigation regardless
92 // RenderView is being newly created or reused.
93 TEST_F(RenderViewHostManagerTest, AlwaysSendEnableViewSourceMode) {
94 BrowserThread ui_thread(BrowserThread::UI, MessageLoop::current());
95 const GURL kNtpUrl(chrome::kChromeUINewTabURL);
96 const GURL kUrl("view-source:http://foo");
97
98 // We have to navigate to some page at first since without this, the first
99 // navigation will reuse the SiteInstance created by Init(), and the second
100 // one will create a new SiteInstance. Because current_instance and
101 // new_instance will be different, a new RenderViewHost will be created for
102 // the second navigation. We have to avoid this in order to exercise the
103 // target code patch.
104 NavigateActiveAndCommit(kNtpUrl);
105
106 // Navigate.
107 controller().LoadURL(kUrl, GURL() /* referer */, PageTransition::TYPED);
108 // Simulate response from RenderView for FirePageBeforeUnload.
109 rvh()->TestOnMessageReceived(
110 ViewHostMsg_ShouldClose_ACK(rvh()->routing_id(), true));
111 ASSERT_TRUE(pending_rvh()); // New pending RenderViewHost will be created.
112 RenderViewHost* last_rvh = pending_rvh();
113 int new_id = static_cast<MockRenderProcessHost*>(pending_rvh()->process())->
114 max_page_id() + 1;
115 pending_rvh()->SendNavigate(new_id, kUrl);
116 EXPECT_EQ(controller().last_committed_entry_index(), 1);
117 ASSERT_TRUE(controller().GetLastCommittedEntry());
118 EXPECT_TRUE(kUrl == controller().GetLastCommittedEntry()->url());
119 EXPECT_FALSE(controller().pending_entry());
120 // Because we're using TestTabContents and TestRenderViewHost in this
121 // unittest, no one calls TabContents::RenderViewCreated(). So, we see no
122 // EnableViewSourceMode message, here.
123
124 // Clear queued messages before load.
125 process()->sink().ClearMessages();
126 // Navigate, again.
127 controller().LoadURL(kUrl, GURL() /* referer */, PageTransition::TYPED);
128 // The same RenderViewHost should be reused.
129 EXPECT_FALSE(pending_rvh());
130 EXPECT_TRUE(last_rvh == rvh());
131 rvh()->SendNavigate(new_id, kUrl); // The same page_id returned.
132 EXPECT_EQ(controller().last_committed_entry_index(), 1);
133 EXPECT_FALSE(controller().pending_entry());
134 // New message should be sent out to make sure to enter view-source mode.
135 EXPECT_TRUE(process()->sink().GetUniqueMessageMatching(
136 ViewMsg_EnableViewSourceMode::ID));
137 }
138
139 // Tests the Init function by checking the initial RenderViewHost.
140 TEST_F(RenderViewHostManagerTest, Init) {
141 // Using TestingProfile.
142 SiteInstance* instance = SiteInstance::CreateSiteInstance(profile_.get());
143 EXPECT_FALSE(instance->has_site());
144
145 TestTabContents tab_contents(profile_.get(), instance);
146 RenderViewHostManager manager(&tab_contents, &tab_contents);
147
148 manager.Init(profile_.get(), instance, MSG_ROUTING_NONE);
149
150 RenderViewHost* host = manager.current_host();
151 ASSERT_TRUE(host);
152 EXPECT_TRUE(instance == host->site_instance());
153 EXPECT_TRUE(&tab_contents == host->delegate());
154 EXPECT_TRUE(manager.GetRenderWidgetHostView());
155 EXPECT_FALSE(manager.pending_render_view_host());
156 }
157
158 // Tests the Navigate function. We navigate three sites consecutively and check
159 // how the pending/committed RenderViewHost are modified.
160 TEST_F(RenderViewHostManagerTest, Navigate) {
161 TestNotificationTracker notifications;
162
163 SiteInstance* instance = SiteInstance::CreateSiteInstance(profile_.get());
164
165 TestTabContents tab_contents(profile_.get(), instance);
166 notifications.ListenFor(NotificationType::RENDER_VIEW_HOST_CHANGED,
167 Source<NavigationController>(&tab_contents.controller()));
168
169 // Create.
170 RenderViewHostManager manager(&tab_contents, &tab_contents);
171
172 manager.Init(profile_.get(), instance, MSG_ROUTING_NONE);
173
174 RenderViewHost* host;
175
176 // 1) The first navigation. --------------------------
177 GURL url1("http://www.google.com/");
178 NavigationEntry entry1(NULL /* instance */, -1 /* page_id */, url1,
179 GURL() /* referrer */, string16() /* title */,
180 PageTransition::TYPED);
181 host = manager.Navigate(entry1);
182
183 // The RenderViewHost created in Init will be reused.
184 EXPECT_TRUE(host == manager.current_host());
185 EXPECT_FALSE(manager.pending_render_view_host());
186
187 // Commit.
188 manager.DidNavigateMainFrame(host);
189 // Commit to SiteInstance should be delayed until RenderView commit.
190 EXPECT_TRUE(host == manager.current_host());
191 ASSERT_TRUE(host);
192 EXPECT_FALSE(host->site_instance()->has_site());
193 host->site_instance()->SetSite(url1);
194
195 // 2) Navigate to next site. -------------------------
196 GURL url2("http://www.google.com/foo");
197 NavigationEntry entry2(NULL /* instance */, -1 /* page_id */, url2,
198 url1 /* referrer */, string16() /* title */,
199 PageTransition::LINK);
200 host = manager.Navigate(entry2);
201
202 // The RenderViewHost created in Init will be reused.
203 EXPECT_TRUE(host == manager.current_host());
204 EXPECT_FALSE(manager.pending_render_view_host());
205
206 // Commit.
207 manager.DidNavigateMainFrame(host);
208 EXPECT_TRUE(host == manager.current_host());
209 ASSERT_TRUE(host);
210 EXPECT_TRUE(host->site_instance()->has_site());
211
212 // 3) Cross-site navigate to next site. --------------
213 GURL url3("http://webkit.org/");
214 NavigationEntry entry3(NULL /* instance */, -1 /* page_id */, url3,
215 url2 /* referrer */, string16() /* title */,
216 PageTransition::LINK);
217 host = manager.Navigate(entry3);
218
219 // A new RenderViewHost should be created.
220 EXPECT_TRUE(manager.pending_render_view_host());
221 EXPECT_TRUE(host == manager.pending_render_view_host());
222
223 notifications.Reset();
224
225 // Commit.
226 manager.DidNavigateMainFrame(manager.pending_render_view_host());
227 EXPECT_TRUE(host == manager.current_host());
228 ASSERT_TRUE(host);
229 EXPECT_TRUE(host->site_instance()->has_site());
230 // Check the pending RenderViewHost has been committed.
231 EXPECT_FALSE(manager.pending_render_view_host());
232
233 // We should observe a notification.
234 EXPECT_TRUE(notifications.Check1AndReset(
235 NotificationType::RENDER_VIEW_HOST_CHANGED));
236 }
237
238 // Tests WebUI creation.
239 TEST_F(RenderViewHostManagerTest, WebUI) {
240 BrowserThread ui_thread(BrowserThread::UI, MessageLoop::current());
241 SiteInstance* instance = SiteInstance::CreateSiteInstance(profile_.get());
242
243 TestTabContents tab_contents(profile_.get(), instance);
244 RenderViewHostManager manager(&tab_contents, &tab_contents);
245
246 manager.Init(profile_.get(), instance, MSG_ROUTING_NONE);
247
248 GURL url(chrome::kChromeUINewTabURL);
249 NavigationEntry entry(NULL /* instance */, -1 /* page_id */, url,
250 GURL() /* referrer */, string16() /* title */,
251 PageTransition::TYPED);
252 RenderViewHost* host = manager.Navigate(entry);
253
254 EXPECT_TRUE(host);
255 EXPECT_TRUE(host == manager.current_host());
256 EXPECT_FALSE(manager.pending_render_view_host());
257
258 // It's important that the site instance get set on the Web UI page as soon
259 // as the navigation starts, rather than lazily after it commits, so we don't
260 // try to re-use the SiteInstance/process for non DOM-UI things that may
261 // get loaded in between.
262 EXPECT_TRUE(host->site_instance()->has_site());
263 EXPECT_EQ(url, host->site_instance()->site());
264
265 // The Web UI is committed immediately because the RenderViewHost has not been
266 // used yet. UpdateRendererStateForNavigate() took the short cut path.
267 EXPECT_FALSE(manager.pending_web_ui());
268 EXPECT_TRUE(manager.web_ui());
269
270 // Commit.
271 manager.DidNavigateMainFrame(host);
272 }
273
274 // Tests that chrome: URLs that are not Web UI pages do not get grouped into
275 // Web UI renderers, even if --process-per-tab is enabled. In that mode, we
276 // still swap processes if ShouldSwapProcessesForNavigation is true.
277 // Regression test for bug 46290.
278 TEST_F(RenderViewHostManagerTest, NonWebUIChromeURLs) {
279 BrowserThread thread(BrowserThread::UI, &message_loop_);
280 SiteInstance* instance = SiteInstance::CreateSiteInstance(profile_.get());
281 TestTabContents tab_contents(profile_.get(), instance);
282 RenderViewHostManager manager(&tab_contents, &tab_contents);
283 manager.Init(profile_.get(), instance, MSG_ROUTING_NONE);
284
285 // NTP is a Web UI page.
286 GURL ntp_url(chrome::kChromeUINewTabURL);
287 NavigationEntry ntp_entry(NULL /* instance */, -1 /* page_id */, ntp_url,
288 GURL() /* referrer */, string16() /* title */,
289 PageTransition::TYPED);
290
291 // about: URLs are not Web UI pages.
292 GURL about_url(chrome::kAboutMemoryURL);
293 // Rewrite so it looks like chrome://about/memory
294 bool reverse_on_redirect = false;
295 BrowserURLHandler::RewriteURLIfNecessary(
296 &about_url, profile_.get(), &reverse_on_redirect);
297 NavigationEntry about_entry(NULL /* instance */, -1 /* page_id */, about_url,
298 GURL() /* referrer */, string16() /* title */,
299 PageTransition::TYPED);
300
301 EXPECT_TRUE(ShouldSwapProcesses(&manager, &ntp_entry, &about_entry));
302 }
303
304 // Tests that we don't end up in an inconsistent state if a page does a back and
305 // then reload. http://crbug.com/51680
306 TEST_F(RenderViewHostManagerTest, PageDoesBackAndReload) {
307 GURL url1("http://www.google.com/");
308 GURL url2("http://www.evil-site.com/");
309
310 // Navigate to a safe site, then an evil site.
311 // This will switch RenderViewHosts. We cannot assert that the first and
312 // second RVHs are different, though, because the first one may be promptly
313 // deleted.
314 contents()->NavigateAndCommit(url1);
315 contents()->NavigateAndCommit(url2);
316 RenderViewHost* evil_rvh = contents()->render_view_host();
317
318 // Now let's simulate the evil page calling history.back().
319 contents()->OnGoToEntryAtOffset(-1);
320 // We should have a new pending RVH.
321 // Note that in this case, the navigation has not committed, so evil_rvh will
322 // not be deleted yet.
323 EXPECT_NE(evil_rvh, contents()->render_manager()->pending_render_view_host());
324
325 // Before that RVH has committed, the evil page reloads itself.
326 ViewHostMsg_FrameNavigate_Params params;
327 params.page_id = 1;
328 params.url = url2;
329 params.transition = PageTransition::CLIENT_REDIRECT;
330 params.should_update_history = false;
331 params.gesture = NavigationGestureAuto;
332 params.was_within_same_page = false;
333 params.is_post = false;
334 contents()->TestDidNavigate(evil_rvh, params);
335
336 // That should have cancelled the pending RVH, and the evil RVH should be the
337 // current one.
338 EXPECT_TRUE(contents()->render_manager()->pending_render_view_host() == NULL);
339 EXPECT_EQ(evil_rvh, contents()->render_manager()->current_host());
340
341 // Also we should not have a pending navigation entry.
342 NavigationEntry* entry = contents()->controller().GetActiveEntry();
343 ASSERT_TRUE(entry != NULL);
344 EXPECT_EQ(url2, entry->url());
345 }
OLDNEW
« no previous file with comments | « chrome/browser/tab_contents/render_view_host_manager.cc ('k') | chrome/browser/tab_contents/tab_contents.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698