| 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 <memory> | |
| 6 #include <vector> | |
| 7 | |
| 8 #include "base/bind.h" | |
| 9 #include "base/lazy_instance.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "gin/array_buffer.h" | |
| 12 #include "gin/public/isolate_holder.h" | |
| 13 #include "services/image_decoder/image_decoder_impl.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 #include "third_party/WebKit/public/platform/scheduler/utility/webthread_impl_fo
r_utility_thread.h" | |
| 16 #include "third_party/WebKit/public/web/WebKit.h" | |
| 17 #include "third_party/skia/include/core/SkBitmap.h" | |
| 18 #include "ui/gfx/codec/jpeg_codec.h" | |
| 19 | |
| 20 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) | |
| 21 #include "gin/v8_initializer.h" | |
| 22 #endif | |
| 23 | |
| 24 namespace image_decoder { | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 const int64_t kTestMaxImageSize = 128 * 1024; | |
| 29 | |
| 30 bool CreateJPEGImage(int width, | |
| 31 int height, | |
| 32 SkColor color, | |
| 33 std::vector<unsigned char>* output) { | |
| 34 SkBitmap bitmap; | |
| 35 bitmap.allocN32Pixels(width, height); | |
| 36 bitmap.eraseColor(color); | |
| 37 | |
| 38 const int kQuality = 50; | |
| 39 if (!gfx::JPEGCodec::Encode( | |
| 40 static_cast<const unsigned char*>(bitmap.getPixels()), | |
| 41 gfx::JPEGCodec::FORMAT_SkBitmap, width, height, | |
| 42 static_cast<int>(bitmap.rowBytes()), kQuality, output)) { | |
| 43 LOG(ERROR) << "Unable to encode " << width << "x" << height << " bitmap"; | |
| 44 return false; | |
| 45 } | |
| 46 return true; | |
| 47 } | |
| 48 | |
| 49 class Request { | |
| 50 public: | |
| 51 explicit Request(ImageDecoderImpl* decoder) : decoder_(decoder) {} | |
| 52 | |
| 53 void DecodeImage(const std::vector<unsigned char>& image, bool shrink) { | |
| 54 decoder_->DecodeImage( | |
| 55 image, mojom::ImageCodec::DEFAULT, shrink, kTestMaxImageSize, | |
| 56 gfx::Size(), // Take the smallest frame (there's only one frame). | |
| 57 base::Bind(&Request::OnRequestDone, base::Unretained(this))); | |
| 58 } | |
| 59 | |
| 60 const SkBitmap& bitmap() const { return bitmap_; } | |
| 61 | |
| 62 private: | |
| 63 void OnRequestDone(const SkBitmap& result_image) { bitmap_ = result_image; } | |
| 64 | |
| 65 ImageDecoderImpl* decoder_; | |
| 66 SkBitmap bitmap_; | |
| 67 }; | |
| 68 | |
| 69 // We need to ensure that Blink and V8 are initialized in order to use content's | |
| 70 // image decoding call. | |
| 71 class BlinkInitializer : public blink::Platform { | |
| 72 public: | |
| 73 BlinkInitializer() | |
| 74 : main_thread_(new blink::scheduler::WebThreadImplForUtilityThread()) { | |
| 75 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) | |
| 76 gin::V8Initializer::LoadV8Snapshot(); | |
| 77 gin::V8Initializer::LoadV8Natives(); | |
| 78 #endif | |
| 79 | |
| 80 blink::initialize(this); | |
| 81 } | |
| 82 | |
| 83 ~BlinkInitializer() override {} | |
| 84 | |
| 85 blink::WebThread* currentThread() override { return main_thread_.get(); } | |
| 86 | |
| 87 private: | |
| 88 std::unique_ptr<blink::scheduler::WebThreadImplForUtilityThread> main_thread_; | |
| 89 | |
| 90 DISALLOW_COPY_AND_ASSIGN(BlinkInitializer); | |
| 91 }; | |
| 92 | |
| 93 base::LazyInstance<BlinkInitializer>::Leaky g_blink_initializer = | |
| 94 LAZY_INSTANCE_INITIALIZER; | |
| 95 | |
| 96 class ImageDecoderImplTest : public testing::Test { | |
| 97 public: | |
| 98 ImageDecoderImplTest() : decoder_(nullptr) {} | |
| 99 ~ImageDecoderImplTest() override {} | |
| 100 | |
| 101 void SetUp() override { g_blink_initializer.Get(); } | |
| 102 | |
| 103 protected: | |
| 104 ImageDecoderImpl* decoder() { return &decoder_; } | |
| 105 | |
| 106 private: | |
| 107 base::MessageLoop message_loop_; | |
| 108 ImageDecoderImpl decoder_; | |
| 109 }; | |
| 110 | |
| 111 } // namespace | |
| 112 | |
| 113 // Test that DecodeImage() doesn't return image message > (max message size) | |
| 114 TEST_F(ImageDecoderImplTest, DecodeImageSizeLimit) { | |
| 115 // Approx max height for 3:2 image that will fit in the allotted space. | |
| 116 // 1.5 for width/height ratio, 4 for bytes/pixel. | |
| 117 int max_height_for_msg = sqrt(kTestMaxImageSize / (1.5 * 4)); | |
| 118 int base_msg_size = sizeof(skia::mojom::Bitmap::Data_); | |
| 119 | |
| 120 // Sizes which should trigger dimension-halving 0, 1 and 2 times | |
| 121 int heights[] = {max_height_for_msg - 10, | |
| 122 max_height_for_msg + 10, | |
| 123 2 * max_height_for_msg + 10}; | |
| 124 int widths[] = {heights[0] * 3 / 2, heights[1] * 3 / 2, heights[2] * 3 / 2}; | |
| 125 for (size_t i = 0; i < arraysize(heights); i++) { | |
| 126 std::vector<unsigned char> jpg; | |
| 127 ASSERT_TRUE(CreateJPEGImage(widths[i], heights[i], SK_ColorRED, &jpg)); | |
| 128 | |
| 129 Request request(decoder()); | |
| 130 request.DecodeImage(jpg, true); | |
| 131 ASSERT_FALSE(request.bitmap().isNull()); | |
| 132 | |
| 133 // Check that image has been shrunk appropriately | |
| 134 EXPECT_LT(request.bitmap().computeSize64() + base_msg_size, | |
| 135 kTestMaxImageSize); | |
| 136 // Android does its own image shrinking for memory conservation deeper in | |
| 137 // the decode, so more specific tests here won't work. | |
| 138 #if !defined(OS_ANDROID) | |
| 139 EXPECT_EQ(widths[i] >> i, request.bitmap().width()); | |
| 140 EXPECT_EQ(heights[i] >> i, request.bitmap().height()); | |
| 141 | |
| 142 // Check that if resize not requested and image exceeds IPC size limit, | |
| 143 // an empty image is returned | |
| 144 if (heights[i] > max_height_for_msg) { | |
| 145 Request request(decoder()); | |
| 146 request.DecodeImage(jpg, false); | |
| 147 EXPECT_TRUE(request.bitmap().isNull()); | |
| 148 } | |
| 149 #endif | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 TEST_F(ImageDecoderImplTest, DecodeImageFailed) { | |
| 154 // The "jpeg" is just some "random" data; | |
| 155 const char kRandomData[] = "u gycfy7xdjkhfgui bdui "; | |
| 156 std::vector<unsigned char> jpg(kRandomData, | |
| 157 kRandomData + sizeof(kRandomData)); | |
| 158 | |
| 159 Request request(decoder()); | |
| 160 request.DecodeImage(jpg, false); | |
| 161 EXPECT_TRUE(request.bitmap().isNull()); | |
| 162 } | |
| 163 | |
| 164 } // namespace image_decoder | |
| OLD | NEW |