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

Side by Side Diff: ios/web/web_state/js/context_menu_js_unittest.mm

Issue 2812573004: Move unit tests for getElementFromPoint to context_menu_js_unittest. (Closed)
Patch Set: Created 3 years, 8 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 | « ios/web/BUILD.gn ('k') | ios/web/web_state/js/core_js_unittest.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 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 #include <vector> 5 #include <vector>
6 6
7 #import <CoreGraphics/CoreGraphics.h> 7 #import <CoreGraphics/CoreGraphics.h>
8 #import <Foundation/Foundation.h> 8 #import <Foundation/Foundation.h>
9 9
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #import "base/strings/sys_string_conversions.h"
13 #import "ios/web/public/test/web_test_with_web_state.h" 12 #import "ios/web/public/test/web_test_with_web_state.h"
14 #import "ios/web/public/web_state/ui/crw_web_view_proxy.h" 13 #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" 14 #import "ios/web/public/web_state/ui/crw_web_view_scroll_view_proxy.h"
16 #import "ios/web/public/web_state/web_state.h" 15 #import "ios/web/public/web_state/web_state.h"
17 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
18 #import "testing/gtest_mac.h" 17 #include "testing/gtest_mac.h"
19 18
20 // Unit tests for ios/web/web_state/js/resources/core.js. 19 // Unit tests for ios/web/web_state/js/resources/context_menu.js.
21 20
22 namespace { 21 namespace {
23 22
24 struct TestScriptAndExpectedValue {
25 NSString* test_script;
26 id expected_value;
27 };
28
29 // Test coordinates and expected result for __gCrWeb.getElementFromPoint call. 23 // Test coordinates and expected result for __gCrWeb.getElementFromPoint call.
30 struct TestCoordinatesAndExpectedValue { 24 struct TestCoordinatesAndExpectedValue {
31 TestCoordinatesAndExpectedValue(CGFloat x, CGFloat y, id expected_value) 25 TestCoordinatesAndExpectedValue(CGFloat x, CGFloat y, id expected_value)
32 : x(x), y(y), expected_value(expected_value) {} 26 : x(x), y(y), expected_value(expected_value) {}
33 CGFloat x = 0; 27 CGFloat x = 0;
34 CGFloat y = 0; 28 CGFloat y = 0;
35 id expected_value = nil; 29 id expected_value = nil;
36 }; 30 };
37 31
38 } // namespace 32 } // namespace
39 33
40 namespace web { 34 namespace web {
41 35
42 // Test fixture to test core.js. 36 // Test fixture to test context_menu.js.
43 class CoreJsTest : public web::WebTestWithWebState { 37 class ContextMenuJsTest : public web::WebTestWithWebState {
44 protected: 38 protected:
45 // Verifies that __gCrWeb.getElementFromPoint returns |expected_value| for 39 // Verifies that __gCrWeb.getElementFromPoint returns |expected_value| for
46 // the given image |html|. 40 // the given image |html|.
47 void ImageTesterHelper(NSString* html, NSDictionary* expected_value) { 41 void ImageTesterHelper(NSString* html, NSDictionary* expected_value) {
48 NSString* page_content_template = 42 NSString* page_content_template =
49 @"<html><body style='margin-left:10px;margin-top:10px;'>" 43 @"<html><body style='margin-left:10px;margin-top:10px;'>"
50 "<div style='width:100px;height:100px;'>" 44 "<div style='width:100px;height:100px;'>"
51 " <p style='position:absolute;left:25px;top:25px;" 45 " <p style='position:absolute;left:25px;top:25px;"
52 " width:50px;height:50px'>" 46 " width:50px;height:50px'>"
53 "%@" 47 "%@"
(...skipping 28 matching lines...) Expand all
82 id ExecuteGetElementFromPointJavaScript(CGFloat x, CGFloat y) { 76 id ExecuteGetElementFromPointJavaScript(CGFloat x, CGFloat y) {
83 CGSize contentSize = GetWebViewContentSize(); 77 CGSize contentSize = GetWebViewContentSize();
84 NSString* const script = [NSString 78 NSString* const script = [NSString
85 stringWithFormat:@"__gCrWeb.getElementFromPoint(%g, %g, %g, %g)", x, y, 79 stringWithFormat:@"__gCrWeb.getElementFromPoint(%g, %g, %g, %g)", x, y,
86 contentSize.width, contentSize.height]; 80 contentSize.width, contentSize.height];
87 return ExecuteJavaScript(script); 81 return ExecuteJavaScript(script);
88 } 82 }
89 }; 83 };
90 84
91 // Tests that __gCrWeb.getElementFromPoint function returns correct src. 85 // Tests that __gCrWeb.getElementFromPoint function returns correct src.
92 TEST_F(CoreJsTest, GetImageUrlAtPoint) { 86 TEST_F(ContextMenuJsTest, GetImageUrlAtPoint) {
93 NSString* html = 87 NSString* html =
94 @"<img id='foo' style='width:200;height:200;' src='file:///bogus'/>"; 88 @"<img id='foo' style='width:200;height:200;' src='file:///bogus'/>";
95 NSDictionary* expected_value = @{ 89 NSDictionary* expected_value = @{
96 @"src" : @"file:///bogus", 90 @"src" : @"file:///bogus",
97 @"referrerPolicy" : @"default", 91 @"referrerPolicy" : @"default",
98 }; 92 };
99 ImageTesterHelper(html, expected_value); 93 ImageTesterHelper(html, expected_value);
100 } 94 }
101 95
102 // Tests that __gCrWeb.getElementFromPoint function returns correct title. 96 // Tests that __gCrWeb.getElementFromPoint function returns correct title.
103 TEST_F(CoreJsTest, GetImageTitleAtPoint) { 97 TEST_F(ContextMenuJsTest, GetImageTitleAtPoint) {
104 NSString* html = @"<img id='foo' title='Hello world!'" 98 NSString* html = @"<img id='foo' title='Hello world!'"
105 "style='width:200;height:200;' src='file:///bogus'/>"; 99 "style='width:200;height:200;' src='file:///bogus'/>";
106 NSDictionary* expected_value = @{ 100 NSDictionary* expected_value = @{
107 @"src" : @"file:///bogus", 101 @"src" : @"file:///bogus",
108 @"referrerPolicy" : @"default", 102 @"referrerPolicy" : @"default",
109 @"title" : @"Hello world!", 103 @"title" : @"Hello world!",
110 }; 104 };
111 ImageTesterHelper(html, expected_value); 105 ImageTesterHelper(html, expected_value);
112 } 106 }
113 107
114 // Tests that __gCrWeb.getElementFromPoint function returns correct href. 108 // Tests that __gCrWeb.getElementFromPoint function returns correct href.
115 TEST_F(CoreJsTest, GetLinkImageUrlAtPoint) { 109 TEST_F(ContextMenuJsTest, GetLinkImageUrlAtPoint) {
116 NSString* html = 110 NSString* html =
117 @"<a href='file:///linky'>" 111 @"<a href='file:///linky'>"
118 "<img id='foo' style='width:200;height:200;' src='file:///bogus'/>" 112 "<img id='foo' style='width:200;height:200;' src='file:///bogus'/>"
119 "</a>"; 113 "</a>";
120 NSDictionary* expected_value = @{ 114 NSDictionary* expected_value = @{
121 @"src" : @"file:///bogus", 115 @"src" : @"file:///bogus",
122 @"referrerPolicy" : @"default", 116 @"referrerPolicy" : @"default",
123 @"href" : @"file:///linky", 117 @"href" : @"file:///linky",
124 }; 118 };
125 ImageTesterHelper(html, expected_value); 119 ImageTesterHelper(html, expected_value);
126 } 120 }
127 121
128 TEST_F(CoreJsTest, TextAreaStopsProximity) { 122 TEST_F(ContextMenuJsTest, TextAreaStopsProximity) {
129 NSString* html = 123 NSString* html =
130 @"<html><body style='margin-left:10px;margin-top:10px;'>" 124 @"<html><body style='margin-left:10px;margin-top:10px;'>"
131 "<div style='width:100px;height:100px;'>" 125 "<div style='width:100px;height:100px;'>"
132 "<img id='foo'" 126 "<img id='foo'"
133 " style='position:absolute;left:0px;top:0px;width:50px;height:50px'" 127 " style='position:absolute;left:0px;top:0px;width:50px;height:50px'"
134 " src='file:///bogus' />" 128 " src='file:///bogus' />"
135 "<input type='text' name='name'" 129 "<input type='text' name='name'"
136 " style='position:absolute;left:5px;top:5px; " 130 " style='position:absolute;left:5px;top:5px; "
137 "width:40px;height:40px'/>" 131 "width:40px;height:40px'/>"
138 "</div></body> </html>"; 132 "</div></body> </html>";
(...skipping 10 matching lines...) Expand all
149 143
150 for (size_t i = 0; i < arraysize(test_data); i++) { 144 for (size_t i = 0; i < arraysize(test_data); i++) {
151 const TestCoordinatesAndExpectedValue& data = test_data[i]; 145 const TestCoordinatesAndExpectedValue& data = test_data[i];
152 LoadHtml(html); 146 LoadHtml(html);
153 id result = ExecuteGetElementFromPointJavaScript(data.x, data.y); 147 id result = ExecuteGetElementFromPointJavaScript(data.x, data.y);
154 EXPECT_NSEQ(data.expected_value, result) 148 EXPECT_NSEQ(data.expected_value, result)
155 << " in test " << i << ": (" << data.x << ", " << data.y << ")"; 149 << " in test " << i << ": (" << data.x << ", " << data.y << ")";
156 } 150 }
157 } 151 }
158 152
159 struct TestDataForPasswordFormDetection {
160 NSString* pageContent;
161 NSNumber* containsPassword;
162 };
163
164 TEST_F(CoreJsTest, HasPasswordField) {
165 TestDataForPasswordFormDetection testData[] = {
166 // Form without a password field.
167 {@"<form><input type='text' name='password'></form>", @NO},
168 // Form with a password field.
169 {@"<form><input type='password' name='password'></form>", @YES}};
170 for (size_t i = 0; i < arraysize(testData); i++) {
171 TestDataForPasswordFormDetection& data = testData[i];
172 LoadHtml(data.pageContent);
173 id result = ExecuteJavaScript(@"__gCrWeb.hasPasswordField()");
174 EXPECT_NSEQ(data.containsPassword, result)
175 << " in test " << i << ": "
176 << base::SysNSStringToUTF8(data.pageContent);
177 }
178 }
179
180 TEST_F(CoreJsTest, HasPasswordFieldinFrame) {
181 TestDataForPasswordFormDetection data = {
182 // Form with a password field in a nested iframe.
183 @"<iframe name='pf'></iframe>"
184 "<script>"
185 " var doc = frames['pf'].document.open();"
186 " doc.write('<form><input type=\\'password\\'></form>');"
187 " doc.close();"
188 "</script>",
189 @YES
190 };
191 LoadHtml(data.pageContent);
192 id result = ExecuteJavaScript(@"__gCrWeb.hasPasswordField()");
193 EXPECT_NSEQ(data.containsPassword, result)
194 << base::SysNSStringToUTF8(data.pageContent);
195 }
196
197 TEST_F(CoreJsTest, Stringify) {
198 // TODO(jeanfrancoisg): Test whether __gCrWeb.stringify(undefined) correctly
199 //returns undefined.
200 TestScriptAndExpectedValue testData[] = {
201 // Stringify a string that contains various characters that must
202 // be escaped.
203 {
204 @"__gCrWeb.stringify('a\\u000a\\t\\b\\\\\\\"Z')",
205 @"\"a\\n\\t\\b\\\\\\\"Z\""
206 },
207 // Stringify a number.
208 {
209 @"__gCrWeb.stringify(77.7)",
210 @"77.7"
211 },
212 // Stringify an array.
213 {
214 @"__gCrWeb.stringify(['a','b'])",
215 @"[\"a\",\"b\"]"
216 },
217 // Stringify an object.
218 {
219 @"__gCrWeb.stringify({'a':'b','c':'d'})",
220 @"{\"a\":\"b\",\"c\":\"d\"}"
221 },
222 // Stringify a hierarchy of objects and arrays.
223 {
224 @"__gCrWeb.stringify([{'a':['b','c'],'d':'e'},'f'])",
225 @"[{\"a\":[\"b\",\"c\"],\"d\":\"e\"},\"f\"]"
226 },
227 // Stringify null.
228 {
229 @"__gCrWeb.stringify(null)",
230 @"null"
231 },
232 // Stringify an object with a toJSON function.
233 {
234 @"temp = [1,2];"
235 "temp.toJSON = function (key) {return undefined};"
236 "__gCrWeb.stringify(temp)",
237 @"[1,2]"
238 },
239 // Stringify an object with a toJSON property that is not a function.
240 {
241 @"temp = [1,2];"
242 "temp.toJSON = 42;"
243 "__gCrWeb.stringify(temp)",
244 @"[1,2]"
245 },
246 };
247
248 for (size_t i = 0; i < arraysize(testData); i++) {
249 TestScriptAndExpectedValue& data = testData[i];
250 // Load a sample HTML page. As a side-effect, loading HTML via
251 // |webController_| will also inject core.js.
252 LoadHtml(@"<p>");
253 id result = ExecuteJavaScript(data.test_script);
254 EXPECT_NSEQ(data.expected_value, result)
255 << " in test " << i << ": "
256 << base::SysNSStringToUTF8(data.test_script);
257 }
258 }
259
260 // Tests the javascript of the url of the an image present in the DOM. 153 // Tests the javascript of the url of the an image present in the DOM.
261 TEST_F(CoreJsTest, LinkOfImage) { 154 TEST_F(ContextMenuJsTest, LinkOfImage) {
262 // A page with a large image surrounded by a link. 155 // A page with a large image surrounded by a link.
263 static const char image[] = 156 static const char image[] =
264 "<a href='%s'><img width=400 height=400 src='foo'></img></a>"; 157 "<a href='%s'><img width=400 height=400 src='foo'></img></a>";
265 158
266 // A page with a link to a destination URL. 159 // A page with a link to a destination URL.
267 LoadHtml(base::StringPrintf(image, "http://destination")); 160 LoadHtml(base::StringPrintf(image, "http://destination"));
268 id result = ExecuteGetElementFromPointJavaScript(20, 20); 161 id result = ExecuteGetElementFromPointJavaScript(20, 20);
269 NSDictionary* expected_result = @{ 162 NSDictionary* expected_result = @{
270 @"src" : [NSString stringWithFormat:@"%sfoo", BaseUrl().c_str()], 163 @"src" : [NSString stringWithFormat:@"%sfoo", BaseUrl().c_str()],
271 @"referrerPolicy" : @"default", 164 @"referrerPolicy" : @"default",
(...skipping 25 matching lines...) Expand all
297 expected_result = @{ 190 expected_result = @{
298 @"src" : [NSString stringWithFormat:@"%sfoo", BaseUrl().c_str()], 191 @"src" : [NSString stringWithFormat:@"%sfoo", BaseUrl().c_str()],
299 @"referrerPolicy" : @"default", 192 @"referrerPolicy" : @"default",
300 }; 193 };
301 // Make sure the returned JSON does not have an 'href' key. 194 // Make sure the returned JSON does not have an 'href' key.
302 EXPECT_NSEQ(expected_result, result); 195 EXPECT_NSEQ(expected_result, result);
303 } 196 }
304 } 197 }
305 198
306 } // namespace web 199 } // namespace web
OLDNEW
« no previous file with comments | « ios/web/BUILD.gn ('k') | ios/web/web_state/js/core_js_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698