Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(352)

Side by Side Diff: chrome/browser/thumbnails/thumbnail_tab_helper_unittest.cc

Issue 11985003: Refactored-out the code of thumbnaling algorithm from thumbnail_tab_helper. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Another round of updates from OWNER's review. Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "chrome/browser/thumbnails/thumbnail_tab_helper.h"
6
7 #include "base/basictypes.h"
8 #include "base/stringprintf.h"
9 #include "chrome/common/render_messages.h"
10 #include "content/public/browser/notification_service.h"
11 #include "content/public/browser/notification_types.h"
12 #include "content/public/test/mock_render_process_host.h"
13 #include "content/public/test/test_renderer_host.h"
14 #include "skia/ext/platform_canvas.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/skia/include/core/SkColorPriv.h"
17 #include "ui/gfx/canvas.h"
18 #include "ui/surface/transport_dib.h"
19
20 using content::WebContents;
21
22 typedef testing::Test ThumbnailTabHelperTest;
23
24 TEST_F(ThumbnailTabHelperTest, CalculateBoringScore_Empty) {
25 SkBitmap bitmap;
26 EXPECT_DOUBLE_EQ(1.0, ThumbnailTabHelper::CalculateBoringScore(bitmap));
27 }
28
29 TEST_F(ThumbnailTabHelperTest, CalculateBoringScore_SingleColor) {
30 const gfx::Size kSize(20, 10);
31 gfx::Canvas canvas(kSize, ui::SCALE_FACTOR_100P, true);
32 // Fill all pixels in black.
33 canvas.FillRect(gfx::Rect(kSize), SK_ColorBLACK);
34
35 SkBitmap bitmap =
36 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
37 // The thumbnail should deserve the highest boring score.
38 EXPECT_DOUBLE_EQ(1.0, ThumbnailTabHelper::CalculateBoringScore(bitmap));
39 }
40
41 TEST_F(ThumbnailTabHelperTest, CalculateBoringScore_TwoColors) {
42 const gfx::Size kSize(20, 10);
43
44 gfx::Canvas canvas(kSize, ui::SCALE_FACTOR_100P, true);
45 // Fill all pixels in black.
46 canvas.FillRect(gfx::Rect(kSize), SK_ColorBLACK);
47 // Fill the left half pixels in white.
48 canvas.FillRect(gfx::Rect(0, 0, kSize.width() / 2, kSize.height()),
49 SK_ColorWHITE);
50
51 SkBitmap bitmap =
52 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
53 ASSERT_EQ(kSize.width(), bitmap.width());
54 ASSERT_EQ(kSize.height(), bitmap.height());
55 // The thumbnail should be less boring because two colors are used.
56 EXPECT_DOUBLE_EQ(0.5, ThumbnailTabHelper::CalculateBoringScore(bitmap));
57 }
58
59 TEST_F(ThumbnailTabHelperTest, GetClippedBitmap_TallerThanWide) {
60 // The input bitmap is vertically long.
61 gfx::Canvas canvas(gfx::Size(40, 90), ui::SCALE_FACTOR_100P, true);
62 SkBitmap bitmap =
63 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
64
65 // The desired size is square.
66 ThumbnailTabHelper::ClipResult clip_result = ThumbnailTabHelper::kNotClipped;
67 SkBitmap clipped_bitmap = ThumbnailTabHelper::GetClippedBitmap(
68 bitmap, 10, 10, &clip_result);
69 // The clipped bitmap should be square.
70 EXPECT_EQ(40, clipped_bitmap.width());
71 EXPECT_EQ(40, clipped_bitmap.height());
72 // The input was taller than wide.
73 EXPECT_EQ(ThumbnailTabHelper::kTallerThanWide, clip_result);
74 }
75
76 TEST_F(ThumbnailTabHelperTest, GetClippedBitmap_WiderThanTall) {
77 // The input bitmap is horizontally long.
78 gfx::Canvas canvas(gfx::Size(70, 40), ui::SCALE_FACTOR_100P, true);
79 SkBitmap bitmap =
80 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
81
82 // The desired size is square.
83 ThumbnailTabHelper::ClipResult clip_result = ThumbnailTabHelper::kNotClipped;
84 SkBitmap clipped_bitmap = ThumbnailTabHelper::GetClippedBitmap(
85 bitmap, 10, 10, &clip_result);
86 // The clipped bitmap should be square.
87 EXPECT_EQ(40, clipped_bitmap.width());
88 EXPECT_EQ(40, clipped_bitmap.height());
89 // The input was wider than tall.
90 EXPECT_EQ(ThumbnailTabHelper::kWiderThanTall, clip_result);
91 }
92
93 TEST_F(ThumbnailTabHelperTest, GetClippedBitmap_TooWiderThanTall) {
94 // The input bitmap is horizontally very long.
95 gfx::Canvas canvas(gfx::Size(90, 40), ui::SCALE_FACTOR_100P, true);
96 SkBitmap bitmap =
97 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
98
99 // The desired size is square.
100 ThumbnailTabHelper::ClipResult clip_result = ThumbnailTabHelper::kNotClipped;
101 SkBitmap clipped_bitmap = ThumbnailTabHelper::GetClippedBitmap(
102 bitmap, 10, 10, &clip_result);
103 // The clipped bitmap should be square.
104 EXPECT_EQ(40, clipped_bitmap.width());
105 EXPECT_EQ(40, clipped_bitmap.height());
106 // The input was wider than tall.
107 EXPECT_EQ(ThumbnailTabHelper::kTooWiderThanTall, clip_result);
108 }
109
110 TEST_F(ThumbnailTabHelperTest, GetClippedBitmap_NotClipped) {
111 // The input bitmap is square.
112 gfx::Canvas canvas(gfx::Size(40, 40), ui::SCALE_FACTOR_100P, true);
113 SkBitmap bitmap =
114 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
115
116 // The desired size is square.
117 ThumbnailTabHelper::ClipResult clip_result = ThumbnailTabHelper::kNotClipped;
118 SkBitmap clipped_bitmap = ThumbnailTabHelper::GetClippedBitmap(
119 bitmap, 10, 10, &clip_result);
120 // The clipped bitmap should be square.
121 EXPECT_EQ(40, clipped_bitmap.width());
122 EXPECT_EQ(40, clipped_bitmap.height());
123 // There was no need to clip.
124 EXPECT_EQ(ThumbnailTabHelper::kNotClipped, clip_result);
125 }
126
127 TEST_F(ThumbnailTabHelperTest, GetClippedBitmap_NonSquareOutput) {
128 // The input bitmap is square.
129 gfx::Canvas canvas(gfx::Size(40, 40), ui::SCALE_FACTOR_100P, true);
130 SkBitmap bitmap =
131 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
132
133 // The desired size is horizontally long.
134 ThumbnailTabHelper::ClipResult clip_result = ThumbnailTabHelper::kNotClipped;
135 SkBitmap clipped_bitmap = ThumbnailTabHelper::GetClippedBitmap(
136 bitmap, 20, 10, &clip_result);
137 // The clipped bitmap should have the same aspect ratio of the desired size.
138 EXPECT_EQ(40, clipped_bitmap.width());
139 EXPECT_EQ(20, clipped_bitmap.height());
140 // The input was taller than wide.
141 EXPECT_EQ(ThumbnailTabHelper::kTallerThanWide, clip_result);
142 }
OLDNEW
« no previous file with comments | « chrome/browser/thumbnails/thumbnail_tab_helper.cc ('k') | chrome/browser/thumbnails/thumbnailing_algorithm.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698