OLD | NEW |
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 "webkit/plugins/ppapi/v8_var_converter.h" | 5 #include "webkit/plugins/ppapi/v8_var_converter.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 } | 126 } |
127 | 127 |
128 bool Equals(const PP_Var& var, | 128 bool Equals(const PP_Var& var, |
129 v8::Handle<v8::Value> val) { | 129 v8::Handle<v8::Value> val) { |
130 VarHandleMap var_handle_map; | 130 VarHandleMap var_handle_map; |
131 return Equals(var, val, &var_handle_map); | 131 return Equals(var, val, &var_handle_map); |
132 } | 132 } |
133 | 133 |
134 class V8VarConverterTest : public testing::Test { | 134 class V8VarConverterTest : public testing::Test { |
135 public: | 135 public: |
136 V8VarConverterTest() {} | 136 V8VarConverterTest() |
| 137 : isolate_(v8::Isolate::GetCurrent()) {} |
137 ~V8VarConverterTest() {} | 138 ~V8VarConverterTest() {} |
138 | 139 |
139 // testing::Test implementation. | 140 // testing::Test implementation. |
140 virtual void SetUp() { | 141 virtual void SetUp() { |
141 ProxyLock::Acquire(); | 142 ProxyLock::Acquire(); |
142 v8::HandleScope handle_scope; | 143 v8::HandleScope handle_scope(isolate_); |
143 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); | 144 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
144 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 145 context_.Reset(isolate_, v8::Context::New(isolate_, NULL, global)); |
145 // TODO(marja): Use v8::Persistent::Reset here. | |
146 context_ = v8::Persistent<v8::Context>( | |
147 isolate, v8::Context::New(isolate, NULL, global)); | |
148 } | 146 } |
149 virtual void TearDown() { | 147 virtual void TearDown() { |
150 context_.Dispose(context_->GetIsolate()); | 148 context_.Dispose(); |
151 ASSERT_TRUE(PpapiGlobals::Get()->GetVarTracker()->GetLiveVars().empty()); | 149 ASSERT_TRUE(PpapiGlobals::Get()->GetVarTracker()->GetLiveVars().empty()); |
152 ProxyLock::Release(); | 150 ProxyLock::Release(); |
153 } | 151 } |
154 | 152 |
155 protected: | 153 protected: |
156 bool RoundTrip(const PP_Var& var, PP_Var* result) { | 154 bool RoundTrip(const PP_Var& var, PP_Var* result) { |
157 V8VarConverter converter; | 155 V8VarConverter converter; |
158 v8::Context::Scope context_scope(context_); | 156 v8::HandleScope handle_scope(isolate_); |
159 v8::HandleScope handle_scope; | 157 v8::Context::Scope context_scope(isolate_, context_); |
| 158 v8::Local<v8::Context> context = |
| 159 v8::Local<v8::Context>::New(isolate_, context_); |
160 v8::Handle<v8::Value> v8_result; | 160 v8::Handle<v8::Value> v8_result; |
161 if (!converter.ToV8Value(var, context_, &v8_result)) | 161 if (!converter.ToV8Value(var, context, &v8_result)) |
162 return false; | 162 return false; |
163 if (!Equals(var, v8_result)) | 163 if (!Equals(var, v8_result)) |
164 return false; | 164 return false; |
165 if (!converter.FromV8Value(v8_result, context_, result)) | 165 if (!converter.FromV8Value(v8_result, context, result)) |
166 return false; | 166 return false; |
167 return true; | 167 return true; |
168 } | 168 } |
169 | 169 |
170 // Assumes a ref for var. | 170 // Assumes a ref for var. |
171 bool RoundTripAndCompare(const PP_Var& var) { | 171 bool RoundTripAndCompare(const PP_Var& var) { |
172 ScopedPPVar expected(ScopedPPVar::PassRef(), var); | 172 ScopedPPVar expected(ScopedPPVar::PassRef(), var); |
173 PP_Var actual_var; | 173 PP_Var actual_var; |
174 if (!RoundTrip(expected.get(), &actual_var)) | 174 if (!RoundTrip(expected.get(), &actual_var)) |
175 return false; | 175 return false; |
176 ScopedPPVar actual(ScopedPPVar::PassRef(), actual_var); | 176 ScopedPPVar actual(ScopedPPVar::PassRef(), actual_var); |
177 return TestEqual(expected.get(), actual.get()); | 177 return TestEqual(expected.get(), actual.get()); |
178 } | 178 } |
179 | 179 |
| 180 v8::Isolate* isolate_; |
| 181 |
180 // Context for the JavaScript in the test. | 182 // Context for the JavaScript in the test. |
181 v8::Persistent<v8::Context> context_; | 183 v8::Persistent<v8::Context> context_; |
182 | 184 |
183 private: | 185 private: |
184 TestGlobals globals_; | 186 TestGlobals globals_; |
185 }; | 187 }; |
186 | 188 |
187 } // namespace | 189 } // namespace |
188 | 190 |
189 TEST_F(V8VarConverterTest, SimpleRoundTripTest) { | 191 TEST_F(V8VarConverterTest, SimpleRoundTripTest) { |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 { | 300 { |
299 // Test keys with '.'. | 301 // Test keys with '.'. |
300 scoped_refptr<DictionaryVar> dictionary(new DictionaryVar); | 302 scoped_refptr<DictionaryVar> dictionary(new DictionaryVar); |
301 dictionary->SetWithStringKey(".", PP_MakeUndefined()); | 303 dictionary->SetWithStringKey(".", PP_MakeUndefined()); |
302 dictionary->SetWithStringKey("x.y", PP_MakeUndefined()); | 304 dictionary->SetWithStringKey("x.y", PP_MakeUndefined()); |
303 EXPECT_TRUE(RoundTripAndCompare(dictionary->GetPPVar())); | 305 EXPECT_TRUE(RoundTripAndCompare(dictionary->GetPPVar())); |
304 } | 306 } |
305 | 307 |
306 { | 308 { |
307 // Test non-string key types. They should be cast to strings. | 309 // Test non-string key types. They should be cast to strings. |
308 v8::Context::Scope context_scope(context_); | 310 v8::HandleScope handle_scope(isolate_); |
309 v8::HandleScope handle_scope; | 311 v8::Context::Scope context_scope(isolate_, context_); |
310 | 312 |
311 const char* source = "(function() {" | 313 const char* source = "(function() {" |
312 "return {" | 314 "return {" |
313 "1: 'foo'," | 315 "1: 'foo'," |
314 "'2': 'bar'," | 316 "'2': 'bar'," |
315 "true: 'baz'," | 317 "true: 'baz'," |
316 "false: 'qux'," | 318 "false: 'qux'," |
317 "null: 'quux'," | 319 "null: 'quux'," |
318 "undefined: 'oops'" | 320 "undefined: 'oops'" |
319 "};" | 321 "};" |
320 "})();"; | 322 "})();"; |
321 | 323 |
322 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source))); | 324 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source))); |
323 v8::Handle<v8::Object> object = script->Run().As<v8::Object>(); | 325 v8::Handle<v8::Object> object = script->Run().As<v8::Object>(); |
324 ASSERT_FALSE(object.IsEmpty()); | 326 ASSERT_FALSE(object.IsEmpty()); |
325 | 327 |
326 V8VarConverter converter; | 328 V8VarConverter converter; |
327 PP_Var actual; | 329 PP_Var actual; |
328 ASSERT_TRUE(converter.FromV8Value(object, context_, &actual)); | 330 ASSERT_TRUE(converter.FromV8Value( |
| 331 object, v8::Local<v8::Context>::New(isolate_, context_), &actual)); |
329 ScopedPPVar release_actual(ScopedPPVar::PassRef(), actual); | 332 ScopedPPVar release_actual(ScopedPPVar::PassRef(), actual); |
330 | 333 |
331 scoped_refptr<DictionaryVar> expected(new DictionaryVar); | 334 scoped_refptr<DictionaryVar> expected(new DictionaryVar); |
332 ScopedPPVar foo(ScopedPPVar::PassRef(), StringVar::StringToPPVar("foo")); | 335 ScopedPPVar foo(ScopedPPVar::PassRef(), StringVar::StringToPPVar("foo")); |
333 expected->SetWithStringKey("1", foo.get()); | 336 expected->SetWithStringKey("1", foo.get()); |
334 ScopedPPVar bar(ScopedPPVar::PassRef(), StringVar::StringToPPVar("bar")); | 337 ScopedPPVar bar(ScopedPPVar::PassRef(), StringVar::StringToPPVar("bar")); |
335 expected->SetWithStringKey("2", bar.get()); | 338 expected->SetWithStringKey("2", bar.get()); |
336 ScopedPPVar baz(ScopedPPVar::PassRef(), StringVar::StringToPPVar("baz")); | 339 ScopedPPVar baz(ScopedPPVar::PassRef(), StringVar::StringToPPVar("baz")); |
337 expected->SetWithStringKey("true", baz.get()); | 340 expected->SetWithStringKey("true", baz.get()); |
338 ScopedPPVar qux(ScopedPPVar::PassRef(), StringVar::StringToPPVar("qux")); | 341 ScopedPPVar qux(ScopedPPVar::PassRef(), StringVar::StringToPPVar("qux")); |
339 expected->SetWithStringKey("false", qux.get()); | 342 expected->SetWithStringKey("false", qux.get()); |
340 ScopedPPVar quux(ScopedPPVar::PassRef(), StringVar::StringToPPVar("quux")); | 343 ScopedPPVar quux(ScopedPPVar::PassRef(), StringVar::StringToPPVar("quux")); |
341 expected->SetWithStringKey("null", quux.get()); | 344 expected->SetWithStringKey("null", quux.get()); |
342 ScopedPPVar oops(ScopedPPVar::PassRef(), StringVar::StringToPPVar("oops")); | 345 ScopedPPVar oops(ScopedPPVar::PassRef(), StringVar::StringToPPVar("oops")); |
343 expected->SetWithStringKey("undefined", oops.get()); | 346 expected->SetWithStringKey("undefined", oops.get()); |
344 ScopedPPVar release_expected( | 347 ScopedPPVar release_expected( |
345 ScopedPPVar::PassRef(), expected->GetPPVar()); | 348 ScopedPPVar::PassRef(), expected->GetPPVar()); |
346 | 349 |
347 ASSERT_TRUE(TestEqual(release_expected.get(), release_actual.get())); | 350 ASSERT_TRUE(TestEqual(release_expected.get(), release_actual.get())); |
348 } | 351 } |
349 } | 352 } |
350 | 353 |
351 } // namespace ppapi | 354 } // namespace ppapi |
352 } // namespace webkit | 355 } // namespace webkit |
OLD | NEW |