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

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

Issue 27045005: When a page is reloaded using the original request URL, ensure that the RVH picked to render it is … (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: review comments Created 7 years, 2 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) 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/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/bind.h" 6 #include "base/bind.h"
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
11 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "base/time/time.h" 13 #include "base/time/time.h"
14 // These are only used for commented out tests. If someone wants to enable
15 // them, they should be moved to chrome first.
16 // #include "chrome/browser/history/history_service.h"
17 // #include "chrome/browser/profiles/profile_manager.h"
18 // #include "chrome/browser/sessions/session_service.h"
19 // #include "chrome/browser/sessions/session_service_factory.h"
20 // #include "chrome/browser/sessions/session_service_test_helper.h"
21 // #include "chrome/browser/sessions/session_types.h"
22 #include "content/browser/renderer_host/test_render_view_host.h" 14 #include "content/browser/renderer_host/test_render_view_host.h"
23 #include "content/browser/site_instance_impl.h" 15 #include "content/browser/site_instance_impl.h"
24 #include "content/browser/web_contents/navigation_controller_impl.h" 16 #include "content/browser/web_contents/navigation_controller_impl.h"
25 #include "content/browser/web_contents/navigation_entry_impl.h" 17 #include "content/browser/web_contents/navigation_entry_impl.h"
26 #include "content/browser/web_contents/web_contents_impl.h" 18 #include "content/browser/web_contents/web_contents_impl.h"
27 #include "content/browser/web_contents/web_contents_screenshot_manager.h" 19 #include "content/browser/web_contents/web_contents_screenshot_manager.h"
28 #include "content/common/view_messages.h" 20 #include "content/common/view_messages.h"
29 #include "content/public/browser/navigation_details.h" 21 #include "content/public/browser/navigation_details.h"
30 #include "content/public/browser/notification_registrar.h" 22 #include "content/public/browser/notification_registrar.h"
31 #include "content/public/browser/notification_types.h" 23 #include "content/public/browser/notification_types.h"
32 #include "content/public/browser/render_view_host.h" 24 #include "content/public/browser/render_view_host.h"
33 #include "content/public/browser/render_view_host_observer.h"
34 #include "content/public/browser/web_contents_delegate.h" 25 #include "content/public/browser/web_contents_delegate.h"
35 #include "content/public/browser/web_contents_observer.h" 26 #include "content/public/browser/web_contents_observer.h"
36 #include "content/public/common/page_state.h" 27 #include "content/public/common/page_state.h"
37 #include "content/public/common/url_constants.h" 28 #include "content/public/common/url_constants.h"
38 #include "content/public/test/mock_render_process_host.h" 29 #include "content/public/test/mock_render_process_host.h"
39 #include "content/public/test/test_notification_tracker.h" 30 #include "content/public/test/test_notification_tracker.h"
40 #include "content/public/test/test_utils.h" 31 #include "content/public/test/test_utils.h"
41 #include "content/test/test_web_contents.h" 32 #include "content/test/test_web_contents.h"
42 #include "net/base/net_util.h" 33 #include "net/base/net_util.h"
43 #include "skia/ext/platform_canvas.h" 34 #include "skia/ext/platform_canvas.h"
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 } 186 }
196 187
197 virtual void SetUp() OVERRIDE { 188 virtual void SetUp() OVERRIDE {
198 RenderViewHostImplTestHarness::SetUp(); 189 RenderViewHostImplTestHarness::SetUp();
199 WebContents* web_contents = RenderViewHostImplTestHarness::web_contents(); 190 WebContents* web_contents = RenderViewHostImplTestHarness::web_contents();
200 ASSERT_TRUE(web_contents); // The WebContents should be created by now. 191 ASSERT_TRUE(web_contents); // The WebContents should be created by now.
201 WebContentsObserver::Observe(web_contents); 192 WebContentsObserver::Observe(web_contents);
202 } 193 }
203 194
204 // WebContentsObserver: 195 // WebContentsObserver:
196 virtual void NavigateToPendingEntry(
197 const GURL& url,
198 NavigationController::ReloadType reload_type) OVERRIDE {
199 navigated_url_ = url;
200 }
201
205 virtual void NavigationEntryCommitted( 202 virtual void NavigationEntryCommitted(
206 const LoadCommittedDetails& load_details) OVERRIDE { 203 const LoadCommittedDetails& load_details) OVERRIDE {
207 navigation_entry_committed_counter_++; 204 navigation_entry_committed_counter_++;
208 } 205 }
209 206
207 const GURL& navigated_url() const {
208 return navigated_url_;
209 }
210
210 NavigationControllerImpl& controller_impl() { 211 NavigationControllerImpl& controller_impl() {
211 return static_cast<NavigationControllerImpl&>(controller()); 212 return static_cast<NavigationControllerImpl&>(controller());
212 } 213 }
213 214
214 protected: 215 protected:
216 GURL navigated_url_;
215 size_t navigation_entry_committed_counter_; 217 size_t navigation_entry_committed_counter_;
216 }; 218 };
217 219
218 void RegisterForAllNavNotifications(TestNotificationTracker* tracker, 220 void RegisterForAllNavNotifications(TestNotificationTracker* tracker,
219 NavigationController* controller) { 221 NavigationController* controller) {
220 tracker->ListenFor(NOTIFICATION_NAV_LIST_PRUNED, 222 tracker->ListenFor(NOTIFICATION_NAV_LIST_PRUNED,
221 Source<NavigationController>(controller)); 223 Source<NavigationController>(controller));
222 tracker->ListenFor(NOTIFICATION_NAV_ENTRY_CHANGED, 224 tracker->ListenFor(NOTIFICATION_NAV_ENTRY_CHANGED,
223 Source<NavigationController>(controller)); 225 Source<NavigationController>(controller));
224 } 226 }
(...skipping 923 matching lines...) Expand 10 before | Expand all | Expand 10 after
1148 // Now the reload is committed. 1150 // Now the reload is committed.
1149 EXPECT_EQ(controller.GetEntryCount(), 2); 1151 EXPECT_EQ(controller.GetEntryCount(), 2);
1150 EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 1); 1152 EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 1);
1151 EXPECT_EQ(controller.GetPendingEntryIndex(), -1); 1153 EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
1152 EXPECT_TRUE(controller.GetLastCommittedEntry()); 1154 EXPECT_TRUE(controller.GetLastCommittedEntry());
1153 EXPECT_FALSE(controller.GetPendingEntry()); 1155 EXPECT_FALSE(controller.GetPendingEntry());
1154 EXPECT_TRUE(controller.CanGoBack()); 1156 EXPECT_TRUE(controller.CanGoBack());
1155 EXPECT_FALSE(controller.CanGoForward()); 1157 EXPECT_FALSE(controller.CanGoForward());
1156 } 1158 }
1157 1159
1158 class TestNavigationObserver : public RenderViewHostObserver {
1159 public:
1160 explicit TestNavigationObserver(RenderViewHost* render_view_host)
1161 : RenderViewHostObserver(render_view_host) {
1162 }
1163
1164 const GURL& navigated_url() const {
1165 return navigated_url_;
1166 }
1167
1168 protected:
1169 virtual void Navigate(const GURL& url) OVERRIDE {
1170 navigated_url_ = url;
1171 }
1172
1173 private:
1174 GURL navigated_url_;
1175 };
1176
1177 #if !defined(OS_ANDROID) // http://crbug.com/157428 1160 #if !defined(OS_ANDROID) // http://crbug.com/157428
1178 TEST_F(NavigationControllerTest, ReloadOriginalRequestURL) { 1161 TEST_F(NavigationControllerTest, ReloadOriginalRequestURL) {
1179 NavigationControllerImpl& controller = controller_impl(); 1162 NavigationControllerImpl& controller = controller_impl();
1180 TestNotificationTracker notifications; 1163 TestNotificationTracker notifications;
1181 RegisterForAllNavNotifications(&notifications, &controller); 1164 RegisterForAllNavNotifications(&notifications, &controller);
1182 TestNavigationObserver observer(test_rvh());
1183 1165
1184 const GURL original_url("http://foo1"); 1166 const GURL original_url("http://foo1");
1185 const GURL final_url("http://foo2"); 1167 const GURL final_url("http://foo2");
1186 1168
1187 // Load up the original URL, but get redirected. 1169 // Load up the original URL, but get redirected.
1188 controller.LoadURL( 1170 controller.LoadURL(
1189 original_url, Referrer(), PAGE_TRANSITION_TYPED, std::string()); 1171 original_url, Referrer(), PAGE_TRANSITION_TYPED, std::string());
1190 EXPECT_EQ(0U, notifications.size()); 1172 EXPECT_EQ(0U, notifications.size());
1191 test_rvh()->SendNavigateWithOriginalRequestURL(0, final_url, original_url); 1173 test_rvh()->SendNavigateWithOriginalRequestURL(0, final_url, original_url);
1192 EXPECT_EQ(1U, navigation_entry_committed_counter_); 1174 EXPECT_EQ(1U, navigation_entry_committed_counter_);
1193 navigation_entry_committed_counter_ = 0; 1175 navigation_entry_committed_counter_ = 0;
1194 1176
1195 // The NavigationEntry should save both the original URL and the final 1177 // The NavigationEntry should save both the original URL and the final
1196 // redirected URL. 1178 // redirected URL.
1197 EXPECT_EQ( 1179 EXPECT_EQ(
1198 original_url, controller.GetVisibleEntry()->GetOriginalRequestURL()); 1180 original_url, controller.GetVisibleEntry()->GetOriginalRequestURL());
1199 EXPECT_EQ(final_url, controller.GetVisibleEntry()->GetURL()); 1181 EXPECT_EQ(final_url, controller.GetVisibleEntry()->GetURL());
1200 1182
1201 // Reload using the original URL. 1183 // Reload using the original URL.
1202 controller.GetVisibleEntry()->SetTitle(ASCIIToUTF16("Title")); 1184 controller.GetVisibleEntry()->SetTitle(ASCIIToUTF16("Title"));
1203 controller.ReloadOriginalRequestURL(false); 1185 controller.ReloadOriginalRequestURL(false);
1204 EXPECT_EQ(0U, notifications.size()); 1186 EXPECT_EQ(0U, notifications.size());
1205 1187
1206 // The reload is pending. The request should point to the original URL. 1188 // The reload is pending. The request should point to the original URL.
1207 EXPECT_EQ(original_url, observer.navigated_url()); 1189 EXPECT_EQ(original_url, navigated_url());
1208 EXPECT_EQ(controller.GetEntryCount(), 1); 1190 EXPECT_EQ(controller.GetEntryCount(), 1);
1209 EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 0); 1191 EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 0);
1210 EXPECT_EQ(controller.GetPendingEntryIndex(), 0); 1192 EXPECT_EQ(controller.GetPendingEntryIndex(), 0);
1211 EXPECT_TRUE(controller.GetLastCommittedEntry()); 1193 EXPECT_TRUE(controller.GetLastCommittedEntry());
1212 EXPECT_TRUE(controller.GetPendingEntry()); 1194 EXPECT_TRUE(controller.GetPendingEntry());
1213 EXPECT_FALSE(controller.CanGoBack()); 1195 EXPECT_FALSE(controller.CanGoBack());
1214 EXPECT_FALSE(controller.CanGoForward()); 1196 EXPECT_FALSE(controller.CanGoForward());
1215 1197
1216 // Make sure the title has been cleared (will be redrawn just after reload). 1198 // Make sure the title has been cleared (will be redrawn just after reload).
1217 // Avoids a stale cached title when the new page being reloaded has no title. 1199 // Avoids a stale cached title when the new page being reloaded has no title.
(...skipping 2570 matching lines...) Expand 10 before | Expand all | Expand 10 after
3788 // cleared. 3770 // cleared.
3789 EXPECT_EQ(1, controller.GetEntryCount()); 3771 EXPECT_EQ(1, controller.GetEntryCount());
3790 EXPECT_EQ(0, controller.GetCurrentEntryIndex()); 3772 EXPECT_EQ(0, controller.GetCurrentEntryIndex());
3791 EXPECT_EQ(0, controller.GetLastCommittedEntryIndex()); 3773 EXPECT_EQ(0, controller.GetLastCommittedEntryIndex());
3792 EXPECT_EQ(-1, controller.GetPendingEntryIndex()); 3774 EXPECT_EQ(-1, controller.GetPendingEntryIndex());
3793 EXPECT_FALSE(controller.CanGoBack()); 3775 EXPECT_FALSE(controller.CanGoBack());
3794 EXPECT_FALSE(controller.CanGoForward()); 3776 EXPECT_FALSE(controller.CanGoForward());
3795 EXPECT_EQ(url4, controller.GetVisibleEntry()->GetURL()); 3777 EXPECT_EQ(url4, controller.GetVisibleEntry()->GetURL());
3796 } 3778 }
3797 3779
3798 /* TODO(brettw) These test pass on my local machine but fail on the XP buildbot
3799 (but not Vista) cleaning up the directory after they run.
3800 This should be fixed.
3801
3802 // NavigationControllerHistoryTest ---------------------------------------------
3803
3804 class NavigationControllerHistoryTest : public NavigationControllerTest {
3805 public:
3806 NavigationControllerHistoryTest()
3807 : url0("http://foo1"),
3808 url1("http://foo1"),
3809 url2("http://foo1"),
3810 profile_manager_(NULL) {
3811 }
3812
3813 virtual ~NavigationControllerHistoryTest() {
3814 // Prevent our base class from deleting the profile since profile's
3815 // lifetime is managed by profile_manager_.
3816 STLDeleteElements(&windows_);
3817 }
3818
3819 // testing::Test overrides.
3820 virtual void SetUp() {
3821 NavigationControllerTest::SetUp();
3822
3823 // Force the session service to be created.
3824 SessionService* service = new SessionService(profile());
3825 SessionServiceFactory::SetForTestProfile(profile(), service);
3826 service->SetWindowType(window_id, Browser::TYPE_TABBED);
3827 service->SetWindowBounds(window_id, gfx::Rect(0, 1, 2, 3), false);
3828 service->SetTabIndexInWindow(window_id,
3829 controller.session_id(), 0);
3830 controller.SetWindowID(window_id);
3831
3832 session_helper_.SetService(service);
3833 }
3834
3835 virtual void TearDown() {
3836 // Release profile's reference to the session service. Otherwise the file
3837 // will still be open and we won't be able to delete the directory below.
3838 session_helper_.ReleaseService(); // profile owns this
3839 SessionServiceFactory::SetForTestProfile(profile(), NULL);
3840
3841 // Make sure we wait for history to shut down before continuing. The task
3842 // we add will cause our message loop to quit once it is destroyed.
3843 HistoryService* history = HistoryServiceFactory::GetForProfiles(
3844 profile(), Profile::IMPLICIT_ACCESS);
3845 if (history) {
3846 history->SetOnBackendDestroyTask(base::MessageLoop::QuitClosure());
3847 base::MessageLoop::current()->Run();
3848 }
3849
3850 // Do normal cleanup before deleting the profile directory below.
3851 NavigationControllerTest::TearDown();
3852
3853 ASSERT_TRUE(base::DeleteFile(test_dir_, true));
3854 ASSERT_FALSE(base::PathExists(test_dir_));
3855 }
3856
3857 // Deletes the current profile manager and creates a new one. Indirectly this
3858 // shuts down the history database and reopens it.
3859 void ReopenDatabase() {
3860 session_helper_.SetService(NULL);
3861 SessionServiceFactory::SetForTestProfile(profile(), NULL);
3862
3863 SessionService* service = new SessionService(profile());
3864 SessionServiceFactory::SetForTestProfile(profile(), service);
3865 session_helper_.SetService(service);
3866 }
3867
3868 void GetLastSession() {
3869 SessionServiceFactory::GetForProfile(profile())->TabClosed(
3870 controller.window_id(), controller.session_id(), false);
3871
3872 ReopenDatabase();
3873 Time close_time;
3874
3875 session_helper_.ReadWindows(&windows_);
3876 }
3877
3878 CancelableRequestConsumer consumer;
3879
3880 // URLs for testing.
3881 const GURL url0;
3882 const GURL url1;
3883 const GURL url2;
3884
3885 std::vector<SessionWindow*> windows_;
3886
3887 SessionID window_id;
3888
3889 SessionServiceTestHelper session_helper_;
3890
3891 private:
3892 ProfileManager* profile_manager_;
3893 base::FilePath test_dir_;
3894 };
3895
3896 // A basic test case. Navigates to a single url, and make sure the history
3897 // db matches.
3898 TEST_F(NavigationControllerHistoryTest, Basic) {
3899 NavigationControllerImpl& controller = controller_impl();
3900 controller.LoadURL(url0, GURL(), PAGE_TRANSITION_LINK);
3901 test_rvh()->SendNavigate(0, url0);
3902
3903 GetLastSession();
3904
3905 session_helper_.AssertSingleWindowWithSingleTab(windows_, 1);
3906 session_helper_.AssertTabEquals(0, 0, 1, *(windows_[0]->tabs[0]));
3907 TabNavigation nav1(0, url0, GURL(), string16(),
3908 webkit_glue::CreateHistoryStateForURL(url0),
3909 PAGE_TRANSITION_LINK);
3910 session_helper_.AssertNavigationEquals(nav1,
3911 windows_[0]->tabs[0]->navigations[0]);
3912 }
3913
3914 // Navigates to three urls, then goes back and make sure the history database
3915 // is in sync.
3916 TEST_F(NavigationControllerHistoryTest, NavigationThenBack) {
3917 NavigationControllerImpl& controller = controller_impl();
3918 test_rvh()->SendNavigate(0, url0);
3919 test_rvh()->SendNavigate(1, url1);
3920 test_rvh()->SendNavigate(2, url2);
3921
3922 controller.GoBack();
3923 test_rvh()->SendNavigate(1, url1);
3924
3925 GetLastSession();
3926
3927 session_helper_.AssertSingleWindowWithSingleTab(windows_, 3);
3928 session_helper_.AssertTabEquals(0, 1, 3, *(windows_[0]->tabs[0]));
3929
3930 TabNavigation nav(0, url0, GURL(), string16(),
3931 webkit_glue::CreateHistoryStateForURL(url0),
3932 PAGE_TRANSITION_LINK);
3933 session_helper_.AssertNavigationEquals(nav,
3934 windows_[0]->tabs[0]->navigations[0]);
3935 nav.set_url(url1);
3936 session_helper_.AssertNavigationEquals(nav,
3937 windows_[0]->tabs[0]->navigations[1]);
3938 nav.set_url(url2);
3939 session_helper_.AssertNavigationEquals(nav,
3940 windows_[0]->tabs[0]->navigations[2]);
3941 }
3942
3943 // Navigates to three urls, then goes back twice, then loads a new url.
3944 TEST_F(NavigationControllerHistoryTest, NavigationPruning) {
3945 NavigationControllerImpl& controller = controller_impl();
3946 test_rvh()->SendNavigate(0, url0);
3947 test_rvh()->SendNavigate(1, url1);
3948 test_rvh()->SendNavigate(2, url2);
3949
3950 controller.GoBack();
3951 test_rvh()->SendNavigate(1, url1);
3952
3953 controller.GoBack();
3954 test_rvh()->SendNavigate(0, url0);
3955
3956 test_rvh()->SendNavigate(3, url2);
3957
3958 // Now have url0, and url2.
3959
3960 GetLastSession();
3961
3962 session_helper_.AssertSingleWindowWithSingleTab(windows_, 2);
3963 session_helper_.AssertTabEquals(0, 1, 2, *(windows_[0]->tabs[0]));
3964
3965 TabNavigation nav(0, url0, GURL(), string16(),
3966 webkit_glue::CreateHistoryStateForURL(url0),
3967 PAGE_TRANSITION_LINK);
3968 session_helper_.AssertNavigationEquals(nav,
3969 windows_[0]->tabs[0]->navigations[0]);
3970 nav.set_url(url2);
3971 session_helper_.AssertNavigationEquals(nav,
3972 windows_[0]->tabs[0]->navigations[1]);
3973 }
3974 */
3975
3976 } // namespace content 3780 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/web_contents/navigation_controller_impl.cc ('k') | content/browser/web_contents/web_contents_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698