OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #import "base/mac/bind_objc_block.h" | |
6 #include "base/memory/ptr_util.h" | |
7 #include "base/strings/stringprintf.h" | |
8 #include "base/strings/utf_string_conversions.h" | |
9 #include "base/test/ios/wait_util.h" | |
10 #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.
| |
11 #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.
| |
12 #import "ios/web/public/test/http_server.h" | |
13 #include "ios/web/public/test/http_server_util.h" | |
14 #import "ios/web/public/test/web_view_interaction_test_util.h" | |
15 #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.
| |
16 #include "ios/web/public/web_state/web_state_observer.h" | |
17 #import "ios/web/test/web_int_test.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 #include "testing/gtest_mac.h" | |
20 | |
21 using base::StringPrintf; | |
22 using base::ASCIIToUTF16; | |
23 | |
24 namespace { | |
25 | |
26 // URL for the test window.location test file. The page at this URL contains | |
27 // several buttons that trigger window.location commands. The page supports | |
28 // several JavaScript functions: | |
29 // - updateUrlToLoadText(), which takes a URL and updates a div on the page to | |
30 // contain that text. This URL is used as the parameter for window.location | |
31 // function calls triggered by button taps. | |
32 // - getUrl(), which returns the URL that was set via updateUrlToLoadText(). | |
33 // - isOnLoadTextVisible(), which returns whether a placeholder string is | |
34 // present on the page. This string is added to the page in the onload event | |
35 // and is removed once a button is tapped. Verifying that the onload text is | |
36 // visible after tapping a button is equivalent to checking that a load has | |
37 // occurred as the result of the button tap. | |
38 const char kWindowLocationTestURL[] = | |
39 "http://ios/testing/data/http_server_files/window_location.html"; | |
40 | |
41 // Button IDs used in the window.location test page. | |
42 const char kWindowLocationAssignID[] = "location-assign"; | |
43 | |
44 // JavaScript functions on the window.location test page. | |
45 const char kUpdateURLScriptFormat[] = "updateUrlToLoadText('%s')"; | |
46 const char kGetURLScript[] = "getUrl()"; | |
47 const char kOnLoadCheckScript[] = "isOnLoadTextVisible()"; | |
48 | |
49 // URL of a sample file-based page. | |
50 const char kSampleFileBasedURL[] = | |
51 "http://ios/testing/data/http_server_files/chromium_logo_page.html"; | |
52 | |
53 // WebStateObserver that can be used to track when page loads finish. | |
54 class WindowLocationTestWebStateObserver : public web::WebStateObserver { | |
55 public: | |
56 WindowLocationTestWebStateObserver(web::WebState* web_state) | |
57 : web::WebStateObserver(web_state), page_loaded_(false) {} | |
58 | |
59 // Instructs the observer to listen for page loads for |url|. | |
60 void ExpectPageLoad(const GURL& url) { | |
61 expected_url_ = url; | |
62 page_loaded_ = false; | |
63 } | |
64 | |
65 // Whether |expected_url_| has been loaded successfully. | |
66 bool IsExpectedPageLoaded() { return page_loaded_; } | |
67 | |
68 // WebStateObserver methods: | |
69 void PageLoaded( | |
70 web::PageLoadCompletionStatus load_completion_status) override { | |
71 ASSERT_EQ(load_completion_status == web::PageLoadCompletionStatus::SUCCESS, | |
72 expected_url_.is_valid()); | |
73 page_loaded_ = true; | |
74 } | |
75 | |
76 private: | |
77 GURL expected_url_; | |
78 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
| |
79 }; | |
80 | |
81 } // namespace | |
82 | |
83 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.
| |
84 protected: | |
85 void SetUp() override { | |
86 web::WebIntTest::SetUp(); | |
87 | |
88 // window.location tests use file-based test pages. | |
89 web::test::SetUpFileBasedHttpServer(); | |
90 | |
91 // Create the window.location test page URL and store it for convenient | |
92 // access later. | |
93 window_location_url_ = | |
94 web::test::HttpServer::MakeUrl(kWindowLocationTestURL); | |
95 | |
96 // Create the WebState and its WebStateObserver. | |
97 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.
| |
98 web_state_ = web::WebState::Create(webStateCreateParams); | |
99 observer_ = | |
100 base::WrapUnique(new WindowLocationTestWebStateObserver(web_state())); | |
101 | |
102 // Resize the webview so that all the buttons are rendered. | |
103 web_state()->GetView().frame = | |
104 [UIApplication sharedApplication].keyWindow.bounds; | |
105 | |
106 // Enable web usage and load the window.location test page. | |
107 web_state()->SetWebUsageEnabled(true); | |
108 LoadUrl(window_location_url()); | |
109 } | |
110 | |
111 // The URL of the window.location test page. | |
112 const GURL& window_location_url() { return window_location_url_; } | |
113 | |
114 // Returns the WebState and NavigationManager used for the test. | |
115 web::WebState* web_state() { return web_state_.get(); } | |
116 web::NavigationManager* navigation_manager() { | |
117 return web_state()->GetNavigationManager(); | |
118 } | |
119 | |
120 // Instructs |observer_| to wait for a successful load event for |url|. | |
121 void ExpectPageLoad(const GURL& url) { observer_->ExpectPageLoad(url); } | |
122 | |
123 // Waits until |observer_| reports that a load has finished successfully. | |
124 void WaitForPageToLoad() { | |
125 base::test::ios::WaitUntilCondition(^bool { | |
126 return observer_->IsExpectedPageLoaded(); | |
127 }); | |
128 } | |
129 | |
130 // Loads |url| in |web_state_|. | |
131 void LoadUrl(const GURL& url) { | |
132 ExpectPageLoad(url); | |
133 web::NavigationManager::WebLoadParams params(url); | |
134 navigation_manager()->LoadURLWithParams(params); | |
135 WaitForPageToLoad(); | |
136 } | |
137 | |
138 // Executes JavaScript on the window.location test page to use |url| as the | |
139 // parameter for the window.location calls executed by tapping the buttons on | |
140 // the page. | |
141 void SetWindowLocationUrl(const GURL& url) { | |
142 ASSERT_EQ(window_location_url(), web_state()->GetLastCommittedURL()); | |
143 base::string16 set_url_script = | |
144 ASCIIToUTF16(StringPrintf(kUpdateURLScriptFormat, url_spec.c_str())); | |
145 web_state()->ExecuteJavaScript(set_url_script); | |
146 __block bool url_is_updated = false; | |
147 base::string16 check_url_script = ASCIIToUTF16(kGetURLScript); | |
148 base::test::ios::WaitUntilCondition(^bool { | |
149 if (!url_is_updated) { | |
150 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.
| |
151 check_url_script, base::BindBlock(^(const base::Value* result) { | |
152 std::string js_url; | |
153 if (result->GetAsString(&js_url) && js_url == url.spec()) { | |
154 url_is_updated = true; | |
155 } | |
156 })); | |
157 } | |
158 return url_is_updated; | |
159 }); | |
160 } | |
161 | |
162 // Executes JavaScript on the window.location test page and returns whether | |
163 // |kOnLoadText| is visible. | |
164 bool IsOnLoadTextVisible() { | |
165 __block bool result_received = false; | |
166 __block bool text_visible = false; | |
167 base::string16 on_load_text_script = ASCIIToUTF16(kOnLoadCheckScript); | |
168 web_state()->ExecuteJavaScript( | |
Eugene But (OOO till 7-30)
2016/12/23 01:53:34
ditto
kkhorimoto
2017/01/18 00:09:33
Done.
| |
169 on_load_text_script, base::BindBlock(^(const base::Value* result) { | |
170 result->GetAsBoolean(&text_visible); | |
171 result_received = true; | |
172 })); | |
173 base::test::ios::WaitUntilCondition(^bool { | |
174 return result_received; | |
175 }); | |
176 return text_visible; | |
177 } | |
178 | |
179 // Returns the index of |item| in the |navigation_manager|'s session history. | |
180 NSInteger GetIndexOfNavigationItem(const web::NavigationItem* item) { | |
181 for (NSInteger i = 0; i < navigation_manager()->GetItemCount(); ++i) { | |
182 if (navigation_manager()->GetItemAtIndex(i) == item) | |
183 return i; | |
184 } | |
185 return NSNotFound; | |
186 } | |
187 | |
188 private: | |
189 GURL window_location_url_; | |
190 std::unique_ptr<web::WebState> web_state_; | |
191 std::unique_ptr<WindowLocationTestWebStateObserver> observer_; | |
192 }; | |
193 | |
194 // Tests that calling window.location.assign() creates a new NavigationItem. | |
195 TEST_F(WindowLocationTest, Assign) { | |
196 // Navigate to about:blank so there is a forward entry to prune. | |
197 GURL about_blank("about:blank"); | |
198 LoadUrl(about_blank); | |
199 web::NavigationItem* about_blank_item = | |
200 navigation_manager()->GetLastCommittedItem(); | |
201 | |
202 // Navigate back to the window.location test page. | |
203 ExpectPageLoad(window_location_url()); | |
204 navigation_manager()->GoBack(); | |
205 WaitForPageToLoad(); | |
206 | |
207 // Set the window.location test URL and tap the window.location.assign() | |
208 // button. | |
209 GURL sample_url = web::test::HttpServer::MakeUrl(kSampleFileBasedURL); | |
210 SetWindowLocationUrl(sample_url); | |
211 ExpectPageLoad(sample_url); | |
212 ASSERT_TRUE( | |
213 web::test::TapWebViewElementWithId(web_state(), kWindowLocationAssignID)); | |
214 WaitForPageToLoad(); | |
215 | |
216 // Verify that |sample_url| was loaded and that |about_blank_item| was pruned. | |
217 EXPECT_EQ(sample_url, navigation_manager()->GetLastCommittedItem()->GetURL()); | |
218 EXPECT_EQ(NSNotFound, GetIndexOfNavigationItem(about_blank_item)); | |
219 } | |
OLD | NEW |