OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "config.h" | 5 #include "config.h" |
6 #include "bindings/core/v8/V8Binding.h" | 6 #include "bindings/core/v8/V8Binding.h" |
7 | 7 |
| 8 #include "bindings/core/v8/ExceptionState.h" |
8 #include "core/testing/GarbageCollectedScriptWrappable.h" | 9 #include "core/testing/GarbageCollectedScriptWrappable.h" |
9 #include "core/testing/RefCountedScriptWrappable.h" | 10 #include "core/testing/RefCountedScriptWrappable.h" |
10 #include "platform/heap/Heap.h" | 11 #include "platform/heap/Heap.h" |
11 #include "wtf/Vector.h" | 12 #include "wtf/Vector.h" |
12 | 13 |
13 #include <gtest/gtest.h> | 14 #include <gtest/gtest.h> |
14 #include <v8.h> | 15 #include <v8.h> |
15 | 16 |
16 #define CHECK_TOV8VALUE(expected, value) check(expected, value, __FILE__, __LINE
__) | 17 #define CHECK_TOV8VALUE(expected, value) check(expected, value, __FILE__, __LINE
__) |
17 | 18 |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 | 291 |
291 TEST_F(V8ValueTraitsTest, v8Value) | 292 TEST_F(V8ValueTraitsTest, v8Value) |
292 { | 293 { |
293 v8::Local<v8::Value> localValue(v8::Number::New(m_scope.isolate(), 1234)); | 294 v8::Local<v8::Value> localValue(v8::Number::New(m_scope.isolate(), 1234)); |
294 v8::Handle<v8::Value> handleValue(v8::Number::New(m_scope.isolate(), 5678)); | 295 v8::Handle<v8::Value> handleValue(v8::Number::New(m_scope.isolate(), 5678)); |
295 | 296 |
296 CHECK_TOV8VALUE("1234", localValue); | 297 CHECK_TOV8VALUE("1234", localValue); |
297 CHECK_TOV8VALUE("5678", handleValue); | 298 CHECK_TOV8VALUE("5678", handleValue); |
298 } | 299 } |
299 | 300 |
| 301 TEST_F(V8ValueTraitsTest, toNativeArray) |
| 302 { |
| 303 { |
| 304 v8::Handle<v8::Array> v8StringArray = v8::Array::New(m_scope.isolate(),
2); |
| 305 v8StringArray->Set(toV8Value(0), toV8Value("Hello, World!")); |
| 306 v8StringArray->Set(toV8Value(1), toV8Value("Hi, Mom!")); |
| 307 |
| 308 Vector<String> stringVector = toNativeArray<String>(v8StringArray, 0, m_
scope.isolate()); |
| 309 EXPECT_EQ(2U, stringVector.size()); |
| 310 EXPECT_EQ("Hello, World!", stringVector[0]); |
| 311 EXPECT_EQ("Hi, Mom!", stringVector[1]); |
| 312 } |
| 313 { |
| 314 v8::Handle<v8::Array> v8UnsignedArray = v8::Array::New(m_scope.isolate()
, 3); |
| 315 v8UnsignedArray->Set(toV8Value(0), toV8Value(42)); |
| 316 v8UnsignedArray->Set(toV8Value(1), toV8Value(1729)); |
| 317 v8UnsignedArray->Set(toV8Value(2), toV8Value(31773)); |
| 318 |
| 319 Vector<unsigned> unsignedVector = toNativeArray<unsigned>(v8UnsignedArra
y, 0, m_scope.isolate()); |
| 320 EXPECT_EQ(3U, unsignedVector.size()); |
| 321 EXPECT_EQ(42U, unsignedVector[0]); |
| 322 EXPECT_EQ(1729U, unsignedVector[1]); |
| 323 EXPECT_EQ(31773U, unsignedVector[2]); |
| 324 } |
| 325 { |
| 326 const double doublePi = 3.141592653589793238; |
| 327 const float floatPi = doublePi; |
| 328 v8::Handle<v8::Array> v8RealArray = v8::Array::New(m_scope.isolate(), 1)
; |
| 329 v8RealArray->Set(toV8Value(0), toV8Value(doublePi)); |
| 330 |
| 331 Vector<double> doubleVector = toNativeArray<double>(v8RealArray, 0, m_sc
ope.isolate()); |
| 332 EXPECT_EQ(1U, doubleVector.size()); |
| 333 EXPECT_EQ(doublePi, doubleVector[0]); |
| 334 |
| 335 Vector<float> floatVector = toNativeArray<float>(v8RealArray, 0, m_scope
.isolate()); |
| 336 EXPECT_EQ(1U, floatVector.size()); |
| 337 EXPECT_EQ(floatPi, floatVector[0]); |
| 338 } |
| 339 { |
| 340 v8::Handle<v8::Array> v8Array = v8::Array::New(m_scope.isolate(), 3); |
| 341 v8Array->Set(toV8Value(0), toV8Value("Vini, vidi, vici.")); |
| 342 v8Array->Set(toV8Value(1), toV8Value(65535)); |
| 343 v8Array->Set(toV8Value(2), toV8Value(0.125)); |
| 344 |
| 345 Vector<v8::Handle<v8::Value> > v8HandleVector = toNativeArray<v8::Handle
<v8::Value> >(v8Array, 0, m_scope.isolate()); |
| 346 EXPECT_EQ(3U, v8HandleVector.size()); |
| 347 NonThrowableExceptionState exceptionState; |
| 348 EXPECT_EQ("Vini, vidi, vici.", toScalarValueString(v8HandleVector[0], ex
ceptionState)); |
| 349 EXPECT_EQ(65535U, toUInt32(v8HandleVector[1])); |
| 350 EXPECT_EQ(0.125, toFloat(v8HandleVector[2])); |
| 351 |
| 352 Vector<ScriptValue> scriptValueVector = toNativeArray<ScriptValue>(v8Arr
ay, 0, m_scope.isolate()); |
| 353 EXPECT_EQ(3U, scriptValueVector.size()); |
| 354 String reportOnZela; |
| 355 EXPECT_TRUE(scriptValueVector[0].toString(reportOnZela)); |
| 356 EXPECT_EQ("Vini, vidi, vici.", reportOnZela); |
| 357 EXPECT_EQ(65535U, toUInt32(scriptValueVector[1].v8Value())); |
| 358 EXPECT_EQ(0.125, toFloat(scriptValueVector[2].v8Value())); |
| 359 } |
| 360 } |
| 361 |
300 } // namespace | 362 } // namespace |
301 | 363 |
302 } // namespace blink | 364 } // namespace blink |
OLD | NEW |