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.
5 #ifndef PPAPI_TESTS_TEST_POST_MESSAGE_H_
6 #define PPAPI_TESTS_TEST_POST_MESSAGE_H_
11 #include "ppapi/tests/test_case.h"
13 class TestPostMessage
: public TestCase
{
15 explicit TestPostMessage(TestingInstance
* instance
);
16 virtual ~TestPostMessage();
19 // TestCase implementation.
21 virtual void RunTests(const std::string
& filter
);
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
);
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
);
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
);
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();
46 // Wait for pending messages; return the number of messages that were pending
47 // at the time of invocation.
48 int WaitForMessages();
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
);
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
);
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
66 std::string
TestSendInInit();
68 // Test some basic functionality; make sure we can send data successfully
69 // in both directions.
70 std::string
TestSendingData();
72 // Test sending string vars in both directions.
73 std::string
TestSendingString();
75 // Test sending ArrayBuffer vars in both directions.
76 std::string
TestSendingArrayBuffer();
78 // Test sending Array vars in both directions.
79 std::string
TestSendingArray();
81 // Test sending Dictionary vars in both directions.
82 std::string
TestSendingDictionary();
84 // Test sending Resource vars in both directions.
85 std::string
TestSendingResource();
87 // Test sending a complex var with references and cycles in both directions.
88 std::string
TestSendingComplexVar();
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();
94 // Test sending a message when no handler exists, make sure nothing happens.
95 std::string
TestNoHandler();
97 // Test sending from JavaScript to the plugin with extra parameters, make sure
99 std::string
TestExtraParam();
101 // Test sending messages off of the main thread.
102 std::string
TestNonMainThread();
104 typedef std::vector
<pp::Var
> VarVector
;
106 // This is used to store pp::Var objects we receive via a call to
108 VarVector message_data_
;
111 #endif // PPAPI_TESTS_TEST_POST_MESSAGE_H_