Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(715)

Side by Side Diff: ppapi/tests/test_post_message.cc

Issue 8920005: Add TestingInstance::EvalScript method which posts a message that the test page can eval(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ppapi/tests/test_input_event.cc ('k') | ppapi/tests/testing_instance.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ppapi/tests/test_post_message.h" 5 #include "ppapi/tests/test_post_message.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "ppapi/c/dev/ppb_testing_dev.h" 9 #include "ppapi/c/dev/ppb_testing_dev.h"
10 #include "ppapi/c/pp_var.h" 10 #include "ppapi/c/pp_var.h"
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 } 58 }
59 59
60 TestPostMessage::~TestPostMessage() { 60 TestPostMessage::~TestPostMessage() {
61 // Remove the special listener that only responds to a FINISHED_WAITING 61 // Remove the special listener that only responds to a FINISHED_WAITING
62 // string. See Init for where it gets added. 62 // string. See Init for where it gets added.
63 std::string js_code; 63 std::string js_code;
64 js_code += "var plugin = document.getElementById('plugin');" 64 js_code += "var plugin = document.getElementById('plugin');"
65 "plugin.removeEventListener('message'," 65 "plugin.removeEventListener('message',"
66 " plugin.wait_for_messages_handler);" 66 " plugin.wait_for_messages_handler);"
67 "delete plugin.wait_for_messages_handler;"; 67 "delete plugin.wait_for_messages_handler;";
68 pp::Var exception; 68 instance_->EvalScript(js_code);
69 instance_->ExecuteScript(js_code, &exception);
70 } 69 }
71 70
72 bool TestPostMessage::Init() { 71 bool TestPostMessage::Init() {
73 bool success = InitTestingInterface(); 72 bool success = InitTestingInterface();
74 73
75 // Set up a special listener that only responds to a FINISHED_WAITING string. 74 // Set up a special listener that only responds to a FINISHED_WAITING string.
76 // This is for use by WaitForMessages. 75 // This is for use by WaitForMessages.
77 std::string js_code; 76 std::string js_code;
78 // Note the following code is dependent on some features of test_case.html. 77 // Note the following code is dependent on some features of test_case.html.
79 // E.g., it is assumed that the DOM element where the plugin is embedded has 78 // E.g., it is assumed that the DOM element where the plugin is embedded has
80 // an id of 'plugin', and there is a function 'IsTestingMessage' that allows 79 // an id of 'plugin', and there is a function 'IsTestingMessage' that allows
81 // us to ignore the messages that are intended for use by the testing 80 // us to ignore the messages that are intended for use by the testing
82 // framework itself. 81 // framework itself.
83 js_code += "var plugin = document.getElementById('plugin');" 82 js_code += "var plugin = document.getElementById('plugin');"
84 "var wait_for_messages_handler = function(message_event) {" 83 "var wait_for_messages_handler = function(message_event) {"
85 " if (!IsTestingMessage(message_event.data) &&" 84 " if (!IsTestingMessage(message_event.data) &&"
86 " message_event.data === '" FINISHED_WAITING_MESSAGE "') {" 85 " message_event.data === '" FINISHED_WAITING_MESSAGE "') {"
87 " plugin.postMessage('" FINISHED_WAITING_MESSAGE "');" 86 " plugin.postMessage('" FINISHED_WAITING_MESSAGE "');"
88 " }" 87 " }"
89 "};" 88 "};"
90 "plugin.addEventListener('message', wait_for_messages_handler);" 89 "plugin.addEventListener('message', wait_for_messages_handler);"
91 // Stash it on the plugin so we can remove it in the destructor. 90 // Stash it on the plugin so we can remove it in the destructor.
92 "plugin.wait_for_messages_handler = wait_for_messages_handler;"; 91 "plugin.wait_for_messages_handler = wait_for_messages_handler;";
93 pp::Var exception; 92 instance_->EvalScript(js_code);
94 instance_->ExecuteScript(js_code, &exception);
95 success = success && exception.is_undefined();
96 93
97 // Set up the JavaScript message event listener to echo the data part of the 94 // Set up the JavaScript message event listener to echo the data part of the
98 // message event back to us. 95 // message event back to us.
99 success = success && AddEchoingListener("message_event.data"); 96 success = success && AddEchoingListener("message_event.data");
100 message_data_.clear(); 97 message_data_.clear();
101 // Send a message that the first test will expect to receive. This is to 98 // Send a message that the first test will expect to receive. This is to
102 // verify that we can send messages when the 'Instance::Init' function is on 99 // verify that we can send messages when the 'Instance::Init' function is on
103 // the stack. 100 // the stack.
104 instance_->PostMessage(pp::Var(kTestString)); 101 instance_->PostMessage(pp::Var(kTestString));
105 102
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 js_code += expression; 139 js_code += expression;
143 js_code += " );" 140 js_code += " );"
144 " }" 141 " }"
145 "};" 142 "};"
146 "plugin.addEventListener('message', message_handler);" 143 "plugin.addEventListener('message', message_handler);"
147 // Maintain an array of all event listeners, attached to the 144 // Maintain an array of all event listeners, attached to the
148 // plugin. This is so that we can easily remove them later (see 145 // plugin. This is so that we can easily remove them later (see
149 // ClearListeners()). 146 // ClearListeners()).
150 "if (!plugin.eventListeners) plugin.eventListeners = [];" 147 "if (!plugin.eventListeners) plugin.eventListeners = [];"
151 "plugin.eventListeners.push(message_handler);"; 148 "plugin.eventListeners.push(message_handler);";
152 pp::Var exception; 149 instance_->EvalScript(js_code);
153 instance_->ExecuteScript(js_code, &exception); 150 return true;
154 return exception.is_undefined();
155 } 151 }
156 152
157 bool TestPostMessage::ClearListeners() { 153 bool TestPostMessage::ClearListeners() {
158 std::string js_code( 154 std::string js_code;
159 "var plugin = document.getElementById('plugin');" 155 js_code += "var plugin = document.getElementById('plugin');"
160 "while (plugin.eventListeners.length) {" 156 "while (plugin.eventListeners.length) {"
161 " plugin.removeEventListener('message', plugin.eventListeners.pop());" 157 " plugin.removeEventListener('message',"
162 "}"); 158 " plugin.eventListeners.pop());"
163 pp::Var exception; 159 "}";
164 instance_->ExecuteScript(js_code, &exception); 160 instance_->EvalScript(js_code);
165 return(exception.is_undefined()); 161 return true;
166 } 162 }
167 163
168 int TestPostMessage::WaitForMessages() { 164 int TestPostMessage::WaitForMessages() {
169 size_t message_size_before = message_data_.size(); 165 size_t message_size_before = message_data_.size();
170 // We first post a FINISHED_WAITING_MESSAGE. This should be guaranteed to 166 // We first post a FINISHED_WAITING_MESSAGE. This should be guaranteed to
171 // come back _after_ any other incoming messages that were already pending. 167 // come back _after_ any other incoming messages that were already pending.
172 instance_->PostMessage(pp::Var(FINISHED_WAITING_MESSAGE)); 168 instance_->PostMessage(pp::Var(FINISHED_WAITING_MESSAGE));
173 testing_interface_->RunMessageLoop(instance_->pp_instance()); 169 testing_interface_->RunMessageLoop(instance_->pp_instance());
174 // Now that the FINISHED_WAITING_MESSAGE has been echoed back to us, we know 170 // Now that the FINISHED_WAITING_MESSAGE has been echoed back to us, we know
175 // that all pending messages have been slurped up. Return the number we 171 // that all pending messages have been slurped up. Return the number we
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 } 444 }
449 ASSERT_TRUE(received_value >= 0); 445 ASSERT_TRUE(received_value >= 0);
450 ASSERT_TRUE(received_value <= kThreadsToRun); 446 ASSERT_TRUE(received_value <= kThreadsToRun);
451 ++received_counts[received_value]; 447 ++received_counts[received_value];
452 } 448 }
453 ASSERT_EQ(received_counts, expected_counts); 449 ASSERT_EQ(received_counts, expected_counts);
454 450
455 PASS(); 451 PASS();
456 } 452 }
457 453
OLDNEW
« no previous file with comments | « ppapi/tests/test_input_event.cc ('k') | ppapi/tests/testing_instance.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698