| 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 "bindings/core/v8/ToV8.h" | 5 #include "bindings/core/v8/ToV8.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/V8Binding.h" | 7 #include "bindings/core/v8/V8Binding.h" |
| 8 #include "bindings/core/v8/V8BindingForTesting.h" | 8 #include "bindings/core/v8/V8BindingForTesting.h" |
| 9 #include "core/testing/GarbageCollectedScriptWrappable.h" | 9 #include "core/testing/GarbageCollectedScriptWrappable.h" |
| 10 #include "platform/heap/Heap.h" | 10 #include "platform/heap/Heap.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "wtf/Vector.h" | 12 #include "wtf/Vector.h" |
| 13 #include <limits> | 13 #include <limits> |
| 14 | 14 |
| 15 #define TEST_TOV8(expected, value) \ | 15 #define TEST_TOV8(expected, value) \ |
| 16 testToV8(&scope, expected, value, __FILE__, __LINE__) | 16 testToV8(&scope, expected, value, __FILE__, __LINE__) |
| 17 | 17 |
| 18 namespace blink { | 18 namespace blink { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 template <typename T> | 22 template <typename T> |
| 23 void testToV8(V8TestingScope* scope, | 23 void testToV8(V8TestingScope* scope, |
| 24 const char* expected, | 24 const char* expected, |
| 25 T value, | 25 T value, |
| 26 const char* path, | 26 const char* path, |
| 27 int lineNumber) { | 27 int lineNumber) { |
| 28 v8::Local<v8::Value> actual = | 28 v8::Local<v8::Value> actual = |
| 29 toV8(value, scope->context()->Global(), scope->isolate()); | 29 ToV8(value, scope->context()->Global(), scope->isolate()); |
| 30 if (actual.IsEmpty()) { | 30 if (actual.IsEmpty()) { |
| 31 ADD_FAILURE_AT(path, lineNumber) << "toV8 returns an empty value."; | 31 ADD_FAILURE_AT(path, lineNumber) << "toV8 returns an empty value."; |
| 32 return; | 32 return; |
| 33 } | 33 } |
| 34 String actualString = | 34 String actualString = |
| 35 toCoreString(actual->ToString(scope->context()).ToLocalChecked()); | 35 toCoreString(actual->ToString(scope->context()).ToLocalChecked()); |
| 36 if (String(expected) != actualString) { | 36 if (String(expected) != actualString) { |
| 37 ADD_FAILURE_AT(path, lineNumber) | 37 ADD_FAILURE_AT(path, lineNumber) |
| 38 << "toV8 returns an incorrect value.\n Actual: " | 38 << "toV8 returns an incorrect value.\n Actual: " |
| 39 << actualString.utf8().data() << "\nExpected: " << expected; | 39 << actualString.utf8().data() << "\nExpected: " << expected; |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 } | 205 } |
| 206 | 206 |
| 207 TEST(ToV8Test, dictionaryVector) { | 207 TEST(ToV8Test, dictionaryVector) { |
| 208 V8TestingScope scope; | 208 V8TestingScope scope; |
| 209 Vector<std::pair<String, int>> dictionary; | 209 Vector<std::pair<String, int>> dictionary; |
| 210 dictionary.push_back(std::make_pair("one", 1)); | 210 dictionary.push_back(std::make_pair("one", 1)); |
| 211 dictionary.push_back(std::make_pair("two", 2)); | 211 dictionary.push_back(std::make_pair("two", 2)); |
| 212 TEST_TOV8("[object Object]", dictionary); | 212 TEST_TOV8("[object Object]", dictionary); |
| 213 v8::Local<v8::Context> context = scope.getScriptState()->context(); | 213 v8::Local<v8::Context> context = scope.getScriptState()->context(); |
| 214 v8::Local<v8::Object> result = | 214 v8::Local<v8::Object> result = |
| 215 toV8(dictionary, context->Global(), scope.isolate()) | 215 ToV8(dictionary, context->Global(), scope.isolate()) |
| 216 ->ToObject(context) | 216 ->ToObject(context) |
| 217 .ToLocalChecked(); | 217 .ToLocalChecked(); |
| 218 v8::Local<v8::Value> one = | 218 v8::Local<v8::Value> one = |
| 219 result->Get(context, v8String(scope.isolate(), "one")).ToLocalChecked(); | 219 result->Get(context, v8String(scope.isolate(), "one")).ToLocalChecked(); |
| 220 EXPECT_EQ(1, one->NumberValue(context).FromJust()); | 220 EXPECT_EQ(1, one->NumberValue(context).FromJust()); |
| 221 v8::Local<v8::Value> two = | 221 v8::Local<v8::Value> two = |
| 222 result->Get(context, v8String(scope.isolate(), "two")).ToLocalChecked(); | 222 result->Get(context, v8String(scope.isolate(), "two")).ToLocalChecked(); |
| 223 EXPECT_EQ(2, two->NumberValue(context).FromJust()); | 223 EXPECT_EQ(2, two->NumberValue(context).FromJust()); |
| 224 } | 224 } |
| 225 | 225 |
| 226 TEST(ToV8Test, heapVector) { | 226 TEST(ToV8Test, heapVector) { |
| 227 V8TestingScope scope; | 227 V8TestingScope scope; |
| 228 HeapVector<Member<GarbageCollectedScriptWrappable>> v; | 228 HeapVector<Member<GarbageCollectedScriptWrappable>> v; |
| 229 v.push_back(new GarbageCollectedScriptWrappable("hoge")); | 229 v.push_back(new GarbageCollectedScriptWrappable("hoge")); |
| 230 v.push_back(new GarbageCollectedScriptWrappable("fuga")); | 230 v.push_back(new GarbageCollectedScriptWrappable("fuga")); |
| 231 v.push_back(nullptr); | 231 v.push_back(nullptr); |
| 232 | 232 |
| 233 TEST_TOV8("hoge,fuga,", v); | 233 TEST_TOV8("hoge,fuga,", v); |
| 234 } | 234 } |
| 235 | 235 |
| 236 TEST(ToV8Test, withScriptState) { | 236 TEST(ToV8Test, withScriptState) { |
| 237 V8TestingScope scope; | 237 V8TestingScope scope; |
| 238 ScriptValue value(scope.getScriptState(), | 238 ScriptValue value(scope.getScriptState(), |
| 239 v8::Number::New(scope.isolate(), 1234.0)); | 239 v8::Number::New(scope.isolate(), 1234.0)); |
| 240 | 240 |
| 241 v8::Local<v8::Value> actual = toV8(value, scope.getScriptState()); | 241 v8::Local<v8::Value> actual = ToV8(value, scope.getScriptState()); |
| 242 EXPECT_FALSE(actual.IsEmpty()); | 242 EXPECT_FALSE(actual.IsEmpty()); |
| 243 | 243 |
| 244 double actualAsNumber = actual.As<v8::Number>()->Value(); | 244 double actualAsNumber = actual.As<v8::Number>()->Value(); |
| 245 EXPECT_EQ(1234.0, actualAsNumber); | 245 EXPECT_EQ(1234.0, actualAsNumber); |
| 246 } | 246 } |
| 247 | 247 |
| 248 } // namespace | 248 } // namespace |
| 249 | 249 |
| 250 } // namespace blink | 250 } // namespace blink |
| OLD | NEW |