OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/utility/image_decoder_impl.h" | 5 #include "chrome/utility/image_decoder_impl.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/message_loop/message_loop.h" | |
11 #include "ipc/ipc_channel.h" | 10 #include "ipc/ipc_channel.h" |
12 #include "skia/public/type_converters.h" | 11 #include "skia/public/type_converters.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "third_party/skia/include/core/SkBitmap.h" | 13 #include "third_party/skia/include/core/SkBitmap.h" |
15 #include "ui/gfx/codec/jpeg_codec.h" | 14 #include "ui/gfx/codec/jpeg_codec.h" |
16 | 15 |
17 namespace mojom { | 16 namespace mojom { |
18 | 17 |
19 namespace { | 18 namespace { |
20 | 19 |
(...skipping 30 matching lines...) Expand all Loading... |
51 | 50 |
52 private: | 51 private: |
53 void OnRequestDone(skia::mojom::BitmapPtr result_image) { | 52 void OnRequestDone(skia::mojom::BitmapPtr result_image) { |
54 bitmap_ = std::move(result_image); | 53 bitmap_ = std::move(result_image); |
55 } | 54 } |
56 | 55 |
57 ImageDecoderImpl* decoder_; | 56 ImageDecoderImpl* decoder_; |
58 skia::mojom::BitmapPtr bitmap_; | 57 skia::mojom::BitmapPtr bitmap_; |
59 }; | 58 }; |
60 | 59 |
61 class ImageDecoderImplTest : public testing::Test { | |
62 protected: | |
63 ~ImageDecoderImplTest() override {} | |
64 | |
65 base::MessageLoop message_loop_; | |
66 }; | |
67 | |
68 } // namespace | 60 } // namespace |
69 | 61 |
70 // Test that DecodeImage() doesn't return image message > (max message size) | 62 // Test that DecodeImage() doesn't return image message > (max message size) |
71 TEST_F(ImageDecoderImplTest, DecodeImageSizeLimit) { | 63 TEST(ImageDecoderImplTest, DecodeImageSizeLimit) { |
72 // Using actual limit generates 14000 x 9400 images, which causes the test to | 64 // Using actual limit generates 14000 x 9400 images, which causes the test to |
73 // timeout. We test with a smaller limit for efficiency. | 65 // timeout. We test with a smaller limit for efficiency. |
74 const size_t kTestMessageSize = IPC::Channel::kMaximumMessageSize / 1024; | 66 const size_t kTestMessageSize = IPC::Channel::kMaximumMessageSize / 1024; |
75 | 67 |
76 ImageDecoderImpl decoder(kTestMessageSize); | 68 ImageDecoderImpl decoder(kTestMessageSize); |
77 | 69 |
78 // Approx max height for 3:2 image that will fit in IPC message; | 70 // Approx max height for 3:2 image that will fit in IPC message; |
79 // 1.5 for width/height ratio, 4 for bytes/pixel | 71 // 1.5 for width/height ratio, 4 for bytes/pixel |
80 int max_height_for_msg = sqrt(kTestMessageSize / (1.5 * 4)); | 72 int max_height_for_msg = sqrt(kTestMessageSize / (1.5 * 4)); |
81 int base_msg_size = sizeof(skia::mojom::Bitmap::Data_); | 73 int base_msg_size = sizeof(skia::mojom::Bitmap::Data_); |
(...skipping 25 matching lines...) Expand all Loading... |
107 // an empty image is returned | 99 // an empty image is returned |
108 if (heights[i] > max_height_for_msg) { | 100 if (heights[i] > max_height_for_msg) { |
109 Request request(&decoder); | 101 Request request(&decoder); |
110 request.DecodeImage(jpg, false); | 102 request.DecodeImage(jpg, false); |
111 EXPECT_TRUE(request.bitmap().is_null()); | 103 EXPECT_TRUE(request.bitmap().is_null()); |
112 } | 104 } |
113 #endif | 105 #endif |
114 } | 106 } |
115 } | 107 } |
116 | 108 |
117 TEST_F(ImageDecoderImplTest, DecodeImageFailed) { | 109 TEST(ImageDecoderImplTest, DecodeImageFailed) { |
118 ImageDecoderImpl decoder(IPC::Channel::kMaximumMessageSize); | 110 ImageDecoderImpl decoder(IPC::Channel::kMaximumMessageSize); |
119 | 111 |
120 // The "jpeg" is just some "random" data; | 112 // The "jpeg" is just some "random" data; |
121 const char kRandomData[] = "u gycfy7xdjkhfgui bdui "; | 113 const char kRandomData[] = "u gycfy7xdjkhfgui bdui "; |
122 std::vector<unsigned char> jpg(kRandomData, | 114 std::vector<unsigned char> jpg(kRandomData, |
123 kRandomData + sizeof(kRandomData)); | 115 kRandomData + sizeof(kRandomData)); |
124 | 116 |
125 Request request(&decoder); | 117 Request request(&decoder); |
126 request.DecodeImage(jpg, false); | 118 request.DecodeImage(jpg, false); |
127 EXPECT_TRUE(request.bitmap().is_null()); | 119 EXPECT_TRUE(request.bitmap().is_null()); |
128 } | 120 } |
129 | 121 |
130 } // namespace mojom | 122 } // namespace mojom |
OLD | NEW |