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) {} | |
Sam McNally
2015/04/08 08:09:00
explicit
Anand Mistry (off Chromium)
2015/04/09 05:25:03
Done.
| |
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 skia::BitmapPtr bitmap; | |
Sam McNally
2015/04/08 08:09:00
No public fields in a class.
Anand Mistry (off Chromium)
2015/04/09 05:25:03
But... but... it's just a test.
| |
50 | |
51 private: | |
52 void OnRequestDone(skia::BitmapPtr result_image) { | |
53 bitmap = result_image.Pass(); | |
54 } | |
55 | |
56 ImageDecoderImpl* decoder_; | |
57 }; | |
58 | |
59 } // namespace | |
60 | |
61 typedef testing::Test ImageDecoderImplTest; | |
62 | |
63 // Test that DecodeImage() doesn't return image message > (max message size) | |
64 TEST_F(ImageDecoderImplTest, DecodeImageSizeLimit) { | |
65 // Using actual limit generates 14000 x 9400 images, which causes the test to | |
66 // timeout. We test with a smaller limit for efficiency. | |
67 const size_t kTestMessageSize = IPC::Channel::kMaximumMessageSize / 1024; | |
68 | |
69 ImageDecoderImpl decoder(kTestMessageSize); | |
70 | |
71 // Approx max height for 3:2 image that will fit in IPC message; | |
72 // 1.5 for width/height ratio, 4 for bytes/pixel | |
73 int max_height_for_msg = sqrt(kTestMessageSize / (1.5 * 4)); | |
74 int base_msg_size = sizeof(skia::Bitmap::Data_); | |
75 | |
76 // Sizes which should trigger dimension-halving 0, 1 and 2 times | |
77 int heights[] = {max_height_for_msg - 10, | |
78 max_height_for_msg + 10, | |
79 2 * max_height_for_msg + 10}; | |
80 int widths[] = {heights[0] * 3 / 2, heights[1] * 3 / 2, heights[2] * 3 / 2}; | |
81 for (size_t i = 0; i < arraysize(heights); i++) { | |
82 std::vector<unsigned char> jpg; | |
83 CreateJPEGImage(widths[i], heights[i], SK_ColorRED, &jpg); | |
Sam McNally
2015/04/08 08:09:00
ASSERT_TRUE()
Anand Mistry (off Chromium)
2015/04/09 05:25:03
Done.
| |
84 | |
85 Request request(&decoder); | |
86 request.DecodeImage(jpg, false, true); | |
87 ASSERT_FALSE(request.bitmap.is_null()); | |
88 SkBitmap bitmap = request.bitmap.To<SkBitmap>(); | |
89 | |
90 // Check that image has been shrunk appropriately | |
91 EXPECT_LT(bitmap.computeSize64() + base_msg_size, | |
92 static_cast<int64_t>(kTestMessageSize)); | |
93 // Android does its own image shrinking for memory conservation deeper in | |
94 // the decode, so more specific tests here won't work. | |
95 #if !defined(OS_ANDROID) | |
96 EXPECT_EQ(widths[i] >> i, bitmap.width()); | |
97 EXPECT_EQ(heights[i] >> i, bitmap.height()); | |
98 | |
99 // Check that if resize not requested and image exceeds IPC size limit, | |
100 // an empty image is returned | |
101 if (heights[i] > max_height_for_msg) { | |
102 Request request(&decoder); | |
103 request.DecodeImage(jpg, false, false); | |
104 EXPECT_TRUE(request.bitmap.is_null()); | |
105 } | |
106 #endif | |
107 } | |
108 } | |
109 | |
110 TEST_F(ImageDecoderImplTest, DecodeImageFailed) { | |
111 ImageDecoderImpl decoder(IPC::Channel::kMaximumMessageSize); | |
112 | |
113 // The "jpeg" is just some "random" data; | |
114 const char kRandomData[] = "u gycfy7xdjkhfgui bdui "; | |
115 std::vector<unsigned char> jpg(kRandomData, | |
116 kRandomData + sizeof(kRandomData)); | |
117 | |
118 Request request(&decoder); | |
119 request.DecodeImage(jpg, false, false); | |
120 EXPECT_TRUE(request.bitmap.is_null()); | |
121 } | |
122 | |
123 } // namespace services | |
OLD | NEW |