OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 <vector> | 5 #include <vector> |
6 | |
7 #import <CoreGraphics/CoreGraphics.h> | |
6 #import <Foundation/Foundation.h> | 8 #import <Foundation/Foundation.h> |
7 | 9 |
8 #include "base/macros.h" | 10 #include "base/macros.h" |
9 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
10 #import "base/strings/sys_string_conversions.h" | 12 #import "base/strings/sys_string_conversions.h" |
11 #import "ios/web/public/test/web_test_with_web_state.h" | 13 #import "ios/web/public/test/web_test_with_web_state.h" |
14 #import "ios/web/public/web_state/ui/crw_web_view_proxy.h" | |
15 #import "ios/web/public/web_state/ui/crw_web_view_scroll_view_proxy.h" | |
16 #import "ios/web/public/web_state/web_state.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
13 #import "testing/gtest_mac.h" | 18 #import "testing/gtest_mac.h" |
14 | 19 |
15 // Unit tests for ios/web/web_state/js/resources/core.js. | 20 // Unit tests for ios/web/web_state/js/resources/core.js. |
16 | 21 |
17 namespace { | 22 namespace { |
18 | 23 |
19 struct TestScriptAndExpectedValue { | 24 struct TestScriptAndExpectedValue { |
danyao
2017/04/10 17:53:07
Can we remove this struct now? It's no longer used
danyao
2017/04/10 18:32:56
Never mind. I was blind and missed the usage in st
| |
20 NSString* test_script; | 25 NSString* test_script; |
21 id expected_value; | 26 id expected_value; |
22 }; | 27 }; |
23 | 28 |
29 // Test coordinates and expected result for __gCrWeb.getElementFromPoint call. | |
30 struct TestCoordinatesAndExpectedValue { | |
31 TestCoordinatesAndExpectedValue(CGFloat x, CGFloat y, id expected_value) | |
32 : x(x), y(y), expected_value(expected_value) {} | |
33 CGFloat x = 0; | |
34 CGFloat y = 0; | |
35 id expected_value = nil; | |
36 }; | |
37 | |
24 } // namespace | 38 } // namespace |
25 | 39 |
26 namespace web { | 40 namespace web { |
27 | 41 |
28 // Test fixture to test core.js. | 42 // Test fixture to test core.js. |
29 class CoreJsTest : public web::WebTestWithWebState { | 43 class CoreJsTest : public web::WebTestWithWebState { |
30 protected: | 44 protected: |
31 // Verifies that __gCrWeb.getElementFromPoint returns |expected_value| for | 45 // Verifies that __gCrWeb.getElementFromPoint returns |expected_value| for |
32 // the given image |html|. | 46 // the given image |html|. |
33 void ImageTesterHelper(NSString* html, NSDictionary* expected_value) { | 47 void ImageTesterHelper(NSString* html, NSDictionary* expected_value) { |
34 NSString* page_content_template = | 48 NSString* page_content_template = |
35 @"<html><body style='margin-left:10px;margin-top:10px;'>" | 49 @"<html><body style='margin-left:10px;margin-top:10px;'>" |
36 "<div style='width:100px;height:100px;'>" | 50 "<div style='width:100px;height:100px;'>" |
37 " <p style='position:absolute;left:25px;top:25px;" | 51 " <p style='position:absolute;left:25px;top:25px;" |
38 " width:50px;height:50px'>" | 52 " width:50px;height:50px'>" |
39 "%@" | 53 "%@" |
40 " Chrome rocks!" | 54 " Chrome rocks!" |
41 " </p></div></body></html>"; | 55 " </p></div></body></html>"; |
42 NSString* page_content = | 56 NSString* page_content = |
43 [NSString stringWithFormat:page_content_template, html]; | 57 [NSString stringWithFormat:page_content_template, html]; |
44 | 58 |
45 TestScriptAndExpectedValue test_data[] = { | 59 TestCoordinatesAndExpectedValue test_data[] = { |
46 // Point outside the document margins. | 60 // Point outside the document margins. |
47 { | 61 {0, 0, @{}}, |
48 @"__gCrWeb.getElementFromPoint(0, 0)", @{}, | 62 // Point inside the <p> element. |
49 }, | 63 {20, 20, expected_value}, |
50 // Point outside the <p> element. | 64 // Point outside the <p> element. |
51 {@"__gCrWeb.getElementFromPoint(100, 100)", expected_value}, | 65 {GetWebViewContentSize().width / 2, 50, @{}}, |
52 // Point inside the <p> element. | |
53 { | |
54 @"__gCrWeb.getElementFromPoint(300, 300)", @{}, | |
55 }, | |
56 }; | 66 }; |
57 for (size_t i = 0; i < arraysize(test_data); i++) { | 67 for (size_t i = 0; i < arraysize(test_data); i++) { |
58 TestScriptAndExpectedValue& data = test_data[i]; | 68 const TestCoordinatesAndExpectedValue& data = test_data[i]; |
59 LoadHtml(page_content); | 69 LoadHtml(page_content); |
60 id result = ExecuteJavaScript(data.test_script); | 70 id result = ExecuteGetElementFromPointJavaScript(data.x, data.y); |
61 EXPECT_NSEQ(data.expected_value, result) | 71 EXPECT_NSEQ(data.expected_value, result) |
62 << " in test " << i << ": " | 72 << " in test " << i << ": (" << data.x << ", " << data.y << ")"; |
63 << base::SysNSStringToUTF8(data.test_script); | |
64 } | 73 } |
65 } | 74 } |
75 // Returns web view's content size from the current web state. | |
76 CGSize GetWebViewContentSize() { | |
77 return web_state()->GetWebViewProxy().scrollViewProxy.contentSize; | |
78 } | |
79 | |
80 // Executes __gCrWeb.getElementFromPoint script and syncronously returns the | |
81 // result. |x| and |y| are points in web view coordinate system. | |
82 id ExecuteGetElementFromPointJavaScript(CGFloat x, CGFloat y) { | |
83 CGSize contentSize = GetWebViewContentSize(); | |
84 NSString* const script = [NSString | |
85 stringWithFormat:@"__gCrWeb.getElementFromPoint(%g, %g, %g, %g)", x, y, | |
86 contentSize.width, contentSize.height]; | |
87 return ExecuteJavaScript(script); | |
88 } | |
66 }; | 89 }; |
67 | 90 |
68 // Tests that __gCrWeb.getElementFromPoint function returns correct src. | 91 // Tests that __gCrWeb.getElementFromPoint function returns correct src. |
69 TEST_F(CoreJsTest, GetImageUrlAtPoint) { | 92 TEST_F(CoreJsTest, GetImageUrlAtPoint) { |
70 NSString* html = | 93 NSString* html = |
71 @"<img id='foo' style='width:200;height:200;' src='file:///bogus'/>"; | 94 @"<img id='foo' style='width:200;height:200;' src='file:///bogus'/>"; |
72 NSDictionary* expected_value = @{ | 95 NSDictionary* expected_value = @{ |
73 @"src" : @"file:///bogus", | 96 @"src" : @"file:///bogus", |
74 @"referrerPolicy" : @"default", | 97 @"referrerPolicy" : @"default", |
75 }; | 98 }; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
113 " style='position:absolute;left:5px;top:5px; " | 136 " style='position:absolute;left:5px;top:5px; " |
114 "width:40px;height:40px'/>" | 137 "width:40px;height:40px'/>" |
115 "</div></body> </html>"; | 138 "</div></body> </html>"; |
116 | 139 |
117 NSDictionary* success = @{ | 140 NSDictionary* success = @{ |
118 @"src" : @"file:///bogus", | 141 @"src" : @"file:///bogus", |
119 @"referrerPolicy" : @"default", | 142 @"referrerPolicy" : @"default", |
120 }; | 143 }; |
121 NSDictionary* failure = @{}; | 144 NSDictionary* failure = @{}; |
122 | 145 |
123 TestScriptAndExpectedValue test_data[] = { | 146 TestCoordinatesAndExpectedValue test_data[] = { |
124 {@"__gCrWeb.getElementFromPoint(2, 20)", success}, | 147 {2, 20, success}, {10, 10, failure}, |
125 {@"__gCrWeb.getElementFromPoint(5, 20)", failure}, | |
126 }; | 148 }; |
127 | 149 |
128 for (size_t i = 0; i < arraysize(test_data); i++) { | 150 for (size_t i = 0; i < arraysize(test_data); i++) { |
129 TestScriptAndExpectedValue& data = test_data[i]; | 151 const TestCoordinatesAndExpectedValue& data = test_data[i]; |
130 LoadHtml(html); | 152 LoadHtml(html); |
131 id result = ExecuteJavaScript(data.test_script); | 153 id result = ExecuteGetElementFromPointJavaScript(data.x, data.y); |
132 EXPECT_NSEQ(data.expected_value, result) | 154 EXPECT_NSEQ(data.expected_value, result) |
133 << " in test " << i << ": " | 155 << " in test " << i << ": (" << data.x << ", " << data.y << ")"; |
134 << base::SysNSStringToUTF8(data.test_script); | |
135 } | 156 } |
136 } | 157 } |
137 | 158 |
138 struct TestDataForPasswordFormDetection { | 159 struct TestDataForPasswordFormDetection { |
139 NSString* pageContent; | 160 NSString* pageContent; |
140 NSNumber* containsPassword; | 161 NSNumber* containsPassword; |
141 }; | 162 }; |
142 | 163 |
143 TEST_F(CoreJsTest, HasPasswordField) { | 164 TEST_F(CoreJsTest, HasPasswordField) { |
144 TestDataForPasswordFormDetection testData[] = { | 165 TestDataForPasswordFormDetection testData[] = { |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
237 } | 258 } |
238 | 259 |
239 // Tests the javascript of the url of the an image present in the DOM. | 260 // Tests the javascript of the url of the an image present in the DOM. |
240 TEST_F(CoreJsTest, LinkOfImage) { | 261 TEST_F(CoreJsTest, LinkOfImage) { |
241 // A page with a large image surrounded by a link. | 262 // A page with a large image surrounded by a link. |
242 static const char image[] = | 263 static const char image[] = |
243 "<a href='%s'><img width=400 height=400 src='foo'></img></a>"; | 264 "<a href='%s'><img width=400 height=400 src='foo'></img></a>"; |
244 | 265 |
245 // A page with a link to a destination URL. | 266 // A page with a link to a destination URL. |
246 LoadHtml(base::StringPrintf(image, "http://destination")); | 267 LoadHtml(base::StringPrintf(image, "http://destination")); |
247 id result = ExecuteJavaScript(@"__gCrWeb['getElementFromPoint'](200, 200)"); | 268 id result = ExecuteGetElementFromPointJavaScript(20, 20); |
248 NSDictionary* expected_result = @{ | 269 NSDictionary* expected_result = @{ |
249 @"src" : [NSString stringWithFormat:@"%sfoo", BaseUrl().c_str()], | 270 @"src" : [NSString stringWithFormat:@"%sfoo", BaseUrl().c_str()], |
250 @"referrerPolicy" : @"default", | 271 @"referrerPolicy" : @"default", |
251 @"href" : @"http://destination/", | 272 @"href" : @"http://destination/", |
252 }; | 273 }; |
253 EXPECT_NSEQ(expected_result, result); | 274 EXPECT_NSEQ(expected_result, result); |
254 | 275 |
255 // A page with a link with some JavaScript that does not result in a NOP. | 276 // A page with a link with some JavaScript that does not result in a NOP. |
256 LoadHtml(base::StringPrintf(image, "javascript:console.log('whatever')")); | 277 LoadHtml(base::StringPrintf(image, "javascript:console.log('whatever')")); |
257 result = ExecuteJavaScript(@"__gCrWeb['getElementFromPoint'](200, 200)"); | 278 result = ExecuteGetElementFromPointJavaScript(20, 20); |
258 expected_result = @{ | 279 expected_result = @{ |
259 @"src" : [NSString stringWithFormat:@"%sfoo", BaseUrl().c_str()], | 280 @"src" : [NSString stringWithFormat:@"%sfoo", BaseUrl().c_str()], |
260 @"referrerPolicy" : @"default", | 281 @"referrerPolicy" : @"default", |
261 @"href" : @"javascript:console.log(", | 282 @"href" : @"javascript:console.log(", |
262 }; | 283 }; |
263 EXPECT_NSEQ(expected_result, result); | 284 EXPECT_NSEQ(expected_result, result); |
264 | 285 |
265 // A list of JavaScripts that result in a NOP. | 286 // A list of JavaScripts that result in a NOP. |
266 std::vector<std::string> nop_javascripts; | 287 std::vector<std::string> nop_javascripts; |
267 nop_javascripts.push_back(";"); | 288 nop_javascripts.push_back(";"); |
268 nop_javascripts.push_back("void(0);"); | 289 nop_javascripts.push_back("void(0);"); |
269 nop_javascripts.push_back("void(0); void(0); void(0)"); | 290 nop_javascripts.push_back("void(0); void(0); void(0)"); |
270 | 291 |
271 for (auto js : nop_javascripts) { | 292 for (auto js : nop_javascripts) { |
272 // A page with a link with some JavaScript that results in a NOP. | 293 // A page with a link with some JavaScript that results in a NOP. |
273 const std::string javascript = std::string("javascript:") + js; | 294 const std::string javascript = std::string("javascript:") + js; |
274 LoadHtml(base::StringPrintf(image, javascript.c_str())); | 295 LoadHtml(base::StringPrintf(image, javascript.c_str())); |
275 result = ExecuteJavaScript(@"__gCrWeb['getElementFromPoint'](200, 200)"); | 296 result = ExecuteGetElementFromPointJavaScript(20, 20); |
276 expected_result = @{ | 297 expected_result = @{ |
277 @"src" : [NSString stringWithFormat:@"%sfoo", BaseUrl().c_str()], | 298 @"src" : [NSString stringWithFormat:@"%sfoo", BaseUrl().c_str()], |
278 @"referrerPolicy" : @"default", | 299 @"referrerPolicy" : @"default", |
279 }; | 300 }; |
280 // Make sure the returned JSON does not have an 'href' key. | 301 // Make sure the returned JSON does not have an 'href' key. |
281 EXPECT_NSEQ(expected_result, result); | 302 EXPECT_NSEQ(expected_result, result); |
282 } | 303 } |
283 } | 304 } |
284 | 305 |
285 } // namespace web | 306 } // namespace web |
OLD | NEW |