OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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.h" | |
6 #include "chrome/browser/thumbnails/advanced_thumbnail_crop.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 using content::BrowserThread; | |
mazda
2013/05/23 19:13:57
It looks this is used only once. Why don't you del
motek.
2013/05/27 16:54:54
Done.
| |
18 | |
19 typedef testing::Test AdvancedThumbnailCropTest; | |
20 | |
21 struct MockThumbnailingContext : public ThumbnailingContext { | |
22 public: | |
23 explicit MockThumbnailingContext(const gfx::Size& size) { | |
24 requested_copy_size = size; | |
25 clip_result = CLIP_RESULT_UNPROCESSED; | |
26 } | |
27 }; | |
28 | |
29 class ConsumerCallbackCatcher { | |
30 public: | |
31 ConsumerCallbackCatcher() | |
32 : called_back_(false), clip_result_(CLIP_RESULT_UNPROCESSED) { | |
33 } | |
34 | |
35 void UiThreadCallback(const ThumbnailingContext& context, | |
36 const SkBitmap& bitmap) { | |
37 called_back_ = true; | |
38 captured_bitmap_ = bitmap; | |
39 clip_result_ = context.clip_result; | |
40 score_ = context.score; | |
41 clip_result_ = context.clip_result; | |
42 } | |
43 | |
44 SkBitmap captured_bitmap_; | |
45 bool called_back_; | |
46 ClipResult clip_result_; | |
47 ThumbnailScore score_; | |
48 | |
49 private: | |
50 DISALLOW_COPY_AND_ASSIGN(ConsumerCallbackCatcher); | |
51 }; | |
52 | |
53 TEST_F(AdvancedThumbnailCropTest, GetCanvasCopyInfo) { | |
54 // We will want to use the entirety of the image as the source. Usually, | |
55 // an image in its original size should be requested, except for reakky large | |
56 // canvas. In that case, image will be shrunk but wit aspect ratio preserved. | |
57 const gfx::Size thumbnail_size(312, 165); | |
58 scoped_refptr<ThumbnailingAlgorithm> algorithm( | |
59 new AdvancedThumbnailCrop(thumbnail_size)); | |
60 | |
61 gfx::Rect clipping_rect; | |
62 gfx::Size target_size; | |
63 gfx::Size source_size(1000, 600); | |
64 | |
65 ClipResult clip_result = algorithm->GetCanvasCopyInfo( | |
66 source_size, ui::SCALE_FACTOR_100P, &clipping_rect, &target_size); | |
67 EXPECT_EQ(clip_result, CLIP_RESULT_SOURCE_SAME_AS_TARGET); | |
mazda
2013/05/23 19:13:57
Please write expectation in the first argument of
motek.
2013/05/27 16:54:54
Done.
| |
68 EXPECT_EQ(clipping_rect.size(), source_size); | |
69 EXPECT_EQ(clipping_rect.origin(), gfx::Point(0, 0)); | |
mazda
2013/05/23 19:13:57
Using ToString makes the test results more readabl
motek.
2013/05/27 16:54:54
Done.
| |
70 EXPECT_EQ(target_size, source_size); | |
71 | |
72 source_size.SetSize(6000, 3000); | |
73 clip_result = algorithm->GetCanvasCopyInfo( | |
74 source_size, ui::SCALE_FACTOR_100P, &clipping_rect, &target_size); | |
75 EXPECT_EQ(clip_result, CLIP_RESULT_NOT_CLIPPED); | |
76 EXPECT_EQ(clipping_rect.size(), source_size); | |
77 EXPECT_EQ(clipping_rect.origin(), gfx::Point(0, 0)); | |
78 EXPECT_LT(target_size.width(), source_size.width()); | |
79 EXPECT_LT(target_size.height(), source_size.height()); | |
80 EXPECT_NEAR(static_cast<float>(target_size.width()) / target_size.height(), | |
81 static_cast<float>(source_size.width()) / source_size.height(), | |
82 0.1f); | |
83 source_size.SetSize(300, 200); | |
84 clip_result = algorithm->GetCanvasCopyInfo( | |
85 source_size, ui::SCALE_FACTOR_100P, &clipping_rect, &target_size); | |
86 EXPECT_EQ(clip_result, CLIP_RESULT_SOURCE_IS_SMALLER); | |
87 EXPECT_EQ(clipping_rect.size(), | |
88 SimpleThumbnailCrop::GetCopySizeForThumbnail(ui::SCALE_FACTOR_100P, | |
89 thumbnail_size)); | |
90 EXPECT_EQ(clipping_rect.origin(), gfx::Point(0, 0)); | |
91 } | |
92 | |
93 TEST_F(AdvancedThumbnailCropTest, PrepareSourceBitmap) { | |
94 const gfx::Size thumbnail_size(312, 165); | |
95 const gfx::Size copy_size(400, 200); | |
96 scoped_refptr<ThumbnailingContext> context( | |
97 new MockThumbnailingContext(copy_size)); | |
98 | |
99 // This calls for exercising two distinct paths: with prior clipping and | |
100 // without. | |
101 SkBitmap source; | |
102 source.setConfig(SkBitmap::kARGB_8888_Config, 800, 600); | |
103 source.allocPixels(); | |
104 source.eraseRGB(50, 150, 200); | |
105 SkBitmap result = AdvancedThumbnailCrop::PrepareSourceBitmap( | |
106 source, thumbnail_size, context); | |
107 EXPECT_EQ(CLIP_RESULT_SOURCE_SAME_AS_TARGET, context->clip_result); | |
108 EXPECT_GE(result.width(), copy_size.width()); | |
109 EXPECT_GE(result.height(), copy_size.height()); | |
110 EXPECT_LT(result.width(), source.width()); | |
111 EXPECT_LT(result.height(), source.height()); | |
112 // The check below is a bit of a side effect: since the image was clipped | |
113 // by scrollbar_size, it cannot be shrunk and thus what we get below is | |
114 // true. | |
115 EXPECT_NEAR(result.width(), source.width(), gfx::scrollbar_size()); | |
116 EXPECT_NEAR(result.height(), source.height(), gfx::scrollbar_size()); | |
117 | |
118 result = AdvancedThumbnailCrop::PrepareSourceBitmap( | |
119 source, thumbnail_size, context); | |
120 EXPECT_EQ(CLIP_RESULT_SOURCE_SAME_AS_TARGET, context->clip_result); | |
121 EXPECT_GE(result.width(), copy_size.width()); | |
122 EXPECT_GE(result.height(), copy_size.height()); | |
123 EXPECT_LT(result.width(), source.width()); | |
124 EXPECT_LT(result.height(), source.height()); | |
125 } | |
126 | |
127 TEST_F(AdvancedThumbnailCropTest, CreateRetargettedThumbnail) { | |
128 // This tests the invocation of the main thumbnail-making apparatus. | |
129 // The actual content is not really of concern here, just check the plumbing. | |
130 const gfx::Size image_size(1200, 800); | |
131 gfx::Canvas canvas(image_size, ui::SCALE_FACTOR_100P, true); | |
132 | |
133 // The image consists of vertical non-overlapping stripes 150 pixels wide. | |
134 canvas.FillRect(gfx::Rect(200, 200, 800, 400), SkColorSetRGB(255, 255, 255)); | |
135 SkBitmap source = | |
136 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); | |
137 | |
138 ConsumerCallbackCatcher catcher; | |
139 const gfx::Size thumbnail_size(432, 284); | |
140 scoped_refptr<ThumbnailingContext> context( | |
141 new MockThumbnailingContext(image_size)); | |
142 | |
143 context->clip_result = CLIP_RESULT_SOURCE_SAME_AS_TARGET; | |
144 MessageLoopForUI message_loop; | |
145 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); | |
146 AdvancedThumbnailCrop::CreateRetargettedThumbnail( | |
147 source, | |
148 thumbnail_size, | |
149 context, | |
150 base::Bind(&ConsumerCallbackCatcher::UiThreadCallback, | |
151 base::Unretained(&catcher))); | |
152 message_loop.RunUntilIdle(); | |
153 ASSERT_TRUE(catcher.called_back_); | |
154 EXPECT_TRUE(catcher.score_.good_clipping); | |
155 EXPECT_FALSE(catcher.captured_bitmap_.empty()); | |
156 EXPECT_LT(catcher.captured_bitmap_.width(), source.width()); | |
157 EXPECT_LT(catcher.captured_bitmap_.height(), source.height()); | |
158 } | |
159 | |
160 } // namespace thumbnails | |
OLD | NEW |