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 |
new file mode 100644 |
index 0000000000000000000000000000000000000000..00e0da40d73cc6e7cf980d5ade1a9ca6400d40cc |
--- /dev/null |
+++ b/ios/web/navigation/history_state_operations_inttest.mm |
@@ -0,0 +1,171 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#import "base/mac/bind_objc_block.h" |
Eugene But (OOO till 7-30)
2017/01/21 02:15:23
Do you need this and many other includes in this l
kkhorimoto
2017/01/24 04:08:15
Nope, removed.
|
+#include "base/memory/ptr_util.h" |
+#include "base/strings/stringprintf.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "base/test/ios/wait_util.h" |
+#import "ios/web/public/navigation_item.h" |
+#import "ios/web/public/navigation_manager.h" |
+#import "ios/web/public/test/http_server.h" |
+#include "ios/web/public/test/http_server_util.h" |
+#import "ios/web/public/test/web_view_interaction_test_util.h" |
+#import "ios/web/public/web_state/web_state.h" |
+#include "ios/web/public/web_state/web_state_observer.h" |
+#import "ios/web/test/web_int_test.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "testing/gtest_mac.h" |
+ |
+namespace { |
+ |
+// URL for the test window.location test file. The page at this URL contains |
+// several buttons that trigger window.location commands. The page supports |
+// several JavaScript functions: |
+// - updateUrlToLoadText(), which takes a URL and updates a div on the page to |
+// contain that text. This URL is used as the parameter for window.location |
+// function calls triggered by button taps. |
+// - getUrl(), which returns the URL that was set via updateUrlToLoadText(). |
+// - isOnLoadTextVisible(), which returns whether a placeholder string is |
+// present on the page. This string is added to the page in the onload event |
+// and is removed once a button is tapped. Verifying that the onload text is |
+// visible after tapping a button is equivalent to checking that a load has |
+// occurred as the result of the button tap. |
+const char kHistoryStateOperationsTestURL[] = |
Eugene But (OOO till 7-30)
2017/01/21 02:15:23
Optional nit: Camel case Url would be more appropr
kkhorimoto
2017/01/24 04:08:15
Done.
|
+ "http://ios/testing/data/http_server_files/state_operations.html"; |
+ |
+// Button IDs used in the window.location test page. |
+const char kPushStateID[] = "push-state"; |
Eugene But (OOO till 7-30)
2017/01/21 02:15:24
Same thing for Id
kkhorimoto
2017/01/24 04:08:16
Done.
|
+const char kReplaceStateID[] = "replace-state"; |
+ |
+// JavaScript functions on the history state test page. |
+NSString* const kUpdateStateParamsScriptFormat = |
+ @"updateStateParams('%s', '%s', '%s')"; |
+NSString* const kOnLoadCheckScript = @"isOnLoadPlaceholderTextVisible()"; |
+NSString* const kNoOpCheckScript = @"isNoOpPlaceholderTextVisible()"; |
+ |
+} // namespace |
+ |
+// Test fixture for integration tests involving html5 window.history state |
+// operations. |
+class HistoryStateOperationsTest : public web::WebIntTest { |
+ protected: |
+ void SetUp() override { |
+ web::WebIntTest::SetUp(); |
+ |
+ // History state tests use file-based test pages. |
+ web::test::SetUpFileBasedHttpServer(); |
+ |
+ // Load the history state test page. |
+ state_operations_url_ = |
+ web::test::HttpServer::MakeUrl(kHistoryStateOperationsTestURL); |
+ LoadUrl(state_operations_url()); |
+ } |
+ |
+ // The URL of the window.location test page. |
+ const GURL& state_operations_url() { return state_operations_url_; } |
+ |
+ // Set the parameters to use for state operations on the test page. This |
Eugene But (OOO till 7-30)
2017/01/21 02:15:23
s/Set/Sets
kkhorimoto
2017/01/24 04:08:15
Done.
|
+ // function executes a script that populates JavaScript values on the test |
+ // page. When the "push-state" or "replace-state" buttons are tapped, these |
+ // parameters will be passed to their corresponding JavaScript function calls. |
+ void SetStateParams(const std::string& state_object, |
+ const std::string& title, |
+ const GURL& url) { |
+ ASSERT_EQ(state_operations_url(), GetLastCommittedItem()->GetURL()); |
+ std::string url_spec = url.possibly_invalid_spec(); |
+ NSString* set_params_script = [NSString |
+ stringWithFormat:kUpdateStateParamsScriptFormat, state_object.c_str(), |
+ title.c_str(), url_spec.c_str()]; |
+ ExecuteJavaScript(set_params_script); |
+ } |
+ |
+ // Executes JavaScript to check whether the onload text is visible. |
+ bool IsOnLoadTextVisible() { |
+ return [ExecuteJavaScript(kOnLoadCheckScript) boolValue]; |
+ } |
+ |
+ // Executes JavaScript to check whether the no-op text is visible. |
+ bool IsNoOpTextVisible() { |
+ return [ExecuteJavaScript(kNoOpCheckScript) boolValue]; |
+ } |
+ |
+ // Waits for the NoOp text to be visible and returns whether the text was |
+ // shown. |
+ bool LastOperationWasNoOp() { |
+ __block bool operation_was_no_op = false; |
+ base::test::ios::WaitUntilCondition(^bool { |
+ operation_was_no_op = IsNoOpTextVisible(); |
+ return operation_was_no_op; |
+ }); |
+ return operation_was_no_op; |
Eugene But (OOO till 7-30)
2017/01/21 02:15:23
Can this ever return false? Should the result be v
|
+ } |
+ |
+ private: |
+ GURL state_operations_url_; |
+}; |
+ |
+// Tests that calling window.history.[push/replace]State() is a no-op for |
+// unresolvable URLs. |
+TEST_F(HistoryStateOperationsTest, NoOpUnresolvable) { |
+ std::string empty_state; |
+ std::string empty_title; |
+ GURL unresolvable_url("http://www.google.invalid"); |
+ // Perform a window.history.pushState() with an unresolvable URL. This will |
+ // clear the OnLoad and NoOp text, so checking below that the NoOp text is |
+ // displayed and the OnLoad text is empty ensures that no navigation occurred |
+ // as the result of the pushState() call. |
+ SetStateParams(empty_state, empty_title, unresolvable_url); |
+ ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kPushStateID)); |
+ EXPECT_TRUE(LastOperationWasNoOp()); |
+ // Reload the page and perform the same check with |
Eugene But (OOO till 7-30)
2017/01/21 02:15:23
Should this be a separate test? Smaller tests are
kkhorimoto
2017/01/24 04:08:15
Done.
|
+ // window.history.replaceState(). |
+ LoadUrl(state_operations_url()); |
+ ASSERT_TRUE(IsOnLoadTextVisible()); |
+ SetStateParams(empty_state, empty_title, unresolvable_url); |
+ ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateID)); |
+ EXPECT_TRUE(LastOperationWasNoOp()); |
+} |
+ |
+// Tests that calling window.history.[push/replace]State() is a no-op for URLs |
+// with a different scheme. |
+TEST_F(HistoryStateOperationsTest, NoOpDifferentScheme) { |
+ std::string empty_state; |
+ std::string empty_title; |
+ GURL different_scheme_url("https://google.com"); |
+ // Perform a window.history.pushState() with a URL with a different scheme. |
+ // This will clear the OnLoad and NoOp text, so checking below that the NoOp |
+ // text is displayed and the OnLoad text is empty ensures that no navigation |
+ // occurred as the result of the pushState() call. |
+ ASSERT_TRUE(IsOnLoadTextVisible()); |
+ SetStateParams(empty_state, empty_title, different_scheme_url); |
+ ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kPushStateID)); |
+ EXPECT_TRUE(LastOperationWasNoOp()); |
+ LoadUrl(state_operations_url()); |
Eugene But (OOO till 7-30)
2017/01/21 02:15:23
Should this be a separate test? Or at least have "
kkhorimoto
2017/01/24 04:08:15
Done.
|
+ ASSERT_TRUE(IsOnLoadTextVisible()); |
+ SetStateParams(empty_state, empty_title, different_scheme_url); |
+ ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateID)); |
+ EXPECT_TRUE(LastOperationWasNoOp()); |
+} |
+ |
+// Tests that calling window.history.[push/replace]State() is a no-op for URLs |
+// with a shceme differing from that of the current page. |
+TEST_F(HistoryStateOperationsTest, NoOpDifferentOrigin) { |
+ std::string empty_state; |
+ std::string empty_title; |
+ GURL different_origin_url("http://localhost:1000"); |
Eugene But (OOO till 7-30)
2017/01/21 02:15:23
Do you want to use |HttpServer::GetPort() + 1|?
kkhorimoto
2017/01/24 04:08:15
Done.
|
+ // Perform a window.history.pushState() with a URL with a different origin. |
+ // This will clear the OnLoad and NoOp text, so checking below that the NoOp |
+ // text is displayed and the OnLoad text is empty ensures that no navigation |
+ // occurred as the result of the pushState() call. |
+ ASSERT_TRUE(IsOnLoadTextVisible()); |
+ SetStateParams(empty_state, empty_title, different_origin_url); |
+ ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kPushStateID)); |
+ EXPECT_TRUE(LastOperationWasNoOp()); |
+ LoadUrl(state_operations_url()); |
Eugene But (OOO till 7-30)
2017/01/21 02:15:23
Should this be a separate test? Or at least have "
kkhorimoto
2017/01/24 04:08:15
Done.
|
+ ASSERT_TRUE(IsOnLoadTextVisible()); |
+ SetStateParams(empty_state, empty_title, different_origin_url); |
+ ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateID)); |
+ EXPECT_TRUE(LastOperationWasNoOp()); |
+} |