Index: chrome/utility/chrome_content_utility_client_unittest.cc |
diff --git a/chrome/utility/chrome_content_utility_client_unittest.cc b/chrome/utility/chrome_content_utility_client_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2341a5b93625418dd522e61e3bb4fd7ee51369a8 |
--- /dev/null |
+++ b/chrome/utility/chrome_content_utility_client_unittest.cc |
@@ -0,0 +1,68 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/common/chrome_utility_messages.h" |
+#include "chrome/utility/chrome_content_utility_client.h" |
+#include "ipc/ipc_channel.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "ui/gfx/codec/jpeg_codec.h" |
+ |
+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.
|
+ int height, |
+ SkColor color, |
+ std::vector<unsigned char>* output) { |
+ SkBitmap bitmap; |
+ bitmap.allocN32Pixels(width, height); |
+ bitmap.eraseColor(color); |
+ |
+ const int kQuality = 80; |
+ if (!gfx::JPEGCodec::Encode( |
+ static_cast<const unsigned char*>(bitmap.getPixels()), |
+ gfx::JPEGCodec::FORMAT_SkBitmap, |
+ width, |
+ height, |
+ bitmap.rowBytes(), |
+ kQuality, |
+ output)) { |
+ LOG(ERROR) << "Unable to encode " << width << "x" << height << " bitmap"; |
+ return false; |
+ } |
+ return true; |
+} |
+ |
+typedef testing::Test ChromeContentUtilityClientTest; |
+ |
+// Test that DecodeImage() doesn't return image message > kMaximumMessageSize |
+TEST_F(ChromeContentUtilityClientTest, DecodeImageSizeLimit) { |
+ // Approx max height for 3:2 image that will fit in IPC message; |
+ // 1.5 for width/height ratio, 4 for bytes/pixel |
+ int max_height_for_msg = sqrt(IPC::Channel::kMaximumMessageSize/(1.5*4)); |
+ int height_large = max_height_for_msg + 20; |
+ int width_large = height_large*3/2; |
+ int height_small = max_height_for_msg - 20; |
+ int width_small = height_small*3/2; |
+ int base_msg_size = sizeof(ChromeUtilityHostMsg_DecodeImage_Succeeded); |
+ |
+ // Slightly larger than IPC msg limit, dimensions will be halved |
+ std::vector<unsigned char> jpg_large; |
+ CreateJPEGImage(width_large, height_large, SK_ColorRED, &jpg_large); |
+ SkBitmap bitmap_shrunk = |
+ ChromeContentUtilityClient::DecodeImage(jpg_large, true); |
+ |
+ EXPECT_EQ(width_large/2, bitmap_shrunk.width()); |
+ EXPECT_EQ(height_large/2, bitmap_shrunk.height()); |
+ EXPECT_LT(bitmap_shrunk.computeSize64() + base_msg_size, |
+ 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.
|
+ |
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
|
+ // Slightly smaller than IPC msg limit, dimensions will be unchanged |
+ std::vector<unsigned char> jpg_small; |
+ CreateJPEGImage(width_small, height_small, SK_ColorRED, &jpg_small); |
+ SkBitmap bitmap_unshrunk = |
+ ChromeContentUtilityClient::DecodeImage(jpg_small, true); |
+ |
+ EXPECT_EQ(width_small, bitmap_unshrunk.width()); |
+ EXPECT_EQ(height_small, bitmap_unshrunk.height()); |
+ EXPECT_LT(bitmap_unshrunk.computeSize64() + base_msg_size, |
+ static_cast<int64_t>(IPC::Channel::kMaximumMessageSize)); |
+} |