- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / thumbnails / content_based_thumbnailing_algorithm_unittest.cc
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.
4
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"
14
15 namespace thumbnails {
16
17 typedef testing::Test ContentBasedThumbnailingAlgorithmTest;
18
19 class ConsumerCallbackCatcher {
20  public:
21   ConsumerCallbackCatcher()
22       : called_back_(false), clip_result_(CLIP_RESULT_UNPROCESSED) {
23   }
24
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;
31   }
32
33   bool called_back() const {
34     return called_back_;
35   }
36
37   const SkBitmap& captured_bitmap() const {
38     return captured_bitmap_;
39   }
40
41   ClipResult clip_result() const {
42     return clip_result_;
43   }
44
45   const ThumbnailScore& score() const {
46     return score_;
47   }
48
49  private:
50   SkBitmap captured_bitmap_;
51   bool called_back_;
52   ClipResult clip_result_;
53   ThumbnailScore score_;
54
55   DISALLOW_COPY_AND_ASSIGN(ConsumerCallbackCatcher);
56 };
57
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));
65
66   gfx::Rect clipping_rect;
67   gfx::Size target_size;
68   gfx::Size source_size(1000, 600);
69
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);
76
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());
96 }
97
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;
104
105   // This calls for exercising two distinct paths: with prior clipping and
106   // without.
107   SkBitmap source;
108   source.setConfig(SkBitmap::kARGB_8888_Config, 800, 600);
109   source.allocPixels();
110   source.eraseRGB(50, 150, 200);
111   SkBitmap result = ContentBasedThumbnailingAlgorithm::PrepareSourceBitmap(
112       source, thumbnail_size, context.get());
113   EXPECT_EQ(CLIP_RESULT_SOURCE_SAME_AS_TARGET, context->clip_result);
114   EXPECT_GE(result.width(), copy_size.width());
115   EXPECT_GE(result.height(), copy_size.height());
116   EXPECT_LT(result.width(), source.width());
117   EXPECT_LT(result.height(), source.height());
118   // The check below is a bit of a side effect: since the image was clipped
119   // by scrollbar_size, it cannot be shrunk and thus what we get below is
120   // true.
121   EXPECT_NEAR(result.width(), source.width(), gfx::scrollbar_size());
122   EXPECT_NEAR(result.height(), source.height(), gfx::scrollbar_size());
123
124   result = ContentBasedThumbnailingAlgorithm::PrepareSourceBitmap(
125       source, thumbnail_size, context.get());
126   EXPECT_EQ(CLIP_RESULT_SOURCE_SAME_AS_TARGET, context->clip_result);
127   EXPECT_GE(result.width(), copy_size.width());
128   EXPECT_GE(result.height(), copy_size.height());
129   EXPECT_LT(result.width(), source.width());
130   EXPECT_LT(result.height(), source.height());
131 }
132
133 TEST_F(ContentBasedThumbnailingAlgorithmTest, CreateRetargetedThumbnail) {
134   // This tests the invocation of the main thumbnail-making apparatus.
135   // The actual content is not really of concern here, just check the plumbing.
136   const gfx::Size image_size(1200, 800);
137   gfx::Canvas canvas(image_size, 1.0f, true);
138
139   // The image consists of vertical non-overlapping stripes 150 pixels wide.
140   canvas.FillRect(gfx::Rect(200, 200, 800, 400), SkColorSetRGB(255, 255, 255));
141   SkBitmap source =
142       skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
143
144   ConsumerCallbackCatcher catcher;
145   const gfx::Size thumbnail_size(432, 284);
146   scoped_refptr<ThumbnailingContext> context(
147       ThumbnailingContext::CreateThumbnailingContextForTest());
148   context->requested_copy_size = image_size;
149   context->clip_result = CLIP_RESULT_SOURCE_SAME_AS_TARGET;
150
151   base::MessageLoopForUI message_loop;
152   content::TestBrowserThread ui_thread(content::BrowserThread::UI,
153                                        &message_loop);
154   ContentBasedThumbnailingAlgorithm::CreateRetargetedThumbnail(
155       source,
156       thumbnail_size,
157       context,
158       base::Bind(&ConsumerCallbackCatcher::UiThreadCallback,
159                  base::Unretained(&catcher)));
160   message_loop.RunUntilIdle();
161   ASSERT_TRUE(catcher.called_back());
162   EXPECT_TRUE(catcher.score().good_clipping);
163   EXPECT_FALSE(catcher.captured_bitmap().empty());
164   EXPECT_LT(catcher.captured_bitmap().width(), source.width());
165   EXPECT_LT(catcher.captured_bitmap().height(), source.height());
166 }
167
168 }  // namespace thumbnails