| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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> | 5 #include <stddef.h> |
| 6 #import <Foundation/Foundation.h> | 6 #import <Foundation/Foundation.h> |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/strings/sys_string_conversions.h" |
| 9 #import "ios/web/public/test/web_test_with_web_state.h" | 10 #import "ios/web/public/test/web_test_with_web_state.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #import "testing/gtest_mac.h" | 12 #import "testing/gtest_mac.h" |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 // Struct for isTextField() test data. | 16 // Struct for isTextField() test data. |
| 16 struct TextFieldTestElement { | 17 struct TextFieldTestElement { |
| 17 // The element name. | 18 // The element name. |
| 18 const char* element_name; | 19 const char* element_name; |
| 19 // The index of this element in those that have the same name. | 20 // The index of this element in those that have the same name. |
| 20 const int element_index; | 21 const int element_index; |
| 21 // True if this is expected to be a text field. | 22 // True if this is expected to be a text field. |
| 22 const bool expected_is_text_field; | 23 const bool expected_is_text_field; |
| 23 }; | 24 }; |
| 24 | 25 |
| 26 // Struct for stringify() test data. |
| 27 struct TestScriptAndExpectedValue { |
| 28 NSString* test_script; |
| 29 id expected_value; |
| 30 }; |
| 31 |
| 25 } // namespace | 32 } // namespace |
| 26 | 33 |
| 27 namespace web { | 34 namespace web { |
| 28 | 35 |
| 29 // Test fixture to test common.js. | 36 // Test fixture to test common.js. |
| 30 typedef web::WebTestWithWebState CommonJsTest; | 37 typedef web::WebTestWithWebState CommonJsTest; |
| 31 | 38 |
| 32 // Tests __gCrWeb.common.isTextField JavaScript API. | 39 // Tests __gCrWeb.common.isTextField JavaScript API. |
| 33 TEST_F(CommonJsTest, IsTestField) { | 40 TEST_F(CommonJsTest, IsTestField) { |
| 34 LoadHtml(@"<html><body>" | 41 LoadHtml(@"<html><body>" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 id result = ExecuteJavaScript([NSString | 87 id result = ExecuteJavaScript([NSString |
| 81 stringWithFormat:@"__gCrWeb.common.isTextField(" | 88 stringWithFormat:@"__gCrWeb.common.isTextField(" |
| 82 "window.document.getElementsByName('%s')[%d])", | 89 "window.document.getElementsByName('%s')[%d])", |
| 83 element.element_name, element.element_index]); | 90 element.element_name, element.element_index]); |
| 84 EXPECT_NSEQ(element.expected_is_text_field ? @YES : @NO, result) | 91 EXPECT_NSEQ(element.expected_is_text_field ? @YES : @NO, result) |
| 85 << element.element_name << " with index " << element.element_index | 92 << element.element_name << " with index " << element.element_index |
| 86 << " isTextField(): " << element.expected_is_text_field; | 93 << " isTextField(): " << element.expected_is_text_field; |
| 87 } | 94 } |
| 88 } | 95 } |
| 89 | 96 |
| 97 // Tests __gCrWeb.stringify JavaScript API. |
| 98 TEST_F(CommonJsTest, Stringify) { |
| 99 TestScriptAndExpectedValue test_data[] = { |
| 100 // Stringify a string that contains various characters that must |
| 101 // be escaped. |
| 102 {@"__gCrWeb.stringify('a\\u000a\\t\\b\\\\\\\"Z')", |
| 103 @"\"a\\n\\t\\b\\\\\\\"Z\""}, |
| 104 // Stringify a number. |
| 105 {@"__gCrWeb.stringify(77.7)", @"77.7"}, |
| 106 // Stringify an array. |
| 107 {@"__gCrWeb.stringify(['a','b'])", @"[\"a\",\"b\"]"}, |
| 108 // Stringify an object. |
| 109 {@"__gCrWeb.stringify({'a':'b','c':'d'})", @"{\"a\":\"b\",\"c\":\"d\"}"}, |
| 110 // Stringify a hierarchy of objects and arrays. |
| 111 {@"__gCrWeb.stringify([{'a':['b','c'],'d':'e'},'f'])", |
| 112 @"[{\"a\":[\"b\",\"c\"],\"d\":\"e\"},\"f\"]"}, |
| 113 // Stringify null. |
| 114 {@"__gCrWeb.stringify(null)", @"null"}, |
| 115 // Stringify an object with a toJSON function. |
| 116 {@"temp = [1,2];" |
| 117 "temp.toJSON = function (key) {return undefined};" |
| 118 "__gCrWeb.stringify(temp)", |
| 119 @"[1,2]"}, |
| 120 // Stringify an object with a toJSON property that is not a function. |
| 121 {@"temp = [1,2];" |
| 122 "temp.toJSON = 42;" |
| 123 "__gCrWeb.stringify(temp)", |
| 124 @"[1,2]"}, |
| 125 // Stringify an undefined object. |
| 126 {@"__gCrWeb.stringify(undefined)", @"undefined"}, |
| 127 }; |
| 128 |
| 129 for (size_t i = 0; i < arraysize(test_data); i++) { |
| 130 TestScriptAndExpectedValue& data = test_data[i]; |
| 131 // Load a sample HTML page. As a side-effect, loading HTML via |
| 132 // |webController_| will also inject core.js. |
| 133 LoadHtml(@"<p>"); |
| 134 id result = ExecuteJavaScript(data.test_script); |
| 135 EXPECT_NSEQ(data.expected_value, result) |
| 136 << " in test " << i << ": " |
| 137 << base::SysNSStringToUTF8(data.test_script); |
| 138 } |
| 139 } |
| 140 |
| 90 } // namespace web | 141 } // namespace web |
| OLD | NEW |