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 "content/renderer/java/gin_java_bridge_value_converter.h" |
| 6 |
5 #include <stddef.h> | 7 #include <stddef.h> |
6 | 8 |
7 #include <cmath> | 9 #include <cmath> |
| 10 #include <memory> |
8 | 11 |
9 #include "base/macros.h" | 12 #include "base/macros.h" |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
12 #include "content/common/android/gin_java_bridge_value.h" | 14 #include "content/common/android/gin_java_bridge_value.h" |
13 #include "content/renderer/java/gin_java_bridge_value_converter.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "v8/include/v8.h" | 16 #include "v8/include/v8.h" |
16 | 17 |
17 namespace content { | 18 namespace content { |
18 | 19 |
19 class GinJavaBridgeValueConverterTest : public testing::Test { | 20 class GinJavaBridgeValueConverterTest : public testing::Test { |
20 public: | 21 public: |
21 GinJavaBridgeValueConverterTest() | 22 GinJavaBridgeValueConverterTest() |
22 : isolate_(v8::Isolate::GetCurrent()) { | 23 : isolate_(v8::Isolate::GetCurrent()) { |
23 } | 24 } |
(...skipping 12 matching lines...) Expand all Loading... |
36 // Context for the JavaScript in the test. | 37 // Context for the JavaScript in the test. |
37 v8::Persistent<v8::Context> context_; | 38 v8::Persistent<v8::Context> context_; |
38 }; | 39 }; |
39 | 40 |
40 TEST_F(GinJavaBridgeValueConverterTest, BasicValues) { | 41 TEST_F(GinJavaBridgeValueConverterTest, BasicValues) { |
41 v8::HandleScope handle_scope(isolate_); | 42 v8::HandleScope handle_scope(isolate_); |
42 v8::Local<v8::Context> context = | 43 v8::Local<v8::Context> context = |
43 v8::Local<v8::Context>::New(isolate_, context_); | 44 v8::Local<v8::Context>::New(isolate_, context_); |
44 v8::Context::Scope context_scope(context); | 45 v8::Context::Scope context_scope(context); |
45 | 46 |
46 scoped_ptr<GinJavaBridgeValueConverter> converter( | 47 std::unique_ptr<GinJavaBridgeValueConverter> converter( |
47 new GinJavaBridgeValueConverter()); | 48 new GinJavaBridgeValueConverter()); |
48 | 49 |
49 v8::Local<v8::Primitive> v8_undefined(v8::Undefined(isolate_)); | 50 v8::Local<v8::Primitive> v8_undefined(v8::Undefined(isolate_)); |
50 scoped_ptr<base::Value> undefined( | 51 std::unique_ptr<base::Value> undefined( |
51 converter->FromV8Value(v8_undefined, context)); | 52 converter->FromV8Value(v8_undefined, context)); |
52 ASSERT_TRUE(undefined.get()); | 53 ASSERT_TRUE(undefined.get()); |
53 EXPECT_TRUE(GinJavaBridgeValue::ContainsGinJavaBridgeValue(undefined.get())); | 54 EXPECT_TRUE(GinJavaBridgeValue::ContainsGinJavaBridgeValue(undefined.get())); |
54 scoped_ptr<const GinJavaBridgeValue> undefined_value( | 55 std::unique_ptr<const GinJavaBridgeValue> undefined_value( |
55 GinJavaBridgeValue::FromValue(undefined.get())); | 56 GinJavaBridgeValue::FromValue(undefined.get())); |
56 ASSERT_TRUE(undefined_value.get()); | 57 ASSERT_TRUE(undefined_value.get()); |
57 EXPECT_TRUE(undefined_value->IsType(GinJavaBridgeValue::TYPE_UNDEFINED)); | 58 EXPECT_TRUE(undefined_value->IsType(GinJavaBridgeValue::TYPE_UNDEFINED)); |
58 | 59 |
59 v8::Local<v8::Number> v8_infinity( | 60 v8::Local<v8::Number> v8_infinity( |
60 v8::Number::New(isolate_, std::numeric_limits<double>::infinity())); | 61 v8::Number::New(isolate_, std::numeric_limits<double>::infinity())); |
61 scoped_ptr<base::Value> infinity( | 62 std::unique_ptr<base::Value> infinity( |
62 converter->FromV8Value(v8_infinity, context)); | 63 converter->FromV8Value(v8_infinity, context)); |
63 ASSERT_TRUE(infinity.get()); | 64 ASSERT_TRUE(infinity.get()); |
64 EXPECT_TRUE( | 65 EXPECT_TRUE( |
65 GinJavaBridgeValue::ContainsGinJavaBridgeValue(infinity.get())); | 66 GinJavaBridgeValue::ContainsGinJavaBridgeValue(infinity.get())); |
66 scoped_ptr<const GinJavaBridgeValue> infinity_value( | 67 std::unique_ptr<const GinJavaBridgeValue> infinity_value( |
67 GinJavaBridgeValue::FromValue(infinity.get())); | 68 GinJavaBridgeValue::FromValue(infinity.get())); |
68 ASSERT_TRUE(infinity_value.get()); | 69 ASSERT_TRUE(infinity_value.get()); |
69 float native_float; | 70 float native_float; |
70 EXPECT_TRUE( | 71 EXPECT_TRUE( |
71 infinity_value->IsType(GinJavaBridgeValue::TYPE_NONFINITE)); | 72 infinity_value->IsType(GinJavaBridgeValue::TYPE_NONFINITE)); |
72 EXPECT_TRUE(infinity_value->GetAsNonFinite(&native_float)); | 73 EXPECT_TRUE(infinity_value->GetAsNonFinite(&native_float)); |
73 EXPECT_TRUE(std::isinf(native_float)); | 74 EXPECT_TRUE(std::isinf(native_float)); |
74 } | 75 } |
75 | 76 |
76 TEST_F(GinJavaBridgeValueConverterTest, ArrayBuffer) { | 77 TEST_F(GinJavaBridgeValueConverterTest, ArrayBuffer) { |
77 v8::HandleScope handle_scope(isolate_); | 78 v8::HandleScope handle_scope(isolate_); |
78 v8::Local<v8::Context> context = | 79 v8::Local<v8::Context> context = |
79 v8::Local<v8::Context>::New(isolate_, context_); | 80 v8::Local<v8::Context>::New(isolate_, context_); |
80 v8::Context::Scope context_scope(context); | 81 v8::Context::Scope context_scope(context); |
81 | 82 |
82 scoped_ptr<GinJavaBridgeValueConverter> converter( | 83 std::unique_ptr<GinJavaBridgeValueConverter> converter( |
83 new GinJavaBridgeValueConverter()); | 84 new GinJavaBridgeValueConverter()); |
84 | 85 |
85 v8::Local<v8::ArrayBuffer> v8_array_buffer( | 86 v8::Local<v8::ArrayBuffer> v8_array_buffer( |
86 v8::ArrayBuffer::New(isolate_, 0)); | 87 v8::ArrayBuffer::New(isolate_, 0)); |
87 scoped_ptr<base::Value> undefined( | 88 std::unique_ptr<base::Value> undefined( |
88 converter->FromV8Value(v8_array_buffer, context)); | 89 converter->FromV8Value(v8_array_buffer, context)); |
89 ASSERT_TRUE(undefined.get()); | 90 ASSERT_TRUE(undefined.get()); |
90 EXPECT_TRUE(GinJavaBridgeValue::ContainsGinJavaBridgeValue(undefined.get())); | 91 EXPECT_TRUE(GinJavaBridgeValue::ContainsGinJavaBridgeValue(undefined.get())); |
91 scoped_ptr<const GinJavaBridgeValue> undefined_value( | 92 std::unique_ptr<const GinJavaBridgeValue> undefined_value( |
92 GinJavaBridgeValue::FromValue(undefined.get())); | 93 GinJavaBridgeValue::FromValue(undefined.get())); |
93 ASSERT_TRUE(undefined_value.get()); | 94 ASSERT_TRUE(undefined_value.get()); |
94 EXPECT_TRUE(undefined_value->IsType(GinJavaBridgeValue::TYPE_UNDEFINED)); | 95 EXPECT_TRUE(undefined_value->IsType(GinJavaBridgeValue::TYPE_UNDEFINED)); |
95 } | 96 } |
96 | 97 |
97 TEST_F(GinJavaBridgeValueConverterTest, TypedArrays) { | 98 TEST_F(GinJavaBridgeValueConverterTest, TypedArrays) { |
98 v8::HandleScope handle_scope(isolate_); | 99 v8::HandleScope handle_scope(isolate_); |
99 v8::Local<v8::Context> context = | 100 v8::Local<v8::Context> context = |
100 v8::Local<v8::Context>::New(isolate_, context_); | 101 v8::Local<v8::Context>::New(isolate_, context_); |
101 v8::Context::Scope context_scope(context); | 102 v8::Context::Scope context_scope(context); |
102 v8::MicrotasksScope microtasks_scope( | 103 v8::MicrotasksScope microtasks_scope( |
103 isolate_, v8::MicrotasksScope::kDoNotRunMicrotasks); | 104 isolate_, v8::MicrotasksScope::kDoNotRunMicrotasks); |
104 | 105 |
105 scoped_ptr<GinJavaBridgeValueConverter> converter( | 106 std::unique_ptr<GinJavaBridgeValueConverter> converter( |
106 new GinJavaBridgeValueConverter()); | 107 new GinJavaBridgeValueConverter()); |
107 | 108 |
108 const char* source_template = "(function() {" | 109 const char* source_template = "(function() {" |
109 "var array_buffer = new ArrayBuffer(%s);" | 110 "var array_buffer = new ArrayBuffer(%s);" |
110 "var array_view = new %s(array_buffer);" | 111 "var array_view = new %s(array_buffer);" |
111 "array_view[0] = 42;" | 112 "array_view[0] = 42;" |
112 "return array_view;" | 113 "return array_view;" |
113 "})();"; | 114 "})();"; |
114 const char* array_types[] = { | 115 const char* array_types[] = { |
115 "1", "Int8Array", "1", "Uint8Array", "1", "Uint8ClampedArray", | 116 "1", "Int8Array", "1", "Uint8Array", "1", "Uint8ClampedArray", |
116 "2", "Int16Array", "2", "Uint16Array", | 117 "2", "Int16Array", "2", "Uint16Array", |
117 "4", "Int32Array", "4", "Uint32Array", | 118 "4", "Int32Array", "4", "Uint32Array", |
118 "4", "Float32Array", "8", "Float64Array" | 119 "4", "Float32Array", "8", "Float64Array" |
119 }; | 120 }; |
120 for (size_t i = 0; i < arraysize(array_types); i += 2) { | 121 for (size_t i = 0; i < arraysize(array_types); i += 2) { |
121 const char* typed_array_type = array_types[i + 1]; | 122 const char* typed_array_type = array_types[i + 1]; |
122 v8::Local<v8::Script> script(v8::Script::Compile(v8::String::NewFromUtf8( | 123 v8::Local<v8::Script> script(v8::Script::Compile(v8::String::NewFromUtf8( |
123 isolate_, | 124 isolate_, |
124 base::StringPrintf( | 125 base::StringPrintf( |
125 source_template, array_types[i], typed_array_type).c_str()))); | 126 source_template, array_types[i], typed_array_type).c_str()))); |
126 v8::Local<v8::Value> v8_typed_array = script->Run(); | 127 v8::Local<v8::Value> v8_typed_array = script->Run(); |
127 scoped_ptr<base::Value> list_value( | 128 std::unique_ptr<base::Value> list_value( |
128 converter->FromV8Value(v8_typed_array, context)); | 129 converter->FromV8Value(v8_typed_array, context)); |
129 ASSERT_TRUE(list_value.get()) << typed_array_type; | 130 ASSERT_TRUE(list_value.get()) << typed_array_type; |
130 EXPECT_TRUE(list_value->IsType(base::Value::TYPE_LIST)) << typed_array_type; | 131 EXPECT_TRUE(list_value->IsType(base::Value::TYPE_LIST)) << typed_array_type; |
131 base::ListValue* list; | 132 base::ListValue* list; |
132 ASSERT_TRUE(list_value->GetAsList(&list)) << typed_array_type; | 133 ASSERT_TRUE(list_value->GetAsList(&list)) << typed_array_type; |
133 EXPECT_EQ(1u, list->GetSize()) << typed_array_type; | 134 EXPECT_EQ(1u, list->GetSize()) << typed_array_type; |
134 double first_element; | 135 double first_element; |
135 ASSERT_TRUE(list->GetDouble(0, &first_element)) << typed_array_type; | 136 ASSERT_TRUE(list->GetDouble(0, &first_element)) << typed_array_type; |
136 EXPECT_EQ(42.0, first_element) << typed_array_type; | 137 EXPECT_EQ(42.0, first_element) << typed_array_type; |
137 } | 138 } |
138 } | 139 } |
139 | 140 |
140 } // namespace content | 141 } // namespace content |
OLD | NEW |