converter_unittest.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // Copyright 2013 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.
  4. #include "gin/converter.h"
  5. #include <limits.h>
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <string>
  9. #include "base/compiler_specific.h"
  10. #include "base/strings/utf_string_conversions.h"
  11. #include "gin/function_template.h"
  12. #include "gin/handle.h"
  13. #include "gin/public/isolate_holder.h"
  14. #include "gin/test/v8_test.h"
  15. #include "gin/wrappable.h"
  16. #include "testing/gmock/include/gmock/gmock.h"
  17. #include "testing/gtest/include/gtest/gtest.h"
  18. #include "v8/include/v8-container.h"
  19. #include "v8/include/v8-forward.h"
  20. #include "v8/include/v8-function.h"
  21. #include "v8/include/v8-isolate.h"
  22. #include "v8/include/v8-primitive.h"
  23. #include "v8/include/v8-template.h"
  24. namespace gin {
  25. using v8::Array;
  26. using v8::Boolean;
  27. using v8::HandleScope;
  28. using v8::Integer;
  29. using v8::Local;
  30. using v8::Null;
  31. using v8::Number;
  32. using v8::Object;
  33. using v8::String;
  34. using v8::Undefined;
  35. using v8::Value;
  36. typedef V8Test ConverterTest;
  37. TEST_F(ConverterTest, Bool) {
  38. HandleScope handle_scope(instance_->isolate());
  39. EXPECT_TRUE(Converter<bool>::ToV8(instance_->isolate(), true)->StrictEquals(
  40. Boolean::New(instance_->isolate(), true)));
  41. EXPECT_TRUE(Converter<bool>::ToV8(instance_->isolate(), false)->StrictEquals(
  42. Boolean::New(instance_->isolate(), false)));
  43. struct {
  44. Local<Value> input;
  45. bool expected;
  46. } test_data[] = {
  47. {Boolean::New(instance_->isolate(), false).As<Value>(), false},
  48. {Boolean::New(instance_->isolate(), true).As<Value>(), true},
  49. {Number::New(instance_->isolate(), 0).As<Value>(), false},
  50. {Number::New(instance_->isolate(), 1).As<Value>(), true},
  51. {Number::New(instance_->isolate(), -1).As<Value>(), true},
  52. {Number::New(instance_->isolate(), 0.1).As<Value>(), true},
  53. {String::NewFromUtf8(instance_->isolate(), "", v8::NewStringType::kNormal)
  54. .ToLocalChecked()
  55. .As<Value>(),
  56. false},
  57. {String::NewFromUtf8(instance_->isolate(), "foo",
  58. v8::NewStringType::kNormal)
  59. .ToLocalChecked()
  60. .As<Value>(),
  61. true},
  62. {Object::New(instance_->isolate()).As<Value>(), true},
  63. {Null(instance_->isolate()).As<Value>(), false},
  64. {Undefined(instance_->isolate()).As<Value>(), false},
  65. };
  66. for (size_t i = 0; i < std::size(test_data); ++i) {
  67. bool result = false;
  68. EXPECT_TRUE(Converter<bool>::FromV8(instance_->isolate(),
  69. test_data[i].input, &result));
  70. EXPECT_EQ(test_data[i].expected, result);
  71. result = true;
  72. EXPECT_TRUE(Converter<bool>::FromV8(instance_->isolate(),
  73. test_data[i].input, &result));
  74. EXPECT_EQ(test_data[i].expected, result);
  75. }
  76. }
  77. TEST_F(ConverterTest, String16) {
  78. v8::Isolate* isolate = instance_->isolate();
  79. HandleScope handle_scope(isolate);
  80. EXPECT_TRUE(Converter<std::u16string>::ToV8(isolate, u"")
  81. ->StrictEquals(StringToV8(isolate, "")));
  82. EXPECT_TRUE(Converter<std::u16string>::ToV8(isolate, u"hello")
  83. ->StrictEquals(StringToV8(isolate, "hello")));
  84. std::u16string result;
  85. ASSERT_FALSE(
  86. Converter<std::u16string>::FromV8(isolate, v8::False(isolate), &result));
  87. ASSERT_FALSE(
  88. Converter<std::u16string>::FromV8(isolate, v8::True(isolate), &result));
  89. ASSERT_TRUE(Converter<std::u16string>::FromV8(
  90. isolate, v8::String::Empty(isolate), &result));
  91. EXPECT_EQ(result, std::u16string());
  92. ASSERT_TRUE(Converter<std::u16string>::FromV8(
  93. isolate, StringToV8(isolate, "hello"), &result));
  94. EXPECT_EQ(result, u"hello");
  95. }
  96. TEST_F(ConverterTest, Int32) {
  97. HandleScope handle_scope(instance_->isolate());
  98. int test_data_to[] = {-1, 0, 1};
  99. for (size_t i = 0; i < std::size(test_data_to); ++i) {
  100. EXPECT_TRUE(Converter<int32_t>::ToV8(instance_->isolate(), test_data_to[i])
  101. ->StrictEquals(
  102. Integer::New(instance_->isolate(), test_data_to[i])));
  103. }
  104. struct {
  105. v8::Local<v8::Value> input;
  106. bool expect_success;
  107. int expected_result;
  108. } test_data_from[] = {
  109. {Boolean::New(instance_->isolate(), false).As<Value>(), false, 0},
  110. {Boolean::New(instance_->isolate(), true).As<Value>(), false, 0},
  111. {Integer::New(instance_->isolate(), -1).As<Value>(), true, -1},
  112. {Integer::New(instance_->isolate(), 0).As<Value>(), true, 0},
  113. {Integer::New(instance_->isolate(), 1).As<Value>(), true, 1},
  114. {Number::New(instance_->isolate(), -1).As<Value>(), true, -1},
  115. {Number::New(instance_->isolate(), 1.1).As<Value>(), false, 0},
  116. {String::NewFromUtf8(instance_->isolate(), "42",
  117. v8::NewStringType::kNormal)
  118. .ToLocalChecked()
  119. .As<Value>(),
  120. false, 0},
  121. {String::NewFromUtf8(instance_->isolate(), "foo",
  122. v8::NewStringType::kNormal)
  123. .ToLocalChecked()
  124. .As<Value>(),
  125. false, 0},
  126. {Object::New(instance_->isolate()).As<Value>(), false, 0},
  127. {Array::New(instance_->isolate()).As<Value>(), false, 0},
  128. {v8::Null(instance_->isolate()).As<Value>(), false, 0},
  129. {v8::Undefined(instance_->isolate()).As<Value>(), false, 0},
  130. };
  131. for (size_t i = 0; i < std::size(test_data_from); ++i) {
  132. int32_t result = std::numeric_limits<int32_t>::min();
  133. bool success = Converter<int32_t>::FromV8(instance_->isolate(),
  134. test_data_from[i].input, &result);
  135. EXPECT_EQ(test_data_from[i].expect_success, success) << i;
  136. if (success)
  137. EXPECT_EQ(test_data_from[i].expected_result, result) << i;
  138. }
  139. }
  140. TEST_F(ConverterTest, Vector) {
  141. HandleScope handle_scope(instance_->isolate());
  142. std::vector<int> expected;
  143. expected.push_back(-1);
  144. expected.push_back(0);
  145. expected.push_back(1);
  146. auto js_array =
  147. Converter<std::vector<int>>::ToV8(instance_->isolate(), expected)
  148. .As<Array>();
  149. EXPECT_EQ(3u, js_array->Length());
  150. v8::Local<v8::Context> context = instance_->isolate()->GetCurrentContext();
  151. for (size_t i = 0; i < expected.size(); ++i) {
  152. EXPECT_TRUE(
  153. Integer::New(instance_->isolate(), expected[i])
  154. ->StrictEquals(
  155. js_array->Get(context, static_cast<int>(i)).ToLocalChecked()));
  156. }
  157. }
  158. TEST_F(ConverterTest, VectorOfVectors) {
  159. HandleScope handle_scope(instance_->isolate());
  160. std::vector<std::vector<int>> vector_of_vectors = {
  161. {1, 2, 3}, {4, 5, 6},
  162. };
  163. v8::Local<v8::Value> v8_value =
  164. ConvertToV8(instance_->isolate(), vector_of_vectors);
  165. std::vector<std::vector<int>> out_value;
  166. ASSERT_TRUE(ConvertFromV8(instance_->isolate(), v8_value, &out_value));
  167. EXPECT_THAT(out_value, testing::ContainerEq(vector_of_vectors));
  168. }
  169. namespace {
  170. class MyObject : public Wrappable<MyObject> {
  171. public:
  172. static WrapperInfo kWrapperInfo;
  173. static gin::Handle<MyObject> Create(v8::Isolate* isolate) {
  174. return CreateHandle(isolate, new MyObject());
  175. }
  176. };
  177. WrapperInfo MyObject::kWrapperInfo = {kEmbedderNativeGin};
  178. } // namespace
  179. TEST_F(ConverterTest, VectorOfWrappables) {
  180. v8::Isolate* isolate = instance_->isolate();
  181. v8::HandleScope handle_scope(isolate);
  182. Handle<MyObject> obj = MyObject::Create(isolate);
  183. std::vector<MyObject*> vector = {obj.get()};
  184. v8::MaybeLocal<v8::Value> maybe = ConvertToV8(isolate, vector);
  185. v8::Local<v8::Value> array;
  186. ASSERT_TRUE(maybe.ToLocal(&array));
  187. v8::Local<v8::Value> array2;
  188. ASSERT_TRUE(TryConvertToV8(isolate, vector, &array2));
  189. std::vector<MyObject*> out_value;
  190. ASSERT_TRUE(ConvertFromV8(isolate, array, &out_value));
  191. EXPECT_THAT(out_value, testing::ContainerEq(vector));
  192. std::vector<MyObject*> out_value2;
  193. ASSERT_TRUE(ConvertFromV8(isolate, array2, &out_value2));
  194. EXPECT_THAT(out_value2, testing::ContainerEq(vector));
  195. }
  196. namespace {
  197. class MoveOnlyObject {
  198. public:
  199. MoveOnlyObject() = default;
  200. MoveOnlyObject(const MoveOnlyObject&) = delete;
  201. MoveOnlyObject& operator=(const MoveOnlyObject&) = delete;
  202. MoveOnlyObject(MoveOnlyObject&&) noexcept = default;
  203. MoveOnlyObject& operator=(MoveOnlyObject&&) noexcept = default;
  204. };
  205. } // namespace
  206. template <>
  207. struct Converter<MoveOnlyObject> {
  208. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, MoveOnlyObject in) {
  209. return v8::Undefined(isolate);
  210. }
  211. static bool FromV8(v8::Isolate* isolate,
  212. v8::Local<v8::Value> val,
  213. MoveOnlyObject* out) {
  214. *out = MoveOnlyObject();
  215. return true;
  216. }
  217. };
  218. TEST_F(ConverterTest, MoveOnlyParameters) {
  219. v8::Isolate* isolate = instance_->isolate();
  220. v8::HandleScope handle_scope(isolate);
  221. auto receives_move_only_obj = [](MoveOnlyObject obj) {};
  222. auto func_templ = gin::CreateFunctionTemplate(
  223. isolate, base::BindRepeating(receives_move_only_obj));
  224. v8::Local<v8::Context> context = instance_->isolate()->GetCurrentContext();
  225. auto func = func_templ->GetFunction(context).ToLocalChecked();
  226. v8::Local<v8::Value> argv[] = {v8::Undefined(isolate)};
  227. func->Call(context, v8::Undefined(isolate), 1, argv).ToLocalChecked();
  228. }
  229. } // namespace gin