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