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