Chromium Code Reviews| Index: ios/web/web_state/js/common_js_unittest.mm |
| diff --git a/ios/web/web_state/js/common_js_unittest.mm b/ios/web/web_state/js/common_js_unittest.mm |
| index 5e10f768660dd2f8406abb84c5246eb193603978..bcdecada816106e1e3f5b0955c7656e24154f77b 100644 |
| --- a/ios/web/web_state/js/common_js_unittest.mm |
| +++ b/ios/web/web_state/js/common_js_unittest.mm |
| @@ -6,13 +6,14 @@ |
| #import <Foundation/Foundation.h> |
| #include "base/macros.h" |
| +#import "base/strings/sys_string_conversions.h" |
|
Eugene But (OOO till 7-30)
2017/04/10 21:42:07
s/import/include (because this header has Objectiv
danyao
2017/04/10 22:12:59
Done.
|
| #import "ios/web/public/test/web_test_with_web_state.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| #import "testing/gtest_mac.h" |
| namespace { |
| -// Struct for isTextField() test data. |
| +/** Struct for isTextField() test data. */ |
|
Eugene But (OOO till 7-30)
2017/04/10 21:42:07
/** */ comments Style Is pretty uncommon in Chrome
danyao
2017/04/10 22:12:59
Ah right this is Objective C. I was carrying over
|
| struct TextFieldTestElement { |
| // The element name. |
| const char* element_name; |
| @@ -22,14 +23,20 @@ struct TextFieldTestElement { |
| const bool expected_is_text_field; |
| }; |
| +/** Struct for stringify() test data. */ |
| +struct TestScriptAndExpectedValue { |
| + NSString* test_script; |
| + id expected_value; |
| +}; |
| + |
| } // namespace |
| namespace web { |
| -// Test fixture to test common.js. |
| +/** Test fixture to test common.js. */ |
| typedef web::WebTestWithWebState CommonJsTest; |
| -// Tests __gCrWeb.common.isTextField JavaScript API. |
| +/** Tests __gCrWeb.common.isTextField JavaScript API. */ |
| TEST_F(CommonJsTest, IsTestField) { |
| LoadHtml(@"<html><body>" |
| "<input type='text' name='firstname'>" |
| @@ -87,4 +94,48 @@ TEST_F(CommonJsTest, IsTestField) { |
| } |
| } |
| +/** Tests __gCrWeb.stringify JavaScript API. */ |
| +TEST_F(CommonJsTest, Stringify) { |
| + TestScriptAndExpectedValue testData[] = { |
|
Eugene But (OOO till 7-30)
2017/04/10 21:42:07
s/testData/test_data to follow C++ Style Guide ins
danyao
2017/04/10 22:12:59
Done.
|
| + // Stringify a string that contains various characters that must |
| + // be escaped. |
| + {@"__gCrWeb.stringify('a\\u000a\\t\\b\\\\\\\"Z')", |
| + @"\"a\\n\\t\\b\\\\\\\"Z\""}, |
| + // Stringify a number. |
| + {@"__gCrWeb.stringify(77.7)", @"77.7"}, |
| + // Stringify an array. |
| + {@"__gCrWeb.stringify(['a','b'])", @"[\"a\",\"b\"]"}, |
| + // Stringify an object. |
| + {@"__gCrWeb.stringify({'a':'b','c':'d'})", @"{\"a\":\"b\",\"c\":\"d\"}"}, |
| + // Stringify a hierarchy of objects and arrays. |
| + {@"__gCrWeb.stringify([{'a':['b','c'],'d':'e'},'f'])", |
| + @"[{\"a\":[\"b\",\"c\"],\"d\":\"e\"},\"f\"]"}, |
| + // Stringify null. |
| + {@"__gCrWeb.stringify(null)", @"null"}, |
| + // Stringify an object with a toJSON function. |
| + {@"temp = [1,2];" |
| + "temp.toJSON = function (key) {return undefined};" |
| + "__gCrWeb.stringify(temp)", |
| + @"[1,2]"}, |
| + // Stringify an object with a toJSON property that is not a function. |
| + {@"temp = [1,2];" |
| + "temp.toJSON = 42;" |
| + "__gCrWeb.stringify(temp)", |
| + @"[1,2]"}, |
| + // Stringify an undefined object. |
| + {@"__gCrWeb.stringify(undefined)", @"undefined"}, |
| + }; |
| + |
| + for (size_t i = 0; i < arraysize(testData); i++) { |
| + TestScriptAndExpectedValue& data = testData[i]; |
| + // Load a sample HTML page. As a side-effect, loading HTML via |
| + // |webController_| will also inject core.js. |
| + LoadHtml(@"<p>"); |
| + id result = ExecuteJavaScript(data.test_script); |
| + EXPECT_NSEQ(data.expected_value, result) |
| + << " in test " << i << ": " |
| + << base::SysNSStringToUTF8(data.test_script); |
| + } |
| +} |
| + |
| } // namespace web |