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.
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() {
17  buffer_interface_ = static_cast<const PPB_Buffer_Dev*>(
18      pp::Module::Get()->GetBrowserInterface(PPB_BUFFER_DEV_INTERFACE));
19  return !!buffer_interface_;
20}
21
22void 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);
27}
28
29std::string TestBuffer::TestInvalidSize() {
30  pp::Buffer_Dev zero_size(instance_, 0);
31  if (!zero_size.is_null())
32    return "Zero size accepted";
33
34  PASS();
35}
36
37std::string TestBuffer::TestInitToZero() {
38  pp::Buffer_Dev buffer(instance_, 100);
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());
47  for (uint32_t index = 0; index < buffer.size(); index++) {
48    if (bytes[index] != 0)
49      return "Buffer isn't entirely zero";
50  }
51
52  PASS();
53}
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;
63  pp::Graphics2D device(instance_, pp::Size(w, h), true);
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.
70  pp::Buffer_Dev buffer(instance_, 100);
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
76  PASS();
77}
78
79std::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
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
102  // Implicitly test that destroying the buffer doesn't encounter a fatal error
103  // in Unmap.
104  delete buffer;
105
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
113  PASS();
114}
115