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

Side by Side Diff: chrome/utility/image_decoder_impl_unittest.cc

Issue 2475543003: Introduce the image_decoder service (Closed)
Patch Set: . Created 4 years, 1 month 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
« no previous file with comments | « chrome/utility/image_decoder_impl.cc ('k') | services/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "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 mojom {
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, bool shrink) {
43 decoder_->DecodeImage(
44 image, ImageCodec::DEFAULT, shrink,
45 base::Bind(&Request::OnRequestDone, base::Unretained(this)));
46 }
47
48 const SkBitmap& bitmap() const { return bitmap_; }
49
50 private:
51 void OnRequestDone(const SkBitmap& result_image) { bitmap_ = result_image; }
52
53 ImageDecoderImpl* decoder_;
54 SkBitmap bitmap_;
55 };
56
57 } // namespace
58
59 // Test that DecodeImage() doesn't return image message > (max message size)
60 TEST(ImageDecoderImplTest, DecodeImageSizeLimit) {
61 // Using actual limit generates 14000 x 9400 images, which causes the test to
62 // timeout. We test with a smaller limit for efficiency.
63 const size_t kTestMessageSize = IPC::Channel::kMaximumMessageSize / 1024;
64
65 ImageDecoderImpl decoder(kTestMessageSize);
66
67 // Approx max height for 3:2 image that will fit in IPC message;
68 // 1.5 for width/height ratio, 4 for bytes/pixel
69 int max_height_for_msg = sqrt(kTestMessageSize / (1.5 * 4));
70 int base_msg_size = sizeof(skia::mojom::Bitmap::Data_);
71
72 // Sizes which should trigger dimension-halving 0, 1 and 2 times
73 int heights[] = {max_height_for_msg - 10,
74 max_height_for_msg + 10,
75 2 * max_height_for_msg + 10};
76 int widths[] = {heights[0] * 3 / 2, heights[1] * 3 / 2, heights[2] * 3 / 2};
77 for (size_t i = 0; i < arraysize(heights); i++) {
78 std::vector<unsigned char> jpg;
79 ASSERT_TRUE(CreateJPEGImage(widths[i], heights[i], SK_ColorRED, &jpg));
80
81 Request request(&decoder);
82 request.DecodeImage(jpg, true);
83 ASSERT_FALSE(request.bitmap().isNull());
84
85 // Check that image has been shrunk appropriately
86 EXPECT_LT(request.bitmap().computeSize64() + base_msg_size,
87 static_cast<int64_t>(kTestMessageSize));
88 // Android does its own image shrinking for memory conservation deeper in
89 // the decode, so more specific tests here won't work.
90 #if !defined(OS_ANDROID)
91 EXPECT_EQ(widths[i] >> i, request.bitmap().width());
92 EXPECT_EQ(heights[i] >> i, request.bitmap().height());
93
94 // Check that if resize not requested and image exceeds IPC size limit,
95 // an empty image is returned
96 if (heights[i] > max_height_for_msg) {
97 Request request(&decoder);
98 request.DecodeImage(jpg, false);
99 EXPECT_TRUE(request.bitmap().isNull());
100 }
101 #endif
102 }
103 }
104
105 TEST(ImageDecoderImplTest, DecodeImageFailed) {
106 ImageDecoderImpl decoder(IPC::Channel::kMaximumMessageSize);
107
108 // The "jpeg" is just some "random" data;
109 const char kRandomData[] = "u gycfy7xdjkhfgui bdui ";
110 std::vector<unsigned char> jpg(kRandomData,
111 kRandomData + sizeof(kRandomData));
112
113 Request request(&decoder);
114 request.DecodeImage(jpg, false);
115 EXPECT_TRUE(request.bitmap().isNull());
116 }
117
118 } // namespace mojom
OLDNEW
« no previous file with comments | « chrome/utility/image_decoder_impl.cc ('k') | services/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698