api_bindings_system_unittest.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2017 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_BINDINGS_SYSTEM_UNITTEST_H_
  5. #define EXTENSIONS_RENDERER_BINDINGS_API_BINDINGS_SYSTEM_UNITTEST_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include "extensions/renderer/bindings/api_binding_test.h"
  10. #include "extensions/renderer/bindings/api_binding_types.h"
  11. #include "extensions/renderer/bindings/api_request_handler.h"
  12. #include "v8/include/v8.h"
  13. namespace base {
  14. class DictionaryValue;
  15. }
  16. namespace extensions {
  17. class APIBindingsSystem;
  18. // The base class to test the APIBindingsSystem. This allows subclasses to
  19. // retrieve API schemas differently.
  20. class APIBindingsSystemTest : public APIBindingTest {
  21. public:
  22. APIBindingsSystemTest(const APIBindingsSystemTest&) = delete;
  23. APIBindingsSystemTest& operator=(const APIBindingsSystemTest&) = delete;
  24. protected:
  25. // A struct representing a "fake" API, including the name and specification.
  26. // The specification is expected to be a JSON-serializable string that
  27. // specifies the types, methods, and events of an API in the same syntax as
  28. // the real extension APIs.
  29. struct FakeSpec {
  30. const char* name;
  31. const char* spec;
  32. };
  33. APIBindingsSystemTest();
  34. ~APIBindingsSystemTest() override;
  35. void SetUp() override;
  36. void TearDown() override;
  37. void OnWillDisposeContext(v8::Local<v8::Context> context) override;
  38. // Returns the collection of fake APIs to be used in the test, allowing
  39. // subclasses to return their own specifications.
  40. virtual std::vector<FakeSpec> GetAPIs();
  41. // Returns the object to be used as the parent for the `lastError`, and,
  42. // optionally, the secondary parent. The default returns an empty JS object
  43. // and does not populate |secondary_parent| (assumes no last errors will be
  44. // set).
  45. virtual v8::Local<v8::Object> GetLastErrorParent(
  46. v8::Local<v8::Context> context,
  47. v8::Local<v8::Object>* secondary_parent);
  48. // Simulates logging an error to the console.
  49. void AddConsoleError(v8::Local<v8::Context> context,
  50. const std::string& error);
  51. // Returns the DictionaryValue representing the schema with the given API
  52. // name.
  53. const base::DictionaryValue& GetAPISchema(const std::string& api_name);
  54. // Callback for event listeners changing.
  55. void OnEventListenersChanged(const std::string& event_name,
  56. binding::EventListenersChanged changed,
  57. const base::DictionaryValue* filter,
  58. bool was_manual,
  59. v8::Local<v8::Context> context);
  60. // Callback for an API request being made. Stores the request in
  61. // |last_request_|.
  62. void OnAPIRequest(std::unique_ptr<APIRequestHandler::Request> request,
  63. v8::Local<v8::Context> context);
  64. // Checks that |last_request_| exists and was provided with the
  65. // |expected_name| and |expected_arguments|.
  66. void ValidateLastRequest(const std::string& expected_name,
  67. const std::string& expected_arguments);
  68. // Wraps the given |script source| in (function(obj) { ... }) and executes
  69. // the result function, passing in |object| for an argument. Returns the
  70. // result of calling the function.
  71. v8::Local<v8::Value> CallFunctionOnObject(v8::Local<v8::Context> context,
  72. v8::Local<v8::Object> object,
  73. const std::string& script_source);
  74. const APIRequestHandler::Request* last_request() const {
  75. return last_request_.get();
  76. }
  77. void reset_last_request() { last_request_.reset(); }
  78. APIBindingsSystem* bindings_system() { return bindings_system_.get(); }
  79. const std::vector<std::string>& console_errors() const {
  80. return console_errors_;
  81. }
  82. private:
  83. // The API schemas for the fake APIs.
  84. std::map<std::string, std::unique_ptr<base::DictionaryValue>> api_schemas_;
  85. // The APIBindingsSystem associated with the test. Safe to use across multiple
  86. // contexts.
  87. std::unique_ptr<APIBindingsSystem> bindings_system_;
  88. // The last request to be received from the APIBindingsSystem, or null if
  89. // there is none.
  90. std::unique_ptr<APIRequestHandler::Request> last_request_;
  91. // A list for keeping track of simulated console errors.
  92. std::vector<std::string> console_errors_;
  93. };
  94. } // namespace extensions
  95. #endif // EXTENSIONS_RENDERER_BINDINGS_API_BINDINGS_SYSTEM_UNITTEST_H_