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_image_data.h" 6 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" 12 13 REGISTER_TEST_CASE(ImageData); 14 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_; 19 } 20 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); 29 } 30 31 std::string TestImageData::TestInvalidFormat() { 32 pp::ImageData a(instance_, static_cast<PP_ImageDataFormat>(1337), 33 pp::Size(16, 16), true); 34 if (!a.is_null()) 35 return "Crazy image data format accepted"; 36 37 pp::ImageData b(instance_, static_cast<PP_ImageDataFormat>(-1), 38 pp::Size(16, 16), true); 39 if (!b.is_null()) 40 return "Negative image data format accepted"; 41 PASS(); 42 } 43 44 std::string TestImageData::SubTestFormatSupported(PP_ImageDataFormat format) { 45 if (!pp::ImageData::IsImageDataFormatSupported(format)) 46 return "ImageData::IsImageDataFormatSupported(format) returned false"; 47 PASS(); 48 } 49 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)); 55 PASS(); 56 } 57 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"; 62 PASS(); 63 } 64 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"; 69 70 pp::ImageData zero_height(instance_, format, pp::Size(16, 0), true); 71 if (!zero_height.is_null()) 72 return "Zero height accepted"; 73 74 pp::ImageData zero_width(instance_, format, pp::Size(0, 16), true); 75 if (!zero_width.is_null()) 76 return "Zero width accepted"; 77 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(), 83 format, 84 &negative_height, PP_TRUE); 85 if (rsrc) 86 return "Negative height accepted"; 87 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(), 93 format, 94 &negative_width, PP_TRUE); 95 if (rsrc) 96 return "Negative width accepted"; 97 98 PASS(); 99 } 100 101 std::string TestImageData::TestInvalidSize() { 102 ASSERT_SUBTEST_SUCCESS(SubTestInvalidSize(PP_IMAGEDATAFORMAT_BGRA_PREMUL)); 103 ASSERT_SUBTEST_SUCCESS(SubTestInvalidSize(PP_IMAGEDATAFORMAT_RGBA_PREMUL)); 104 PASS(); 105 } 106 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"; 112 PASS(); 113 } 114 115 std::string TestImageData::TestHugeSize() { 116 ASSERT_SUBTEST_SUCCESS(SubTestHugeSize(PP_IMAGEDATAFORMAT_BGRA_PREMUL)); 117 ASSERT_SUBTEST_SUCCESS(SubTestHugeSize(PP_IMAGEDATAFORMAT_RGBA_PREMUL)); 118 PASS(); 119 } 120 121 std::string TestImageData::SubTestInitToZero(PP_ImageDataFormat format) { 122 const int w = 5; 123 const int h = 6; 124 pp::ImageData img(instance_, format, pp::Size(w, h), true); 125 if (img.is_null()) 126 return "Could not create bitmap"; 127 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) 131 return "Wrong size"; 132 if (img.format() != format) 133 return "Wrong format"; 134 if (img.stride() < w * 4) 135 return "Stride too small"; 136 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++) { 141 if (row[x] != 0) 142 return "Image data isn't entirely zero"; 143 } 144 } 145 146 PASS(); 147 } 148 149 std::string TestImageData::TestInitToZero() { 150 ASSERT_SUBTEST_SUCCESS(SubTestInitToZero(PP_IMAGEDATAFORMAT_BGRA_PREMUL)); 151 ASSERT_SUBTEST_SUCCESS(SubTestInitToZero(PP_IMAGEDATAFORMAT_RGBA_PREMUL)); 152 PASS(); 153 } 154 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); 159 if (img.is_null()) 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"; 163 PASS(); 164 } 165 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"; 171 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"; 179 180 ASSERT_SUBTEST_SUCCESS(SubTestIsImageData(PP_IMAGEDATAFORMAT_BGRA_PREMUL)); 181 ASSERT_SUBTEST_SUCCESS(SubTestIsImageData(PP_IMAGEDATAFORMAT_RGBA_PREMUL)); 182 PASS(); 183 } 184