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

Side by Side Diff: content/renderer/v8_value_converter_impl_unittest.cc

Issue 16690003: Update tests to not use to-be-removed V8 handle operations. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixed Created 7 years, 6 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/test/base/v8_unit_test.cc ('k') | webkit/plugins/ppapi/v8_var_converter_unittest.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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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 <cmath> 5 #include <cmath>
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/stl_util.h" 8 #include "base/stl_util.h"
9 #include "base/test/values_test_util.h" 9 #include "base/test/values_test_util.h"
10 #include "base/values.h" 10 #include "base/values.h"
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 48
49 // A dumb getter for an object's named callback. 49 // A dumb getter for an object's named callback.
50 v8::Handle<v8::Value> NamedCallbackGetter(v8::Local<v8::String> name, 50 v8::Handle<v8::Value> NamedCallbackGetter(v8::Local<v8::String> name,
51 const v8::AccessorInfo& info) { 51 const v8::AccessorInfo& info) {
52 return v8::String::New("bar"); 52 return v8::String::New("bar");
53 } 53 }
54 54
55 } // namespace 55 } // namespace
56 56
57 class V8ValueConverterImplTest : public testing::Test { 57 class V8ValueConverterImplTest : public testing::Test {
58 public:
59 V8ValueConverterImplTest()
60 : isolate_(v8::Isolate::GetCurrent()) {
61 }
62
58 protected: 63 protected:
59 virtual void SetUp() { 64 virtual void SetUp() {
60 v8::HandleScope handle_scope; 65 v8::HandleScope handle_scope(isolate_);
61 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); 66 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
62 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 67 context_.Reset(isolate_, v8::Context::New(isolate_, NULL, global));
63 // TODO(marja): Use v8::Persistent::Reset here.
64 context_ = v8::Persistent<v8::Context>(
65 isolate, v8::Context::New(isolate, NULL, global));
66 } 68 }
67 69
68 virtual void TearDown() { 70 virtual void TearDown() {
69 context_.Dispose(context_->GetIsolate()); 71 context_.Dispose();
70 } 72 }
71 73
72 std::string GetString(base::DictionaryValue* value, const std::string& key) { 74 std::string GetString(base::DictionaryValue* value, const std::string& key) {
73 std::string temp; 75 std::string temp;
74 if (!value->GetString(key, &temp)) { 76 if (!value->GetString(key, &temp)) {
75 ADD_FAILURE(); 77 ADD_FAILURE();
76 return std::string(); 78 return std::string();
77 } 79 }
78 return temp; 80 return temp;
79 } 81 }
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 ADD_FAILURE(); 143 ADD_FAILURE();
142 return false; 144 return false;
143 } 145 }
144 return child->IsNull(); 146 return child->IsNull();
145 } 147 }
146 148
147 void TestWeirdType(const V8ValueConverterImpl& converter, 149 void TestWeirdType(const V8ValueConverterImpl& converter,
148 v8::Handle<v8::Value> val, 150 v8::Handle<v8::Value> val,
149 base::Value::Type expected_type, 151 base::Value::Type expected_type,
150 scoped_ptr<base::Value> expected_value) { 152 scoped_ptr<base::Value> expected_value) {
151 scoped_ptr<base::Value> raw(converter.FromV8Value(val, context_)); 153 v8::Local<v8::Context> context =
154 v8::Local<v8::Context>::New(isolate_, context_);
155 scoped_ptr<base::Value> raw(converter.FromV8Value(val, context));
152 156
153 if (expected_value) { 157 if (expected_value) {
154 ASSERT_TRUE(raw.get()); 158 ASSERT_TRUE(raw.get());
155 EXPECT_TRUE(expected_value->Equals(raw.get())); 159 EXPECT_TRUE(expected_value->Equals(raw.get()));
156 EXPECT_EQ(expected_type, raw->GetType()); 160 EXPECT_EQ(expected_type, raw->GetType());
157 } else { 161 } else {
158 EXPECT_FALSE(raw.get()); 162 EXPECT_FALSE(raw.get());
159 } 163 }
160 164
161 v8::Handle<v8::Object> object(v8::Object::New()); 165 v8::Handle<v8::Object> object(v8::Object::New());
162 object->Set(v8::String::New("test"), val); 166 object->Set(v8::String::New("test"), val);
163 scoped_ptr<base::DictionaryValue> dictionary( 167 scoped_ptr<base::DictionaryValue> dictionary(
164 static_cast<base::DictionaryValue*>( 168 static_cast<base::DictionaryValue*>(
165 converter.FromV8Value(object, context_))); 169 converter.FromV8Value(object, context)));
166 ASSERT_TRUE(dictionary.get()); 170 ASSERT_TRUE(dictionary.get());
167 171
168 if (expected_value) { 172 if (expected_value) {
169 base::Value* temp = NULL; 173 base::Value* temp = NULL;
170 ASSERT_TRUE(dictionary->Get("test", &temp)); 174 ASSERT_TRUE(dictionary->Get("test", &temp));
171 EXPECT_EQ(expected_type, temp->GetType()); 175 EXPECT_EQ(expected_type, temp->GetType());
172 EXPECT_TRUE(expected_value->Equals(temp)); 176 EXPECT_TRUE(expected_value->Equals(temp));
173 } else { 177 } else {
174 EXPECT_FALSE(dictionary->HasKey("test")); 178 EXPECT_FALSE(dictionary->HasKey("test"));
175 } 179 }
176 180
177 v8::Handle<v8::Array> array(v8::Array::New()); 181 v8::Handle<v8::Array> array(v8::Array::New());
178 array->Set(0, val); 182 array->Set(0, val);
179 scoped_ptr<base::ListValue> list( 183 scoped_ptr<base::ListValue> list(
180 static_cast<base::ListValue*>(converter.FromV8Value(array, context_))); 184 static_cast<base::ListValue*>(converter.FromV8Value(array, context)));
181 ASSERT_TRUE(list.get()); 185 ASSERT_TRUE(list.get());
182 if (expected_value) { 186 if (expected_value) {
183 base::Value* temp = NULL; 187 base::Value* temp = NULL;
184 ASSERT_TRUE(list->Get(0, &temp)); 188 ASSERT_TRUE(list->Get(0, &temp));
185 EXPECT_EQ(expected_type, temp->GetType()); 189 EXPECT_EQ(expected_type, temp->GetType());
186 EXPECT_TRUE(expected_value->Equals(temp)); 190 EXPECT_TRUE(expected_value->Equals(temp));
187 } else { 191 } else {
188 // Arrays should preserve their length, and convert unconvertible 192 // Arrays should preserve their length, and convert unconvertible
189 // types into null. 193 // types into null.
190 base::Value* temp = NULL; 194 base::Value* temp = NULL;
191 ASSERT_TRUE(list->Get(0, &temp)); 195 ASSERT_TRUE(list->Get(0, &temp));
192 EXPECT_EQ(base::Value::TYPE_NULL, temp->GetType()); 196 EXPECT_EQ(base::Value::TYPE_NULL, temp->GetType());
193 } 197 }
194 } 198 }
195 199
200 v8::Isolate* isolate_;
201
196 // Context for the JavaScript in the test. 202 // Context for the JavaScript in the test.
197 v8::Persistent<v8::Context> context_; 203 v8::Persistent<v8::Context> context_;
198 }; 204 };
199 205
200 TEST_F(V8ValueConverterImplTest, BasicRoundTrip) { 206 TEST_F(V8ValueConverterImplTest, BasicRoundTrip) {
201 scoped_ptr<base::Value> original_root = base::test::ParseJson( 207 scoped_ptr<base::Value> original_root = base::test::ParseJson(
202 "{ \n" 208 "{ \n"
203 " \"null\": null, \n" 209 " \"null\": null, \n"
204 " \"true\": true, \n" 210 " \"true\": true, \n"
205 " \"false\": false, \n" 211 " \"false\": false, \n"
206 " \"positive-int\": 42, \n" 212 " \"positive-int\": 42, \n"
207 " \"negative-int\": -42, \n" 213 " \"negative-int\": -42, \n"
208 " \"zero\": 0, \n" 214 " \"zero\": 0, \n"
209 " \"double\": 88.8, \n" 215 " \"double\": 88.8, \n"
210 " \"big-integral-double\": 9007199254740992.0, \n" // 2.0^53 216 " \"big-integral-double\": 9007199254740992.0, \n" // 2.0^53
211 " \"string\": \"foobar\", \n" 217 " \"string\": \"foobar\", \n"
212 " \"empty-string\": \"\", \n" 218 " \"empty-string\": \"\", \n"
213 " \"dictionary\": { \n" 219 " \"dictionary\": { \n"
214 " \"foo\": \"bar\",\n" 220 " \"foo\": \"bar\",\n"
215 " \"hot\": \"dog\",\n" 221 " \"hot\": \"dog\",\n"
216 " }, \n" 222 " }, \n"
217 " \"empty-dictionary\": {}, \n" 223 " \"empty-dictionary\": {}, \n"
218 " \"list\": [ \"monkey\", \"balls\" ], \n" 224 " \"list\": [ \"monkey\", \"balls\" ], \n"
219 " \"empty-list\": [], \n" 225 " \"empty-list\": [], \n"
220 "}"); 226 "}");
221 227
222 v8::Context::Scope context_scope(context_); 228 v8::HandleScope handle_scope(isolate_);
223 v8::HandleScope handle_scope; 229 v8::Context::Scope context_scope(isolate_, context_);
230 v8::Local<v8::Context> context =
231 v8::Local<v8::Context>::New(isolate_, context_);
224 232
225 V8ValueConverterImpl converter; 233 V8ValueConverterImpl converter;
226 v8::Handle<v8::Object> v8_object = 234 v8::Handle<v8::Object> v8_object =
227 converter.ToV8Value(original_root.get(), context_).As<v8::Object>(); 235 converter.ToV8Value(original_root.get(), context).As<v8::Object>();
228 ASSERT_FALSE(v8_object.IsEmpty()); 236 ASSERT_FALSE(v8_object.IsEmpty());
229 237
230 EXPECT_EQ(static_cast<const base::DictionaryValue&>(*original_root).size(), 238 EXPECT_EQ(static_cast<const base::DictionaryValue&>(*original_root).size(),
231 v8_object->GetPropertyNames()->Length()); 239 v8_object->GetPropertyNames()->Length());
232 EXPECT_TRUE(v8_object->Get(v8::String::New("null"))->IsNull()); 240 EXPECT_TRUE(v8_object->Get(v8::String::New("null"))->IsNull());
233 EXPECT_TRUE(v8_object->Get(v8::String::New("true"))->IsTrue()); 241 EXPECT_TRUE(v8_object->Get(v8::String::New("true"))->IsTrue());
234 EXPECT_TRUE(v8_object->Get(v8::String::New("false"))->IsFalse()); 242 EXPECT_TRUE(v8_object->Get(v8::String::New("false"))->IsFalse());
235 EXPECT_TRUE(v8_object->Get(v8::String::New("positive-int"))->IsInt32()); 243 EXPECT_TRUE(v8_object->Get(v8::String::New("positive-int"))->IsInt32());
236 EXPECT_TRUE(v8_object->Get(v8::String::New("negative-int"))->IsInt32()); 244 EXPECT_TRUE(v8_object->Get(v8::String::New("negative-int"))->IsInt32());
237 EXPECT_TRUE(v8_object->Get(v8::String::New("zero"))->IsInt32()); 245 EXPECT_TRUE(v8_object->Get(v8::String::New("zero"))->IsInt32());
238 EXPECT_TRUE(v8_object->Get(v8::String::New("double"))->IsNumber()); 246 EXPECT_TRUE(v8_object->Get(v8::String::New("double"))->IsNumber());
239 EXPECT_TRUE( 247 EXPECT_TRUE(
240 v8_object->Get(v8::String::New("big-integral-double"))->IsNumber()); 248 v8_object->Get(v8::String::New("big-integral-double"))->IsNumber());
241 EXPECT_TRUE(v8_object->Get(v8::String::New("string"))->IsString()); 249 EXPECT_TRUE(v8_object->Get(v8::String::New("string"))->IsString());
242 EXPECT_TRUE(v8_object->Get(v8::String::New("empty-string"))->IsString()); 250 EXPECT_TRUE(v8_object->Get(v8::String::New("empty-string"))->IsString());
243 EXPECT_TRUE(v8_object->Get(v8::String::New("dictionary"))->IsObject()); 251 EXPECT_TRUE(v8_object->Get(v8::String::New("dictionary"))->IsObject());
244 EXPECT_TRUE(v8_object->Get(v8::String::New("empty-dictionary"))->IsObject()); 252 EXPECT_TRUE(v8_object->Get(v8::String::New("empty-dictionary"))->IsObject());
245 EXPECT_TRUE(v8_object->Get(v8::String::New("list"))->IsArray()); 253 EXPECT_TRUE(v8_object->Get(v8::String::New("list"))->IsArray());
246 EXPECT_TRUE(v8_object->Get(v8::String::New("empty-list"))->IsArray()); 254 EXPECT_TRUE(v8_object->Get(v8::String::New("empty-list"))->IsArray());
247 255
248 scoped_ptr<base::Value> new_root(converter.FromV8Value(v8_object, context_)); 256 scoped_ptr<base::Value> new_root(converter.FromV8Value(v8_object, context));
249 EXPECT_NE(original_root.get(), new_root.get()); 257 EXPECT_NE(original_root.get(), new_root.get());
250 EXPECT_TRUE(original_root->Equals(new_root.get())); 258 EXPECT_TRUE(original_root->Equals(new_root.get()));
251 } 259 }
252 260
253 TEST_F(V8ValueConverterImplTest, KeysWithDots) { 261 TEST_F(V8ValueConverterImplTest, KeysWithDots) {
254 scoped_ptr<base::Value> original = 262 scoped_ptr<base::Value> original =
255 base::test::ParseJson("{ \"foo.bar\": \"baz\" }"); 263 base::test::ParseJson("{ \"foo.bar\": \"baz\" }");
256 264
257 v8::Context::Scope context_scope(context_); 265 v8::HandleScope handle_scope(isolate_);
258 v8::HandleScope handle_scope; 266 v8::Context::Scope context_scope(isolate_, context_);
267 v8::Local<v8::Context> context =
268 v8::Local<v8::Context>::New(isolate_, context_);
259 269
260 V8ValueConverterImpl converter; 270 V8ValueConverterImpl converter;
261 scoped_ptr<base::Value> copy( 271 scoped_ptr<base::Value> copy(
262 converter.FromV8Value( 272 converter.FromV8Value(
263 converter.ToV8Value(original.get(), context_), context_)); 273 converter.ToV8Value(original.get(), context), context));
264 274
265 EXPECT_TRUE(original->Equals(copy.get())); 275 EXPECT_TRUE(original->Equals(copy.get()));
266 } 276 }
267 277
268 TEST_F(V8ValueConverterImplTest, ObjectExceptions) { 278 TEST_F(V8ValueConverterImplTest, ObjectExceptions) {
269 v8::Context::Scope context_scope(context_); 279 v8::HandleScope handle_scope(isolate_);
270 v8::HandleScope handle_scope; 280 v8::Context::Scope context_scope(isolate_, context_);
281 v8::Local<v8::Context> context =
282 v8::Local<v8::Context>::New(isolate_, context_);
271 283
272 // Set up objects to throw when reading or writing 'foo'. 284 // Set up objects to throw when reading or writing 'foo'.
273 const char* source = 285 const char* source =
274 "Object.prototype.__defineSetter__('foo', " 286 "Object.prototype.__defineSetter__('foo', "
275 " function() { throw new Error('muah!'); });" 287 " function() { throw new Error('muah!'); });"
276 "Object.prototype.__defineGetter__('foo', " 288 "Object.prototype.__defineGetter__('foo', "
277 " function() { throw new Error('muah!'); });"; 289 " function() { throw new Error('muah!'); });";
278 290
279 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source))); 291 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source)));
280 script->Run(); 292 script->Run();
281 293
282 v8::Handle<v8::Object> object(v8::Object::New()); 294 v8::Handle<v8::Object> object(v8::Object::New());
283 object->Set(v8::String::New("bar"), v8::String::New("bar")); 295 object->Set(v8::String::New("bar"), v8::String::New("bar"));
284 296
285 // Converting from v8 value should replace the foo property with null. 297 // Converting from v8 value should replace the foo property with null.
286 V8ValueConverterImpl converter; 298 V8ValueConverterImpl converter;
287 scoped_ptr<base::DictionaryValue> converted( 299 scoped_ptr<base::DictionaryValue> converted(
288 static_cast<base::DictionaryValue*>( 300 static_cast<base::DictionaryValue*>(
289 converter.FromV8Value(object, context_))); 301 converter.FromV8Value(object, context)));
290 EXPECT_TRUE(converted.get()); 302 EXPECT_TRUE(converted.get());
291 // http://code.google.com/p/v8/issues/detail?id=1342 303 // http://code.google.com/p/v8/issues/detail?id=1342
292 // EXPECT_EQ(2u, converted->size()); 304 // EXPECT_EQ(2u, converted->size());
293 // EXPECT_TRUE(IsNull(converted.get(), "foo")); 305 // EXPECT_TRUE(IsNull(converted.get(), "foo"));
294 EXPECT_EQ(1u, converted->size()); 306 EXPECT_EQ(1u, converted->size());
295 EXPECT_EQ("bar", GetString(converted.get(), "bar")); 307 EXPECT_EQ("bar", GetString(converted.get(), "bar"));
296 308
297 // Converting to v8 value should drop the foo property. 309 // Converting to v8 value should drop the foo property.
298 converted->SetString("foo", "foo"); 310 converted->SetString("foo", "foo");
299 v8::Handle<v8::Object> copy = 311 v8::Handle<v8::Object> copy =
300 converter.ToV8Value(converted.get(), context_).As<v8::Object>(); 312 converter.ToV8Value(converted.get(), context).As<v8::Object>();
301 EXPECT_FALSE(copy.IsEmpty()); 313 EXPECT_FALSE(copy.IsEmpty());
302 EXPECT_EQ(2u, copy->GetPropertyNames()->Length()); 314 EXPECT_EQ(2u, copy->GetPropertyNames()->Length());
303 EXPECT_EQ("bar", GetString(copy, "bar")); 315 EXPECT_EQ("bar", GetString(copy, "bar"));
304 } 316 }
305 317
306 TEST_F(V8ValueConverterImplTest, ArrayExceptions) { 318 TEST_F(V8ValueConverterImplTest, ArrayExceptions) {
307 v8::Context::Scope context_scope(context_); 319 v8::HandleScope handle_scope(isolate_);
308 v8::HandleScope handle_scope; 320 v8::Context::Scope context_scope(isolate_, context_);
321 v8::Local<v8::Context> context =
322 v8::Local<v8::Context>::New(isolate_, context_);
309 323
310 const char* source = "(function() {" 324 const char* source = "(function() {"
311 "var arr = [];" 325 "var arr = [];"
312 "arr.__defineSetter__(0, " 326 "arr.__defineSetter__(0, "
313 " function() { throw new Error('muah!'); });" 327 " function() { throw new Error('muah!'); });"
314 "arr.__defineGetter__(0, " 328 "arr.__defineGetter__(0, "
315 " function() { throw new Error('muah!'); });" 329 " function() { throw new Error('muah!'); });"
316 "arr[1] = 'bar';" 330 "arr[1] = 'bar';"
317 "return arr;" 331 "return arr;"
318 "})();"; 332 "})();";
319 333
320 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source))); 334 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source)));
321 v8::Handle<v8::Array> array = script->Run().As<v8::Array>(); 335 v8::Handle<v8::Array> array = script->Run().As<v8::Array>();
322 ASSERT_FALSE(array.IsEmpty()); 336 ASSERT_FALSE(array.IsEmpty());
323 337
324 // Converting from v8 value should replace the first item with null. 338 // Converting from v8 value should replace the first item with null.
325 V8ValueConverterImpl converter; 339 V8ValueConverterImpl converter;
326 scoped_ptr<base::ListValue> converted(static_cast<base::ListValue*>( 340 scoped_ptr<base::ListValue> converted(static_cast<base::ListValue*>(
327 converter.FromV8Value(array, context_))); 341 converter.FromV8Value(array, context)));
328 ASSERT_TRUE(converted.get()); 342 ASSERT_TRUE(converted.get());
329 // http://code.google.com/p/v8/issues/detail?id=1342 343 // http://code.google.com/p/v8/issues/detail?id=1342
330 EXPECT_EQ(2u, converted->GetSize()); 344 EXPECT_EQ(2u, converted->GetSize());
331 EXPECT_TRUE(IsNull(converted.get(), 0)); 345 EXPECT_TRUE(IsNull(converted.get(), 0));
332 346
333 // Converting to v8 value should drop the first item and leave a hole. 347 // Converting to v8 value should drop the first item and leave a hole.
334 converted.reset(static_cast<base::ListValue*>( 348 converted.reset(static_cast<base::ListValue*>(
335 base::test::ParseJson("[ \"foo\", \"bar\" ]").release())); 349 base::test::ParseJson("[ \"foo\", \"bar\" ]").release()));
336 v8::Handle<v8::Array> copy = 350 v8::Handle<v8::Array> copy =
337 converter.ToV8Value(converted.get(), context_).As<v8::Array>(); 351 converter.ToV8Value(converted.get(), context).As<v8::Array>();
338 ASSERT_FALSE(copy.IsEmpty()); 352 ASSERT_FALSE(copy.IsEmpty());
339 EXPECT_EQ(2u, copy->Length()); 353 EXPECT_EQ(2u, copy->Length());
340 EXPECT_EQ("bar", GetString(copy, 1)); 354 EXPECT_EQ("bar", GetString(copy, 1));
341 } 355 }
342 356
343 TEST_F(V8ValueConverterImplTest, WeirdTypes) { 357 TEST_F(V8ValueConverterImplTest, WeirdTypes) {
344 v8::Context::Scope context_scope(context_); 358 v8::HandleScope handle_scope(isolate_);
345 v8::HandleScope handle_scope; 359 v8::Context::Scope context_scope(isolate_, context_);
346 360
347 v8::Handle<v8::RegExp> regex( 361 v8::Handle<v8::RegExp> regex(
348 v8::RegExp::New(v8::String::New("."), v8::RegExp::kNone)); 362 v8::RegExp::New(v8::String::New("."), v8::RegExp::kNone));
349 363
350 V8ValueConverterImpl converter; 364 V8ValueConverterImpl converter;
351 TestWeirdType(converter, 365 TestWeirdType(converter,
352 v8::Undefined(), 366 v8::Undefined(),
353 base::Value::TYPE_NULL, // Arbitrary type, result is NULL. 367 base::Value::TYPE_NULL, // Arbitrary type, result is NULL.
354 scoped_ptr<base::Value>(NULL)); 368 scoped_ptr<base::Value>(NULL));
355 TestWeirdType(converter, 369 TestWeirdType(converter,
(...skipping 12 matching lines...) Expand all
368 scoped_ptr<base::Value>(new base::FundamentalValue(1.0))); 382 scoped_ptr<base::Value>(new base::FundamentalValue(1.0)));
369 383
370 converter.SetRegExpAllowed(true); 384 converter.SetRegExpAllowed(true);
371 TestWeirdType(converter, 385 TestWeirdType(converter,
372 regex, 386 regex,
373 base::Value::TYPE_STRING, 387 base::Value::TYPE_STRING,
374 scoped_ptr<base::Value>(new base::StringValue("/./"))); 388 scoped_ptr<base::Value>(new base::StringValue("/./")));
375 } 389 }
376 390
377 TEST_F(V8ValueConverterImplTest, Prototype) { 391 TEST_F(V8ValueConverterImplTest, Prototype) {
378 v8::Context::Scope context_scope(context_); 392 v8::HandleScope handle_scope(isolate_);
379 v8::HandleScope handle_scope; 393 v8::Context::Scope context_scope(isolate_, context_);
394 v8::Local<v8::Context> context =
395 v8::Local<v8::Context>::New(isolate_, context_);
380 396
381 const char* source = "(function() {" 397 const char* source = "(function() {"
382 "Object.prototype.foo = 'foo';" 398 "Object.prototype.foo = 'foo';"
383 "return {};" 399 "return {};"
384 "})();"; 400 "})();";
385 401
386 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source))); 402 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source)));
387 v8::Handle<v8::Object> object = script->Run().As<v8::Object>(); 403 v8::Handle<v8::Object> object = script->Run().As<v8::Object>();
388 ASSERT_FALSE(object.IsEmpty()); 404 ASSERT_FALSE(object.IsEmpty());
389 405
390 V8ValueConverterImpl converter; 406 V8ValueConverterImpl converter;
391 scoped_ptr<base::DictionaryValue> result( 407 scoped_ptr<base::DictionaryValue> result(
392 static_cast<base::DictionaryValue*>( 408 static_cast<base::DictionaryValue*>(
393 converter.FromV8Value(object, context_))); 409 converter.FromV8Value(object, context)));
394 ASSERT_TRUE(result.get()); 410 ASSERT_TRUE(result.get());
395 EXPECT_EQ(0u, result->size()); 411 EXPECT_EQ(0u, result->size());
396 } 412 }
397 413
398 TEST_F(V8ValueConverterImplTest, StripNullFromObjects) { 414 TEST_F(V8ValueConverterImplTest, StripNullFromObjects) {
399 v8::Context::Scope context_scope(context_); 415 v8::HandleScope handle_scope(isolate_);
400 v8::HandleScope handle_scope; 416 v8::Context::Scope context_scope(isolate_, context_);
417 v8::Local<v8::Context> context =
418 v8::Local<v8::Context>::New(isolate_, context_);
401 419
402 const char* source = "(function() {" 420 const char* source = "(function() {"
403 "return { foo: undefined, bar: null };" 421 "return { foo: undefined, bar: null };"
404 "})();"; 422 "})();";
405 423
406 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source))); 424 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source)));
407 v8::Handle<v8::Object> object = script->Run().As<v8::Object>(); 425 v8::Handle<v8::Object> object = script->Run().As<v8::Object>();
408 ASSERT_FALSE(object.IsEmpty()); 426 ASSERT_FALSE(object.IsEmpty());
409 427
410 V8ValueConverterImpl converter; 428 V8ValueConverterImpl converter;
411 converter.SetStripNullFromObjects(true); 429 converter.SetStripNullFromObjects(true);
412 430
413 scoped_ptr<base::DictionaryValue> result( 431 scoped_ptr<base::DictionaryValue> result(
414 static_cast<base::DictionaryValue*>( 432 static_cast<base::DictionaryValue*>(
415 converter.FromV8Value(object, context_))); 433 converter.FromV8Value(object, context)));
416 ASSERT_TRUE(result.get()); 434 ASSERT_TRUE(result.get());
417 EXPECT_EQ(0u, result->size()); 435 EXPECT_EQ(0u, result->size());
418 } 436 }
419 437
420 TEST_F(V8ValueConverterImplTest, RecursiveObjects) { 438 TEST_F(V8ValueConverterImplTest, RecursiveObjects) {
421 v8::Context::Scope context_scope(context_); 439 v8::HandleScope handle_scope(isolate_);
422 v8::HandleScope handle_scope; 440 v8::Context::Scope context_scope(isolate_, context_);
441 v8::Local<v8::Context> context =
442 v8::Local<v8::Context>::New(isolate_, context_);
423 443
424 V8ValueConverterImpl converter; 444 V8ValueConverterImpl converter;
425 445
426 v8::Handle<v8::Object> object = v8::Object::New().As<v8::Object>(); 446 v8::Handle<v8::Object> object = v8::Object::New().As<v8::Object>();
427 ASSERT_FALSE(object.IsEmpty()); 447 ASSERT_FALSE(object.IsEmpty());
428 object->Set(v8::String::New("foo"), v8::String::New("bar")); 448 object->Set(v8::String::New("foo"), v8::String::New("bar"));
429 object->Set(v8::String::New("obj"), object); 449 object->Set(v8::String::New("obj"), object);
430 450
431 scoped_ptr<base::DictionaryValue> object_result( 451 scoped_ptr<base::DictionaryValue> object_result(
432 static_cast<base::DictionaryValue*>( 452 static_cast<base::DictionaryValue*>(
433 converter.FromV8Value(object, context_))); 453 converter.FromV8Value(object, context)));
434 ASSERT_TRUE(object_result.get()); 454 ASSERT_TRUE(object_result.get());
435 EXPECT_EQ(2u, object_result->size()); 455 EXPECT_EQ(2u, object_result->size());
436 EXPECT_TRUE(IsNull(object_result.get(), "obj")); 456 EXPECT_TRUE(IsNull(object_result.get(), "obj"));
437 457
438 v8::Handle<v8::Array> array = v8::Array::New().As<v8::Array>(); 458 v8::Handle<v8::Array> array = v8::Array::New().As<v8::Array>();
439 ASSERT_FALSE(array.IsEmpty()); 459 ASSERT_FALSE(array.IsEmpty());
440 array->Set(0, v8::String::New("1")); 460 array->Set(0, v8::String::New("1"));
441 array->Set(1, array); 461 array->Set(1, array);
442 462
443 scoped_ptr<base::ListValue> list_result( 463 scoped_ptr<base::ListValue> list_result(
444 static_cast<base::ListValue*>(converter.FromV8Value(array, context_))); 464 static_cast<base::ListValue*>(converter.FromV8Value(array, context)));
445 ASSERT_TRUE(list_result.get()); 465 ASSERT_TRUE(list_result.get());
446 EXPECT_EQ(2u, list_result->GetSize()); 466 EXPECT_EQ(2u, list_result->GetSize());
447 EXPECT_TRUE(IsNull(list_result.get(), 1)); 467 EXPECT_TRUE(IsNull(list_result.get(), 1));
448 } 468 }
449 469
450 TEST_F(V8ValueConverterImplTest, WeirdProperties) { 470 TEST_F(V8ValueConverterImplTest, WeirdProperties) {
451 v8::Context::Scope context_scope(context_); 471 v8::HandleScope handle_scope(isolate_);
452 v8::HandleScope handle_scope; 472 v8::Context::Scope context_scope(isolate_, context_);
473 v8::Local<v8::Context> context =
474 v8::Local<v8::Context>::New(isolate_, context_);
453 475
454 const char* source = "(function() {" 476 const char* source = "(function() {"
455 "return {" 477 "return {"
456 "1: 'foo'," 478 "1: 'foo',"
457 "'2': 'bar'," 479 "'2': 'bar',"
458 "true: 'baz'," 480 "true: 'baz',"
459 "false: 'qux'," 481 "false: 'qux',"
460 "null: 'quux'," 482 "null: 'quux',"
461 "undefined: 'oops'" 483 "undefined: 'oops'"
462 "};" 484 "};"
463 "})();"; 485 "})();";
464 486
465 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source))); 487 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source)));
466 v8::Handle<v8::Object> object = script->Run().As<v8::Object>(); 488 v8::Handle<v8::Object> object = script->Run().As<v8::Object>();
467 ASSERT_FALSE(object.IsEmpty()); 489 ASSERT_FALSE(object.IsEmpty());
468 490
469 V8ValueConverterImpl converter; 491 V8ValueConverterImpl converter;
470 scoped_ptr<base::Value> actual(converter.FromV8Value(object, context_)); 492 scoped_ptr<base::Value> actual(converter.FromV8Value(object, context));
471 493
472 scoped_ptr<base::Value> expected = base::test::ParseJson( 494 scoped_ptr<base::Value> expected = base::test::ParseJson(
473 "{ \n" 495 "{ \n"
474 " \"1\": \"foo\", \n" 496 " \"1\": \"foo\", \n"
475 " \"2\": \"bar\", \n" 497 " \"2\": \"bar\", \n"
476 " \"true\": \"baz\", \n" 498 " \"true\": \"baz\", \n"
477 " \"false\": \"qux\", \n" 499 " \"false\": \"qux\", \n"
478 " \"null\": \"quux\", \n" 500 " \"null\": \"quux\", \n"
479 " \"undefined\": \"oops\", \n" 501 " \"undefined\": \"oops\", \n"
480 "}"); 502 "}");
481 503
482 EXPECT_TRUE(expected->Equals(actual.get())); 504 EXPECT_TRUE(expected->Equals(actual.get()));
483 } 505 }
484 506
485 TEST_F(V8ValueConverterImplTest, ArrayGetters) { 507 TEST_F(V8ValueConverterImplTest, ArrayGetters) {
486 v8::Context::Scope context_scope(context_); 508 v8::HandleScope handle_scope(isolate_);
487 v8::HandleScope handle_scope; 509 v8::Context::Scope context_scope(isolate_, context_);
510 v8::Local<v8::Context> context =
511 v8::Local<v8::Context>::New(isolate_, context_);
488 512
489 const char* source = "(function() {" 513 const char* source = "(function() {"
490 "var a = [0];" 514 "var a = [0];"
491 "a.__defineGetter__(1, function() { return 'bar'; });" 515 "a.__defineGetter__(1, function() { return 'bar'; });"
492 "return a;" 516 "return a;"
493 "})();"; 517 "})();";
494 518
495 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source))); 519 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source)));
496 v8::Handle<v8::Array> array = script->Run().As<v8::Array>(); 520 v8::Handle<v8::Array> array = script->Run().As<v8::Array>();
497 ASSERT_FALSE(array.IsEmpty()); 521 ASSERT_FALSE(array.IsEmpty());
498 522
499 V8ValueConverterImpl converter; 523 V8ValueConverterImpl converter;
500 scoped_ptr<base::ListValue> result( 524 scoped_ptr<base::ListValue> result(
501 static_cast<base::ListValue*>(converter.FromV8Value(array, context_))); 525 static_cast<base::ListValue*>(converter.FromV8Value(array, context)));
502 ASSERT_TRUE(result.get()); 526 ASSERT_TRUE(result.get());
503 EXPECT_EQ(2u, result->GetSize()); 527 EXPECT_EQ(2u, result->GetSize());
504 } 528 }
505 529
506 TEST_F(V8ValueConverterImplTest, UndefinedValueBehavior) { 530 TEST_F(V8ValueConverterImplTest, UndefinedValueBehavior) {
507 v8::Context::Scope context_scope(context_); 531 v8::HandleScope handle_scope(isolate_);
508 v8::HandleScope handle_scope; 532 v8::Context::Scope context_scope(isolate_, context_);
533 v8::Local<v8::Context> context =
534 v8::Local<v8::Context>::New(isolate_, context_);
509 535
510 v8::Handle<v8::Object> object; 536 v8::Handle<v8::Object> object;
511 { 537 {
512 const char* source = "(function() {" 538 const char* source = "(function() {"
513 "return { foo: undefined, bar: null, baz: function(){} };" 539 "return { foo: undefined, bar: null, baz: function(){} };"
514 "})();"; 540 "})();";
515 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source))); 541 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source)));
516 object = script->Run().As<v8::Object>(); 542 object = script->Run().As<v8::Object>();
517 ASSERT_FALSE(object.IsEmpty()); 543 ASSERT_FALSE(object.IsEmpty());
518 } 544 }
519 545
520 v8::Handle<v8::Array> array; 546 v8::Handle<v8::Array> array;
521 { 547 {
522 const char* source = "(function() {" 548 const char* source = "(function() {"
523 "return [ undefined, null, function(){} ];" 549 "return [ undefined, null, function(){} ];"
524 "})();"; 550 "})();";
525 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source))); 551 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source)));
526 array = script->Run().As<v8::Array>(); 552 array = script->Run().As<v8::Array>();
527 ASSERT_FALSE(array.IsEmpty()); 553 ASSERT_FALSE(array.IsEmpty());
528 } 554 }
529 555
530 V8ValueConverterImpl converter; 556 V8ValueConverterImpl converter;
531 557
532 scoped_ptr<base::Value> actual_object( 558 scoped_ptr<base::Value> actual_object(
533 converter.FromV8Value(object, context_)); 559 converter.FromV8Value(object, context));
534 EXPECT_TRUE(base::Value::Equals( 560 EXPECT_TRUE(base::Value::Equals(
535 base::test::ParseJson("{ \"bar\": null }").get(), actual_object.get())); 561 base::test::ParseJson("{ \"bar\": null }").get(), actual_object.get()));
536 562
537 // Everything is null because JSON stringification preserves array length. 563 // Everything is null because JSON stringification preserves array length.
538 scoped_ptr<Value> actual_array(converter.FromV8Value(array, context_)); 564 scoped_ptr<Value> actual_array(converter.FromV8Value(array, context));
539 EXPECT_TRUE(base::Value::Equals( 565 EXPECT_TRUE(base::Value::Equals(
540 base::test::ParseJson("[ null, null, null ]").get(), actual_array.get())); 566 base::test::ParseJson("[ null, null, null ]").get(), actual_array.get()));
541 } 567 }
542 568
543 TEST_F(V8ValueConverterImplTest, ObjectsWithClashingIdentityHash) { 569 TEST_F(V8ValueConverterImplTest, ObjectsWithClashingIdentityHash) {
544 v8::Context::Scope context_scope(context_); 570 v8::HandleScope handle_scope(isolate_);
545 v8::HandleScope handle_scope; 571 v8::Context::Scope context_scope(isolate_, context_);
572 v8::Local<v8::Context> context =
573 v8::Local<v8::Context>::New(isolate_, context_);
546 V8ValueConverterImpl converter; 574 V8ValueConverterImpl converter;
547 575
548 // We check that the converter checks identity correctly by disabling the 576 // We check that the converter checks identity correctly by disabling the
549 // optimization of using identity hashes. 577 // optimization of using identity hashes.
550 ScopedAvoidIdentityHashForTesting scoped_hash_avoider(&converter); 578 ScopedAvoidIdentityHashForTesting scoped_hash_avoider(&converter);
551 579
552 // Create the v8::Object to be converted. 580 // Create the v8::Object to be converted.
553 v8::Handle<v8::Array> root(v8::Array::New(4)); 581 v8::Handle<v8::Array> root(v8::Array::New(4));
554 root->Set(0, v8::Handle<v8::Object>(v8::Object::New())); 582 root->Set(0, v8::Handle<v8::Object>(v8::Object::New()));
555 root->Set(1, v8::Handle<v8::Object>(v8::Object::New())); 583 root->Set(1, v8::Handle<v8::Object>(v8::Object::New()));
556 root->Set(2, v8::Handle<v8::Object>(v8::Array::New(0))); 584 root->Set(2, v8::Handle<v8::Object>(v8::Array::New(0)));
557 root->Set(3, v8::Handle<v8::Object>(v8::Array::New(0))); 585 root->Set(3, v8::Handle<v8::Object>(v8::Array::New(0)));
558 586
559 // The expected base::Value result. 587 // The expected base::Value result.
560 scoped_ptr<base::Value> expected = base::test::ParseJson("[{},{},[],[]]"); 588 scoped_ptr<base::Value> expected = base::test::ParseJson("[{},{},[],[]]");
561 ASSERT_TRUE(expected.get()); 589 ASSERT_TRUE(expected.get());
562 590
563 // The actual result. 591 // The actual result.
564 scoped_ptr<base::Value> value(converter.FromV8Value(root, context_)); 592 scoped_ptr<base::Value> value(converter.FromV8Value(root, context));
565 ASSERT_TRUE(value.get()); 593 ASSERT_TRUE(value.get());
566 594
567 EXPECT_TRUE(expected->Equals(value.get())); 595 EXPECT_TRUE(expected->Equals(value.get()));
568 } 596 }
569 597
570 TEST_F(V8ValueConverterImplTest, DetectCycles) { 598 TEST_F(V8ValueConverterImplTest, DetectCycles) {
571 v8::Context::Scope context_scope(context_); 599 v8::HandleScope handle_scope(isolate_);
572 v8::HandleScope handle_scope; 600 v8::Context::Scope context_scope(isolate_, context_);
601 v8::Local<v8::Context> context =
602 v8::Local<v8::Context>::New(isolate_, context_);
573 V8ValueConverterImpl converter; 603 V8ValueConverterImpl converter;
574 604
575 // Create a recursive array. 605 // Create a recursive array.
576 v8::Handle<v8::Array> recursive_array(v8::Array::New(1)); 606 v8::Handle<v8::Array> recursive_array(v8::Array::New(1));
577 recursive_array->Set(0, recursive_array); 607 recursive_array->Set(0, recursive_array);
578 608
579 // The first repetition should be trimmed and replaced by a null value. 609 // The first repetition should be trimmed and replaced by a null value.
580 base::ListValue expected_list; 610 base::ListValue expected_list;
581 expected_list.Append(base::Value::CreateNullValue()); 611 expected_list.Append(base::Value::CreateNullValue());
582 612
583 // The actual result. 613 // The actual result.
584 scoped_ptr<base::Value> actual_list( 614 scoped_ptr<base::Value> actual_list(
585 converter.FromV8Value(recursive_array, context_)); 615 converter.FromV8Value(recursive_array, context));
586 ASSERT_TRUE(actual_list.get()); 616 ASSERT_TRUE(actual_list.get());
587 617
588 EXPECT_TRUE(expected_list.Equals(actual_list.get())); 618 EXPECT_TRUE(expected_list.Equals(actual_list.get()));
589 619
590 // Now create a recursive object 620 // Now create a recursive object
591 const std::string key("key"); 621 const std::string key("key");
592 v8::Handle<v8::Object> recursive_object(v8::Object::New()); 622 v8::Handle<v8::Object> recursive_object(v8::Object::New());
593 v8::TryCatch try_catch; 623 v8::TryCatch try_catch;
594 recursive_object->Set(v8::String::New(key.c_str(), key.length()), 624 recursive_object->Set(v8::String::New(key.c_str(), key.length()),
595 recursive_object); 625 recursive_object);
596 ASSERT_FALSE(try_catch.HasCaught()); 626 ASSERT_FALSE(try_catch.HasCaught());
597 627
598 // The first repetition should be trimmed and replaced by a null value. 628 // The first repetition should be trimmed and replaced by a null value.
599 base::DictionaryValue expected_dictionary; 629 base::DictionaryValue expected_dictionary;
600 expected_dictionary.Set(key, base::Value::CreateNullValue()); 630 expected_dictionary.Set(key, base::Value::CreateNullValue());
601 631
602 // The actual result. 632 // The actual result.
603 scoped_ptr<base::Value> actual_dictionary( 633 scoped_ptr<base::Value> actual_dictionary(
604 converter.FromV8Value(recursive_object, context_)); 634 converter.FromV8Value(recursive_object, context));
605 ASSERT_TRUE(actual_dictionary.get()); 635 ASSERT_TRUE(actual_dictionary.get());
606 636
607 EXPECT_TRUE(expected_dictionary.Equals(actual_dictionary.get())); 637 EXPECT_TRUE(expected_dictionary.Equals(actual_dictionary.get()));
608 } 638 }
609 639
610 TEST_F(V8ValueConverterImplTest, MaxRecursionDepth) { 640 TEST_F(V8ValueConverterImplTest, MaxRecursionDepth) {
611 v8::Context::Scope context_scope(context_); 641 v8::HandleScope handle_scope(isolate_);
612 v8::HandleScope handle_scope; 642 v8::Context::Scope context_scope(isolate_, context_);
643 v8::Local<v8::Context> context =
644 v8::Local<v8::Context>::New(isolate_, context_);
613 645
614 // Must larger than kMaxRecursionDepth in v8_value_converter_impl.cc. 646 // Must larger than kMaxRecursionDepth in v8_value_converter_impl.cc.
615 int kDepth = 100; 647 int kDepth = 100;
616 const char kKey[] = "key"; 648 const char kKey[] = "key";
617 649
618 v8::Local<v8::Object> deep_object = v8::Object::New(); 650 v8::Local<v8::Object> deep_object = v8::Object::New();
619 651
620 v8::Local<v8::Object> leaf = deep_object; 652 v8::Local<v8::Object> leaf = deep_object;
621 for (int i = 0; i < kDepth; ++i) { 653 for (int i = 0; i < kDepth; ++i) {
622 v8::Local<v8::Object> new_object = v8::Object::New(); 654 v8::Local<v8::Object> new_object = v8::Object::New();
623 leaf->Set(v8::String::New(kKey), new_object); 655 leaf->Set(v8::String::New(kKey), new_object);
624 leaf = new_object; 656 leaf = new_object;
625 } 657 }
626 658
627 V8ValueConverterImpl converter; 659 V8ValueConverterImpl converter;
628 scoped_ptr<base::Value> value(converter.FromV8Value(deep_object, context_)); 660 scoped_ptr<base::Value> value(converter.FromV8Value(deep_object, context));
629 ASSERT_TRUE(value); 661 ASSERT_TRUE(value);
630 662
631 // Expected depth is kMaxRecursionDepth in v8_value_converter_impl.cc. 663 // Expected depth is kMaxRecursionDepth in v8_value_converter_impl.cc.
632 int kExpectedDepth = 10; 664 int kExpectedDepth = 10;
633 665
634 base::Value* current = value.get(); 666 base::Value* current = value.get();
635 for (int i = 1; i < kExpectedDepth; ++i) { 667 for (int i = 1; i < kExpectedDepth; ++i) {
636 base::DictionaryValue* current_as_object = NULL; 668 base::DictionaryValue* current_as_object = NULL;
637 ASSERT_TRUE(current->GetAsDictionary(&current_as_object)) << i; 669 ASSERT_TRUE(current->GetAsDictionary(&current_as_object)) << i;
638 ASSERT_TRUE(current_as_object->Get(kKey, &current)) << i; 670 ASSERT_TRUE(current_as_object->Get(kKey, &current)) << i;
639 } 671 }
640 672
641 // The leaf node shouldn't have any properties. 673 // The leaf node shouldn't have any properties.
642 base::DictionaryValue empty; 674 base::DictionaryValue empty;
643 EXPECT_TRUE(Value::Equals(&empty, current)) << *current; 675 EXPECT_TRUE(Value::Equals(&empty, current)) << *current;
644 } 676 }
645 677
646 } // namespace content 678 } // namespace content
OLDNEW
« no previous file with comments | « chrome/test/base/v8_unit_test.cc ('k') | webkit/plugins/ppapi/v8_var_converter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698