Index: ios/web/navigation/history_state_operations_inttest.mm |
diff --git a/ios/web/navigation/history_state_operations_inttest.mm b/ios/web/navigation/history_state_operations_inttest.mm |
index 2889de6453b4cc0239f8c47745597307617d9ffa..d6d98b200b214cf4926e056d96fd73d80eb50632 100644 |
--- a/ios/web/navigation/history_state_operations_inttest.mm |
+++ b/ios/web/navigation/history_state_operations_inttest.mm |
@@ -4,8 +4,10 @@ |
#include "base/memory/ptr_util.h" |
#include "base/strings/string_number_conversions.h" |
+#include "base/strings/sys_string_conversions.h" |
#include "base/strings/utf_string_conversions.h" |
#include "base/test/ios/wait_util.h" |
+#import "ios/web/navigation/navigation_item_impl.h" |
#import "ios/web/public/navigation_item.h" |
#import "ios/web/public/navigation_manager.h" |
#import "ios/web/public/test/http_server.h" |
@@ -82,6 +84,11 @@ void SetStateParams(const std::string& state_object, |
ExecuteJavaScript(set_params_script); |
} |
+ // Returns the state object returned by JavaScript. |
+ std::string GetJavaScriptState() { |
+ return base::SysNSStringToUTF8(ExecuteJavaScript(@"window.history.state")); |
+ } |
+ |
// Executes JavaScript to check whether the onload text is visible. |
bool IsOnLoadTextVisible() { |
return [ExecuteJavaScript(kOnLoadCheckScript) boolValue]; |
@@ -238,3 +245,40 @@ void WaitForNoOpText() { |
EXPECT_EQ(GetIndexOfNavigationItem(GetLastCommittedItem()) + 1, |
GetIndexOfNavigationItem(about_blank_item)); |
} |
+ |
+// Tests that calling window.history.replaceState() with a new state object |
+// replaces the state object for the current NavigationItem. |
+TEST_F(HistoryStateOperationsTest, StateReplacement) { |
+ // Navigate to about:blank then navigate back to the test page. The created |
+ // NavigationItem can be used later to verify that the state is replaced |
+ // rather than pushed. |
+ GURL about_blank("about:blank"); |
+ LoadUrl(about_blank); |
+ web::NavigationItem* about_blank_item = GetLastCommittedItem(); |
+ ExecuteBlockAndWaitForLoad(state_operations_url(), ^{ |
+ navigation_manager()->GoBack(); |
+ }); |
+ ASSERT_EQ(state_operations_url(), GetLastCommittedItem()->GetURL()); |
+ // Set up the state parameters and tap the replace state button. |
+ std::string new_state("STATE OBJECT"); |
+ std::string empty_title; |
+ GURL empty_url; |
+ SetStateParams(new_state, empty_title, empty_url); |
+ ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateId)); |
+ // Verify that the state is reflected in the JavaScript context. |
+ base::test::ios::WaitUntilCondition(^bool { |
+ return GetJavaScriptState() == new_state; |
+ }); |
+ // Verify that the state is reflected in the latest NavigationItem. |
+ std::string serialized_state("\"STATE OBJECT\""); |
+ base::test::ios::WaitUntilCondition(^bool { |
+ web::NavigationItemImpl* item = |
+ static_cast<web::NavigationItemImpl*>(GetLastCommittedItem()); |
+ std::string item_state = |
+ base::SysNSStringToUTF8(item->GetSerializedStateObject()); |
+ return item_state == serialized_state; |
+ }); |
+ // Verify that the forward navigation was not pruned. |
+ EXPECT_EQ(GetIndexOfNavigationItem(GetLastCommittedItem()) + 1, |
+ GetIndexOfNavigationItem(about_blank_item)); |
+} |