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

Side by Side Diff: chrome/browser/ui/webui/web_ui_unittest.cc

Issue 9645003: Introduce abstractions to allow embedders to test RenderViewHost (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge to head. Created 8 years, 9 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 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" 11 #include "content/browser/tab_contents/test_tab_contents.h"
12 #include "content/public/browser/navigation_controller.h" 12 #include "content/public/browser/navigation_controller.h"
13 #include "content/public/browser/site_instance.h" 13 #include "content/public/browser/site_instance.h"
14 #include "content/test/test_browser_thread.h" 14 #include "content/test/test_browser_thread.h"
15 #include "content/test/test_renderer_host.h"
15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
16 17
17 using content::BrowserThread; 18 using content::BrowserThread;
18 using content::NavigationController; 19 using content::NavigationController;
20 using content::RenderViewHost;
21 using content::RenderViewHostTester;
19 using content::SiteInstance; 22 using content::SiteInstance;
20 using content::TestRenderViewHost;
21 using content::WebContents; 23 using content::WebContents;
22 24
23 class WebUITest : public TabContentsWrapperTestHarness { 25 class WebUITest : public TabContentsWrapperTestHarness {
24 public: 26 public:
25 WebUITest() : ui_thread_(BrowserThread::UI, MessageLoop::current()) {} 27 WebUITest() : ui_thread_(BrowserThread::UI, MessageLoop::current()) {}
26 28
27 // Tests navigating with a Web UI from a fresh (nothing pending or committed) 29 // Tests navigating with a Web UI from a fresh (nothing pending or committed)
28 // state, through pending, committed, then another navigation. The first page 30 // state, through pending, committed, then another navigation. The first page
29 // ID that we should use is passed as a parameter. We'll use the next two 31 // ID that we should use is passed as a parameter. We'll use the next two
30 // values. This must be increasing for the life of the tests. 32 // values. This must be increasing for the life of the tests.
31 static void DoNavigationTest(TabContentsWrapper* wrapper, int page_id) { 33 static void DoNavigationTest(TabContentsWrapper* wrapper, int page_id) {
32 WebContents* contents = wrapper->web_contents(); 34 WebContents* contents = wrapper->web_contents();
33 NavigationController* controller = &contents->GetController(); 35 NavigationController* controller = &contents->GetController();
34 36
35 // Start a pending load. 37 // Start a pending load.
36 GURL new_tab_url(chrome::kChromeUINewTabURL); 38 GURL new_tab_url(chrome::kChromeUINewTabURL);
37 controller->LoadURL(new_tab_url, content::Referrer(), 39 controller->LoadURL(new_tab_url, content::Referrer(),
38 content::PAGE_TRANSITION_LINK, 40 content::PAGE_TRANSITION_LINK,
39 std::string()); 41 std::string());
40 42
41 // The navigation entry should be pending with no committed entry. 43 // The navigation entry should be pending with no committed entry.
42 ASSERT_TRUE(controller->GetPendingEntry()); 44 ASSERT_TRUE(controller->GetPendingEntry());
43 ASSERT_FALSE(controller->GetLastCommittedEntry()); 45 ASSERT_FALSE(controller->GetLastCommittedEntry());
44 46
45 // Check the things the pending Web UI should have set. 47 // Check the things the pending Web UI should have set.
46 EXPECT_FALSE(wrapper->favicon_tab_helper()->ShouldDisplayFavicon()); 48 EXPECT_FALSE(wrapper->favicon_tab_helper()->ShouldDisplayFavicon());
47 EXPECT_TRUE(contents->FocusLocationBarByDefault()); 49 EXPECT_TRUE(contents->FocusLocationBarByDefault());
48 50
49 // Now commit the load. 51 // Now commit the load.
50 static_cast<TestRenderViewHost*>( 52 RenderViewHostTester::For(
51 contents->GetRenderViewHost())->SendNavigate(page_id, new_tab_url); 53 contents->GetRenderViewHost())->SendNavigate(page_id, new_tab_url);
52 54
53 // The same flags should be set as before now that the load has committed. 55 // The same flags should be set as before now that the load has committed.
54 EXPECT_FALSE(wrapper->favicon_tab_helper()->ShouldDisplayFavicon()); 56 EXPECT_FALSE(wrapper->favicon_tab_helper()->ShouldDisplayFavicon());
55 EXPECT_TRUE(contents->FocusLocationBarByDefault()); 57 EXPECT_TRUE(contents->FocusLocationBarByDefault());
56 58
57 // Start a pending navigation to a regular page. 59 // Start a pending navigation to a regular page.
58 GURL next_url("http://google.com/"); 60 GURL next_url("http://google.com/");
59 controller->LoadURL(next_url, content::Referrer(), 61 controller->LoadURL(next_url, content::Referrer(),
60 content::PAGE_TRANSITION_LINK, 62 content::PAGE_TRANSITION_LINK,
61 std::string()); 63 std::string());
62 64
63 // Check the flags. Some should reflect the new page (URL, title), some 65 // Check the flags. Some should reflect the new page (URL, title), some
64 // should reflect the old one (bookmark bar) until it has committed. 66 // should reflect the old one (bookmark bar) until it has committed.
65 EXPECT_TRUE(wrapper->favicon_tab_helper()->ShouldDisplayFavicon()); 67 EXPECT_TRUE(wrapper->favicon_tab_helper()->ShouldDisplayFavicon());
66 EXPECT_FALSE(contents->FocusLocationBarByDefault()); 68 EXPECT_FALSE(contents->FocusLocationBarByDefault());
67 69
68 // Commit the regular page load. Note that we must send it to the "pending" 70 // Commit the regular page load. Note that we must send it to the "pending"
69 // RenderViewHost if there is one, since this transition will also cause a 71 // RenderViewHost if there is one, since this transition will also cause a
70 // process transition, and our RVH pointer will be the "committed" one. 72 // process transition, and our RVH pointer will be the "committed" one.
71 // In the second call to this function from WebUIToStandard, it won't 73 // In the second call to this function from WebUIToStandard, it won't
72 // actually be pending, which is the point of this test. 74 // actually be pending, which is the point of this test.
73 TestRenderViewHost* pending_rvh = 75 RenderViewHost* pending_rvh =
74 TestRenderViewHost::GetPendingForController(controller); 76 RenderViewHostTester::GetPendingForController(controller);
75 if (pending_rvh) { 77 if (pending_rvh) {
76 pending_rvh->SendNavigate(page_id + 1, next_url); 78 RenderViewHostTester::For(
79 pending_rvh)->SendNavigate(page_id + 1, next_url);
77 } else { 80 } else {
78 static_cast<TestRenderViewHost*>( 81 RenderViewHostTester::For(
79 contents->GetRenderViewHost())->SendNavigate(page_id + 1, next_url); 82 contents->GetRenderViewHost())->SendNavigate(page_id + 1, next_url);
80 } 83 }
81 84
82 // The state should now reflect a regular page. 85 // The state should now reflect a regular page.
83 EXPECT_TRUE(wrapper->favicon_tab_helper()->ShouldDisplayFavicon()); 86 EXPECT_TRUE(wrapper->favicon_tab_helper()->ShouldDisplayFavicon());
84 EXPECT_FALSE(contents->FocusLocationBarByDefault()); 87 EXPECT_FALSE(contents->FocusLocationBarByDefault());
85 } 88 }
86 89
87 private: 90 private:
88 content::TestBrowserThread ui_thread_; 91 content::TestBrowserThread ui_thread_;
(...skipping 16 matching lines...) Expand all
105 108
106 DoNavigationTest(&wrapper2, 101); 109 DoNavigationTest(&wrapper2, 101);
107 } 110 }
108 111
109 TEST_F(WebUITest, WebUIToWebUI) { 112 TEST_F(WebUITest, WebUIToWebUI) {
110 // Do a load (this state is tested above). 113 // Do a load (this state is tested above).
111 GURL new_tab_url(chrome::kChromeUINewTabURL); 114 GURL new_tab_url(chrome::kChromeUINewTabURL);
112 controller().LoadURL(new_tab_url, content::Referrer(), 115 controller().LoadURL(new_tab_url, content::Referrer(),
113 content::PAGE_TRANSITION_LINK, 116 content::PAGE_TRANSITION_LINK,
114 std::string()); 117 std::string());
115 rvh()->SendNavigate(1, new_tab_url); 118 rvh_tester()->SendNavigate(1, new_tab_url);
116 119
117 // Start another pending load of the new tab page. 120 // Start another pending load of the new tab page.
118 controller().LoadURL(new_tab_url, content::Referrer(), 121 controller().LoadURL(new_tab_url, content::Referrer(),
119 content::PAGE_TRANSITION_LINK, 122 content::PAGE_TRANSITION_LINK,
120 std::string()); 123 std::string());
121 rvh()->SendNavigate(2, new_tab_url); 124 rvh_tester()->SendNavigate(2, new_tab_url);
122 125
123 // The flags should be the same as the non-pending state. 126 // The flags should be the same as the non-pending state.
124 EXPECT_FALSE( 127 EXPECT_FALSE(
125 contents_wrapper()->favicon_tab_helper()->ShouldDisplayFavicon()); 128 contents_wrapper()->favicon_tab_helper()->ShouldDisplayFavicon());
126 EXPECT_TRUE(contents()->FocusLocationBarByDefault()); 129 EXPECT_TRUE(contents()->FocusLocationBarByDefault());
127 } 130 }
128 131
129 TEST_F(WebUITest, StandardToWebUI) { 132 TEST_F(WebUITest, StandardToWebUI) {
130 // Start a pending navigation to a regular page. 133 // Start a pending navigation to a regular page.
131 GURL std_url("http://google.com/"); 134 GURL std_url("http://google.com/");
132 135
133 controller().LoadURL(std_url, content::Referrer(), 136 controller().LoadURL(std_url, content::Referrer(),
134 content::PAGE_TRANSITION_LINK, 137 content::PAGE_TRANSITION_LINK,
135 std::string()); 138 std::string());
136 139
137 // The state should now reflect the default. 140 // The state should now reflect the default.
138 EXPECT_TRUE(contents_wrapper()->favicon_tab_helper()->ShouldDisplayFavicon()); 141 EXPECT_TRUE(contents_wrapper()->favicon_tab_helper()->ShouldDisplayFavicon());
139 EXPECT_FALSE(contents()->FocusLocationBarByDefault()); 142 EXPECT_FALSE(contents()->FocusLocationBarByDefault());
140 143
141 // Commit the load, the state should be the same. 144 // Commit the load, the state should be the same.
142 rvh()->SendNavigate(1, std_url); 145 rvh_tester()->SendNavigate(1, std_url);
143 EXPECT_TRUE(contents_wrapper()->favicon_tab_helper()->ShouldDisplayFavicon()); 146 EXPECT_TRUE(contents_wrapper()->favicon_tab_helper()->ShouldDisplayFavicon());
144 EXPECT_FALSE(contents()->FocusLocationBarByDefault()); 147 EXPECT_FALSE(contents()->FocusLocationBarByDefault());
145 148
146 // Start a pending load for a WebUI. 149 // Start a pending load for a WebUI.
147 GURL new_tab_url(chrome::kChromeUINewTabURL); 150 GURL new_tab_url(chrome::kChromeUINewTabURL);
148 controller().LoadURL(new_tab_url, content::Referrer(), 151 controller().LoadURL(new_tab_url, content::Referrer(),
149 content::PAGE_TRANSITION_LINK, 152 content::PAGE_TRANSITION_LINK,
150 std::string()); 153 std::string());
151 EXPECT_TRUE(contents_wrapper()->favicon_tab_helper()->ShouldDisplayFavicon()); 154 EXPECT_TRUE(contents_wrapper()->favicon_tab_helper()->ShouldDisplayFavicon());
152 EXPECT_TRUE(contents()->FocusLocationBarByDefault()); 155 EXPECT_TRUE(contents()->FocusLocationBarByDefault());
(...skipping 22 matching lines...) Expand all
175 SiteInstance::Create(contents()->GetBrowserContext())); 178 SiteInstance::Create(contents()->GetBrowserContext()));
176 tc->GetController().CopyStateFrom(controller()); 179 tc->GetController().CopyStateFrom(controller());
177 SetContents(tc); 180 SetContents(tc);
178 int page_id = 200; 181 int page_id = 200;
179 182
180 // Load the NTP. 183 // Load the NTP.
181 GURL new_tab_url(chrome::kChromeUINewTabURL); 184 GURL new_tab_url(chrome::kChromeUINewTabURL);
182 controller().LoadURL(new_tab_url, content::Referrer(), 185 controller().LoadURL(new_tab_url, content::Referrer(),
183 content::PAGE_TRANSITION_LINK, 186 content::PAGE_TRANSITION_LINK,
184 std::string()); 187 std::string());
185 rvh()->SendNavigate(page_id, new_tab_url); 188 rvh_tester()->SendNavigate(page_id, new_tab_url);
186 189
187 // Navigate to another page. 190 // Navigate to another page.
188 GURL next_url("http://google.com/"); 191 GURL next_url("http://google.com/");
189 int next_page_id = page_id + 1; 192 int next_page_id = page_id + 1;
190 controller().LoadURL(next_url, content::Referrer(), 193 controller().LoadURL(next_url, content::Referrer(),
191 content::PAGE_TRANSITION_LINK, 194 content::PAGE_TRANSITION_LINK,
192 std::string()); 195 std::string());
193 TestRenderViewHost* old_rvh = rvh(); 196 RenderViewHost* old_rvh = rvh();
194 old_rvh->SendShouldCloseACK(true); 197 RenderViewHostTester::For(old_rvh)->SendShouldCloseACK(true);
195 pending_rvh()->SendNavigate(next_page_id, next_url); 198 RenderViewHostTester::For(
196 old_rvh->OnSwapOutACK(); 199 pending_rvh())->SendNavigate(next_page_id, next_url);
200 RenderViewHostTester::For(old_rvh)->SimulateSwapOutACK();
197 201
198 // Navigate back. Should focus the location bar. 202 // Navigate back. Should focus the location bar.
199 int focus_called = tc->focus_called(); 203 int focus_called = tc->focus_called();
200 ASSERT_TRUE(controller().CanGoBack()); 204 ASSERT_TRUE(controller().CanGoBack());
201 controller().GoBack(); 205 controller().GoBack();
202 old_rvh = rvh(); 206 old_rvh = rvh();
203 old_rvh->SendShouldCloseACK(true); 207 RenderViewHostTester::For(old_rvh)->SendShouldCloseACK(true);
204 pending_rvh()->SendNavigate(page_id, new_tab_url); 208 RenderViewHostTester::For(pending_rvh())->SendNavigate(page_id, new_tab_url);
205 old_rvh->OnSwapOutACK(); 209 RenderViewHostTester::For(old_rvh)->SimulateSwapOutACK();
206 EXPECT_LT(focus_called, tc->focus_called()); 210 EXPECT_LT(focus_called, tc->focus_called());
207 211
208 // Navigate forward. Shouldn't focus the location bar. 212 // Navigate forward. Shouldn't focus the location bar.
209 focus_called = tc->focus_called(); 213 focus_called = tc->focus_called();
210 ASSERT_TRUE(controller().CanGoForward()); 214 ASSERT_TRUE(controller().CanGoForward());
211 controller().GoForward(); 215 controller().GoForward();
212 old_rvh = rvh(); 216 old_rvh = rvh();
213 old_rvh->SendShouldCloseACK(true); 217 RenderViewHostTester::For(old_rvh)->SendShouldCloseACK(true);
214 pending_rvh()->SendNavigate(next_page_id, next_url); 218 RenderViewHostTester::For(
215 old_rvh->OnSwapOutACK(); 219 pending_rvh())->SendNavigate(next_page_id, next_url);
220 RenderViewHostTester::For(old_rvh)->SimulateSwapOutACK();
216 EXPECT_EQ(focus_called, tc->focus_called()); 221 EXPECT_EQ(focus_called, tc->focus_called());
217 } 222 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698