test_post_message.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (c) 2011 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 PPAPI_TESTS_TEST_POST_MESSAGE_H_
  5. #define PPAPI_TESTS_TEST_POST_MESSAGE_H_
  6. #include <string>
  7. #include <vector>
  8. #include "ppapi/tests/test_case.h"
  9. class TestPostMessage : public TestCase {
  10. public:
  11. explicit TestPostMessage(TestingInstance* instance);
  12. virtual ~TestPostMessage();
  13. private:
  14. // TestCase implementation.
  15. virtual bool Init();
  16. virtual void RunTests(const std::string& filter);
  17. // A handler for JS->Native calls to postMessage. Simply pushes
  18. // the given value to the back of message_data_
  19. virtual void HandleMessage(const pp::Var& message_data);
  20. // Add a listener for message events which will echo back the given
  21. // JavaScript expression by passing it to postMessage. JavaScript Variables
  22. // available to the expression are:
  23. // 'plugin' - the DOM element for the test plugin.
  24. // 'message_event' - the message event parameter to the listener function.
  25. // This also adds the new listener to an array called 'eventListeners' on the
  26. // plugin's DOM element. This is used by ClearListeners().
  27. // Returns true on success, false on failure.
  28. bool AddEchoingListener(const std::string& expression);
  29. // Posts a message from JavaScript to the plugin. |func| should be a
  30. // JavaScript function which returns the variable to post.
  31. bool PostMessageFromJavaScript(const std::string& func);
  32. // Clear any listeners that have been added using AddEchoingListener by
  33. // calling removeEventListener for each.
  34. // Returns true on success, false on failure.
  35. bool ClearListeners();
  36. // Wait for pending messages; return the number of messages that were pending
  37. // at the time of invocation.
  38. int WaitForMessages();
  39. // Posts a message from JavaScript to the plugin and wait for it to arrive.
  40. // |func| should be a JavaScript function(callback) which calls |callback|
  41. // with the variable to post. This function will block until the message
  42. // arrives on the plugin side (there is no need to use WaitForMessages()).
  43. // Returns the number of messages that were pending at the time of invocation.
  44. int PostAsyncMessageFromJavaScriptAndWait(const std::string& func);
  45. // Verifies that the given javascript assertions are true of the message
  46. // (|test_data|) passed via PostMessage().
  47. std::string CheckMessageProperties(
  48. const pp::Var& test_data,
  49. const std::vector<std::string>& properties_to_check);
  50. // Test that we can send a message from Instance::Init. Note the actual
  51. // message is sent in TestPostMessage::Init, and this test simply makes sure
  52. // we got it.
  53. std::string TestSendInInit();
  54. // Test some basic functionality; make sure we can send data successfully
  55. // in both directions.
  56. std::string TestSendingData();
  57. // Test sending string vars in both directions.
  58. std::string TestSendingString();
  59. // Test sending ArrayBuffer vars in both directions.
  60. std::string TestSendingArrayBuffer();
  61. // Test sending Array vars in both directions.
  62. std::string TestSendingArray();
  63. // Test sending Dictionary vars in both directions.
  64. std::string TestSendingDictionary();
  65. // Test sending Resource vars in both directions.
  66. std::string TestSendingResource();
  67. // Test sending a complex var with references and cycles in both directions.
  68. std::string TestSendingComplexVar();
  69. // Test the MessageEvent object that JavaScript received to make sure it is
  70. // of the right type and has all the expected fields.
  71. std::string TestMessageEvent();
  72. // Test sending a message when no handler exists, make sure nothing happens.
  73. std::string TestNoHandler();
  74. // Test sending from JavaScript to the plugin with extra parameters, make sure
  75. // nothing happens.
  76. std::string TestExtraParam();
  77. // Test sending messages off of the main thread.
  78. std::string TestNonMainThread();
  79. typedef std::vector<pp::Var> VarVector;
  80. // This is used to store pp::Var objects we receive via a call to
  81. // HandleMessage.
  82. VarVector message_data_;
  83. };
  84. #endif // PPAPI_TESTS_TEST_POST_MESSAGE_H_