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/29 17:06:50
s/include/import
kkhorimoto
2017/01/21 01:40:37
Done.
| |
11 #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.
| |
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/29 17:06:50
ditto
kkhorimoto
2017/01/21 01:40:37
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 kHistoryStateOperationsTestURL[] = | |
39 "http://ios/testing/data/http_server_files/state_operations.html"; | |
40 | |
41 // Button IDs used in the window.location test page. | |
42 const char kPushStateID[] = "push-state"; | |
43 const char kReplaceStateID[] = "replace-state"; | |
44 | |
45 // JavaScript functions on the history state test page. | |
46 const char kUpdateStateParamsScriptFormat[] = | |
47 "updateStateParams('%s', '%s', '%s')"; | |
48 const char kCheckStateParamsScriptFormat[] = | |
49 "checkStateParams('%s', '%s', '%s')"; | |
50 const char kOnLoadCheckScript[] = "isOnLoadTextVisible()"; | |
51 const char kNoOpCheckScript[] = "isNoOpTextVisible()"; | |
52 | |
53 // WebStateObserver that can be used to track when page loads finish. | |
54 class HistoryStateOperationsTestWebStateObserver | |
55 : public web::WebStateObserver { | |
56 public: | |
57 HistoryStateOperationsTestWebStateObserver(web::WebState* web_state) | |
58 : web::WebStateObserver(web_state), page_loaded_(false) {} | |
59 | |
60 // Instructs the observer to listen for page loads for |url|. | |
61 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.
| |
62 expected_url_ = url; | |
63 page_loaded_ = false; | |
64 } | |
65 | |
66 // Whether |expected_url_| has been loaded successfully. | |
67 bool IsExpectedPageLoaded() { return page_loaded_; } | |
68 | |
69 // WebStateObserver methods: | |
70 void PageLoaded( | |
71 web::PageLoadCompletionStatus load_completion_status) override { | |
72 ASSERT_EQ(load_completion_status == web::PageLoadCompletionStatus::SUCCESS, | |
73 expected_url_.is_valid()); | |
74 ASSERT_EQ(expected_url_, web_state()->GetLastCommittedURL()); | |
75 page_loaded_ = true; | |
76 } | |
77 | |
78 private: | |
79 GURL expected_url_; | |
80 bool page_loaded_; | |
81 }; | |
82 | |
83 } // namespace | |
84 | |
85 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.
| |
86 protected: | |
87 void SetUp() override { | |
88 web::WebIntTest::SetUp(); | |
89 | |
90 // History state tests use file-based test pages. | |
91 web::test::SetUpFileBasedHttpServer(); | |
92 | |
93 // Create the state operations test page URL and store it for convenient | |
94 // access later. | |
95 state_operations_url_ = | |
96 web::test::HttpServer::MakeUrl(kHistoryStateOperationsTestURL); | |
97 | |
98 // Create the WebState and its WebStateObserver. | |
99 web::WebState::CreateParams webStateCreateParams(GetBrowserState()); | |
100 web_state_ = web::WebState::Create(webStateCreateParams); | |
101 observer_ = base::WrapUnique( | |
102 new HistoryStateOperationsTestWebStateObserver(web_state())); | |
103 | |
104 // Resize the webview so that all the buttons are rendered. | |
105 web_state()->GetView().frame = | |
106 [UIApplication sharedApplication].keyWindow.bounds; | |
107 | |
108 // Enable web usage and load the state operations test page. | |
109 web_state()->SetWebUsageEnabled(true); | |
110 LoadUrl(state_operations_url()); | |
111 } | |
112 | |
113 // The URL of the window.location test page. | |
114 const GURL& state_operations_url() { return state_operations_url_; } | |
115 | |
116 // Returns the WebState and NavigationManager used for the test. | |
117 web::WebState* web_state() { return web_state_.get(); } | |
118 web::NavigationManager* navigation_manager() { | |
119 return web_state()->GetNavigationManager(); | |
120 } | |
121 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.
| |
122 return navigation_manager()->GetLastCommittedItem(); | |
123 } | |
124 | |
125 // Instructs |observer_| to wait for a successful load event for |url|. | |
126 void ExpectPageLoad(const GURL& url) { observer_->ExpectPageLoad(url); } | |
127 | |
128 // Waits until |observer_| reports that a load has finished successfully. | |
129 void WaitForPageToLoad() { | |
130 base::test::ios::WaitUntilCondition(^bool { | |
131 return observer_->IsExpectedPageLoaded(); | |
132 }); | |
133 } | |
134 | |
135 // Loads |url| in |web_state_|. | |
136 void LoadUrl(const GURL& url) { | |
137 ExpectPageLoad(url); | |
138 web::NavigationManager::WebLoadParams params(url); | |
139 navigation_manager()->LoadURLWithParams(params); | |
140 WaitForPageToLoad(); | |
141 } | |
142 | |
143 // 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.
| |
144 void SetStateParams(const std::string& state_object, | |
145 const std::string& title, | |
146 const GURL& url) { | |
147 ASSERT_EQ(state_operations_url(), current_item()->GetURL()); | |
148 std::string url_spec = url.possibly_invalid_spec(); | |
149 base::string16 set_params_script = ASCIIToUTF16( | |
150 StringPrintf(kUpdateStateParamsScriptFormat, state_object.c_str(), | |
151 title.c_str(), url_spec.c_str())); | |
152 web_state()->ExecuteJavaScript(set_params_script); | |
153 base::string16 check_params_script = ASCIIToUTF16( | |
154 StringPrintf(kCheckStateParamsScriptFormat, state_object.c_str(), | |
155 title.c_str(), url_spec.c_str())); | |
156 __block bool params_updated = false; | |
157 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.
| |
158 if (!params_updated) { | |
159 web_state()->ExecuteJavaScript( | |
160 check_params_script, base::BindBlock(^(const base::Value* result) { | |
161 result->GetAsBoolean(¶ms_updated); | |
162 })); | |
163 } | |
164 return params_updated; | |
165 }); | |
166 } | |
167 | |
168 // Executes |script| in the state operation test page, waits to receive its | |
169 // boolean result, then returns it. | |
170 bool GetScriptBoolResult(const char script[]) { | |
171 __block bool result_received = false; | |
172 __block bool script_result = false; | |
173 base::string16 bool_script = ASCIIToUTF16(script); | |
174 web_state()->ExecuteJavaScript( | |
175 bool_script, base::BindBlock(^(const base::Value* result) { | |
176 result->GetAsBoolean(&script_result); | |
177 result_received = true; | |
178 })); | |
179 base::test::ios::WaitUntilCondition(^bool { | |
180 return result_received; | |
181 }); | |
182 return script_result; | |
183 } | |
184 | |
185 // Executes JavaScript to check whether the onload text is visible. | |
186 bool IsOnLoadTextVisible() { return GetScriptBoolResult(kOnLoadCheckScript); } | |
187 | |
188 // Executes JavaScript to check whether the no-op text is visible. | |
189 bool IsNoOpTextVisible() { return GetScriptBoolResult(kNoOpCheckScript); } | |
190 | |
191 // Waits for the NoOp text to be visible and returns whether the text was | |
192 // shown. The NoOp text is displayed 0.5s after tapping a button, so there is | |
193 // a timeout of 1s. | |
194 bool LastOperationWasNoOp() { | |
195 __block bool operation_was_no_op = false; | |
196 base::test::ios::WaitUntilCondition( | |
197 ^bool { | |
198 operation_was_no_op = IsNoOpTextVisible(); | |
199 return operation_was_no_op; | |
200 }, | |
201 false, base::TimeDelta::FromSeconds(1.0)); | |
202 return operation_was_no_op; | |
203 } | |
204 | |
205 // Returns the index of |item| in the |navigation_manager|'s session history. | |
206 NSInteger GetIndexOfNavigationItem(const web::NavigationItem* item) { | |
207 for (NSInteger i = 0; i < navigation_manager()->GetItemCount(); ++i) { | |
208 if (navigation_manager()->GetItemAtIndex(i) == item) | |
209 return i; | |
210 } | |
211 return NSNotFound; | |
212 } | |
213 | |
214 private: | |
215 GURL state_operations_url_; | |
216 std::unique_ptr<web::WebState> web_state_; | |
217 std::unique_ptr<HistoryStateOperationsTestWebStateObserver> observer_; | |
218 }; | |
219 | |
220 // Tests that calling window.history.[push/replace]State() is a no-op for | |
221 // invalid parameters. | |
222 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.
| |
223 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.
| |
224 std::string empty_title(""); | |
225 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
| |
226 // Perform a window.history.pushState() with an unresolvable URL. This will | |
227 // clear the OnLoad and NoOp text, so checking below that the NoOp text is | |
228 // displayed and the OnLoad text is empty ensures that no navigation occurred | |
229 // as the result of the pushState() call. | |
230 SetStateParams(empty_state, empty_title, unresolvable_url); | |
231 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kPushStateID)); | |
232 EXPECT_TRUE(LastOperationWasNoOp()); | |
233 // Reload the page and perform the same check with | |
234 // window.history.replaceState(). | |
235 LoadUrl(state_operations_url()); | |
236 ASSERT_TRUE(IsOnLoadTextVisible()); | |
237 SetStateParams(empty_state, empty_title, unresolvable_url); | |
238 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateID)); | |
239 EXPECT_TRUE(LastOperationWasNoOp()); | |
240 // Perform the same checks for URLs with different schemes. | |
241 GURL different_scheme_url("https://google.com"); | |
242 LoadUrl(state_operations_url()); | |
243 ASSERT_TRUE(IsOnLoadTextVisible()); | |
244 SetStateParams(empty_state, empty_title, different_scheme_url); | |
245 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kPushStateID)); | |
246 EXPECT_TRUE(LastOperationWasNoOp()); | |
247 LoadUrl(state_operations_url()); | |
248 ASSERT_TRUE(IsOnLoadTextVisible()); | |
249 SetStateParams(empty_state, empty_title, different_scheme_url); | |
250 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateID)); | |
251 EXPECT_TRUE(LastOperationWasNoOp()); | |
252 // Perform the same checks for URLs with a different origin. | |
253 GURL different_origin_url("http://localhost:1000"); | |
254 LoadUrl(state_operations_url()); | |
255 ASSERT_TRUE(IsOnLoadTextVisible()); | |
256 SetStateParams(empty_state, empty_title, different_origin_url); | |
257 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kPushStateID)); | |
258 EXPECT_TRUE(LastOperationWasNoOp()); | |
259 LoadUrl(state_operations_url()); | |
260 ASSERT_TRUE(IsOnLoadTextVisible()); | |
261 SetStateParams(empty_state, empty_title, different_origin_url); | |
262 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateID)); | |
263 EXPECT_TRUE(LastOperationWasNoOp()); | |
264 } | |
OLD | NEW |