summaryrefslogtreecommitdiffstats
path: root/chrome/browser/thumbnails/content_based_thumbnailing_algorithm_unittest.cc
diff options
context:
space:
mode:
authormotek@chromium.org <motek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-06-04 06:04:09 +0000
committermotek@chromium.org <motek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-06-04 06:04:09 +0000
commit821e62c180b425a2b41d08e7023ac8a3ce8047cc (patch)
tree0653e4abbc23997cc6be225f549f8e340a838d9e /chrome/browser/thumbnails/content_based_thumbnailing_algorithm_unittest.cc
parent2a3b375776c3899010088c03c27af8cfe20736c9 (diff)
downloadchromium_src-821e62c180b425a2b41d08e7023ac8a3ce8047cc.zip
chromium_src-821e62c180b425a2b41d08e7023ac8a3ce8047cc.tar.gz
chromium_src-821e62c180b425a2b41d08e7023ac8a3ce8047cc.tar.bz2
Plugs in the new thumbnailing algorithm to ThumbnailTabHelper.
The new method is available as preview under a command line switch. Some algorithm fixes remain + a proper Finch experiment. BUG=155269 Review URL: https://chromiumcodereview.appspot.com/15458003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@203856 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/thumbnails/content_based_thumbnailing_algorithm_unittest.cc')
-rw-r--r--chrome/browser/thumbnails/content_based_thumbnailing_algorithm_unittest.cc168
1 files changed, 168 insertions, 0 deletions
diff --git a/chrome/browser/thumbnails/content_based_thumbnailing_algorithm_unittest.cc b/chrome/browser/thumbnails/content_based_thumbnailing_algorithm_unittest.cc
new file mode 100644
index 0000000..38b0209
--- /dev/null
+++ b/chrome/browser/thumbnails/content_based_thumbnailing_algorithm_unittest.cc
@@ -0,0 +1,168 @@
+// Copyright 2013 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 "base/message_loop.h"
+#include "chrome/browser/thumbnails/content_based_thumbnailing_algorithm.h"
+#include "chrome/browser/thumbnails/simple_thumbnail_crop.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/test/test_browser_thread.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+#include "ui/gfx/canvas.h"
+#include "ui/gfx/scrollbar_size.h"
+
+namespace thumbnails {
+
+typedef testing::Test ContentBasedThumbnailingAlgorithmTest;
+
+class ConsumerCallbackCatcher {
+ public:
+ ConsumerCallbackCatcher()
+ : called_back_(false), clip_result_(CLIP_RESULT_UNPROCESSED) {
+ }
+
+ void UiThreadCallback(const ThumbnailingContext& context,
+ const SkBitmap& bitmap) {
+ called_back_ = true;
+ captured_bitmap_ = bitmap;
+ clip_result_ = context.clip_result;
+ score_ = context.score;
+ }
+
+ bool called_back() const {
+ return called_back_;
+ }
+
+ const SkBitmap& captured_bitmap() const {
+ return captured_bitmap_;
+ }
+
+ ClipResult clip_result() const {
+ return clip_result_;
+ }
+
+ const ThumbnailScore& score() const {
+ return score_;
+ }
+
+ private:
+ SkBitmap captured_bitmap_;
+ bool called_back_;
+ ClipResult clip_result_;
+ ThumbnailScore score_;
+
+ DISALLOW_COPY_AND_ASSIGN(ConsumerCallbackCatcher);
+};
+
+TEST_F(ContentBasedThumbnailingAlgorithmTest, GetCanvasCopyInfo) {
+ // We will want to use the entirety of the image as the source. Usually,
+ // an image in its original size should be requested, except for reakky large
+ // canvas. In that case, image will be shrunk but wit aspect ratio preserved.
+ const gfx::Size thumbnail_size(312, 165);
+ scoped_refptr<ThumbnailingAlgorithm> algorithm(
+ new ContentBasedThumbnailingAlgorithm(thumbnail_size));
+
+ gfx::Rect clipping_rect;
+ gfx::Size target_size;
+ gfx::Size source_size(1000, 600);
+
+ ClipResult clip_result = algorithm->GetCanvasCopyInfo(
+ source_size, ui::SCALE_FACTOR_100P, &clipping_rect, &target_size);
+ EXPECT_EQ(CLIP_RESULT_SOURCE_SAME_AS_TARGET, clip_result);
+ EXPECT_EQ(source_size.ToString(), clipping_rect.size().ToString());
+ EXPECT_EQ(gfx::Point(0, 0).ToString(), clipping_rect.origin().ToString());
+ EXPECT_EQ(source_size, target_size);
+
+ source_size.SetSize(6000, 3000);
+ clip_result = algorithm->GetCanvasCopyInfo(
+ source_size, ui::SCALE_FACTOR_100P, &clipping_rect, &target_size);
+ EXPECT_EQ(CLIP_RESULT_NOT_CLIPPED, clip_result);
+ EXPECT_EQ(source_size.ToString(), clipping_rect.size().ToString());
+ EXPECT_EQ(gfx::Point(0, 0).ToString(), clipping_rect.origin().ToString());
+ EXPECT_LT(target_size.width(), source_size.width());
+ EXPECT_LT(target_size.height(), source_size.height());
+ EXPECT_NEAR(static_cast<float>(target_size.width()) / target_size.height(),
+ static_cast<float>(source_size.width()) / source_size.height(),
+ 0.1f);
+ source_size.SetSize(300, 200);
+ clip_result = algorithm->GetCanvasCopyInfo(
+ source_size, ui::SCALE_FACTOR_100P, &clipping_rect, &target_size);
+ EXPECT_EQ(CLIP_RESULT_SOURCE_IS_SMALLER, clip_result);
+ EXPECT_EQ(clipping_rect.size().ToString(),
+ SimpleThumbnailCrop::GetCopySizeForThumbnail(
+ ui::SCALE_FACTOR_100P, thumbnail_size).ToString());
+ EXPECT_EQ(gfx::Point(0, 0).ToString(), clipping_rect.origin().ToString());
+}
+
+TEST_F(ContentBasedThumbnailingAlgorithmTest, PrepareSourceBitmap) {
+ const gfx::Size thumbnail_size(312, 165);
+ const gfx::Size copy_size(400, 200);
+ scoped_refptr<ThumbnailingContext> context(
+ ThumbnailingContext::CreateThumbnailingContextForTest());
+ context->requested_copy_size = copy_size;
+
+ // This calls for exercising two distinct paths: with prior clipping and
+ // without.
+ SkBitmap source;
+ source.setConfig(SkBitmap::kARGB_8888_Config, 800, 600);
+ source.allocPixels();
+ source.eraseRGB(50, 150, 200);
+ SkBitmap result = ContentBasedThumbnailingAlgorithm::PrepareSourceBitmap(
+ source, thumbnail_size, context);
+ EXPECT_EQ(CLIP_RESULT_SOURCE_SAME_AS_TARGET, context->clip_result);
+ EXPECT_GE(result.width(), copy_size.width());
+ EXPECT_GE(result.height(), copy_size.height());
+ EXPECT_LT(result.width(), source.width());
+ EXPECT_LT(result.height(), source.height());
+ // The check below is a bit of a side effect: since the image was clipped
+ // by scrollbar_size, it cannot be shrunk and thus what we get below is
+ // true.
+ EXPECT_NEAR(result.width(), source.width(), gfx::scrollbar_size());
+ EXPECT_NEAR(result.height(), source.height(), gfx::scrollbar_size());
+
+ result = ContentBasedThumbnailingAlgorithm::PrepareSourceBitmap(
+ source, thumbnail_size, context);
+ EXPECT_EQ(CLIP_RESULT_SOURCE_SAME_AS_TARGET, context->clip_result);
+ EXPECT_GE(result.width(), copy_size.width());
+ EXPECT_GE(result.height(), copy_size.height());
+ EXPECT_LT(result.width(), source.width());
+ EXPECT_LT(result.height(), source.height());
+}
+
+TEST_F(ContentBasedThumbnailingAlgorithmTest, CreateRetargetedThumbnail) {
+ // This tests the invocation of the main thumbnail-making apparatus.
+ // The actual content is not really of concern here, just check the plumbing.
+ const gfx::Size image_size(1200, 800);
+ gfx::Canvas canvas(image_size, ui::SCALE_FACTOR_100P, true);
+
+ // The image consists of vertical non-overlapping stripes 150 pixels wide.
+ canvas.FillRect(gfx::Rect(200, 200, 800, 400), SkColorSetRGB(255, 255, 255));
+ SkBitmap source =
+ skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
+
+ ConsumerCallbackCatcher catcher;
+ const gfx::Size thumbnail_size(432, 284);
+ scoped_refptr<ThumbnailingContext> context(
+ ThumbnailingContext::CreateThumbnailingContextForTest());
+ context->requested_copy_size = image_size;
+ context->clip_result = CLIP_RESULT_SOURCE_SAME_AS_TARGET;
+
+ base::MessageLoopForUI message_loop;
+ content::TestBrowserThread ui_thread(content::BrowserThread::UI,
+ &message_loop);
+ ContentBasedThumbnailingAlgorithm::CreateRetargetedThumbnail(
+ source,
+ thumbnail_size,
+ context,
+ base::Bind(&ConsumerCallbackCatcher::UiThreadCallback,
+ base::Unretained(&catcher)));
+ message_loop.RunUntilIdle();
+ ASSERT_TRUE(catcher.called_back());
+ EXPECT_TRUE(catcher.score().good_clipping);
+ EXPECT_FALSE(catcher.captured_bitmap().empty());
+ EXPECT_LT(catcher.captured_bitmap().width(), source.width());
+ EXPECT_LT(catcher.captured_bitmap().height(), source.height());
+}
+
+} // namespace thumbnails