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

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

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

Powered by Google App Engine
This is Rietveld 408576698