Chromium Code Reviews| 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..88feaf519e6203a7164943397bfa7a840e077303 |
| --- /dev/null |
| +++ b/ios/web/navigation/history_state_operations_inttest.mm |
| @@ -0,0 +1,264 @@ |
| +// 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" |
| +#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" |
| +#include "ios/web/public/navigation_item.h" |
|
Eugene But (OOO till 7-30)
2016/12/29 17:06:50
s/include/import
kkhorimoto
2017/01/21 01:40:37
Done.
|
| +#include "ios/web/public/navigation_manager.h" |
|
Eugene But (OOO till 7-30)
2016/12/29 17:06:50
ditto
kkhorimoto
2017/01/21 01:40:37
Done.
|
| +#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" |
| +#include "ios/web/public/web_state/web_state.h" |
|
Eugene But (OOO till 7-30)
2016/12/29 17:06:50
ditto
kkhorimoto
2017/01/21 01:40:37
Done.
|
| +#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" |
| + |
| +using base::StringPrintf; |
| +using base::ASCIIToUTF16; |
| + |
| +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[] = |
| + "http://ios/testing/data/http_server_files/state_operations.html"; |
| + |
| +// Button IDs used in the window.location test page. |
| +const char kPushStateID[] = "push-state"; |
| +const char kReplaceStateID[] = "replace-state"; |
| + |
| +// JavaScript functions on the history state test page. |
| +const char kUpdateStateParamsScriptFormat[] = |
| + "updateStateParams('%s', '%s', '%s')"; |
| +const char kCheckStateParamsScriptFormat[] = |
| + "checkStateParams('%s', '%s', '%s')"; |
| +const char kOnLoadCheckScript[] = "isOnLoadTextVisible()"; |
| +const char kNoOpCheckScript[] = "isNoOpTextVisible()"; |
| + |
| +// WebStateObserver that can be used to track when page loads finish. |
| +class HistoryStateOperationsTestWebStateObserver |
| + : public web::WebStateObserver { |
| + public: |
| + HistoryStateOperationsTestWebStateObserver(web::WebState* web_state) |
| + : web::WebStateObserver(web_state), page_loaded_(false) {} |
| + |
| + // Instructs the observer to listen for page loads for |url|. |
| + void ExpectPageLoad(const GURL& url) { |
|
Eugene But (OOO till 7-30)
2016/12/29 17:06:50
Could you please avoid code duplication. Same code
kkhorimoto
2017/01/21 01:40:37
Done.
|
| + expected_url_ = url; |
| + page_loaded_ = false; |
| + } |
| + |
| + // Whether |expected_url_| has been loaded successfully. |
| + bool IsExpectedPageLoaded() { return page_loaded_; } |
| + |
| + // WebStateObserver methods: |
| + void PageLoaded( |
| + web::PageLoadCompletionStatus load_completion_status) override { |
| + ASSERT_EQ(load_completion_status == web::PageLoadCompletionStatus::SUCCESS, |
| + expected_url_.is_valid()); |
| + ASSERT_EQ(expected_url_, web_state()->GetLastCommittedURL()); |
| + page_loaded_ = true; |
| + } |
| + |
| + private: |
| + GURL expected_url_; |
| + bool page_loaded_; |
| +}; |
| + |
| +} // namespace |
| + |
| +class HistoryStateOperationsTest : public web::WebIntTest { |
|
Eugene But (OOO till 7-30)
2016/12/29 17:06:50
Please explain in the comments the purpose of this
Eugene But (OOO till 7-30)
2016/12/29 17:06:50
This class have many common things from this CL: h
Eugene But (OOO till 7-30)
2016/12/29 17:31:53
Or maybe move this stuff to web::WebIntTest?
kkhorimoto
2017/01/21 01:40:37
Done.
|
| + protected: |
| + void SetUp() override { |
| + web::WebIntTest::SetUp(); |
| + |
| + // History state tests use file-based test pages. |
| + web::test::SetUpFileBasedHttpServer(); |
| + |
| + // Create the state operations test page URL and store it for convenient |
| + // access later. |
| + state_operations_url_ = |
| + web::test::HttpServer::MakeUrl(kHistoryStateOperationsTestURL); |
| + |
| + // Create the WebState and its WebStateObserver. |
| + web::WebState::CreateParams webStateCreateParams(GetBrowserState()); |
| + web_state_ = web::WebState::Create(webStateCreateParams); |
| + observer_ = base::WrapUnique( |
| + new HistoryStateOperationsTestWebStateObserver(web_state())); |
| + |
| + // Resize the webview so that all the buttons are rendered. |
| + web_state()->GetView().frame = |
| + [UIApplication sharedApplication].keyWindow.bounds; |
| + |
| + // Enable web usage and load the state operations test page. |
| + web_state()->SetWebUsageEnabled(true); |
| + LoadUrl(state_operations_url()); |
| + } |
| + |
| + // The URL of the window.location test page. |
| + const GURL& state_operations_url() { return state_operations_url_; } |
| + |
| + // Returns the WebState and NavigationManager used for the test. |
| + web::WebState* web_state() { return web_state_.get(); } |
| + web::NavigationManager* navigation_manager() { |
| + return web_state()->GetNavigationManager(); |
| + } |
| + web::NavigationItem* current_item() { |
|
Eugene But (OOO till 7-30)
2016/12/29 17:06:50
s/current_item/GetLastCommittedItem
When I see cu
kkhorimoto
2017/01/21 01:40:37
Done.
|
| + return navigation_manager()->GetLastCommittedItem(); |
| + } |
| + |
| + // Instructs |observer_| to wait for a successful load event for |url|. |
| + void ExpectPageLoad(const GURL& url) { observer_->ExpectPageLoad(url); } |
| + |
| + // Waits until |observer_| reports that a load has finished successfully. |
| + void WaitForPageToLoad() { |
| + base::test::ios::WaitUntilCondition(^bool { |
| + return observer_->IsExpectedPageLoaded(); |
| + }); |
| + } |
| + |
| + // Loads |url| in |web_state_|. |
| + void LoadUrl(const GURL& url) { |
| + ExpectPageLoad(url); |
| + web::NavigationManager::WebLoadParams params(url); |
| + navigation_manager()->LoadURLWithParams(params); |
| + WaitForPageToLoad(); |
| + } |
| + |
| + // Set the parameters to use for state operations on the test page. |
|
Eugene But (OOO till 7-30)
2016/12/29 17:06:50
Could you please be more specific with comments. D
kkhorimoto
2017/01/21 01:40:37
Done.
|
| + void SetStateParams(const std::string& state_object, |
| + const std::string& title, |
| + const GURL& url) { |
| + ASSERT_EQ(state_operations_url(), current_item()->GetURL()); |
| + std::string url_spec = url.possibly_invalid_spec(); |
| + base::string16 set_params_script = ASCIIToUTF16( |
| + StringPrintf(kUpdateStateParamsScriptFormat, state_object.c_str(), |
| + title.c_str(), url_spec.c_str())); |
| + web_state()->ExecuteJavaScript(set_params_script); |
| + base::string16 check_params_script = ASCIIToUTF16( |
| + StringPrintf(kCheckStateParamsScriptFormat, state_object.c_str(), |
| + title.c_str(), url_spec.c_str())); |
| + __block bool params_updated = false; |
| + base::test::ios::WaitUntilCondition(^bool { |
|
Eugene But (OOO till 7-30)
2016/12/29 17:06:50
Per comments in my previous CLs, could you please
kkhorimoto
2017/01/21 01:40:37
Done.
|
| + if (!params_updated) { |
| + web_state()->ExecuteJavaScript( |
| + check_params_script, base::BindBlock(^(const base::Value* result) { |
| + result->GetAsBoolean(¶ms_updated); |
| + })); |
| + } |
| + return params_updated; |
| + }); |
| + } |
| + |
| + // Executes |script| in the state operation test page, waits to receive its |
| + // boolean result, then returns it. |
| + bool GetScriptBoolResult(const char script[]) { |
| + __block bool result_received = false; |
| + __block bool script_result = false; |
| + base::string16 bool_script = ASCIIToUTF16(script); |
| + web_state()->ExecuteJavaScript( |
| + bool_script, base::BindBlock(^(const base::Value* result) { |
| + result->GetAsBoolean(&script_result); |
| + result_received = true; |
| + })); |
| + base::test::ios::WaitUntilCondition(^bool { |
| + return result_received; |
| + }); |
| + return script_result; |
| + } |
| + |
| + // Executes JavaScript to check whether the onload text is visible. |
| + bool IsOnLoadTextVisible() { return GetScriptBoolResult(kOnLoadCheckScript); } |
| + |
| + // Executes JavaScript to check whether the no-op text is visible. |
| + bool IsNoOpTextVisible() { return GetScriptBoolResult(kNoOpCheckScript); } |
| + |
| + // Waits for the NoOp text to be visible and returns whether the text was |
| + // shown. The NoOp text is displayed 0.5s after tapping a button, so there is |
| + // a timeout of 1s. |
| + bool LastOperationWasNoOp() { |
| + __block bool operation_was_no_op = false; |
| + base::test::ios::WaitUntilCondition( |
| + ^bool { |
| + operation_was_no_op = IsNoOpTextVisible(); |
| + return operation_was_no_op; |
| + }, |
| + false, base::TimeDelta::FromSeconds(1.0)); |
| + return operation_was_no_op; |
| + } |
| + |
| + // Returns the index of |item| in the |navigation_manager|'s session history. |
| + NSInteger GetIndexOfNavigationItem(const web::NavigationItem* item) { |
| + for (NSInteger i = 0; i < navigation_manager()->GetItemCount(); ++i) { |
| + if (navigation_manager()->GetItemAtIndex(i) == item) |
| + return i; |
| + } |
| + return NSNotFound; |
| + } |
| + |
| + private: |
| + GURL state_operations_url_; |
| + std::unique_ptr<web::WebState> web_state_; |
| + std::unique_ptr<HistoryStateOperationsTestWebStateObserver> observer_; |
| +}; |
| + |
| +// Tests that calling window.history.[push/replace]State() is a no-op for |
| +// invalid parameters. |
| +TEST_F(HistoryStateOperationsTest, NoOps) { |
|
Eugene But (OOO till 7-30)
2016/12/29 17:06:50
Looks like this test have multiple test cases insi
kkhorimoto
2017/01/21 01:40:37
Done.
|
| + std::string empty_state(""); |
|
Eugene But (OOO till 7-30)
2016/12/29 17:06:50
std::string empty_state;
Same comment for the nex
kkhorimoto
2017/01/21 01:40:37
Done.
|
| + std::string empty_title(""); |
| + GURL unresolvable_url("http://www.google.invalid"); |
|
Eugene But (OOO till 7-30)
2016/12/29 17:06:50
Does this URL hit network? Do you need to use loca
kkhorimoto
2017/01/21 01:40:37
No, the special "invalid" TLD prevents JS from res
|
| + // 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 |
| + // 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()); |
| + // Perform the same checks for URLs with different schemes. |
| + GURL different_scheme_url("https://google.com"); |
| + LoadUrl(state_operations_url()); |
| + 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()); |
| + ASSERT_TRUE(IsOnLoadTextVisible()); |
| + SetStateParams(empty_state, empty_title, different_scheme_url); |
| + ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateID)); |
| + EXPECT_TRUE(LastOperationWasNoOp()); |
| + // Perform the same checks for URLs with a different origin. |
| + GURL different_origin_url("http://localhost:1000"); |
| + LoadUrl(state_operations_url()); |
| + 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()); |
| + ASSERT_TRUE(IsOnLoadTextVisible()); |
| + SetStateParams(empty_state, empty_title, different_origin_url); |
| + ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateID)); |
| + EXPECT_TRUE(LastOperationWasNoOp()); |
| +} |