activity_log_converter_strategy_unittest.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.
  4. #include "extensions/renderer/activity_log_converter_strategy.h"
  5. #include <memory>
  6. #include "base/values.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. #include "v8/include/v8.h"
  9. namespace extensions {
  10. class ActivityLogConverterStrategyTest : public testing::Test {
  11. public:
  12. ActivityLogConverterStrategyTest()
  13. : isolate_(v8::Isolate::GetCurrent()),
  14. handle_scope_(isolate_),
  15. context_(isolate_, v8::Context::New(isolate_)),
  16. context_scope_(context()) {}
  17. protected:
  18. void SetUp() override {
  19. converter_ = content::V8ValueConverter::Create();
  20. strategy_ = std::make_unique<ActivityLogConverterStrategy>();
  21. converter_->SetFunctionAllowed(true);
  22. converter_->SetStrategy(strategy_.get());
  23. }
  24. testing::AssertionResult VerifyNull(v8::Local<v8::Value> v8_value) {
  25. std::unique_ptr<base::Value> value(
  26. converter_->FromV8Value(v8_value, context()));
  27. if (value->is_none())
  28. return testing::AssertionSuccess();
  29. return testing::AssertionFailure();
  30. }
  31. testing::AssertionResult VerifyBoolean(v8::Local<v8::Value> v8_value,
  32. bool expected) {
  33. std::unique_ptr<base::Value> value(
  34. converter_->FromV8Value(v8_value, context()));
  35. if (value->is_bool() && value->GetBool() == expected)
  36. return testing::AssertionSuccess();
  37. return testing::AssertionFailure();
  38. }
  39. testing::AssertionResult VerifyInteger(v8::Local<v8::Value> v8_value,
  40. int expected) {
  41. std::unique_ptr<base::Value> value(
  42. converter_->FromV8Value(v8_value, context()));
  43. if (value->is_int() && value->GetInt() == expected)
  44. return testing::AssertionSuccess();
  45. return testing::AssertionFailure();
  46. }
  47. testing::AssertionResult VerifyDouble(v8::Local<v8::Value> v8_value,
  48. double expected) {
  49. std::unique_ptr<base::Value> value(
  50. converter_->FromV8Value(v8_value, context()));
  51. if (value->is_double() && value->GetDouble() == expected)
  52. return testing::AssertionSuccess();
  53. return testing::AssertionFailure();
  54. }
  55. testing::AssertionResult VerifyString(v8::Local<v8::Value> v8_value,
  56. const std::string& expected) {
  57. std::unique_ptr<base::Value> value(
  58. converter_->FromV8Value(v8_value, context()));
  59. if (value->is_string() && value->GetString() == expected)
  60. return testing::AssertionSuccess();
  61. return testing::AssertionFailure();
  62. }
  63. v8::Local<v8::Context> context() const {
  64. return v8::Local<v8::Context>::New(isolate_, context_);
  65. }
  66. v8::Isolate* isolate_;
  67. v8::HandleScope handle_scope_;
  68. v8::Global<v8::Context> context_;
  69. v8::Context::Scope context_scope_;
  70. std::unique_ptr<content::V8ValueConverter> converter_;
  71. std::unique_ptr<ActivityLogConverterStrategy> strategy_;
  72. };
  73. TEST_F(ActivityLogConverterStrategyTest, ConversionTest) {
  74. const char* source = "(function() {"
  75. "function foo() {}"
  76. "return {"
  77. "null: null,"
  78. "true: true,"
  79. "false: false,"
  80. "positive_int: 42,"
  81. "negative_int: -42,"
  82. "zero: 0,"
  83. "double: 88.8,"
  84. "big_integral_double: 9007199254740992.0," // 2.0^53
  85. "string: \"foobar\","
  86. "empty_string: \"\","
  87. "dictionary: {"
  88. "foo: \"bar\","
  89. "hot: \"dog\","
  90. "},"
  91. "empty_dictionary: {},"
  92. "list: [ \"bar\", \"foo\" ],"
  93. "empty_list: [],"
  94. "function: (0, function() {})," // ensure function is anonymous
  95. "named_function: foo"
  96. "};"
  97. "})();";
  98. v8::MicrotasksScope microtasks(
  99. isolate_, v8::MicrotasksScope::kDoNotRunMicrotasks);
  100. v8::Local<v8::Context> context = context_.Get(isolate_);
  101. v8::Local<v8::Script> script(
  102. v8::Script::Compile(
  103. context, v8::String::NewFromUtf8(isolate_, source,
  104. v8::NewStringType::kInternalized)
  105. .ToLocalChecked())
  106. .ToLocalChecked());
  107. v8::Local<v8::Object> v8_object =
  108. script->Run(context).ToLocalChecked().As<v8::Object>();
  109. EXPECT_TRUE(VerifyString(v8_object, "[Object]"));
  110. EXPECT_TRUE(VerifyNull(
  111. v8_object
  112. ->Get(context, v8::String::NewFromUtf8(
  113. isolate_, "null", v8::NewStringType::kInternalized)
  114. .ToLocalChecked())
  115. .ToLocalChecked()));
  116. EXPECT_TRUE(VerifyBoolean(
  117. v8_object
  118. ->Get(context, v8::String::NewFromUtf8(
  119. isolate_, "true", v8::NewStringType::kInternalized)
  120. .ToLocalChecked())
  121. .ToLocalChecked(),
  122. true));
  123. EXPECT_TRUE(VerifyBoolean(
  124. v8_object
  125. ->Get(context,
  126. v8::String::NewFromUtf8(isolate_, "false",
  127. v8::NewStringType::kInternalized)
  128. .ToLocalChecked())
  129. .ToLocalChecked(),
  130. false));
  131. EXPECT_TRUE(VerifyInteger(
  132. v8_object
  133. ->Get(context,
  134. v8::String::NewFromUtf8(isolate_, "positive_int",
  135. v8::NewStringType::kInternalized)
  136. .ToLocalChecked())
  137. .ToLocalChecked(),
  138. 42));
  139. EXPECT_TRUE(VerifyInteger(
  140. v8_object
  141. ->Get(context,
  142. v8::String::NewFromUtf8(isolate_, "negative_int",
  143. v8::NewStringType::kInternalized)
  144. .ToLocalChecked())
  145. .ToLocalChecked(),
  146. -42));
  147. EXPECT_TRUE(VerifyInteger(
  148. v8_object
  149. ->Get(context, v8::String::NewFromUtf8(
  150. isolate_, "zero", v8::NewStringType::kInternalized)
  151. .ToLocalChecked())
  152. .ToLocalChecked(),
  153. 0));
  154. EXPECT_TRUE(VerifyDouble(
  155. v8_object
  156. ->Get(context,
  157. v8::String::NewFromUtf8(isolate_, "double",
  158. v8::NewStringType::kInternalized)
  159. .ToLocalChecked())
  160. .ToLocalChecked(),
  161. 88.8));
  162. EXPECT_TRUE(VerifyDouble(
  163. v8_object
  164. ->Get(context,
  165. v8::String::NewFromUtf8(isolate_, "big_integral_double",
  166. v8::NewStringType::kInternalized)
  167. .ToLocalChecked())
  168. .ToLocalChecked(),
  169. 9007199254740992.0));
  170. EXPECT_TRUE(VerifyString(
  171. v8_object
  172. ->Get(context,
  173. v8::String::NewFromUtf8(isolate_, "string",
  174. v8::NewStringType::kInternalized)
  175. .ToLocalChecked())
  176. .ToLocalChecked(),
  177. "foobar"));
  178. EXPECT_TRUE(VerifyString(
  179. v8_object
  180. ->Get(context,
  181. v8::String::NewFromUtf8(isolate_, "empty_string",
  182. v8::NewStringType::kInternalized)
  183. .ToLocalChecked())
  184. .ToLocalChecked(),
  185. ""));
  186. EXPECT_TRUE(VerifyString(
  187. v8_object
  188. ->Get(context,
  189. v8::String::NewFromUtf8(isolate_, "dictionary",
  190. v8::NewStringType::kInternalized)
  191. .ToLocalChecked())
  192. .ToLocalChecked(),
  193. "[Object]"));
  194. EXPECT_TRUE(VerifyString(
  195. v8_object
  196. ->Get(context,
  197. v8::String::NewFromUtf8(isolate_, "empty_dictionary",
  198. v8::NewStringType::kInternalized)
  199. .ToLocalChecked())
  200. .ToLocalChecked(),
  201. "[Object]"));
  202. EXPECT_TRUE(VerifyString(
  203. v8_object
  204. ->Get(context, v8::String::NewFromUtf8(
  205. isolate_, "list", v8::NewStringType::kInternalized)
  206. .ToLocalChecked())
  207. .ToLocalChecked(),
  208. "[Array]"));
  209. EXPECT_TRUE(VerifyString(
  210. v8_object
  211. ->Get(context,
  212. v8::String::NewFromUtf8(isolate_, "empty_list",
  213. v8::NewStringType::kInternalized)
  214. .ToLocalChecked())
  215. .ToLocalChecked(),
  216. "[Array]"));
  217. EXPECT_TRUE(VerifyString(
  218. v8_object
  219. ->Get(context,
  220. v8::String::NewFromUtf8(isolate_, "function",
  221. v8::NewStringType::kInternalized)
  222. .ToLocalChecked())
  223. .ToLocalChecked(),
  224. "[Function]"));
  225. EXPECT_TRUE(VerifyString(
  226. v8_object
  227. ->Get(context,
  228. v8::String::NewFromUtf8(isolate_, "named_function",
  229. v8::NewStringType::kInternalized)
  230. .ToLocalChecked())
  231. .ToLocalChecked(),
  232. "[Function foo()]"));
  233. }
  234. } // namespace extensions