blob: d143043bb6ae9e060bc6992e732287f50a7da35e [file] [log] [blame]
[email protected]32523382011-06-10 02:30:001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]1758e882010-11-01 16:16:502// 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
14REGISTER_TEST_CASE(Buffer);
15
16bool TestBuffer::Init() {
[email protected]4d529232011-12-08 22:31:2117 buffer_interface_ = static_cast<const PPB_Buffer_Dev*>(
[email protected]1758e882010-11-01 16:16:5018 pp::Module::Get()->GetBrowserInterface(PPB_BUFFER_DEV_INTERFACE));
19 return !!buffer_interface_;
20}
21
[email protected]2622d6b2011-11-16 04:28:0222void TestBuffer::RunTests(const std::string& filter) {
[email protected]76aa16e2013-02-19 23:36:2223 RUN_TEST(InvalidSize, filter);
24 RUN_TEST(InitToZero, filter);
25 RUN_TEST(IsBuffer, filter);
26 RUN_TEST(BasicLifeCycle, filter);
[email protected]1758e882010-11-01 16:16:5027}
28
29std::string TestBuffer::TestInvalidSize() {
[email protected]859a7f32011-01-15 03:44:1330 pp::Buffer_Dev zero_size(instance_, 0);
[email protected]1758e882010-11-01 16:16:5031 if (!zero_size.is_null())
32 return "Zero size accepted";
33
[email protected]ce8b30e2011-01-07 21:27:3134 PASS();
[email protected]1758e882010-11-01 16:16:5035}
36
37std::string TestBuffer::TestInitToZero() {
[email protected]859a7f32011-01-15 03:44:1338 pp::Buffer_Dev buffer(instance_, 100);
[email protected]1758e882010-11-01 16:16:5039 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]867b76d632010-12-02 00:09:0747 for (uint32_t index = 0; index < buffer.size(); index++) {
[email protected]1758e882010-11-01 16:16:5048 if (bytes[index] != 0)
49 return "Buffer isn't entirely zero";
50 }
51
[email protected]ce8b30e2011-01-07 21:27:3152 PASS();
[email protected]1758e882010-11-01 16:16:5053}
54
55std::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]7ca87c22011-01-07 05:33:2063 pp::Graphics2D device(instance_, pp::Size(w, h), true);
[email protected]1758e882010-11-01 16:16:5064 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]859a7f32011-01-15 03:44:1370 pp::Buffer_Dev buffer(instance_, 100);
[email protected]1758e882010-11-01 16:16:5071 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]ce8b30e2011-01-07 21:27:3176 PASS();
[email protected]1758e882010-11-01 16:16:5077}
78
[email protected]32523382011-06-10 02:30:0079std::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]81fc59f2011-06-14 19:28:4198 // 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]32523382011-06-10 02:30:00102 // Implicitly test that destroying the buffer doesn't encounter a fatal error
103 // in Unmap.
104 delete buffer;
105
[email protected]81fc59f2011-06-14 19:28:41106 // 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]32523382011-06-10 02:30:00113 PASS();
114}