OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "chrome/utility/image_decoder_impl.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "ipc/ipc_channel.h" |
| 11 #include "skia/public/type_converters.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/skia/include/core/SkBitmap.h" |
| 14 #include "ui/gfx/codec/jpeg_codec.h" |
| 15 |
| 16 namespace mojom { |
| 17 |
| 18 namespace { |
| 19 |
| 20 bool CreateJPEGImage(int width, |
| 21 int height, |
| 22 SkColor color, |
| 23 std::vector<unsigned char>* output) { |
| 24 SkBitmap bitmap; |
| 25 bitmap.allocN32Pixels(width, height); |
| 26 bitmap.eraseColor(color); |
| 27 |
| 28 const int kQuality = 50; |
| 29 if (!gfx::JPEGCodec::Encode( |
| 30 static_cast<const unsigned char*>(bitmap.getPixels()), |
| 31 gfx::JPEGCodec::FORMAT_SkBitmap, width, height, |
| 32 static_cast<int>(bitmap.rowBytes()), kQuality, output)) { |
| 33 LOG(ERROR) << "Unable to encode " << width << "x" << height << " bitmap"; |
| 34 return false; |
| 35 } |
| 36 return true; |
| 37 } |
| 38 |
| 39 class Request { |
| 40 public: |
| 41 explicit Request(ImageDecoderImpl* decoder) : decoder_(decoder) {} |
| 42 |
| 43 void DecodeImage(const std::vector<unsigned char>& image, bool shrink) { |
| 44 decoder_->DecodeImage( |
| 45 mojo::Array<uint8_t>::From(image), ImageCodec::DEFAULT, |
| 46 shrink, base::Bind(&Request::OnRequestDone, base::Unretained(this))); |
| 47 } |
| 48 |
| 49 const skia::mojom::BitmapPtr& bitmap() const { return bitmap_; } |
| 50 |
| 51 private: |
| 52 void OnRequestDone(skia::mojom::BitmapPtr result_image) { |
| 53 bitmap_ = std::move(result_image); |
| 54 } |
| 55 |
| 56 ImageDecoderImpl* decoder_; |
| 57 skia::mojom::BitmapPtr bitmap_; |
| 58 }; |
| 59 |
| 60 } // namespace |
| 61 |
| 62 // Test that DecodeImage() doesn't return image message > (max message size) |
| 63 TEST(ImageDecoderImplTest, DecodeImageSizeLimit) { |
| 64 // Using actual limit generates 14000 x 9400 images, which causes the test to |
| 65 // timeout. We test with a smaller limit for efficiency. |
| 66 const size_t kTestMessageSize = IPC::Channel::kMaximumMessageSize / 1024; |
| 67 |
| 68 ImageDecoderImpl decoder(kTestMessageSize); |
| 69 |
| 70 // Approx max height for 3:2 image that will fit in IPC message; |
| 71 // 1.5 for width/height ratio, 4 for bytes/pixel |
| 72 int max_height_for_msg = sqrt(kTestMessageSize / (1.5 * 4)); |
| 73 int base_msg_size = sizeof(skia::mojom::Bitmap::Data_); |
| 74 |
| 75 // Sizes which should trigger dimension-halving 0, 1 and 2 times |
| 76 int heights[] = {max_height_for_msg - 10, |
| 77 max_height_for_msg + 10, |
| 78 2 * max_height_for_msg + 10}; |
| 79 int widths[] = {heights[0] * 3 / 2, heights[1] * 3 / 2, heights[2] * 3 / 2}; |
| 80 for (size_t i = 0; i < arraysize(heights); i++) { |
| 81 std::vector<unsigned char> jpg; |
| 82 ASSERT_TRUE(CreateJPEGImage(widths[i], heights[i], SK_ColorRED, &jpg)); |
| 83 |
| 84 Request request(&decoder); |
| 85 request.DecodeImage(jpg, true); |
| 86 ASSERT_FALSE(request.bitmap().is_null()); |
| 87 SkBitmap bitmap = request.bitmap().To<SkBitmap>(); |
| 88 |
| 89 // Check that image has been shrunk appropriately |
| 90 EXPECT_LT(bitmap.computeSize64() + base_msg_size, |
| 91 static_cast<int64_t>(kTestMessageSize)); |
| 92 // Android does its own image shrinking for memory conservation deeper in |
| 93 // the decode, so more specific tests here won't work. |
| 94 #if !defined(OS_ANDROID) |
| 95 EXPECT_EQ(widths[i] >> i, bitmap.width()); |
| 96 EXPECT_EQ(heights[i] >> i, bitmap.height()); |
| 97 |
| 98 // Check that if resize not requested and image exceeds IPC size limit, |
| 99 // an empty image is returned |
| 100 if (heights[i] > max_height_for_msg) { |
| 101 Request request(&decoder); |
| 102 request.DecodeImage(jpg, false); |
| 103 EXPECT_TRUE(request.bitmap().is_null()); |
| 104 } |
| 105 #endif |
| 106 } |
| 107 } |
| 108 |
| 109 TEST(ImageDecoderImplTest, DecodeImageFailed) { |
| 110 ImageDecoderImpl decoder(IPC::Channel::kMaximumMessageSize); |
| 111 |
| 112 // The "jpeg" is just some "random" data; |
| 113 const char kRandomData[] = "u gycfy7xdjkhfgui bdui "; |
| 114 std::vector<unsigned char> jpg(kRandomData, |
| 115 kRandomData + sizeof(kRandomData)); |
| 116 |
| 117 Request request(&decoder); |
| 118 request.DecodeImage(jpg, false); |
| 119 EXPECT_TRUE(request.bitmap().is_null()); |
| 120 } |
| 121 |
| 122 } // namespace mojom |
OLD | NEW |