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
3 // found in the LICENSE file.
7 #include "base/basictypes.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/stringprintf.h"
10 #include "content/common/android/gin_java_bridge_value.h"
11 #include "content/renderer/java/gin_java_bridge_value_converter.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "v8/include/v8.h"
17 class GinJavaBridgeValueConverterTest
: public testing::Test
{
19 GinJavaBridgeValueConverterTest()
20 : isolate_(v8::Isolate::GetCurrent()) {
24 void SetUp() override
{
25 v8::HandleScope
handle_scope(isolate_
);
26 v8::Local
<v8::ObjectTemplate
> global
= v8::ObjectTemplate::New(isolate_
);
27 context_
.Reset(isolate_
, v8::Context::New(isolate_
, NULL
, global
));
30 void TearDown() override
{ context_
.Reset(); }
32 v8::Isolate
* isolate_
;
34 // Context for the JavaScript in the test.
35 v8::Persistent
<v8::Context
> context_
;
38 TEST_F(GinJavaBridgeValueConverterTest
, BasicValues
) {
39 v8::HandleScope
handle_scope(isolate_
);
40 v8::Local
<v8::Context
> context
=
41 v8::Local
<v8::Context
>::New(isolate_
, context_
);
42 v8::Context::Scope
context_scope(context
);
44 scoped_ptr
<GinJavaBridgeValueConverter
> converter(
45 new GinJavaBridgeValueConverter());
47 v8::Local
<v8::Primitive
> v8_undefined(v8::Undefined(isolate_
));
48 scoped_ptr
<base::Value
> undefined(
49 converter
->FromV8Value(v8_undefined
, context
));
50 ASSERT_TRUE(undefined
.get());
51 EXPECT_TRUE(GinJavaBridgeValue::ContainsGinJavaBridgeValue(undefined
.get()));
52 scoped_ptr
<const GinJavaBridgeValue
> undefined_value(
53 GinJavaBridgeValue::FromValue(undefined
.get()));
54 ASSERT_TRUE(undefined_value
.get());
55 EXPECT_TRUE(undefined_value
->IsType(GinJavaBridgeValue::TYPE_UNDEFINED
));
57 v8::Local
<v8::Number
> v8_infinity(
58 v8::Number::New(isolate_
, std::numeric_limits
<double>::infinity()));
59 scoped_ptr
<base::Value
> infinity(
60 converter
->FromV8Value(v8_infinity
, context
));
61 ASSERT_TRUE(infinity
.get());
63 GinJavaBridgeValue::ContainsGinJavaBridgeValue(infinity
.get()));
64 scoped_ptr
<const GinJavaBridgeValue
> infinity_value(
65 GinJavaBridgeValue::FromValue(infinity
.get()));
66 ASSERT_TRUE(infinity_value
.get());
69 infinity_value
->IsType(GinJavaBridgeValue::TYPE_NONFINITE
));
70 EXPECT_TRUE(infinity_value
->GetAsNonFinite(&native_float
));
71 EXPECT_TRUE(std::isinf(native_float
));
74 TEST_F(GinJavaBridgeValueConverterTest
, ArrayBuffer
) {
75 v8::HandleScope
handle_scope(isolate_
);
76 v8::Local
<v8::Context
> context
=
77 v8::Local
<v8::Context
>::New(isolate_
, context_
);
78 v8::Context::Scope
context_scope(context
);
80 scoped_ptr
<GinJavaBridgeValueConverter
> converter(
81 new GinJavaBridgeValueConverter());
83 v8::Local
<v8::ArrayBuffer
> v8_array_buffer(
84 v8::ArrayBuffer::New(isolate_
, 0));
85 scoped_ptr
<base::Value
> undefined(
86 converter
->FromV8Value(v8_array_buffer
, context
));
87 ASSERT_TRUE(undefined
.get());
88 EXPECT_TRUE(GinJavaBridgeValue::ContainsGinJavaBridgeValue(undefined
.get()));
89 scoped_ptr
<const GinJavaBridgeValue
> undefined_value(
90 GinJavaBridgeValue::FromValue(undefined
.get()));
91 ASSERT_TRUE(undefined_value
.get());
92 EXPECT_TRUE(undefined_value
->IsType(GinJavaBridgeValue::TYPE_UNDEFINED
));
95 TEST_F(GinJavaBridgeValueConverterTest
, TypedArrays
) {
96 v8::HandleScope
handle_scope(isolate_
);
97 v8::Local
<v8::Context
> context
=
98 v8::Local
<v8::Context
>::New(isolate_
, context_
);
99 v8::Context::Scope
context_scope(context
);
101 scoped_ptr
<GinJavaBridgeValueConverter
> converter(
102 new GinJavaBridgeValueConverter());
104 const char* source_template
= "(function() {"
105 "var array_buffer = new ArrayBuffer(%s);"
106 "var array_view = new %s(array_buffer);"
107 "array_view[0] = 42;"
110 const char* array_types
[] = {
111 "1", "Int8Array", "1", "Uint8Array", "1", "Uint8ClampedArray",
112 "2", "Int16Array", "2", "Uint16Array",
113 "4", "Int32Array", "4", "Uint32Array",
114 "4", "Float32Array", "8", "Float64Array"
116 for (size_t i
= 0; i
< arraysize(array_types
); i
+= 2) {
117 const char* typed_array_type
= array_types
[i
+ 1];
118 v8::Local
<v8::Script
> script(v8::Script::Compile(v8::String::NewFromUtf8(
121 source_template
, array_types
[i
], typed_array_type
).c_str())));
122 v8::Local
<v8::Value
> v8_typed_array
= script
->Run();
123 scoped_ptr
<base::Value
> list_value(
124 converter
->FromV8Value(v8_typed_array
, context
));
125 ASSERT_TRUE(list_value
.get()) << typed_array_type
;
126 EXPECT_TRUE(list_value
->IsType(base::Value::TYPE_LIST
)) << typed_array_type
;
127 base::ListValue
* list
;
128 ASSERT_TRUE(list_value
->GetAsList(&list
)) << typed_array_type
;
129 EXPECT_EQ(1u, list
->GetSize()) << typed_array_type
;
130 double first_element
;
131 ASSERT_TRUE(list
->GetDouble(0, &first_element
)) << typed_array_type
;
132 EXPECT_EQ(42.0, first_element
) << typed_array_type
;
136 } // namespace content