native_extension_bindings_system_test_base.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_NATIVE_EXTENSION_BINDINGS_SYSTEM_TEST_BASE_H_
  5. #define EXTENSIONS_RENDERER_NATIVE_EXTENSION_BINDINGS_SYSTEM_TEST_BASE_H_
  6. #include <memory>
  7. #include <string>
  8. #include "extensions/common/api/messaging/message.h"
  9. #include "extensions/common/api/messaging/port_id.h"
  10. #include "extensions/common/extension.h"
  11. #include "extensions/common/extension_id.h"
  12. #include "extensions/common/features/feature.h"
  13. #include "extensions/common/mojom/frame.mojom-forward.h"
  14. #include "extensions/renderer/bindings/api_binding_test.h"
  15. #include "extensions/renderer/bindings/api_binding_types.h"
  16. #include "extensions/renderer/ipc_message_sender.h"
  17. #include "extensions/renderer/message_target.h"
  18. #include "extensions/renderer/string_source_map.h"
  19. #include "extensions/renderer/test_extensions_renderer_client.h"
  20. #include "mojo/public/cpp/bindings/struct_ptr.h"
  21. #include "testing/gmock/include/gmock/gmock.h"
  22. #include "v8/include/v8-forward.h"
  23. struct ExtensionHostMsg_APIActionOrEvent_Params;
  24. namespace base {
  25. class DictionaryValue;
  26. }
  27. namespace content {
  28. class MockRenderThread;
  29. }
  30. namespace v8 {
  31. class ExtensionConfiguration;
  32. }
  33. namespace extensions {
  34. class NativeExtensionBindingsSystem;
  35. class ScriptContext;
  36. class ScriptContextSet;
  37. // A mocked-up IPC message sender for use in testing.
  38. class TestIPCMessageSender : public IPCMessageSender {
  39. public:
  40. TestIPCMessageSender();
  41. TestIPCMessageSender(const TestIPCMessageSender&) = delete;
  42. TestIPCMessageSender& operator=(const TestIPCMessageSender&) = delete;
  43. ~TestIPCMessageSender() override;
  44. // IPCMessageSender:
  45. void SendRequestIPC(ScriptContext* context,
  46. mojom::RequestParamsPtr params) override;
  47. void SendOnRequestResponseReceivedIPC(int request_id) override {}
  48. // The event listener methods are less of a pain to mock (since they don't
  49. // have complex parameters like mojom::RequestParams).
  50. MOCK_METHOD2(SendAddUnfilteredEventListenerIPC,
  51. void(ScriptContext* context, const std::string& event_name));
  52. MOCK_METHOD2(SendRemoveUnfilteredEventListenerIPC,
  53. void(ScriptContext* context, const std::string& event_name));
  54. // Send a message to add/remove a lazy unfiltered listener.
  55. MOCK_METHOD2(SendAddUnfilteredLazyEventListenerIPC,
  56. void(ScriptContext* context, const std::string& event_name));
  57. MOCK_METHOD2(SendRemoveUnfilteredLazyEventListenerIPC,
  58. void(ScriptContext* context, const std::string& event_name));
  59. // Send a message to add/remove a filtered listener.
  60. MOCK_METHOD4(SendAddFilteredEventListenerIPC,
  61. void(ScriptContext* context,
  62. const std::string& event_name,
  63. const base::DictionaryValue& filter,
  64. bool is_lazy));
  65. MOCK_METHOD4(SendRemoveFilteredEventListenerIPC,
  66. void(ScriptContext* context,
  67. const std::string& event_name,
  68. const base::DictionaryValue& filter,
  69. bool remove_lazy_listener));
  70. MOCK_METHOD4(SendOpenMessageChannel,
  71. void(ScriptContext* script_context,
  72. const PortId& port_id,
  73. const MessageTarget& target,
  74. const std::string& channel_name));
  75. MOCK_METHOD2(SendOpenMessagePort,
  76. void(int routing_id, const PortId& port_id));
  77. MOCK_METHOD3(SendCloseMessagePort,
  78. void(int routing_id, const PortId& port_id, bool close_channel));
  79. MOCK_METHOD2(SendPostMessageToPort,
  80. void(const PortId& port_id, const Message& message));
  81. MOCK_METHOD2(SendMessageResponsePending,
  82. void(int routing_id, const PortId& port_id));
  83. MOCK_METHOD3(SendActivityLogIPC,
  84. void(const ExtensionId& extension_id,
  85. IPCMessageSender::ActivityLogCallType call_type,
  86. const ExtensionHostMsg_APIActionOrEvent_Params& params));
  87. const mojom::RequestParams* last_params() const { return last_params_.get(); }
  88. private:
  89. mojom::RequestParamsPtr last_params_;
  90. };
  91. // A test harness to instantiate the NativeExtensionBindingsSystem (along with
  92. // its dependencies) and support adding/removing extensions and ScriptContexts.
  93. // This is useful for bindings tests that need extensions-specific knowledge.
  94. class NativeExtensionBindingsSystemUnittest : public APIBindingTest {
  95. public:
  96. NativeExtensionBindingsSystemUnittest();
  97. NativeExtensionBindingsSystemUnittest(
  98. const NativeExtensionBindingsSystemUnittest&) = delete;
  99. NativeExtensionBindingsSystemUnittest& operator=(
  100. const NativeExtensionBindingsSystemUnittest&) = delete;
  101. ~NativeExtensionBindingsSystemUnittest() override;
  102. protected:
  103. // APIBindingTest:
  104. void SetUp() override;
  105. void TearDown() override;
  106. void OnWillDisposeContext(v8::Local<v8::Context> context) override;
  107. v8::ExtensionConfiguration* GetV8ExtensionConfiguration() override;
  108. std::unique_ptr<TestJSRunner::Scope> CreateTestJSRunner() override;
  109. ScriptContext* CreateScriptContext(v8::Local<v8::Context> v8_context,
  110. const Extension* extension,
  111. Feature::Context context_type);
  112. void RegisterExtension(scoped_refptr<const Extension> extension);
  113. // Returns whether or not a StrictMock should be used for the
  114. // IPCMessageSender. The default is to return false.
  115. virtual bool UseStrictIPCMessageSender();
  116. NativeExtensionBindingsSystem* bindings_system() {
  117. return bindings_system_.get();
  118. }
  119. bool has_last_params() const { return !!ipc_message_sender_->last_params(); }
  120. const mojom::RequestParams& last_params() {
  121. return *ipc_message_sender_->last_params();
  122. }
  123. StringSourceMap* source_map() { return &source_map_; }
  124. TestIPCMessageSender* ipc_message_sender() { return ipc_message_sender_; }
  125. ScriptContextSet* script_context_set() { return script_context_set_.get(); }
  126. void set_allow_unregistered_contexts(bool allow_unregistered_contexts) {
  127. allow_unregistered_contexts_ = allow_unregistered_contexts;
  128. }
  129. private:
  130. ExtensionIdSet extension_ids_;
  131. std::unique_ptr<content::MockRenderThread> render_thread_;
  132. std::unique_ptr<ScriptContextSet> script_context_set_;
  133. std::vector<ScriptContext*> raw_script_contexts_;
  134. std::unique_ptr<NativeExtensionBindingsSystem> bindings_system_;
  135. // The TestIPCMessageSender; owned by the bindings system.
  136. TestIPCMessageSender* ipc_message_sender_ = nullptr;
  137. StringSourceMap source_map_;
  138. TestExtensionsRendererClient renderer_client_;
  139. // True if we allow some v8::Contexts to avoid registration as a
  140. // ScriptContext.
  141. bool allow_unregistered_contexts_ = false;
  142. };
  143. } // namespace extensions
  144. #endif // EXTENSIONS_RENDERER_NATIVE_EXTENSION_BINDINGS_SYSTEM_TEST_BASE_H_