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