| 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 <stddef.h> | |
| 6 #import <UIKit/UIKit.h> | |
| 7 | |
| 8 #include <vector> | 5 #include <vector> |
| 6 #import <Foundation/Foundation.h> |
| 9 | 7 |
| 10 #include "base/macros.h" | 8 #include "base/macros.h" |
| 11 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 12 #include "ios/web/public/test/web_test_util.h" | |
| 13 #import "ios/web/test/web_test.h" | 10 #import "ios/web/test/web_test.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "testing/gtest_mac.h" | 12 #include "testing/gtest_mac.h" |
| 16 | 13 |
| 17 // Unit tests for ios/web/web_state/js/resources/core.js. | 14 // Unit tests for ios/web/web_state/js/resources/core.js. |
| 18 | 15 |
| 19 namespace { | 16 namespace { |
| 20 | 17 |
| 21 struct TestScriptAndExpectedValue { | 18 struct TestScriptAndExpectedValue { |
| 22 NSString* testScript; | 19 NSString* testScript; |
| 23 NSString* expectedValue; | 20 NSString* expectedValue; |
| 24 }; | 21 }; |
| 25 | 22 |
| 26 // A mixin class for testing with CRWWKWebViewWebController or | 23 } // namespace |
| 27 // CRWUIWebViewWebController. | 24 |
| 28 template <typename WebTestT> | 25 namespace web { |
| 29 class CoreJsTest : public WebTestT { | 26 |
| 27 // Test fixture to test core.js. |
| 28 class CoreJsTest : public web::WebTestWithWKWebViewWebController { |
| 30 protected: | 29 protected: |
| 31 void ImageTesterHelper( | 30 void ImageTesterHelper( |
| 32 NSString* htmlForImage, | 31 NSString* htmlForImage, |
| 33 NSString* expectedValueWhenClickingOnImage) { | 32 NSString* expectedValueWhenClickingOnImage) { |
| 34 NSString* pageContentTemplate = | 33 NSString* pageContentTemplate = |
| 35 @"<html><body style='margin-left:10px;margin-top:10px;'>" | 34 @"<html><body style='margin-left:10px;margin-top:10px;'>" |
| 36 "<div style='width:100px;height:100px;'>" | 35 "<div style='width:100px;height:100px;'>" |
| 37 " <p style='position:absolute;left:25px;top:25px;" | 36 " <p style='position:absolute;left:25px;top:25px;" |
| 38 " width:50px;height:50px'>" | 37 " width:50px;height:50px'>" |
| 39 "%@" | 38 "%@" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 54 expectedValueWhenClickingOnImage | 53 expectedValueWhenClickingOnImage |
| 55 }, | 54 }, |
| 56 // Point inside the <p> element. | 55 // Point inside the <p> element. |
| 57 { | 56 { |
| 58 @"__gCrWeb.getElementFromPoint(300, 300)", | 57 @"__gCrWeb.getElementFromPoint(300, 300)", |
| 59 @"{}" | 58 @"{}" |
| 60 }, | 59 }, |
| 61 }; | 60 }; |
| 62 for (size_t i = 0; i < arraysize(testData); i++) { | 61 for (size_t i = 0; i < arraysize(testData); i++) { |
| 63 TestScriptAndExpectedValue& data = testData[i]; | 62 TestScriptAndExpectedValue& data = testData[i]; |
| 64 WebTestT::LoadHtml(pageContent); | 63 LoadHtml(pageContent); |
| 65 NSString* result = WebTestT::RunJavaScript(data.testScript); | 64 NSString* result = RunJavaScript(data.testScript); |
| 66 EXPECT_NSEQ(data.expectedValue, result) << " in test " << i << ": " << | 65 EXPECT_NSEQ(data.expectedValue, result) << " in test " << i << ": " << |
| 67 [data.testScript UTF8String]; | 66 [data.testScript UTF8String]; |
| 68 } | 67 } |
| 69 } | 68 } |
| 70 }; | 69 }; |
| 71 | 70 |
| 72 // Concrete test fixture to test core.js using UIWebView-based web controller. | 71 TEST_F(CoreJsTest, GetImageUrlAtPoint) { |
| 73 typedef CoreJsTest<web::WebTestWithUIWebViewWebController> CoreJSUIWebViewTest; | |
| 74 | |
| 75 // Concrete test fixture to test core.js using WKWebView-based web controller. | |
| 76 typedef CoreJsTest<web::WebTestWithWKWebViewWebController> CoreJSWKWebViewTest; | |
| 77 | |
| 78 WEB_TEST_F(CoreJSUIWebViewTest, CoreJSWKWebViewTest, GetImageUrlAtPoint) { | |
| 79 NSString* htmlForImage = | 72 NSString* htmlForImage = |
| 80 @"<img id='foo' style='width:200;height:200;' src='file:///bogus'/>"; | 73 @"<img id='foo' style='width:200;height:200;' src='file:///bogus'/>"; |
| 81 NSString* expectedValueWhenClickingOnImage = | 74 NSString* expectedValueWhenClickingOnImage = |
| 82 @"{\"src\":\"file:///bogus\",\"referrerPolicy\":\"default\"}"; | 75 @"{\"src\":\"file:///bogus\",\"referrerPolicy\":\"default\"}"; |
| 83 this->ImageTesterHelper(htmlForImage, expectedValueWhenClickingOnImage); | 76 ImageTesterHelper(htmlForImage, expectedValueWhenClickingOnImage); |
| 84 } | 77 } |
| 85 | 78 |
| 86 WEB_TEST_F(CoreJSUIWebViewTest, CoreJSWKWebViewTest, GetImageTitleAtPoint) { | 79 TEST_F(CoreJsTest, GetImageTitleAtPoint) { |
| 87 NSString* htmlForImage = | 80 NSString* htmlForImage = |
| 88 @"<img id='foo' title='Hello world!'" | 81 @"<img id='foo' title='Hello world!'" |
| 89 "style='width:200;height:200;' src='file:///bogus'/>"; | 82 "style='width:200;height:200;' src='file:///bogus'/>"; |
| 90 NSString* expectedValueWhenClickingOnImage = | 83 NSString* expectedValueWhenClickingOnImage = |
| 91 @"{\"src\":\"file:///bogus\",\"referrerPolicy\":\"default\"," | 84 @"{\"src\":\"file:///bogus\",\"referrerPolicy\":\"default\"," |
| 92 "\"title\":\"Hello world!\"}"; | 85 "\"title\":\"Hello world!\"}"; |
| 93 this->ImageTesterHelper(htmlForImage, expectedValueWhenClickingOnImage); | 86 ImageTesterHelper(htmlForImage, expectedValueWhenClickingOnImage); |
| 94 } | 87 } |
| 95 | 88 |
| 96 WEB_TEST_F(CoreJSUIWebViewTest, CoreJSWKWebViewTest, GetLinkImageUrlAtPoint) { | 89 TEST_F(CoreJsTest, GetLinkImageUrlAtPoint) { |
| 97 NSString* htmlForImage = | 90 NSString* htmlForImage = |
| 98 @"<a href='file:///linky'>" | 91 @"<a href='file:///linky'>" |
| 99 "<img id='foo' style='width:200;height:200;' src='file:///bogus'/>" | 92 "<img id='foo' style='width:200;height:200;' src='file:///bogus'/>" |
| 100 "</a>"; | 93 "</a>"; |
| 101 NSString* expectedValueWhenClickingOnImage = | 94 NSString* expectedValueWhenClickingOnImage = |
| 102 @"{\"src\":\"file:///bogus\",\"referrerPolicy\":\"default\"," | 95 @"{\"src\":\"file:///bogus\",\"referrerPolicy\":\"default\"," |
| 103 "\"href\":\"file:///linky\"}"; | 96 "\"href\":\"file:///linky\"}"; |
| 104 this->ImageTesterHelper(htmlForImage, expectedValueWhenClickingOnImage); | 97 ImageTesterHelper(htmlForImage, expectedValueWhenClickingOnImage); |
| 105 } | 98 } |
| 106 | 99 |
| 107 WEB_TEST_F(CoreJSUIWebViewTest, CoreJSWKWebViewTest, TextAreaStopsProximity) { | 100 TEST_F(CoreJsTest, TextAreaStopsProximity) { |
| 108 NSString* pageContent = | 101 NSString* pageContent = |
| 109 @"<html><body style='margin-left:10px;margin-top:10px;'>" | 102 @"<html><body style='margin-left:10px;margin-top:10px;'>" |
| 110 "<div style='width:100px;height:100px;'>" | 103 "<div style='width:100px;height:100px;'>" |
| 111 "<img id='foo'" | 104 "<img id='foo'" |
| 112 " style='position:absolute;left:0px;top:0px;width:50px;height:50px'" | 105 " style='position:absolute;left:0px;top:0px;width:50px;height:50px'" |
| 113 " src='file:///bogus' />" | 106 " src='file:///bogus' />" |
| 114 "<input type='text' name='name'" | 107 "<input type='text' name='name'" |
| 115 " style='position:absolute;left:5px;top:5px; width:40px;height:40px'/>" | 108 " style='position:absolute;left:5px;top:5px; width:40px;height:40px'/>" |
| 116 "</div></body> </html>"; | 109 "</div></body> </html>"; |
| 117 | 110 |
| 118 NSString* success = @"{\"src\":\"file:///bogus\"," | 111 NSString* success = @"{\"src\":\"file:///bogus\"," |
| 119 "\"referrerPolicy\":\"default\"}"; | 112 "\"referrerPolicy\":\"default\"}"; |
| 120 NSString* failure = @"{}"; | 113 NSString* failure = @"{}"; |
| 121 | 114 |
| 122 TestScriptAndExpectedValue testData[] = { | 115 TestScriptAndExpectedValue testData[] = { |
| 123 { | 116 { |
| 124 @"__gCrWeb.getElementFromPoint(2, 20)", | 117 @"__gCrWeb.getElementFromPoint(2, 20)", |
| 125 success | 118 success |
| 126 }, | 119 }, |
| 127 { | 120 { |
| 128 @"__gCrWeb.getElementFromPoint(5, 20)", | 121 @"__gCrWeb.getElementFromPoint(5, 20)", |
| 129 failure | 122 failure |
| 130 }, | 123 }, |
| 131 }; | 124 }; |
| 132 | 125 |
| 133 for (size_t i = 0; i < arraysize(testData); i++) { | 126 for (size_t i = 0; i < arraysize(testData); i++) { |
| 134 TestScriptAndExpectedValue& data = testData[i]; | 127 TestScriptAndExpectedValue& data = testData[i]; |
| 135 this->LoadHtml(pageContent); | 128 LoadHtml(pageContent); |
| 136 NSString* result = this->RunJavaScript(data.testScript); | 129 NSString* result = RunJavaScript(data.testScript); |
| 137 EXPECT_NSEQ(data.expectedValue, result) << " in test " << i << ": " << | 130 EXPECT_NSEQ(data.expectedValue, result) << " in test " << i << ": " << |
| 138 [data.testScript UTF8String]; | 131 [data.testScript UTF8String]; |
| 139 } | 132 } |
| 140 }; | 133 }; |
| 141 | 134 |
| 142 struct TestDataForPasswordFormDetection { | 135 struct TestDataForPasswordFormDetection { |
| 143 NSString* pageContent; | 136 NSString* pageContent; |
| 144 NSString* containsPassword; | 137 NSString* containsPassword; |
| 145 }; | 138 }; |
| 146 | 139 |
| 147 WEB_TEST_F(CoreJSUIWebViewTest, CoreJSWKWebViewTest, HasPasswordField) { | 140 TEST_F(CoreJsTest, HasPasswordField) { |
| 148 TestDataForPasswordFormDetection testData[] = { | 141 TestDataForPasswordFormDetection testData[] = { |
| 149 // Form without a password field. | 142 // Form without a password field. |
| 150 { | 143 { |
| 151 @"<form><input type='text' name='password'></form>", | 144 @"<form><input type='text' name='password'></form>", |
| 152 @"false" | 145 @"false" |
| 153 }, | 146 }, |
| 154 // Form with a password field. | 147 // Form with a password field. |
| 155 { | 148 { |
| 156 @"<form><input type='password' name='password'></form>", | 149 @"<form><input type='password' name='password'></form>", |
| 157 @"true" | 150 @"true" |
| 158 } | 151 } |
| 159 }; | 152 }; |
| 160 for (size_t i = 0; i < arraysize(testData); i++) { | 153 for (size_t i = 0; i < arraysize(testData); i++) { |
| 161 TestDataForPasswordFormDetection& data = testData[i]; | 154 TestDataForPasswordFormDetection& data = testData[i]; |
| 162 this->LoadHtml(data.pageContent); | 155 LoadHtml(data.pageContent); |
| 163 NSString* result = this->RunJavaScript(@"__gCrWeb.hasPasswordField()"); | 156 NSString* result = RunJavaScript(@"__gCrWeb.hasPasswordField()"); |
| 164 EXPECT_NSEQ(data.containsPassword, result) << | 157 EXPECT_NSEQ(data.containsPassword, result) << |
| 165 " in test " << i << ": " << [data.pageContent UTF8String]; | 158 " in test " << i << ": " << [data.pageContent UTF8String]; |
| 166 } | 159 } |
| 167 } | 160 } |
| 168 | 161 |
| 169 WEB_TEST_F(CoreJSUIWebViewTest, CoreJSWKWebViewTest, HasPasswordFieldinFrame) { | 162 TEST_F(CoreJsTest, HasPasswordFieldinFrame) { |
| 170 TestDataForPasswordFormDetection data = { | 163 TestDataForPasswordFormDetection data = { |
| 171 // Form with a password field in a nested iframe. | 164 // Form with a password field in a nested iframe. |
| 172 @"<iframe name='pf'></iframe>" | 165 @"<iframe name='pf'></iframe>" |
| 173 "<script>" | 166 "<script>" |
| 174 " var doc = frames['pf'].document.open();" | 167 " var doc = frames['pf'].document.open();" |
| 175 " doc.write('<form><input type=\\'password\\'></form>');" | 168 " doc.write('<form><input type=\\'password\\'></form>');" |
| 176 " doc.close();" | 169 " doc.close();" |
| 177 "</script>", | 170 "</script>", |
| 178 @"true" | 171 @"true" |
| 179 }; | 172 }; |
| 180 this->LoadHtml(data.pageContent); | 173 LoadHtml(data.pageContent); |
| 181 NSString* result = this->RunJavaScript(@"__gCrWeb.hasPasswordField()"); | 174 NSString* result = RunJavaScript(@"__gCrWeb.hasPasswordField()"); |
| 182 EXPECT_NSEQ(data.containsPassword, result) << [data.pageContent UTF8String]; | 175 EXPECT_NSEQ(data.containsPassword, result) << [data.pageContent UTF8String]; |
| 183 } | 176 } |
| 184 | 177 |
| 185 WEB_TEST_F(CoreJSUIWebViewTest, CoreJSWKWebViewTest, Stringify) { | 178 TEST_F(CoreJsTest, Stringify) { |
| 186 // TODO(jeanfrancoisg): Test whether __gCrWeb.stringify(undefined) correctly | 179 // TODO(jeanfrancoisg): Test whether __gCrWeb.stringify(undefined) correctly |
| 187 //returns undefined. | 180 //returns undefined. |
| 188 TestScriptAndExpectedValue testData[] = { | 181 TestScriptAndExpectedValue testData[] = { |
| 189 // Stringify a string that contains various characters that must | 182 // Stringify a string that contains various characters that must |
| 190 // be escaped. | 183 // be escaped. |
| 191 { | 184 { |
| 192 @"__gCrWeb.stringify('a\\u000a\\t\\b\\\\\\\"Z')", | 185 @"__gCrWeb.stringify('a\\u000a\\t\\b\\\\\\\"Z')", |
| 193 @"\"a\\n\\t\\b\\\\\\\"Z\"" | 186 @"\"a\\n\\t\\b\\\\\\\"Z\"" |
| 194 }, | 187 }, |
| 195 // Stringify a number. | 188 // Stringify a number. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 "temp.toJSON = 42;" | 223 "temp.toJSON = 42;" |
| 231 "__gCrWeb.stringify(temp)", | 224 "__gCrWeb.stringify(temp)", |
| 232 @"[1,2]" | 225 @"[1,2]" |
| 233 }, | 226 }, |
| 234 }; | 227 }; |
| 235 | 228 |
| 236 for (size_t i = 0; i < arraysize(testData); i++) { | 229 for (size_t i = 0; i < arraysize(testData); i++) { |
| 237 TestScriptAndExpectedValue& data = testData[i]; | 230 TestScriptAndExpectedValue& data = testData[i]; |
| 238 // Load a sample HTML page. As a side-effect, loading HTML via | 231 // Load a sample HTML page. As a side-effect, loading HTML via |
| 239 // |webController_| will also inject core.js. | 232 // |webController_| will also inject core.js. |
| 240 this->LoadHtml(@"<p>"); | 233 LoadHtml(@"<p>"); |
| 241 NSString* result = this->RunJavaScript(data.testScript); | 234 NSString* result = RunJavaScript(data.testScript); |
| 242 EXPECT_NSEQ(data.expectedValue, result) << " in test " << i << ": " << | 235 EXPECT_NSEQ(data.expectedValue, result) << " in test " << i << ": " << |
| 243 [data.testScript UTF8String]; | 236 [data.testScript UTF8String]; |
| 244 } | 237 } |
| 245 } | 238 } |
| 246 | 239 |
| 247 // Tests the javascript of the url of the an image present in the DOM. | 240 // Tests the javascript of the url of the an image present in the DOM. |
| 248 WEB_TEST_F(CoreJSUIWebViewTest, CoreJSWKWebViewTest, LinkOfImage) { | 241 TEST_F(CoreJsTest, LinkOfImage) { |
| 249 // A page with a large image surrounded by a link. | 242 // A page with a large image surrounded by a link. |
| 250 static const char image[] = | 243 static const char image[] = |
| 251 "<a href='%s'><img width=400 height=400 src='foo'></img></a>"; | 244 "<a href='%s'><img width=400 height=400 src='foo'></img></a>"; |
| 252 | 245 |
| 253 // A page with a link to a destination URL. | 246 // A page with a link to a destination URL. |
| 254 this->LoadHtml(base::StringPrintf(image, "http://destination")); | 247 LoadHtml(base::StringPrintf(image, "http://destination")); |
| 255 NSString* result = | 248 NSString* result = |
| 256 this->RunJavaScript(@"__gCrWeb['getElementFromPoint'](200, 200)"); | 249 RunJavaScript(@"__gCrWeb['getElementFromPoint'](200, 200)"); |
| 257 std::string expected_result = | 250 std::string expected_result = |
| 258 R"({"src":"foo","referrerPolicy":"default",)" | 251 R"({"src":"foo","referrerPolicy":"default",)" |
| 259 R"("href":"http://destination/"})"; | 252 R"("href":"http://destination/"})"; |
| 260 EXPECT_EQ(expected_result, [result UTF8String]); | 253 EXPECT_EQ(expected_result, [result UTF8String]); |
| 261 | 254 |
| 262 // A page with a link with some JavaScript that does not result in a NOP. | 255 // A page with a link with some JavaScript that does not result in a NOP. |
| 263 this->LoadHtml(base::StringPrintf(image, | 256 LoadHtml(base::StringPrintf(image, "javascript:console.log('whatever')")); |
| 264 "javascript:console.log('whatever')")); | 257 result = RunJavaScript(@"__gCrWeb['getElementFromPoint'](200, 200)"); |
| 265 result = this->RunJavaScript(@"__gCrWeb['getElementFromPoint'](200, 200)"); | |
| 266 expected_result = | 258 expected_result = |
| 267 R"({"src":"foo","referrerPolicy":"default",)" | 259 R"({"src":"foo","referrerPolicy":"default",)" |
| 268 R"("href":"javascript:console.log("})"; | 260 R"("href":"javascript:console.log("})"; |
| 269 EXPECT_EQ(expected_result, [result UTF8String]); | 261 EXPECT_EQ(expected_result, [result UTF8String]); |
| 270 | 262 |
| 271 // A list of JavaScripts that result in a NOP. | 263 // A list of JavaScripts that result in a NOP. |
| 272 std::vector<std::string> nop_javascripts; | 264 std::vector<std::string> nop_javascripts; |
| 273 nop_javascripts.push_back(";"); | 265 nop_javascripts.push_back(";"); |
| 274 nop_javascripts.push_back("void(0);"); | 266 nop_javascripts.push_back("void(0);"); |
| 275 nop_javascripts.push_back("void(0); void(0); void(0)"); | 267 nop_javascripts.push_back("void(0); void(0); void(0)"); |
| 276 | 268 |
| 277 for (auto js : nop_javascripts) { | 269 for (auto js : nop_javascripts) { |
| 278 // A page with a link with some JavaScript that results in a NOP. | 270 // A page with a link with some JavaScript that results in a NOP. |
| 279 const std::string javascript = std::string("javascript:") + js; | 271 const std::string javascript = std::string("javascript:") + js; |
| 280 this->LoadHtml(base::StringPrintf(image, javascript.c_str())); | 272 LoadHtml(base::StringPrintf(image, javascript.c_str())); |
| 281 result = this->RunJavaScript(@"__gCrWeb['getElementFromPoint'](200, 200)"); | 273 result = RunJavaScript(@"__gCrWeb['getElementFromPoint'](200, 200)"); |
| 282 expected_result = R"({"src":"foo","referrerPolicy":"default"})"; | 274 expected_result = R"({"src":"foo","referrerPolicy":"default"})"; |
| 283 | 275 |
| 284 // Make sure the returned JSON does not have an 'href' key. | 276 // Make sure the returned JSON does not have an 'href' key. |
| 285 EXPECT_EQ(expected_result, [result UTF8String]); | 277 EXPECT_EQ(expected_result, [result UTF8String]); |
| 286 } | 278 } |
| 287 } | 279 } |
| 288 | 280 |
| 289 } // namespace | 281 } // namespace web |
| OLD | NEW |