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

Side by Side Diff: chrome/common/json_value_serializer_unittest.cc

Issue 31014: Port DictionaryValue to use string16 instead of wstring. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/ssl/ssl_policy.cc ('k') | chrome/common/pref_service.cc » ('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 (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/file_util.h" 6 #include "base/file_util.h"
7 #include "base/json_reader.h" 7 #include "base/json_reader.h"
8 #include "base/json_writer.h" 8 #include "base/json_writer.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
11 #include "base/values.h" 11 #include "base/values.h"
12 #include "chrome/common/chrome_paths.h" 12 #include "chrome/common/chrome_paths.h"
13 #include "chrome/common/json_value_serializer.h" 13 #include "chrome/common/json_value_serializer.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 15
16 TEST(JSONValueSerializerTest, Roundtrip) { 16 TEST(JSONValueSerializerTest, Roundtrip) {
17 const std::string original_serialization = 17 const std::string original_serialization =
18 "{\"bool\":true,\"int\":42,\"list\":[1,2],\"null\":null,\"real\":3.14}"; 18 "{\"bool\":true,\"int\":42,\"list\":[1,2],\"null\":null,\"real\":3.14}";
19 JSONStringValueSerializer serializer(original_serialization); 19 JSONStringValueSerializer serializer(original_serialization);
20 scoped_ptr<Value> root(serializer.Deserialize(NULL)); 20 scoped_ptr<Value> root(serializer.Deserialize(NULL));
21 ASSERT_TRUE(root.get()); 21 ASSERT_TRUE(root.get());
22 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 22 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
23 23
24 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); 24 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
25 25
26 Value* null_value = NULL; 26 Value* null_value = NULL;
27 ASSERT_TRUE(root_dict->Get(L"null", &null_value)); 27 ASSERT_TRUE(root_dict->Get(ASCIIToUTF16("null"), &null_value));
28 ASSERT_TRUE(null_value); 28 ASSERT_TRUE(null_value);
29 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); 29 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL));
30 30
31 bool bool_value = false; 31 bool bool_value = false;
32 ASSERT_TRUE(root_dict->GetBoolean(L"bool", &bool_value)); 32 ASSERT_TRUE(root_dict->GetBoolean(ASCIIToUTF16("bool"), &bool_value));
33 ASSERT_TRUE(bool_value); 33 ASSERT_TRUE(bool_value);
34 34
35 int int_value = 0; 35 int int_value = 0;
36 ASSERT_TRUE(root_dict->GetInteger(L"int", &int_value)); 36 ASSERT_TRUE(root_dict->GetInteger(ASCIIToUTF16("int"), &int_value));
37 ASSERT_EQ(42, int_value); 37 ASSERT_EQ(42, int_value);
38 38
39 double real_value = 0.0; 39 double real_value = 0.0;
40 ASSERT_TRUE(root_dict->GetReal(L"real", &real_value)); 40 ASSERT_TRUE(root_dict->GetReal(ASCIIToUTF16("real"), &real_value));
41 ASSERT_DOUBLE_EQ(3.14, real_value); 41 ASSERT_DOUBLE_EQ(3.14, real_value);
42 42
43 // We shouldn't be able to write using this serializer, since it was 43 // We shouldn't be able to write using this serializer, since it was
44 // initialized with a const string. 44 // initialized with a const string.
45 ASSERT_FALSE(serializer.Serialize(*root_dict)); 45 ASSERT_FALSE(serializer.Serialize(*root_dict));
46 46
47 std::string test_serialization = ""; 47 std::string test_serialization = "";
48 JSONStringValueSerializer mutable_serializer(&test_serialization); 48 JSONStringValueSerializer mutable_serializer(&test_serialization);
49 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict)); 49 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict));
50 ASSERT_EQ(original_serialization, test_serialization); 50 ASSERT_EQ(original_serialization, test_serialization);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 "\\xCC\\xCD\\xCE\\xCF\\xD0\\xD1\\xD2\\xD3\\xD4\\xD5\\xD6\\xD7\\xD8\\xD9" 85 "\\xCC\\xCD\\xCE\\xCF\\xD0\\xD1\\xD2\\xD3\\xD4\\xD5\\xD6\\xD7\\xD8\\xD9"
86 "\\xDA\\xDB\\xDC\\xDD\\xDE\\xDF\\xE0\\xE1\\xE2\\xE3\\xE4\\xE5\\xE6\\xE7" 86 "\\xDA\\xDB\\xDC\\xDD\\xDE\\xDF\\xE0\\xE1\\xE2\\xE3\\xE4\\xE5\\xE6\\xE7"
87 "\\xE8\\xE9\\xEA\\xEB\\xEC\\xED\\xEE\\xEF\\xF0\\xF1\\xF2\\xF3\\xF4\\xF5" 87 "\\xE8\\xE9\\xEA\\xEB\\xEC\\xED\\xEE\\xEF\\xF0\\xF1\\xF2\\xF3\\xF4\\xF5"
88 "\\xF6\\xF7\\xF8\\xF9\\xFA\\xFB\\xFC\\xFD\\xFE\\xFF"; 88 "\\xF6\\xF7\\xF8\\xF9\\xFA\\xFB\\xFC\\xFD\\xFE\\xFF";
89 89
90 std::string expected_output = "{\"all_chars\":\"" + all_chars_expected + 90 std::string expected_output = "{\"all_chars\":\"" + all_chars_expected +
91 "\"}"; 91 "\"}";
92 // Test JSONWriter interface 92 // Test JSONWriter interface
93 std::string output_js; 93 std::string output_js;
94 DictionaryValue valueRoot; 94 DictionaryValue valueRoot;
95 valueRoot.SetString(L"all_chars", all_chars); 95 valueRoot.SetString(ASCIIToUTF16("all_chars"), WideToUTF16Hack(all_chars));
96 JSONWriter::Write(&valueRoot, false, &output_js); 96 JSONWriter::Write(&valueRoot, false, &output_js);
97 ASSERT_EQ(expected_output, output_js); 97 ASSERT_EQ(expected_output, output_js);
98 98
99 // Test JSONValueSerializer interface (uses JSONWriter). 99 // Test JSONValueSerializer interface (uses JSONWriter).
100 JSONStringValueSerializer serializer(&output_js); 100 JSONStringValueSerializer serializer(&output_js);
101 ASSERT_TRUE(serializer.Serialize(valueRoot)); 101 ASSERT_TRUE(serializer.Serialize(valueRoot));
102 ASSERT_EQ(expected_output, output_js); 102 ASSERT_EQ(expected_output, output_js);
103 } 103 }
104 104
105 TEST(JSONValueSerializerTest, UnicodeStrings) { 105 TEST(JSONValueSerializerTest, UnicodeStrings) {
106 // unicode string json -> escaped ascii text 106 // unicode string json -> escaped ascii text
107 DictionaryValue root; 107 DictionaryValue root;
108 std::wstring test(L"\x7F51\x9875"); 108 std::wstring test(L"\x7F51\x9875");
109 root.SetString(L"web", test); 109 root.SetString(ASCIIToUTF16("web"), WideToUTF16Hack(test));
110 110
111 std::string expected = "{\"web\":\"\\u7F51\\u9875\"}"; 111 std::string expected = "{\"web\":\"\\u7F51\\u9875\"}";
112 112
113 std::string actual; 113 std::string actual;
114 JSONStringValueSerializer serializer(&actual); 114 JSONStringValueSerializer serializer(&actual);
115 ASSERT_TRUE(serializer.Serialize(root)); 115 ASSERT_TRUE(serializer.Serialize(root));
116 ASSERT_EQ(expected, actual); 116 ASSERT_EQ(expected, actual);
117 117
118 // escaped ascii text -> json 118 // escaped ascii text -> json
119 JSONStringValueSerializer deserializer(expected); 119 JSONStringValueSerializer deserializer(expected);
120 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL)); 120 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL));
121 ASSERT_TRUE(deserial_root.get()); 121 ASSERT_TRUE(deserial_root.get());
122 DictionaryValue* dict_root = 122 DictionaryValue* dict_root =
123 static_cast<DictionaryValue*>(deserial_root.get()); 123 static_cast<DictionaryValue*>(deserial_root.get());
124 std::wstring web_value; 124 string16 web_value;
125 ASSERT_TRUE(dict_root->GetString(L"web", &web_value)); 125 ASSERT_TRUE(dict_root->GetString(ASCIIToUTF16("web"), &web_value));
126 ASSERT_EQ(test, web_value); 126 ASSERT_EQ(test, UTF16ToWideHack(web_value));
127 } 127 }
128 128
129 TEST(JSONValueSerializerTest, HexStrings) { 129 TEST(JSONValueSerializerTest, HexStrings) {
130 // hex string json -> escaped ascii text 130 // hex string json -> escaped ascii text
131 DictionaryValue root; 131 DictionaryValue root;
132 std::wstring test(L"\x01\x02"); 132 std::wstring test(L"\x01\x02");
133 root.SetString(L"test", test); 133 root.SetString(ASCIIToUTF16("test"), WideToUTF16Hack(test));
134 134
135 std::string expected = "{\"test\":\"\\x01\\x02\"}"; 135 std::string expected = "{\"test\":\"\\x01\\x02\"}";
136 136
137 std::string actual; 137 std::string actual;
138 JSONStringValueSerializer serializer(&actual); 138 JSONStringValueSerializer serializer(&actual);
139 ASSERT_TRUE(serializer.Serialize(root)); 139 ASSERT_TRUE(serializer.Serialize(root));
140 ASSERT_EQ(expected, actual); 140 ASSERT_EQ(expected, actual);
141 141
142 // escaped ascii text -> json 142 // escaped ascii text -> json
143 JSONStringValueSerializer deserializer(expected); 143 JSONStringValueSerializer deserializer(expected);
144 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL)); 144 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL));
145 ASSERT_TRUE(deserial_root.get()); 145 ASSERT_TRUE(deserial_root.get());
146 DictionaryValue* dict_root = 146 DictionaryValue* dict_root =
147 static_cast<DictionaryValue*>(deserial_root.get()); 147 static_cast<DictionaryValue*>(deserial_root.get());
148 std::wstring test_value; 148 string16 test_value;
149 ASSERT_TRUE(dict_root->GetString(L"test", &test_value)); 149 ASSERT_TRUE(dict_root->GetString(ASCIIToUTF16("test"), &test_value));
150 ASSERT_EQ(test, test_value); 150 ASSERT_EQ(test, UTF16ToWideHack(test_value));
151 151
152 // Test converting escaped regular chars 152 // Test converting escaped regular chars
153 std::string escaped_chars = "{\"test\":\"\\x67\\x6f\"}"; 153 std::string escaped_chars = "{\"test\":\"\\x67\\x6f\"}";
154 JSONStringValueSerializer deserializer2(escaped_chars); 154 JSONStringValueSerializer deserializer2(escaped_chars);
155 deserial_root.reset(deserializer2.Deserialize(NULL)); 155 deserial_root.reset(deserializer2.Deserialize(NULL));
156 ASSERT_TRUE(deserial_root.get()); 156 ASSERT_TRUE(deserial_root.get());
157 dict_root = static_cast<DictionaryValue*>(deserial_root.get()); 157 dict_root = static_cast<DictionaryValue*>(deserial_root.get());
158 ASSERT_TRUE(dict_root->GetString(L"test", &test_value)); 158 ASSERT_TRUE(dict_root->GetString(ASCIIToUTF16("test"), &test_value));
159 ASSERT_EQ(std::wstring(L"go"), test_value); 159 ASSERT_EQ(L"go", UTF16ToWideHack(test_value));
160 } 160 }
161 161
162 TEST(JSONValueSerializerTest, AllowTrailingComma) { 162 TEST(JSONValueSerializerTest, AllowTrailingComma) {
163 scoped_ptr<Value> root; 163 scoped_ptr<Value> root;
164 scoped_ptr<Value> root_expected; 164 scoped_ptr<Value> root_expected;
165 std::string test_with_commas("{\"key\": [true,],}"); 165 std::string test_with_commas("{\"key\": [true,],}");
166 std::string test_no_commas("{\"key\": [true]}"); 166 std::string test_no_commas("{\"key\": [true]}");
167 167
168 JSONStringValueSerializer serializer(test_with_commas); 168 JSONStringValueSerializer serializer(test_with_commas);
169 serializer.set_allow_trailing_comma(true); 169 serializer.set_allow_trailing_comma(true);
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 JSONFileValueSerializer deserializer(original_file_path); 253 JSONFileValueSerializer deserializer(original_file_path);
254 scoped_ptr<Value> root; 254 scoped_ptr<Value> root;
255 root.reset(deserializer.Deserialize(NULL)); 255 root.reset(deserializer.Deserialize(NULL));
256 256
257 ASSERT_TRUE(root.get()); 257 ASSERT_TRUE(root.get());
258 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 258 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
259 259
260 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); 260 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
261 261
262 Value* null_value = NULL; 262 Value* null_value = NULL;
263 ASSERT_TRUE(root_dict->Get(L"null", &null_value)); 263 ASSERT_TRUE(root_dict->Get(ASCIIToUTF16("null"), &null_value));
264 ASSERT_TRUE(null_value); 264 ASSERT_TRUE(null_value);
265 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); 265 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL));
266 266
267 bool bool_value = false; 267 bool bool_value = false;
268 ASSERT_TRUE(root_dict->GetBoolean(L"bool", &bool_value)); 268 ASSERT_TRUE(root_dict->GetBoolean(ASCIIToUTF16("bool"), &bool_value));
269 ASSERT_TRUE(bool_value); 269 ASSERT_TRUE(bool_value);
270 270
271 int int_value = 0; 271 int int_value = 0;
272 ASSERT_TRUE(root_dict->GetInteger(L"int", &int_value)); 272 ASSERT_TRUE(root_dict->GetInteger(ASCIIToUTF16("int"), &int_value));
273 ASSERT_EQ(42, int_value); 273 ASSERT_EQ(42, int_value);
274 274
275 std::wstring string_value; 275 string16 string_value;
276 ASSERT_TRUE(root_dict->GetString(L"string", &string_value)); 276 ASSERT_TRUE(root_dict->GetString(ASCIIToUTF16("string"), &string_value));
277 ASSERT_EQ(L"hello", string_value); 277 ASSERT_EQ(L"hello", UTF16ToWideHack(string_value));
278 278
279 // Now try writing. 279 // Now try writing.
280 std::wstring written_file_path = test_dir_; 280 std::wstring written_file_path = test_dir_;
281 file_util::AppendToPath(&written_file_path, L"test_output.js"); 281 file_util::AppendToPath(&written_file_path, L"test_output.js");
282 282
283 ASSERT_FALSE(file_util::PathExists(written_file_path)); 283 ASSERT_FALSE(file_util::PathExists(written_file_path));
284 JSONFileValueSerializer serializer(written_file_path); 284 JSONFileValueSerializer serializer(written_file_path);
285 ASSERT_TRUE(serializer.Serialize(*root)); 285 ASSERT_TRUE(serializer.Serialize(*root));
286 ASSERT_TRUE(file_util::PathExists(written_file_path)); 286 ASSERT_TRUE(file_util::PathExists(written_file_path));
287 287
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 std::wstring source_file_path; 321 std::wstring source_file_path;
322 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); 322 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path));
323 file_util::AppendToPath(&source_file_path, 323 file_util::AppendToPath(&source_file_path,
324 L"serializer_test_nowhitespace.js"); 324 L"serializer_test_nowhitespace.js");
325 ASSERT_TRUE(file_util::PathExists(source_file_path)); 325 ASSERT_TRUE(file_util::PathExists(source_file_path));
326 JSONFileValueSerializer serializer(source_file_path); 326 JSONFileValueSerializer serializer(source_file_path);
327 scoped_ptr<Value> root; 327 scoped_ptr<Value> root;
328 root.reset(serializer.Deserialize(NULL)); 328 root.reset(serializer.Deserialize(NULL));
329 ASSERT_TRUE(root.get()); 329 ASSERT_TRUE(root.get());
330 } 330 }
OLDNEW
« no previous file with comments | « chrome/browser/ssl/ssl_policy.cc ('k') | chrome/common/pref_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698