api_binding_test_util.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright 2016 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. #ifndef EXTENSIONS_RENDERER_BINDINGS_API_BINDING_TEST_UTIL_H_
  5. #define EXTENSIONS_RENDERER_BINDINGS_API_BINDING_TEST_UTIL_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/strings/string_piece.h"
  9. #include "base/values.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. #include "v8/include/v8.h"
  12. namespace extensions {
  13. // Returns a string with all single quotes replaced with double quotes. Useful
  14. // to write JSON strings without needing to escape quotes.
  15. std::string ReplaceSingleQuotes(base::StringPiece str);
  16. // Returns a base::Value parsed from |str|. Will ADD_FAILURE on error.
  17. base::Value ValueFromString(base::StringPiece str);
  18. // As above, but returning a Value::List.
  19. base::Value::List ListValueFromString(base::StringPiece str);
  20. // As above, but returning a Value::Dict.
  21. base::Value::Dict DictValueFromString(base::StringPiece str);
  22. // Returns a base::Value parsed from |str|. EXPECTs the conversion to succeed.
  23. // DEPRECATED: prefer `ValueFromString`.
  24. std::unique_ptr<base::Value> DeprecatedValueFromString(base::StringPiece str);
  25. // As above, but returning a ListValue.
  26. // DEPRECATED: prefer `ListValueFromString`.
  27. std::unique_ptr<base::ListValue> DeprecatedListValueFromString(
  28. base::StringPiece str);
  29. // As above, but returning a DictionaryValue.
  30. // DEPRECATED: prefer `DictValueFromString`.
  31. std::unique_ptr<base::DictionaryValue> DeprecatedDictionaryValueFromString(
  32. base::StringPiece str);
  33. // Converts the given |value| to a JSON string. EXPECTs the conversion to
  34. // succeed.
  35. std::string ValueToString(const base::Value& value);
  36. // Converts the given |value| to a string. Returns "empty", "undefined", "null",
  37. // or "function" for unserializable values. Note this differs from
  38. // gin::V8ToString, which only accepts v8::String values.
  39. std::string V8ToString(v8::Local<v8::Value> value,
  40. v8::Local<v8::Context> context);
  41. // Returns a v8::Value result from compiling and running |source|, or an empty
  42. // local on failure.
  43. v8::Local<v8::Value> V8ValueFromScriptSource(v8::Local<v8::Context> context,
  44. base::StringPiece source);
  45. // Returns a v8::Function parsed from the given |source|. EXPECTs the conversion
  46. // to succeed.
  47. v8::Local<v8::Function> FunctionFromString(v8::Local<v8::Context> context,
  48. base::StringPiece source);
  49. // Converts the given |value| to a base::Value and returns the result.
  50. std::unique_ptr<base::Value> V8ToBaseValue(v8::Local<v8::Value> value,
  51. v8::Local<v8::Context> context);
  52. // Calls the given |function| with the specified |receiver| and arguments, and
  53. // returns the result. EXPECTs no errors to be thrown.
  54. v8::Local<v8::Value> RunFunction(v8::Local<v8::Function> function,
  55. v8::Local<v8::Context> context,
  56. v8::Local<v8::Value> receiver,
  57. int argc,
  58. v8::Local<v8::Value> argv[]);
  59. // Like RunFunction(), but uses v8::Undefined for the receiver.
  60. v8::Local<v8::Value> RunFunction(v8::Local<v8::Function> function,
  61. v8::Local<v8::Context> context,
  62. int argc,
  63. v8::Local<v8::Value> argv[]);
  64. // Like RunFunction(), but uses the |context|'s Global for the receiver.
  65. v8::Local<v8::Value> RunFunctionOnGlobal(v8::Local<v8::Function> function,
  66. v8::Local<v8::Context> context,
  67. int argc,
  68. v8::Local<v8::Value> argv[]);
  69. // Like RunFunctionOnGlobal(), but doesn't return the result. This is useful
  70. // for binding in places a result isn't expected.
  71. void RunFunctionOnGlobalAndIgnoreResult(v8::Local<v8::Function> function,
  72. v8::Local<v8::Context> context,
  73. int argc,
  74. v8::Local<v8::Value> argv[]);
  75. // Like RunFunctionOnGlobal(), but returns a persistent handle for the result.
  76. v8::Global<v8::Value> RunFunctionOnGlobalAndReturnHandle(
  77. v8::Local<v8::Function> function,
  78. v8::Local<v8::Context> context,
  79. int argc,
  80. v8::Local<v8::Value> argv[]);
  81. // Calls the given |function| with the specified |receiver| and arguments, but
  82. // EXPECTs the function to throw the |expected_error|.
  83. void RunFunctionAndExpectError(v8::Local<v8::Function> function,
  84. v8::Local<v8::Context> context,
  85. v8::Local<v8::Value> receiver,
  86. int argc,
  87. v8::Local<v8::Value> argv[],
  88. const std::string& expected_error);
  89. // Like RunFunctionAndExpectError(), but uses v8::Undefined for the receiver.
  90. void RunFunctionAndExpectError(v8::Local<v8::Function> function,
  91. v8::Local<v8::Context> context,
  92. int argc,
  93. v8::Local<v8::Value> argv[],
  94. const std::string& expected_error);
  95. // Returns the property with the given |key| from the |object|. EXPECTs the
  96. // operation not throw an error, but doesn't assume the key is present.
  97. v8::Local<v8::Value> GetPropertyFromObject(v8::Local<v8::Object> object,
  98. v8::Local<v8::Context> context,
  99. base::StringPiece key);
  100. // As above, but converts the result to a base::Value.
  101. std::unique_ptr<base::Value> GetBaseValuePropertyFromObject(
  102. v8::Local<v8::Object> object,
  103. v8::Local<v8::Context> context,
  104. base::StringPiece key);
  105. // As above, but returns a JSON-serialized version of the value, or
  106. // "undefined", "null", "function", or "empty".
  107. std::string GetStringPropertyFromObject(v8::Local<v8::Object> object,
  108. v8::Local<v8::Context> context,
  109. base::StringPiece key);
  110. // A utility to determine if a V8 value is a certain type.
  111. template <typename T>
  112. struct ValueTypeChecker {};
  113. template <>
  114. struct ValueTypeChecker<v8::Function> {
  115. static bool IsType(v8::Local<v8::Value> value);
  116. };
  117. template <>
  118. struct ValueTypeChecker<v8::Object> {
  119. static bool IsType(v8::Local<v8::Value> value);
  120. };
  121. template <>
  122. struct ValueTypeChecker<v8::Promise> {
  123. static bool IsType(v8::Local<v8::Value> value);
  124. };
  125. template <>
  126. struct ValueTypeChecker<v8::Array> {
  127. static bool IsType(v8::Local<v8::Value> value);
  128. };
  129. // Attempts to convert `value` to the expected type `T`, and populate `out`
  130. // with the result. Returns true on success; adds test failures and returns
  131. // false on error. (We do both so that it fits well into an EXPECT_TRUE() but
  132. // also prints out more detailed failure information).
  133. template <typename T>
  134. bool GetValueAs(v8::Local<v8::Value> value, v8::Local<T>* out) {
  135. if (value.IsEmpty()) {
  136. ADD_FAILURE() << "Value is empty.";
  137. return false;
  138. }
  139. if (!ValueTypeChecker<T>::IsType(value)) {
  140. // TODO(devlin): Move the code to print out the type of a v8::Value from
  141. // argument_spec.cc into a common place, so we can print out
  142. // "Failed to convert value. Actual type <some type>".
  143. ADD_FAILURE() << "Value is incorrect type.";
  144. return false;
  145. }
  146. *out = value.As<T>();
  147. return true;
  148. }
  149. // Returns true if the given `value` is both non-empty and is the expected
  150. // type `T`.
  151. template <typename T>
  152. bool V8ValueIs(v8::Local<v8::Value> value) {
  153. return !value.IsEmpty() && ValueTypeChecker<T>::IsType(value);
  154. }
  155. // Like GetPropertyFromObject(), but attempts to convert the value to the
  156. // expected type `T`. Returns true and populates `out` on success; adds
  157. // test failures and returns false on failure.
  158. template <typename T>
  159. bool GetPropertyFromObjectAs(v8::Local<v8::Object> object,
  160. v8::Local<v8::Context> context,
  161. base::StringPiece key,
  162. v8::Local<T>* out) {
  163. v8::Local<v8::Value> value = GetPropertyFromObject(object, context, key);
  164. return GetValueAs(value, out);
  165. }
  166. } // extensions
  167. #endif // EXTENSIONS_RENDERER_BINDINGS_API_BINDING_TEST_UTIL_H_