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