OLD | NEW |
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 "chrome/browser/favicon/favicon_tab_helper.h" | 5 #include "chrome/browser/favicon/favicon_tab_helper.h" |
6 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h" | 6 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h" |
7 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 7 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
8 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h" | 8 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h" |
9 #include "chrome/common/url_constants.h" | 9 #include "chrome/common/url_constants.h" |
10 #include "chrome/test/base/testing_profile.h" | 10 #include "chrome/test/base/testing_profile.h" |
11 #include "content/browser/tab_contents/test_tab_contents.h" | |
12 #include "content/public/browser/navigation_controller.h" | 11 #include "content/public/browser/navigation_controller.h" |
13 #include "content/public/browser/site_instance.h" | 12 #include "content/public/browser/site_instance.h" |
| 13 #include "content/public/browser/web_contents.h" |
| 14 #include "content/public/common/referrer.h" |
14 #include "content/test/test_browser_thread.h" | 15 #include "content/test/test_browser_thread.h" |
15 #include "content/test/test_renderer_host.h" | 16 #include "content/test/test_renderer_host.h" |
| 17 #include "content/test/web_contents_tester.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
17 | 19 |
18 using content::BrowserThread; | 20 using content::BrowserThread; |
19 using content::NavigationController; | 21 using content::NavigationController; |
20 using content::RenderViewHost; | 22 using content::RenderViewHost; |
21 using content::RenderViewHostTester; | 23 using content::RenderViewHostTester; |
22 using content::SiteInstance; | 24 using content::SiteInstance; |
23 using content::WebContents; | 25 using content::WebContents; |
| 26 using content::WebContentsTester; |
24 | 27 |
25 class WebUITest : public TabContentsWrapperTestHarness { | 28 class WebUITest : public TabContentsWrapperTestHarness { |
26 public: | 29 public: |
27 WebUITest() : ui_thread_(BrowserThread::UI, MessageLoop::current()) {} | 30 WebUITest() : ui_thread_(BrowserThread::UI, MessageLoop::current()) {} |
28 | 31 |
29 // Tests navigating with a Web UI from a fresh (nothing pending or committed) | 32 // Tests navigating with a Web UI from a fresh (nothing pending or committed) |
30 // state, through pending, committed, then another navigation. The first page | 33 // state, through pending, committed, then another navigation. The first page |
31 // ID that we should use is passed as a parameter. We'll use the next two | 34 // ID that we should use is passed as a parameter. We'll use the next two |
32 // values. This must be increasing for the life of the tests. | 35 // values. This must be increasing for the life of the tests. |
33 static void DoNavigationTest(TabContentsWrapper* wrapper, int page_id) { | 36 static void DoNavigationTest(TabContentsWrapper* wrapper, int page_id) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 // Tests that the New Tab Page flags are correctly set and propogated by | 99 // Tests that the New Tab Page flags are correctly set and propogated by |
97 // TabContents when we first navigate to a Web UI page, then to a standard | 100 // TabContents when we first navigate to a Web UI page, then to a standard |
98 // non-DOM-UI page. | 101 // non-DOM-UI page. |
99 TEST_F(WebUITest, WebUIToStandard) { | 102 TEST_F(WebUITest, WebUIToStandard) { |
100 DoNavigationTest(contents_wrapper(), 1); | 103 DoNavigationTest(contents_wrapper(), 1); |
101 | 104 |
102 // Test the case where we're not doing the initial navigation. This is | 105 // Test the case where we're not doing the initial navigation. This is |
103 // slightly different than the very-first-navigation case since the | 106 // slightly different than the very-first-navigation case since the |
104 // SiteInstance will be the same (the original TabContents must still be | 107 // SiteInstance will be the same (the original TabContents must still be |
105 // alive), which will trigger different behavior in RenderViewHostManager. | 108 // alive), which will trigger different behavior in RenderViewHostManager. |
106 TestTabContents* contents2 = new TestTabContents(profile(), NULL); | 109 WebContents* contents2 = |
| 110 WebContentsTester::CreateTestWebContents(profile(), NULL); |
107 TabContentsWrapper wrapper2(contents2); | 111 TabContentsWrapper wrapper2(contents2); |
108 | 112 |
109 DoNavigationTest(&wrapper2, 101); | 113 DoNavigationTest(&wrapper2, 101); |
110 } | 114 } |
111 | 115 |
112 TEST_F(WebUITest, WebUIToWebUI) { | 116 TEST_F(WebUITest, WebUIToWebUI) { |
113 // Do a load (this state is tested above). | 117 // Do a load (this state is tested above). |
114 GURL new_tab_url(chrome::kChromeUINewTabURL); | 118 GURL new_tab_url(chrome::kChromeUINewTabURL); |
115 controller().LoadURL(new_tab_url, content::Referrer(), | 119 controller().LoadURL(new_tab_url, content::Referrer(), |
116 content::PAGE_TRANSITION_LINK, | 120 content::PAGE_TRANSITION_LINK, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 GURL new_tab_url(chrome::kChromeUINewTabURL); | 154 GURL new_tab_url(chrome::kChromeUINewTabURL); |
151 controller().LoadURL(new_tab_url, content::Referrer(), | 155 controller().LoadURL(new_tab_url, content::Referrer(), |
152 content::PAGE_TRANSITION_LINK, | 156 content::PAGE_TRANSITION_LINK, |
153 std::string()); | 157 std::string()); |
154 EXPECT_TRUE(contents_wrapper()->favicon_tab_helper()->ShouldDisplayFavicon()); | 158 EXPECT_TRUE(contents_wrapper()->favicon_tab_helper()->ShouldDisplayFavicon()); |
155 EXPECT_TRUE(contents()->FocusLocationBarByDefault()); | 159 EXPECT_TRUE(contents()->FocusLocationBarByDefault()); |
156 | 160 |
157 // Committing Web UI is tested above. | 161 // Committing Web UI is tested above. |
158 } | 162 } |
159 | 163 |
160 class TabContentsForFocusTest : public TestTabContents { | |
161 public: | |
162 TabContentsForFocusTest(content::BrowserContext* browser_context, | |
163 SiteInstance* instance) | |
164 : TestTabContents(browser_context, instance), focus_called_(0) { | |
165 } | |
166 | |
167 virtual void SetFocusToLocationBar(bool select_all) { ++focus_called_; } | |
168 int focus_called() const { return focus_called_; } | |
169 | |
170 private: | |
171 int focus_called_; | |
172 }; | |
173 | |
174 TEST_F(WebUITest, FocusOnNavigate) { | 164 TEST_F(WebUITest, FocusOnNavigate) { |
175 // Setup. |tc| will be used to track when we try to focus the location bar. | 165 // Setup. |wc| will be used to track when we try to focus the location bar. |
176 TabContentsForFocusTest* tc = new TabContentsForFocusTest( | 166 WebContents* wc = |
177 contents()->GetBrowserContext(), | 167 WebContentsTester::CreateTestWebContentsCountSetFocusToLocationBar( |
178 SiteInstance::Create(contents()->GetBrowserContext())); | 168 contents()->GetBrowserContext(), |
179 tc->GetController().CopyStateFrom(controller()); | 169 SiteInstance::Create(contents()->GetBrowserContext())); |
180 SetContents(tc); | 170 WebContentsTester* wct = WebContentsTester::For(wc); |
| 171 wc->GetController().CopyStateFrom(controller()); |
| 172 SetContents(wc); |
181 int page_id = 200; | 173 int page_id = 200; |
182 | 174 |
183 // Load the NTP. | 175 // Load the NTP. |
184 GURL new_tab_url(chrome::kChromeUINewTabURL); | 176 GURL new_tab_url(chrome::kChromeUINewTabURL); |
185 controller().LoadURL(new_tab_url, content::Referrer(), | 177 controller().LoadURL(new_tab_url, content::Referrer(), |
186 content::PAGE_TRANSITION_LINK, | 178 content::PAGE_TRANSITION_LINK, |
187 std::string()); | 179 std::string()); |
188 rvh_tester()->SendNavigate(page_id, new_tab_url); | 180 rvh_tester()->SendNavigate(page_id, new_tab_url); |
189 | 181 |
190 // Navigate to another page. | 182 // Navigate to another page. |
191 GURL next_url("http://google.com/"); | 183 GURL next_url("http://google.com/"); |
192 int next_page_id = page_id + 1; | 184 int next_page_id = page_id + 1; |
193 controller().LoadURL(next_url, content::Referrer(), | 185 controller().LoadURL(next_url, content::Referrer(), |
194 content::PAGE_TRANSITION_LINK, | 186 content::PAGE_TRANSITION_LINK, |
195 std::string()); | 187 std::string()); |
196 RenderViewHost* old_rvh = rvh(); | 188 RenderViewHost* old_rvh = rvh(); |
197 RenderViewHostTester::For(old_rvh)->SendShouldCloseACK(true); | 189 RenderViewHostTester::For(old_rvh)->SendShouldCloseACK(true); |
198 RenderViewHostTester::For( | 190 RenderViewHostTester::For( |
199 pending_rvh())->SendNavigate(next_page_id, next_url); | 191 pending_rvh())->SendNavigate(next_page_id, next_url); |
200 RenderViewHostTester::For(old_rvh)->SimulateSwapOutACK(); | 192 RenderViewHostTester::For(old_rvh)->SimulateSwapOutACK(); |
201 | 193 |
202 // Navigate back. Should focus the location bar. | 194 // Navigate back. Should focus the location bar. |
203 int focus_called = tc->focus_called(); | 195 int focus_called = wct->GetNumberOfFocusCalls(); |
204 ASSERT_TRUE(controller().CanGoBack()); | 196 ASSERT_TRUE(controller().CanGoBack()); |
205 controller().GoBack(); | 197 controller().GoBack(); |
206 old_rvh = rvh(); | 198 old_rvh = rvh(); |
207 RenderViewHostTester::For(old_rvh)->SendShouldCloseACK(true); | 199 RenderViewHostTester::For(old_rvh)->SendShouldCloseACK(true); |
208 RenderViewHostTester::For(pending_rvh())->SendNavigate(page_id, new_tab_url); | 200 RenderViewHostTester::For(pending_rvh())->SendNavigate(page_id, new_tab_url); |
209 RenderViewHostTester::For(old_rvh)->SimulateSwapOutACK(); | 201 RenderViewHostTester::For(old_rvh)->SimulateSwapOutACK(); |
210 EXPECT_LT(focus_called, tc->focus_called()); | 202 EXPECT_LT(focus_called, wct->GetNumberOfFocusCalls()); |
211 | 203 |
212 // Navigate forward. Shouldn't focus the location bar. | 204 // Navigate forward. Shouldn't focus the location bar. |
213 focus_called = tc->focus_called(); | 205 focus_called = wct->GetNumberOfFocusCalls(); |
214 ASSERT_TRUE(controller().CanGoForward()); | 206 ASSERT_TRUE(controller().CanGoForward()); |
215 controller().GoForward(); | 207 controller().GoForward(); |
216 old_rvh = rvh(); | 208 old_rvh = rvh(); |
217 RenderViewHostTester::For(old_rvh)->SendShouldCloseACK(true); | 209 RenderViewHostTester::For(old_rvh)->SendShouldCloseACK(true); |
218 RenderViewHostTester::For( | 210 RenderViewHostTester::For( |
219 pending_rvh())->SendNavigate(next_page_id, next_url); | 211 pending_rvh())->SendNavigate(next_page_id, next_url); |
220 RenderViewHostTester::For(old_rvh)->SimulateSwapOutACK(); | 212 RenderViewHostTester::For(old_rvh)->SimulateSwapOutACK(); |
221 EXPECT_EQ(focus_called, tc->focus_called()); | 213 EXPECT_EQ(focus_called, wct->GetNumberOfFocusCalls()); |
222 } | 214 } |
OLD | NEW |