| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "ios/web/web_state/web_state_impl.h" | 5 #import "ios/web/web_state/web_state_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/base64.h" | 11 #include "base/base64.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #import "base/mac/bind_objc_block.h" | 14 #import "base/mac/bind_objc_block.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/strings/utf_string_conversions.h" | |
| 17 #import "base/test/ios/wait_util.h" | 16 #import "base/test/ios/wait_util.h" |
| 18 #include "base/values.h" | |
| 19 #import "ios/web/public/java_script_dialog_presenter.h" | 17 #import "ios/web/public/java_script_dialog_presenter.h" |
| 20 #include "ios/web/public/load_committed_details.h" | 18 #include "ios/web/public/load_committed_details.h" |
| 21 #include "ios/web/public/test/fakes/test_browser_state.h" | 19 #include "ios/web/public/test/fakes/test_browser_state.h" |
| 22 #import "ios/web/public/test/fakes/test_web_state_delegate.h" | 20 #import "ios/web/public/test/fakes/test_web_state_delegate.h" |
| 23 #include "ios/web/public/test/web_test.h" | 21 #include "ios/web/public/test/web_test.h" |
| 24 #import "ios/web/public/web_state/context_menu_params.h" | 22 #import "ios/web/public/web_state/context_menu_params.h" |
| 25 #include "ios/web/public/web_state/global_web_state_observer.h" | 23 #include "ios/web/public/web_state/global_web_state_observer.h" |
| 26 #import "ios/web/public/web_state/web_state_delegate.h" | 24 #import "ios/web/public/web_state/web_state_delegate.h" |
| 27 #include "ios/web/public/web_state/web_state_observer.h" | 25 #include "ios/web/public/web_state/web_state_observer.h" |
| 28 #import "ios/web/public/web_state/web_state_policy_decider.h" | 26 #import "ios/web/public/web_state/web_state_policy_decider.h" |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 const GURL& expected_url, | 232 const GURL& expected_url, |
| 235 const base::DictionaryValue& value, | 233 const base::DictionaryValue& value, |
| 236 const GURL& url, | 234 const GURL& url, |
| 237 bool user_is_interacting) { | 235 bool user_is_interacting) { |
| 238 *is_called = true; | 236 *is_called = true; |
| 239 EXPECT_TRUE(expected_value->Equals(&value)); | 237 EXPECT_TRUE(expected_value->Equals(&value)); |
| 240 EXPECT_EQ(expected_url, url); | 238 EXPECT_EQ(expected_url, url); |
| 241 return should_handle; | 239 return should_handle; |
| 242 } | 240 } |
| 243 | 241 |
| 244 class WebStateTest : public web::WebTest { | 242 } // namespace |
| 243 |
| 244 // Test fixture for web::WebStateImpl class. |
| 245 class WebStateImplTest : public web::WebTest { |
| 245 protected: | 246 protected: |
| 246 void SetUp() override { | 247 WebStateImplTest() : web_state_(new WebStateImpl(GetBrowserState())) {} |
| 247 web_state_.reset(new WebStateImpl(&browser_state_)); | |
| 248 } | |
| 249 | 248 |
| 250 // Loads specified html page into WebState. | |
| 251 void LoadHtml(std::string html) { | |
| 252 web_state_->GetNavigationManagerImpl().InitializeSession(NO); | |
| 253 | |
| 254 // Use data: url for loading html page. | |
| 255 std::string encoded_html; | |
| 256 base::Base64Encode(html, &encoded_html); | |
| 257 GURL url("data:text/html;charset=utf8;base64," + encoded_html); | |
| 258 web::NavigationManager::WebLoadParams params(url); | |
| 259 web_state_->GetNavigationManager()->LoadURLWithParams(params); | |
| 260 | |
| 261 // Trigger the load. | |
| 262 web_state_->SetWebUsageEnabled(true); | |
| 263 web_state_->GetView(); | |
| 264 | |
| 265 // Wait until load is completed. | |
| 266 EXPECT_TRUE(web_state_->IsLoading()); | |
| 267 base::test::ios::WaitUntilCondition(^bool() { | |
| 268 return !web_state_->IsLoading(); | |
| 269 }); | |
| 270 } | |
| 271 | |
| 272 web::TestBrowserState browser_state_; | |
| 273 std::unique_ptr<WebStateImpl> web_state_; | 249 std::unique_ptr<WebStateImpl> web_state_; |
| 274 }; | 250 }; |
| 275 | 251 |
| 276 TEST_F(WebStateTest, WebUsageEnabled) { | 252 TEST_F(WebStateImplTest, WebUsageEnabled) { |
| 277 // Default is false. | 253 // Default is false. |
| 278 ASSERT_FALSE(web_state_->IsWebUsageEnabled()); | 254 ASSERT_FALSE(web_state_->IsWebUsageEnabled()); |
| 279 | 255 |
| 280 web_state_->SetWebUsageEnabled(true); | 256 web_state_->SetWebUsageEnabled(true); |
| 281 EXPECT_TRUE(web_state_->IsWebUsageEnabled()); | 257 EXPECT_TRUE(web_state_->IsWebUsageEnabled()); |
| 282 EXPECT_TRUE(web_state_->GetWebController().webUsageEnabled); | 258 EXPECT_TRUE(web_state_->GetWebController().webUsageEnabled); |
| 283 | 259 |
| 284 web_state_->SetWebUsageEnabled(false); | 260 web_state_->SetWebUsageEnabled(false); |
| 285 EXPECT_FALSE(web_state_->IsWebUsageEnabled()); | 261 EXPECT_FALSE(web_state_->IsWebUsageEnabled()); |
| 286 EXPECT_FALSE(web_state_->GetWebController().webUsageEnabled); | 262 EXPECT_FALSE(web_state_->GetWebController().webUsageEnabled); |
| 287 } | 263 } |
| 288 | 264 |
| 289 TEST_F(WebStateTest, ShouldSuppressDialogs) { | 265 TEST_F(WebStateImplTest, ShouldSuppressDialogs) { |
| 290 // Default is false. | 266 // Default is false. |
| 291 ASSERT_FALSE(web_state_->ShouldSuppressDialogs()); | 267 ASSERT_FALSE(web_state_->ShouldSuppressDialogs()); |
| 292 | 268 |
| 293 web_state_->SetShouldSuppressDialogs(true); | 269 web_state_->SetShouldSuppressDialogs(true); |
| 294 EXPECT_TRUE(web_state_->ShouldSuppressDialogs()); | 270 EXPECT_TRUE(web_state_->ShouldSuppressDialogs()); |
| 295 EXPECT_TRUE(web_state_->GetWebController().shouldSuppressDialogs); | 271 EXPECT_TRUE(web_state_->GetWebController().shouldSuppressDialogs); |
| 296 | 272 |
| 297 web_state_->SetShouldSuppressDialogs(false); | 273 web_state_->SetShouldSuppressDialogs(false); |
| 298 EXPECT_FALSE(web_state_->ShouldSuppressDialogs()); | 274 EXPECT_FALSE(web_state_->ShouldSuppressDialogs()); |
| 299 EXPECT_FALSE(web_state_->GetWebController().shouldSuppressDialogs); | 275 EXPECT_FALSE(web_state_->GetWebController().shouldSuppressDialogs); |
| 300 } | 276 } |
| 301 | 277 |
| 302 TEST_F(WebStateTest, ResponseHeaders) { | 278 TEST_F(WebStateImplTest, ResponseHeaders) { |
| 303 GURL real_url("http://foo.com/bar"); | 279 GURL real_url("http://foo.com/bar"); |
| 304 GURL frame_url("http://frames-r-us.com/"); | 280 GURL frame_url("http://frames-r-us.com/"); |
| 305 scoped_refptr<net::HttpResponseHeaders> real_headers(HeadersFromString( | 281 scoped_refptr<net::HttpResponseHeaders> real_headers(HeadersFromString( |
| 306 "HTTP/1.1 200 OK\r\n" | 282 "HTTP/1.1 200 OK\r\n" |
| 307 "Content-Type: text/html\r\n" | 283 "Content-Type: text/html\r\n" |
| 308 "Content-Language: en\r\n" | 284 "Content-Language: en\r\n" |
| 309 "X-Should-Be-Here: yep\r\n" | 285 "X-Should-Be-Here: yep\r\n" |
| 310 "\r\n")); | 286 "\r\n")); |
| 311 scoped_refptr<net::HttpResponseHeaders> frame_headers(HeadersFromString( | 287 scoped_refptr<net::HttpResponseHeaders> frame_headers(HeadersFromString( |
| 312 "HTTP/1.1 200 OK\r\n" | 288 "HTTP/1.1 200 OK\r\n" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 325 EXPECT_TRUE( | 301 EXPECT_TRUE( |
| 326 web_state_->GetHttpResponseHeaders()->HasHeader("X-Should-Be-Here")); | 302 web_state_->GetHttpResponseHeaders()->HasHeader("X-Should-Be-Here")); |
| 327 EXPECT_FALSE( | 303 EXPECT_FALSE( |
| 328 web_state_->GetHttpResponseHeaders()->HasHeader("X-Should-Not-Be-Here")); | 304 web_state_->GetHttpResponseHeaders()->HasHeader("X-Should-Not-Be-Here")); |
| 329 | 305 |
| 330 // And that it was parsed correctly. | 306 // And that it was parsed correctly. |
| 331 EXPECT_EQ("text/html", web_state_->GetContentsMimeType()); | 307 EXPECT_EQ("text/html", web_state_->GetContentsMimeType()); |
| 332 EXPECT_EQ("en", web_state_->GetContentLanguageHeader()); | 308 EXPECT_EQ("en", web_state_->GetContentLanguageHeader()); |
| 333 } | 309 } |
| 334 | 310 |
| 335 TEST_F(WebStateTest, ResponseHeaderClearing) { | 311 TEST_F(WebStateImplTest, ResponseHeaderClearing) { |
| 336 GURL url("http://foo.com/"); | 312 GURL url("http://foo.com/"); |
| 337 scoped_refptr<net::HttpResponseHeaders> headers(HeadersFromString( | 313 scoped_refptr<net::HttpResponseHeaders> headers(HeadersFromString( |
| 338 "HTTP/1.1 200 OK\r\n" | 314 "HTTP/1.1 200 OK\r\n" |
| 339 "Content-Type: text/html\r\n" | 315 "Content-Type: text/html\r\n" |
| 340 "Content-Language: en\r\n" | 316 "Content-Language: en\r\n" |
| 341 "\r\n")); | 317 "\r\n")); |
| 342 web_state_->OnHttpResponseHeadersReceived(headers.get(), url); | 318 web_state_->OnHttpResponseHeadersReceived(headers.get(), url); |
| 343 | 319 |
| 344 // There should be no headers before loading. | 320 // There should be no headers before loading. |
| 345 EXPECT_EQ(NULL, web_state_->GetHttpResponseHeaders()); | 321 EXPECT_EQ(NULL, web_state_->GetHttpResponseHeaders()); |
| 346 | 322 |
| 347 // There should be headers and parsed values after loading. | 323 // There should be headers and parsed values after loading. |
| 348 web_state_->OnNavigationCommitted(url); | 324 web_state_->OnNavigationCommitted(url); |
| 349 EXPECT_TRUE(web_state_->GetHttpResponseHeaders()->HasHeader("Content-Type")); | 325 EXPECT_TRUE(web_state_->GetHttpResponseHeaders()->HasHeader("Content-Type")); |
| 350 EXPECT_NE("", web_state_->GetContentsMimeType()); | 326 EXPECT_NE("", web_state_->GetContentsMimeType()); |
| 351 EXPECT_NE("", web_state_->GetContentLanguageHeader()); | 327 EXPECT_NE("", web_state_->GetContentLanguageHeader()); |
| 352 | 328 |
| 353 // ... but not after loading another page, nor should there be specific | 329 // ... but not after loading another page, nor should there be specific |
| 354 // parsed values. | 330 // parsed values. |
| 355 web_state_->OnNavigationCommitted(GURL("http://elsewhere.com/")); | 331 web_state_->OnNavigationCommitted(GURL("http://elsewhere.com/")); |
| 356 EXPECT_EQ(NULL, web_state_->GetHttpResponseHeaders()); | 332 EXPECT_EQ(NULL, web_state_->GetHttpResponseHeaders()); |
| 357 EXPECT_EQ("", web_state_->GetContentsMimeType()); | 333 EXPECT_EQ("", web_state_->GetContentsMimeType()); |
| 358 EXPECT_EQ("", web_state_->GetContentLanguageHeader()); | 334 EXPECT_EQ("", web_state_->GetContentLanguageHeader()); |
| 359 } | 335 } |
| 360 | 336 |
| 361 TEST_F(WebStateTest, ObserverTest) { | 337 TEST_F(WebStateImplTest, ObserverTest) { |
| 362 std::unique_ptr<TestWebStateObserver> observer( | 338 std::unique_ptr<TestWebStateObserver> observer( |
| 363 new TestWebStateObserver(web_state_.get())); | 339 new TestWebStateObserver(web_state_.get())); |
| 364 EXPECT_EQ(web_state_.get(), observer->web_state()); | 340 EXPECT_EQ(web_state_.get(), observer->web_state()); |
| 365 | 341 |
| 366 // Test that ProvisionalNavigationStarted() is called. | 342 // Test that ProvisionalNavigationStarted() is called. |
| 367 EXPECT_FALSE(observer->provisional_navigation_started_called()); | 343 EXPECT_FALSE(observer->provisional_navigation_started_called()); |
| 368 web_state_->OnProvisionalNavigationStarted(GURL("http://test")); | 344 web_state_->OnProvisionalNavigationStarted(GURL("http://test")); |
| 369 EXPECT_TRUE(observer->provisional_navigation_started_called()); | 345 EXPECT_TRUE(observer->provisional_navigation_started_called()); |
| 370 | 346 |
| 371 // Test that NavigationItemsPruned() is called. | 347 // Test that NavigationItemsPruned() is called. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 | 386 |
| 411 // Test that WebStateDestroyed() is called. | 387 // Test that WebStateDestroyed() is called. |
| 412 EXPECT_FALSE(observer->web_state_destroyed_called()); | 388 EXPECT_FALSE(observer->web_state_destroyed_called()); |
| 413 web_state_.reset(); | 389 web_state_.reset(); |
| 414 EXPECT_TRUE(observer->web_state_destroyed_called()); | 390 EXPECT_TRUE(observer->web_state_destroyed_called()); |
| 415 | 391 |
| 416 EXPECT_EQ(nullptr, observer->web_state()); | 392 EXPECT_EQ(nullptr, observer->web_state()); |
| 417 } | 393 } |
| 418 | 394 |
| 419 // Tests that WebStateDelegate methods appropriately called. | 395 // Tests that WebStateDelegate methods appropriately called. |
| 420 TEST_F(WebStateTest, DelegateTest) { | 396 TEST_F(WebStateImplTest, DelegateTest) { |
| 421 TestWebStateDelegate delegate; | 397 TestWebStateDelegate delegate; |
| 422 web_state_->SetDelegate(&delegate); | 398 web_state_->SetDelegate(&delegate); |
| 423 | 399 |
| 424 // Test that HandleContextMenu() is called. | 400 // Test that HandleContextMenu() is called. |
| 425 EXPECT_FALSE(delegate.handle_context_menu_called()); | 401 EXPECT_FALSE(delegate.handle_context_menu_called()); |
| 426 web::ContextMenuParams context_menu_params; | 402 web::ContextMenuParams context_menu_params; |
| 427 web_state_->HandleContextMenu(context_menu_params); | 403 web_state_->HandleContextMenu(context_menu_params); |
| 428 EXPECT_TRUE(delegate.handle_context_menu_called()); | 404 EXPECT_TRUE(delegate.handle_context_menu_called()); |
| 429 | 405 |
| 430 // Test that ShowRepostFormWarningDialog() is called. | 406 // Test that ShowRepostFormWarningDialog() is called. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 ASSERT_TRUE(delegate.last_authentication_request()); | 443 ASSERT_TRUE(delegate.last_authentication_request()); |
| 468 EXPECT_EQ(delegate.last_authentication_request()->web_state, | 444 EXPECT_EQ(delegate.last_authentication_request()->web_state, |
| 469 web_state_.get()); | 445 web_state_.get()); |
| 470 EXPECT_EQ(delegate.last_authentication_request()->protection_space, | 446 EXPECT_EQ(delegate.last_authentication_request()->protection_space, |
| 471 protection_space.get()); | 447 protection_space.get()); |
| 472 EXPECT_EQ(delegate.last_authentication_request()->credential, | 448 EXPECT_EQ(delegate.last_authentication_request()->credential, |
| 473 credential.get()); | 449 credential.get()); |
| 474 } | 450 } |
| 475 | 451 |
| 476 // Verifies that GlobalWebStateObservers are called when expected. | 452 // Verifies that GlobalWebStateObservers are called when expected. |
| 477 TEST_F(WebStateTest, GlobalObserverTest) { | 453 TEST_F(WebStateImplTest, GlobalObserverTest) { |
| 478 std::unique_ptr<TestGlobalWebStateObserver> observer( | 454 std::unique_ptr<TestGlobalWebStateObserver> observer( |
| 479 new TestGlobalWebStateObserver()); | 455 new TestGlobalWebStateObserver()); |
| 480 | 456 |
| 481 // Test that NavigationItemsPruned() is called. | 457 // Test that NavigationItemsPruned() is called. |
| 482 EXPECT_FALSE(observer->navigation_items_pruned_called()); | 458 EXPECT_FALSE(observer->navigation_items_pruned_called()); |
| 483 web_state_->OnNavigationItemsPruned(1); | 459 web_state_->OnNavigationItemsPruned(1); |
| 484 EXPECT_TRUE(observer->navigation_items_pruned_called()); | 460 EXPECT_TRUE(observer->navigation_items_pruned_called()); |
| 485 | 461 |
| 486 // Test that NavigationItemChanged() is called. | 462 // Test that NavigationItemChanged() is called. |
| 487 EXPECT_FALSE(observer->navigation_item_changed_called()); | 463 EXPECT_FALSE(observer->navigation_item_changed_called()); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 511 web_state_->OnPageLoaded(GURL("http://test"), true); | 487 web_state_->OnPageLoaded(GURL("http://test"), true); |
| 512 EXPECT_TRUE(observer->page_loaded_called_with_success()); | 488 EXPECT_TRUE(observer->page_loaded_called_with_success()); |
| 513 | 489 |
| 514 // Test that WebStateDestroyed() is called. | 490 // Test that WebStateDestroyed() is called. |
| 515 EXPECT_FALSE(observer->web_state_destroyed_called()); | 491 EXPECT_FALSE(observer->web_state_destroyed_called()); |
| 516 web_state_.reset(); | 492 web_state_.reset(); |
| 517 EXPECT_TRUE(observer->web_state_destroyed_called()); | 493 EXPECT_TRUE(observer->web_state_destroyed_called()); |
| 518 } | 494 } |
| 519 | 495 |
| 520 // Verifies that policy deciders are correctly called by the web state. | 496 // Verifies that policy deciders are correctly called by the web state. |
| 521 TEST_F(WebStateTest, PolicyDeciderTest) { | 497 TEST_F(WebStateImplTest, PolicyDeciderTest) { |
| 522 MockWebStatePolicyDecider decider(web_state_.get()); | 498 MockWebStatePolicyDecider decider(web_state_.get()); |
| 523 MockWebStatePolicyDecider decider2(web_state_.get()); | 499 MockWebStatePolicyDecider decider2(web_state_.get()); |
| 524 EXPECT_EQ(web_state_.get(), decider.web_state()); | 500 EXPECT_EQ(web_state_.get(), decider.web_state()); |
| 525 | 501 |
| 526 // Test that ShouldAllowRequest() is called. | 502 // Test that ShouldAllowRequest() is called. |
| 527 EXPECT_CALL(decider, ShouldAllowRequest(_)).Times(1).WillOnce(Return(true)); | 503 EXPECT_CALL(decider, ShouldAllowRequest(_)).Times(1).WillOnce(Return(true)); |
| 528 EXPECT_CALL(decider2, ShouldAllowRequest(_)).Times(1).WillOnce(Return(true)); | 504 EXPECT_CALL(decider2, ShouldAllowRequest(_)).Times(1).WillOnce(Return(true)); |
| 529 EXPECT_TRUE(web_state_->ShouldAllowRequest(nil)); | 505 EXPECT_TRUE(web_state_->ShouldAllowRequest(nil)); |
| 530 | 506 |
| 531 // Test that ShouldAllowRequest() is stopping on negative answer. Only one | 507 // Test that ShouldAllowRequest() is stopping on negative answer. Only one |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 564 } | 540 } |
| 565 | 541 |
| 566 // Test that WebStateDestroyed() is called. | 542 // Test that WebStateDestroyed() is called. |
| 567 EXPECT_CALL(decider, WebStateDestroyed()).Times(1); | 543 EXPECT_CALL(decider, WebStateDestroyed()).Times(1); |
| 568 EXPECT_CALL(decider2, WebStateDestroyed()).Times(1); | 544 EXPECT_CALL(decider2, WebStateDestroyed()).Times(1); |
| 569 web_state_.reset(); | 545 web_state_.reset(); |
| 570 EXPECT_EQ(nullptr, decider.web_state()); | 546 EXPECT_EQ(nullptr, decider.web_state()); |
| 571 } | 547 } |
| 572 | 548 |
| 573 // Tests that script command callbacks are called correctly. | 549 // Tests that script command callbacks are called correctly. |
| 574 TEST_F(WebStateTest, ScriptCommand) { | 550 TEST_F(WebStateImplTest, ScriptCommand) { |
| 575 // Set up two script command callbacks. | 551 // Set up two script command callbacks. |
| 576 const std::string kPrefix1("prefix1"); | 552 const std::string kPrefix1("prefix1"); |
| 577 const std::string kCommand1("prefix1.command1"); | 553 const std::string kCommand1("prefix1.command1"); |
| 578 base::DictionaryValue value_1; | 554 base::DictionaryValue value_1; |
| 579 value_1.SetString("a", "b"); | 555 value_1.SetString("a", "b"); |
| 580 const GURL kUrl1("http://foo"); | 556 const GURL kUrl1("http://foo"); |
| 581 bool is_called_1 = false; | 557 bool is_called_1 = false; |
| 582 web_state_->AddScriptCommandCallback( | 558 web_state_->AddScriptCommandCallback( |
| 583 base::Bind(&HandleScriptCommand, &is_called_1, true, &value_1, kUrl1), | 559 base::Bind(&HandleScriptCommand, &is_called_1, true, &value_1, kUrl1), |
| 584 kPrefix1); | 560 kPrefix1); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 621 | 597 |
| 622 // Check that a false return value is forwarded correctly. | 598 // Check that a false return value is forwarded correctly. |
| 623 EXPECT_FALSE( | 599 EXPECT_FALSE( |
| 624 web_state_->OnScriptCommandReceived(kCommand2, value_2, kUrl2, false)); | 600 web_state_->OnScriptCommandReceived(kCommand2, value_2, kUrl2, false)); |
| 625 EXPECT_FALSE(is_called_1); | 601 EXPECT_FALSE(is_called_1); |
| 626 EXPECT_TRUE(is_called_2); | 602 EXPECT_TRUE(is_called_2); |
| 627 | 603 |
| 628 web_state_->RemoveScriptCommandCallback(kPrefix2); | 604 web_state_->RemoveScriptCommandCallback(kPrefix2); |
| 629 } | 605 } |
| 630 | 606 |
| 631 // Tests script execution with and without callback. | |
| 632 TEST_F(WebStateTest, ScriptExecution) { | |
| 633 LoadHtml("<html></html>"); | |
| 634 | |
| 635 // Execute script without callback. | |
| 636 web_state_->ExecuteJavaScript(base::UTF8ToUTF16("window.foo = 'bar'")); | |
| 637 | |
| 638 // Execute script with callback. | |
| 639 __block std::unique_ptr<base::Value> execution_result; | |
| 640 __block bool execution_complete = false; | |
| 641 web_state_->ExecuteJavaScript(base::UTF8ToUTF16("window.foo"), | |
| 642 base::BindBlock(^(const base::Value* value) { | |
| 643 execution_result = value->CreateDeepCopy(); | |
| 644 execution_complete = true; | |
| 645 })); | |
| 646 base::test::ios::WaitUntilCondition(^{ | |
| 647 return execution_complete; | |
| 648 }); | |
| 649 | |
| 650 ASSERT_TRUE(execution_result); | |
| 651 std::string string_result; | |
| 652 execution_result->GetAsString(&string_result); | |
| 653 EXPECT_EQ("bar", string_result); | |
| 654 } | |
| 655 | |
| 656 // Tests loading progress. | |
| 657 TEST_F(WebStateTest, LoadingProgress) { | |
| 658 EXPECT_FLOAT_EQ(0.0, web_state_->GetLoadingProgress()); | |
| 659 LoadHtml("<html></html>"); | |
| 660 base::test::ios::WaitUntilCondition(^bool() { | |
| 661 return web_state_->GetLoadingProgress() == 1.0; | |
| 662 }); | |
| 663 } | |
| 664 | |
| 665 // Tests that page which overrides window.webkit object does not break the | |
| 666 // messaging system. | |
| 667 TEST_F(WebStateTest, OverridingWebKitObject) { | |
| 668 // Add a script command handler. | |
| 669 __block bool message_received = false; | |
| 670 const web::WebState::ScriptCommandCallback callback = | |
| 671 base::BindBlock(^bool(const base::DictionaryValue&, const GURL&, bool) { | |
| 672 message_received = true; | |
| 673 return true; | |
| 674 }); | |
| 675 web_state_->AddScriptCommandCallback(callback, "test"); | |
| 676 | |
| 677 // Load the page which overrides window.webkit object and wait until the | |
| 678 // test message is received. | |
| 679 LoadHtml( | |
| 680 "<script>" | |
| 681 " webkit = undefined;" | |
| 682 " __gCrWeb.message.invokeOnHost({'command': 'test.webkit-overriding'});" | |
| 683 "</script>"); | |
| 684 | |
| 685 base::test::ios::WaitUntilCondition(^{ | |
| 686 return message_received; | |
| 687 }); | |
| 688 web_state_->RemoveScriptCommandCallback("test"); | |
| 689 } | |
| 690 | |
| 691 } // namespace | |
| 692 } // namespace web | 607 } // namespace web |
| OLD | NEW |