| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 "ios/web/public/web_state/web_state.h" |
| 6 |
| 7 #include "base/mac/bind_objc_block.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/values.h" |
| 10 #import "ios/web/public/test/web_test_with_web_state.h" |
| 11 |
| 12 namespace web { |
| 13 |
| 14 // Test fixture for web::WebTest class. |
| 15 typedef web::WebTestWithWebState WebStateTest; |
| 16 |
| 17 // Tests script execution with and without callback. |
| 18 TEST_F(WebStateTest, ScriptExecution) { |
| 19 LoadHtml("<html></html>"); |
| 20 |
| 21 // Execute script without callback. |
| 22 web_state()->ExecuteJavaScript(base::UTF8ToUTF16("window.foo = 'bar'")); |
| 23 |
| 24 // Execute script with callback. |
| 25 __block std::unique_ptr<base::Value> execution_result; |
| 26 __block bool execution_complete = false; |
| 27 web_state()->ExecuteJavaScript(base::UTF8ToUTF16("window.foo"), |
| 28 base::BindBlock(^(const base::Value* value) { |
| 29 execution_result = value->CreateDeepCopy(); |
| 30 execution_complete = true; |
| 31 })); |
| 32 WaitForCondition(^{ |
| 33 return execution_complete; |
| 34 }); |
| 35 |
| 36 ASSERT_TRUE(execution_result); |
| 37 std::string string_result; |
| 38 execution_result->GetAsString(&string_result); |
| 39 EXPECT_EQ("bar", string_result); |
| 40 } |
| 41 |
| 42 // Tests loading progress. |
| 43 TEST_F(WebStateTest, LoadingProgress) { |
| 44 EXPECT_FLOAT_EQ(0.0, web_state()->GetLoadingProgress()); |
| 45 LoadHtml("<html></html>"); |
| 46 WaitForCondition(^bool() { |
| 47 return web_state()->GetLoadingProgress() == 1.0; |
| 48 }); |
| 49 } |
| 50 |
| 51 // Tests that page which overrides window.webkit object does not break the |
| 52 // messaging system. |
| 53 TEST_F(WebStateTest, OverridingWebKitObject) { |
| 54 // Add a script command handler. |
| 55 __block bool message_received = false; |
| 56 const web::WebState::ScriptCommandCallback callback = |
| 57 base::BindBlock(^bool(const base::DictionaryValue&, const GURL&, bool) { |
| 58 message_received = true; |
| 59 return true; |
| 60 }); |
| 61 web_state()->AddScriptCommandCallback(callback, "test"); |
| 62 |
| 63 // Load the page which overrides window.webkit object and wait until the |
| 64 // test message is received. |
| 65 LoadHtml( |
| 66 "<script>" |
| 67 " webkit = undefined;" |
| 68 " __gCrWeb.message.invokeOnHost({'command': 'test.webkit-overriding'});" |
| 69 "</script>"); |
| 70 |
| 71 WaitForCondition(^{ |
| 72 return message_received; |
| 73 }); |
| 74 web_state()->RemoveScriptCommandCallback("test"); |
| 75 } |
| 76 |
| 77 } // namespace web |
| OLD | NEW |