Chromium Code Reviews| Index: chrome/browser/thumbnails/advanced_thumbnail_crop_unittest.cc |
| diff --git a/chrome/browser/thumbnails/advanced_thumbnail_crop_unittest.cc b/chrome/browser/thumbnails/advanced_thumbnail_crop_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..34f4905eece0432082ae7c1e872d35e8298f702a |
| --- /dev/null |
| +++ b/chrome/browser/thumbnails/advanced_thumbnail_crop_unittest.cc |
| @@ -0,0 +1,160 @@ |
| +// Copyright (c) 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/advanced_thumbnail_crop.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 { |
| + |
| +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.
|
| + |
| +typedef testing::Test AdvancedThumbnailCropTest; |
| + |
| +struct MockThumbnailingContext : public ThumbnailingContext { |
| + public: |
| + explicit MockThumbnailingContext(const gfx::Size& size) { |
| + requested_copy_size = size; |
| + clip_result = CLIP_RESULT_UNPROCESSED; |
| + } |
| +}; |
| + |
| +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; |
| + clip_result_ = context.clip_result; |
| + } |
| + |
| + SkBitmap captured_bitmap_; |
| + bool called_back_; |
| + ClipResult clip_result_; |
| + ThumbnailScore score_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(ConsumerCallbackCatcher); |
| +}; |
| + |
| +TEST_F(AdvancedThumbnailCropTest, 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 AdvancedThumbnailCrop(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, 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.
|
| + EXPECT_EQ(clipping_rect.size(), source_size); |
| + 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.
|
| + EXPECT_EQ(target_size, source_size); |
| + |
| + source_size.SetSize(6000, 3000); |
| + clip_result = algorithm->GetCanvasCopyInfo( |
| + source_size, ui::SCALE_FACTOR_100P, &clipping_rect, &target_size); |
| + EXPECT_EQ(clip_result, CLIP_RESULT_NOT_CLIPPED); |
| + EXPECT_EQ(clipping_rect.size(), source_size); |
| + EXPECT_EQ(clipping_rect.origin(), gfx::Point(0, 0)); |
| + 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, CLIP_RESULT_SOURCE_IS_SMALLER); |
| + EXPECT_EQ(clipping_rect.size(), |
| + SimpleThumbnailCrop::GetCopySizeForThumbnail(ui::SCALE_FACTOR_100P, |
| + thumbnail_size)); |
| + EXPECT_EQ(clipping_rect.origin(), gfx::Point(0, 0)); |
| +} |
| + |
| +TEST_F(AdvancedThumbnailCropTest, PrepareSourceBitmap) { |
| + const gfx::Size thumbnail_size(312, 165); |
| + const gfx::Size copy_size(400, 200); |
| + scoped_refptr<ThumbnailingContext> context( |
| + new MockThumbnailingContext(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 = AdvancedThumbnailCrop::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 = AdvancedThumbnailCrop::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(AdvancedThumbnailCropTest, CreateRetargettedThumbnail) { |
| + // 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( |
| + new MockThumbnailingContext(image_size)); |
| + |
| + context->clip_result = CLIP_RESULT_SOURCE_SAME_AS_TARGET; |
| + MessageLoopForUI message_loop; |
| + content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); |
| + AdvancedThumbnailCrop::CreateRetargettedThumbnail( |
| + 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 |