api_test_utils.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #ifndef EXTENSIONS_BROWSER_API_TEST_UTILS_H_
  5. #define EXTENSIONS_BROWSER_API_TEST_UTILS_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/memory/ref_counted.h"
  9. #include "base/run_loop.h"
  10. #include "extensions/browser/extension_function.h"
  11. namespace base {
  12. class DictionaryValue;
  13. class ListValue;
  14. class Value;
  15. }
  16. namespace content {
  17. class BrowserContext;
  18. }
  19. namespace extensions {
  20. class ExtensionFunctionDispatcher;
  21. // TODO(yoz): crbug.com/394840: Remove duplicate functionality in
  22. // chrome/browser/extensions/extension_function_test_utils.h.
  23. //
  24. // TODO(ckehoe): Accept args as std::unique_ptr<base::Value>,
  25. // and migrate existing users to the new API.
  26. namespace api_test_utils {
  27. // A helper class to handle waiting for a function response.
  28. class SendResponseHelper {
  29. public:
  30. explicit SendResponseHelper(ExtensionFunction* function);
  31. SendResponseHelper(const SendResponseHelper&) = delete;
  32. SendResponseHelper& operator=(const SendResponseHelper&) = delete;
  33. ~SendResponseHelper();
  34. bool has_response() { return response_.get() != nullptr; }
  35. // Asserts a response has been posted (has_response()) and returns the value.
  36. bool GetResponse();
  37. // Waits until a response is posted.
  38. void WaitForResponse();
  39. private:
  40. // Response handler.
  41. void OnResponse(ExtensionFunction::ResponseType response,
  42. base::Value::List results,
  43. const std::string& error);
  44. base::RunLoop run_loop_;
  45. std::unique_ptr<bool> response_;
  46. };
  47. enum RunFunctionFlags { NONE = 0, INCLUDE_INCOGNITO = 1 << 0 };
  48. // Parse JSON and return as the specified type, or NULL if the JSON is invalid
  49. // or not the specified type.
  50. std::unique_ptr<base::DictionaryValue> ParseDictionary(const std::string& data);
  51. // Get |key| from |val| as the specified type. If |key| does not exist, or is
  52. // not of the specified type, adds a failure to the current test and returns
  53. // false, 0, empty string, etc.
  54. bool GetBoolean(const base::Value::Dict& val, const std::string& key);
  55. int GetInteger(const base::Value::Dict& val, const std::string& key);
  56. std::string GetString(const base::Value::Dict& val, const std::string& key);
  57. base::Value::List GetList(const base::Value::Dict& val, const std::string& key);
  58. base::Value::Dict GetDict(const base::Value::Dict& val, const std::string& key);
  59. // Run |function| with |args| and return the result. Adds an error to the
  60. // current test if |function| returns an error. Takes ownership of
  61. // |function|. The caller takes ownership of the result.
  62. std::unique_ptr<base::Value> RunFunctionWithDelegateAndReturnSingleResult(
  63. scoped_refptr<ExtensionFunction> function,
  64. const std::string& args,
  65. std::unique_ptr<ExtensionFunctionDispatcher> dispatcher,
  66. RunFunctionFlags flags);
  67. std::unique_ptr<base::Value> RunFunctionWithDelegateAndReturnSingleResult(
  68. scoped_refptr<ExtensionFunction> function,
  69. std::unique_ptr<base::ListValue> args,
  70. std::unique_ptr<ExtensionFunctionDispatcher> dispatcher,
  71. RunFunctionFlags flags);
  72. // RunFunctionWithDelegateAndReturnSingleResult, except with a NULL
  73. // implementation of the Delegate.
  74. std::unique_ptr<base::Value> RunFunctionAndReturnSingleResult(
  75. ExtensionFunction* function,
  76. const std::string& args,
  77. content::BrowserContext* context);
  78. std::unique_ptr<base::Value> RunFunctionAndReturnSingleResult(
  79. ExtensionFunction* function,
  80. const std::string& args,
  81. content::BrowserContext* context,
  82. RunFunctionFlags flags);
  83. // Run |function| with |args| and return the resulting error. Adds an error to
  84. // the current test if |function| returns a result. Takes ownership of
  85. // |function|.
  86. std::string RunFunctionAndReturnError(ExtensionFunction* function,
  87. const std::string& args,
  88. content::BrowserContext* context,
  89. RunFunctionFlags flags);
  90. std::string RunFunctionAndReturnError(ExtensionFunction* function,
  91. const std::string& args,
  92. content::BrowserContext* context);
  93. // Create and run |function| with |args|. Works with both synchronous and async
  94. // functions. Ownership of |function| remains with the caller.
  95. //
  96. // TODO(aa): It would be nice if |args| could be validated against the schema
  97. // that |function| expects. That way, we know that we are testing something
  98. // close to what the bindings would actually send.
  99. //
  100. // TODO(aa): I'm concerned that this style won't scale to all the bits and bobs
  101. // we're going to need to frob for all the different extension functions. But
  102. // we can refactor when we see what is needed.
  103. bool RunFunction(ExtensionFunction* function,
  104. const std::string& args,
  105. content::BrowserContext* context);
  106. bool RunFunction(ExtensionFunction* function,
  107. const std::string& args,
  108. std::unique_ptr<ExtensionFunctionDispatcher> dispatcher,
  109. RunFunctionFlags flags);
  110. bool RunFunction(ExtensionFunction* function,
  111. std::unique_ptr<base::ListValue> args,
  112. std::unique_ptr<ExtensionFunctionDispatcher> dispatcher,
  113. RunFunctionFlags flags);
  114. } // namespace api_test_utils
  115. } // namespace extensions
  116. #endif // EXTENSIONS_BROWSER_API_TEST_UTILS_H_