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

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

Issue 9022021: Proxy PPB_ArrayBuffer_Dev, make them work over Messaging (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add testing for multiple sizes of array. Created 8 years, 12 months 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
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 #include <sstream>
8 9
9 #include "ppapi/c/dev/ppb_testing_dev.h" 10 #include "ppapi/c/dev/ppb_testing_dev.h"
10 #include "ppapi/c/pp_var.h" 11 #include "ppapi/c/pp_var.h"
11 #include "ppapi/cpp/dev/var_array_buffer_dev.h" 12 #include "ppapi/cpp/dev/var_array_buffer_dev.h"
12 #include "ppapi/cpp/instance.h" 13 #include "ppapi/cpp/instance.h"
13 #include "ppapi/cpp/var.h" 14 #include "ppapi/cpp/var.h"
14 #include "ppapi/tests/pp_thread.h" 15 #include "ppapi/tests/pp_thread.h"
15 #include "ppapi/tests/test_utils.h" 16 #include "ppapi/tests/test_utils.h"
16 #include "ppapi/tests/testing_instance.h" 17 #include "ppapi/tests/testing_instance.h"
17 18
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 PASS(); 244 PASS();
244 } 245 }
245 246
246 std::string TestPostMessage::TestSendingArrayBuffer() { 247 std::string TestPostMessage::TestSendingArrayBuffer() {
247 // Clean up after previous tests. This also swallows the message sent by Init 248 // Clean up after previous tests. This also swallows the message sent by Init
248 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' 249 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit'
249 // should start with these. 250 // should start with these.
250 WaitForMessages(); 251 WaitForMessages();
251 ASSERT_TRUE(ClearListeners()); 252 ASSERT_TRUE(ClearListeners());
252 253
253 // Create a 100-byte array buffer with test_data[i] == i. 254 // TODO(dmichael): Add testing of longer array buffers when crbug.com/106266
254 pp::VarArrayBuffer_Dev test_data(100u); 255 // is fixed.
255 ASSERT_NE(NULL, test_data.Map()); 256 uint32_t sizes[] = { 0, 100, 1000 };
256 ASSERT_EQ(100u, test_data.ByteLength()); 257 for (size_t i = 0; i < sizeof(sizes)/sizeof(sizes[i]); ++i) {
257 unsigned char* buff = static_cast<unsigned char*>(test_data.Map()); 258 // Create an appropriately sized array buffer with test_data[i] == i.
258 for (size_t i = 0; i < test_data.ByteLength(); ++i) 259 pp::VarArrayBuffer_Dev test_data(sizes[i]);
259 buff[i] = i; 260 if (sizes[i] > 0)
261 ASSERT_NE(NULL, test_data.Map());
262 ASSERT_EQ(sizes[i], test_data.ByteLength());
263 unsigned char* buff = static_cast<unsigned char*>(test_data.Map());
264 const uint32_t kByteLength = test_data.ByteLength();
265 for (size_t j = 0; j < kByteLength; ++j)
266 buff[j] = static_cast<uint8_t>(j % 256u);
267 std::ostringstream size_stream;
268 size_stream << sizes[i];
269 const std::string kSizeAsString(size_stream.str());
260 270
261 // Have the listener test some properties of the ArrayBuffer. 271 // Have the listener test some properties of the ArrayBuffer.
262 const char* const properties_to_check[] = { 272 std::vector<std::string> properties_to_check;
263 "message_event.data.constructor.name === 'ArrayBuffer'", 273 properties_to_check.push_back(
264 "message_event.data.byteLength === 100", 274 "message_event.data.constructor.name === 'ArrayBuffer'");
265 "(new DataView(message_event.data)).getInt8(0) == 0", 275 properties_to_check.push_back(
266 "(new DataView(message_event.data)).getInt8(99) == 99", 276 std::string("message_event.data.byteLength === ") + kSizeAsString);
267 NULL}; 277 if (sizes[i] > 0) {
268 for (size_t i = 0; properties_to_check[i]; ++i) { 278 properties_to_check.push_back(
269 ASSERT_TRUE(AddEchoingListener(properties_to_check[i])); 279 "(new DataView(message_event.data)).getUint8(0) == 0");
280 // Checks that the last element has the right value: (byteLength-1)%256.
281 properties_to_check.push_back(
282 "(new DataView(message_event.data))."
283 " getUint8(message_event.data.byteLength-1) =="
284 " (message_event.data.byteLength-1)%256");
285 }
286 for (std::vector<std::string>::iterator iter = properties_to_check.begin();
287 iter != properties_to_check.end();
288 ++iter) {
289 ASSERT_TRUE(AddEchoingListener(*iter));
290 message_data_.clear();
291 instance_->PostMessage(test_data);
292 ASSERT_EQ(message_data_.size(), 0);
293 ASSERT_EQ(WaitForMessages(), 1);
294 ASSERT_TRUE(message_data_.back().is_bool());
295 if (!message_data_.back().AsBool())
296 return std::string("Failed: ") + *iter + ", size: " + kSizeAsString;
297 ASSERT_TRUE(message_data_.back().AsBool());
298 ASSERT_TRUE(ClearListeners());
299 }
300
301 // Set up the JavaScript message event listener to echo the data part of the
302 // message event back to us.
303 ASSERT_TRUE(AddEchoingListener("message_event.data"));
270 message_data_.clear(); 304 message_data_.clear();
271 instance_->PostMessage(test_data); 305 instance_->PostMessage(test_data);
306 // PostMessage is asynchronous, so we should not receive a response yet.
272 ASSERT_EQ(message_data_.size(), 0); 307 ASSERT_EQ(message_data_.size(), 0);
273 ASSERT_EQ(WaitForMessages(), 1); 308 ASSERT_EQ(WaitForMessages(), 1);
274 ASSERT_TRUE(message_data_.back().is_bool()); 309 ASSERT_TRUE(message_data_.back().is_array_buffer());
275 if (!message_data_.back().AsBool()) 310 pp::VarArrayBuffer_Dev received(message_data_.back());
276 return std::string("Failed: ") + properties_to_check[i]; 311 message_data_.clear();
277 ASSERT_TRUE(message_data_.back().AsBool()); 312 ASSERT_EQ(test_data.ByteLength(), received.ByteLength());
313 unsigned char* received_buff = static_cast<unsigned char*>(received.Map());
314 // The buffer should be copied, so this should be a distinct buffer. When
315 // 'transferrables' are implemented for PPAPI, we'll also want to test that
316 // we get the _same_ buffer back when it's transferred.
317 if (sizes[i] > 0)
318 ASSERT_NE(buff, received_buff);
319 for (size_t i = 0; i < test_data.ByteLength(); ++i)
320 ASSERT_EQ(buff[i], received_buff[i]);
321
322 message_data_.clear();
278 ASSERT_TRUE(ClearListeners()); 323 ASSERT_TRUE(ClearListeners());
279 } 324 }
280 325
281 // Set up the JavaScript message event listener to echo the data part of the
282 // message event back to us.
283 ASSERT_TRUE(AddEchoingListener("message_event.data"));
284 message_data_.clear();
285 instance_->PostMessage(test_data);
286 // PostMessage is asynchronous, so we should not receive a response yet.
287 ASSERT_EQ(message_data_.size(), 0);
288 ASSERT_EQ(WaitForMessages(), 1);
289 ASSERT_TRUE(message_data_.back().is_array_buffer());
290 pp::VarArrayBuffer_Dev received(message_data_.back());
291 message_data_.clear();
292 ASSERT_EQ(test_data.ByteLength(), received.ByteLength());
293 unsigned char* received_buff = static_cast<unsigned char*>(received.Map());
294 // The buffer should be copied, so this should be a distinct buffer. When
295 // 'transferrables' are implemented for PPAPI, we'll also want to test that
296 // we get the _same_ buffer back when it's transferred.
297 ASSERT_NE(buff, received_buff);
298 for (size_t i = 0; i < test_data.ByteLength(); ++i)
299 ASSERT_EQ(buff[i], received_buff[i]);
300
301 message_data_.clear();
302 ASSERT_TRUE(ClearListeners());
303
304 PASS(); 326 PASS();
305 } 327 }
306 328
307 std::string TestPostMessage::TestMessageEvent() { 329 std::string TestPostMessage::TestMessageEvent() {
308 // Set up the JavaScript message event listener to pass us some values from 330 // Set up the JavaScript message event listener to pass us some values from
309 // the MessageEvent and make sure they match our expectations. 331 // the MessageEvent and make sure they match our expectations.
310 332
311 WaitForMessages(); 333 WaitForMessages();
312 ASSERT_TRUE(ClearListeners()); 334 ASSERT_TRUE(ClearListeners());
313 // Have the listener pass back the class name of message_event and make sure 335 // Have the listener pass back the class name of message_event and make sure
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
455 ++received_counts[received_value]; 477 ++received_counts[received_value];
456 } 478 }
457 ASSERT_EQ(received_counts, expected_counts); 479 ASSERT_EQ(received_counts, expected_counts);
458 480
459 message_data_.clear(); 481 message_data_.clear();
460 ASSERT_TRUE(ClearListeners()); 482 ASSERT_TRUE(ClearListeners());
461 483
462 PASS(); 484 PASS();
463 } 485 }
464 486
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698