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

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/V8BindingTest.cpp

Issue 2840563002: bindings: Remove unused To*Array() and ToV8Sequence() functions. (Closed)
Patch Set: Rebase after #466709 Created 3 years, 7 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
« no previous file with comments | « third_party/WebKit/Source/bindings/core/v8/V8BindingForCore.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/V8BindingForCore.h" 5 #include "bindings/core/v8/V8BindingForCore.h"
6 6
7 #include "bindings/core/v8/ExceptionState.h" 7 #include "bindings/core/v8/ExceptionState.h"
8 #include "bindings/core/v8/IDLTypes.h"
9 #include "bindings/core/v8/NativeValueTraitsImpl.h"
10 #include "bindings/core/v8/ScriptValue.h"
8 #include "bindings/core/v8/ToV8ForCore.h" 11 #include "bindings/core/v8/ToV8ForCore.h"
9 #include "bindings/core/v8/V8BindingForTesting.h" 12 #include "bindings/core/v8/V8BindingForTesting.h"
10 #include "platform/wtf/Vector.h" 13 #include "platform/wtf/Vector.h"
11 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
12 15
13 namespace blink { 16 namespace blink {
14 17
15 namespace { 18 namespace {
16 19
17 template <typename T> 20 template <typename T>
18 v8::Local<v8::Value> ToV8(V8TestingScope* scope, T value) { 21 v8::Local<v8::Value> ToV8(V8TestingScope* scope, T value) {
19 return blink::ToV8(value, scope->GetContext()->Global(), scope->GetIsolate()); 22 return blink::ToV8(value, scope->GetContext()->Global(), scope->GetIsolate());
20 } 23 }
21 24
22 TEST(V8BindingTest, toImplSequence) { 25 TEST(V8BindingTest, ToImplArray) {
23 V8TestingScope scope; 26 V8TestingScope scope;
24 { 27 {
25 v8::Local<v8::Array> v8_string_array =
26 v8::Array::New(scope.GetIsolate(), 2);
27 v8_string_array
28 ->Set(scope.GetContext(), ToV8(&scope, 0),
29 ToV8(&scope, "Hello, World!"))
30 .ToChecked();
31 v8_string_array
32 ->Set(scope.GetContext(), ToV8(&scope, 1), ToV8(&scope, "Hi, Mom!"))
33 .ToChecked();
34
35 NonThrowableExceptionState exception_state;
36 Vector<String> string_vector = ToImplSequence<Vector<String>>(
37 scope.GetIsolate(), v8_string_array, exception_state);
38 EXPECT_EQ(2U, string_vector.size());
39 EXPECT_EQ("Hello, World!", string_vector[0]);
40 EXPECT_EQ("Hi, Mom!", string_vector[1]);
41 }
42 }
43
44 TEST(V8BindingTest, toImplArray) {
45 V8TestingScope scope;
46 {
47 v8::Local<v8::Array> v8_string_array =
48 v8::Array::New(scope.GetIsolate(), 2);
49 EXPECT_TRUE(V8CallBoolean(v8_string_array->Set(
50 scope.GetContext(), ToV8(&scope, 0), ToV8(&scope, "Hello, World!"))));
51 EXPECT_TRUE(V8CallBoolean(v8_string_array->Set(
52 scope.GetContext(), ToV8(&scope, 1), ToV8(&scope, "Hi, Mom!"))));
53
54 NonThrowableExceptionState exception_state;
55 Vector<String> string_vector = ToImplArray<Vector<String>>(
56 v8_string_array, 0, scope.GetIsolate(), exception_state);
57 EXPECT_EQ(2U, string_vector.size());
58 EXPECT_EQ("Hello, World!", string_vector[0]);
59 EXPECT_EQ("Hi, Mom!", string_vector[1]);
60 }
61 {
62 v8::Local<v8::Array> v8_unsigned_array =
63 v8::Array::New(scope.GetIsolate(), 3);
64 EXPECT_TRUE(V8CallBoolean(v8_unsigned_array->Set(
65 scope.GetContext(), ToV8(&scope, 0), ToV8(&scope, 42))));
66 EXPECT_TRUE(V8CallBoolean(v8_unsigned_array->Set(
67 scope.GetContext(), ToV8(&scope, 1), ToV8(&scope, 1729))));
68 EXPECT_TRUE(V8CallBoolean(v8_unsigned_array->Set(
69 scope.GetContext(), ToV8(&scope, 2), ToV8(&scope, 31773))));
70
71 NonThrowableExceptionState exception_state;
72 Vector<unsigned> unsigned_vector = ToImplArray<Vector<unsigned>>(
73 v8_unsigned_array, 0, scope.GetIsolate(), exception_state);
74 EXPECT_EQ(3U, unsigned_vector.size());
75 EXPECT_EQ(42U, unsigned_vector[0]);
76 EXPECT_EQ(1729U, unsigned_vector[1]);
77 EXPECT_EQ(31773U, unsigned_vector[2]);
78 }
79 {
80 const double kDoublePi = 3.141592653589793238;
81 const float kFloatPi = kDoublePi;
82 v8::Local<v8::Array> v8_real_array = v8::Array::New(scope.GetIsolate(), 1);
83 EXPECT_TRUE(V8CallBoolean(v8_real_array->Set(
84 scope.GetContext(), ToV8(&scope, 0), ToV8(&scope, kDoublePi))));
85
86 NonThrowableExceptionState exception_state;
87 Vector<double> double_vector = ToImplArray<Vector<double>>(
88 v8_real_array, 0, scope.GetIsolate(), exception_state);
89 EXPECT_EQ(1U, double_vector.size());
90 EXPECT_EQ(kDoublePi, double_vector[0]);
91
92 Vector<float> float_vector = ToImplArray<Vector<float>>(
93 v8_real_array, 0, scope.GetIsolate(), exception_state);
94 EXPECT_EQ(1U, float_vector.size());
95 EXPECT_EQ(kFloatPi, float_vector[0]);
96 }
97 {
98 v8::Local<v8::Array> v8_array = v8::Array::New(scope.GetIsolate(), 3); 28 v8::Local<v8::Array> v8_array = v8::Array::New(scope.GetIsolate(), 3);
99 EXPECT_TRUE( 29 EXPECT_TRUE(
100 V8CallBoolean(v8_array->Set(scope.GetContext(), ToV8(&scope, 0), 30 V8CallBoolean(v8_array->Set(scope.GetContext(), ToV8(&scope, 0),
101 ToV8(&scope, "Vini, vidi, vici.")))); 31 ToV8(&scope, "Vini, vidi, vici."))));
102 EXPECT_TRUE(V8CallBoolean(v8_array->Set(scope.GetContext(), ToV8(&scope, 1), 32 EXPECT_TRUE(V8CallBoolean(v8_array->Set(scope.GetContext(), ToV8(&scope, 1),
103 ToV8(&scope, 65535)))); 33 ToV8(&scope, 65535))));
104 EXPECT_TRUE(V8CallBoolean(v8_array->Set(scope.GetContext(), ToV8(&scope, 2), 34 EXPECT_TRUE(V8CallBoolean(v8_array->Set(scope.GetContext(), ToV8(&scope, 2),
105 ToV8(&scope, 0.125)))); 35 ToV8(&scope, 0.125))));
106 36
107 NonThrowableExceptionState exception_state; 37 NonThrowableExceptionState exception_state;
108 Vector<v8::Local<v8::Value>> v8_handle_vector = 38 Vector<ScriptValue> script_value_vector =
109 ToImplArray<Vector<v8::Local<v8::Value>>>( 39 NativeValueTraits<IDLSequence<ScriptValue>>::NativeValue(
110 v8_array, 0, scope.GetIsolate(), exception_state); 40 scope.GetIsolate(), v8_array, exception_state);
111 EXPECT_EQ(3U, v8_handle_vector.size()); 41 Vector<String> string_vector = ToImplArray<Vector<String>>(
112 EXPECT_EQ( 42 script_value_vector, scope.GetIsolate(), exception_state);
113 "Vini, vidi, vici.", 43 EXPECT_EQ(3U, string_vector.size());
114 ToUSVString(scope.GetIsolate(), v8_handle_vector[0], exception_state)); 44 EXPECT_EQ("Vini, vidi, vici.", string_vector[0]);
115 EXPECT_EQ(65535U, ToUInt32(scope.GetIsolate(), v8_handle_vector[1], 45 EXPECT_EQ("65535", string_vector[1]);
116 kNormalConversion, exception_state));
117
118 Vector<ScriptValue> script_value_vector = ToImplArray<Vector<ScriptValue>>(
119 v8_array, 0, scope.GetIsolate(), exception_state);
120 EXPECT_EQ(3U, script_value_vector.size());
121 String report_on_zela;
122 EXPECT_TRUE(script_value_vector[0].ToString(report_on_zela));
123 EXPECT_EQ("Vini, vidi, vici.", report_on_zela);
124 EXPECT_EQ(65535U,
125 ToUInt32(scope.GetIsolate(), script_value_vector[1].V8Value(),
126 kNormalConversion, exception_state));
127 }
128 {
129 v8::Local<v8::Array> v8_string_array1 =
130 v8::Array::New(scope.GetIsolate(), 2);
131 EXPECT_TRUE(V8CallBoolean(v8_string_array1->Set(
132 scope.GetContext(), ToV8(&scope, 0), ToV8(&scope, "foo"))));
133 EXPECT_TRUE(V8CallBoolean(v8_string_array1->Set(
134 scope.GetContext(), ToV8(&scope, 1), ToV8(&scope, "bar"))));
135 v8::Local<v8::Array> v8_string_array2 =
136 v8::Array::New(scope.GetIsolate(), 3);
137 EXPECT_TRUE(V8CallBoolean(v8_string_array2->Set(
138 scope.GetContext(), ToV8(&scope, 0), ToV8(&scope, "x"))));
139 EXPECT_TRUE(V8CallBoolean(v8_string_array2->Set(
140 scope.GetContext(), ToV8(&scope, 1), ToV8(&scope, "y"))));
141 EXPECT_TRUE(V8CallBoolean(v8_string_array2->Set(
142 scope.GetContext(), ToV8(&scope, 2), ToV8(&scope, "z"))));
143 v8::Local<v8::Array> v8_string_array_array =
144 v8::Array::New(scope.GetIsolate(), 2);
145 EXPECT_TRUE(V8CallBoolean(v8_string_array_array->Set(
146 scope.GetContext(), ToV8(&scope, 0), v8_string_array1)));
147 EXPECT_TRUE(V8CallBoolean(v8_string_array_array->Set(
148 scope.GetContext(), ToV8(&scope, 1), v8_string_array2)));
149
150 NonThrowableExceptionState exception_state;
151 Vector<Vector<String>> string_vector_vector =
152 ToImplArray<Vector<Vector<String>>>(
153 v8_string_array_array, 0, scope.GetIsolate(), exception_state);
154 EXPECT_EQ(2U, string_vector_vector.size());
155 EXPECT_EQ(2U, string_vector_vector[0].size());
156 EXPECT_EQ("foo", string_vector_vector[0][0]);
157 EXPECT_EQ("bar", string_vector_vector[0][1]);
158 EXPECT_EQ(3U, string_vector_vector[1].size());
159 EXPECT_EQ("x", string_vector_vector[1][0]);
160 EXPECT_EQ("y", string_vector_vector[1][1]);
161 EXPECT_EQ("z", string_vector_vector[1][2]);
162 } 46 }
163 } 47 }
164 48
165 } // namespace 49 } // namespace
166 50
167 } // namespace blink 51 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/bindings/core/v8/V8BindingForCore.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698