send_message_tester.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_SEND_MESSAGE_TESTER_H_
  5. #define EXTENSIONS_RENDERER_SEND_MESSAGE_TESTER_H_
  6. #include <string>
  7. #include "v8/include/v8-forward.h"
  8. namespace extensions {
  9. class ScriptContext;
  10. class TestIPCMessageSender;
  11. struct MessageTarget;
  12. // A helper class for testing the sendMessage, sendRequest, and connect API
  13. // calls, since these are used across three different API namespaces
  14. // (chrome.runtime, chrome.tabs, and chrome.extension).
  15. class SendMessageTester {
  16. public:
  17. SendMessageTester(TestIPCMessageSender* ipc_sender,
  18. ScriptContext* script_context,
  19. int next_port_id,
  20. const std::string& api_namespace);
  21. SendMessageTester(const SendMessageTester&) = delete;
  22. SendMessageTester& operator=(const SendMessageTester&) = delete;
  23. ~SendMessageTester();
  24. // Whether we expect the port to be open or closed at the end of the call.
  25. enum PortStatus {
  26. CLOSED,
  27. OPEN,
  28. };
  29. // Tests the sendMessage API with the specified expectations.
  30. v8::Local<v8::Value> TestSendMessage(const std::string& args,
  31. const std::string& expected_message,
  32. const MessageTarget& expected_target,
  33. PortStatus expected_port_status);
  34. // Tests the sendRequest API with the specified expectations.
  35. v8::Local<v8::Value> TestSendRequest(const std::string& args,
  36. const std::string& expected_message,
  37. const MessageTarget& expected_target,
  38. PortStatus expected_port_status);
  39. // Tests the sendNativeMessage API with the specified expectations.
  40. v8::Local<v8::Value> TestSendNativeMessage(
  41. const std::string& args,
  42. const std::string& expected_message,
  43. const std::string& expected_application_name);
  44. // Tests the connect API with the specified expectations.
  45. void TestConnect(const std::string& args,
  46. const std::string& expected_channel,
  47. const MessageTarget& expected_target);
  48. private:
  49. enum Method {
  50. SEND_REQUEST,
  51. SEND_MESSAGE,
  52. SEND_NATIVE_MESSAGE,
  53. };
  54. // Common handler for testing sendMessage and sendRequest.
  55. void TestSendMessageOrRequest(const std::string& args,
  56. const std::string& expected_message,
  57. const MessageTarget& expected_target,
  58. PortStatus expected_port_status,
  59. Method method,
  60. v8::Local<v8::Value>& out_value);
  61. TestIPCMessageSender* ipc_sender_;
  62. ScriptContext* script_context_;
  63. int next_port_id_;
  64. std::string api_namespace_;
  65. };
  66. } // namespace extensions
  67. #endif // EXTENSIONS_RENDERER_SEND_MESSAGE_TESTER_H_