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

Side by Side Diff: content/browser/web_contents/navigation_controller_impl_unittest.cc

Issue 10830144: Consolidate all NavigationController::LoadURL and family functions (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: address comments Created 8 years, 4 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
OLDNEW
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 "base/file_util.h" 5 #include "base/file_util.h"
6 #include "base/memory/scoped_ptr.h" 6 #include "base/memory/scoped_ptr.h"
7 #include "base/path_service.h" 7 #include "base/path_service.h"
8 #include "base/stl_util.h" 8 #include "base/stl_util.h"
9 #include "base/string_util.h" 9 #include "base/string_util.h"
10 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 EXPECT_EQ(controller.GetEntryCount(), 2); 174 EXPECT_EQ(controller.GetEntryCount(), 2);
175 EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 1); 175 EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 1);
176 EXPECT_EQ(controller.GetPendingEntryIndex(), -1); 176 EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
177 EXPECT_TRUE(controller.GetLastCommittedEntry()); 177 EXPECT_TRUE(controller.GetLastCommittedEntry());
178 EXPECT_FALSE(controller.GetPendingEntry()); 178 EXPECT_FALSE(controller.GetPendingEntry());
179 EXPECT_TRUE(controller.CanGoBack()); 179 EXPECT_TRUE(controller.CanGoBack());
180 EXPECT_FALSE(controller.CanGoForward()); 180 EXPECT_FALSE(controller.CanGoForward());
181 EXPECT_EQ(contents()->GetMaxPageID(), 1); 181 EXPECT_EQ(contents()->GetMaxPageID(), 1);
182 } 182 }
183 183
184 void CheckNavigationEntryMatchLoadParams(
185 NavigationController::LoadURLParams& load_params,
186 NavigationEntryImpl* entry) {
187 EXPECT_EQ(load_params.url, entry->GetURL());
188 EXPECT_EQ(load_params.referrer.url, entry->GetReferrer().url);
189 EXPECT_EQ(load_params.referrer.policy, entry->GetReferrer().policy);
190 EXPECT_EQ(load_params.transition_type, entry->GetTransitionType());
191 EXPECT_EQ(load_params.extra_headers, entry->extra_headers());
192
193 EXPECT_EQ(load_params.is_renderer_initiated, entry->is_renderer_initiated());
194 EXPECT_EQ(load_params.base_url_for_data_url, entry->GetBaseURLForDataURL());
195 if (!load_params.virtual_url_for_data_url.is_empty()) {
196 EXPECT_EQ(load_params.virtual_url_for_data_url, entry->GetVirtualURL());
197 }
198 if (NavigationController::UA_OVERRIDE_INHERIT !=
199 load_params.override_user_agent) {
200 bool should_override = (NavigationController::UA_OVERRIDE_TRUE ==
201 load_params.override_user_agent);
202 EXPECT_EQ(should_override, entry->GetIsOverridingUserAgent());
203 }
204 EXPECT_EQ(load_params.browser_initiated_post_data,
205 entry->GetBrowserInitiatedPostData());
206 EXPECT_EQ(load_params.transferred_global_request_id,
207 entry->transferred_global_request_id());
208 }
209
210 TEST_F(NavigationControllerTest, LoadURLWithParams) {
211 NavigationControllerImpl& controller = controller_impl();
212
213 NavigationController::LoadURLParams load_params(GURL("http://foo"));
214 load_params.referrer = content::Referrer(GURL("http://referrer"),
215 WebKit::WebReferrerPolicyDefault);
216 load_params.transition_type = content::PAGE_TRANSITION_GENERATED;
217 load_params.extra_headers = "content-type: text/plain";
218 load_params.load_type = NavigationController::LOAD_TYPE_DEFAULT;
219 load_params.is_renderer_initiated = true;
220 load_params.override_user_agent = NavigationController::UA_OVERRIDE_TRUE;
221 load_params.transferred_global_request_id = content::GlobalRequestID(2,3);
222
223 controller.LoadURLWithParams(load_params);
224 NavigationEntryImpl* entry =
225 NavigationEntryImpl::FromNavigationEntry(
226 controller.GetPendingEntry());
227
228 CheckNavigationEntryMatchLoadParams(load_params, entry);
229 }
230
231 TEST_F(NavigationControllerTest, LoadURLWithExtraParams_Data) {
232 NavigationControllerImpl& controller = controller_impl();
233
234 NavigationController::LoadURLParams load_params(
235 GURL("data:text/html,dataurl"));
236 load_params.load_type = NavigationController::LOAD_TYPE_DATA;
237 load_params.base_url_for_data_url = GURL("http://foo");
238 load_params.virtual_url_for_data_url = GURL("about:blank");
239 load_params.override_user_agent = NavigationController::UA_OVERRIDE_FALSE;
240
241 controller.LoadURLWithParams(load_params);
242 NavigationEntryImpl* entry =
243 NavigationEntryImpl::FromNavigationEntry(
244 controller.GetPendingEntry());
245
246 CheckNavigationEntryMatchLoadParams(load_params, entry);
247 }
248
249 TEST_F(NavigationControllerTest, LoadURLWithExtraParams_HttpPost) {
250 NavigationControllerImpl& controller = controller_impl();
251
252 NavigationController::LoadURLParams load_params(GURL("https://posturl"));
253 load_params.transition_type = content::PAGE_TRANSITION_TYPED;
254 load_params.load_type =
255 NavigationController::LOAD_TYPE_BROWSER_INITIATED_HTTP_POST;
256 load_params.override_user_agent = NavigationController::UA_OVERRIDE_TRUE;
257
258
259 const unsigned char* raw_data =
260 reinterpret_cast<const unsigned char*>("d\n\0a2");
261 const int length = 5;
262 std::vector<unsigned char> post_data_vector(raw_data, raw_data+length);
263 scoped_refptr<base::RefCountedBytes> data =
264 base::RefCountedBytes::TakeVector(&post_data_vector);
265 load_params.browser_initiated_post_data = data.get();
266
267 controller.LoadURLWithParams(load_params);
268 NavigationEntryImpl* entry =
269 NavigationEntryImpl::FromNavigationEntry(
270 controller.GetPendingEntry());
271
272 CheckNavigationEntryMatchLoadParams(load_params, entry);
273 }
274
184 // Tests what happens when the same page is loaded again. Should not create a 275 // Tests what happens when the same page is loaded again. Should not create a
185 // new session history entry. This is what happens when you press enter in the 276 // new session history entry. This is what happens when you press enter in the
186 // URL bar to reload: a pending entry is created and then it is discarded when 277 // URL bar to reload: a pending entry is created and then it is discarded when
187 // the load commits (because WebCore didn't actually make a new entry). 278 // the load commits (because WebCore didn't actually make a new entry).
188 TEST_F(NavigationControllerTest, LoadURL_SamePage) { 279 TEST_F(NavigationControllerTest, LoadURL_SamePage) {
189 NavigationControllerImpl& controller = controller_impl(); 280 NavigationControllerImpl& controller = controller_impl();
190 TestNotificationTracker notifications; 281 TestNotificationTracker notifications;
191 RegisterForAllNavNotifications(&notifications, &controller); 282 RegisterForAllNavNotifications(&notifications, &controller);
192 283
193 const GURL url1("http://foo1"); 284 const GURL url1("http://foo1");
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after
502 // Set a WebContentsDelegate to listen for state changes. 593 // Set a WebContentsDelegate to listen for state changes.
503 scoped_ptr<TestWebContentsDelegate> delegate(new TestWebContentsDelegate()); 594 scoped_ptr<TestWebContentsDelegate> delegate(new TestWebContentsDelegate());
504 EXPECT_FALSE(contents()->GetDelegate()); 595 EXPECT_FALSE(contents()->GetDelegate());
505 contents()->SetDelegate(delegate.get()); 596 contents()->SetDelegate(delegate.get());
506 597
507 // Without any navigations, the renderer starts at about:blank. 598 // Without any navigations, the renderer starts at about:blank.
508 const GURL kExistingURL("about:blank"); 599 const GURL kExistingURL("about:blank");
509 600
510 // Now make a pending new navigation, initiated by the renderer. 601 // Now make a pending new navigation, initiated by the renderer.
511 const GURL kNewURL("http://eh"); 602 const GURL kNewURL("http://eh");
512 controller.LoadURLFromRenderer( 603 NavigationController::LoadURLParams load_url_params(kNewURL);
513 kNewURL, content::Referrer(), content::PAGE_TRANSITION_TYPED, 604 load_url_params.transition_type = content::PAGE_TRANSITION_TYPED;
514 std::string()); 605 load_url_params.is_renderer_initiated = true;
606 controller.LoadURLWithParams(load_url_params);
515 EXPECT_EQ(0U, notifications.size()); 607 EXPECT_EQ(0U, notifications.size());
516 EXPECT_EQ(-1, controller.GetPendingEntryIndex()); 608 EXPECT_EQ(-1, controller.GetPendingEntryIndex());
517 EXPECT_TRUE(controller.GetPendingEntry()); 609 EXPECT_TRUE(controller.GetPendingEntry());
518 EXPECT_EQ(-1, controller.GetLastCommittedEntryIndex()); 610 EXPECT_EQ(-1, controller.GetLastCommittedEntryIndex());
519 EXPECT_EQ(1, delegate->navigation_state_change_count()); 611 EXPECT_EQ(1, delegate->navigation_state_change_count());
520 612
521 // There should be no visible entry (resulting in about:blank in the 613 // There should be no visible entry (resulting in about:blank in the
522 // omnibox), because it was renderer-initiated and there's no last committed 614 // omnibox), because it was renderer-initiated and there's no last committed
523 // entry. 615 // entry.
524 EXPECT_FALSE(controller.GetVisibleEntry()); 616 EXPECT_FALSE(controller.GetVisibleEntry());
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 EXPECT_EQ(-1, controller.GetLastCommittedEntryIndex()); 648 EXPECT_EQ(-1, controller.GetLastCommittedEntryIndex());
557 EXPECT_EQ(1, delegate->navigation_state_change_count()); 649 EXPECT_EQ(1, delegate->navigation_state_change_count());
558 650
559 // There should be no visible entry (resulting in about:blank in the 651 // There should be no visible entry (resulting in about:blank in the
560 // omnibox), ensuring no spoof is possible. 652 // omnibox), ensuring no spoof is possible.
561 EXPECT_FALSE(controller.GetVisibleEntry()); 653 EXPECT_FALSE(controller.GetVisibleEntry());
562 654
563 contents()->SetDelegate(NULL); 655 contents()->SetDelegate(NULL);
564 } 656 }
565 657
566 // Test NavigationEntry is constructed correctly. No other logic tested.
567 TEST_F(NavigationControllerTest, PostURL) {
568 NavigationControllerImpl& controller = controller_impl();
569
570 const GURL url("http://foo1");
571
572 const int length = 5;
573 const unsigned char* raw_data =
574 reinterpret_cast<const unsigned char*>("d\n\0a2");
575 std::vector<unsigned char> post_data_vector(raw_data, raw_data+length);
576 scoped_refptr<base::RefCountedBytes> data =
577 base::RefCountedBytes::TakeVector(&post_data_vector);
578
579 controller.PostURL(url, content::Referrer(), *data.get(), true);
580
581 NavigationEntryImpl* post_entry =
582 NavigationEntryImpl::FromNavigationEntry(
583 controller.GetPendingEntry());
584
585 EXPECT_TRUE(post_entry);
586 EXPECT_TRUE(post_entry->GetHasPostData());
587 EXPECT_EQ(data->front(),
588 post_entry->GetBrowserInitiatedPostData()->front());
589 }
590
591 TEST_F(NavigationControllerTest, Reload) { 658 TEST_F(NavigationControllerTest, Reload) {
592 NavigationControllerImpl& controller = controller_impl(); 659 NavigationControllerImpl& controller = controller_impl();
593 TestNotificationTracker notifications; 660 TestNotificationTracker notifications;
594 RegisterForAllNavNotifications(&notifications, &controller); 661 RegisterForAllNavNotifications(&notifications, &controller);
595 662
596 const GURL url1("http://foo1"); 663 const GURL url1("http://foo1");
597 664
598 controller.LoadURL( 665 controller.LoadURL(
599 url1, content::Referrer(), content::PAGE_TRANSITION_TYPED, std::string()); 666 url1, content::Referrer(), content::PAGE_TRANSITION_TYPED, std::string());
600 EXPECT_EQ(0U, notifications.size()); 667 EXPECT_EQ(0U, notifications.size());
(...skipping 1338 matching lines...) Expand 10 before | Expand all | Expand 10 after
1939 // For typed navigations (browser-initiated), both active and visible entries 2006 // For typed navigations (browser-initiated), both active and visible entries
1940 // should update before commit. 2007 // should update before commit.
1941 controller.LoadURL(url0, content::Referrer(), 2008 controller.LoadURL(url0, content::Referrer(),
1942 content::PAGE_TRANSITION_TYPED, std::string()); 2009 content::PAGE_TRANSITION_TYPED, std::string());
1943 EXPECT_EQ(url0, controller.GetActiveEntry()->GetURL()); 2010 EXPECT_EQ(url0, controller.GetActiveEntry()->GetURL());
1944 EXPECT_EQ(url0, controller.GetVisibleEntry()->GetURL()); 2011 EXPECT_EQ(url0, controller.GetVisibleEntry()->GetURL());
1945 test_rvh()->SendNavigate(0, url0); 2012 test_rvh()->SendNavigate(0, url0);
1946 2013
1947 // For link clicks (renderer-initiated navigations), the active entry should 2014 // For link clicks (renderer-initiated navigations), the active entry should
1948 // update before commit but the visible should not. 2015 // update before commit but the visible should not.
1949 controller.LoadURLFromRenderer(url1, content::Referrer(), 2016 NavigationController::LoadURLParams load_url_params(url1);
1950 content::PAGE_TRANSITION_LINK, 2017 load_url_params.is_renderer_initiated = true;
1951 std::string()); 2018 controller.LoadURLWithParams(load_url_params);
1952 EXPECT_EQ(url1, controller.GetActiveEntry()->GetURL()); 2019 EXPECT_EQ(url1, controller.GetActiveEntry()->GetURL());
1953 EXPECT_EQ(url0, controller.GetVisibleEntry()->GetURL()); 2020 EXPECT_EQ(url0, controller.GetVisibleEntry()->GetURL());
1954 EXPECT_TRUE( 2021 EXPECT_TRUE(
1955 NavigationEntryImpl::FromNavigationEntry(controller.GetPendingEntry())-> 2022 NavigationEntryImpl::FromNavigationEntry(controller.GetPendingEntry())->
1956 is_renderer_initiated()); 2023 is_renderer_initiated());
1957 2024
1958 // After commit, both should be updated, and we should no longer treat the 2025 // After commit, both should be updated, and we should no longer treat the
1959 // entry as renderer-initiated. 2026 // entry as renderer-initiated.
1960 test_rvh()->SendNavigate(1, url1); 2027 test_rvh()->SendNavigate(1, url1);
1961 EXPECT_EQ(url1, controller.GetActiveEntry()->GetURL()); 2028 EXPECT_EQ(url1, controller.GetActiveEntry()->GetURL());
(...skipping 693 matching lines...) Expand 10 before | Expand all | Expand 10 after
2655 TabNavigation nav(0, url0, GURL(), string16(), 2722 TabNavigation nav(0, url0, GURL(), string16(),
2656 webkit_glue::CreateHistoryStateForURL(url0), 2723 webkit_glue::CreateHistoryStateForURL(url0),
2657 content::PAGE_TRANSITION_LINK); 2724 content::PAGE_TRANSITION_LINK);
2658 session_helper_.AssertNavigationEquals(nav, 2725 session_helper_.AssertNavigationEquals(nav,
2659 windows_[0]->tabs[0]->navigations[0]); 2726 windows_[0]->tabs[0]->navigations[0]);
2660 nav.set_url(url2); 2727 nav.set_url(url2);
2661 session_helper_.AssertNavigationEquals(nav, 2728 session_helper_.AssertNavigationEquals(nav,
2662 windows_[0]->tabs[0]->navigations[1]); 2729 windows_[0]->tabs[0]->navigations[1]);
2663 } 2730 }
2664 */ 2731 */
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698