Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(197)

Side by Side Diff: ios/web/navigation/history_state_operations_inttest.mm

Issue 2731283006: Update HistoryStateOperationsTest waits to include failure messages. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "base/memory/ptr_util.h" 5 #include "base/memory/ptr_util.h"
6 #include "base/strings/string_number_conversions.h" 6 #include "base/strings/string_number_conversions.h"
7 #include "base/strings/sys_string_conversions.h" 7 #include "base/strings/sys_string_conversions.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "base/test/ios/wait_util.h" 9 #import "ios/testing/wait_util.h"
10 #import "ios/web/navigation/navigation_item_impl.h" 10 #import "ios/web/navigation/navigation_item_impl.h"
11 #import "ios/web/public/navigation_item.h" 11 #import "ios/web/public/navigation_item.h"
12 #import "ios/web/public/navigation_manager.h" 12 #import "ios/web/public/navigation_manager.h"
13 #import "ios/web/public/test/http_server.h" 13 #import "ios/web/public/test/http_server.h"
14 #include "ios/web/public/test/http_server_util.h" 14 #include "ios/web/public/test/http_server_util.h"
15 #import "ios/web/public/test/web_view_interaction_test_util.h" 15 #import "ios/web/public/test/web_view_interaction_test_util.h"
16 #import "ios/web/public/web_state/web_state.h" 16 #import "ios/web/public/web_state/web_state.h"
17 #import "ios/web/test/web_int_test.h" 17 #import "ios/web/test/web_int_test.h"
18 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "testing/gtest_mac.h" 19 #include "testing/gtest_mac.h"
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 return [ExecuteJavaScript(kOnLoadCheckScript) boolValue]; 103 return [ExecuteJavaScript(kOnLoadCheckScript) boolValue];
104 } 104 }
105 105
106 // Executes JavaScript to check whether the no-op text is visible. 106 // Executes JavaScript to check whether the no-op text is visible.
107 bool IsNoOpTextVisible() { 107 bool IsNoOpTextVisible() {
108 return [ExecuteJavaScript(kNoOpCheckScript) boolValue]; 108 return [ExecuteJavaScript(kNoOpCheckScript) boolValue];
109 } 109 }
110 110
111 // Waits for the NoOp text to be visible. 111 // Waits for the NoOp text to be visible.
112 void WaitForNoOpText() { 112 void WaitForNoOpText() {
113 base::test::ios::WaitUntilCondition(^bool { 113 BOOL completed = testing::WaitUntilConditionOrTimeout(
114 return IsNoOpTextVisible(); 114 testing::kWaitForJSCompletionTimeout, ^{
Eugene But (OOO till 7-30) 2017/03/07 21:10:38 This does not look like JS execution. Is there a b
michaeldo 2017/03/07 22:21:54 This one is JS execution (defined in the helper ab
Eugene But (OOO till 7-30) 2017/03/08 00:55:00 Those are all State Update operations, which is ha
michaeldo 2017/03/08 07:26:54 Thanks for the explanation, it wasn't immediately
115 }); 115 return IsNoOpTextVisible();
116 });
117 EXPECT_TRUE(completed) << "NoOp text failed to be visible.";
116 } 118 }
117 119
118 private: 120 private:
119 GURL state_operations_url_; 121 GURL state_operations_url_;
120 }; 122 };
121 123
122 // Tests that calling window.history.pushState() is a no-op for unresolvable 124 // Tests that calling window.history.pushState() is a no-op for unresolvable
123 // URLs. 125 // URLs.
124 TEST_F(HistoryStateOperationsTest, NoOpPushUnresolvable) { 126 TEST_F(HistoryStateOperationsTest, NoOpPushUnresolvable) {
125 // Perform a window.history.pushState() with an unresolvable URL. This will 127 // Perform a window.history.pushState() with an unresolvable URL. This will
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 navigation_manager()->GoBack(); 242 navigation_manager()->GoBack();
241 }); 243 });
242 EXPECT_EQ(state_operations_url(), GetLastCommittedItem()->GetURL()); 244 EXPECT_EQ(state_operations_url(), GetLastCommittedItem()->GetURL());
243 // Set up the state parameters and tap the replace state button. 245 // Set up the state parameters and tap the replace state button.
244 std::string empty_state; 246 std::string empty_state;
245 std::string new_title("NEW TITLE"); 247 std::string new_title("NEW TITLE");
246 GURL empty_url; 248 GURL empty_url;
247 SetStateParams(empty_state, new_title, empty_url); 249 SetStateParams(empty_state, new_title, empty_url);
248 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateId)); 250 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateId));
249 // Wait for the title to be reflected in the NavigationItem. 251 // Wait for the title to be reflected in the NavigationItem.
250 base::test::ios::WaitUntilCondition(^bool { 252 BOOL completed = testing::WaitUntilConditionOrTimeout(
251 return GetLastCommittedItem()->GetTitle() == ASCIIToUTF16(new_title); 253 testing::kWaitForJSCompletionTimeout, ^{
252 }); 254 return GetLastCommittedItem()->GetTitle() == ASCIIToUTF16(new_title);
255 });
256 EXPECT_TRUE(completed) << "Failed to validate NavigationItem title.";
253 // Verify that the forward navigation was not pruned. 257 // Verify that the forward navigation was not pruned.
254 EXPECT_EQ(GetIndexOfNavigationItem(GetLastCommittedItem()) + 1, 258 EXPECT_EQ(GetIndexOfNavigationItem(GetLastCommittedItem()) + 1,
255 GetIndexOfNavigationItem(about_blank_item)); 259 GetIndexOfNavigationItem(about_blank_item));
256 } 260 }
257 261
258 // Tests that calling window.history.replaceState() with a new state object 262 // Tests that calling window.history.replaceState() with a new state object
259 // replaces the state object for the current NavigationItem. 263 // replaces the state object for the current NavigationItem.
260 TEST_F(HistoryStateOperationsTest, StateReplacement) { 264 TEST_F(HistoryStateOperationsTest, StateReplacement) {
261 // Navigate to about:blank then navigate back to the test page. The created 265 // Navigate to about:blank then navigate back to the test page. The created
262 // NavigationItem can be used later to verify that the state is replaced 266 // NavigationItem can be used later to verify that the state is replaced
263 // rather than pushed. 267 // rather than pushed.
264 GURL about_blank("about:blank"); 268 GURL about_blank("about:blank");
265 LoadUrl(about_blank); 269 LoadUrl(about_blank);
266 web::NavigationItem* about_blank_item = GetLastCommittedItem(); 270 web::NavigationItem* about_blank_item = GetLastCommittedItem();
267 ExecuteBlockAndWaitForLoad(state_operations_url(), ^{ 271 ExecuteBlockAndWaitForLoad(state_operations_url(), ^{
268 navigation_manager()->GoBack(); 272 navigation_manager()->GoBack();
269 }); 273 });
270 ASSERT_EQ(state_operations_url(), GetLastCommittedItem()->GetURL()); 274 ASSERT_EQ(state_operations_url(), GetLastCommittedItem()->GetURL());
271 // Set up the state parameters and tap the replace state button. 275 // Set up the state parameters and tap the replace state button.
272 std::string new_state("STATE OBJECT"); 276 std::string new_state("STATE OBJECT");
273 std::string empty_title; 277 std::string empty_title;
274 GURL empty_url; 278 GURL empty_url;
275 SetStateParams(new_state, empty_title, empty_url); 279 SetStateParams(new_state, empty_title, empty_url);
276 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateId)); 280 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateId));
277 // Verify that the state is reflected in the JavaScript context. 281 // Verify that the state is reflected in the JavaScript context.
278 base::test::ios::WaitUntilCondition(^bool { 282 BOOL verify_java_script_context_completed =
279 return GetJavaScriptState() == new_state; 283 testing::WaitUntilConditionOrTimeout(
280 }); 284 testing::kWaitForJSCompletionTimeout, ^{
285 return GetJavaScriptState() == new_state;
286 });
287 EXPECT_TRUE(verify_java_script_context_completed)
288 << "Failed to validate JavaScript state.";
281 // Verify that the state is reflected in the latest NavigationItem. 289 // Verify that the state is reflected in the latest NavigationItem.
282 std::string serialized_state("\"STATE OBJECT\""); 290 std::string serialized_state("\"STATE OBJECT\"");
283 base::test::ios::WaitUntilCondition(^bool { 291 BOOL verify_navigation_item_completed = testing::WaitUntilConditionOrTimeout(
284 web::NavigationItemImpl* item = 292 testing::kWaitForJSCompletionTimeout, ^{
285 static_cast<web::NavigationItemImpl*>(GetLastCommittedItem()); 293 web::NavigationItemImpl* item =
286 std::string item_state = 294 static_cast<web::NavigationItemImpl*>(GetLastCommittedItem());
287 base::SysNSStringToUTF8(item->GetSerializedStateObject()); 295 std::string item_state =
288 return item_state == serialized_state; 296 base::SysNSStringToUTF8(item->GetSerializedStateObject());
289 }); 297 return item_state == serialized_state;
298 });
299 EXPECT_TRUE(verify_navigation_item_completed)
300 << "Failed to validate NavigationItem state.";
290 // Verify that the forward navigation was not pruned. 301 // Verify that the forward navigation was not pruned.
291 EXPECT_EQ(GetIndexOfNavigationItem(GetLastCommittedItem()) + 1, 302 EXPECT_EQ(GetIndexOfNavigationItem(GetLastCommittedItem()) + 1,
292 GetIndexOfNavigationItem(about_blank_item)); 303 GetIndexOfNavigationItem(about_blank_item));
293 } 304 }
294 305
295 // Tests that the state object is reset to the correct value after reloading a 306 // Tests that the state object is reset to the correct value after reloading a
296 // page whose state has been replaced. 307 // page whose state has been replaced.
297 TEST_F(HistoryStateOperationsTest, StateReplacementReload) { 308 TEST_F(HistoryStateOperationsTest, StateReplacementReload) {
298 // Set up the state parameters and tap the replace state button. 309 // Set up the state parameters and tap the replace state button.
299 std::string new_state("STATE OBJECT"); 310 std::string new_state("STATE OBJECT");
300 std::string empty_title; 311 std::string empty_title;
301 GURL empty_url; 312 GURL empty_url;
302 SetStateParams(new_state, empty_title, empty_url); 313 SetStateParams(new_state, empty_title, empty_url);
303 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateId)); 314 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateId));
304 // Reload the page and check that the state object is present. 315 // Reload the page and check that the state object is present.
305 Reload(); 316 Reload();
306 ASSERT_TRUE(IsOnLoadTextVisible()); 317 ASSERT_TRUE(IsOnLoadTextVisible());
307 base::test::ios::WaitUntilCondition(^bool { 318 BOOL completed = testing::WaitUntilConditionOrTimeout(
308 return GetJavaScriptState() == new_state; 319 testing::kWaitForJSCompletionTimeout, ^{
309 }); 320 return GetJavaScriptState() == new_state;
321 });
322 EXPECT_TRUE(completed) << "Failed to validate JavaScript state.";
310 } 323 }
311 324
312 // Tests that the state object is correctly set for a page after a back/forward 325 // Tests that the state object is correctly set for a page after a back/forward
313 // navigation. 326 // navigation.
314 TEST_F(HistoryStateOperationsTest, StateReplacementBackForward) { 327 TEST_F(HistoryStateOperationsTest, StateReplacementBackForward) {
315 // Navigate to about:blank then navigate back to the test page. The created 328 // Navigate to about:blank then navigate back to the test page. The created
316 // NavigationItem can be used later to verify that the state is replaced 329 // NavigationItem can be used later to verify that the state is replaced
317 // rather than pushed. 330 // rather than pushed.
318 GURL about_blank("about:blank"); 331 GURL about_blank("about:blank");
319 LoadUrl(about_blank); 332 LoadUrl(about_blank);
320 ExecuteBlockAndWaitForLoad(state_operations_url(), ^{ 333 ExecuteBlockAndWaitForLoad(state_operations_url(), ^{
321 navigation_manager()->GoBack(); 334 navigation_manager()->GoBack();
322 }); 335 });
323 ASSERT_EQ(state_operations_url(), GetLastCommittedItem()->GetURL()); 336 ASSERT_EQ(state_operations_url(), GetLastCommittedItem()->GetURL());
324 // Set up the state parameters and tap the replace state button. 337 // Set up the state parameters and tap the replace state button.
325 std::string new_state("STATE OBJECT"); 338 std::string new_state("STATE OBJECT");
326 std::string empty_title(""); 339 std::string empty_title("");
327 GURL empty_url(""); 340 GURL empty_url("");
328 SetStateParams(new_state, empty_title, empty_url); 341 SetStateParams(new_state, empty_title, empty_url);
329 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateId)); 342 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateId));
330 // Go forward and back, then check that the state object is present. 343 // Go forward and back, then check that the state object is present.
331 ExecuteBlockAndWaitForLoad(about_blank, ^{ 344 ExecuteBlockAndWaitForLoad(about_blank, ^{
332 navigation_manager()->GoForward(); 345 navigation_manager()->GoForward();
333 }); 346 });
334 ExecuteBlockAndWaitForLoad(state_operations_url(), ^{ 347 ExecuteBlockAndWaitForLoad(state_operations_url(), ^{
335 navigation_manager()->GoBack(); 348 navigation_manager()->GoBack();
336 }); 349 });
337 ASSERT_TRUE(IsOnLoadTextVisible()); 350 ASSERT_TRUE(IsOnLoadTextVisible());
338 base::test::ios::WaitUntilCondition(^bool { 351 BOOL completed = testing::WaitUntilConditionOrTimeout(
339 return GetJavaScriptState() == new_state; 352 testing::kWaitForJSCompletionTimeout, ^{
340 }); 353 return GetJavaScriptState() == new_state;
354 });
355 EXPECT_TRUE(completed) << "Failed to validate JavaScript state.";
341 } 356 }
342 357
343 // Tests that calling window.history.pushState() creates a new NavigationItem 358 // Tests that calling window.history.pushState() creates a new NavigationItem
344 // and prunes trailing items. 359 // and prunes trailing items.
345 TEST_F(HistoryStateOperationsTest, PushState) { 360 TEST_F(HistoryStateOperationsTest, PushState) {
346 // Navigate to about:blank then navigate back to the test page. The created 361 // Navigate to about:blank then navigate back to the test page. The created
347 // NavigationItem can be used later to verify that the state is replaced 362 // NavigationItem can be used later to verify that the state is replaced
348 // rather than pushed. 363 // rather than pushed.
349 GURL about_blank("about:blank"); 364 GURL about_blank("about:blank");
350 LoadUrl(about_blank); 365 LoadUrl(about_blank);
351 web::NavigationItem* about_blank_item = GetLastCommittedItem(); 366 web::NavigationItem* about_blank_item = GetLastCommittedItem();
352 ExecuteBlockAndWaitForLoad(state_operations_url(), ^{ 367 ExecuteBlockAndWaitForLoad(state_operations_url(), ^{
353 navigation_manager()->GoBack(); 368 navigation_manager()->GoBack();
354 }); 369 });
355 ASSERT_EQ(state_operations_url(), GetLastCommittedItem()->GetURL()); 370 ASSERT_EQ(state_operations_url(), GetLastCommittedItem()->GetURL());
356 web::NavigationItem* non_pushed_item = GetLastCommittedItem(); 371 web::NavigationItem* non_pushed_item = GetLastCommittedItem();
357 // Set up the state parameters and tap the replace state button. 372 // Set up the state parameters and tap the replace state button.
358 std::string empty_state; 373 std::string empty_state;
359 std::string empty_title; 374 std::string empty_title;
360 GURL new_url = state_operations_url().Resolve("path"); 375 GURL new_url = state_operations_url().Resolve("path");
361 SetStateParams(empty_state, empty_title, new_url); 376 SetStateParams(empty_state, empty_title, new_url);
362 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kPushStateId)); 377 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kPushStateId));
363 // Verify that the url with the path is pushed. 378 // Verify that the url with the path is pushed.
364 base::test::ios::WaitUntilCondition(^bool { 379 BOOL completed = testing::WaitUntilConditionOrTimeout(
365 return GetLastCommittedItem()->GetURL() == new_url; 380 testing::kWaitForJSCompletionTimeout, ^{
366 }); 381 return GetLastCommittedItem()->GetURL() == new_url;
382 });
383 EXPECT_TRUE(completed) << "Failed to validate current url.";
367 // Verify that a new NavigationItem was created and that the forward item was 384 // Verify that a new NavigationItem was created and that the forward item was
368 // pruned. 385 // pruned.
369 EXPECT_EQ(GetIndexOfNavigationItem(non_pushed_item) + 1, 386 EXPECT_EQ(GetIndexOfNavigationItem(non_pushed_item) + 1,
370 GetIndexOfNavigationItem(GetLastCommittedItem())); 387 GetIndexOfNavigationItem(GetLastCommittedItem()));
371 EXPECT_EQ(NSNotFound, GetIndexOfNavigationItem(about_blank_item)); 388 EXPECT_EQ(NSNotFound, GetIndexOfNavigationItem(about_blank_item));
372 } 389 }
373 390
374 // Tests that performing a replaceState() on a page created with a POST request 391 // Tests that performing a replaceState() on a page created with a POST request
375 // resets the page to a GET request. 392 // resets the page to a GET request.
376 TEST_F(HistoryStateOperationsTest, ReplaceStatePostRequest) { 393 TEST_F(HistoryStateOperationsTest, ReplaceStatePostRequest) {
377 // Add POST data to the current NavigationItem. 394 // Add POST data to the current NavigationItem.
378 base::scoped_nsobject<NSData> post_data([NSData data]); 395 base::scoped_nsobject<NSData> post_data([NSData data]);
379 static_cast<web::NavigationItemImpl*>(GetLastCommittedItem()) 396 static_cast<web::NavigationItemImpl*>(GetLastCommittedItem())
380 ->SetPostData(post_data); 397 ->SetPostData(post_data);
381 ASSERT_TRUE(GetLastCommittedItem()->HasPostData()); 398 ASSERT_TRUE(GetLastCommittedItem()->HasPostData());
382 // Set up the state parameters and tap the replace state button. 399 // Set up the state parameters and tap the replace state button.
383 std::string new_state("STATE OBJECT"); 400 std::string new_state("STATE OBJECT");
384 std::string empty_title; 401 std::string empty_title;
385 GURL new_url = state_operations_url().Resolve("path"); 402 GURL new_url = state_operations_url().Resolve("path");
386 SetStateParams(new_state, empty_title, new_url); 403 SetStateParams(new_state, empty_title, new_url);
387 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateId)); 404 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateId));
388 // Verify that url has been replaced. 405 // Verify that url has been replaced.
389 base::test::ios::WaitUntilCondition(^bool { 406 BOOL completed = testing::WaitUntilConditionOrTimeout(
390 return GetLastCommittedItem()->GetURL() == new_url; 407 testing::kWaitForJSCompletionTimeout, ^{
391 }); 408 return GetLastCommittedItem()->GetURL() == new_url;
409 });
410 EXPECT_TRUE(completed) << "Failed to validate current url.";
392 // Verify that the NavigationItem no longer has POST data. 411 // Verify that the NavigationItem no longer has POST data.
393 EXPECT_FALSE(GetLastCommittedItem()->HasPostData()); 412 EXPECT_FALSE(GetLastCommittedItem()->HasPostData());
394 } 413 }
395 414
396 // Tests that performing a replaceState() on a page where only the URL fragment 415 // Tests that performing a replaceState() on a page where only the URL fragment
397 // is updated does not trigger a hashchange event. 416 // is updated does not trigger a hashchange event.
398 TEST_F(HistoryStateOperationsTest, ReplaceStateNoHashChangeEvent) { 417 TEST_F(HistoryStateOperationsTest, ReplaceStateNoHashChangeEvent) {
399 // Set up the state parameters and tap the replace state button. 418 // Set up the state parameters and tap the replace state button.
400 std::string empty_state; 419 std::string empty_state;
401 std::string empty_title; 420 std::string empty_title;
402 GURL new_url = state_operations_url().Resolve("#hash"); 421 GURL new_url = state_operations_url().Resolve("#hash");
403 SetStateParams(empty_state, empty_title, new_url); 422 SetStateParams(empty_state, empty_title, new_url);
404 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateId)); 423 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateId));
405 // Verify that url has been replaced. 424 // Verify that url has been replaced.
406 base::test::ios::WaitUntilCondition(^bool { 425 BOOL completed = testing::WaitUntilConditionOrTimeout(
407 return GetLastCommittedItem()->GetURL() == new_url; 426 testing::kWaitForJSCompletionTimeout, ^{
408 }); 427 return GetLastCommittedItem()->GetURL() == new_url;
428 });
429 EXPECT_TRUE(completed) << "Failed to validate current url.";
409 // Verify that the hashchange event was not fired. 430 // Verify that the hashchange event was not fired.
410 EXPECT_FALSE(static_cast<web::NavigationItemImpl*>(GetLastCommittedItem()) 431 EXPECT_FALSE(static_cast<web::NavigationItemImpl*>(GetLastCommittedItem())
411 ->IsCreatedFromHashChange()); 432 ->IsCreatedFromHashChange());
412 } 433 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698