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_image_data.h"
7 #include "ppapi/cpp/graphics_2d.h"
8 #include "ppapi/cpp/image_data.h"
9 #include "ppapi/cpp/instance.h"
10 #include "ppapi/cpp/module.h"
11 #include "ppapi/tests/testing_instance.h"
13 REGISTER_TEST_CASE(ImageData
);
15 bool TestImageData::Init() {
16 image_data_interface_
= static_cast<const PPB_ImageData
*>(
17 pp::Module::Get()->GetBrowserInterface(PPB_IMAGEDATA_INTERFACE
));
18 return !!image_data_interface_
;
21 void TestImageData::RunTests(const std::string
& filter
) {
22 RUN_TEST(InvalidFormat
, filter
);
23 RUN_TEST(GetNativeFormat
, filter
);
24 RUN_TEST(FormatSupported
, filter
);
25 RUN_TEST(InvalidSize
, filter
);
26 RUN_TEST(HugeSize
, filter
);
27 RUN_TEST(InitToZero
, filter
);
28 RUN_TEST(IsImageData
, filter
);
31 std::string
TestImageData::TestInvalidFormat() {
32 pp::ImageData
a(instance_
, static_cast<PP_ImageDataFormat
>(1337),
33 pp::Size(16, 16), true);
35 return "Crazy image data format accepted";
37 pp::ImageData
b(instance_
, static_cast<PP_ImageDataFormat
>(-1),
38 pp::Size(16, 16), true);
40 return "Negative image data format accepted";
44 std::string
TestImageData::SubTestFormatSupported(PP_ImageDataFormat format
) {
45 if (!pp::ImageData::IsImageDataFormatSupported(format
))
46 return "ImageData::IsImageDataFormatSupported(format) returned false";
50 std::string
TestImageData::TestFormatSupported() {
51 ASSERT_SUBTEST_SUCCESS(SubTestFormatSupported(
52 PP_IMAGEDATAFORMAT_BGRA_PREMUL
));
53 ASSERT_SUBTEST_SUCCESS(SubTestFormatSupported(
54 PP_IMAGEDATAFORMAT_RGBA_PREMUL
));
58 std::string
TestImageData::TestGetNativeFormat() {
59 PP_ImageDataFormat format
= pp::ImageData::GetNativeImageDataFormat();
60 if (!pp::ImageData::IsImageDataFormatSupported(format
))
61 return "ImageData::GetNativeImageDataFormat() returned unsupported format";
65 std::string
TestImageData::SubTestInvalidSize(PP_ImageDataFormat format
) {
66 pp::ImageData
zero_size(instance_
, format
, pp::Size(0, 0), true);
67 if (!zero_size
.is_null())
68 return "Zero width and height accepted";
70 pp::ImageData
zero_height(instance_
, format
, pp::Size(16, 0), true);
71 if (!zero_height
.is_null())
72 return "Zero height accepted";
74 pp::ImageData
zero_width(instance_
, format
, pp::Size(0, 16), true);
75 if (!zero_width
.is_null())
76 return "Zero width accepted";
78 PP_Size negative_height
;
79 negative_height
.width
= 16;
80 negative_height
.height
= -2;
81 PP_Resource rsrc
= image_data_interface_
->Create(
82 instance_
->pp_instance(),
84 &negative_height
, PP_TRUE
);
86 return "Negative height accepted";
88 PP_Size negative_width
;
89 negative_width
.width
= -2;
90 negative_width
.height
= 16;
91 rsrc
= image_data_interface_
->Create(
92 instance_
->pp_instance(),
94 &negative_width
, PP_TRUE
);
96 return "Negative width accepted";
101 std::string
TestImageData::TestInvalidSize() {
102 ASSERT_SUBTEST_SUCCESS(SubTestInvalidSize(PP_IMAGEDATAFORMAT_BGRA_PREMUL
));
103 ASSERT_SUBTEST_SUCCESS(SubTestInvalidSize(PP_IMAGEDATAFORMAT_RGBA_PREMUL
));
107 std::string
TestImageData::SubTestHugeSize(PP_ImageDataFormat format
) {
108 pp::ImageData
huge_size(instance_
, format
,
109 pp::Size(100000000, 100000000), true);
110 if (!huge_size
.is_null())
111 return "31-bit overflow size accepted";
115 std::string
TestImageData::TestHugeSize() {
116 ASSERT_SUBTEST_SUCCESS(SubTestHugeSize(PP_IMAGEDATAFORMAT_BGRA_PREMUL
));
117 ASSERT_SUBTEST_SUCCESS(SubTestHugeSize(PP_IMAGEDATAFORMAT_RGBA_PREMUL
));
121 std::string
TestImageData::SubTestInitToZero(PP_ImageDataFormat format
) {
124 pp::ImageData
img(instance_
, format
, pp::Size(w
, h
), true);
126 return "Could not create bitmap";
128 // Basic validity checking of the bitmap. This also tests "describe" since
129 // that's where the image data object got its info from.
130 if (img
.size().width() != w
|| img
.size().height() != h
)
132 if (img
.format() != format
)
133 return "Wrong format";
134 if (img
.stride() < w
* 4)
135 return "Stride too small";
137 // Now check that everything is 0.
138 for (int y
= 0; y
< h
; y
++) {
139 uint32_t* row
= img
.GetAddr32(pp::Point(0, y
));
140 for (int x
= 0; x
< w
; x
++) {
142 return "Image data isn't entirely zero";
149 std::string
TestImageData::TestInitToZero() {
150 ASSERT_SUBTEST_SUCCESS(SubTestInitToZero(PP_IMAGEDATAFORMAT_BGRA_PREMUL
));
151 ASSERT_SUBTEST_SUCCESS(SubTestInitToZero(PP_IMAGEDATAFORMAT_RGBA_PREMUL
));
155 std::string
TestImageData::SubTestIsImageData(PP_ImageDataFormat format
) {
156 // Make a valid image resource.
157 const int w
= 16, h
= 16;
158 pp::ImageData
img(instance_
, format
, pp::Size(w
, h
), true);
160 return "Couldn't create image data";
161 if (!image_data_interface_
->IsImageData(img
.pp_resource()))
162 return "Image data should be identified as an image";
166 std::string
TestImageData::TestIsImageData() {
167 // Test that a NULL resource isn't an image data.
168 pp::Resource null_resource
;
169 if (image_data_interface_
->IsImageData(null_resource
.pp_resource()))
170 return "Null resource was reported as a valid image";
172 // Make another resource type and test it.
173 const int w
= 16, h
= 16;
174 pp::Graphics2D
device(instance_
, pp::Size(w
, h
), true);
175 if (device
.is_null())
176 return "Couldn't create device context";
177 if (image_data_interface_
->IsImageData(device
.pp_resource()))
178 return "Device context was reported as an image";
180 ASSERT_SUBTEST_SUCCESS(SubTestIsImageData(PP_IMAGEDATAFORMAT_BGRA_PREMUL
));
181 ASSERT_SUBTEST_SUCCESS(SubTestIsImageData(PP_IMAGEDATAFORMAT_RGBA_PREMUL
));