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.
5 #include "base/message_loop/message_loop.h"
6 #include "components/wallpaper/wallpaper_resizer.h"
7 #include "components/wallpaper/wallpaper_resizer_observer.h"
8 #include "content/public/test/test_browser_thread.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/gfx/image/image_skia_rep.h"
14 const int kTestImageWidth
= 5;
15 const int kTestImageHeight
= 2;
16 const int kTargetWidth
= 1;
17 const int kTargetHeight
= 1;
18 const uint32_t kExpectedCenter
= 0x02020202u
;
19 const uint32_t kExpectedCenterCropped
= 0x03030303u
;
20 const uint32_t kExpectedStretch
= 0x04040404u
;
21 const uint32_t kExpectedTile
= 0;
23 gfx::ImageSkia
CreateTestImage(const gfx::Size
& size
) {
26 int h
= size
.height();
27 src
.allocN32Pixels(w
, h
);
29 // Fill bitmap with data.
30 for (int y
= 0; y
< h
; ++y
) {
31 for (int x
= 0; x
< w
; ++x
) {
32 const uint8_t component
= static_cast<uint8_t>(y
* w
+ x
);
34 SkColorSetARGB(component
, component
, component
, component
);
35 *(src
.getAddr32(x
, y
)) = pixel
;
39 gfx::ImageSkia image
= gfx::ImageSkia::CreateFrom1xBitmap(src
);
43 bool IsColor(const gfx::ImageSkia
& image
, const uint32_t expect
) {
44 EXPECT_EQ(image
.width(), kTargetWidth
);
45 EXPECT_EQ(image
.height(), kTargetHeight
);
46 const SkBitmap
* image_bitmap
= image
.bitmap();
47 SkAutoLockPixels
image_lock(*image_bitmap
);
48 return *image_bitmap
->getAddr32(0, 0) == expect
;
55 class WallpaperResizerTest
: public testing::Test
,
56 public WallpaperResizerObserver
{
58 WallpaperResizerTest()
59 : ui_thread_(content::BrowserThread::UI
, &message_loop_
) {}
60 ~WallpaperResizerTest() override
{}
62 gfx::ImageSkia
Resize(const gfx::ImageSkia
& image
,
63 const gfx::Size
& target_size
,
64 WallpaperLayout layout
) {
65 scoped_ptr
<WallpaperResizer
> resizer
;
66 resizer
.reset(new WallpaperResizer(
67 image
, target_size
, layout
, content::BrowserThread::GetBlockingPool()));
68 resizer
->AddObserver(this);
69 resizer
->StartResize();
71 resizer
->RemoveObserver(this);
72 return resizer
->image();
75 void WaitForResize() { message_loop_
.Run(); }
77 void OnWallpaperResized() override
{ message_loop_
.Quit(); }
80 base::MessageLoop message_loop_
;
81 content::TestBrowserThread ui_thread_
;
83 DISALLOW_COPY_AND_ASSIGN(WallpaperResizerTest
);
86 TEST_F(WallpaperResizerTest
, BasicResize
) {
87 // Keeps in sync with WallpaperLayout enum.
88 WallpaperLayout layouts
[4] = {
89 WALLPAPER_LAYOUT_CENTER
,
90 WALLPAPER_LAYOUT_CENTER_CROPPED
,
91 WALLPAPER_LAYOUT_STRETCH
,
92 WALLPAPER_LAYOUT_TILE
,
94 const int length
= arraysize(layouts
);
96 for (int i
= 0; i
< length
; i
++) {
97 WallpaperLayout layout
= layouts
[i
];
98 gfx::ImageSkia
small_image(gfx::ImageSkiaRep(gfx::Size(10, 20), 1.0f
));
100 gfx::ImageSkia resized_small
=
101 Resize(small_image
, gfx::Size(800, 600), layout
);
102 EXPECT_EQ(10, resized_small
.width());
103 EXPECT_EQ(20, resized_small
.height());
105 gfx::ImageSkia
large_image(gfx::ImageSkiaRep(gfx::Size(1000, 1000), 1.0f
));
106 gfx::ImageSkia resized_large
=
107 Resize(large_image
, gfx::Size(800, 600), layout
);
108 EXPECT_EQ(800, resized_large
.width());
109 EXPECT_EQ(600, resized_large
.height());
113 // Test for crbug.com/244629. "CENTER_CROPPED generates the same image as
115 TEST_F(WallpaperResizerTest
, AllLayoutDifferent
) {
116 gfx::ImageSkia image
=
117 CreateTestImage(gfx::Size(kTestImageWidth
, kTestImageHeight
));
119 gfx::Size target_size
= gfx::Size(kTargetWidth
, kTargetHeight
);
120 gfx::ImageSkia center
= Resize(image
, target_size
, WALLPAPER_LAYOUT_CENTER
);
122 gfx::ImageSkia center_cropped
=
123 Resize(image
, target_size
, WALLPAPER_LAYOUT_CENTER_CROPPED
);
125 gfx::ImageSkia stretch
= Resize(image
, target_size
, WALLPAPER_LAYOUT_STRETCH
);
127 gfx::ImageSkia tile
= Resize(image
, target_size
, WALLPAPER_LAYOUT_TILE
);
129 EXPECT_TRUE(IsColor(center
, kExpectedCenter
));
130 EXPECT_TRUE(IsColor(center_cropped
, kExpectedCenterCropped
));
131 EXPECT_TRUE(IsColor(stretch
, kExpectedStretch
));
132 EXPECT_TRUE(IsColor(tile
, kExpectedTile
));
135 TEST_F(WallpaperResizerTest
, ImageId
) {
136 gfx::ImageSkia image
=
137 CreateTestImage(gfx::Size(kTestImageWidth
, kTestImageHeight
));
139 // Create a WallpaperResizer and check that it reports an original image ID
140 // both pre- and post-resize that matches the ID returned by GetImageId().
141 WallpaperResizer
resizer(image
, gfx::Size(10, 20), WALLPAPER_LAYOUT_STRETCH
,
142 content::BrowserThread::GetBlockingPool());
143 EXPECT_EQ(WallpaperResizer::GetImageId(image
), resizer
.original_image_id());
144 resizer
.AddObserver(this);
145 resizer
.StartResize();
147 resizer
.RemoveObserver(this);
148 EXPECT_EQ(WallpaperResizer::GetImageId(image
), resizer
.original_image_id());