OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/favicon/favicon_tab_helper.h" | |
6 #include "chrome/common/url_constants.h" | |
7 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
8 #include "chrome/test/base/testing_profile.h" | |
9 #include "content/public/browser/navigation_controller.h" | |
10 #include "content/public/browser/site_instance.h" | |
11 #include "content/public/browser/web_contents.h" | |
12 #include "content/public/browser/web_contents_delegate.h" | |
13 #include "content/public/common/referrer.h" | |
14 #include "content/public/test/test_browser_thread.h" | |
15 #include "content/public/test/test_renderer_host.h" | |
16 #include "content/public/test/web_contents_tester.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 using content::BrowserThread; | |
20 using content::NavigationController; | |
21 using content::RenderViewHost; | |
22 using content::RenderViewHostTester; | |
23 using content::SiteInstance; | |
24 using content::WebContents; | |
25 using content::WebContentsTester; | |
26 | |
27 class WebUITest : public ChromeRenderViewHostTestHarness { | |
28 public: | |
29 WebUITest() : ui_thread_(BrowserThread::UI, MessageLoop::current()) {} | |
30 | |
31 // Tests navigating with a Web UI from a fresh (nothing pending or committed) | |
32 // state, through pending, committed, then another navigation. The first page | |
33 // ID that we should use is passed as a parameter. We'll use the next two | |
34 // values. This must be increasing for the life of the tests. | |
35 static void DoNavigationTest(WebContents* web_contents, int page_id) { | |
36 NavigationController* controller = &web_contents->GetController(); | |
37 FaviconTabHelper* favicon_tab_helper = | |
38 FaviconTabHelper::FromWebContents(web_contents); | |
39 | |
40 // Start a pending load. | |
41 GURL new_tab_url(chrome::kChromeUINewTabURL); | |
42 controller->LoadURL(new_tab_url, content::Referrer(), | |
43 content::PAGE_TRANSITION_LINK, | |
44 std::string()); | |
45 | |
46 // The navigation entry should be pending with no committed entry. | |
47 ASSERT_TRUE(controller->GetPendingEntry()); | |
48 ASSERT_FALSE(controller->GetLastCommittedEntry()); | |
49 | |
50 // Check the things the pending Web UI should have set. | |
51 EXPECT_FALSE(favicon_tab_helper->ShouldDisplayFavicon()); | |
52 EXPECT_TRUE(web_contents->FocusLocationBarByDefault()); | |
53 | |
54 // Now commit the load. | |
55 RenderViewHostTester::For( | |
56 web_contents->GetRenderViewHost())->SendNavigate(page_id, new_tab_url); | |
57 | |
58 // The same flags should be set as before now that the load has committed. | |
59 EXPECT_FALSE(favicon_tab_helper->ShouldDisplayFavicon()); | |
60 EXPECT_TRUE(web_contents->FocusLocationBarByDefault()); | |
61 | |
62 // Start a pending navigation to a regular page. | |
63 GURL next_url("http://google.com/"); | |
64 controller->LoadURL(next_url, content::Referrer(), | |
65 content::PAGE_TRANSITION_LINK, | |
66 std::string()); | |
67 | |
68 // Check the flags. Some should reflect the new page (URL, title), some | |
69 // should reflect the old one (bookmark bar) until it has committed. | |
70 EXPECT_TRUE(favicon_tab_helper->ShouldDisplayFavicon()); | |
71 EXPECT_FALSE(web_contents->FocusLocationBarByDefault()); | |
72 | |
73 // Commit the regular page load. Note that we must send it to the "pending" | |
74 // RenderViewHost if there is one, since this transition will also cause a | |
75 // process transition, and our RVH pointer will be the "committed" one. | |
76 // In the second call to this function from WebUIToStandard, it won't | |
77 // actually be pending, which is the point of this test. | |
78 RenderViewHost* pending_rvh = | |
79 RenderViewHostTester::GetPendingForController(controller); | |
80 if (pending_rvh) { | |
81 RenderViewHostTester::For(pending_rvh)-> | |
82 SendNavigate(page_id + 1, next_url); | |
83 } else { | |
84 RenderViewHostTester::For(web_contents->GetRenderViewHost())-> | |
85 SendNavigate(page_id + 1, next_url); | |
86 } | |
87 | |
88 // The state should now reflect a regular page. | |
89 EXPECT_TRUE(favicon_tab_helper->ShouldDisplayFavicon()); | |
90 EXPECT_FALSE(web_contents->FocusLocationBarByDefault()); | |
91 } | |
92 | |
93 private: | |
94 virtual void SetUp() OVERRIDE { | |
95 ChromeRenderViewHostTestHarness::SetUp(); | |
96 FaviconTabHelper::CreateForWebContents(web_contents()); | |
97 } | |
98 | |
99 content::TestBrowserThread ui_thread_; | |
100 | |
101 DISALLOW_COPY_AND_ASSIGN(WebUITest); | |
102 }; | |
103 | |
104 // Tests that the New Tab Page flags are correctly set and propogated by | |
105 // WebContents when we first navigate to a Web UI page, then to a standard | |
106 // non-DOM-UI page. | |
107 TEST_F(WebUITest, WebUIToStandard) { | |
108 DoNavigationTest(web_contents(), 1); | |
109 | |
110 // Test the case where we're not doing the initial navigation. This is | |
111 // slightly different than the very-first-navigation case since the | |
112 // SiteInstance will be the same (the original WebContents must still be | |
113 // alive), which will trigger different behavior in RenderViewHostManager. | |
114 scoped_ptr<WebContents> contents2( | |
115 WebContentsTester::CreateTestWebContents(profile(), NULL)); | |
116 FaviconTabHelper::CreateForWebContents(contents2.get()); | |
117 | |
118 DoNavigationTest(contents2.get(), 101); | |
119 } | |
120 | |
121 TEST_F(WebUITest, WebUIToWebUI) { | |
122 // Do a load (this state is tested above). | |
123 GURL new_tab_url(chrome::kChromeUINewTabURL); | |
124 controller().LoadURL(new_tab_url, content::Referrer(), | |
125 content::PAGE_TRANSITION_LINK, | |
126 std::string()); | |
127 rvh_tester()->SendNavigate(1, new_tab_url); | |
128 | |
129 // Start another pending load of the new tab page. | |
130 controller().LoadURL(new_tab_url, content::Referrer(), | |
131 content::PAGE_TRANSITION_LINK, | |
132 std::string()); | |
133 rvh_tester()->SendNavigate(2, new_tab_url); | |
134 | |
135 // The flags should be the same as the non-pending state. | |
136 FaviconTabHelper* favicon_tab_helper = | |
137 FaviconTabHelper::FromWebContents(web_contents()); | |
138 EXPECT_FALSE(favicon_tab_helper->ShouldDisplayFavicon()); | |
139 EXPECT_TRUE(web_contents()->FocusLocationBarByDefault()); | |
140 } | |
141 | |
142 TEST_F(WebUITest, StandardToWebUI) { | |
143 // Start a pending navigation to a regular page. | |
144 GURL std_url("http://google.com/"); | |
145 | |
146 controller().LoadURL(std_url, content::Referrer(), | |
147 content::PAGE_TRANSITION_LINK, | |
148 std::string()); | |
149 | |
150 FaviconTabHelper* favicon_tab_helper = | |
151 FaviconTabHelper::FromWebContents(web_contents()); | |
152 // The state should now reflect the default. | |
153 EXPECT_TRUE(favicon_tab_helper->ShouldDisplayFavicon()); | |
154 EXPECT_FALSE(web_contents()->FocusLocationBarByDefault()); | |
155 | |
156 // Commit the load, the state should be the same. | |
157 rvh_tester()->SendNavigate(1, std_url); | |
158 EXPECT_TRUE(favicon_tab_helper->ShouldDisplayFavicon()); | |
159 EXPECT_FALSE(web_contents()->FocusLocationBarByDefault()); | |
160 | |
161 // Start a pending load for a WebUI. | |
162 GURL new_tab_url(chrome::kChromeUINewTabURL); | |
163 controller().LoadURL(new_tab_url, content::Referrer(), | |
164 content::PAGE_TRANSITION_LINK, | |
165 std::string()); | |
166 EXPECT_TRUE(favicon_tab_helper->ShouldDisplayFavicon()); | |
167 EXPECT_TRUE(web_contents()->FocusLocationBarByDefault()); | |
168 | |
169 // Committing Web UI is tested above. | |
170 } | |
171 | |
172 namespace { | |
173 | |
174 class TestDelegate : public content::WebContentsDelegate { | |
175 public: | |
176 TestDelegate() : focus_count_(0) {} | |
177 | |
178 virtual void SetFocusToLocationBar(bool select_all) OVERRIDE { | |
179 focus_count_++; | |
180 } | |
181 | |
182 int focus_count() const { return focus_count_; } | |
183 | |
184 private: | |
185 int focus_count_; | |
186 }; | |
187 | |
188 } | |
189 | |
190 TEST_F(WebUITest, FocusOnNavigate) { | |
191 // Setup. |wc| will be used to track when we try to focus the location bar. | |
192 WebContents* wc = WebContentsTester::CreateTestWebContents( | |
193 web_contents()->GetBrowserContext(), | |
194 SiteInstance::Create(web_contents()->GetBrowserContext())); | |
195 TestDelegate delegate; | |
196 wc->SetDelegate(&delegate); | |
197 wc->GetController().CopyStateFrom(controller()); | |
198 SetContents(wc); | |
199 int page_id = 200; | |
200 | |
201 // Load the NTP. | |
202 GURL new_tab_url(chrome::kChromeUINewTabURL); | |
203 controller().LoadURL(new_tab_url, content::Referrer(), | |
204 content::PAGE_TRANSITION_LINK, | |
205 std::string()); | |
206 rvh_tester()->SendNavigate(page_id, new_tab_url); | |
207 | |
208 // Navigate to another page. | |
209 GURL next_url("http://google.com/"); | |
210 int next_page_id = page_id + 1; | |
211 controller().LoadURL(next_url, content::Referrer(), | |
212 content::PAGE_TRANSITION_LINK, | |
213 std::string()); | |
214 RenderViewHost* old_rvh = rvh(); | |
215 RenderViewHostTester::For(old_rvh)->SendShouldCloseACK(true); | |
216 RenderViewHostTester::For( | |
217 pending_rvh())->SendNavigate(next_page_id, next_url); | |
218 RenderViewHostTester::For(old_rvh)->SimulateSwapOutACK(); | |
219 | |
220 // Navigate back. Should focus the location bar. | |
221 int focus_called = delegate.focus_count(); | |
222 ASSERT_TRUE(controller().CanGoBack()); | |
223 controller().GoBack(); | |
224 old_rvh = rvh(); | |
225 RenderViewHostTester::For(old_rvh)->SendShouldCloseACK(true); | |
226 RenderViewHostTester::For(pending_rvh())->SendNavigate(page_id, new_tab_url); | |
227 RenderViewHostTester::For(old_rvh)->SimulateSwapOutACK(); | |
228 EXPECT_LT(focus_called, delegate.focus_count()); | |
229 | |
230 // Navigate forward. Shouldn't focus the location bar. | |
231 focus_called = delegate.focus_count(); | |
232 ASSERT_TRUE(controller().CanGoForward()); | |
233 controller().GoForward(); | |
234 old_rvh = rvh(); | |
235 RenderViewHostTester::For(old_rvh)->SendShouldCloseACK(true); | |
236 RenderViewHostTester::For( | |
237 pending_rvh())->SendNavigate(next_page_id, next_url); | |
238 RenderViewHostTester::For(old_rvh)->SimulateSwapOutACK(); | |
239 EXPECT_EQ(focus_called, delegate.focus_count()); | |
240 } | |
OLD | NEW |