Commit a170e4db authored by Danyao Wang's avatar Danyao Wang Committed by Commit Bot

[Nav Experiment] Enable NavigationManagerTest for WK-based nav manager.

This uncovered a bug where GetLastCommittedItemIndex() returned the
incorrect value because it assumes WKBackForwardList's |currentItem|
is always the last committed item. This is not the case if the pending
navigation is a back-forward navigation. It turned out that this can't
be fixed 100%, but implemented a heurstic and documented the edge case.

Bug: 734150
Change-Id: Ie2c026a2c5231cbd4151c106cd0db664dcf6d587
Reviewed-on: https://chromium-review.googlesource.com/596828Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Commit-Queue: Danyao Wang <danyao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#491749}
parent 42544c12
......@@ -4,18 +4,23 @@
#import "ios/web/navigation/navigation_manager_impl.h"
#include <string>
#include "base/logging.h"
#include "base/mac/bind_objc_block.h"
#include "base/strings/sys_string_conversions.h"
#import "ios/web/navigation/crw_session_controller+private_constructors.h"
#import "ios/web/navigation/legacy_navigation_manager_impl.h"
#import "ios/web/navigation/navigation_manager_delegate.h"
#import "ios/web/navigation/wk_based_navigation_manager_impl.h"
#include "ios/web/public/navigation_item.h"
#include "ios/web/public/test/fakes/test_browser_state.h"
#import "ios/web/test/fakes/crw_test_back_forward_list.h"
#include "ios/web/test/test_url_constants.h"
#import "ios/web/web_state/ui/crw_web_view_navigation_proxy.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#include "third_party/ocmock/OCMock/OCMock.h"
#include "url/scheme_host_port.h"
#include "url/url_util.h"
......@@ -59,6 +64,7 @@ class TestNavigationManagerDelegate : public NavigationManagerDelegate {
void SetSessionController(CRWSessionController* session_controller) {
session_controller_ = session_controller;
}
void SetWKWebView(id web_view) { mock_web_view_ = web_view; }
private:
// NavigationManagerDelegate overrides.
......@@ -72,11 +78,12 @@ class TestNavigationManagerDelegate : public NavigationManagerDelegate {
void OnNavigationItemCommitted(const LoadCommittedDetails&) override {}
WebState* GetWebState() override { return nullptr; }
id<CRWWebViewNavigationProxy> GetWebViewNavigationProxy() const override {
return nil;
return mock_web_view_;
}
bool reload_called_ = false;
CRWSessionController* session_controller_;
id mock_web_view_;
};
} // namespace
......@@ -103,6 +110,10 @@ class NavigationManagerTest
delegate_.SetSessionController(session_controller());
} else {
manager_.reset(new WKBasedNavigationManagerImpl);
mock_web_view_ = OCMClassMock([WKWebView class]);
mock_wk_list_ = [[CRWTestBackForwardList alloc] init];
OCMStub([mock_web_view_ backForwardList]).andReturn(mock_wk_list_);
delegate_.SetWKWebView(mock_web_view_);
}
// Setup rewriter.
BrowserURLRewriter::GetInstance()->AddURLRewriter(UrlRewriter);
......@@ -112,6 +123,7 @@ class NavigationManagerTest
manager_->SetBrowserState(&browser_state_);
manager_->SetSessionController(controller_);
}
CRWSessionController* session_controller() { return controller_; }
NavigationManagerImpl* navigation_manager() { return manager_.get(); }
......@@ -119,6 +131,10 @@ class NavigationManagerTest
return delegate_;
}
protected:
CRWTestBackForwardList* mock_wk_list_;
WKWebView* mock_web_view_;
private:
TestBrowserState browser_state_;
TestNavigationManagerDelegate delegate_;
......@@ -142,7 +158,10 @@ TEST_P(NavigationManagerTest, GetPendingItemIndexWithoutPendingEntry) {
GURL("http://www.url.com"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com"];
navigation_manager()->CommitPendingItem();
EXPECT_EQ(-1, navigation_manager()->GetPendingItemIndex());
}
......@@ -153,7 +172,10 @@ TEST_P(NavigationManagerTest, GetPendingItemIndexWithPendingEntry) {
GURL("http://www.url.com"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com"];
navigation_manager()->CommitPendingItem();
navigation_manager()->AddPendingItem(
GURL("http://www.url.com/0"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
......@@ -168,18 +190,25 @@ TEST_P(NavigationManagerTest, GetPendingItemIndexWithIndexedPendingEntry) {
GURL("http://www.url.com"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com"];
navigation_manager()->CommitPendingItem();
navigation_manager()->AddPendingItem(
GURL("http://www.url.com/0"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com/0"
backListURLs:@[ @"http://www.url.com" ]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
EXPECT_EQ(-1, navigation_manager()->GetPendingItemIndex());
if (GetParam() == TEST_LEGACY_NAVIGATION_MANAGER) {
[session_controller() setPendingItemIndex:0];
}
EXPECT_EQ(0, navigation_manager()->GetPendingItemIndex());
}
}
// Tests that going back or negative offset is not possible without a committed
......@@ -209,7 +238,15 @@ TEST_P(NavigationManagerTest, CanGoBackWithTransientItemAndCommittedItem) {
GURL("http://www.url.com"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com"];
navigation_manager()->CommitPendingItem();
// Setup a pending item for which a transient item can be added.
navigation_manager()->AddPendingItem(
GURL("http://www.2.com"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
navigation_manager()->AddTransientItem(GURL("http://www.url.com/0"));
EXPECT_TRUE(navigation_manager()->CanGoBack());
......@@ -223,6 +260,8 @@ TEST_P(NavigationManagerTest, CanGoBackWithSingleCommitedItem) {
GURL("http://www.url.com"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com"];
navigation_manager()->CommitPendingItem();
EXPECT_FALSE(navigation_manager()->CanGoBack());
......@@ -235,21 +274,38 @@ TEST_P(NavigationManagerTest, CanGoBackWithMultipleCommitedItems) {
GURL("http://www.url.com"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com"];
navigation_manager()->CommitPendingItem();
navigation_manager()->AddPendingItem(
GURL("http://www.url.com/0"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com/0"
backListURLs:@[ @"http://www.url.com" ]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
navigation_manager()->AddPendingItem(
GURL("http://www.url.com/1"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_
setCurrentURL:@"http://www.url.com/1"
backListURLs:@[ @"http://www.url.com", @"http://www.url.com/0" ]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
EXPECT_TRUE(navigation_manager()->CanGoBack());
EXPECT_TRUE(navigation_manager()->CanGoToOffset(-1));
if (GetParam() == TEST_WK_BASED_NAVIGATION_MANAGER) {
// TODO(crbug.com/734150): Enable this test once |GoToIndex| is
// implemented in WKBasedNavigationManager.
return;
}
navigation_manager()->GoToIndex(1);
EXPECT_TRUE(navigation_manager()->CanGoBack());
EXPECT_TRUE(navigation_manager()->CanGoToOffset(-1));
......@@ -277,6 +333,7 @@ TEST_P(NavigationManagerTest, CanGoForwardWithSingleCommitedItem) {
GURL("http://www.url.com"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com/"];
navigation_manager()->CommitPendingItem();
EXPECT_FALSE(navigation_manager()->CanGoForward());
......@@ -289,21 +346,39 @@ TEST_P(NavigationManagerTest, CanGoForwardWithMultipleCommitedEntries) {
GURL("http://www.url.com"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com"];
navigation_manager()->CommitPendingItem();
navigation_manager()->AddPendingItem(
GURL("http://www.url.com/0"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com/0"
backListURLs:@[ @"http://www.url.com" ]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
navigation_manager()->AddPendingItem(
GURL("http://www.url.com/1"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_
setCurrentURL:@"http://www.url.com/1"
backListURLs:@[ @"http://www.url.com", @"http://www.url.com/0" ]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
EXPECT_FALSE(navigation_manager()->CanGoForward());
EXPECT_FALSE(navigation_manager()->CanGoToOffset(1));
if (GetParam() == TEST_WK_BASED_NAVIGATION_MANAGER) {
// TODO(crbug.com/734150): Enable this test once |GoToIndex| is
// implemented in WKBasedNavigationManager.
return;
}
navigation_manager()->GoToIndex(1);
EXPECT_TRUE(navigation_manager()->CanGoForward());
EXPECT_TRUE(navigation_manager()->CanGoToOffset(1));
......@@ -329,32 +404,68 @@ TEST_P(NavigationManagerTest, OffsetsWithoutPendingIndex) {
GURL("http://www.url.com/0"), Referrer(), ui::PAGE_TRANSITION_LINK,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com/0"];
navigation_manager()->CommitPendingItem();
navigation_manager()->AddPendingItem(
GURL("http://www.url.com/redirect"), Referrer(),
ui::PAGE_TRANSITION_CLIENT_REDIRECT,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com/redirect"
backListURLs:@[ @"http://www.url.com/0" ]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
navigation_manager()->AddPendingItem(
GURL("http://www.url.com/1"), Referrer(), ui::PAGE_TRANSITION_LINK,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com/1"
backListURLs:@[
@"http://www.url.com/0", @"http://www.url.com/redirect"
]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
navigation_manager()->AddPendingItem(
GURL("http://www.url.com/2"), Referrer(), ui::PAGE_TRANSITION_LINK,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com/2"
backListURLs:@[
@"http://www.url.com/0", @"http://www.url.com/redirect",
@"http://www.url.com/1"
]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
navigation_manager()->AddPendingItem(
GURL("http://www.url.com/redirect"), Referrer(),
ui::PAGE_TRANSITION_CLIENT_REDIRECT,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com/redirect"
backListURLs:@[
@"http://www.url.com/0", @"http://www.url.com/redirect",
@"http://www.url.com/1", @"http://www.url.com/2"
]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
ASSERT_EQ(5, navigation_manager()->GetItemCount());
ASSERT_EQ(4, navigation_manager()->GetLastCommittedItemIndex());
if (GetParam() == TEST_WK_BASED_NAVIGATION_MANAGER) {
// TODO(crbug.com/734150): Enable this test once |GoToIndex| is
// implemented in WKBasedNavigationManager.
return;
}
// Go to entry at index 1 and test API from that state.
navigation_manager()->GoToIndex(1);
ASSERT_EQ(1, navigation_manager()->GetLastCommittedItemIndex());
......@@ -560,24 +671,60 @@ TEST_P(NavigationManagerTest, OffsetsWithoutPendingIndex) {
// forward from a pending navigation entry that is added to the middle of the
// navigation stack).
TEST_P(NavigationManagerTest, OffsetsWithPendingTransientEntry) {
// This test directly manipulates the WKBackForwardListItem mocks stored in
// mock_wk_list_ so that the associated NavigationItem objects are retained
// throughout the test case.
WKBackForwardListItem* wk_item0 =
[CRWTestBackForwardList itemWithURLString:@"http://www.url.com/0"];
WKBackForwardListItem* wk_item1 =
[CRWTestBackForwardList itemWithURLString:@"http://www.url.com/1"];
WKBackForwardListItem* wk_item2 =
[CRWTestBackForwardList itemWithURLString:@"http://www.url.com/2"];
// Create a transient item in the middle of the navigation stack and go back
// to it (pending index is 1, current index is 2).
navigation_manager()->AddPendingItem(
GURL("http://www.url.com/0"), Referrer(), ui::PAGE_TRANSITION_LINK,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
mock_wk_list_.currentItem = wk_item0;
mock_wk_list_.backList = nil;
mock_wk_list_.forwardList = nil;
navigation_manager()->CommitPendingItem();
navigation_manager()->AddPendingItem(
GURL("http://www.url.com/1"), Referrer(), ui::PAGE_TRANSITION_LINK,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
mock_wk_list_.currentItem = wk_item1;
mock_wk_list_.backList = @[ wk_item0 ];
navigation_manager()->CommitPendingItem();
navigation_manager()->AddPendingItem(
GURL("http://www.url.com/2"), Referrer(), ui::PAGE_TRANSITION_LINK,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
mock_wk_list_.currentItem = wk_item2;
mock_wk_list_.backList = @[ wk_item0, wk_item1 ];
navigation_manager()->CommitPendingItem();
// Under back-forward navigation, both WKBackForwardList and WKWebView.URL are
// updated before |didStartProvisionalNavigation| callback, which calls
// AddPendingItem. Simulate this behavior.
OCMExpect([mock_web_view_ URL])
.andReturn([NSURL URLWithString:@"http://www.url.com/1"]);
mock_wk_list_.currentItem = wk_item1;
mock_wk_list_.backList = @[ wk_item0 ];
mock_wk_list_.forwardList = @[ wk_item2 ];
navigation_manager()->AddPendingItem(
GURL("http://www.url.com/1"), Referrer(), ui::PAGE_TRANSITION_LINK,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
navigation_manager()->AddTransientItem(GURL("http://www.url.com/1"));
if (GetParam() == TEST_LEGACY_NAVIGATION_MANAGER) {
[session_controller() setPendingItemIndex:1];
}
......@@ -588,6 +735,12 @@ TEST_P(NavigationManagerTest, OffsetsWithPendingTransientEntry) {
EXPECT_EQ(2, navigation_manager()->GetIndexForOffset(1));
EXPECT_EQ(0, navigation_manager()->GetIndexForOffset(-1));
if (GetParam() == TEST_WK_BASED_NAVIGATION_MANAGER) {
// TODO(crbug.com/734150): Enable this test once |GoToIndex| is
// implemented in WKBasedNavigationManager.
return;
}
// Now go forward to that middle transient item (pending index is 1,
// current index is 0).
navigation_manager()->GoToIndex(0);
......@@ -643,9 +796,18 @@ TEST_P(NavigationManagerTest, NotReplaceSameUrlPendingItemIfNotFormSubmission) {
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
ASSERT_TRUE(navigation_manager()->GetPendingItem());
if (GetParam() == TEST_LEGACY_NAVIGATION_MANAGER) {
EXPECT_TRUE(ui::PageTransitionCoreTypeIs(
navigation_manager()->GetPendingItem()->GetTransitionType(),
ui::PAGE_TRANSITION_TYPED));
} else {
// WKBasedNavigationManager assumes that AddPendingItem() is only called for
// new navigation, so it always creates a new pending item.
EXPECT_TRUE(ui::PageTransitionCoreTypeIs(
navigation_manager()->GetPendingItem()->GetTransitionType(),
ui::PAGE_TRANSITION_LINK));
}
EXPECT_EQ(0, navigation_manager()->GetItemCount());
}
......@@ -684,6 +846,7 @@ TEST_P(NavigationManagerTest, NotReplaceSameUrlPendingItemIfOverrideInherit) {
existing_url, Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
ASSERT_TRUE(navigation_manager()->GetPendingItem());
EXPECT_TRUE(ui::PageTransitionCoreTypeIs(
navigation_manager()->GetPendingItem()->GetTransitionType(),
......@@ -691,14 +854,23 @@ TEST_P(NavigationManagerTest, NotReplaceSameUrlPendingItemIfOverrideInherit) {
EXPECT_EQ(0, navigation_manager()->GetItemCount());
navigation_manager()->AddPendingItem(
existing_url, Referrer(), ui::PAGE_TRANSITION_RELOAD,
existing_url, Referrer(), ui::PAGE_TRANSITION_LINK,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
ASSERT_TRUE(navigation_manager()->GetPendingItem());
if (GetParam() == TEST_LEGACY_NAVIGATION_MANAGER) {
EXPECT_TRUE(ui::PageTransitionCoreTypeIs(
navigation_manager()->GetPendingItem()->GetTransitionType(),
ui::PAGE_TRANSITION_TYPED));
} else {
// WKBasedNavigationManager assumes that AddPendingItem() is only called for
// new navigation, so it always creates a new pending item.
EXPECT_TRUE(ui::PageTransitionCoreTypeIs(
navigation_manager()->GetPendingItem()->GetTransitionType(),
ui::PAGE_TRANSITION_LINK));
}
EXPECT_EQ(0, navigation_manager()->GetItemCount());
}
......@@ -766,7 +938,10 @@ TEST_P(NavigationManagerTest, AddPendingItemIfDiffernetURL) {
existing_url, Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.existing.com"];
navigation_manager()->CommitPendingItem();
ASSERT_TRUE(navigation_manager()->GetLastCommittedItem());
EXPECT_EQ(existing_url,
navigation_manager()->GetLastCommittedItem()->GetURL());
......@@ -790,7 +965,10 @@ TEST_P(NavigationManagerTest, NotAddSameUrlPendingItemIfNotFormSubmission) {
existing_url, Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.existing.com"];
navigation_manager()->CommitPendingItem();
ASSERT_TRUE(navigation_manager()->GetLastCommittedItem());
EXPECT_TRUE(ui::PageTransitionCoreTypeIs(
navigation_manager()->GetLastCommittedItem()->GetTransitionType(),
......@@ -802,7 +980,17 @@ TEST_P(NavigationManagerTest, NotAddSameUrlPendingItemIfNotFormSubmission) {
existing_url, Referrer(), ui::PAGE_TRANSITION_LINK,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
if (GetParam() == TEST_LEGACY_NAVIGATION_MANAGER) {
EXPECT_FALSE(navigation_manager()->GetPendingItem());
} else {
// WKBasedNavigationManager assumes that AddPendingItem() is only called for
// new navigation, so it always creates a new pending item.
ASSERT_TRUE(navigation_manager()->GetPendingItem());
EXPECT_TRUE(ui::PageTransitionCoreTypeIs(
navigation_manager()->GetPendingItem()->GetTransitionType(),
ui::PAGE_TRANSITION_LINK));
}
EXPECT_EQ(1, navigation_manager()->GetItemCount());
}
......@@ -815,7 +1003,10 @@ TEST_P(NavigationManagerTest, AddSameUrlPendingItemIfFormSubmission) {
existing_url, Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.existing.com"];
navigation_manager()->CommitPendingItem();
ASSERT_TRUE(navigation_manager()->GetLastCommittedItem());
EXPECT_TRUE(ui::PageTransitionCoreTypeIs(
navigation_manager()->GetLastCommittedItem()->GetTransitionType(),
......@@ -840,12 +1031,20 @@ TEST_P(NavigationManagerTest, AddSameUrlPendingItemIfFormSubmission) {
// submissions.
TEST_P(NavigationManagerTest,
NotAddSameUrlPendingItemIfDuplicateFormSubmission) {
if (GetParam() == TEST_WK_BASED_NAVIGATION_MANAGER) {
// TODO(crbug.com/734150): Enable this test when form submission check is
// implemented.
return;
}
GURL existing_url = GURL("http://www.existing.com");
navigation_manager()->AddPendingItem(
existing_url, Referrer(), ui::PAGE_TRANSITION_FORM_SUBMIT,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.existing.com"];
navigation_manager()->CommitPendingItem();
ASSERT_TRUE(navigation_manager()->GetLastCommittedItem());
EXPECT_FALSE(navigation_manager()->GetPendingItem());
EXPECT_EQ(1, navigation_manager()->GetItemCount());
......@@ -866,7 +1065,10 @@ TEST_P(NavigationManagerTest, NotAddSameUrlPendingItemIfOverrideInherit) {
existing_url, Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.existing.com"];
navigation_manager()->CommitPendingItem();
ASSERT_TRUE(navigation_manager()->GetLastCommittedItem());
EXPECT_TRUE(ui::PageTransitionCoreTypeIs(
navigation_manager()->GetLastCommittedItem()->GetTransitionType(),
......@@ -874,10 +1076,20 @@ TEST_P(NavigationManagerTest, NotAddSameUrlPendingItemIfOverrideInherit) {
EXPECT_EQ(1, navigation_manager()->GetItemCount());
navigation_manager()->AddPendingItem(
existing_url, Referrer(), ui::PAGE_TRANSITION_RELOAD,
existing_url, Referrer(), ui::PAGE_TRANSITION_LINK,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
if (GetParam() == TEST_LEGACY_NAVIGATION_MANAGER) {
EXPECT_FALSE(navigation_manager()->GetPendingItem());
} else {
// WKBasedNavigationManager assumes that AddPendingItem() is only called for
// new navigation, so it always creates a new pending item.
ASSERT_TRUE(navigation_manager()->GetPendingItem());
EXPECT_TRUE(ui::PageTransitionCoreTypeIs(
navigation_manager()->GetPendingItem()->GetTransitionType(),
ui::PAGE_TRANSITION_LINK));
}
EXPECT_EQ(1, navigation_manager()->GetItemCount());
}
......@@ -889,7 +1101,10 @@ TEST_P(NavigationManagerTest, AddSameUrlPendingItemIfOverrideDesktop) {
existing_url, Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::MOBILE);
[mock_wk_list_ setCurrentURL:@"http://www.existing.com"];
navigation_manager()->CommitPendingItem();
ASSERT_TRUE(navigation_manager()->GetLastCommittedItem());
EXPECT_TRUE(ui::PageTransitionCoreTypeIs(
navigation_manager()->GetLastCommittedItem()->GetTransitionType(),
......@@ -918,7 +1133,10 @@ TEST_P(NavigationManagerTest, AddSameUrlPendingItemIfOverrideMobile) {
existing_url, Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::DESKTOP);
[mock_wk_list_ setCurrentURL:@"http://www.existing.com"];
navigation_manager()->CommitPendingItem();
ASSERT_TRUE(navigation_manager()->GetLastCommittedItem());
EXPECT_TRUE(ui::PageTransitionCoreTypeIs(
navigation_manager()->GetLastCommittedItem()->GetTransitionType(),
......@@ -945,7 +1163,10 @@ TEST_P(NavigationManagerTest, OverrideUserAgentWithDesktop) {
GURL("http://www.1.com"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::MOBILE);
[mock_wk_list_ setCurrentURL:@"http://www.1.com"];
navigation_manager()->CommitPendingItem();
NavigationItem* last_committed_item =
navigation_manager()->GetLastCommittedItem();
EXPECT_EQ(UserAgentType::MOBILE, last_committed_item->GetUserAgentType());
......@@ -968,7 +1189,10 @@ TEST_P(NavigationManagerTest, OverrideUserAgentWithMobile) {
GURL("http://www.1.com"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.1.com"];
navigation_manager()->CommitPendingItem();
NavigationItem* last_committed_item =
navigation_manager()->GetLastCommittedItem();
last_committed_item->SetUserAgentType(UserAgentType::DESKTOP);
......@@ -990,7 +1214,10 @@ TEST_P(NavigationManagerTest, OverrideUserAgentWithInheritAfterInherit) {
GURL("http://www.1.com"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.1.com"];
navigation_manager()->CommitPendingItem();
ASSERT_TRUE(navigation_manager()->GetLastCommittedItem());
EXPECT_EQ(web::UserAgentType::MOBILE,
navigation_manager()->GetLastCommittedItem()->GetUserAgentType());
......@@ -999,7 +1226,12 @@ TEST_P(NavigationManagerTest, OverrideUserAgentWithInheritAfterInherit) {
GURL("http://www.2.com"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.2.com"
backListURLs:@[ @"http://www.1.com" ]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
ASSERT_TRUE(navigation_manager()->GetLastCommittedItem());
EXPECT_EQ(web::UserAgentType::MOBILE,
navigation_manager()->GetLastCommittedItem()->GetUserAgentType());
......@@ -1012,7 +1244,10 @@ TEST_P(NavigationManagerTest, OverrideUserAgentWithInheritAfterMobile) {
GURL("http://www.1.com"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::MOBILE);
[mock_wk_list_ setCurrentURL:@"http://www.1.com"];
navigation_manager()->CommitPendingItem();
ASSERT_TRUE(navigation_manager()->GetLastCommittedItem());
EXPECT_EQ(web::UserAgentType::MOBILE,
navigation_manager()->GetLastCommittedItem()->GetUserAgentType());
......@@ -1021,7 +1256,12 @@ TEST_P(NavigationManagerTest, OverrideUserAgentWithInheritAfterMobile) {
GURL("http://www.2.com"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.2.com"
backListURLs:@[ @"http://www.1.com" ]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
ASSERT_TRUE(navigation_manager()->GetLastCommittedItem());
EXPECT_EQ(web::UserAgentType::MOBILE,
navigation_manager()->GetLastCommittedItem()->GetUserAgentType());
......@@ -1034,7 +1274,10 @@ TEST_P(NavigationManagerTest, OverrideUserAgentWithInheritAfterDesktop) {
GURL("http://www.1.com"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::DESKTOP);
[mock_wk_list_ setCurrentURL:@"http://www.1.com"];
navigation_manager()->CommitPendingItem();
ASSERT_TRUE(navigation_manager()->GetLastCommittedItem());
EXPECT_EQ(web::UserAgentType::DESKTOP,
navigation_manager()->GetLastCommittedItem()->GetUserAgentType());
......@@ -1043,15 +1286,25 @@ TEST_P(NavigationManagerTest, OverrideUserAgentWithInheritAfterDesktop) {
GURL("http://www.2.com"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.2.com"
backListURLs:@[ @"http://www.1.com" ]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
ASSERT_TRUE(navigation_manager()->GetLastCommittedItem());
EXPECT_EQ(web::UserAgentType::DESKTOP,
navigation_manager()->GetLastCommittedItem()->GetUserAgentType());
}
// Tests that the UserAgentType is propagated to subsequent NavigationItems if
// a native URL exists in between naviations.
// a native URL exists in between navigations.
TEST_P(NavigationManagerTest, UserAgentTypePropagationPastNativeItems) {
if (GetParam() == TEST_WK_BASED_NAVIGATION_MANAGER) {
// TODO(crbug.com/734150): Enable this test once |AddPendingItem| is
// integrated with native views.
return;
}
// GURL::Replacements that will replace a GURL's scheme with the test native
// scheme.
GURL::Replacements native_scheme_replacement;
......@@ -1062,14 +1315,24 @@ TEST_P(NavigationManagerTest, UserAgentTypePropagationPastNativeItems) {
GURL("http://www.1.com"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.1.com"];
navigation_manager()->CommitPendingItem();
web::NavigationItem* item1 = navigation_manager()->GetLastCommittedItem();
ASSERT_EQ(web::UserAgentType::MOBILE, item1->GetUserAgentType());
GURL item2_url = item1->GetURL().ReplaceComponents(native_scheme_replacement);
navigation_manager()->AddPendingItem(
item1->GetURL().ReplaceComponents(native_scheme_replacement), Referrer(),
ui::PAGE_TRANSITION_TYPED, web::NavigationInitiationType::USER_INITIATED,
item2_url, Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:base::SysUTF8ToNSString(item2_url.spec())
backListURLs:@[ @"http://www.1.com" ]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
web::NavigationItem* native_item1 =
navigation_manager()->GetLastCommittedItem();
ASSERT_EQ(web::UserAgentType::NONE, native_item1->GetUserAgentType());
......@@ -1077,6 +1340,13 @@ TEST_P(NavigationManagerTest, UserAgentTypePropagationPastNativeItems) {
GURL("http://www.2.com"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_
setCurrentURL:@"http://www.2.com"
backListURLs:@[
@"http://www.1.com", base::SysUTF8ToNSString(item2_url.spec())
]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
web::NavigationItem* item2 = navigation_manager()->GetLastCommittedItem();
......@@ -1087,10 +1357,20 @@ TEST_P(NavigationManagerTest, UserAgentTypePropagationPastNativeItems) {
// once again separated by a native one.
item2->SetUserAgentType(web::UserAgentType::DESKTOP);
ASSERT_EQ(web::UserAgentType::DESKTOP, item2->GetUserAgentType());
GURL item3_url = item2->GetURL().ReplaceComponents(native_scheme_replacement);
navigation_manager()->AddPendingItem(
item2->GetURL().ReplaceComponents(native_scheme_replacement), Referrer(),
ui::PAGE_TRANSITION_TYPED, web::NavigationInitiationType::USER_INITIATED,
item3_url, Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_
setCurrentURL:base::SysUTF8ToNSString(item3_url.spec())
backListURLs:@[
@"http://www.1.com", base::SysUTF8ToNSString(item2_url.spec()),
@"http://www.2.com"
]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
web::NavigationItem* native_item2 =
navigation_manager()->GetLastCommittedItem();
......@@ -1099,6 +1379,14 @@ TEST_P(NavigationManagerTest, UserAgentTypePropagationPastNativeItems) {
GURL("http://www.3.com"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_
setCurrentURL:@"http://www.3.com"
backListURLs:@[
@"http://www.1.com", base::SysUTF8ToNSString(item2_url.spec()),
@"http://www.2.com", base::SysUTF8ToNSString(item3_url.spec())
]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
web::NavigationItem* item3 = navigation_manager()->GetLastCommittedItem();
......@@ -1203,6 +1491,8 @@ TEST_P(NavigationManagerTest, ReloadLastCommittedItemWithNormalType) {
GURL("http://www.url.com/0"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com/0"];
navigation_manager()->CommitPendingItem();
GURL url_before_reload = GURL("http://www.url.com/1");
......@@ -1210,6 +1500,10 @@ TEST_P(NavigationManagerTest, ReloadLastCommittedItemWithNormalType) {
url_before_reload, Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com/1"
backListURLs:@[ @"http://www.url.com/0" ]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
navigation_manager()->Reload(web::ReloadType::NORMAL,
......@@ -1230,6 +1524,8 @@ TEST_P(NavigationManagerTest,
GURL("http://www.url.com/0"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com/0"];
navigation_manager()->CommitPendingItem();
GURL url_before_reload = GURL("http://www.url.com/1");
......@@ -1237,14 +1533,29 @@ TEST_P(NavigationManagerTest,
url_before_reload, Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com/1"
backListURLs:@[ @"http://www.url.com/0" ]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
navigation_manager()->AddPendingItem(
GURL("http://www.url.com/2"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_
setCurrentURL:@"http://www.url.com/2"
backListURLs:@[ @"http://www.url.com/0", @"http://www.url.com/1" ]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
if (GetParam() == TEST_WK_BASED_NAVIGATION_MANAGER) {
// TODO(crbug.com/734150): Enable this test once |GoToIndex| is
// implemented in WKBasedNavigationManager.
return;
}
navigation_manager()->GoToIndex(1);
EXPECT_EQ(1, navigation_manager()->GetLastCommittedItemIndex());
......@@ -1325,6 +1636,8 @@ TEST_P(NavigationManagerTest, ReloadLastCommittedItemWithOriginalType) {
GURL("http://www.url.com/0"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com/0"];
navigation_manager()->CommitPendingItem();
navigation_manager()->AddPendingItem(
......@@ -1335,6 +1648,10 @@ TEST_P(NavigationManagerTest, ReloadLastCommittedItemWithOriginalType) {
ASSERT_TRUE(navigation_manager()->GetPendingItem());
navigation_manager()->GetPendingItem()->SetOriginalRequestURL(
expected_original_url);
[mock_wk_list_ setCurrentURL:@"http://www.url.com/1/original"
backListURLs:@[ @"http://www.url.com/0" ]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
navigation_manager()->Reload(web::ReloadType::ORIGINAL_REQUEST_URL,
......@@ -1355,6 +1672,8 @@ TEST_P(NavigationManagerTest,
GURL("http://www.url.com/0"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com/0"];
navigation_manager()->CommitPendingItem();
navigation_manager()->AddPendingItem(
......@@ -1365,14 +1684,29 @@ TEST_P(NavigationManagerTest,
ASSERT_TRUE(navigation_manager()->GetPendingItem());
navigation_manager()->GetPendingItem()->SetOriginalRequestURL(
expected_original_url);
[mock_wk_list_ setCurrentURL:@"http://www.url.com/1/original"
backListURLs:@[ @"http://www.url.com/0" ]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
navigation_manager()->AddPendingItem(
GURL("http://www.url.com/2"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com/2"
backListURLs:@[
@"http://www.url.com/0", @"http://www.url.com/1/original"
]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
if (GetParam() == TEST_WK_BASED_NAVIGATION_MANAGER) {
// TODO(crbug.com/734150): Enable this test once |GoToIndex| is
// implemented in WKBasedNavigationManager.
return;
}
navigation_manager()->GoToIndex(1);
EXPECT_EQ(1, navigation_manager()->GetLastCommittedItemIndex());
......@@ -1397,7 +1731,9 @@ TEST_P(NavigationManagerTest, RewritingAppSpecificUrls) {
EXPECT_EQ(url1, navigation_manager()->GetPendingItem()->GetURL());
// URL should not be rewritten because last committed URL is not app-specific.
[mock_wk_list_ setCurrentURL:base::SysUTF8ToNSString(url1.spec())];
navigation_manager()->CommitPendingItem();
GURL url2(url::SchemeHostPort(kSchemeToRewrite, "test2", 0).Serialize());
navigation_manager()->AddPendingItem(
url2, Referrer(), ui::PAGE_TRANSITION_LINK,
......@@ -1416,7 +1752,11 @@ TEST_P(NavigationManagerTest, RewritingAppSpecificUrls) {
EXPECT_EQ(rewritten_url3, navigation_manager()->GetPendingItem()->GetURL());
// URL should be rewritten because last committed URL is app-specific.
[mock_wk_list_ setCurrentURL:base::SysUTF8ToNSString(rewritten_url3.spec())
backListURLs:@[ base::SysUTF8ToNSString(url1.spec()) ]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
GURL url4(url::SchemeHostPort(kSchemeToRewrite, "test4", 0).Serialize());
navigation_manager()->AddPendingItem(
url4, Referrer(), ui::PAGE_TRANSITION_LINK,
......@@ -1451,18 +1791,32 @@ TEST_P(NavigationManagerTest, ApplyTransientRewriters) {
// Tests that GetIndexOfItem() returns the correct values.
TEST_P(NavigationManagerTest, GetIndexOfItem) {
if (GetParam() == TEST_WK_BASED_NAVIGATION_MANAGER) {
// TODO(crbug.com/734150): Enable this test once |GetIndexOfItem| is
// implemented in WKBasedNavigationManager.
return;
}
// Create two items and add them to the NavigationManagerImpl.
navigation_manager()->AddPendingItem(
GURL("http://www.url.com/0"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com/0"];
navigation_manager()->CommitPendingItem();
web::NavigationItem* item0 = navigation_manager()->GetLastCommittedItem();
navigation_manager()->AddPendingItem(
GURL("http://www.url.com/1"), Referrer(), ui::PAGE_TRANSITION_TYPED,
web::NavigationInitiationType::USER_INITIATED,
web::NavigationManager::UserAgentOverrideOption::INHERIT);
[mock_wk_list_ setCurrentURL:@"http://www.url.com/1"
backListURLs:@[ @"http://www.url.com/0" ]
forwardListURLs:nil];
navigation_manager()->CommitPendingItem();
web::NavigationItem* item1 = navigation_manager()->GetLastCommittedItem();
// Create an item that does not exist in the NavigationManagerImpl.
std::unique_ptr<web::NavigationItem> item_not_found =
......@@ -1476,6 +1830,8 @@ TEST_P(NavigationManagerTest, GetIndexOfItem) {
INSTANTIATE_TEST_CASE_P(
ProgrammaticNavigationManagerTest,
NavigationManagerTest,
::testing::Values(NavigationManagerChoice::TEST_LEGACY_NAVIGATION_MANAGER));
::testing::Values(
NavigationManagerChoice::TEST_LEGACY_NAVIGATION_MANAGER,
NavigationManagerChoice::TEST_WK_BASED_NAVIGATION_MANAGER));
} // namespace web
......@@ -105,8 +105,9 @@ void WKBasedNavigationManagerImpl::AddTransientItem(const GURL& url) {
// Transient item is only supposed to be added for pending non-app-specific
// navigations.
DCHECK(pending_item_->GetUserAgentType() != UserAgentType::NONE);
transient_item_->SetUserAgentType(pending_item_->GetUserAgentType());
NavigationItem* pending_item = GetPendingItem();
DCHECK(pending_item->GetUserAgentType() != UserAgentType::NONE);
transient_item_->SetUserAgentType(pending_item->GetUserAgentType());
}
void WKBasedNavigationManagerImpl::AddPendingItem(
......@@ -176,12 +177,11 @@ void WKBasedNavigationManagerImpl::CommitPendingItem() {
pending_item_index_ = -1;
previous_item_index_ = last_committed_item_index_;
last_committed_item_index_ = GetWKCurrentItemIndex();
OnNavigationItemCommitted();
}
int WKBasedNavigationManagerImpl::GetIndexForOffset(int offset) const {
int result = (pending_item_index_ == -1) ? GetLastCommittedItemIndex()
int result = (pending_item_index_ == -1) ? GetWKCurrentItemIndex()
: pending_item_index_;
if (offset < 0 && GetTransientItem() && pending_item_index_ == -1) {
......@@ -276,6 +276,15 @@ int WKBasedNavigationManagerImpl::GetPendingItemIndex() const {
}
int WKBasedNavigationManagerImpl::GetLastCommittedItemIndex() const {
// WKBackForwardList's |currentItem| is usually the last committed item,
// except when the pending navigation is a back-forward navigation, in which
// case it is actually the pending item. As a workaround, fall back to
// last_committed_item_index_. This is not 100% correct (since
// last_committed_item_index_ is only updated for main frame navigations),
// but is the best possible answer.
if (pending_item_index_ >= 0) {
return last_committed_item_index_;
}
return GetWKCurrentItemIndex();
}
......
......@@ -7,11 +7,16 @@
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@class WKBackForwardListItem;
// A CRWTestBackForwardList can be used to stub out WKBackForwardList in tests.
@interface CRWTestBackForwardList : NSObject
// Returns an OCMock of WKBackForwardListItem with the given URL.
+ (WKBackForwardListItem*)itemWithURLString:(NSString*)URL;
// WKBackForwardList interface
@property(nullable, nonatomic, copy) NSArray<WKBackForwardListItem*>* backList;
@property(nullable, nonatomic, copy)
......@@ -27,9 +32,11 @@
// Resets this instance to simulate a session with the current entry at
// |currentItemURL|, and back and forward history entries as specified in
// |backListURLs| and |forwardListURLs|.
- (void)setCurrentURL:(nonnull NSString*)currentItemURL
- (void)setCurrentURL:(NSString*)currentItemURL
backListURLs:(nullable NSArray<NSString*>*)backListURLs
forwardListURLs:(nullable NSArray<NSString*>*)forwardListURLs;
@end
NS_ASSUME_NONNULL_END
#endif // IOS_WEB_NAVIGATION_CRW_TEST_BACK_FORWARD_LIST_H_
......@@ -13,7 +13,6 @@
#endif
@interface CRWTestBackForwardList (PrivateMethods)
- (WKBackForwardListItem*)mockItemWithURLString:(NSString*)URL;
- (NSArray*)mockSublistWithURLArray:(NSArray<NSString*>*)URLs;
@end
......@@ -23,6 +22,12 @@
@synthesize forwardList;
@synthesize currentItem;
+ (WKBackForwardListItem*)itemWithURLString:(NSString*)URL {
id mock = OCMClassMock([WKBackForwardListItem class]);
OCMStub([mock URL]).andReturn([NSURL URLWithString:URL]);
return mock;
}
- (WKBackForwardListItem*)itemAtIndex:(NSInteger)index {
if (index == 0) {
return self.currentItem;
......@@ -41,21 +46,15 @@
- (void)setCurrentURL:(NSString*)currentItemURL
backListURLs:(nullable NSArray<NSString*>*)backListURLs
forwardListURLs:(nullable NSArray<NSString*>*)forwardListURLs {
self.currentItem = [self mockItemWithURLString:currentItemURL];
self.currentItem = [CRWTestBackForwardList itemWithURLString:currentItemURL];
self.backList = [self mockSublistWithURLArray:backListURLs];
self.forwardList = [self mockSublistWithURLArray:forwardListURLs];
}
- (WKBackForwardListItem*)mockItemWithURLString:(NSString*)URL {
id mock = OCMClassMock([WKBackForwardListItem class]);
OCMStub([mock URL]).andReturn([NSURL URLWithString:URL]);
return mock;
}
- (NSArray*)mockSublistWithURLArray:(NSArray<NSString*>*)URLs {
NSMutableArray* array = [NSMutableArray arrayWithCapacity:URLs.count];
for (NSString* URL : URLs) {
[array addObject:[self mockItemWithURLString:URL]];
[array addObject:[CRWTestBackForwardList itemWithURLString:URL]];
}
return [NSArray arrayWithArray:array];
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment