This source file includes following definitions.
- CreateImage
- DoImagesMatch
- encoding_screenshot_in_progress_
- TakeScreenshotFor
- GetScreenshotCount
- WaitUntilScreenshotIsReady
- TakeScreenshotImpl
- OnScreenshotSet
- TEST
- TEST
- TEST
- TEST
- SetUp
- DidStartNavigationToPendingEntry
- NavigationEntryCommitted
- navigated_url
- controller_impl
- RegisterForAllNavNotifications
- GetSiteInstanceFromEntry
- navigation_state_change_count
- NavigationStateChanged
- TEST_F
- TEST_F
- TEST_F
- GetFixedTime
- TEST_F
- CheckNavigationEntryMatchLoadParams
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- Observe
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/file_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/path_service.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "content/browser/frame_host/cross_site_transferring_request.h"
#include "content/browser/frame_host/navigation_controller_impl.h"
#include "content/browser/frame_host/navigation_entry_impl.h"
#include "content/browser/frame_host/navigation_entry_screenshot_manager.h"
#include "content/browser/frame_host/navigator.h"
#include "content/browser/site_instance_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/frame_messages.h"
#include "content/common/view_messages.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/page_state.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_notification_tracker.h"
#include "content/public/test/test_utils.h"
#include "content/test/test_render_view_host.h"
#include "content/test/test_web_contents.h"
#include "net/base/net_util.h"
#include "skia/ext/platform_canvas.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::Time;
namespace {
gfx::Image CreateImage(SkColor color) {
SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
bitmap.allocPixels();
bitmap.eraseColor(color);
return gfx::Image::CreateFrom1xBitmap(bitmap);
}
bool DoImagesMatch(const gfx::Image& a, const gfx::Image& b) {
SkBitmap a_bitmap = a.AsBitmap();
SkBitmap b_bitmap = b.AsBitmap();
if (a_bitmap.width() != b_bitmap.width() ||
a_bitmap.height() != b_bitmap.height()) {
return false;
}
SkAutoLockPixels a_bitmap_lock(a_bitmap);
SkAutoLockPixels b_bitmap_lock(b_bitmap);
return memcmp(a_bitmap.getPixels(),
b_bitmap.getPixels(),
a_bitmap.getSize()) == 0;
}
class MockScreenshotManager : public content::NavigationEntryScreenshotManager {
public:
explicit MockScreenshotManager(content::NavigationControllerImpl* owner)
: content::NavigationEntryScreenshotManager(owner),
encoding_screenshot_in_progress_(false) {
}
virtual ~MockScreenshotManager() {
}
void TakeScreenshotFor(content::NavigationEntryImpl* entry) {
SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
bitmap.allocPixels();
bitmap.eraseRGB(0, 0, 0);
encoding_screenshot_in_progress_ = true;
OnScreenshotTaken(entry->GetUniqueID(), true, bitmap);
WaitUntilScreenshotIsReady();
}
int GetScreenshotCount() {
return content::NavigationEntryScreenshotManager::GetScreenshotCount();
}
void WaitUntilScreenshotIsReady() {
if (!encoding_screenshot_in_progress_)
return;
message_loop_runner_ = new content::MessageLoopRunner;
message_loop_runner_->Run();
}
private:
virtual void TakeScreenshotImpl(
content::RenderViewHost* host,
content::NavigationEntryImpl* entry) OVERRIDE {
}
virtual void OnScreenshotSet(content::NavigationEntryImpl* entry) OVERRIDE {
encoding_screenshot_in_progress_ = false;
NavigationEntryScreenshotManager::OnScreenshotSet(entry);
if (message_loop_runner_.get())
message_loop_runner_->Quit();
}
scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
bool encoding_screenshot_in_progress_;
DISALLOW_COPY_AND_ASSIGN(MockScreenshotManager);
};
}
namespace content {
TEST(TimeSmoother, Basic) {
NavigationControllerImpl::TimeSmoother smoother;
for (int64 i = 1; i < 1000; ++i) {
base::Time t = base::Time::FromInternalValue(i);
EXPECT_EQ(t, smoother.GetSmoothedTime(t));
}
}
TEST(TimeSmoother, SingleDuplicate) {
NavigationControllerImpl::TimeSmoother smoother;
base::Time t = base::Time::FromInternalValue(1);
EXPECT_EQ(t, smoother.GetSmoothedTime(t));
for (int64 i = 1; i < 1000; ++i) {
base::Time expected_t = base::Time::FromInternalValue(i + 1);
t = base::Time::FromInternalValue(i);
EXPECT_EQ(expected_t, smoother.GetSmoothedTime(t));
}
}
TEST(TimeSmoother, ManyDuplicates) {
const int64 kNumDuplicates = 100;
NavigationControllerImpl::TimeSmoother smoother;
base::Time t = base::Time::FromInternalValue(1);
for (int64 i = 0; i < kNumDuplicates; ++i) {
base::Time expected_t = base::Time::FromInternalValue(i + 1);
EXPECT_EQ(expected_t, smoother.GetSmoothedTime(t));
}
for (int64 i = 1; i < 1000; ++i) {
base::Time expected_t =
base::Time::FromInternalValue(i + kNumDuplicates);
t = base::Time::FromInternalValue(i);
EXPECT_EQ(expected_t, smoother.GetSmoothedTime(t));
}
}
TEST(TimeSmoother, ClockBackwardsJump) {
const int64 kNumDuplicates = 100;
NavigationControllerImpl::TimeSmoother smoother;
base::Time t = base::Time::FromInternalValue(1000);
for (int64 i = 0; i < kNumDuplicates; ++i) {
base::Time expected_t = base::Time::FromInternalValue(i + 1000);
EXPECT_EQ(expected_t, smoother.GetSmoothedTime(t));
}
t = base::Time::FromInternalValue(500);
EXPECT_EQ(t, smoother.GetSmoothedTime(t));
}
class NavigationControllerTest
: public RenderViewHostImplTestHarness,
public WebContentsObserver {
public:
NavigationControllerTest() : navigation_entry_committed_counter_(0) {
}
virtual void SetUp() OVERRIDE {
RenderViewHostImplTestHarness::SetUp();
WebContents* web_contents = RenderViewHostImplTestHarness::web_contents();
ASSERT_TRUE(web_contents);
WebContentsObserver::Observe(web_contents);
}
virtual void DidStartNavigationToPendingEntry(
const GURL& url,
NavigationController::ReloadType reload_type) OVERRIDE {
navigated_url_ = url;
}
virtual void NavigationEntryCommitted(
const LoadCommittedDetails& load_details) OVERRIDE {
navigation_entry_committed_counter_++;
}
const GURL& navigated_url() const {
return navigated_url_;
}
NavigationControllerImpl& controller_impl() {
return static_cast<NavigationControllerImpl&>(controller());
}
protected:
GURL navigated_url_;
size_t navigation_entry_committed_counter_;
};
void RegisterForAllNavNotifications(TestNotificationTracker* tracker,
NavigationController* controller) {
tracker->ListenFor(NOTIFICATION_NAV_LIST_PRUNED,
Source<NavigationController>(controller));
tracker->ListenFor(NOTIFICATION_NAV_ENTRY_CHANGED,
Source<NavigationController>(controller));
}
SiteInstance* GetSiteInstanceFromEntry(NavigationEntry* entry) {
return NavigationEntryImpl::FromNavigationEntry(entry)->site_instance();
}
class TestWebContentsDelegate : public WebContentsDelegate {
public:
explicit TestWebContentsDelegate() :
navigation_state_change_count_(0) {}
int navigation_state_change_count() {
return navigation_state_change_count_;
}
virtual void NavigationStateChanged(const WebContents* source,
unsigned changed_flags) OVERRIDE {
navigation_state_change_count_++;
}
private:
int navigation_state_change_count_;
};
TEST_F(NavigationControllerTest, Defaults) {
NavigationControllerImpl& controller = controller_impl();
EXPECT_FALSE(controller.GetPendingEntry());
EXPECT_FALSE(controller.GetVisibleEntry());
EXPECT_FALSE(controller.GetLastCommittedEntry());
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), -1);
EXPECT_EQ(controller.GetEntryCount(), 0);
EXPECT_FALSE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
}
TEST_F(NavigationControllerTest, GoToOffset) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const int kNumUrls = 5;
std::vector<GURL> urls(kNumUrls);
for (int i = 0; i < kNumUrls; ++i) {
urls[i] = GURL(base::StringPrintf("http://www.a.com/%d", i));
}
main_test_rfh()->SendNavigate(0, urls[0]);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(urls[0], controller.GetVisibleEntry()->GetVirtualURL());
EXPECT_FALSE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
EXPECT_FALSE(controller.CanGoToOffset(1));
for (int i = 1; i <= 4; ++i) {
main_test_rfh()->SendNavigate(i, urls[i]);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(urls[i], controller.GetVisibleEntry()->GetVirtualURL());
EXPECT_TRUE(controller.CanGoToOffset(-i));
EXPECT_FALSE(controller.CanGoToOffset(-(i + 1)));
EXPECT_FALSE(controller.CanGoToOffset(1));
}
int url_index = 4;
enum Tests {
GO_TO_MIDDLE_PAGE = -2,
GO_FORWARDS = 1,
GO_BACKWARDS = -1,
GO_TO_BEGINNING = -2,
GO_TO_END = 4,
NUM_TESTS = 5,
};
const int test_offsets[NUM_TESTS] = {
GO_TO_MIDDLE_PAGE,
GO_FORWARDS,
GO_BACKWARDS,
GO_TO_BEGINNING,
GO_TO_END
};
for (int test = 0; test < NUM_TESTS; ++test) {
int offset = test_offsets[test];
controller.GoToOffset(offset);
url_index += offset;
EXPECT_EQ(urls[url_index], controller.GetPendingEntry()->GetVirtualURL());
main_test_rfh()->SendNavigate(url_index, urls[url_index]);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
for (size_t j = 0; j < urls.size(); ++j)
EXPECT_TRUE(controller.CanGoToOffset(j - url_index));
EXPECT_FALSE(controller.CanGoToOffset(-(url_index + 1)));
EXPECT_FALSE(controller.CanGoToOffset(urls.size() - url_index));
}
}
TEST_F(NavigationControllerTest, LoadURL) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url1("http://foo1");
const GURL url2("http://foo2");
controller.LoadURL(url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
EXPECT_EQ(0U, notifications.size());
EXPECT_EQ(controller.GetEntryCount(), 0);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), -1);
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_FALSE(controller.GetLastCommittedEntry());
ASSERT_TRUE(controller.GetPendingEntry());
EXPECT_EQ(controller.GetPendingEntry(), controller.GetVisibleEntry());
EXPECT_FALSE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
EXPECT_EQ(contents()->GetMaxPageID(), -1);
EXPECT_TRUE(controller.GetPendingEntry()->GetTimestamp().is_null());
EXPECT_EQ(0, controller.GetPendingEntry()->GetHttpStatusCode());
EXPECT_EQ(0U, notifications.size());
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(controller.GetEntryCount(), 1);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 0);
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_FALSE(controller.GetPendingEntry());
ASSERT_TRUE(controller.GetVisibleEntry());
EXPECT_FALSE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
EXPECT_EQ(contents()->GetMaxPageID(), 0);
EXPECT_EQ(0, NavigationEntryImpl::FromNavigationEntry(
controller.GetLastCommittedEntry())->bindings());
EXPECT_FALSE(controller.GetVisibleEntry()->GetTimestamp().is_null());
controller.LoadURL(url2, Referrer(), PAGE_TRANSITION_TYPED, std::string());
EXPECT_EQ(controller.GetEntryCount(), 1);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 0);
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_TRUE(controller.GetLastCommittedEntry());
ASSERT_TRUE(controller.GetPendingEntry());
EXPECT_EQ(controller.GetPendingEntry(), controller.GetVisibleEntry());
EXPECT_FALSE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
EXPECT_EQ(contents()->GetMaxPageID(), 0);
EXPECT_TRUE(controller.GetPendingEntry()->GetTimestamp().is_null());
test_rvh()->SendBeforeUnloadACK(true);
static_cast<TestRenderViewHost*>(
contents()->GetPendingRenderViewHost())->SendNavigate(1, url2);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(controller.GetEntryCount(), 2);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 1);
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_FALSE(controller.GetPendingEntry());
ASSERT_TRUE(controller.GetVisibleEntry());
EXPECT_TRUE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
EXPECT_EQ(contents()->GetMaxPageID(), 1);
EXPECT_FALSE(controller.GetVisibleEntry()->GetTimestamp().is_null());
}
namespace {
base::Time GetFixedTime(base::Time time) {
return time;
}
}
TEST_F(NavigationControllerTest, LoadURLSameTime) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
controller.SetGetTimestampCallbackForTest(
base::Bind(&GetFixedTime, base::Time::FromInternalValue(1)));
const GURL url1("http://foo1");
const GURL url2("http://foo2");
controller.LoadURL(url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
controller.LoadURL(url2, Referrer(), PAGE_TRANSITION_TYPED, std::string());
test_rvh()->SendBeforeUnloadACK(true);
main_test_rfh()->SendNavigate(1, url2);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
ASSERT_EQ(controller.GetEntryCount(), 2);
EXPECT_EQ(1u,
controller.GetEntryAtIndex(0)->GetTimestamp().ToInternalValue());
EXPECT_EQ(2u,
controller.GetEntryAtIndex(1)->GetTimestamp().ToInternalValue());
}
void CheckNavigationEntryMatchLoadParams(
NavigationController::LoadURLParams& load_params,
NavigationEntryImpl* entry) {
EXPECT_EQ(load_params.url, entry->GetURL());
EXPECT_EQ(load_params.referrer.url, entry->GetReferrer().url);
EXPECT_EQ(load_params.referrer.policy, entry->GetReferrer().policy);
EXPECT_EQ(load_params.transition_type, entry->GetTransitionType());
EXPECT_EQ(load_params.extra_headers, entry->extra_headers());
EXPECT_EQ(load_params.is_renderer_initiated, entry->is_renderer_initiated());
EXPECT_EQ(load_params.base_url_for_data_url, entry->GetBaseURLForDataURL());
if (!load_params.virtual_url_for_data_url.is_empty()) {
EXPECT_EQ(load_params.virtual_url_for_data_url, entry->GetVirtualURL());
}
if (NavigationController::UA_OVERRIDE_INHERIT !=
load_params.override_user_agent) {
bool should_override = (NavigationController::UA_OVERRIDE_TRUE ==
load_params.override_user_agent);
EXPECT_EQ(should_override, entry->GetIsOverridingUserAgent());
}
EXPECT_EQ(load_params.browser_initiated_post_data,
entry->GetBrowserInitiatedPostData());
EXPECT_EQ(load_params.transferred_global_request_id,
entry->transferred_global_request_id());
}
TEST_F(NavigationControllerTest, LoadURLWithParams) {
NavigationControllerImpl& controller = controller_impl();
NavigationController::LoadURLParams load_params(GURL("http://foo"));
load_params.referrer =
Referrer(GURL("http://referrer"), blink::WebReferrerPolicyDefault);
load_params.transition_type = PAGE_TRANSITION_GENERATED;
load_params.extra_headers = "content-type: text/plain";
load_params.load_type = NavigationController::LOAD_TYPE_DEFAULT;
load_params.is_renderer_initiated = true;
load_params.override_user_agent = NavigationController::UA_OVERRIDE_TRUE;
load_params.transferred_global_request_id = GlobalRequestID(2, 3);
controller.LoadURLWithParams(load_params);
NavigationEntryImpl* entry =
NavigationEntryImpl::FromNavigationEntry(
controller.GetPendingEntry());
ASSERT_TRUE(entry);
EXPECT_TRUE(entry->GetTimestamp().is_null());
CheckNavigationEntryMatchLoadParams(load_params, entry);
}
TEST_F(NavigationControllerTest, LoadURLWithExtraParams_Data) {
NavigationControllerImpl& controller = controller_impl();
NavigationController::LoadURLParams load_params(
GURL("data:text/html,dataurl"));
load_params.load_type = NavigationController::LOAD_TYPE_DATA;
load_params.base_url_for_data_url = GURL("http://foo");
load_params.virtual_url_for_data_url = GURL(kAboutBlankURL);
load_params.override_user_agent = NavigationController::UA_OVERRIDE_FALSE;
controller.LoadURLWithParams(load_params);
NavigationEntryImpl* entry =
NavigationEntryImpl::FromNavigationEntry(
controller.GetPendingEntry());
CheckNavigationEntryMatchLoadParams(load_params, entry);
}
TEST_F(NavigationControllerTest, LoadURLWithExtraParams_HttpPost) {
NavigationControllerImpl& controller = controller_impl();
NavigationController::LoadURLParams load_params(GURL("https://posturl"));
load_params.transition_type = PAGE_TRANSITION_TYPED;
load_params.load_type =
NavigationController::LOAD_TYPE_BROWSER_INITIATED_HTTP_POST;
load_params.override_user_agent = NavigationController::UA_OVERRIDE_TRUE;
const unsigned char* raw_data =
reinterpret_cast<const unsigned char*>("d\n\0a2");
const int length = 5;
std::vector<unsigned char> post_data_vector(raw_data, raw_data+length);
scoped_refptr<base::RefCountedBytes> data =
base::RefCountedBytes::TakeVector(&post_data_vector);
load_params.browser_initiated_post_data = data.get();
controller.LoadURLWithParams(load_params);
NavigationEntryImpl* entry =
NavigationEntryImpl::FromNavigationEntry(
controller.GetPendingEntry());
CheckNavigationEntryMatchLoadParams(load_params, entry);
}
TEST_F(NavigationControllerTest, LoadURL_SamePage) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url1("http://foo1");
controller.LoadURL(url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
EXPECT_EQ(0U, notifications.size());
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
ASSERT_TRUE(controller.GetVisibleEntry());
const base::Time timestamp = controller.GetVisibleEntry()->GetTimestamp();
EXPECT_FALSE(timestamp.is_null());
controller.LoadURL(url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
EXPECT_EQ(0U, notifications.size());
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(controller.GetEntryCount(), 1);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 0);
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_FALSE(controller.GetPendingEntry());
ASSERT_TRUE(controller.GetVisibleEntry());
EXPECT_FALSE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
EXPECT_GE(controller.GetVisibleEntry()->GetTimestamp(), timestamp);
}
TEST_F(NavigationControllerTest, LoadURL_SamePage_DifferentMethod) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url1("http://foo1");
controller.LoadURL(url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
FrameHostMsg_DidCommitProvisionalLoad_Params params;
params.page_id = 0;
params.url = url1;
params.transition = PAGE_TRANSITION_TYPED;
params.is_post = true;
params.post_id = 123;
params.page_state = PageState::CreateForTesting(url1, false, 0, 0);
main_test_rfh()->SendNavigateWithParams(¶ms);
NavigationEntry* entry = controller.GetVisibleEntry();
ASSERT_TRUE(entry);
EXPECT_TRUE(entry->GetHasPostData());
EXPECT_EQ(entry->GetPostID(), 123);
controller.LoadURL(url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(0, url1);
ASSERT_EQ(controller.GetVisibleEntry(), entry);
EXPECT_FALSE(entry->GetHasPostData());
EXPECT_EQ(entry->GetPostID(), 0);
}
TEST_F(NavigationControllerTest, LoadURL_Discarded) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url1("http://foo1");
const GURL url2("http://foo2");
controller.LoadURL(url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
EXPECT_EQ(0U, notifications.size());
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
ASSERT_TRUE(controller.GetVisibleEntry());
const base::Time timestamp = controller.GetVisibleEntry()->GetTimestamp();
EXPECT_FALSE(timestamp.is_null());
controller.LoadURL(url2, Referrer(), PAGE_TRANSITION_TYPED, std::string());
controller.DiscardNonCommittedEntries();
EXPECT_EQ(0U, notifications.size());
EXPECT_EQ(controller.GetEntryCount(), 1);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 0);
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_FALSE(controller.GetPendingEntry());
ASSERT_TRUE(controller.GetVisibleEntry());
EXPECT_FALSE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
EXPECT_EQ(timestamp, controller.GetVisibleEntry()->GetTimestamp());
}
TEST_F(NavigationControllerTest, LoadURL_NoPending) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL kExistingURL1("http://eh");
controller.LoadURL(
kExistingURL1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(0, kExistingURL1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
const GURL kNewURL("http://see");
main_test_rfh()->SendNavigate(99, kNewURL);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_EQ(1, controller.GetLastCommittedEntryIndex());
EXPECT_EQ(kNewURL, controller.GetVisibleEntry()->GetURL());
}
TEST_F(NavigationControllerTest, LoadURL_NewPending) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL kExistingURL1("http://eh");
controller.LoadURL(
kExistingURL1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(0, kExistingURL1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
const GURL kExistingURL2("http://bee");
controller.LoadURL(
kExistingURL2, Referrer(), PAGE_TRANSITION_TYPED, std::string());
EXPECT_EQ(0U, notifications.size());
test_rvh()->SendBeforeUnloadACK(true);
const GURL kNewURL("http://see");
static_cast<TestRenderViewHost*>(
contents()->GetPendingRenderViewHost())->SendNavigate(3, kNewURL);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_EQ(1, controller.GetLastCommittedEntryIndex());
EXPECT_EQ(kNewURL, controller.GetVisibleEntry()->GetURL());
}
TEST_F(NavigationControllerTest, LoadURL_ExistingPending) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL kExistingURL1("http://foo/eh");
controller.LoadURL(
kExistingURL1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(0, kExistingURL1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
const GURL kExistingURL2("http://foo/bee");
controller.LoadURL(
kExistingURL2, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(1, kExistingURL2);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
controller.GoBack();
EXPECT_EQ(0U, notifications.size());
EXPECT_EQ(0, controller.GetPendingEntryIndex());
EXPECT_EQ(1, controller.GetLastCommittedEntryIndex());
const GURL kNewURL("http://foo/see");
LoadCommittedDetails details;
main_test_rfh()->SendNavigate(3, kNewURL);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_EQ(2, controller.GetLastCommittedEntryIndex());
EXPECT_EQ(kNewURL, controller.GetVisibleEntry()->GetURL());
}
TEST_F(NavigationControllerTest, LoadURL_PrivilegedPending) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL kExistingURL1("http://privileged");
controller.LoadURL(
kExistingURL1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
test_rvh()->AllowBindings(2);
main_test_rfh()->SendNavigate(0, kExistingURL1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
const GURL kExistingURL2("http://foo/eh");
controller.LoadURL(
kExistingURL2, Referrer(), PAGE_TRANSITION_TYPED, std::string());
test_rvh()->SendBeforeUnloadACK(true);
TestRenderViewHost* foo_rvh = static_cast<TestRenderViewHost*>(
contents()->GetPendingRenderViewHost());
foo_rvh->SendNavigate(1, kExistingURL2);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
controller.GoBack();
foo_rvh->SendBeforeUnloadACK(true);
EXPECT_EQ(0U, notifications.size());
EXPECT_EQ(0, controller.GetPendingEntryIndex());
EXPECT_EQ(1, controller.GetLastCommittedEntryIndex());
EXPECT_EQ(2, NavigationEntryImpl::FromNavigationEntry(
controller.GetPendingEntry())->bindings());
const GURL kNewURL("http://foo/bee");
LoadCommittedDetails details;
foo_rvh->SendNavigate(3, kNewURL);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_EQ(2, controller.GetLastCommittedEntryIndex());
EXPECT_EQ(kNewURL, controller.GetVisibleEntry()->GetURL());
EXPECT_EQ(0, NavigationEntryImpl::FromNavigationEntry(
controller.GetLastCommittedEntry())->bindings());
}
TEST_F(NavigationControllerTest, LoadURL_BackPreemptsPending) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL kExistingURL1("http://foo/eh");
controller.LoadURL(
kExistingURL1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(0, kExistingURL1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
const GURL kExistingURL2("http://foo/bee");
controller.LoadURL(
kExistingURL2, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(1, kExistingURL2);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
const GURL kNewURL("http://foo/see");
controller.LoadURL(
kNewURL, Referrer(), PAGE_TRANSITION_TYPED, std::string());
EXPECT_EQ(0U, notifications.size());
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_EQ(1, controller.GetLastCommittedEntryIndex());
main_test_rfh()->SendNavigate(0, kExistingURL1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_EQ(0, controller.GetLastCommittedEntryIndex());
EXPECT_EQ(kExistingURL1, controller.GetVisibleEntry()->GetURL());
}
TEST_F(NavigationControllerTest, LoadURL_IgnorePreemptsPending) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
scoped_ptr<TestWebContentsDelegate> delegate(new TestWebContentsDelegate());
EXPECT_FALSE(contents()->GetDelegate());
contents()->SetDelegate(delegate.get());
const GURL kExistingURL(kAboutBlankURL);
const GURL kNewURL("http://eh");
controller.LoadURL(
kNewURL, Referrer(), PAGE_TRANSITION_TYPED, std::string());
EXPECT_EQ(0U, notifications.size());
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_TRUE(controller.GetPendingEntry());
EXPECT_EQ(-1, controller.GetLastCommittedEntryIndex());
EXPECT_EQ(1, delegate->navigation_state_change_count());
main_test_rfh()->SendNavigate(-1, kExistingURL);
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_FALSE(controller.GetPendingEntry());
EXPECT_EQ(-1, controller.GetLastCommittedEntryIndex());
EXPECT_EQ(2, delegate->navigation_state_change_count());
contents()->SetDelegate(NULL);
}
TEST_F(NavigationControllerTest, LoadURL_AbortDoesntCancelPending) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
scoped_ptr<TestWebContentsDelegate> delegate(new TestWebContentsDelegate());
EXPECT_FALSE(contents()->GetDelegate());
contents()->SetDelegate(delegate.get());
const GURL kNewURL("http://eh");
controller.LoadURL(
kNewURL, Referrer(), PAGE_TRANSITION_TYPED, std::string());
EXPECT_EQ(0U, notifications.size());
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_TRUE(controller.GetPendingEntry());
EXPECT_EQ(-1, controller.GetLastCommittedEntryIndex());
EXPECT_EQ(1, delegate->navigation_state_change_count());
FrameHostMsg_DidFailProvisionalLoadWithError_Params params;
params.error_code = net::ERR_ABORTED;
params.error_description = base::string16();
params.url = kNewURL;
params.showing_repost_interstitial = false;
main_test_rfh()->OnMessageReceived(
FrameHostMsg_DidFailProvisionalLoadWithError(0,
params));
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_TRUE(controller.GetPendingEntry());
EXPECT_EQ(-1, controller.GetLastCommittedEntryIndex());
EXPECT_EQ(1, delegate->navigation_state_change_count());
NavigationEntry* pending_entry = controller.GetPendingEntry();
controller.Reload(true);
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_TRUE(controller.GetPendingEntry());
EXPECT_EQ(pending_entry, controller.GetPendingEntry());
EXPECT_EQ(-1, controller.GetLastCommittedEntryIndex());
contents()->SetDelegate(NULL);
}
TEST_F(NavigationControllerTest, LoadURL_RedirectAbortDoesntShowPendingURL) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL kExistingURL("http://foo/eh");
controller.LoadURL(kExistingURL, content::Referrer(),
content::PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(0, kExistingURL);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
scoped_ptr<TestWebContentsDelegate> delegate(new TestWebContentsDelegate());
EXPECT_FALSE(contents()->GetDelegate());
contents()->SetDelegate(delegate.get());
const GURL kNewURL("http://foo/bee");
NavigationController::LoadURLParams load_url_params(kNewURL);
load_url_params.transition_type = PAGE_TRANSITION_TYPED;
load_url_params.is_renderer_initiated = true;
controller.LoadURLWithParams(load_url_params);
EXPECT_EQ(0U, notifications.size());
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_TRUE(controller.GetPendingEntry());
EXPECT_EQ(0, controller.GetLastCommittedEntryIndex());
EXPECT_EQ(0, delegate->navigation_state_change_count());
EXPECT_EQ(kExistingURL, controller.GetVisibleEntry()->GetURL());
const GURL kRedirectURL("http://foo/see");
main_test_rfh()->OnMessageReceived(
FrameHostMsg_DidRedirectProvisionalLoad(0,
-1,
kNewURL,
kRedirectURL));
EXPECT_EQ(kNewURL, controller.GetPendingEntry()->GetURL());
FrameHostMsg_DidFailProvisionalLoadWithError_Params params;
params.error_code = net::ERR_ABORTED;
params.error_description = base::string16();
params.url = kRedirectURL;
params.showing_repost_interstitial = false;
main_test_rfh()->OnMessageReceived(
FrameHostMsg_DidFailProvisionalLoadWithError(0,
params));
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_FALSE(controller.GetPendingEntry());
EXPECT_EQ(0, controller.GetLastCommittedEntryIndex());
EXPECT_EQ(0, delegate->navigation_state_change_count());
EXPECT_EQ(kExistingURL, controller.GetVisibleEntry()->GetURL());
contents()->SetDelegate(NULL);
}
TEST_F(NavigationControllerTest, LoadURL_WithBindings) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
std::vector<GURL> url_chain;
const GURL url1("http://foo1");
const GURL url2("http://foo2");
controller.LoadURL(url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
EXPECT_EQ(NavigationEntryImpl::kInvalidBindings,
NavigationEntryImpl::FromNavigationEntry(
controller.GetPendingEntry())->bindings());
TestRenderViewHost* orig_rvh = static_cast<TestRenderViewHost*>(test_rvh());
orig_rvh->SendNavigate(0, url1);
EXPECT_EQ(controller.GetEntryCount(), 1);
EXPECT_EQ(0, controller.GetLastCommittedEntryIndex());
EXPECT_EQ(0, NavigationEntryImpl::FromNavigationEntry(
controller.GetLastCommittedEntry())->bindings());
static_cast<SiteInstanceImpl*>(orig_rvh->GetSiteInstance())->
increment_active_view_count();
controller.LoadURL(url2, Referrer(), PAGE_TRANSITION_TYPED, std::string());
orig_rvh->SendBeforeUnloadACK(true);
contents()->GetRenderManagerForTesting()->OnCrossSiteResponse(
contents()->GetRenderManagerForTesting()->pending_frame_host(),
GlobalRequestID(0, 0), scoped_ptr<CrossSiteTransferringRequest>(),
url_chain, Referrer(), PAGE_TRANSITION_TYPED, false);
TestRenderViewHost* new_rvh =
static_cast<TestRenderViewHost*>(contents()->GetPendingRenderViewHost());
new_rvh->AllowBindings(1);
new_rvh->SendNavigate(1, url2);
EXPECT_EQ(controller.GetEntryCount(), 2);
EXPECT_EQ(1, controller.GetLastCommittedEntryIndex());
EXPECT_TRUE(controller.CanGoBack());
EXPECT_EQ(1, NavigationEntryImpl::FromNavigationEntry(
controller.GetLastCommittedEntry())->bindings());
controller.GoBack();
new_rvh->SendBeforeUnloadACK(true);
contents()->GetRenderManagerForTesting()->OnCrossSiteResponse(
contents()->GetRenderManagerForTesting()->pending_frame_host(),
GlobalRequestID(0, 0), scoped_ptr<CrossSiteTransferringRequest>(),
url_chain, Referrer(), PAGE_TRANSITION_TYPED, false);
orig_rvh->SendNavigate(0, url1);
EXPECT_EQ(0, controller.GetLastCommittedEntryIndex());
EXPECT_EQ(0, NavigationEntryImpl::FromNavigationEntry(
controller.GetLastCommittedEntry())->bindings());
}
TEST_F(NavigationControllerTest, Reload) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url1("http://foo1");
controller.LoadURL(url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
EXPECT_EQ(0U, notifications.size());
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
ASSERT_TRUE(controller.GetVisibleEntry());
controller.GetVisibleEntry()->SetTitle(base::ASCIIToUTF16("Title"));
controller.Reload(true);
EXPECT_EQ(0U, notifications.size());
const base::Time timestamp = controller.GetVisibleEntry()->GetTimestamp();
EXPECT_FALSE(timestamp.is_null());
EXPECT_EQ(controller.GetEntryCount(), 1);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 0);
EXPECT_EQ(controller.GetPendingEntryIndex(), 0);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_TRUE(controller.GetPendingEntry());
EXPECT_FALSE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
EXPECT_TRUE(controller.GetVisibleEntry()->GetTitle().empty());
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(controller.GetEntryCount(), 1);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 0);
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_FALSE(controller.GetPendingEntry());
EXPECT_FALSE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
ASSERT_TRUE(controller.GetVisibleEntry());
EXPECT_GE(controller.GetVisibleEntry()->GetTimestamp(), timestamp);
}
TEST_F(NavigationControllerTest, Reload_GeneratesNewPage) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url1("http://foo1");
const GURL url2("http://foo2");
controller.LoadURL(url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
controller.Reload(true);
EXPECT_EQ(0U, notifications.size());
main_test_rfh()->SendNavigate(1, url2);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(controller.GetEntryCount(), 2);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 1);
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_FALSE(controller.GetPendingEntry());
EXPECT_TRUE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
}
TEST_F(NavigationControllerTest, ReloadWithGuest) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo1");
controller.LoadURL(url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(0, url1);
ASSERT_TRUE(controller.GetVisibleEntry());
NavigationEntryImpl* entry1 =
NavigationEntryImpl::FromNavigationEntry(controller.GetVisibleEntry());
reinterpret_cast<MockRenderProcessHost*>(
entry1->site_instance()->GetProcess())->SetIsGuest(true);
controller.Reload(true);
EXPECT_EQ(controller.GetEntryCount(), 1);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 0);
EXPECT_EQ(controller.GetPendingEntryIndex(), 0);
NavigationEntryImpl* entry2 =
NavigationEntryImpl::FromNavigationEntry(controller.GetPendingEntry());
EXPECT_EQ(entry1, entry2);
}
#if !defined(OS_ANDROID)
TEST_F(NavigationControllerTest, ReloadOriginalRequestURL) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL original_url("http://foo1");
const GURL final_url("http://foo2");
controller.LoadURL(
original_url, Referrer(), PAGE_TRANSITION_TYPED, std::string());
EXPECT_EQ(0U, notifications.size());
main_test_rfh()->SendNavigateWithOriginalRequestURL(
0, final_url, original_url);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(
original_url, controller.GetVisibleEntry()->GetOriginalRequestURL());
EXPECT_EQ(final_url, controller.GetVisibleEntry()->GetURL());
controller.GetVisibleEntry()->SetTitle(base::ASCIIToUTF16("Title"));
controller.ReloadOriginalRequestURL(false);
EXPECT_EQ(0U, notifications.size());
EXPECT_EQ(original_url, navigated_url());
EXPECT_EQ(controller.GetEntryCount(), 1);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 0);
EXPECT_EQ(controller.GetPendingEntryIndex(), 0);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_TRUE(controller.GetPendingEntry());
EXPECT_FALSE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
EXPECT_TRUE(controller.GetVisibleEntry()->GetTitle().empty());
main_test_rfh()->SendNavigate(0, final_url);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(controller.GetEntryCount(), 1);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 0);
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_FALSE(controller.GetPendingEntry());
EXPECT_FALSE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
}
#endif
TEST_F(NavigationControllerTest, ResetEntryValuesAfterCommit) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo1");
controller.LoadURL(url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
const unsigned char* raw_data =
reinterpret_cast<const unsigned char*>("post\n\n\0data");
const int length = 11;
std::vector<unsigned char> post_data_vector(raw_data, raw_data+length);
scoped_refptr<base::RefCountedBytes> post_data =
base::RefCountedBytes::TakeVector(&post_data_vector);
GlobalRequestID transfer_id(3, 4);
std::vector<GURL> redirects;
redirects.push_back(GURL("http://foo2"));
NavigationEntryImpl* pending_entry =
NavigationEntryImpl::FromNavigationEntry(controller.GetPendingEntry());
pending_entry->SetBrowserInitiatedPostData(post_data.get());
pending_entry->set_is_renderer_initiated(true);
pending_entry->set_transferred_global_request_id(transfer_id);
pending_entry->set_should_replace_entry(true);
pending_entry->set_redirect_chain(redirects);
pending_entry->set_should_clear_history_list(true);
EXPECT_EQ(post_data.get(), pending_entry->GetBrowserInitiatedPostData());
EXPECT_TRUE(pending_entry->is_renderer_initiated());
EXPECT_EQ(transfer_id, pending_entry->transferred_global_request_id());
EXPECT_TRUE(pending_entry->should_replace_entry());
EXPECT_EQ(1U, pending_entry->redirect_chain().size());
EXPECT_TRUE(pending_entry->should_clear_history_list());
main_test_rfh()->SendNavigate(0, url1);
NavigationEntryImpl* committed_entry =
NavigationEntryImpl::FromNavigationEntry(
controller.GetLastCommittedEntry());
EXPECT_FALSE(committed_entry->GetBrowserInitiatedPostData());
EXPECT_FALSE(committed_entry->is_renderer_initiated());
EXPECT_EQ(GlobalRequestID(-1, -1),
committed_entry->transferred_global_request_id());
EXPECT_FALSE(committed_entry->should_replace_entry());
EXPECT_EQ(0U, committed_entry->redirect_chain().size());
EXPECT_FALSE(committed_entry->should_clear_history_list());
}
TEST_F(NavigationControllerTest, Back) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url1("http://foo1");
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
const GURL url2("http://foo2");
main_test_rfh()->SendNavigate(1, url2);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
controller.GoBack();
EXPECT_EQ(0U, notifications.size());
EXPECT_EQ(controller.GetEntryCount(), 2);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 1);
EXPECT_EQ(controller.GetPendingEntryIndex(), 0);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_TRUE(controller.GetPendingEntry());
EXPECT_FALSE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoToOffset(-1));
EXPECT_TRUE(controller.CanGoForward());
EXPECT_TRUE(controller.CanGoToOffset(1));
EXPECT_FALSE(controller.CanGoToOffset(2));
EXPECT_FALSE(controller.GetEntryAtIndex(0)->GetTimestamp().is_null());
EXPECT_GE(controller.GetEntryAtIndex(1)->GetTimestamp(),
controller.GetEntryAtIndex(0)->GetTimestamp());
main_test_rfh()->SendNavigate(0, url2);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(controller.GetEntryCount(), 2);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 0);
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_FALSE(controller.GetPendingEntry());
EXPECT_FALSE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoToOffset(-1));
EXPECT_TRUE(controller.CanGoForward());
EXPECT_TRUE(controller.CanGoToOffset(1));
EXPECT_FALSE(controller.CanGoToOffset(2));
EXPECT_GE(controller.GetEntryAtIndex(0)->GetTimestamp(),
controller.GetEntryAtIndex(1)->GetTimestamp());
}
TEST_F(NavigationControllerTest, Back_GeneratesNewPage) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url1("http://foo/1");
const GURL url2("http://foo/2");
const GURL url3("http://foo/3");
controller.LoadURL(
url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
controller.LoadURL(url2, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(1, url2);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
controller.GoBack();
EXPECT_EQ(0U, notifications.size());
EXPECT_EQ(controller.GetEntryCount(), 2);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 1);
EXPECT_EQ(controller.GetPendingEntryIndex(), 0);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_TRUE(controller.GetPendingEntry());
EXPECT_FALSE(controller.CanGoBack());
EXPECT_TRUE(controller.CanGoForward());
main_test_rfh()->SendNavigate(2, url3);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(controller.GetEntryCount(), 3);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 2);
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_FALSE(controller.GetPendingEntry());
EXPECT_TRUE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
}
TEST_F(NavigationControllerTest, Back_NewPending) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL kUrl1("http://foo1");
const GURL kUrl2("http://foo2");
const GURL kUrl3("http://foo3");
main_test_rfh()->SendNavigate(0, kUrl1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
main_test_rfh()->SendNavigate(1, kUrl2);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
controller.LoadURL(kUrl3, Referrer(), PAGE_TRANSITION_TYPED, std::string());
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_EQ(kUrl3, controller.GetPendingEntry()->GetURL());
controller.GoBack();
EXPECT_EQ(0, controller.GetPendingEntryIndex());
EXPECT_EQ(kUrl1, controller.GetPendingEntry()->GetURL());
}
TEST_F(NavigationControllerTest, Back_OtherBackPending) {
NavigationControllerImpl& controller = controller_impl();
const GURL kUrl1("http://foo/1");
const GURL kUrl2("http://foo/2");
const GURL kUrl3("http://foo/3");
main_test_rfh()->SendNavigate(0, kUrl1);
main_test_rfh()->SendNavigate(1, kUrl2);
main_test_rfh()->SendNavigate(2, kUrl3);
main_test_rfh()->SendNavigate(1, kUrl2);
SiteInstance* site_instance =
NavigationEntryImpl::FromNavigationEntry(
controller.GetLastCommittedEntry())->site_instance();
EXPECT_EQ(kUrl2, controller.GetEntryWithPageID(site_instance, 1)->GetURL());
EXPECT_EQ(1, controller.GetLastCommittedEntryIndex());
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
controller.GoForward();
main_test_rfh()->SendNavigate(2, kUrl3);
controller.GoBack();
EXPECT_EQ(1, controller.GetPendingEntryIndex());
EXPECT_EQ(2, controller.GetLastCommittedEntryIndex());
main_test_rfh()->SendNavigate(0, kUrl1);
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_EQ(0, controller.GetLastCommittedEntryIndex());
EXPECT_EQ(kUrl1, controller.GetLastCommittedEntry()->GetURL());
}
TEST_F(NavigationControllerTest, Forward) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url1("http://foo1");
const GURL url2("http://foo2");
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
main_test_rfh()->SendNavigate(1, url2);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
controller.GoBack();
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
controller.GoForward();
EXPECT_EQ(controller.GetEntryCount(), 2);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 0);
EXPECT_EQ(controller.GetPendingEntryIndex(), 1);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_TRUE(controller.GetPendingEntry());
EXPECT_TRUE(controller.CanGoBack());
EXPECT_TRUE(controller.CanGoToOffset(-1));
EXPECT_FALSE(controller.CanGoToOffset(-2));
EXPECT_FALSE(controller.CanGoForward());
EXPECT_FALSE(controller.CanGoToOffset(1));
EXPECT_FALSE(controller.GetEntryAtIndex(0)->GetTimestamp().is_null());
EXPECT_GE(controller.GetEntryAtIndex(0)->GetTimestamp(),
controller.GetEntryAtIndex(1)->GetTimestamp());
main_test_rfh()->SendNavigate(1, url2);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(controller.GetEntryCount(), 2);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 1);
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_FALSE(controller.GetPendingEntry());
EXPECT_TRUE(controller.CanGoBack());
EXPECT_TRUE(controller.CanGoToOffset(-1));
EXPECT_FALSE(controller.CanGoToOffset(-2));
EXPECT_FALSE(controller.CanGoForward());
EXPECT_FALSE(controller.CanGoToOffset(1));
EXPECT_GE(controller.GetEntryAtIndex(1)->GetTimestamp(),
controller.GetEntryAtIndex(0)->GetTimestamp());
}
TEST_F(NavigationControllerTest, Forward_GeneratesNewPage) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url1("http://foo1");
const GURL url2("http://foo2");
const GURL url3("http://foo3");
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
main_test_rfh()->SendNavigate(1, url2);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
controller.GoBack();
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
controller.GoForward();
EXPECT_EQ(0U, notifications.size());
EXPECT_EQ(controller.GetEntryCount(), 2);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 0);
EXPECT_EQ(controller.GetPendingEntryIndex(), 1);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_TRUE(controller.GetPendingEntry());
EXPECT_TRUE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
main_test_rfh()->SendNavigate(2, url3);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_TRUE(notifications.Check1AndReset(NOTIFICATION_NAV_LIST_PRUNED));
EXPECT_EQ(controller.GetEntryCount(), 2);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 1);
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_FALSE(controller.GetPendingEntry());
EXPECT_TRUE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
}
TEST_F(NavigationControllerTest, Redirect) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url1("http://foo1");
const GURL url2("http://foo2");
controller.LoadURL(url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
EXPECT_EQ(0U, notifications.size());
main_test_rfh()->SendNavigate(0, url2);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
controller.LoadURL(url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
EXPECT_TRUE(controller.GetPendingEntry());
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_EQ(url1, controller.GetVisibleEntry()->GetURL());
FrameHostMsg_DidCommitProvisionalLoad_Params params;
params.page_id = 0;
params.url = url2;
params.transition = PAGE_TRANSITION_SERVER_REDIRECT;
params.redirects.push_back(GURL("http://foo1"));
params.redirects.push_back(GURL("http://foo2"));
params.should_update_history = false;
params.gesture = NavigationGestureAuto;
params.is_post = false;
params.page_state = PageState::CreateFromURL(url2);
LoadCommittedDetails details;
EXPECT_EQ(0U, notifications.size());
EXPECT_TRUE(controller.RendererDidNavigate(main_test_rfh(), params,
&details));
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_TRUE(details.type == NAVIGATION_TYPE_SAME_PAGE);
EXPECT_EQ(controller.GetEntryCount(), 1);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 0);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_FALSE(controller.GetPendingEntry());
EXPECT_EQ(url2, controller.GetVisibleEntry()->GetURL());
EXPECT_FALSE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
}
TEST_F(NavigationControllerTest, PostThenRedirect) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url1("http://foo1");
const GURL url2("http://foo2");
controller.LoadURL(url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
controller.GetVisibleEntry()->SetHasPostData(true);
EXPECT_EQ(0U, notifications.size());
main_test_rfh()->SendNavigate(0, url2);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
controller.LoadURL(url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
EXPECT_TRUE(controller.GetPendingEntry());
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_EQ(url1, controller.GetVisibleEntry()->GetURL());
FrameHostMsg_DidCommitProvisionalLoad_Params params;
params.page_id = 0;
params.url = url2;
params.transition = PAGE_TRANSITION_SERVER_REDIRECT;
params.redirects.push_back(GURL("http://foo1"));
params.redirects.push_back(GURL("http://foo2"));
params.should_update_history = false;
params.gesture = NavigationGestureAuto;
params.is_post = false;
params.page_state = PageState::CreateFromURL(url2);
LoadCommittedDetails details;
EXPECT_EQ(0U, notifications.size());
EXPECT_TRUE(controller.RendererDidNavigate(main_test_rfh(), params,
&details));
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_TRUE(details.type == NAVIGATION_TYPE_SAME_PAGE);
EXPECT_EQ(controller.GetEntryCount(), 1);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 0);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_FALSE(controller.GetPendingEntry());
EXPECT_EQ(url2, controller.GetVisibleEntry()->GetURL());
EXPECT_FALSE(controller.GetVisibleEntry()->GetHasPostData());
EXPECT_FALSE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
}
TEST_F(NavigationControllerTest, ImmediateRedirect) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url1("http://foo1");
const GURL url2("http://foo2");
controller.LoadURL(url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
EXPECT_TRUE(controller.GetPendingEntry());
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_EQ(url1, controller.GetVisibleEntry()->GetURL());
FrameHostMsg_DidCommitProvisionalLoad_Params params;
params.page_id = 0;
params.url = url2;
params.transition = PAGE_TRANSITION_SERVER_REDIRECT;
params.redirects.push_back(GURL("http://foo1"));
params.redirects.push_back(GURL("http://foo2"));
params.should_update_history = false;
params.gesture = NavigationGestureAuto;
params.is_post = false;
params.page_state = PageState::CreateFromURL(url2);
LoadCommittedDetails details;
EXPECT_EQ(0U, notifications.size());
EXPECT_TRUE(controller.RendererDidNavigate(main_test_rfh(), params,
&details));
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_TRUE(details.type == NAVIGATION_TYPE_NEW_PAGE);
EXPECT_EQ(controller.GetEntryCount(), 1);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 0);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_FALSE(controller.GetPendingEntry());
EXPECT_EQ(url2, controller.GetVisibleEntry()->GetURL());
EXPECT_FALSE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
}
TEST_F(NavigationControllerTest, NewSubframe) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url1("http://foo1");
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
const GURL url2("http://foo2");
FrameHostMsg_DidCommitProvisionalLoad_Params params;
params.page_id = 1;
params.url = url2;
params.transition = PAGE_TRANSITION_MANUAL_SUBFRAME;
params.should_update_history = false;
params.gesture = NavigationGestureUser;
params.is_post = false;
params.page_state = PageState::CreateFromURL(url2);
LoadCommittedDetails details;
EXPECT_TRUE(controller.RendererDidNavigate(main_test_rfh(), params,
&details));
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(url1, details.previous_url);
EXPECT_FALSE(details.is_in_page);
EXPECT_FALSE(details.is_main_frame);
EXPECT_EQ(2, controller.GetEntryCount());
EXPECT_EQ(url1, details.entry->GetURL());
EXPECT_EQ(params.page_id, details.entry->GetPageID());
}
TEST_F(NavigationControllerTest, SubframeOnEmptyPage) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url("http://foo2");
FrameHostMsg_DidCommitProvisionalLoad_Params params;
params.page_id = 1;
params.url = url;
params.transition = PAGE_TRANSITION_AUTO_SUBFRAME;
params.should_update_history = false;
params.gesture = NavigationGestureAuto;
params.is_post = false;
params.page_state = PageState::CreateFromURL(url);
LoadCommittedDetails details;
EXPECT_FALSE(controller.RendererDidNavigate(main_test_rfh(), params,
&details));
EXPECT_EQ(0U, notifications.size());
}
TEST_F(NavigationControllerTest, AutoSubframe) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url1("http://foo1");
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
const GURL url2("http://foo2");
FrameHostMsg_DidCommitProvisionalLoad_Params params;
params.page_id = 0;
params.url = url2;
params.transition = PAGE_TRANSITION_AUTO_SUBFRAME;
params.should_update_history = false;
params.gesture = NavigationGestureUser;
params.is_post = false;
params.page_state = PageState::CreateFromURL(url2);
LoadCommittedDetails details;
EXPECT_FALSE(controller.RendererDidNavigate(main_test_rfh(), params,
&details));
EXPECT_EQ(0U, notifications.size());
EXPECT_EQ(1, controller.GetEntryCount());
}
TEST_F(NavigationControllerTest, BackSubframe) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url1("http://foo1");
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
const GURL url2("http://foo2");
FrameHostMsg_DidCommitProvisionalLoad_Params params;
params.page_id = 1;
params.url = url2;
params.transition = PAGE_TRANSITION_MANUAL_SUBFRAME;
params.should_update_history = false;
params.gesture = NavigationGestureUser;
params.is_post = false;
params.page_state = PageState::CreateFromURL(url2);
LoadCommittedDetails details;
EXPECT_TRUE(controller.RendererDidNavigate(main_test_rfh(), params,
&details));
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(2, controller.GetEntryCount());
const GURL url3("http://foo3");
params.page_id = 2;
params.url = url3;
EXPECT_TRUE(controller.RendererDidNavigate(main_test_rfh(), params,
&details));
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(3, controller.GetEntryCount());
EXPECT_EQ(2, controller.GetCurrentEntryIndex());
controller.GoBack();
params.url = url2;
params.page_id = 1;
EXPECT_TRUE(controller.RendererDidNavigate(main_test_rfh(), params,
&details));
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(3, controller.GetEntryCount());
EXPECT_EQ(1, controller.GetCurrentEntryIndex());
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_FALSE(controller.GetPendingEntry());
controller.GoBack();
params.url = url1;
params.page_id = 0;
EXPECT_TRUE(controller.RendererDidNavigate(main_test_rfh(), params,
&details));
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(3, controller.GetEntryCount());
EXPECT_EQ(0, controller.GetCurrentEntryIndex());
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_FALSE(controller.GetPendingEntry());
}
TEST_F(NavigationControllerTest, LinkClick) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url1("http://foo1");
const GURL url2("http://foo2");
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
main_test_rfh()->SendNavigate(1, url2);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(controller.GetEntryCount(), 2);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 1);
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_FALSE(controller.GetPendingEntry());
EXPECT_TRUE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
}
TEST_F(NavigationControllerTest, InPage) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url1("http://foo");
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
FrameHostMsg_DidCommitProvisionalLoad_Params self_params;
self_params.page_id = 0;
self_params.url = url1;
self_params.transition = PAGE_TRANSITION_LINK;
self_params.should_update_history = false;
self_params.gesture = NavigationGestureUser;
self_params.is_post = false;
self_params.page_state = PageState::CreateFromURL(url1);
self_params.was_within_same_page = true;
LoadCommittedDetails details;
EXPECT_TRUE(controller.RendererDidNavigate(main_test_rfh(), self_params,
&details));
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_TRUE(details.is_in_page);
EXPECT_TRUE(details.did_replace_entry);
EXPECT_EQ(1, controller.GetEntryCount());
const GURL url2("http://foo#a");
FrameHostMsg_DidCommitProvisionalLoad_Params params;
params.page_id = 1;
params.url = url2;
params.transition = PAGE_TRANSITION_LINK;
params.should_update_history = false;
params.gesture = NavigationGestureUser;
params.is_post = false;
params.page_state = PageState::CreateFromURL(url2);
params.was_within_same_page = true;
EXPECT_TRUE(controller.RendererDidNavigate(main_test_rfh(), params,
&details));
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_TRUE(details.is_in_page);
EXPECT_FALSE(details.did_replace_entry);
EXPECT_EQ(2, controller.GetEntryCount());
FrameHostMsg_DidCommitProvisionalLoad_Params back_params(params);
controller.GoBack();
back_params.url = url1;
back_params.page_id = 0;
EXPECT_TRUE(controller.RendererDidNavigate(main_test_rfh(), back_params,
&details));
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_TRUE(details.is_in_page);
EXPECT_EQ(2, controller.GetEntryCount());
EXPECT_EQ(0, controller.GetCurrentEntryIndex());
EXPECT_EQ(back_params.url, controller.GetVisibleEntry()->GetURL());
FrameHostMsg_DidCommitProvisionalLoad_Params forward_params(params);
controller.GoForward();
forward_params.url = url2;
forward_params.page_id = 1;
EXPECT_TRUE(controller.RendererDidNavigate(main_test_rfh(), forward_params,
&details));
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_TRUE(details.is_in_page);
EXPECT_EQ(2, controller.GetEntryCount());
EXPECT_EQ(1, controller.GetCurrentEntryIndex());
EXPECT_EQ(forward_params.url,
controller.GetVisibleEntry()->GetURL());
controller.GoBack();
EXPECT_TRUE(controller.RendererDidNavigate(main_test_rfh(), back_params,
&details));
controller.GoForward();
EXPECT_TRUE(controller.RendererDidNavigate(main_test_rfh(), forward_params,
&details));
EXPECT_EQ(forward_params.url,
controller.GetVisibleEntry()->GetURL());
const GURL url3("http://bar");
params.page_id = 2;
params.url = url3;
navigation_entry_committed_counter_ = 0;
EXPECT_TRUE(controller.RendererDidNavigate(main_test_rfh(), params,
&details));
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_FALSE(details.is_in_page);
EXPECT_EQ(3, controller.GetEntryCount());
EXPECT_EQ(2, controller.GetCurrentEntryIndex());
}
TEST_F(NavigationControllerTest, InPage_Replace) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url1("http://foo");
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
const GURL url2("http://foo#a");
FrameHostMsg_DidCommitProvisionalLoad_Params params;
params.page_id = 0;
params.url = url2;
params.transition = PAGE_TRANSITION_LINK;
params.should_update_history = false;
params.gesture = NavigationGestureUser;
params.is_post = false;
params.page_state = PageState::CreateFromURL(url2);
LoadCommittedDetails details;
EXPECT_TRUE(controller.RendererDidNavigate(main_test_rfh(), params,
&details));
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_TRUE(details.is_in_page);
EXPECT_TRUE(details.did_replace_entry);
EXPECT_EQ(1, controller.GetEntryCount());
}
TEST_F(NavigationControllerTest, ClientRedirectAfterInPageNavigation) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
{
const GURL url("http://foo/");
main_test_rfh()->SendNavigate(0, url);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
}
{
const GURL url("http://foo2/");
main_test_rfh()->SendNavigate(1, url);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
}
{
const GURL url("http://foo2/#a");
FrameHostMsg_DidCommitProvisionalLoad_Params params;
params.page_id = 1;
params.url = url;
params.transition = PAGE_TRANSITION_LINK;
params.redirects.push_back(url);
params.should_update_history = true;
params.gesture = NavigationGestureUnknown;
params.is_post = false;
params.page_state = PageState::CreateFromURL(url);
LoadCommittedDetails details;
EXPECT_TRUE(controller.RendererDidNavigate(main_test_rfh(), params,
&details));
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_TRUE(details.is_in_page);
EXPECT_TRUE(details.did_replace_entry);
EXPECT_EQ(2, controller.GetEntryCount());
}
{
const GURL url("http://foo3/");
FrameHostMsg_DidCommitProvisionalLoad_Params params;
params.page_id = 2;
params.url = url;
params.transition = PAGE_TRANSITION_CLIENT_REDIRECT;
params.redirects.push_back(GURL("http://foo2/#a"));
params.redirects.push_back(url);
params.should_update_history = true;
params.gesture = NavigationGestureUnknown;
params.is_post = false;
params.page_state = PageState::CreateFromURL(url);
LoadCommittedDetails details;
EXPECT_TRUE(controller.RendererDidNavigate(main_test_rfh(), params,
&details));
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_FALSE(details.is_in_page);
EXPECT_EQ(3, controller.GetEntryCount());
}
{
const GURL url("http://foo2/");
controller.GoBack();
main_test_rfh()->SendNavigate(1, url);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_EQ(url, controller.GetVisibleEntry()->GetURL());
}
}
class PrunedListener : public NotificationObserver {
public:
explicit PrunedListener(NavigationControllerImpl* controller)
: notification_count_(0) {
registrar_.Add(this, NOTIFICATION_NAV_LIST_PRUNED,
Source<NavigationController>(controller));
}
virtual void Observe(int type,
const NotificationSource& source,
const NotificationDetails& details) OVERRIDE {
if (type == NOTIFICATION_NAV_LIST_PRUNED) {
notification_count_++;
details_ = *(Details<PrunedDetails>(details).ptr());
}
}
int notification_count_;
PrunedDetails details_;
private:
NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(PrunedListener);
};
TEST_F(NavigationControllerTest, EnforceMaxNavigationCount) {
NavigationControllerImpl& controller = controller_impl();
size_t original_count = NavigationControllerImpl::max_entry_count();
const int kMaxEntryCount = 5;
NavigationControllerImpl::set_max_entry_count_for_testing(kMaxEntryCount);
int url_index;
for (url_index = 0; url_index < kMaxEntryCount; url_index++) {
GURL url(base::StringPrintf("http://www.a.com/%d", url_index));
controller.LoadURL(
url, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(url_index, url);
}
EXPECT_EQ(controller.GetEntryCount(), kMaxEntryCount);
PrunedListener listener(&controller);
GURL url(base::StringPrintf("http://www.a.com/%d", url_index));
controller.LoadURL(
url, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(url_index, url);
url_index++;
EXPECT_EQ(1, listener.notification_count_);
EXPECT_TRUE(listener.details_.from_front);
EXPECT_EQ(1, listener.details_.count);
EXPECT_EQ(controller.GetEntryCount(), kMaxEntryCount);
EXPECT_EQ(controller.GetEntryAtIndex(0)->GetURL(),
GURL("http:////www.a.com/1"));
for (int i = 0; i < 3; i++) {
url = GURL(base::StringPrintf("http:////www.a.com/%d", url_index));
controller.LoadURL(
url, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(url_index, url);
url_index++;
}
EXPECT_EQ(controller.GetEntryCount(), kMaxEntryCount);
EXPECT_EQ(controller.GetEntryAtIndex(0)->GetURL(),
GURL("http:////www.a.com/4"));
NavigationControllerImpl::set_max_entry_count_for_testing(original_count);
}
TEST_F(NavigationControllerTest, RestoreNavigate) {
GURL url("http://foo");
std::vector<NavigationEntry*> entries;
NavigationEntry* entry = NavigationControllerImpl::CreateNavigationEntry(
url, Referrer(), PAGE_TRANSITION_RELOAD, false, std::string(),
browser_context());
entry->SetPageID(0);
entry->SetTitle(base::ASCIIToUTF16("Title"));
entry->SetPageState(PageState::CreateFromEncodedData("state"));
const base::Time timestamp = base::Time::Now();
entry->SetTimestamp(timestamp);
entries.push_back(entry);
scoped_ptr<WebContentsImpl> our_contents(static_cast<WebContentsImpl*>(
WebContents::Create(WebContents::CreateParams(browser_context()))));
NavigationControllerImpl& our_controller = our_contents->GetController();
our_controller.Restore(
0,
NavigationController::RESTORE_LAST_SESSION_EXITED_CLEANLY,
&entries);
ASSERT_EQ(0u, entries.size());
ASSERT_EQ(1, our_controller.GetEntryCount());
EXPECT_EQ(NavigationEntryImpl::RESTORE_LAST_SESSION_EXITED_CLEANLY,
NavigationEntryImpl::FromNavigationEntry(
our_controller.GetEntryAtIndex(0))->restore_type());
EXPECT_FALSE(NavigationEntryImpl::FromNavigationEntry(
our_controller.GetEntryAtIndex(0))->site_instance());
our_controller.GoToIndex(0);
EXPECT_EQ(1, our_controller.GetEntryCount());
EXPECT_EQ(our_controller.GetEntryAtIndex(0),
our_controller.GetPendingEntry());
EXPECT_EQ(0, our_controller.GetEntryAtIndex(0)->GetPageID());
EXPECT_EQ(NavigationEntryImpl::RESTORE_NONE,
NavigationEntryImpl::FromNavigationEntry
(our_controller.GetEntryAtIndex(0))->restore_type());
EXPECT_TRUE(NavigationEntryImpl::FromNavigationEntry(
our_controller.GetEntryAtIndex(0))->site_instance());
EXPECT_EQ(timestamp, our_controller.GetEntryAtIndex(0)->GetTimestamp());
FrameHostMsg_DidCommitProvisionalLoad_Params params;
params.page_id = 0;
params.url = url;
params.transition = PAGE_TRANSITION_LINK;
params.should_update_history = false;
params.gesture = NavigationGestureUser;
params.is_post = false;
params.page_state = PageState::CreateFromURL(url);
LoadCommittedDetails details;
our_controller.RendererDidNavigate(our_contents->GetMainFrame(), params,
&details);
EXPECT_EQ(1, our_controller.GetEntryCount());
EXPECT_EQ(0, our_controller.GetLastCommittedEntryIndex());
EXPECT_FALSE(our_controller.GetPendingEntry());
EXPECT_EQ(url,
NavigationEntryImpl::FromNavigationEntry(
our_controller.GetLastCommittedEntry())->site_instance()->
GetSiteURL());
EXPECT_EQ(NavigationEntryImpl::RESTORE_NONE,
NavigationEntryImpl::FromNavigationEntry(
our_controller.GetEntryAtIndex(0))->restore_type());
EXPECT_GE(our_controller.GetEntryAtIndex(0)->GetTimestamp(), timestamp);
}
TEST_F(NavigationControllerTest, RestoreNavigateAfterFailure) {
GURL url("http://foo");
std::vector<NavigationEntry*> entries;
NavigationEntry* entry = NavigationControllerImpl::CreateNavigationEntry(
url, Referrer(), PAGE_TRANSITION_RELOAD, false, std::string(),
browser_context());
entry->SetPageID(0);
entry->SetTitle(base::ASCIIToUTF16("Title"));
entry->SetPageState(PageState::CreateFromEncodedData("state"));
entries.push_back(entry);
scoped_ptr<WebContentsImpl> our_contents(static_cast<WebContentsImpl*>(
WebContents::Create(WebContents::CreateParams(browser_context()))));
NavigationControllerImpl& our_controller = our_contents->GetController();
our_controller.Restore(
0, NavigationController::RESTORE_LAST_SESSION_EXITED_CLEANLY, &entries);
ASSERT_EQ(0u, entries.size());
EXPECT_EQ(NavigationEntryImpl::RESTORE_LAST_SESSION_EXITED_CLEANLY,
NavigationEntryImpl::FromNavigationEntry(
our_controller.GetEntryAtIndex(0))->restore_type());
EXPECT_FALSE(NavigationEntryImpl::FromNavigationEntry(
our_controller.GetEntryAtIndex(0))->site_instance());
our_controller.GoToIndex(0);
EXPECT_EQ(1, our_controller.GetEntryCount());
EXPECT_EQ(our_controller.GetEntryAtIndex(0),
our_controller.GetPendingEntry());
EXPECT_EQ(0, our_controller.GetEntryAtIndex(0)->GetPageID());
EXPECT_EQ(NavigationEntryImpl::RESTORE_NONE,
NavigationEntryImpl::FromNavigationEntry(
our_controller.GetEntryAtIndex(0))->restore_type());
EXPECT_TRUE(NavigationEntryImpl::FromNavigationEntry(
our_controller.GetEntryAtIndex(0))->site_instance());
FrameHostMsg_DidFailProvisionalLoadWithError_Params fail_load_params;
fail_load_params.error_code = net::ERR_ABORTED;
fail_load_params.error_description = base::string16();
fail_load_params.url = url;
fail_load_params.showing_repost_interstitial = false;
main_test_rfh()->OnMessageReceived(
FrameHostMsg_DidFailProvisionalLoadWithError(0,
fail_load_params));
FrameHostMsg_DidCommitProvisionalLoad_Params params;
params.page_id = 0;
params.url = url;
params.transition = PAGE_TRANSITION_LINK;
params.should_update_history = false;
params.gesture = NavigationGestureUser;
params.is_post = false;
params.page_state = PageState::CreateFromURL(url);
LoadCommittedDetails details;
our_controller.RendererDidNavigate(our_contents->GetMainFrame(), params,
&details);
EXPECT_EQ(1, our_controller.GetEntryCount());
EXPECT_EQ(0, our_controller.GetLastCommittedEntryIndex());
EXPECT_FALSE(our_controller.GetPendingEntry());
EXPECT_EQ(url,
NavigationEntryImpl::FromNavigationEntry(
our_controller.GetLastCommittedEntry())->site_instance()->
GetSiteURL());
EXPECT_EQ(NavigationEntryImpl::RESTORE_NONE,
NavigationEntryImpl::FromNavigationEntry(
our_controller.GetEntryAtIndex(0))->restore_type());
}
TEST_F(NavigationControllerTest, Interstitial) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo");
controller.LoadURL(
url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(0, url1);
const GURL url2("http://bar");
controller.LoadURL(
url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
NavigationEntryImpl::FromNavigationEntry(controller.GetPendingEntry())->
set_page_type(PAGE_TYPE_INTERSTITIAL);
main_test_rfh()->SendNavigate(1, url2);
EXPECT_EQ(url2, controller.GetLastCommittedEntry()->GetURL());
EXPECT_EQ(PAGE_TYPE_NORMAL,
controller.GetLastCommittedEntry()->GetPageType());
}
TEST_F(NavigationControllerTest, RemoveEntry) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo/1");
const GURL url2("http://foo/2");
const GURL url3("http://foo/3");
const GURL url4("http://foo/4");
const GURL url5("http://foo/5");
const GURL pending_url("http://foo/pending");
const GURL default_url("http://foo/default");
controller.LoadURL(
url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(0, url1);
controller.LoadURL(
url2, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(1, url2);
controller.LoadURL(
url3, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(2, url3);
controller.LoadURL(
url4, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(3, url4);
controller.LoadURL(
url5, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(4, url5);
EXPECT_FALSE(controller.RemoveEntryAtIndex(controller.GetEntryCount() - 1));
EXPECT_EQ(5, controller.GetEntryCount());
EXPECT_EQ(4, controller.GetLastCommittedEntryIndex());
controller.GoBack();
EXPECT_FALSE(controller.RemoveEntryAtIndex(controller.GetEntryCount() - 1));
EXPECT_FALSE(controller.RemoveEntryAtIndex(controller.GetEntryCount() - 2));
main_test_rfh()->SendNavigate(3, url4);
EXPECT_TRUE(controller.RemoveEntryAtIndex(controller.GetEntryCount() - 1));
EXPECT_EQ(4, controller.GetEntryCount());
EXPECT_EQ(3, controller.GetLastCommittedEntryIndex());
EXPECT_FALSE(controller.GetPendingEntry());
EXPECT_TRUE(controller.RemoveEntryAtIndex(0));
EXPECT_EQ(3, controller.GetEntryCount());
EXPECT_EQ(2, controller.GetLastCommittedEntryIndex());
EXPECT_FALSE(controller.GetPendingEntry());
controller.RemoveEntryAtIndex(1);
controller.RemoveEntryAtIndex(0);
EXPECT_EQ(1, controller.GetEntryCount());
EXPECT_EQ(0, controller.GetLastCommittedEntryIndex());
}
TEST_F(NavigationControllerTest, TransientEntry) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url0("http://foo/0");
const GURL url1("http://foo/1");
const GURL url2("http://foo/2");
const GURL url3("http://foo/3");
const GURL url3_ref("http://foo/3#bar");
const GURL url4("http://foo/4");
const GURL transient_url("http://foo/transient");
controller.LoadURL(
url0, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(0, url0);
controller.LoadURL(
url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(1, url1);
notifications.Reset();
NavigationEntryImpl* transient_entry = new NavigationEntryImpl;
transient_entry->SetURL(transient_url);
controller.SetTransientEntry(transient_entry);
EXPECT_EQ(0U, notifications.size());
EXPECT_EQ(transient_url, controller.GetVisibleEntry()->GetURL());
EXPECT_EQ(controller.GetEntryCount(), 3);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 1);
EXPECT_EQ(controller.GetPendingEntryIndex(), -1);
EXPECT_TRUE(controller.GetLastCommittedEntry());
EXPECT_FALSE(controller.GetPendingEntry());
EXPECT_TRUE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
EXPECT_EQ(contents()->GetMaxPageID(), 1);
controller.LoadURL(
url2, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(2, url2);
EXPECT_EQ(url2, controller.GetVisibleEntry()->GetURL());
EXPECT_EQ(controller.GetEntryCount(), 3);
transient_entry = new NavigationEntryImpl;
transient_entry->SetURL(transient_url);
controller.SetTransientEntry(transient_entry);
EXPECT_EQ(transient_url, controller.GetVisibleEntry()->GetURL());
main_test_rfh()->SendNavigate(3, url3);
EXPECT_EQ(url3, controller.GetVisibleEntry()->GetURL());
EXPECT_EQ(controller.GetEntryCount(), 4);
controller.LoadURL(
url4, Referrer(), PAGE_TRANSITION_TYPED, std::string());
transient_entry = new NavigationEntryImpl;
transient_entry->SetURL(transient_url);
controller.SetTransientEntry(transient_entry);
EXPECT_EQ(transient_url, controller.GetVisibleEntry()->GetURL());
main_test_rfh()->SendNavigate(4, url4);
EXPECT_EQ(url4, controller.GetVisibleEntry()->GetURL());
EXPECT_EQ(controller.GetEntryCount(), 5);
transient_entry = new NavigationEntryImpl;
transient_entry->SetURL(transient_url);
controller.SetTransientEntry(transient_entry);
EXPECT_EQ(transient_url, controller.GetVisibleEntry()->GetURL());
EXPECT_TRUE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
controller.GoBack();
EXPECT_EQ(url4, controller.GetVisibleEntry()->GetURL());
EXPECT_EQ(controller.GetEntryCount(), 5);
main_test_rfh()->SendNavigate(3, url3);
transient_entry = new NavigationEntryImpl;
transient_entry->SetURL(transient_url);
controller.SetTransientEntry(transient_entry);
EXPECT_EQ(transient_url, controller.GetVisibleEntry()->GetURL());
controller.GoToIndex(1);
EXPECT_FALSE(controller.GetTransientEntry());
EXPECT_EQ(url1, controller.GetPendingEntry()->GetURL());
EXPECT_EQ(url3, controller.GetVisibleEntry()->GetURL());
main_test_rfh()->SendNavigate(1, url1);
EXPECT_EQ(url1, controller.GetVisibleEntry()->GetURL());
transient_entry = new NavigationEntryImpl;
transient_entry->SetURL(transient_url);
controller.SetTransientEntry(transient_entry);
EXPECT_EQ(transient_url, controller.GetVisibleEntry()->GetURL());
controller.GoToIndex(3);
EXPECT_EQ(url2, controller.GetPendingEntry()->GetURL());
EXPECT_EQ(url1, controller.GetVisibleEntry()->GetURL());
main_test_rfh()->SendNavigate(2, url2);
EXPECT_EQ(url2, controller.GetVisibleEntry()->GetURL());
transient_entry = new NavigationEntryImpl;
transient_entry->SetURL(transient_url);
controller.SetTransientEntry(transient_entry);
EXPECT_EQ(transient_url, controller.GetVisibleEntry()->GetURL());
EXPECT_TRUE(controller.CanGoForward());
controller.GoForward();
EXPECT_FALSE(controller.GetTransientEntry());
EXPECT_EQ(url3, controller.GetPendingEntry()->GetURL());
EXPECT_EQ(url2, controller.GetVisibleEntry()->GetURL());
main_test_rfh()->SendNavigate(3, url3);
EXPECT_EQ(url3, controller.GetVisibleEntry()->GetURL());
transient_entry = new NavigationEntryImpl;
transient_entry->SetURL(transient_url);
controller.SetTransientEntry(transient_entry);
EXPECT_EQ(transient_url, controller.GetVisibleEntry()->GetURL());
main_test_rfh()->SendNavigate(3, url3_ref);
EXPECT_FALSE(controller.GetTransientEntry());
EXPECT_EQ(url3_ref, controller.GetVisibleEntry()->GetURL());
EXPECT_EQ(controller.GetEntryCount(), 5);
EXPECT_EQ(controller.GetEntryAtIndex(0)->GetURL(), url0);
EXPECT_EQ(controller.GetEntryAtIndex(1)->GetURL(), url1);
EXPECT_EQ(controller.GetEntryAtIndex(2)->GetURL(), url2);
EXPECT_EQ(controller.GetEntryAtIndex(3)->GetURL(), url3_ref);
EXPECT_EQ(controller.GetEntryAtIndex(4)->GetURL(), url4);
}
TEST_F(NavigationControllerTest, ReloadTransient) {
NavigationControllerImpl& controller = controller_impl();
const GURL url0("http://foo/0");
const GURL url1("http://foo/1");
const GURL transient_url("http://foo/transient");
controller.LoadURL(
url0, Referrer(), PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SendNavigate(0, url0);
controller.LoadURL(
url1, Referrer(), PAGE_TRANSITION_TYPED, std::string());
NavigationEntryImpl* transient_entry = new NavigationEntryImpl;
transient_entry->SetURL(transient_url);
controller.SetTransientEntry(transient_entry);
EXPECT_TRUE(controller.GetTransientEntry());
EXPECT_EQ(transient_url, controller.GetVisibleEntry()->GetURL());
controller.Reload(true);
EXPECT_FALSE(controller.GetTransientEntry());
EXPECT_TRUE(controller.GetPendingEntry());
EXPECT_EQ(transient_url, controller.GetVisibleEntry()->GetURL());
ASSERT_EQ(controller.GetEntryCount(), 1);
EXPECT_EQ(controller.GetEntryAtIndex(0)->GetURL(), url0);
main_test_rfh()->SendNavigate(1, transient_url);
ASSERT_EQ(controller.GetEntryCount(), 2);
EXPECT_EQ(controller.GetEntryAtIndex(0)->GetURL(), url0);
EXPECT_EQ(controller.GetEntryAtIndex(1)->GetURL(), transient_url);
}
TEST_F(NavigationControllerTest, RendererInitiatedPendingEntries) {
NavigationControllerImpl& controller = controller_impl();
Navigator* navigator =
contents()->GetFrameTree()->root()->navigator();
const GURL url1("nonexistent:12121");
const GURL url1_fixed("http://nonexistent:12121/");
const GURL url2("http://foo");
navigator->DidStartProvisionalLoad(main_test_rfh(), -1, url1);
controller.GetPendingEntry()->SetURL(url1_fixed);
controller.GetPendingEntry()->SetVirtualURL(url1);
EXPECT_EQ(url1_fixed, controller.GetPendingEntry()->GetURL());
EXPECT_EQ(url1, controller.GetPendingEntry()->GetVirtualURL());
EXPECT_TRUE(
NavigationEntryImpl::FromNavigationEntry(controller.GetPendingEntry())->
is_renderer_initiated());
navigator->DidStartProvisionalLoad(main_test_rfh(), -1, url2);
EXPECT_EQ(url2, controller.GetPendingEntry()->GetURL());
EXPECT_EQ(url2, controller.GetPendingEntry()->GetVirtualURL());
main_test_rfh()->SendNavigate(0, url2);
EXPECT_EQ(url2, controller.GetLastCommittedEntry()->GetURL());
EXPECT_EQ(url2, controller.GetLastCommittedEntry()->GetVirtualURL());
navigator->DidStartProvisionalLoad(main_test_rfh(), -1, url1);
EXPECT_EQ(url1, controller.GetPendingEntry()->GetURL());
navigator->DidStartProvisionalLoad(
main_test_rfh(), -1, GURL(kUnreachableWebDataURL));
EXPECT_EQ(url1, controller.GetPendingEntry()->GetURL());
navigator->DidStartProvisionalLoad(main_test_rfh(), -1, url1);
NavigationEntryImpl::FromNavigationEntry(controller.GetPendingEntry())->
set_should_replace_entry(true);
navigator->DidStartProvisionalLoad(main_test_rfh(), -1, url2);
EXPECT_TRUE(
NavigationEntryImpl::FromNavigationEntry(controller.GetPendingEntry())->
should_replace_entry());
main_test_rfh()->SendNavigate(0, url2);
EXPECT_EQ(url2, controller.GetLastCommittedEntry()->GetURL());
}
TEST_F(NavigationControllerTest, DontShowRendererURLUntilCommit) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url0("http://foo/0");
const GURL url1("http://foo/1");
controller.LoadURL(url0, Referrer(), PAGE_TRANSITION_TYPED, std::string());
EXPECT_EQ(url0, controller.GetPendingEntry()->GetURL());
EXPECT_EQ(url0, controller.GetVisibleEntry()->GetURL());
main_test_rfh()->SendNavigate(0, url0);
NavigationController::LoadURLParams load_url_params(url1);
load_url_params.is_renderer_initiated = true;
controller.LoadURLWithParams(load_url_params);
EXPECT_EQ(url0, controller.GetVisibleEntry()->GetURL());
EXPECT_EQ(url1, controller.GetPendingEntry()->GetURL());
EXPECT_TRUE(
NavigationEntryImpl::FromNavigationEntry(controller.GetPendingEntry())->
is_renderer_initiated());
main_test_rfh()->SendNavigate(1, url1);
EXPECT_EQ(url1, controller.GetVisibleEntry()->GetURL());
EXPECT_FALSE(controller.GetPendingEntry());
EXPECT_FALSE(
NavigationEntryImpl::FromNavigationEntry(
controller.GetLastCommittedEntry())->is_renderer_initiated());
notifications.Reset();
}
TEST_F(NavigationControllerTest, ShowRendererURLInNewTabUntilModified) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url("http://foo");
NavigationController::LoadURLParams load_url_params(url);
load_url_params.transition_type = PAGE_TRANSITION_LINK;
load_url_params.is_renderer_initiated = true;
controller.LoadURLWithParams(load_url_params);
EXPECT_EQ(url, controller.GetVisibleEntry()->GetURL());
EXPECT_EQ(url, controller.GetPendingEntry()->GetURL());
EXPECT_TRUE(
NavigationEntryImpl::FromNavigationEntry(controller.GetPendingEntry())->
is_renderer_initiated());
EXPECT_TRUE(controller.IsInitialNavigation());
EXPECT_FALSE(test_rvh()->has_accessed_initial_document());
EXPECT_TRUE(contents()->GetTitle().empty());
test_rvh()->OnMessageReceived(
ViewHostMsg_DidAccessInitialDocument(0));
EXPECT_TRUE(test_rvh()->has_accessed_initial_document());
EXPECT_FALSE(controller.GetVisibleEntry());
EXPECT_EQ(url, controller.GetPendingEntry()->GetURL());
notifications.Reset();
}
TEST_F(NavigationControllerTest, DontShowRendererURLInNewTabAfterCommit) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
const GURL url1("http://foo/eh");
const GURL url2("http://foo/bee");
NavigationController::LoadURLParams load_url_params(url1);
load_url_params.transition_type = PAGE_TRANSITION_LINK;
load_url_params.is_renderer_initiated = true;
controller.LoadURLWithParams(load_url_params);
EXPECT_EQ(url1, controller.GetVisibleEntry()->GetURL());
EXPECT_TRUE(
NavigationEntryImpl::FromNavigationEntry(controller.GetPendingEntry())->
is_renderer_initiated());
EXPECT_TRUE(controller.IsInitialNavigation());
EXPECT_FALSE(test_rvh()->has_accessed_initial_document());
main_test_rfh()->SendNavigate(0, url1);
NavigationController::LoadURLParams load_url2_params(url2);
load_url2_params.transition_type = PAGE_TRANSITION_LINK;
load_url2_params.is_renderer_initiated = true;
controller.LoadURLWithParams(load_url2_params);
EXPECT_FALSE(test_rvh()->has_accessed_initial_document());
EXPECT_FALSE(controller.IsInitialNavigation());
EXPECT_TRUE(controller.GetVisibleEntry());
EXPECT_EQ(url1, controller.GetVisibleEntry()->GetURL());
notifications.Reset();
}
TEST_F(NavigationControllerTest, IsInPageNavigation) {
NavigationControllerImpl& controller = controller_impl();
const GURL url("http://www.google.com/home.html");
main_test_rfh()->SendNavigate(0, url);
EXPECT_FALSE(controller.IsURLInPageNavigation(url));
const GURL other_url("http://www.google.com/add.html");
EXPECT_FALSE(controller.IsURLInPageNavigation(other_url));
const GURL url_with_ref("http://www.google.com/home.html#my_ref");
EXPECT_TRUE(controller.IsURLInPageNavigation(url_with_ref));
main_test_rfh()->SendNavigate(1, url_with_ref);
EXPECT_FALSE(controller.IsURLInPageNavigation(url_with_ref));
EXPECT_FALSE(controller.IsURLInPageNavigation(url));
EXPECT_FALSE(controller.IsURLInPageNavigation(other_url));
const GURL other_url_with_ref("http://www.google.com/home.html#my_other_ref");
EXPECT_TRUE(controller.IsURLInPageNavigation(other_url_with_ref));
EXPECT_TRUE(controller.IsURLInPageNavigation(url_with_ref, true,
NAVIGATION_TYPE_UNKNOWN));
EXPECT_TRUE(controller.IsURLInPageNavigation(url, true,
NAVIGATION_TYPE_IN_PAGE));
}
TEST_F(NavigationControllerTest, SameSubframe) {
NavigationControllerImpl& controller = controller_impl();
const GURL url("http://www.google.com/");
main_test_rfh()->SendNavigate(0, url);
EXPECT_EQ(controller.GetEntryCount(), 1);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 0);
const GURL subframe("http://www.google.com/#");
FrameHostMsg_DidCommitProvisionalLoad_Params params;
params.page_id = 0;
params.url = subframe;
params.transition = PAGE_TRANSITION_AUTO_SUBFRAME;
params.should_update_history = false;
params.gesture = NavigationGestureAuto;
params.is_post = false;
params.page_state = PageState::CreateFromURL(subframe);
LoadCommittedDetails details;
EXPECT_FALSE(controller.RendererDidNavigate(main_test_rfh(), params,
&details));
EXPECT_EQ(controller.GetEntryCount(), 1);
EXPECT_EQ(controller.GetLastCommittedEntryIndex(), 0);
}
TEST_F(NavigationControllerTest, CloneAndGoBack) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo1");
const GURL url2("http://foo2");
const base::string16 title(base::ASCIIToUTF16("Title"));
NavigateAndCommit(url1);
controller.GetVisibleEntry()->SetTitle(title);
NavigateAndCommit(url2);
scoped_ptr<WebContents> clone(controller.GetWebContents()->Clone());
ASSERT_EQ(2, clone->GetController().GetEntryCount());
EXPECT_TRUE(clone->GetController().NeedsReload());
clone->GetController().GoBack();
EXPECT_FALSE(clone->GetController().NeedsReload());
EXPECT_EQ(url1, clone->GetController().GetVisibleEntry()->GetURL());
EXPECT_EQ(title, clone->GetTitle());
}
TEST_F(NavigationControllerTest, CloneAndReload) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo1");
const GURL url2("http://foo2");
const base::string16 title(base::ASCIIToUTF16("Title"));
NavigateAndCommit(url1);
controller.GetVisibleEntry()->SetTitle(title);
NavigateAndCommit(url2);
scoped_ptr<WebContents> clone(controller.GetWebContents()->Clone());
clone->GetController().LoadIfNecessary();
ASSERT_EQ(2, clone->GetController().GetEntryCount());
EXPECT_EQ(1, clone->GetController().GetPendingEntryIndex());
clone->GetController().Reload(true);
EXPECT_EQ(1, clone->GetController().GetPendingEntryIndex());
}
TEST_F(NavigationControllerTest, CloneOmitsInterstitials) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo1");
const GURL url2("http://foo2");
NavigateAndCommit(url1);
NavigateAndCommit(url2);
NavigationEntryImpl* interstitial_entry = new NavigationEntryImpl();
interstitial_entry->set_page_type(PAGE_TYPE_INTERSTITIAL);
controller.SetTransientEntry(interstitial_entry);
scoped_ptr<WebContents> clone(controller.GetWebContents()->Clone());
ASSERT_EQ(2, clone->GetController().GetEntryCount());
}
TEST_F(NavigationControllerTest, LazyReload) {
NavigationControllerImpl& controller = controller_impl();
const GURL url("http://foo");
NavigateAndCommit(url);
ASSERT_FALSE(controller.NeedsReload());
controller.SetNeedsReload();
ASSERT_TRUE(controller.NeedsReload());
controller.SetActive(true);
ASSERT_FALSE(controller.NeedsReload());
}
TEST_F(NavigationControllerTest, SubframeWhilePending) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo/");
NavigateAndCommit(url1);
const GURL url2("http://bar/");
controller.LoadURL(
url2, Referrer(), PAGE_TRANSITION_TYPED, std::string());
const GURL url1_sub("http://foo/subframe");
FrameHostMsg_DidCommitProvisionalLoad_Params params;
params.page_id = controller.GetLastCommittedEntry()->GetPageID();
params.url = url1_sub;
params.transition = PAGE_TRANSITION_AUTO_SUBFRAME;
params.should_update_history = false;
params.gesture = NavigationGestureAuto;
params.is_post = false;
params.page_state = PageState::CreateFromURL(url1_sub);
LoadCommittedDetails details;
EXPECT_FALSE(controller.RendererDidNavigate(main_test_rfh(), params,
&details));
EXPECT_EQ(url1, controller.GetLastCommittedEntry()->GetURL());
EXPECT_EQ(url2, controller.GetVisibleEntry()->GetURL());
}
TEST_F(NavigationControllerTest, CopyStateFrom) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo1");
const GURL url2("http://foo2");
NavigateAndCommit(url1);
NavigateAndCommit(url2);
controller.GoBack();
contents()->CommitPendingNavigation();
scoped_ptr<TestWebContents> other_contents(
static_cast<TestWebContents*>(CreateTestWebContents()));
NavigationControllerImpl& other_controller = other_contents->GetController();
other_controller.CopyStateFrom(controller);
ASSERT_EQ(2, other_controller.GetEntryCount());
ASSERT_EQ(0, other_controller.GetCurrentEntryIndex());
EXPECT_EQ(url1, other_controller.GetEntryAtIndex(0)->GetURL());
EXPECT_EQ(0, other_controller.GetEntryAtIndex(0)->GetPageID());
EXPECT_EQ(url2, other_controller.GetEntryAtIndex(1)->GetURL());
EXPECT_EQ(0, other_controller.GetEntryAtIndex(1)->GetPageID());
SiteInstance* instance1 =
GetSiteInstanceFromEntry(other_controller.GetEntryAtIndex(0));
EXPECT_EQ(0, other_contents->GetMaxPageIDForSiteInstance(instance1));
const SessionStorageNamespaceMap& session_storage_namespace_map =
controller.GetSessionStorageNamespaceMap();
const SessionStorageNamespaceMap& other_session_storage_namespace_map =
other_controller.GetSessionStorageNamespaceMap();
EXPECT_EQ(session_storage_namespace_map.size(),
other_session_storage_namespace_map.size());
for (SessionStorageNamespaceMap::const_iterator it =
session_storage_namespace_map.begin();
it != session_storage_namespace_map.end();
++it) {
SessionStorageNamespaceMap::const_iterator other =
other_session_storage_namespace_map.find(it->first);
EXPECT_TRUE(other != other_session_storage_namespace_map.end());
}
}
TEST_F(NavigationControllerTest, CopyStateFromAndPrune) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo/1");
const GURL url2("http://foo/2");
const GURL url3("http://foo/3");
NavigateAndCommit(url1);
NavigateAndCommit(url2);
SiteInstance* instance1 =
GetSiteInstanceFromEntry(controller.GetEntryAtIndex(0));
SiteInstance* instance2 =
GetSiteInstanceFromEntry(controller.GetEntryAtIndex(1));
EXPECT_EQ(instance1, instance2);
EXPECT_EQ(0, controller.GetEntryAtIndex(0)->GetPageID());
EXPECT_EQ(1, controller.GetEntryAtIndex(1)->GetPageID());
EXPECT_EQ(1, contents()->GetMaxPageIDForSiteInstance(instance1));
scoped_ptr<TestWebContents> other_contents(
static_cast<TestWebContents*>(CreateTestWebContents()));
NavigationControllerImpl& other_controller = other_contents->GetController();
other_contents->NavigateAndCommit(url3);
other_contents->ExpectSetHistoryLengthAndPrune(
GetSiteInstanceFromEntry(other_controller.GetEntryAtIndex(0)), 2,
other_controller.GetEntryAtIndex(0)->GetPageID());
other_controller.CopyStateFromAndPrune(&controller, false);
ASSERT_EQ(3, other_controller.GetEntryCount());
ASSERT_EQ(2, other_controller.GetCurrentEntryIndex());
EXPECT_EQ(url1, other_controller.GetEntryAtIndex(0)->GetURL());
EXPECT_EQ(url2, other_controller.GetEntryAtIndex(1)->GetURL());
EXPECT_EQ(url3, other_controller.GetEntryAtIndex(2)->GetURL());
EXPECT_EQ(0, other_controller.GetEntryAtIndex(0)->GetPageID());
EXPECT_EQ(1, other_controller.GetEntryAtIndex(1)->GetPageID());
EXPECT_EQ(0, other_controller.GetEntryAtIndex(2)->GetPageID());
SiteInstance* instance3 =
GetSiteInstanceFromEntry(other_controller.GetEntryAtIndex(2));
EXPECT_NE(instance3, instance1);
EXPECT_FALSE(instance3->IsRelatedSiteInstance(instance1));
EXPECT_EQ(1, other_contents->GetMaxPageIDForSiteInstance(instance1));
EXPECT_EQ(0, other_contents->GetMaxPageIDForSiteInstance(instance3));
}
TEST_F(NavigationControllerTest, CopyStateFromAndPrune2) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo1");
const GURL url2("http://foo2");
const GURL url3("http://foo3");
NavigateAndCommit(url1);
NavigateAndCommit(url2);
controller.GoBack();
contents()->CommitPendingNavigation();
scoped_ptr<TestWebContents> other_contents(
static_cast<TestWebContents*>(CreateTestWebContents()));
NavigationControllerImpl& other_controller = other_contents->GetController();
other_contents->NavigateAndCommit(url3);
other_contents->ExpectSetHistoryLengthAndPrune(
GetSiteInstanceFromEntry(other_controller.GetEntryAtIndex(0)), 1,
other_controller.GetEntryAtIndex(0)->GetPageID());
other_controller.CopyStateFromAndPrune(&controller, false);
ASSERT_EQ(2, other_controller.GetEntryCount());
ASSERT_EQ(1, other_controller.GetCurrentEntryIndex());
EXPECT_EQ(url1, other_controller.GetEntryAtIndex(0)->GetURL());
EXPECT_EQ(url3, other_controller.GetEntryAtIndex(1)->GetURL());
EXPECT_EQ(0, other_controller.GetEntryAtIndex(1)->GetPageID());
SiteInstance* instance1 =
GetSiteInstanceFromEntry(other_controller.GetEntryAtIndex(1));
EXPECT_EQ(0, other_contents->GetMaxPageIDForSiteInstance(instance1));
}
TEST_F(NavigationControllerTest, CopyStateFromAndPrune3) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo1");
const GURL url2("http://foo2");
const GURL url3("http://foo3");
const GURL url4("http://foo4");
NavigateAndCommit(url1);
NavigateAndCommit(url2);
scoped_ptr<TestWebContents> other_contents(
static_cast<TestWebContents*>(CreateTestWebContents()));
NavigationControllerImpl& other_controller = other_contents->GetController();
other_contents->NavigateAndCommit(url3);
other_contents->NavigateAndCommit(url4);
other_contents->ExpectSetHistoryLengthAndPrune(
GetSiteInstanceFromEntry(other_controller.GetEntryAtIndex(1)), 2,
other_controller.GetEntryAtIndex(0)->GetPageID());
other_controller.CopyStateFromAndPrune(&controller, false);
ASSERT_EQ(3, other_controller.GetEntryCount());
ASSERT_EQ(2, other_controller.GetCurrentEntryIndex());
EXPECT_EQ(url1, other_controller.GetEntryAtIndex(0)->GetURL());
EXPECT_EQ(url2, other_controller.GetEntryAtIndex(1)->GetURL());
EXPECT_EQ(url4, other_controller.GetEntryAtIndex(2)->GetURL());
SiteInstance* instance1 =
GetSiteInstanceFromEntry(other_controller.GetEntryAtIndex(2));
EXPECT_EQ(0, other_contents->GetMaxPageIDForSiteInstance(instance1));
}
TEST_F(NavigationControllerTest, CopyStateFromAndPruneNotLast) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo1");
const GURL url2("http://foo2");
const GURL url3("http://foo3");
const GURL url4("http://foo4");
NavigateAndCommit(url1);
NavigateAndCommit(url2);
scoped_ptr<TestWebContents> other_contents(
static_cast<TestWebContents*>(CreateTestWebContents()));
NavigationControllerImpl& other_controller = other_contents->GetController();
other_contents->NavigateAndCommit(url3);
other_contents->NavigateAndCommit(url4);
other_controller.GoBack();
other_contents->CommitPendingNavigation();
other_contents->ExpectSetHistoryLengthAndPrune(
GetSiteInstanceFromEntry(other_controller.GetEntryAtIndex(0)), 2,
other_controller.GetEntryAtIndex(0)->GetPageID());
other_controller.CopyStateFromAndPrune(&controller, false);
ASSERT_EQ(3, other_controller.GetEntryCount());
ASSERT_EQ(2, other_controller.GetCurrentEntryIndex());
EXPECT_EQ(url1, other_controller.GetEntryAtIndex(0)->GetURL());
EXPECT_EQ(url2, other_controller.GetEntryAtIndex(1)->GetURL());
EXPECT_EQ(url3, other_controller.GetEntryAtIndex(2)->GetURL());
SiteInstance* instance1 =
GetSiteInstanceFromEntry(other_controller.GetEntryAtIndex(2));
EXPECT_EQ(0, other_contents->GetMaxPageIDForSiteInstance(instance1));
}
TEST_F(NavigationControllerTest, CopyStateFromAndPruneTargetPending) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo1");
const GURL url2("http://foo2");
const GURL url3("http://foo3");
const GURL url4("http://foo4");
NavigateAndCommit(url1);
NavigateAndCommit(url2);
controller.GoBack();
contents()->CommitPendingNavigation();
scoped_ptr<TestWebContents> other_contents(
static_cast<TestWebContents*>(CreateTestWebContents()));
NavigationControllerImpl& other_controller = other_contents->GetController();
other_contents->NavigateAndCommit(url3);
other_controller.LoadURL(
url4, Referrer(), PAGE_TRANSITION_TYPED, std::string());
other_contents->ExpectSetHistoryLengthAndPrune(
GetSiteInstanceFromEntry(other_controller.GetEntryAtIndex(0)), 1,
other_controller.GetEntryAtIndex(0)->GetPageID());
other_controller.CopyStateFromAndPrune(&controller, false);
ASSERT_EQ(2, other_controller.GetEntryCount());
EXPECT_EQ(1, other_controller.GetCurrentEntryIndex());
EXPECT_EQ(url1, other_controller.GetEntryAtIndex(0)->GetURL());
EXPECT_EQ(url3, other_controller.GetEntryAtIndex(1)->GetURL());
ASSERT_TRUE(other_controller.GetPendingEntry());
EXPECT_EQ(url4, other_controller.GetPendingEntry()->GetURL());
SiteInstance* instance1 =
GetSiteInstanceFromEntry(other_controller.GetEntryAtIndex(0));
EXPECT_EQ(0, other_contents->GetMaxPageIDForSiteInstance(instance1));
}
TEST_F(NavigationControllerTest, CopyStateFromAndPruneTargetPending2) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo1");
const GURL url2a("http://foo2/a");
const GURL url2b("http://foo2/b");
NavigateAndCommit(url1);
scoped_ptr<TestWebContents> other_contents(
static_cast<TestWebContents*>(CreateTestWebContents()));
NavigationControllerImpl& other_controller = other_contents->GetController();
other_contents->NavigateAndCommit(url2a);
other_controller.LoadURL(
url2b, Referrer(), PAGE_TRANSITION_LINK, std::string());
other_controller.GetPendingEntry()->SetPageID(
other_controller.GetLastCommittedEntry()->GetPageID());
other_contents->ExpectSetHistoryLengthAndPrune(
GetSiteInstanceFromEntry(other_controller.GetEntryAtIndex(0)), 1,
other_controller.GetEntryAtIndex(0)->GetPageID());
other_controller.CopyStateFromAndPrune(&controller, false);
ASSERT_EQ(2, other_controller.GetEntryCount());
EXPECT_EQ(1, other_controller.GetCurrentEntryIndex());
EXPECT_EQ(url1, other_controller.GetEntryAtIndex(0)->GetURL());
EXPECT_EQ(url2a, other_controller.GetEntryAtIndex(1)->GetURL());
ASSERT_TRUE(other_controller.GetPendingEntry());
EXPECT_EQ(url2b, other_controller.GetPendingEntry()->GetURL());
other_contents->CommitPendingNavigation();
SiteInstance* instance1 =
GetSiteInstanceFromEntry(other_controller.GetEntryAtIndex(1));
EXPECT_EQ(0, other_contents->GetMaxPageIDForSiteInstance(instance1));
}
TEST_F(NavigationControllerTest, CopyStateFromAndPruneSourcePending) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo1");
const GURL url2("http://foo2");
const GURL url3("http://foo3");
NavigateAndCommit(url1);
NavigateAndCommit(url2);
controller.GoBack();
scoped_ptr<TestWebContents> other_contents(
static_cast<TestWebContents*>(CreateTestWebContents()));
NavigationControllerImpl& other_controller = other_contents->GetController();
other_contents->NavigateAndCommit(url3);
other_contents->ExpectSetHistoryLengthAndPrune(
GetSiteInstanceFromEntry(other_controller.GetEntryAtIndex(0)), 2,
other_controller.GetEntryAtIndex(0)->GetPageID());
other_controller.CopyStateFromAndPrune(&controller, false);
ASSERT_EQ(3, other_controller.GetEntryCount());
ASSERT_EQ(2, other_controller.GetCurrentEntryIndex());
EXPECT_EQ(url1, other_controller.GetEntryAtIndex(0)->GetURL());
EXPECT_EQ(url2, other_controller.GetEntryAtIndex(1)->GetURL());
EXPECT_EQ(url3, other_controller.GetEntryAtIndex(2)->GetURL());
EXPECT_EQ(0, other_controller.GetEntryAtIndex(2)->GetPageID());
SiteInstance* instance1 =
GetSiteInstanceFromEntry(other_controller.GetEntryAtIndex(2));
EXPECT_EQ(0, other_contents->GetMaxPageIDForSiteInstance(instance1));
}
TEST_F(NavigationControllerTest, CopyStateFromAndPruneMaxEntries) {
NavigationControllerImpl& controller = controller_impl();
size_t original_count = NavigationControllerImpl::max_entry_count();
const int kMaxEntryCount = 3;
NavigationControllerImpl::set_max_entry_count_for_testing(kMaxEntryCount);
const GURL url1("http://foo/1");
const GURL url2("http://foo/2");
const GURL url3("http://foo/3");
const GURL url4("http://foo/4");
PrunedListener listener(&controller);
NavigateAndCommit(url1);
NavigateAndCommit(url2);
NavigateAndCommit(url3);
scoped_ptr<TestWebContents> other_contents(
static_cast<TestWebContents*>(CreateTestWebContents()));
NavigationControllerImpl& other_controller = other_contents->GetController();
other_contents->NavigateAndCommit(url4);
other_contents->ExpectSetHistoryLengthAndPrune(
GetSiteInstanceFromEntry(other_controller.GetEntryAtIndex(0)), 2,
other_controller.GetEntryAtIndex(0)->GetPageID());
other_controller.CopyStateFromAndPrune(&controller, false);
EXPECT_EQ(1, listener.notification_count_);
EXPECT_TRUE(listener.details_.from_front);
EXPECT_EQ(1, listener.details_.count);
ASSERT_EQ(3, other_controller.GetEntryCount());
ASSERT_EQ(2, other_controller.GetCurrentEntryIndex());
EXPECT_EQ(url2, other_controller.GetEntryAtIndex(0)->GetURL());
EXPECT_EQ(url3, other_controller.GetEntryAtIndex(1)->GetURL());
EXPECT_EQ(url4, other_controller.GetEntryAtIndex(2)->GetURL());
EXPECT_EQ(1, other_controller.GetEntryAtIndex(0)->GetPageID());
EXPECT_EQ(2, other_controller.GetEntryAtIndex(1)->GetPageID());
EXPECT_EQ(0, other_controller.GetEntryAtIndex(2)->GetPageID());
NavigationControllerImpl::set_max_entry_count_for_testing(original_count);
}
TEST_F(NavigationControllerTest, CopyStateFromAndPruneReplaceEntry) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo/1");
const GURL url2("http://foo/2");
const GURL url3("http://foo/3");
NavigateAndCommit(url1);
NavigateAndCommit(url2);
SiteInstance* instance1 =
GetSiteInstanceFromEntry(controller.GetEntryAtIndex(0));
SiteInstance* instance2 =
GetSiteInstanceFromEntry(controller.GetEntryAtIndex(1));
EXPECT_EQ(instance1, instance2);
EXPECT_EQ(0, controller.GetEntryAtIndex(0)->GetPageID());
EXPECT_EQ(1, controller.GetEntryAtIndex(1)->GetPageID());
EXPECT_EQ(1, contents()->GetMaxPageIDForSiteInstance(instance1));
scoped_ptr<TestWebContents> other_contents(
static_cast<TestWebContents*>(CreateTestWebContents()));
NavigationControllerImpl& other_controller = other_contents->GetController();
other_contents->NavigateAndCommit(url3);
other_contents->ExpectSetHistoryLengthAndPrune(
GetSiteInstanceFromEntry(other_controller.GetEntryAtIndex(0)), 1,
other_controller.GetEntryAtIndex(0)->GetPageID());
other_controller.CopyStateFromAndPrune(&controller, true);
ASSERT_EQ(2, other_controller.GetEntryCount());
ASSERT_EQ(1, other_controller.GetCurrentEntryIndex());
EXPECT_EQ(url1, other_controller.GetEntryAtIndex(0)->GetURL());
EXPECT_EQ(url3, other_controller.GetEntryAtIndex(1)->GetURL());
EXPECT_EQ(0, other_controller.GetEntryAtIndex(0)->GetPageID());
EXPECT_EQ(0, other_controller.GetEntryAtIndex(1)->GetPageID());
SiteInstance* instance3 =
GetSiteInstanceFromEntry(other_controller.GetEntryAtIndex(1));
EXPECT_NE(instance3, instance1);
EXPECT_FALSE(instance3->IsRelatedSiteInstance(instance1));
EXPECT_EQ(1, other_contents->GetMaxPageIDForSiteInstance(instance1));
EXPECT_EQ(0, other_contents->GetMaxPageIDForSiteInstance(instance3));
}
TEST_F(NavigationControllerTest, CopyStateFromAndPruneMaxEntriesReplaceEntry) {
NavigationControllerImpl& controller = controller_impl();
size_t original_count = NavigationControllerImpl::max_entry_count();
const int kMaxEntryCount = 3;
NavigationControllerImpl::set_max_entry_count_for_testing(kMaxEntryCount);
const GURL url1("http://foo/1");
const GURL url2("http://foo/2");
const GURL url3("http://foo/3");
const GURL url4("http://foo/4");
PrunedListener listener(&controller);
NavigateAndCommit(url1);
NavigateAndCommit(url2);
NavigateAndCommit(url3);
scoped_ptr<TestWebContents> other_contents(
static_cast<TestWebContents*>(CreateTestWebContents()));
NavigationControllerImpl& other_controller = other_contents->GetController();
other_contents->NavigateAndCommit(url4);
other_contents->ExpectSetHistoryLengthAndPrune(
GetSiteInstanceFromEntry(other_controller.GetEntryAtIndex(0)), 2,
other_controller.GetEntryAtIndex(0)->GetPageID());
other_controller.CopyStateFromAndPrune(&controller, true);
EXPECT_EQ(0, listener.notification_count_);
ASSERT_EQ(3, other_controller.GetEntryCount());
ASSERT_EQ(2, other_controller.GetCurrentEntryIndex());
EXPECT_EQ(url1, other_controller.GetEntryAtIndex(0)->GetURL());
EXPECT_EQ(url2, other_controller.GetEntryAtIndex(1)->GetURL());
EXPECT_EQ(url4, other_controller.GetEntryAtIndex(2)->GetURL());
EXPECT_EQ(0, other_controller.GetEntryAtIndex(0)->GetPageID());
EXPECT_EQ(1, other_controller.GetEntryAtIndex(1)->GetPageID());
EXPECT_EQ(0, other_controller.GetEntryAtIndex(2)->GetPageID());
NavigationControllerImpl::set_max_entry_count_for_testing(original_count);
}
TEST_F(NavigationControllerTest, HistoryNavigate) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo/1");
const GURL url2("http://foo/2");
const GURL url3("http://foo/3");
NavigateAndCommit(url1);
NavigateAndCommit(url2);
NavigateAndCommit(url3);
controller.GoBack();
contents()->CommitPendingNavigation();
contents()->OnGoToEntryAtOffset(-1);
EXPECT_EQ(0, controller.GetPendingEntryIndex());
contents()->ProceedWithCrossSiteNavigation();
const IPC::Message* message =
process()->sink().GetFirstMessageMatching(FrameMsg_Navigate::ID);
ASSERT_TRUE(message != NULL);
Tuple1<FrameMsg_Navigate_Params> nav_params;
FrameMsg_Navigate::Read(message, &nav_params);
EXPECT_EQ(url1, nav_params.a.url);
process()->sink().ClearMessages();
contents()->OnGoToEntryAtOffset(2);
EXPECT_EQ(2, controller.GetPendingEntryIndex());
contents()->ProceedWithCrossSiteNavigation();
message = process()->sink().GetFirstMessageMatching(FrameMsg_Navigate::ID);
ASSERT_TRUE(message != NULL);
FrameMsg_Navigate::Read(message, &nav_params);
EXPECT_EQ(url3, nav_params.a.url);
process()->sink().ClearMessages();
controller.DiscardNonCommittedEntries();
contents()->OnGoToEntryAtOffset(120);
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
message = process()->sink().GetFirstMessageMatching(FrameMsg_Navigate::ID);
EXPECT_TRUE(message == NULL);
}
TEST_F(NavigationControllerTest, PruneAllButLastCommittedForSingle) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo1");
NavigateAndCommit(url1);
contents()->ExpectSetHistoryLengthAndPrune(
GetSiteInstanceFromEntry(controller.GetEntryAtIndex(0)), 0,
controller.GetEntryAtIndex(0)->GetPageID());
controller.PruneAllButLastCommitted();
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_EQ(controller.GetEntryAtIndex(0)->GetURL(), url1);
}
TEST_F(NavigationControllerTest, PruneAllButLastCommittedForFirst) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo/1");
const GURL url2("http://foo/2");
const GURL url3("http://foo/3");
NavigateAndCommit(url1);
NavigateAndCommit(url2);
NavigateAndCommit(url3);
controller.GoBack();
controller.GoBack();
contents()->CommitPendingNavigation();
contents()->ExpectSetHistoryLengthAndPrune(
GetSiteInstanceFromEntry(controller.GetEntryAtIndex(0)), 0,
controller.GetEntryAtIndex(0)->GetPageID());
controller.PruneAllButLastCommitted();
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_EQ(controller.GetEntryAtIndex(0)->GetURL(), url1);
}
TEST_F(NavigationControllerTest, PruneAllButLastCommittedForIntermediate) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo/1");
const GURL url2("http://foo/2");
const GURL url3("http://foo/3");
NavigateAndCommit(url1);
NavigateAndCommit(url2);
NavigateAndCommit(url3);
controller.GoBack();
contents()->CommitPendingNavigation();
contents()->ExpectSetHistoryLengthAndPrune(
GetSiteInstanceFromEntry(controller.GetEntryAtIndex(1)), 0,
controller.GetEntryAtIndex(1)->GetPageID());
controller.PruneAllButLastCommitted();
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_EQ(controller.GetEntryAtIndex(0)->GetURL(), url2);
}
TEST_F(NavigationControllerTest, PruneAllButLastCommittedForPendingNotInList) {
NavigationControllerImpl& controller = controller_impl();
const GURL url1("http://foo/1");
const GURL url2("http://foo/2");
const GURL url3("http://foo/3");
NavigateAndCommit(url1);
NavigateAndCommit(url2);
controller.LoadURL(
url3, Referrer(), PAGE_TRANSITION_TYPED, std::string());
EXPECT_TRUE(controller.GetPendingEntry());
EXPECT_EQ(2, controller.GetEntryCount());
contents()->ExpectSetHistoryLengthAndPrune(
NULL, 0, controller.GetPendingEntry()->GetPageID());
controller.PruneAllButLastCommitted();
EXPECT_EQ(0, controller.GetLastCommittedEntryIndex());
EXPECT_EQ(url2, controller.GetEntryAtIndex(0)->GetURL());
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_TRUE(controller.GetPendingEntry());
EXPECT_EQ(1, controller.GetEntryCount());
main_test_rfh()->SendNavigate(2, url3);
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_FALSE(controller.GetPendingEntry());
EXPECT_EQ(2, controller.GetEntryCount());
EXPECT_EQ(url3, controller.GetEntryAtIndex(1)->GetURL());
}
TEST_F(NavigationControllerTest, StopOnHistoryNavigationToCurrentPage) {
NavigationControllerImpl& controller = controller_impl();
const GURL url0("http://foo/0");
const GURL url1("http://foo/1");
NavigateAndCommit(url0);
NavigateAndCommit(url1);
controller.GoBack();
contents()->CommitPendingNavigation();
controller.GoForward();
EXPECT_EQ(1, controller.GetPendingEntryIndex());
controller.GoBack();
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
}
TEST_F(NavigationControllerTest, IsInitialNavigation) {
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
EXPECT_TRUE(controller.IsInitialNavigation());
const GURL url1("http://foo1");
main_test_rfh()->SendNavigate(0, url1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
EXPECT_FALSE(controller.IsInitialNavigation());
const GURL url2("http://foo2");
controller.LoadURL(
url2, Referrer(), PAGE_TRANSITION_TYPED, std::string());
}
TEST_F(NavigationControllerTest, ClearFaviconOnRedirect) {
const GURL kPageWithFavicon("http://withfavicon.html");
const GURL kPageWithoutFavicon("http://withoutfavicon.html");
const GURL kIconURL("http://withfavicon.ico");
const gfx::Image kDefaultFavicon = FaviconStatus().image;
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
main_test_rfh()->SendNavigate(0, kPageWithFavicon);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
NavigationEntry* entry = controller.GetLastCommittedEntry();
EXPECT_TRUE(entry);
EXPECT_EQ(kPageWithFavicon, entry->GetURL());
content::FaviconStatus& favicon_status = entry->GetFavicon();
favicon_status.image = CreateImage(SK_ColorWHITE);
favicon_status.url = kIconURL;
favicon_status.valid = true;
EXPECT_FALSE(DoImagesMatch(kDefaultFavicon, entry->GetFavicon().image));
main_test_rfh()->SendNavigateWithTransition(
0,
kPageWithoutFavicon,
PAGE_TRANSITION_CLIENT_REDIRECT);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
entry = controller.GetLastCommittedEntry();
EXPECT_TRUE(entry);
EXPECT_EQ(kPageWithoutFavicon, entry->GetURL());
EXPECT_TRUE(DoImagesMatch(kDefaultFavicon, entry->GetFavicon().image));
}
TEST_F(NavigationControllerTest, BackNavigationDoesNotClearFavicon) {
const GURL kUrl1("http://www.a.com/1");
const GURL kUrl2("http://www.a.com/2");
const GURL kIconURL("http://www.a.com/1/favicon.ico");
NavigationControllerImpl& controller = controller_impl();
TestNotificationTracker notifications;
RegisterForAllNavNotifications(¬ifications, &controller);
main_test_rfh()->SendNavigate(0, kUrl1);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
gfx::Image favicon_image = CreateImage(SK_ColorWHITE);
content::NavigationEntry* entry = controller.GetLastCommittedEntry();
EXPECT_TRUE(entry);
content::FaviconStatus& favicon_status = entry->GetFavicon();
favicon_status.image = favicon_image;
favicon_status.url = kIconURL;
favicon_status.valid = true;
main_test_rfh()->SendNavigate(1, kUrl2);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
main_test_rfh()->SendNavigateWithTransition(
0,
kUrl1,
PAGE_TRANSITION_FORWARD_BACK);
EXPECT_EQ(1U, navigation_entry_committed_counter_);
navigation_entry_committed_counter_ = 0;
entry = controller.GetEntryAtIndex(0);
EXPECT_TRUE(entry);
EXPECT_EQ(kUrl1, entry->GetURL());
EXPECT_TRUE(DoImagesMatch(favicon_image, entry->GetFavicon().image));
}
#if defined(OS_ANDROID)
#define MAYBE_PurgeScreenshot DISABLED_PurgeScreenshot
#else
#define MAYBE_PurgeScreenshot PurgeScreenshot
#endif
TEST_F(NavigationControllerTest, MAYBE_PurgeScreenshot) {
NavigationControllerImpl& controller = controller_impl();
NavigationEntryImpl* entry;
for (int i = 0; i < 12; ++i) {
const GURL url(base::StringPrintf("http://foo%d/", i));
NavigateAndCommit(url);
EXPECT_EQ(i, controller.GetCurrentEntryIndex());
}
MockScreenshotManager* screenshot_manager =
new MockScreenshotManager(&controller);
controller.SetScreenshotManager(screenshot_manager);
for (int i = 0; i < controller.GetEntryCount(); ++i) {
entry = NavigationEntryImpl::FromNavigationEntry(
controller.GetEntryAtIndex(i));
screenshot_manager->TakeScreenshotFor(entry);
EXPECT_TRUE(entry->screenshot().get());
}
NavigateAndCommit(GURL("https://foo/"));
EXPECT_EQ(13, controller.GetEntryCount());
entry = NavigationEntryImpl::FromNavigationEntry(
controller.GetEntryAtIndex(11));
screenshot_manager->TakeScreenshotFor(entry);
for (int i = 0; i < 2; ++i) {
entry = NavigationEntryImpl::FromNavigationEntry(
controller.GetEntryAtIndex(i));
EXPECT_FALSE(entry->screenshot().get()) << "Screenshot " << i
<< " not purged";
}
for (int i = 2; i < controller.GetEntryCount() - 1; ++i) {
entry = NavigationEntryImpl::FromNavigationEntry(
controller.GetEntryAtIndex(i));
EXPECT_TRUE(entry->screenshot().get()) << "Screenshot not found for " << i;
}
controller.GoToIndex(5);
contents()->CommitPendingNavigation();
EXPECT_EQ(5, controller.GetCurrentEntryIndex());
for (int i = 0; i < controller.GetEntryCount() - 1; ++i) {
entry = NavigationEntryImpl::FromNavigationEntry(
controller.GetEntryAtIndex(i));
screenshot_manager->TakeScreenshotFor(entry);
}
for (int i = 10; i <= 12; ++i) {
entry = NavigationEntryImpl::FromNavigationEntry(
controller.GetEntryAtIndex(i));
EXPECT_FALSE(entry->screenshot().get()) << "Screenshot " << i
<< " not purged";
screenshot_manager->TakeScreenshotFor(entry);
}
controller.GoToIndex(7);
contents()->CommitPendingNavigation();
EXPECT_EQ(7, controller.GetCurrentEntryIndex());
for (int i = 0; i < controller.GetEntryCount() - 1; ++i) {
entry = NavigationEntryImpl::FromNavigationEntry(
controller.GetEntryAtIndex(i));
screenshot_manager->TakeScreenshotFor(entry);
}
for (int i = 0; i < 2; ++i) {
entry = NavigationEntryImpl::FromNavigationEntry(
controller.GetEntryAtIndex(i));
EXPECT_FALSE(entry->screenshot().get()) << "Screenshot " << i
<< " not purged";
}
EXPECT_EQ(13, controller.GetEntryCount());
EXPECT_EQ(10, screenshot_manager->GetScreenshotCount());
controller.ClearAllScreenshots();
EXPECT_EQ(0, screenshot_manager->GetScreenshotCount());
for (int i = 0; i < controller.GetEntryCount(); ++i) {
entry = NavigationEntryImpl::FromNavigationEntry(
controller.GetEntryAtIndex(i));
EXPECT_FALSE(entry->screenshot().get()) << "Screenshot " << i
<< " not cleared";
}
}
TEST_F(NavigationControllerTest, ClearHistoryList) {
const GURL url1("http://foo1");
const GURL url2("http://foo2");
const GURL url3("http://foo3");
const GURL url4("http://foo4");
NavigationControllerImpl& controller = controller_impl();
NavigateAndCommit(url1);
NavigateAndCommit(url2);
NavigateAndCommit(url3);
controller.GoBack();
contents()->CommitPendingNavigation();
EXPECT_EQ(3, controller.GetEntryCount());
EXPECT_EQ(1, controller.GetCurrentEntryIndex());
NavigationController::LoadURLParams params(url4);
params.should_clear_history_list = true;
controller.LoadURLWithParams(params);
NavigationEntryImpl* entry =
NavigationEntryImpl::FromNavigationEntry(
controller.GetPendingEntry());
ASSERT_TRUE(entry);
EXPECT_TRUE(entry->should_clear_history_list());
static_cast<TestRenderViewHost*>(contents()->GetPendingRenderViewHost())->
set_simulate_history_list_was_cleared(true);
contents()->CommitPendingNavigation();
EXPECT_EQ(1, controller.GetEntryCount());
EXPECT_EQ(0, controller.GetCurrentEntryIndex());
EXPECT_EQ(0, controller.GetLastCommittedEntryIndex());
EXPECT_EQ(-1, controller.GetPendingEntryIndex());
EXPECT_FALSE(controller.CanGoBack());
EXPECT_FALSE(controller.CanGoForward());
EXPECT_EQ(url4, controller.GetVisibleEntry()->GetURL());
}
}