Index: ios/web/navigation/window_location_inttest.mm |
diff --git a/ios/web/navigation/window_location_inttest.mm b/ios/web/navigation/window_location_inttest.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3ed6cefdb7ce33215d6f770c7b601178b9e07f39 |
--- /dev/null |
+++ b/ios/web/navigation/window_location_inttest.mm |
@@ -0,0 +1,219 @@ |
+// 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/23 01:53:35
import
kkhorimoto
2017/01/18 00:09:33
Done.
|
+#include "ios/web/public/navigation_manager.h" |
Eugene But (OOO till 7-30)
2016/12/23 01:53:34
import
kkhorimoto
2017/01/18 00:09:33
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/23 01:53:35
import
kkhorimoto
2017/01/18 00:09:33
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 kWindowLocationTestURL[] = |
+ "http://ios/testing/data/http_server_files/window_location.html"; |
+ |
+// Button IDs used in the window.location test page. |
+const char kWindowLocationAssignID[] = "location-assign"; |
+ |
+// JavaScript functions on the window.location test page. |
+const char kUpdateURLScriptFormat[] = "updateUrlToLoadText('%s')"; |
+const char kGetURLScript[] = "getUrl()"; |
+const char kOnLoadCheckScript[] = "isOnLoadTextVisible()"; |
+ |
+// URL of a sample file-based page. |
+const char kSampleFileBasedURL[] = |
+ "http://ios/testing/data/http_server_files/chromium_logo_page.html"; |
+ |
+// WebStateObserver that can be used to track when page loads finish. |
+class WindowLocationTestWebStateObserver : public web::WebStateObserver { |
+ public: |
+ WindowLocationTestWebStateObserver(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) { |
+ 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()); |
+ page_loaded_ = true; |
+ } |
+ |
+ private: |
+ GURL expected_url_; |
+ bool page_loaded_; |
Eugene But (OOO till 7-30)
2016/12/23 01:53:35
bool page_loaded_ = false;
kkhorimoto
2017/01/18 00:09:33
The default value is already set in the constructo
|
+}; |
+ |
+} // namespace |
+ |
+class WindowLocationTest : public web::WebIntTest { |
Eugene But (OOO till 7-30)
2016/12/23 01:53:35
Do you need comment here?
kkhorimoto
2017/01/18 00:09:33
Done.
|
+ protected: |
+ void SetUp() override { |
+ web::WebIntTest::SetUp(); |
+ |
+ // window.location tests use file-based test pages. |
+ web::test::SetUpFileBasedHttpServer(); |
+ |
+ // Create the window.location test page URL and store it for convenient |
+ // access later. |
+ window_location_url_ = |
+ web::test::HttpServer::MakeUrl(kWindowLocationTestURL); |
+ |
+ // Create the WebState and its WebStateObserver. |
+ web::WebState::CreateParams webStateCreateParams(GetBrowserState()); |
Eugene But (OOO till 7-30)
2016/12/23 01:53:35
web_state_create_params
kkhorimoto
2017/01/18 00:09:33
Done.
|
+ web_state_ = web::WebState::Create(webStateCreateParams); |
+ observer_ = |
+ base::WrapUnique(new WindowLocationTestWebStateObserver(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 window.location test page. |
+ web_state()->SetWebUsageEnabled(true); |
+ LoadUrl(window_location_url()); |
+ } |
+ |
+ // The URL of the window.location test page. |
+ const GURL& window_location_url() { return window_location_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(); |
+ } |
+ |
+ // 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(); |
+ } |
+ |
+ // Executes JavaScript on the window.location test page to use |url| as the |
+ // parameter for the window.location calls executed by tapping the buttons on |
+ // the page. |
+ void SetWindowLocationUrl(const GURL& url) { |
+ ASSERT_EQ(window_location_url(), web_state()->GetLastCommittedURL()); |
+ base::string16 set_url_script = |
+ ASCIIToUTF16(StringPrintf(kUpdateURLScriptFormat, url_spec.c_str())); |
+ web_state()->ExecuteJavaScript(set_url_script); |
+ __block bool url_is_updated = false; |
+ base::string16 check_url_script = ASCIIToUTF16(kGetURLScript); |
+ base::test::ios::WaitUntilCondition(^bool { |
+ if (!url_is_updated) { |
+ web_state()->ExecuteJavaScript( |
Eugene But (OOO till 7-30)
2016/12/23 01:53:35
There is web::ExecuteJavaScript test method which
kkhorimoto
2017/01/18 00:09:33
Done.
|
+ check_url_script, base::BindBlock(^(const base::Value* result) { |
+ std::string js_url; |
+ if (result->GetAsString(&js_url) && js_url == url.spec()) { |
+ url_is_updated = true; |
+ } |
+ })); |
+ } |
+ return url_is_updated; |
+ }); |
+ } |
+ |
+ // Executes JavaScript on the window.location test page and returns whether |
+ // |kOnLoadText| is visible. |
+ bool IsOnLoadTextVisible() { |
+ __block bool result_received = false; |
+ __block bool text_visible = false; |
+ base::string16 on_load_text_script = ASCIIToUTF16(kOnLoadCheckScript); |
+ web_state()->ExecuteJavaScript( |
Eugene But (OOO till 7-30)
2016/12/23 01:53:34
ditto
kkhorimoto
2017/01/18 00:09:33
Done.
|
+ on_load_text_script, base::BindBlock(^(const base::Value* result) { |
+ result->GetAsBoolean(&text_visible); |
+ result_received = true; |
+ })); |
+ base::test::ios::WaitUntilCondition(^bool { |
+ return result_received; |
+ }); |
+ return text_visible; |
+ } |
+ |
+ // 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 window_location_url_; |
+ std::unique_ptr<web::WebState> web_state_; |
+ std::unique_ptr<WindowLocationTestWebStateObserver> observer_; |
+}; |
+ |
+// Tests that calling window.location.assign() creates a new NavigationItem. |
+TEST_F(WindowLocationTest, Assign) { |
+ // Navigate to about:blank so there is a forward entry to prune. |
+ GURL about_blank("about:blank"); |
+ LoadUrl(about_blank); |
+ web::NavigationItem* about_blank_item = |
+ navigation_manager()->GetLastCommittedItem(); |
+ |
+ // Navigate back to the window.location test page. |
+ ExpectPageLoad(window_location_url()); |
+ navigation_manager()->GoBack(); |
+ WaitForPageToLoad(); |
+ |
+ // Set the window.location test URL and tap the window.location.assign() |
+ // button. |
+ GURL sample_url = web::test::HttpServer::MakeUrl(kSampleFileBasedURL); |
+ SetWindowLocationUrl(sample_url); |
+ ExpectPageLoad(sample_url); |
+ ASSERT_TRUE( |
+ web::test::TapWebViewElementWithId(web_state(), kWindowLocationAssignID)); |
+ WaitForPageToLoad(); |
+ |
+ // Verify that |sample_url| was loaded and that |about_blank_item| was pruned. |
+ EXPECT_EQ(sample_url, navigation_manager()->GetLastCommittedItem()->GetURL()); |
+ EXPECT_EQ(NSNotFound, GetIndexOfNavigationItem(about_blank_item)); |
+} |