Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: components/image_decoder/image_decoder_impl_unittest.cc

Issue 1028543002: Turn the utility process image decoder into a Mojo service. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase and address comments. Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "components/image_decoder/image_decoder_impl.h"
5
6 #include <vector>
7
8 #include "base/bind.h"
9 #include "ipc/ipc_channel.h"
10 #include "skia/public/type_converters.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "ui/gfx/codec/jpeg_codec.h"
14
15 namespace image_decoder {
16
17 namespace {
18
19 bool CreateJPEGImage(int width,
20 int height,
21 SkColor color,
22 std::vector<unsigned char>* output) {
23 SkBitmap bitmap;
24 bitmap.allocN32Pixels(width, height);
25 bitmap.eraseColor(color);
26
27 const int kQuality = 50;
28 if (!gfx::JPEGCodec::Encode(
29 static_cast<const unsigned char*>(bitmap.getPixels()),
30 gfx::JPEGCodec::FORMAT_SkBitmap, width, height,
31 static_cast<int>(bitmap.rowBytes()), kQuality, output)) {
32 LOG(ERROR) << "Unable to encode " << width << "x" << height << " bitmap";
33 return false;
34 }
35 return true;
36 }
37
38 class Request {
39 public:
40 explicit Request(ImageDecoderImpl* decoder) : decoder_(decoder) {}
41
42 void DecodeImage(const std::vector<unsigned char>& image,
43 bool use_robust_decoder,
44 bool shrink) {
45 decoder_->DecodeImage(
46 mojo::Array<uint8_t>::From(image),
47 use_robust_decoder ? IMAGE_CODEC_ROBUST_JPEG : IMAGE_CODEC_DEFAULT,
48 shrink, base::Bind(&Request::OnRequestDone, base::Unretained(this)));
49 }
50
51 const skia::BitmapPtr& bitmap() const { return bitmap_; }
52
53 private:
54 void OnRequestDone(skia::BitmapPtr result_image) {
55 bitmap_ = result_image.Pass();
56 }
57
58 ImageDecoderImpl* decoder_;
59 skia::BitmapPtr bitmap_;
60 };
61
62 } // namespace
63
64 typedef testing::Test ImageDecoderImplTest;
65
66 // Test that DecodeImage() doesn't return image message > (max message size)
67 TEST_F(ImageDecoderImplTest, DecodeImageSizeLimit) {
68 // Using actual limit generates 14000 x 9400 images, which causes the test to
69 // timeout. We test with a smaller limit for efficiency.
70 const size_t kTestMessageSize = IPC::Channel::kMaximumMessageSize / 1024;
71
72 ImageDecoderImpl decoder(kTestMessageSize);
73
74 // Approx max height for 3:2 image that will fit in IPC message;
75 // 1.5 for width/height ratio, 4 for bytes/pixel
76 int max_height_for_msg = sqrt(kTestMessageSize / (1.5 * 4));
77 int base_msg_size = sizeof(skia::Bitmap::Data_);
78
79 // Sizes which should trigger dimension-halving 0, 1 and 2 times
80 int heights[] = {max_height_for_msg - 10,
81 max_height_for_msg + 10,
82 2 * max_height_for_msg + 10};
83 int widths[] = {heights[0] * 3 / 2, heights[1] * 3 / 2, heights[2] * 3 / 2};
84 for (size_t i = 0; i < arraysize(heights); i++) {
85 std::vector<unsigned char> jpg;
86 ASSERT_TRUE(CreateJPEGImage(widths[i], heights[i], SK_ColorRED, &jpg));
87
88 Request request(&decoder);
89 request.DecodeImage(jpg, false, true);
90 ASSERT_FALSE(request.bitmap().is_null());
91 SkBitmap bitmap = request.bitmap().To<SkBitmap>();
92
93 // Check that image has been shrunk appropriately
94 EXPECT_LT(bitmap.computeSize64() + base_msg_size,
95 static_cast<int64_t>(kTestMessageSize));
96 // Android does its own image shrinking for memory conservation deeper in
97 // the decode, so more specific tests here won't work.
98 #if !defined(OS_ANDROID)
99 EXPECT_EQ(widths[i] >> i, bitmap.width());
100 EXPECT_EQ(heights[i] >> i, bitmap.height());
101
102 // Check that if resize not requested and image exceeds IPC size limit,
103 // an empty image is returned
104 if (heights[i] > max_height_for_msg) {
105 Request request(&decoder);
106 request.DecodeImage(jpg, false, false);
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_TRUE(request.bitmap().is_null());
124 }
125
126 } // namespace image_decoder
OLDNEW
« no previous file with comments | « components/image_decoder/image_decoder_impl.cc ('k') | components/image_decoder/public/interfaces/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698