OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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/renderer_host/test_render_view_host.h" | |
6 #include "chrome/browser/tab_contents/navigation_controller.h" | |
7 #include "chrome/browser/tab_contents/navigation_entry.h" | |
8 #include "chrome/browser/renderer_host/render_view_host_manager.h" | |
9 #include "chrome/common/ipc_message.h" | |
10 #include "chrome/common/render_messages.h" | |
11 #include "chrome/common/url_constants.h" | |
12 #include "chrome/test/test_notification_tracker.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 class RenderViewHostManagerTest : public RenderViewHostTestHarness { | |
16 public: | |
17 void NavigateActiveAndCommit(const GURL& url) { | |
18 // Note: we navigate the active RenderViewHost because previous navigations | |
19 // won't have committed yet, so NavigateAndCommit does the wrong thing | |
20 // for us. | |
21 controller().LoadURL(url, GURL(), 0); | |
22 active_rvh()->SendNavigate( | |
23 static_cast<MockRenderProcessHost*>(active_rvh()->process())-> | |
24 max_page_id() + 1, | |
25 url); | |
26 } | |
27 }; | |
28 | |
29 // Tests that when you navigate from the New TabPage to another page, and | |
30 // then do that same thing in another tab, that the two resulting pages have | |
31 // different SiteInstances, BrowsingInstances, and RenderProcessHosts. This is | |
32 // a regression test for bug 9364. | |
33 TEST_F(RenderViewHostManagerTest, NewTabPageProcesses) { | |
34 GURL ntp(chrome::kChromeUINewTabURL); | |
35 GURL dest("http://www.google.com/"); | |
36 | |
37 // Navigate our first tab to the new tab page and then to the destination. | |
38 NavigateActiveAndCommit(ntp); | |
39 NavigateActiveAndCommit(dest); | |
40 | |
41 // Make a second tab. | |
42 TestTabContents contents2(profile_.get(), NULL); | |
43 | |
44 // Load the two URLs in the second tab. Note that the first navigation creates | |
45 // a RVH that's not pending (since there is no cross-site transition), so | |
46 // we use the committed one, but the second one is the opposite. | |
47 contents2.controller().LoadURL(ntp, GURL(), PageTransition::LINK); | |
48 static_cast<TestRenderViewHost*>(contents2.render_manager()-> | |
49 current_host())->SendNavigate(100, ntp); | |
50 contents2.controller().LoadURL(dest, GURL(), PageTransition::LINK); | |
51 static_cast<TestRenderViewHost*>(contents2.render_manager()-> | |
52 pending_render_view_host())->SendNavigate(101, dest); | |
53 | |
54 // The two RVH's should be different in every way. | |
55 EXPECT_NE(active_rvh()->process(), contents2.render_view_host()->process()); | |
56 EXPECT_NE(active_rvh()->site_instance(), | |
57 contents2.render_view_host()->site_instance()); | |
58 EXPECT_NE(active_rvh()->site_instance()->browsing_instance(), | |
59 contents2.render_view_host()->site_instance()->browsing_instance()); | |
60 | |
61 // Navigate both to the new tab page, and verify that they share a | |
62 // SiteInstance. | |
63 NavigateActiveAndCommit(ntp); | |
64 | |
65 contents2.controller().LoadURL(ntp, GURL(), PageTransition::LINK); | |
66 static_cast<TestRenderViewHost*>(contents2.render_manager()-> | |
67 pending_render_view_host())->SendNavigate(102, ntp); | |
68 | |
69 EXPECT_EQ(active_rvh()->site_instance(), | |
70 contents2.render_view_host()->site_instance()); | |
71 } | |
72 | |
73 // When there is an error with the specified page, renderer exits view-source | |
74 // mode. See WebFrameImpl::DidFail(). We check by this test that | |
75 // EnableViewSourceMode message is sent on every navigation regardless | |
76 // RenderView is being newly created or reused. | |
77 TEST_F(RenderViewHostManagerTest, AlwaysSendEnableViewSourceMode) { | |
78 const GURL kNtpUrl(chrome::kChromeUINewTabURL); | |
79 const GURL kUrl("view-source:http://foo"); | |
80 | |
81 // We have to navigate to some page at first since without this, the first | |
82 // navigation will reuse the SiteInstance created by Init(), and the second | |
83 // one will create a new SiteInstance. Because current_instance and | |
84 // new_instance will be different, a new RenderViewHost will be created for | |
85 // the second navigation. We have to avoid this in order to exercise the | |
86 // target code patch. | |
87 NavigateActiveAndCommit(kNtpUrl); | |
88 | |
89 // Navigate. | |
90 controller().LoadURL(kUrl, GURL() /* referer */, PageTransition::TYPED); | |
91 // Simulate response from RenderView for FirePageBeforeUnload. | |
92 rvh()->TestOnMessageReceived( | |
93 ViewHostMsg_ShouldClose_ACK(rvh()->routing_id(), true)); | |
94 ASSERT_TRUE(pending_rvh()); // New pending RenderViewHost will be created. | |
95 RenderViewHost* last_rvh = pending_rvh(); | |
96 int new_id = static_cast<MockRenderProcessHost*>(pending_rvh()->process())-> | |
97 max_page_id() + 1; | |
98 pending_rvh()->SendNavigate(new_id, kUrl); | |
99 EXPECT_EQ(controller().last_committed_entry_index(), 1); | |
100 ASSERT_TRUE(controller().GetLastCommittedEntry()); | |
101 EXPECT_TRUE(kUrl == controller().GetLastCommittedEntry()->url()); | |
102 EXPECT_FALSE(controller().pending_entry()); | |
103 // Because we're using TestTabContents and TestRenderViewHost in this | |
104 // unittest, no one calls TabContents::RenderViewCreated(). So, we see no | |
105 // EnableViewSourceMode message, here. | |
106 | |
107 // Clear queued messages before load. | |
108 process()->sink().ClearMessages(); | |
109 // Navigate, again. | |
110 controller().LoadURL(kUrl, GURL() /* referer */, PageTransition::TYPED); | |
111 // The same RenderViewHost should be reused. | |
112 EXPECT_FALSE(pending_rvh()); | |
113 EXPECT_TRUE(last_rvh == rvh()); | |
114 rvh()->SendNavigate(new_id, kUrl); // The same page_id returned. | |
115 EXPECT_EQ(controller().last_committed_entry_index(), 1); | |
116 EXPECT_FALSE(controller().pending_entry()); | |
117 // New message should be sent out to make sure to enter view-source mode. | |
118 EXPECT_TRUE(process()->sink().GetUniqueMessageMatching( | |
119 ViewMsg_EnableViewSourceMode::ID)); | |
120 } | |
121 | |
122 // Tests the Init function by checking the initial RenderViewHost. | |
123 TEST_F(RenderViewHostManagerTest, Init) { | |
124 // Using TestingProfile. | |
125 SiteInstance* instance = SiteInstance::CreateSiteInstance(profile_.get()); | |
126 EXPECT_FALSE(instance->has_site()); | |
127 | |
128 TestTabContents tab_contents(profile_.get(), instance); | |
129 RenderViewHostManager manager(&tab_contents, &tab_contents); | |
130 | |
131 manager.Init(profile_.get(), instance, MSG_ROUTING_NONE, | |
132 NULL /* modal_dialog_event */); | |
133 | |
134 RenderViewHost* host = manager.current_host(); | |
135 ASSERT_TRUE(host); | |
136 EXPECT_TRUE(instance == host->site_instance()); | |
137 EXPECT_TRUE(&tab_contents == host->delegate()); | |
138 EXPECT_TRUE(manager.current_view()); | |
139 EXPECT_FALSE(manager.pending_render_view_host()); | |
140 } | |
141 | |
142 // Tests the Navigate function. We navigate three sites consequently and check | |
143 // how the pending/committed RenderViewHost are modified. | |
144 TEST_F(RenderViewHostManagerTest, Navigate) { | |
145 TestNotificationTracker notifications; | |
146 | |
147 SiteInstance* instance = SiteInstance::CreateSiteInstance(profile_.get()); | |
148 | |
149 TestTabContents tab_contents(profile_.get(), instance); | |
150 notifications.ListenFor(NotificationType::RENDER_VIEW_HOST_CHANGED, | |
151 Source<NavigationController>(&tab_contents.controller())); | |
152 | |
153 // Create. | |
154 RenderViewHostManager manager(&tab_contents, &tab_contents); | |
155 | |
156 manager.Init(profile_.get(), instance, MSG_ROUTING_NONE, | |
157 NULL /* modal_dialog_event */); | |
158 | |
159 RenderViewHost* host; | |
160 | |
161 // 1) The first navigation. -------------------------- | |
162 GURL url1("http://www.google.com/"); | |
163 NavigationEntry entry1(NULL /* instance */, -1 /* page_id */, url1, | |
164 GURL() /* referrer */, string16() /* title */, | |
165 PageTransition::TYPED); | |
166 host = manager.Navigate(entry1); | |
167 | |
168 // The RenderViewHost created in Init will be reused. | |
169 EXPECT_TRUE(host == manager.current_host()); | |
170 EXPECT_FALSE(manager.pending_render_view_host()); | |
171 | |
172 // Commit. | |
173 manager.DidNavigateMainFrame(host); | |
174 // Commit to SiteInstance should be delayed until RenderView commit. | |
175 EXPECT_TRUE(host == manager.current_host()); | |
176 ASSERT_TRUE(host); | |
177 EXPECT_FALSE(host->site_instance()->has_site()); | |
178 host->site_instance()->SetSite(url1); | |
179 | |
180 // 2) Navigate to next site. ------------------------- | |
181 GURL url2("http://www.google.com/foo"); | |
182 NavigationEntry entry2(NULL /* instance */, -1 /* page_id */, url2, | |
183 url1 /* referrer */, string16() /* title */, | |
184 PageTransition::LINK); | |
185 host = manager.Navigate(entry2); | |
186 | |
187 // The RenderViewHost created in Init will be reused. | |
188 EXPECT_TRUE(host == manager.current_host()); | |
189 EXPECT_FALSE(manager.pending_render_view_host()); | |
190 | |
191 // Commit. | |
192 manager.DidNavigateMainFrame(host); | |
193 EXPECT_TRUE(host == manager.current_host()); | |
194 ASSERT_TRUE(host); | |
195 EXPECT_TRUE(host->site_instance()->has_site()); | |
196 | |
197 // 3) Cross-site navigate to next site. -------------- | |
198 GURL url3("http://webkit.org/"); | |
199 NavigationEntry entry3(NULL /* instance */, -1 /* page_id */, url3, | |
200 url2 /* referrer */, string16() /* title */, | |
201 PageTransition::LINK); | |
202 host = manager.Navigate(entry3); | |
203 | |
204 // A new RenderViewHost should be created. | |
205 EXPECT_TRUE(manager.pending_render_view_host()); | |
206 EXPECT_TRUE(host == manager.pending_render_view_host()); | |
207 | |
208 notifications.Reset(); | |
209 | |
210 // Commit. | |
211 manager.DidNavigateMainFrame(manager.pending_render_view_host()); | |
212 EXPECT_TRUE(host == manager.current_host()); | |
213 ASSERT_TRUE(host); | |
214 EXPECT_TRUE(host->site_instance()->has_site()); | |
215 // Check the pending RenderViewHost has been committed. | |
216 EXPECT_FALSE(manager.pending_render_view_host()); | |
217 | |
218 // We should observe a notification. | |
219 EXPECT_TRUE(notifications.Check1AndReset( | |
220 NotificationType::RENDER_VIEW_HOST_CHANGED)); | |
221 } | |
222 | |
223 // Tests DOMUI creation. | |
224 TEST_F(RenderViewHostManagerTest, DOMUI) { | |
225 SiteInstance* instance = SiteInstance::CreateSiteInstance(profile_.get()); | |
226 | |
227 TestTabContents tab_contents(profile_.get(), instance); | |
228 RenderViewHostManager manager(&tab_contents, &tab_contents); | |
229 | |
230 manager.Init(profile_.get(), instance, MSG_ROUTING_NONE, | |
231 NULL /* modal_dialog_event */); | |
232 | |
233 GURL url("chrome://newtab"); | |
234 NavigationEntry entry(NULL /* instance */, -1 /* page_id */, url, | |
235 GURL() /* referrer */, string16() /* title */, | |
236 PageTransition::TYPED); | |
237 RenderViewHost* host = manager.Navigate(entry); | |
238 | |
239 EXPECT_TRUE(host); | |
240 EXPECT_TRUE(host == manager.current_host()); | |
241 EXPECT_FALSE(manager.pending_render_view_host()); | |
242 EXPECT_TRUE(manager.pending_dom_ui()); | |
243 EXPECT_FALSE(manager.dom_ui()); | |
244 | |
245 // Commit. | |
246 manager.DidNavigateMainFrame(host); | |
247 | |
248 EXPECT_FALSE(manager.pending_dom_ui()); | |
249 EXPECT_TRUE(manager.dom_ui()); | |
250 } | |
OLD | NEW |