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 "chrome/common/chrome_utility_messages.h" | |
6 #include "chrome/utility/chrome_content_utility_client.h" | |
7 #include "ipc/ipc_channel.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 #include "ui/gfx/codec/jpeg_codec.h" | |
10 | |
11 bool CreateJPEGImage(int width, | |
bshe
2014/08/29 19:56:26
nit: wrap this function in a anonymous namespace.
Greg Levin
2014/09/04 16:39:19
Done.
| |
12 int height, | |
13 SkColor color, | |
14 std::vector<unsigned char>* output) { | |
15 SkBitmap bitmap; | |
16 bitmap.allocN32Pixels(width, height); | |
17 bitmap.eraseColor(color); | |
18 | |
19 const int kQuality = 80; | |
20 if (!gfx::JPEGCodec::Encode( | |
21 static_cast<const unsigned char*>(bitmap.getPixels()), | |
22 gfx::JPEGCodec::FORMAT_SkBitmap, | |
23 width, | |
24 height, | |
25 bitmap.rowBytes(), | |
26 kQuality, | |
27 output)) { | |
28 LOG(ERROR) << "Unable to encode " << width << "x" << height << " bitmap"; | |
29 return false; | |
30 } | |
31 return true; | |
32 } | |
33 | |
34 typedef testing::Test ChromeContentUtilityClientTest; | |
35 | |
36 // Test that DecodeImage() doesn't return image message > kMaximumMessageSize | |
37 TEST_F(ChromeContentUtilityClientTest, DecodeImageSizeLimit) { | |
38 // Approx max height for 3:2 image that will fit in IPC message; | |
39 // 1.5 for width/height ratio, 4 for bytes/pixel | |
40 int max_height_for_msg = sqrt(IPC::Channel::kMaximumMessageSize/(1.5*4)); | |
41 int height_large = max_height_for_msg + 20; | |
42 int width_large = height_large*3/2; | |
43 int height_small = max_height_for_msg - 20; | |
44 int width_small = height_small*3/2; | |
45 int base_msg_size = sizeof(ChromeUtilityHostMsg_DecodeImage_Succeeded); | |
46 | |
47 // Slightly larger than IPC msg limit, dimensions will be halved | |
48 std::vector<unsigned char> jpg_large; | |
49 CreateJPEGImage(width_large, height_large, SK_ColorRED, &jpg_large); | |
50 SkBitmap bitmap_shrunk = | |
51 ChromeContentUtilityClient::DecodeImage(jpg_large, true); | |
52 | |
53 EXPECT_EQ(width_large/2, bitmap_shrunk.width()); | |
54 EXPECT_EQ(height_large/2, bitmap_shrunk.height()); | |
55 EXPECT_LT(bitmap_shrunk.computeSize64() + base_msg_size, | |
56 static_cast<int64_t>(IPC::Channel::kMaximumMessageSize)); | |
bshe
2014/08/29 19:56:26
nit: indent
Greg Levin
2014/09/04 16:39:19
Done.
| |
57 | |
Tom Sepez
2014/08/29 19:51:09
how about 4x just to run your loop more than once?
Greg Levin
2014/09/04 16:39:19
Done. Also added check for "shrink_to_fit == fals
| |
58 // Slightly smaller than IPC msg limit, dimensions will be unchanged | |
59 std::vector<unsigned char> jpg_small; | |
60 CreateJPEGImage(width_small, height_small, SK_ColorRED, &jpg_small); | |
61 SkBitmap bitmap_unshrunk = | |
62 ChromeContentUtilityClient::DecodeImage(jpg_small, true); | |
63 | |
64 EXPECT_EQ(width_small, bitmap_unshrunk.width()); | |
65 EXPECT_EQ(height_small, bitmap_unshrunk.height()); | |
66 EXPECT_LT(bitmap_unshrunk.computeSize64() + base_msg_size, | |
67 static_cast<int64_t>(IPC::Channel::kMaximumMessageSize)); | |
68 } | |
OLD | NEW |