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 instance_
->LogTest("InvalidFormat", TestInvalidFormat());
23 instance_
->LogTest("InvalidSize", TestInvalidSize());
24 instance_
->LogTest("HugeSize", TestHugeSize());
25 instance_
->LogTest("InitToZero", TestInitToZero());
26 instance_
->LogTest("IsImageData", TestIsImageData());
29 std::string
TestImageData::TestInvalidFormat() {
30 pp::ImageData
a(instance_
, static_cast<PP_ImageDataFormat
>(1337),
31 pp::Size(16, 16), true);
33 return "Crazy image data format accepted";
35 pp::ImageData
b(instance_
, static_cast<PP_ImageDataFormat
>(-1),
36 pp::Size(16, 16), true);
38 return "Negative image data format accepted";
43 std::string
TestImageData::TestInvalidSize() {
44 pp::ImageData
zero_size(instance_
, PP_IMAGEDATAFORMAT_BGRA_PREMUL
,
45 pp::Size(0, 0), true);
46 if (!zero_size
.is_null())
47 return "Zero width and height accepted";
49 pp::ImageData
zero_height(instance_
, PP_IMAGEDATAFORMAT_BGRA_PREMUL
,
50 pp::Size(16, 0), true);
51 if (!zero_height
.is_null())
52 return "Zero height accepted";
54 pp::ImageData
zero_width(instance_
, PP_IMAGEDATAFORMAT_BGRA_PREMUL
,
55 pp::Size(0, 16), true);
56 if (!zero_width
.is_null())
57 return "Zero width accepted";
59 PP_Size negative_height
;
60 negative_height
.width
= 16;
61 negative_height
.height
= -2;
62 PP_Resource rsrc
= image_data_interface_
->Create(
63 instance_
->pp_instance(),
64 PP_IMAGEDATAFORMAT_BGRA_PREMUL
,
65 &negative_height
, PP_TRUE
);
67 return "Negative height accepted";
69 PP_Size negative_width
;
70 negative_width
.width
= -2;
71 negative_width
.height
= 16;
72 rsrc
= image_data_interface_
->Create(
73 instance_
->pp_instance(),
74 PP_IMAGEDATAFORMAT_BGRA_PREMUL
,
75 &negative_width
, PP_TRUE
);
77 return "Negative width accepted";
82 std::string
TestImageData::TestHugeSize() {
83 pp::ImageData
huge_size(instance_
, PP_IMAGEDATAFORMAT_BGRA_PREMUL
,
84 pp::Size(100000000, 100000000), true);
85 if (!huge_size
.is_null())
86 return "31-bit overflow size accepted";
90 std::string
TestImageData::TestInitToZero() {
93 pp::ImageData
img(instance_
, PP_IMAGEDATAFORMAT_BGRA_PREMUL
,
94 pp::Size(w
, h
), true);
96 return "Could not create bitmap";
98 // Basic validity checking of the bitmap. This also tests "describe" since
99 // that's where the image data object got its imfo from.
100 if (img
.size().width() != w
|| img
.size().height() != h
)
102 if (img
.format() != PP_IMAGEDATAFORMAT_BGRA_PREMUL
)
103 return "Wrong format";
104 if (img
.stride() < w
* 4)
105 return "Stride too small";
107 // Now check that everything is 0.
108 for (int y
= 0; y
< h
; y
++) {
109 uint32_t* row
= img
.GetAddr32(pp::Point(0, y
));
110 for (int x
= 0; x
< w
; x
++) {
112 return "Image data isn't entirely zero";
119 std::string
TestImageData::TestIsImageData() {
120 // Test that a NULL resource isn't an image data.
121 pp::Resource null_resource
;
122 if (image_data_interface_
->IsImageData(null_resource
.pp_resource()))
123 return "Null resource was reported as a valid image";
125 // Make another resource type and test it.
126 const int w
= 16, h
= 16;
127 pp::Graphics2D
device(instance_
, pp::Size(w
, h
), true);
128 if (device
.is_null())
129 return "Couldn't create device context";
130 if (image_data_interface_
->IsImageData(device
.pp_resource()))
131 return "Device context was reported as an image";
133 // Make a valid image resource.
134 pp::ImageData
img(instance_
, PP_IMAGEDATAFORMAT_BGRA_PREMUL
,
135 pp::Size(w
, h
), true);
137 return "Couldn't create image data";
138 if (!image_data_interface_
->IsImageData(img
.pp_resource()))
139 return "Image data should be identified as an image";