OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 <stddef.h> | |
6 #include <stdint.h> | |
7 | |
8 #include "base/macros.h" | |
9 #include "build/build_config.h" | |
10 #include "chrome/common/chrome_utility_messages.h" | |
11 #include "chrome/utility/chrome_content_utility_client.h" | |
12 #include "ipc/ipc_channel.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 #include "ui/gfx/codec/jpeg_codec.h" | |
15 | |
16 namespace { | |
17 | |
18 bool CreateJPEGImage(int width, | |
19 int height, | |
20 SkColor color, | |
21 std::vector<unsigned char>* output) { | |
22 SkBitmap bitmap; | |
23 bitmap.allocN32Pixels(width, height); | |
24 bitmap.eraseColor(color); | |
25 | |
26 const int kQuality = 50; | |
27 if (!gfx::JPEGCodec::Encode( | |
28 static_cast<const unsigned char*>(bitmap.getPixels()), | |
29 gfx::JPEGCodec::FORMAT_SkBitmap, | |
30 width, | |
31 height, | |
32 bitmap.rowBytes(), | |
33 kQuality, | |
34 output)) { | |
35 LOG(ERROR) << "Unable to encode " << width << "x" << height << " bitmap"; | |
36 return false; | |
37 } | |
38 return true; | |
39 } | |
40 | |
41 } // namespace | |
42 | |
43 typedef testing::Test ChromeContentUtilityClientTest; | |
44 | |
45 // Test that DecodeImage() doesn't return image message > (max message size) | |
46 TEST_F(ChromeContentUtilityClientTest, DecodeImageSizeLimit) { | |
47 // Using actual limit generates 14000 x 9400 images, which causes the test to | |
48 // timeout. We test with a smaller limit for efficiency. | |
49 const size_t kTestMessageSize = IPC::Channel::kMaximumMessageSize / 1024; | |
50 ChromeContentUtilityClient::set_max_ipc_message_size_for_test( | |
51 kTestMessageSize); | |
52 // Approx max height for 3:2 image that will fit in IPC message; | |
53 // 1.5 for width/height ratio, 4 for bytes/pixel | |
54 int max_height_for_msg = sqrt(kTestMessageSize / (1.5 * 4)); | |
55 int base_msg_size = sizeof(ChromeUtilityHostMsg_DecodeImage_Succeeded); | |
56 | |
57 // Sizes which should trigger dimension-halving 0, 1 and 2 times | |
58 int heights[] = { max_height_for_msg - 10, | |
59 max_height_for_msg + 10, | |
60 2 * max_height_for_msg + 10 }; | |
61 int widths[] = { heights[0] * 3 / 2, heights[1] * 3 / 2, heights[2] * 3 / 2 }; | |
62 for (size_t i = 0; i < arraysize(heights); i++) { | |
63 std::vector<unsigned char> jpg; | |
64 CreateJPEGImage(widths[i], heights[i], SK_ColorRED, &jpg); | |
65 SkBitmap bitmap = ChromeContentUtilityClient::DecodeImage(jpg, true); | |
66 | |
67 // Check that image has been shrunk appropriately | |
68 EXPECT_LT(bitmap.computeSize64() + base_msg_size, | |
69 static_cast<int64_t>(kTestMessageSize)); | |
70 // Android does its own image shrinking for memory conservation deeper in | |
71 // the decode, so more specific tests here won't work. | |
72 #if !defined(OS_ANDROID) | |
73 EXPECT_EQ(widths[i] >> i, bitmap.width()); | |
74 EXPECT_EQ(heights[i] >> i, bitmap.height()); | |
75 | |
76 // Check that if resize not requested and image exceeds IPC size limit, | |
77 // an empty image is returned | |
78 if (heights[i] > max_height_for_msg) { | |
79 SkBitmap empty_bmp = ChromeContentUtilityClient::DecodeImage(jpg, false); | |
80 EXPECT_TRUE(empty_bmp.empty()); | |
81 } | |
82 #endif | |
83 } | |
84 } | |
OLD | NEW |