OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "base/mac/bind_objc_block.h" | 5 #import "base/mac/bind_objc_block.h" |
6 #include "base/memory/ptr_util.h" | 6 #include "base/memory/ptr_util.h" |
7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #import "base/strings/sys_string_conversions.h" | 8 #import "base/strings/sys_string_conversions.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "base/test/ios/wait_util.h" | 10 #include "base/test/ios/wait_util.h" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 // Instructs |observer_| to wait for a successful load event for |url|. | 127 // Instructs |observer_| to wait for a successful load event for |url|. |
128 void ExpectPageLoad(const GURL& url) { observer_->ExpectPageLoad(url); } | 128 void ExpectPageLoad(const GURL& url) { observer_->ExpectPageLoad(url); } |
129 | 129 |
130 // Waits until |observer_| reports that a load has finished successfully. | 130 // Waits until |observer_| reports that a load has finished successfully. |
131 void WaitForPageToLoad() { | 131 void WaitForPageToLoad() { |
132 base::test::ios::WaitUntilCondition(^bool { | 132 base::test::ios::WaitUntilCondition(^bool { |
133 return observer_->IsExpectedPageLoaded(); | 133 return observer_->IsExpectedPageLoaded(); |
134 }); | 134 }); |
135 } | 135 } |
136 | 136 |
137 // Executes |block| and waits for |url| to be successfully loaded. | |
138 void ExecuteBlockAndWaitForLoad(const GURL& url, ProceduralBlock block) { | |
139 ExpectPageLoad(url); | |
140 block(); | |
141 WaitForPageToLoad(); | |
142 } | |
143 | |
137 // Loads |url| in |web_state_|. | 144 // Loads |url| in |web_state_|. |
138 void LoadUrl(const GURL& url) { | 145 void LoadUrl(const GURL& url) { |
139 ExpectPageLoad(url); | 146 ExecuteBlockAndWaitForLoad(url, ^{ |
140 web::NavigationManager::WebLoadParams params(url); | 147 web::NavigationManager::WebLoadParams params(url); |
141 navigation_manager()->LoadURLWithParams(params); | 148 navigation_manager()->LoadURLWithParams(params); |
142 WaitForPageToLoad(); | 149 }); |
143 } | 150 } |
144 | 151 |
145 // Reloads the page and waits for the load to finish. | 152 // Reloads the page and waits for the load to finish. |
146 void Reload() { | 153 void Reload() { |
147 ExpectPageLoad(current_item()->GetURL()); | 154 ExecuteBlockAndWaitForLoad(current_item()->GetURL(), ^{ |
148 // TODO(crbug.com/677364): Use NavigationManager::Reload() once it no longer | 155 // TODO(crbug.com/677364): Use NavigationManager::Reload() once it no |
149 // requires a web delegate. | 156 // longer requires a web delegate. |
150 web_state()->ExecuteJavaScript(ASCIIToUTF16("window.location.reload()")); | 157 web_state()->ExecuteJavaScript(ASCIIToUTF16("window.location.reload()")); |
151 WaitForPageToLoad(); | 158 }); |
152 } | 159 } |
153 | 160 |
154 // Set the parameters to use for state operations on the test page. | 161 // Set the parameters to use for state operations on the test page. |
155 void SetStateParams(const std::string& state_object, | 162 void SetStateParams(const std::string& state_object, |
156 const std::string& title, | 163 const std::string& title, |
157 const GURL& url) { | 164 const GURL& url) { |
158 ASSERT_EQ(state_operations_url(), current_item()->GetURL()); | 165 ASSERT_EQ(state_operations_url(), current_item()->GetURL()); |
159 std::string url_spec = url.possibly_invalid_spec(); | 166 std::string url_spec = url.possibly_invalid_spec(); |
160 base::string16 set_params_script = ASCIIToUTF16( | 167 base::string16 set_params_script = ASCIIToUTF16( |
161 StringPrintf(kUpdateStateParamsScriptFormat, state_object.c_str(), | 168 StringPrintf(kUpdateStateParamsScriptFormat, state_object.c_str(), |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
365 result->GetAsString(&state); | 372 result->GetAsString(&state); |
366 state_object_present = state == new_state; | 373 state_object_present = state == new_state; |
367 })); | 374 })); |
368 }, | 375 }, |
369 ^bool { | 376 ^bool { |
370 return state_object_present; | 377 return state_object_present; |
371 }, | 378 }, |
372 false, base::TimeDelta::FromSeconds(10.0)); | 379 false, base::TimeDelta::FromSeconds(10.0)); |
373 EXPECT_TRUE(state_object_present); | 380 EXPECT_TRUE(state_object_present); |
374 } | 381 } |
382 | |
383 // Tests that the state object is correctly set for a page after a back/forward | |
384 // navigation. | |
385 TEST_F(HistoryStateOperationsTest, StateReplacementBackForward) { | |
386 // Navigate to about:blank then navigate back to the test page. The created | |
387 // NavigationItem can be used later to verify that the state is replaced | |
388 // rather than pushed. | |
389 GURL about_blank("about:blank"); | |
390 LoadUrl(about_blank); | |
391 ExecuteBlockAndWaitForLoad(state_operations_url(), ^{ | |
392 navigation_manager()->GoBack(); | |
393 }); | |
394 ASSERT_EQ(state_operations_url(), current_item()->GetURL()); | |
395 // Set up the state parameters and tap the replace state button. | |
396 std::string new_state("STATE OBJECT"); | |
397 std::string empty_title(""); | |
Eugene But (OOO till 7-30)
2016/12/29 17:19:24
std::string empty_title;
kkhorimoto
2017/01/24 05:22:54
Done.
| |
398 GURL empty_url(""); | |
Eugene But (OOO till 7-30)
2016/12/29 17:19:24
GURL empty_url;
kkhorimoto
2017/01/24 05:22:54
Done.
| |
399 SetStateParams(new_state, empty_title, empty_url); | |
400 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateID)); | |
401 // Go forward and back, then check that the state object is present. | |
402 ExecuteBlockAndWaitForLoad(about_blank, ^{ | |
403 navigation_manager()->GoForward(); | |
404 }); | |
405 ExecuteBlockAndWaitForLoad(state_operations_url(), ^{ | |
406 navigation_manager()->GoBack(); | |
407 }); | |
408 ASSERT_TRUE(IsOnLoadTextVisible()); | |
409 __block bool state_object_present = false; | |
410 base::string16 state_object_script = ASCIIToUTF16("window.history.state"); | |
411 base::test::ios::TimeUntilCondition( | |
412 ^{ | |
413 web_state()->ExecuteJavaScript( | |
Eugene But (OOO till 7-30)
2016/12/29 17:19:24
Please use synchronous API.
kkhorimoto
2017/01/24 05:22:54
Done.
| |
414 state_object_script, base::BindBlock(^(const base::Value* result) { | |
415 std::string state; | |
416 result->GetAsString(&state); | |
417 state_object_present = state == new_state; | |
418 })); | |
419 }, | |
420 ^bool { | |
421 return state_object_present; | |
422 }, | |
423 false, base::TimeDelta::FromSeconds(10.0)); | |
424 EXPECT_TRUE(state_object_present); | |
Eugene But (OOO till 7-30)
2016/12/29 17:19:24
No need for this line.
kkhorimoto
2017/01/24 05:22:54
Done.
| |
425 } | |
OLD | NEW |