| 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 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
| 10 #import "ios/web/public/test/web_test_with_web_state.h" | 10 #import "ios/web/public/test/web_test_with_web_state.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "testing/gtest_mac.h" | 12 #include "testing/gtest_mac.h" |
| 13 | 13 |
| 14 // Unit tests for ios/web/web_state/js/resources/core.js. | 14 // Unit tests for ios/web/web_state/js/resources/core.js. |
| 15 | 15 |
| 16 namespace { | |
| 17 | |
| 18 struct TestScriptAndExpectedValue { | |
| 19 NSString* test_script; | |
| 20 id expected_value; | |
| 21 }; | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 namespace web { | 16 namespace web { |
| 26 | 17 |
| 27 // Test fixture to test core.js. | 18 // Test fixture to test core.js. |
| 28 typedef web::WebTestWithWebState CoreJsTest; | 19 typedef web::WebTestWithWebState CoreJsTest; |
| 29 | 20 |
| 30 struct TestDataForPasswordFormDetection { | 21 struct TestDataForPasswordFormDetection { |
| 31 NSString* pageContent; | 22 NSString* pageContent; |
| 32 NSNumber* containsPassword; | 23 NSNumber* containsPassword; |
| 33 }; | 24 }; |
| 34 | 25 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 58 " doc.close();" | 49 " doc.close();" |
| 59 "</script>", | 50 "</script>", |
| 60 @YES | 51 @YES |
| 61 }; | 52 }; |
| 62 LoadHtml(data.pageContent); | 53 LoadHtml(data.pageContent); |
| 63 id result = ExecuteJavaScript(@"__gCrWeb.hasPasswordField()"); | 54 id result = ExecuteJavaScript(@"__gCrWeb.hasPasswordField()"); |
| 64 EXPECT_NSEQ(data.containsPassword, result) | 55 EXPECT_NSEQ(data.containsPassword, result) |
| 65 << base::SysNSStringToUTF8(data.pageContent); | 56 << base::SysNSStringToUTF8(data.pageContent); |
| 66 } | 57 } |
| 67 | 58 |
| 68 TEST_F(CoreJsTest, Stringify) { | |
| 69 // TODO(jeanfrancoisg): Test whether __gCrWeb.stringify(undefined) correctly | |
| 70 //returns undefined. | |
| 71 TestScriptAndExpectedValue testData[] = { | |
| 72 // Stringify a string that contains various characters that must | |
| 73 // be escaped. | |
| 74 { | |
| 75 @"__gCrWeb.stringify('a\\u000a\\t\\b\\\\\\\"Z')", | |
| 76 @"\"a\\n\\t\\b\\\\\\\"Z\"" | |
| 77 }, | |
| 78 // Stringify a number. | |
| 79 { | |
| 80 @"__gCrWeb.stringify(77.7)", | |
| 81 @"77.7" | |
| 82 }, | |
| 83 // Stringify an array. | |
| 84 { | |
| 85 @"__gCrWeb.stringify(['a','b'])", | |
| 86 @"[\"a\",\"b\"]" | |
| 87 }, | |
| 88 // Stringify an object. | |
| 89 { | |
| 90 @"__gCrWeb.stringify({'a':'b','c':'d'})", | |
| 91 @"{\"a\":\"b\",\"c\":\"d\"}" | |
| 92 }, | |
| 93 // Stringify a hierarchy of objects and arrays. | |
| 94 { | |
| 95 @"__gCrWeb.stringify([{'a':['b','c'],'d':'e'},'f'])", | |
| 96 @"[{\"a\":[\"b\",\"c\"],\"d\":\"e\"},\"f\"]" | |
| 97 }, | |
| 98 // Stringify null. | |
| 99 { | |
| 100 @"__gCrWeb.stringify(null)", | |
| 101 @"null" | |
| 102 }, | |
| 103 // Stringify an object with a toJSON function. | |
| 104 { | |
| 105 @"temp = [1,2];" | |
| 106 "temp.toJSON = function (key) {return undefined};" | |
| 107 "__gCrWeb.stringify(temp)", | |
| 108 @"[1,2]" | |
| 109 }, | |
| 110 // Stringify an object with a toJSON property that is not a function. | |
| 111 { | |
| 112 @"temp = [1,2];" | |
| 113 "temp.toJSON = 42;" | |
| 114 "__gCrWeb.stringify(temp)", | |
| 115 @"[1,2]" | |
| 116 }, | |
| 117 }; | |
| 118 | |
| 119 for (size_t i = 0; i < arraysize(testData); i++) { | |
| 120 TestScriptAndExpectedValue& data = testData[i]; | |
| 121 // Load a sample HTML page. As a side-effect, loading HTML via | |
| 122 // |webController_| will also inject core.js. | |
| 123 LoadHtml(@"<p>"); | |
| 124 id result = ExecuteJavaScript(data.test_script); | |
| 125 EXPECT_NSEQ(data.expected_value, result) | |
| 126 << " in test " << i << ": " | |
| 127 << base::SysNSStringToUTF8(data.test_script); | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 } // namespace web | 59 } // namespace web |
| OLD | NEW |