Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / thumbnails / content_based_thumbnailing_algorithm_unittest.cc
blob3fbc8d42f6f0b5c0e345c2812450ae4b97347bb8
1 // Copyright 2013 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.
5 #include "base/message_loop/message_loop.h"
6 #include "chrome/browser/thumbnails/content_based_thumbnailing_algorithm.h"
7 #include "chrome/browser/thumbnails/simple_thumbnail_crop.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "content/public/test/test_browser_thread.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/skia/include/core/SkBitmap.h"
12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/scrollbar_size.h"
15 namespace thumbnails {
17 typedef testing::Test ContentBasedThumbnailingAlgorithmTest;
19 class ConsumerCallbackCatcher {
20 public:
21 ConsumerCallbackCatcher()
22 : called_back_(false), clip_result_(CLIP_RESULT_UNPROCESSED) {
25 void UiThreadCallback(const ThumbnailingContext& context,
26 const SkBitmap& bitmap) {
27 called_back_ = true;
28 captured_bitmap_ = bitmap;
29 clip_result_ = context.clip_result;
30 score_ = context.score;
33 bool called_back() const {
34 return called_back_;
37 const SkBitmap& captured_bitmap() const {
38 return captured_bitmap_;
41 ClipResult clip_result() const {
42 return clip_result_;
45 const ThumbnailScore& score() const {
46 return score_;
49 private:
50 SkBitmap captured_bitmap_;
51 bool called_back_;
52 ClipResult clip_result_;
53 ThumbnailScore score_;
55 DISALLOW_COPY_AND_ASSIGN(ConsumerCallbackCatcher);
58 TEST_F(ContentBasedThumbnailingAlgorithmTest, GetCanvasCopyInfo) {
59 // We will want to use the entirety of the image as the source. Usually,
60 // an image in its original size should be requested, except for reakky large
61 // canvas. In that case, image will be shrunk but wit aspect ratio preserved.
62 const gfx::Size thumbnail_size(312, 165);
63 scoped_refptr<ThumbnailingAlgorithm> algorithm(
64 new ContentBasedThumbnailingAlgorithm(thumbnail_size));
66 gfx::Rect clipping_rect;
67 gfx::Size target_size;
68 gfx::Size source_size(1000, 600);
70 ClipResult clip_result = algorithm->GetCanvasCopyInfo(
71 source_size, ui::SCALE_FACTOR_100P, &clipping_rect, &target_size);
72 EXPECT_EQ(CLIP_RESULT_SOURCE_SAME_AS_TARGET, clip_result);
73 EXPECT_EQ(source_size.ToString(), clipping_rect.size().ToString());
74 EXPECT_EQ(gfx::Point(0, 0).ToString(), clipping_rect.origin().ToString());
75 EXPECT_EQ(source_size, target_size);
77 source_size.SetSize(6000, 3000);
78 clip_result = algorithm->GetCanvasCopyInfo(
79 source_size, ui::SCALE_FACTOR_100P, &clipping_rect, &target_size);
80 EXPECT_EQ(CLIP_RESULT_NOT_CLIPPED, clip_result);
81 EXPECT_EQ(source_size.ToString(), clipping_rect.size().ToString());
82 EXPECT_EQ(gfx::Point(0, 0).ToString(), clipping_rect.origin().ToString());
83 EXPECT_LT(target_size.width(), source_size.width());
84 EXPECT_LT(target_size.height(), source_size.height());
85 EXPECT_NEAR(static_cast<float>(target_size.width()) / target_size.height(),
86 static_cast<float>(source_size.width()) / source_size.height(),
87 0.1f);
88 source_size.SetSize(300, 200);
89 clip_result = algorithm->GetCanvasCopyInfo(
90 source_size, ui::SCALE_FACTOR_100P, &clipping_rect, &target_size);
91 EXPECT_EQ(CLIP_RESULT_SOURCE_IS_SMALLER, clip_result);
92 EXPECT_EQ(clipping_rect.size().ToString(),
93 SimpleThumbnailCrop::GetCopySizeForThumbnail(
94 ui::SCALE_FACTOR_100P, thumbnail_size).ToString());
95 EXPECT_EQ(gfx::Point(0, 0).ToString(), clipping_rect.origin().ToString());
98 TEST_F(ContentBasedThumbnailingAlgorithmTest, PrepareSourceBitmap) {
99 const gfx::Size thumbnail_size(312, 165);
100 const gfx::Size copy_size(400, 200);
101 scoped_refptr<ThumbnailingContext> context(
102 ThumbnailingContext::CreateThumbnailingContextForTest());
103 context->requested_copy_size = copy_size;
105 // This calls for exercising two distinct paths: with prior clipping and
106 // without.
107 SkBitmap source;
108 source.allocN32Pixels(800, 600);
109 source.eraseARGB(255, 50, 150, 200);
110 SkBitmap result = ContentBasedThumbnailingAlgorithm::PrepareSourceBitmap(
111 source, thumbnail_size, context.get());
112 EXPECT_EQ(CLIP_RESULT_SOURCE_SAME_AS_TARGET, context->clip_result);
113 EXPECT_GE(result.width(), copy_size.width());
114 EXPECT_GE(result.height(), copy_size.height());
115 EXPECT_LT(result.width(), source.width());
116 EXPECT_LT(result.height(), source.height());
117 // The check below is a bit of a side effect: since the image was clipped
118 // by scrollbar_size, it cannot be shrunk and thus what we get below is
119 // true.
120 EXPECT_NEAR(result.width(), source.width(), gfx::scrollbar_size());
121 EXPECT_NEAR(result.height(), source.height(), gfx::scrollbar_size());
123 result = ContentBasedThumbnailingAlgorithm::PrepareSourceBitmap(
124 source, thumbnail_size, context.get());
125 EXPECT_EQ(CLIP_RESULT_SOURCE_SAME_AS_TARGET, context->clip_result);
126 EXPECT_GE(result.width(), copy_size.width());
127 EXPECT_GE(result.height(), copy_size.height());
128 EXPECT_LT(result.width(), source.width());
129 EXPECT_LT(result.height(), source.height());
132 TEST_F(ContentBasedThumbnailingAlgorithmTest, CreateRetargetedThumbnail) {
133 // This tests the invocation of the main thumbnail-making apparatus.
134 // The actual content is not really of concern here, just check the plumbing.
135 const gfx::Size image_size(1200, 800);
136 gfx::Canvas canvas(image_size, 1.0f, true);
138 // The image consists of vertical non-overlapping stripes 150 pixels wide.
139 canvas.FillRect(gfx::Rect(200, 200, 800, 400), SkColorSetRGB(255, 255, 255));
140 SkBitmap source =
141 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
143 ConsumerCallbackCatcher catcher;
144 const gfx::Size thumbnail_size(432, 284);
145 scoped_refptr<ThumbnailingContext> context(
146 ThumbnailingContext::CreateThumbnailingContextForTest());
147 context->requested_copy_size = image_size;
148 context->clip_result = CLIP_RESULT_SOURCE_SAME_AS_TARGET;
150 base::MessageLoopForUI message_loop;
151 content::TestBrowserThread ui_thread(content::BrowserThread::UI,
152 &message_loop);
153 ContentBasedThumbnailingAlgorithm::CreateRetargetedThumbnail(
154 source,
155 thumbnail_size,
156 context,
157 base::Bind(&ConsumerCallbackCatcher::UiThreadCallback,
158 base::Unretained(&catcher)));
159 message_loop.RunUntilIdle();
160 ASSERT_TRUE(catcher.called_back());
161 EXPECT_TRUE(catcher.score().good_clipping);
162 EXPECT_FALSE(catcher.captured_bitmap().empty());
163 EXPECT_LT(catcher.captured_bitmap().width(), source.width());
164 EXPECT_LT(catcher.captured_bitmap().height(), source.height());
167 } // namespace thumbnails