Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
|
David Springer
2010/11/19 00:14:21
Is the removal of these files necessary for this C
dmichael(do not use this one)
2010/11/19 18:53:27
It's not strictly necessary, since the script is n
| |
| 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_instance.h" | |
| 6 | |
| 7 #include "ppapi/cpp/device_context_2d.h" | |
| 8 #include "ppapi/cpp/image_data.h" | |
| 9 #include "ppapi/cpp/instance.h" | |
| 10 #include "ppapi/cpp/module.h" | |
| 11 | |
| 12 class Instance : public TestInstance { | |
| 13 public: | |
| 14 Instance(PP_Instance instance) : TestInstance(instance) {} | |
| 15 | |
| 16 virtual bool Init(size_t argc, const char* argn[], const char* argv[]) { | |
| 17 image_data_interface_ = reinterpret_cast<PPB_ImageData const*>( | |
| 18 pp::Module::Get()->GetBrowserInterface(PPB_IMAGEDATA_INTERFACE)); | |
| 19 return !!image_data_interface_; | |
| 20 } | |
| 21 | |
| 22 virtual std::string GetTestCaseName() const { | |
| 23 return std::string("ImageData"); | |
| 24 } | |
| 25 | |
| 26 virtual void RunTest() { | |
| 27 LogTest("InvalidFormat", TestInvalidFormat()); | |
| 28 LogTest("InvalidSize", TestInvalidSize()); | |
| 29 LogTest("HugeSize", TestHugeSize()); | |
| 30 LogTest("InitToZero", TestInitToZero()); | |
| 31 LogTest("IsImageData", TestIsImageData()); | |
| 32 } | |
| 33 | |
| 34 private: | |
| 35 std::string TestInvalidFormat() { | |
| 36 pp::ImageData a(static_cast<PP_ImageDataFormat>(1337), 16, 16, true); | |
| 37 if (!a.is_null()) | |
| 38 return "Crazy image data format accepted"; | |
| 39 | |
| 40 pp::ImageData b(static_cast<PP_ImageDataFormat>(-1), 16, 16, true); | |
| 41 if (!b.is_null()) | |
| 42 return "Negative image data format accepted"; | |
| 43 | |
| 44 return ""; | |
| 45 } | |
| 46 | |
| 47 std::string TestInvalidSize() { | |
| 48 pp::ImageData zero_size(PP_IMAGEDATAFORMAT_BGRA_PREMUL, 0, 0, true); | |
| 49 if (!zero_size.is_null()) | |
| 50 return "Zero width and height accepted"; | |
| 51 | |
| 52 pp::ImageData zero_height(PP_IMAGEDATAFORMAT_BGRA_PREMUL, 16, 0, true); | |
| 53 if (!zero_height.is_null()) | |
| 54 return "Zero height accepted"; | |
| 55 | |
| 56 pp::ImageData zero_width(PP_IMAGEDATAFORMAT_BGRA_PREMUL, 0, 16, true); | |
| 57 if (!zero_width.is_null()) | |
| 58 return "Zero width accepted"; | |
| 59 | |
| 60 pp::ImageData negative_height(PP_IMAGEDATAFORMAT_BGRA_PREMUL, 16, -2, true); | |
| 61 if (!negative_height.is_null()) | |
| 62 return "Negative height accepted"; | |
| 63 | |
| 64 pp::ImageData negative_width(PP_IMAGEDATAFORMAT_BGRA_PREMUL, -2, 16, true); | |
| 65 if (!negative_width.is_null()) | |
| 66 return "Negative width accepted"; | |
| 67 | |
| 68 return ""; | |
| 69 } | |
| 70 | |
| 71 std::string TestHugeSize() { | |
| 72 pp::ImageData huge_size(PP_IMAGEDATAFORMAT_BGRA_PREMUL, | |
| 73 100000000, 100000000, true); | |
| 74 if (!huge_size.is_null()) | |
| 75 return "31-bit overflow size accepted"; | |
| 76 return ""; | |
| 77 } | |
| 78 | |
| 79 std::string TestInitToZero() { | |
| 80 const int w = 5; | |
| 81 const int h = 6; | |
| 82 pp::ImageData img(PP_IMAGEDATAFORMAT_BGRA_PREMUL, w, h, true); | |
| 83 if (img.is_null()) | |
| 84 return "Could not create bitmap"; | |
| 85 | |
| 86 // Basic validity checking of the bitmap. This also tests "describe" since | |
| 87 // that's where the image data object got its imfo from. | |
| 88 if (img.width() != w || img.height() != h) | |
| 89 return "Wrong size"; | |
| 90 if (img.format() != PP_IMAGEDATAFORMAT_BGRA_PREMUL) | |
| 91 return "Wrong format"; | |
| 92 if (img.stride() < w * 4) | |
| 93 return "Stride too small"; | |
| 94 | |
| 95 // Now check that everything is 0. | |
| 96 for (int y = 0; y < h; y++) { | |
| 97 uint32_t* row = img.GetAddr32(0, y); | |
| 98 for (int x = 0; x < w; x++) { | |
| 99 if (row[x] != 0) | |
| 100 return "Image data isn't entirely zero"; | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 return ""; | |
| 105 } | |
| 106 | |
| 107 std::string TestIsImageData() { | |
| 108 // Test that a NULL resource isn't an image data. | |
| 109 pp::Resource null_resource; | |
| 110 if (image_data_interface_->IsImageData(null_resource.pp_resource())) | |
| 111 return "Null resource was reported as a valid image"; | |
| 112 | |
| 113 // Make another resource type and test it. | |
| 114 const int w = 16, h = 16; | |
| 115 pp::DeviceContext2D device(w, h, true); | |
| 116 if (device.is_null()) | |
| 117 return "Couldn't create device context"; | |
| 118 if (image_data_interface_->IsImageData(device.pp_resource())) | |
| 119 return "Device context was reported as an image"; | |
| 120 | |
| 121 // Make a valid image resource. | |
| 122 pp::ImageData img(PP_IMAGEDATAFORMAT_BGRA_PREMUL, w, h, true); | |
| 123 if (img.is_null()) | |
| 124 return "Couldn't create image data"; | |
| 125 if (!image_data_interface_->IsImageData(img.pp_resource())) | |
| 126 return "Image data should be identified as an image"; | |
| 127 | |
| 128 return ""; | |
| 129 } | |
| 130 | |
| 131 // Used by the tests that access the C API directly. | |
| 132 const PPB_ImageData* image_data_interface_; | |
| 133 }; | |
| 134 | |
| 135 class Module : public pp::Module { | |
| 136 public: | |
| 137 Module() : pp::Module() {} | |
| 138 virtual ~Module() {} | |
| 139 | |
| 140 virtual pp::Instance* CreateInstance(PP_Instance instance) { | |
| 141 return new Instance(instance); | |
| 142 } | |
| 143 }; | |
| 144 | |
| 145 namespace pp { | |
| 146 | |
| 147 Module* CreateModule() { | |
| 148 return new ::Module(); | |
| 149 } | |
| 150 | |
| 151 } // namespace pp | |
| OLD | NEW |