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 #include "ppapi/tests/test_buffer.h"
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"
14 REGISTER_TEST_CASE(Buffer
);
16 bool TestBuffer::Init() {
17 buffer_interface_
= static_cast<const PPB_Buffer_Dev
*>(
18 pp::Module::Get()->GetBrowserInterface(PPB_BUFFER_DEV_INTERFACE
));
19 return !!buffer_interface_
;
22 void TestBuffer::RunTests(const std::string
& filter
) {
23 RUN_TEST(InvalidSize
, filter
);
24 RUN_TEST(InitToZero
, filter
);
25 RUN_TEST(IsBuffer
, filter
);
26 RUN_TEST(BasicLifeCycle
, filter
);
29 std::string
TestBuffer::TestInvalidSize() {
30 pp::Buffer_Dev
zero_size(instance_
, 0);
31 if (!zero_size
.is_null())
32 return "Zero size accepted";
37 std::string
TestBuffer::TestInitToZero() {
38 pp::Buffer_Dev
buffer(instance_
, 100);
40 return "Could not create buffer";
42 if (buffer
.size() != 100)
43 return "Buffer size not as expected";
45 // Now check that everything is 0.
46 unsigned char* bytes
= static_cast<unsigned char *>(buffer
.data());
47 for (uint32_t index
= 0; index
< buffer
.size(); index
++) {
48 if (bytes
[index
] != 0)
49 return "Buffer isn't entirely zero";
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";
61 // Make another resource type and test it.
62 const int w
= 16, h
= 16;
63 pp::Graphics2D
device(instance_
, pp::Size(w
, h
), true);
65 return "Couldn't create device context";
66 if (buffer_interface_
->IsBuffer(device
.pp_resource()))
67 return "Device context was reported as a buffer";
69 // Make a valid buffer.
70 pp::Buffer_Dev
buffer(instance_
, 100);
72 return "Couldn't create buffer";
73 if (!buffer_interface_
->IsBuffer(buffer
.pp_resource()))
74 return "Buffer should be identified as a buffer";
79 std::string
TestBuffer::TestBasicLifeCycle() {
80 enum { kBufferSize
= 100 };
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)";
89 // Test that the buffer got created & mapped.
90 if (buffer
->data() == NULL
)
91 return "Failed to Map() buffer";
93 // Test that the buffer is writeable.
94 char* data
= static_cast<char*>(buffer
->data());
95 for (int i
= 0; i
< kBufferSize
; ++i
)
98 // Implicitly test that the copy constructor doesn't cause a double-unmap on
100 pp::Buffer_Dev
* copy
= new pp::Buffer_Dev(*buffer
);
102 // Implicitly test that destroying the buffer doesn't encounter a fatal error
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
)