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