|
OLD | NEW |
---|---|
(Empty) | |
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 #include "ppapi/tests/test_post_message.h" | |
6 | |
7 #include "ppapi/c/dev/ppb_testing_dev.h" | |
8 #include "ppapi/c/pp_var.h" | |
9 #include "ppapi/cpp/dev/scriptable_object_deprecated.h" | |
10 #include "ppapi/cpp/instance.h" | |
11 #include "ppapi/cpp/var.h" | |
12 #include "ppapi/tests/testing_instance.h" | |
13 | |
14 REGISTER_TEST_CASE(PostMessage); | |
15 | |
16 namespace { | |
brettw
2011/03/22 05:59:11
Blank lines on both sides of namespace begins & en
dmichael(do not use this one)
2011/03/22 15:39:00
Done.
| |
17 const PPB_Testing_Dev* GetTestingInterface() { | |
18 static const PPB_Testing_Dev* g_testing_interface = | |
19 reinterpret_cast<PPB_Testing_Dev const*>( | |
20 pp::Module::Get()->GetBrowserInterface(PPB_TESTING_DEV_INTERFACE)); | |
21 return g_testing_interface; | |
22 } | |
23 | |
24 const std::string kTestString("Hello world!"); | |
brettw
2011/03/22 05:59:11
Even though this is just test code, it would be ni
dmichael(do not use this one)
2011/03/22 15:39:00
Done.
| |
25 const bool kTestBool(true); | |
26 const int32_t kTestInt(42); | |
27 const double kTestDouble(42.0); | |
28 const int32_t kThreadsToRun(10); | |
29 } // namespace | |
brettw
2011/03/22 05:59:11
Ditto
dmichael(do not use this one)
2011/03/22 15:39:00
Done.
| |
30 | |
31 bool TestPostMessage::Init() { | |
32 testing_interface_ = reinterpret_cast<const PPB_Testing_Dev*>( | |
33 pp::Module::Get()->GetBrowserInterface(PPB_TESTING_DEV_INTERFACE)); | |
34 if (!testing_interface_) { | |
35 // Give a more helpful error message for the testing interface being gone | |
36 // since that needs special enabling in Chrome. | |
37 instance_->AppendError("This test needs the testing interface, which is " | |
38 "not currently available. In Chrome, use --enable-pepper-testing when " | |
39 "launching."); | |
40 } | |
41 return (testing_interface_ != NULL); | |
42 } | |
43 | |
44 void TestPostMessage::RunTest() { | |
45 RUN_TEST(SendingData); | |
46 RUN_TEST(MessageEvent); | |
47 RUN_TEST(NoHandler); | |
48 } | |
49 | |
50 void TestPostMessage::HandleMessage(const pp::Var& message_data) { | |
51 message_data_.push_back(message_data); | |
52 } | |
53 | |
54 bool TestPostMessage::MakeOnMessageEcho(const std::string& expression) { | |
55 std::string js_code( | |
56 "document.getElementById('plugin').onmessage = function(message_event) {" | |
57 " document.getElementById('plugin').postMessage("); | |
58 js_code += expression; | |
59 js_code += ");}"; | |
60 pp::Var exception; | |
61 // TODO(dmichael): Move ExecuteScript to the testing interface. | |
62 instance_->ExecuteScript(js_code, &exception); | |
63 return(exception.is_undefined()); | |
64 } | |
65 | |
66 std::string TestPostMessage::TestSendingData() { | |
67 // Set up the JavaScript onmessage handler to echo the data part of the | |
68 // message event back to us. | |
69 ASSERT_TRUE(MakeOnMessageEcho("message_event.data")); | |
70 | |
71 // Test sending a message to JavaScript for each supported type. The JS sends | |
72 // the data back to us, and we check that they match. | |
73 message_data_.clear(); | |
74 instance_->PostMessage(pp::Var(kTestString)); | |
75 // Note that the trusted in-process version is completely synchronous, so we | |
76 // do not need to use 'RunMessageLoop' to wait. | |
77 ASSERT_EQ(message_data_.size(), 1); | |
78 ASSERT_TRUE(message_data_.back().is_string()); | |
79 ASSERT_EQ(message_data_.back().AsString(), kTestString); | |
80 | |
81 message_data_.clear(); | |
82 instance_->PostMessage(pp::Var(kTestBool)); | |
83 ASSERT_EQ(message_data_.size(), 1); | |
84 ASSERT_TRUE(message_data_.back().is_bool()); | |
85 ASSERT_EQ(message_data_.back().AsBool(), kTestBool); | |
86 | |
87 message_data_.clear(); | |
88 instance_->PostMessage(pp::Var(kTestInt)); | |
89 ASSERT_EQ(message_data_.size(), 1); | |
90 ASSERT_TRUE(message_data_.back().is_number()); | |
91 ASSERT_DOUBLE_EQ(message_data_.back().AsDouble(), | |
92 static_cast<double>(kTestInt)); | |
93 | |
94 message_data_.clear(); | |
95 instance_->PostMessage(pp::Var(kTestDouble)); | |
96 ASSERT_EQ(message_data_.size(), 1); | |
97 ASSERT_TRUE(message_data_.back().is_number()); | |
98 ASSERT_DOUBLE_EQ(message_data_.back().AsDouble(), kTestDouble); | |
99 | |
100 message_data_.clear(); | |
101 instance_->PostMessage(pp::Var()); | |
102 ASSERT_EQ(message_data_.size(), 1); | |
103 ASSERT_TRUE(message_data_.back().is_undefined()); | |
104 | |
105 message_data_.clear(); | |
106 instance_->PostMessage(pp::Var(pp::Var::Null())); | |
107 ASSERT_EQ(message_data_.size(), 1); | |
108 ASSERT_TRUE(message_data_.back().is_null()); | |
109 | |
110 return std::string(); | |
111 } | |
112 | |
113 std::string TestPostMessage::TestMessageEvent() { | |
114 // Set up the JavaScript onmessage handler to pass us some values from the | |
115 // MessageEvent and make sure they match our expectations. | |
116 | |
117 // Have onmessage pass back the type of message_event and make sure it's | |
118 // "object". | |
119 ASSERT_TRUE(MakeOnMessageEcho("typeof(message_event)")); | |
120 message_data_.clear(); | |
121 instance_->PostMessage(pp::Var(kTestInt)); | |
122 ASSERT_EQ(message_data_.size(), 1); | |
123 ASSERT_TRUE(message_data_.back().is_string()); | |
124 ASSERT_EQ(message_data_.back().AsString(), "object"); | |
125 | |
126 // Make sure all the non-data properties have the expected values. | |
127 bool success = MakeOnMessageEcho("((message_event.origin == '')" | |
128 " && (message_event.lastEventId == '')" | |
129 " && (message_event.source == null)" | |
130 " && (message_event.ports == null)" | |
131 " && (message_event.bubbles == false)" | |
132 " && (message_event.cancelable == false)" | |
133 ")"); | |
134 ASSERT_TRUE(success); | |
135 message_data_.clear(); | |
136 instance_->PostMessage(pp::Var(kTestInt)); | |
137 // Note that the trusted in-process version is completely synchronous, so we | |
138 // do not need to use 'RunMessageLoop' to wait. | |
139 ASSERT_EQ(message_data_.size(), 1); | |
140 ASSERT_TRUE(message_data_.back().is_bool()); | |
141 ASSERT_TRUE(message_data_.back().AsBool()); | |
142 | |
143 return std::string(); | |
144 } | |
145 | |
146 std::string TestPostMessage::TestNoHandler() { | |
147 // Delete the onmessage handler (if it exists) | |
148 std::string js_code( | |
149 "if (document.getElementById('plugin').onmessage) {" | |
150 " delete document.getElementById('plugin').onmessage;" | |
151 "}"); | |
152 pp::Var exception; | |
153 instance_->ExecuteScript(js_code, &exception); | |
154 ASSERT_TRUE(exception.is_undefined()); | |
155 | |
156 // Now send a message and make sure we don't get anything back (and that we | |
157 // don't crash). | |
158 message_data_.clear(); | |
159 instance_->PostMessage(pp::Var()); | |
160 ASSERT_EQ(message_data_.size(), 0); | |
161 return std::string(); | |
162 } | |
163 | |
OLD | NEW |