[email protected] | 3252338 | 2011-06-10 02:30:00 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 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_buffer.h" |
| 6 | |
| 7 | #include "ppapi/c/dev/ppb_buffer_dev.h" |
| 8 | #include "ppapi/cpp/dev/buffer_dev.h" |
| 9 | #include "ppapi/cpp/graphics_2d.h" |
| 10 | #include "ppapi/cpp/instance.h" |
| 11 | #include "ppapi/cpp/module.h" |
| 12 | #include "ppapi/tests/testing_instance.h" |
| 13 | |
| 14 | REGISTER_TEST_CASE(Buffer); |
| 15 | |
| 16 | bool TestBuffer::Init() { |
[email protected] | 4d52923 | 2011-12-08 22:31:21 | [diff] [blame] | 17 | buffer_interface_ = static_cast<const PPB_Buffer_Dev*>( |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 18 | pp::Module::Get()->GetBrowserInterface(PPB_BUFFER_DEV_INTERFACE)); |
| 19 | return !!buffer_interface_; |
| 20 | } |
| 21 | |
[email protected] | 2622d6b | 2011-11-16 04:28:02 | [diff] [blame] | 22 | void TestBuffer::RunTests(const std::string& filter) { |
[email protected] | 76aa16e | 2013-02-19 23:36:22 | [diff] [blame] | 23 | RUN_TEST(InvalidSize, filter); |
| 24 | RUN_TEST(InitToZero, filter); |
| 25 | RUN_TEST(IsBuffer, filter); |
| 26 | RUN_TEST(BasicLifeCycle, filter); |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | std::string TestBuffer::TestInvalidSize() { |
[email protected] | 859a7f3 | 2011-01-15 03:44:13 | [diff] [blame] | 30 | pp::Buffer_Dev zero_size(instance_, 0); |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 31 | if (!zero_size.is_null()) |
| 32 | return "Zero size accepted"; |
| 33 | |
[email protected] | ce8b30e | 2011-01-07 21:27:31 | [diff] [blame] | 34 | PASS(); |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | std::string TestBuffer::TestInitToZero() { |
[email protected] | 859a7f3 | 2011-01-15 03:44:13 | [diff] [blame] | 38 | pp::Buffer_Dev buffer(instance_, 100); |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 39 | if (buffer.is_null()) |
| 40 | return "Could not create buffer"; |
| 41 | |
| 42 | if (buffer.size() != 100) |
| 43 | return "Buffer size not as expected"; |
| 44 | |
| 45 | // Now check that everything is 0. |
| 46 | unsigned char* bytes = static_cast<unsigned char *>(buffer.data()); |
[email protected] | 867b76d63 | 2010-12-02 00:09:07 | [diff] [blame] | 47 | for (uint32_t index = 0; index < buffer.size(); index++) { |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 48 | if (bytes[index] != 0) |
| 49 | return "Buffer isn't entirely zero"; |
| 50 | } |
| 51 | |
[email protected] | ce8b30e | 2011-01-07 21:27:31 | [diff] [blame] | 52 | PASS(); |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | std::string TestBuffer::TestIsBuffer() { |
| 56 | // Test that a NULL resource isn't a buffer. |
| 57 | pp::Resource null_resource; |
| 58 | if (buffer_interface_->IsBuffer(null_resource.pp_resource())) |
| 59 | return "Null resource was reported as a valid buffer"; |
| 60 | |
| 61 | // Make another resource type and test it. |
| 62 | const int w = 16, h = 16; |
[email protected] | 7ca87c2 | 2011-01-07 05:33:20 | [diff] [blame] | 63 | pp::Graphics2D device(instance_, pp::Size(w, h), true); |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 64 | if (device.is_null()) |
| 65 | return "Couldn't create device context"; |
| 66 | if (buffer_interface_->IsBuffer(device.pp_resource())) |
| 67 | return "Device context was reported as a buffer"; |
| 68 | |
| 69 | // Make a valid buffer. |
[email protected] | 859a7f3 | 2011-01-15 03:44:13 | [diff] [blame] | 70 | pp::Buffer_Dev buffer(instance_, 100); |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 71 | if (buffer.is_null()) |
| 72 | return "Couldn't create buffer"; |
| 73 | if (!buffer_interface_->IsBuffer(buffer.pp_resource())) |
| 74 | return "Buffer should be identified as a buffer"; |
| 75 | |
[email protected] | ce8b30e | 2011-01-07 21:27:31 | [diff] [blame] | 76 | PASS(); |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 77 | } |
| 78 | |
[email protected] | 3252338 | 2011-06-10 02:30:00 | [diff] [blame] | 79 | std::string TestBuffer::TestBasicLifeCycle() { |
| 80 | enum { kBufferSize = 100 }; |
| 81 | |
| 82 | pp::Buffer_Dev *buffer = new pp::Buffer_Dev(instance_, kBufferSize); |
| 83 | if (buffer->is_null() || |
| 84 | !buffer_interface_->IsBuffer(buffer->pp_resource()) || |
| 85 | buffer->size() != kBufferSize) { |
| 86 | return "Error creating buffer (earlier test should have failed)"; |
| 87 | } |
| 88 | |
| 89 | // Test that the buffer got created & mapped. |
| 90 | if (buffer->data() == NULL) |
| 91 | return "Failed to Map() buffer"; |
| 92 | |
| 93 | // Test that the buffer is writeable. |
| 94 | char* data = static_cast<char*>(buffer->data()); |
| 95 | for (int i = 0; i < kBufferSize; ++i) |
| 96 | data[i] = 'X'; |
| 97 | |
[email protected] | 81fc59f | 2011-06-14 19:28:41 | [diff] [blame] | 98 | // Implicitly test that the copy constructor doesn't cause a double-unmap on |
| 99 | // delete. |
| 100 | pp::Buffer_Dev* copy = new pp::Buffer_Dev(*buffer); |
| 101 | |
[email protected] | 3252338 | 2011-06-10 02:30:00 | [diff] [blame] | 102 | // Implicitly test that destroying the buffer doesn't encounter a fatal error |
| 103 | // in Unmap. |
| 104 | delete buffer; |
| 105 | |
[email protected] | 81fc59f | 2011-06-14 19:28:41 | [diff] [blame] | 106 | // Test that we can still write to copy's copy of the data. |
| 107 | char* copy_data = static_cast<char*>(copy->data()); |
| 108 | for (int i = 0; i < kBufferSize; ++i) |
| 109 | copy_data[i] = 'Y'; |
| 110 | |
| 111 | delete copy; |
| 112 | |
[email protected] | 3252338 | 2011-06-10 02:30:00 | [diff] [blame] | 113 | PASS(); |
| 114 | } |