OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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/public/web_state/web_state.h" | 5 #import "ios/web/public/web_state/web_state.h" |
6 | 6 |
7 #include "base/mac/bind_objc_block.h" | 7 #include "base/mac/bind_objc_block.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #import "ios/web/public/navigation_manager.h" | 10 #import "ios/web/public/navigation_manager.h" |
11 #import "ios/web/public/test/web_test_with_web_state.h" | 11 #import "ios/web/public/test/web_test_with_web_state.h" |
12 | 12 |
| 13 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 14 #error "This file requires ARC support." |
| 15 #endif |
| 16 |
13 namespace web { | 17 namespace web { |
14 | 18 |
15 // Test fixture for web::WebTest class. | 19 // Test fixture for web::WebTest class. |
16 typedef web::WebTestWithWebState WebStateTest; | 20 typedef web::WebTestWithWebState WebStateTest; |
17 | 21 |
18 // Tests script execution with and without callback. | 22 // Tests script execution with and without callback. |
19 TEST_F(WebStateTest, ScriptExecution) { | 23 TEST_F(WebStateTest, ScriptExecution) { |
20 LoadHtml("<html></html>"); | 24 LoadHtml("<html></html>"); |
21 | 25 |
22 // Execute script without callback. | 26 // Execute script without callback. |
23 web_state()->ExecuteJavaScript(base::UTF8ToUTF16("window.foo = 'bar'")); | 27 web_state()->ExecuteJavaScript(base::UTF8ToUTF16("window.foo = 'bar'")); |
24 | 28 |
25 // Execute script with callback. | 29 // Execute script with callback. |
26 __block std::unique_ptr<base::Value> execution_result; | 30 __block std::unique_ptr<base::Value> execution_result; |
27 __block bool execution_complete = false; | 31 __block bool execution_complete = false; |
28 web_state()->ExecuteJavaScript(base::UTF8ToUTF16("window.foo"), | 32 web_state()->ExecuteJavaScript( |
29 base::BindBlock(^(const base::Value* value) { | 33 base::UTF8ToUTF16("window.foo"), |
30 execution_result = value->CreateDeepCopy(); | 34 base::BindBlockArc(^(const base::Value* value) { |
31 execution_complete = true; | 35 execution_result = value->CreateDeepCopy(); |
32 })); | 36 execution_complete = true; |
| 37 })); |
33 WaitForCondition(^{ | 38 WaitForCondition(^{ |
34 return execution_complete; | 39 return execution_complete; |
35 }); | 40 }); |
36 | 41 |
37 ASSERT_TRUE(execution_result); | 42 ASSERT_TRUE(execution_result); |
38 std::string string_result; | 43 std::string string_result; |
39 execution_result->GetAsString(&string_result); | 44 execution_result->GetAsString(&string_result); |
40 EXPECT_EQ("bar", string_result); | 45 EXPECT_EQ("bar", string_result); |
41 } | 46 } |
42 | 47 |
43 // Tests loading progress. | 48 // Tests loading progress. |
44 TEST_F(WebStateTest, LoadingProgress) { | 49 TEST_F(WebStateTest, LoadingProgress) { |
45 EXPECT_FLOAT_EQ(0.0, web_state()->GetLoadingProgress()); | 50 EXPECT_FLOAT_EQ(0.0, web_state()->GetLoadingProgress()); |
46 LoadHtml("<html></html>"); | 51 LoadHtml("<html></html>"); |
47 WaitForCondition(^bool() { | 52 WaitForCondition(^bool() { |
48 return web_state()->GetLoadingProgress() == 1.0; | 53 return web_state()->GetLoadingProgress() == 1.0; |
49 }); | 54 }); |
50 } | 55 } |
51 | 56 |
52 // Tests that page which overrides window.webkit object does not break the | 57 // Tests that page which overrides window.webkit object does not break the |
53 // messaging system. | 58 // messaging system. |
54 TEST_F(WebStateTest, OverridingWebKitObject) { | 59 TEST_F(WebStateTest, OverridingWebKitObject) { |
55 // Add a script command handler. | 60 // Add a script command handler. |
56 __block bool message_received = false; | 61 __block bool message_received = false; |
57 const web::WebState::ScriptCommandCallback callback = | 62 const web::WebState::ScriptCommandCallback callback = base::BindBlockArc( |
58 base::BindBlock(^bool(const base::DictionaryValue&, const GURL&, bool) { | 63 ^bool(const base::DictionaryValue&, const GURL&, bool) { |
59 message_received = true; | 64 message_received = true; |
60 return true; | 65 return true; |
61 }); | 66 }); |
62 web_state()->AddScriptCommandCallback(callback, "test"); | 67 web_state()->AddScriptCommandCallback(callback, "test"); |
63 | 68 |
64 // Load the page which overrides window.webkit object and wait until the | 69 // Load the page which overrides window.webkit object and wait until the |
65 // test message is received. | 70 // test message is received. |
66 LoadHtml( | 71 LoadHtml( |
67 "<script>" | 72 "<script>" |
68 " webkit = undefined;" | 73 " webkit = undefined;" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 | 106 |
102 navigation_manager->Reload(web::ReloadType::ORIGINAL_REQUEST_URL, | 107 navigation_manager->Reload(web::ReloadType::ORIGINAL_REQUEST_URL, |
103 false /* check_for_repost */); | 108 false /* check_for_repost */); |
104 | 109 |
105 ASSERT_FALSE(navigation_manager->GetTransientItem()); | 110 ASSERT_FALSE(navigation_manager->GetTransientItem()); |
106 ASSERT_FALSE(navigation_manager->GetPendingItem()); | 111 ASSERT_FALSE(navigation_manager->GetPendingItem()); |
107 ASSERT_FALSE(navigation_manager->GetLastCommittedItem()); | 112 ASSERT_FALSE(navigation_manager->GetLastCommittedItem()); |
108 } | 113 } |
109 | 114 |
110 } // namespace web | 115 } // namespace web |
OLD | NEW |