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.
5 #include "base/basictypes.h"
6 #include "base/float_util.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/stringprintf.h"
9 #include "content/common/android/gin_java_bridge_value.h"
10 #include "content/renderer/java/gin_java_bridge_value_converter.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "v8/include/v8.h"
16 class GinJavaBridgeValueConverterTest
: public testing::Test
{
18 GinJavaBridgeValueConverterTest()
19 : isolate_(v8::Isolate::GetCurrent()) {
23 void SetUp() override
{
24 v8::HandleScope
handle_scope(isolate_
);
25 v8::Handle
<v8::ObjectTemplate
> global
= v8::ObjectTemplate::New(isolate_
);
26 context_
.Reset(isolate_
, v8::Context::New(isolate_
, NULL
, global
));
29 void TearDown() override
{ context_
.Reset(); }
31 v8::Isolate
* isolate_
;
33 // Context for the JavaScript in the test.
34 v8::Persistent
<v8::Context
> context_
;
37 TEST_F(GinJavaBridgeValueConverterTest
, BasicValues
) {
38 v8::HandleScope
handle_scope(isolate_
);
39 v8::Local
<v8::Context
> context
=
40 v8::Local
<v8::Context
>::New(isolate_
, context_
);
41 v8::Context::Scope
context_scope(context
);
43 scoped_ptr
<GinJavaBridgeValueConverter
> converter(
44 new GinJavaBridgeValueConverter());
46 v8::Handle
<v8::Primitive
> v8_undefined(v8::Undefined(isolate_
));
47 scoped_ptr
<base::Value
> undefined(
48 converter
->FromV8Value(v8_undefined
, context
));
49 ASSERT_TRUE(undefined
.get());
50 EXPECT_TRUE(GinJavaBridgeValue::ContainsGinJavaBridgeValue(undefined
.get()));
51 scoped_ptr
<const GinJavaBridgeValue
> undefined_value(
52 GinJavaBridgeValue::FromValue(undefined
.get()));
53 ASSERT_TRUE(undefined_value
.get());
54 EXPECT_TRUE(undefined_value
->IsType(GinJavaBridgeValue::TYPE_UNDEFINED
));
56 v8::Handle
<v8::Number
> v8_infinity(
57 v8::Number::New(isolate_
, std::numeric_limits
<double>::infinity()));
58 scoped_ptr
<base::Value
> infinity(
59 converter
->FromV8Value(v8_infinity
, context
));
60 ASSERT_TRUE(infinity
.get());
62 GinJavaBridgeValue::ContainsGinJavaBridgeValue(infinity
.get()));
63 scoped_ptr
<const GinJavaBridgeValue
> infinity_value(
64 GinJavaBridgeValue::FromValue(infinity
.get()));
65 ASSERT_TRUE(infinity_value
.get());
68 infinity_value
->IsType(GinJavaBridgeValue::TYPE_NONFINITE
));
69 EXPECT_TRUE(infinity_value
->GetAsNonFinite(&native_float
));
70 EXPECT_FALSE(base::IsFinite(native_float
));
71 EXPECT_FALSE(base::IsNaN(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::Handle
<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::Handle
<v8::Script
> script(v8::Script::Compile(v8::String::NewFromUtf8(
121 source_template
, array_types
[i
], typed_array_type
).c_str())));
122 v8::Handle
<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