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 "ash/desktop_background/wallpaper_resizer.h"
7 #include "ash/desktop_background/wallpaper_resizer_observer.h"
8 #include "base/message_loop/message_loop.h"
9 #include "content/public/test/test_browser_thread.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/gfx/image/image_skia_rep.h"
17 const int kTestImageWidth
= 5;
18 const int kTestImageHeight
= 2;
19 const int kTargetWidth
= 1;
20 const int kTargetHeight
= 1;
21 const uint32_t kExpectedCenter
= 0x02020202u
;
22 const uint32_t kExpectedCenterCropped
= 0x03030303u
;
23 const uint32_t kExpectedStretch
= 0x04040404u
;
24 const uint32_t kExpectedTile
= 0;
26 gfx::ImageSkia
CreateTestImage(const gfx::Size
& size
) {
29 int h
= size
.height();
30 src
.allocN32Pixels(w
, h
);
32 // Fill bitmap with data.
33 for (int y
= 0; y
< h
; ++y
) {
34 for (int x
= 0; x
< w
; ++x
) {
35 const uint8_t component
= static_cast<uint8_t>(y
* w
+ x
);
36 const SkColor pixel
= SkColorSetARGB(component
, component
,
37 component
, component
);
38 *(src
.getAddr32(x
, y
)) = pixel
;
42 gfx::ImageSkia image
= gfx::ImageSkia::CreateFrom1xBitmap(src
);
46 bool IsColor(const gfx::ImageSkia
& image
, const uint32_t expect
) {
47 EXPECT_EQ(image
.width(), kTargetWidth
);
48 EXPECT_EQ(image
.height(), kTargetHeight
);
49 const SkBitmap
* image_bitmap
= image
.bitmap();
50 SkAutoLockPixels
image_lock(*image_bitmap
);
51 return *image_bitmap
->getAddr32(0, 0) == expect
;
58 class WallpaperResizerTest
: public testing::Test
,
59 public WallpaperResizerObserver
{
61 WallpaperResizerTest()
62 : ui_thread_(content::BrowserThread::UI
, &message_loop_
) {
64 ~WallpaperResizerTest() override
{}
66 gfx::ImageSkia
Resize(const gfx::ImageSkia
& image
,
67 const gfx::Size
& target_size
,
68 WallpaperLayout layout
) {
69 scoped_ptr
<WallpaperResizer
> resizer
;
70 resizer
.reset(new WallpaperResizer(image
, target_size
, layout
));
71 resizer
->AddObserver(this);
72 resizer
->StartResize();
74 resizer
->RemoveObserver(this);
75 return resizer
->image();
78 void WaitForResize() {
82 void OnWallpaperResized() override
{ message_loop_
.Quit(); }
85 base::MessageLoop message_loop_
;
86 content::TestBrowserThread ui_thread_
;
88 DISALLOW_COPY_AND_ASSIGN(WallpaperResizerTest
);
91 TEST_F(WallpaperResizerTest
, BasicResize
) {
92 // Keeps in sync with WallpaperLayout enum.
93 WallpaperLayout layouts
[4] = {
94 WALLPAPER_LAYOUT_CENTER
,
95 WALLPAPER_LAYOUT_CENTER_CROPPED
,
96 WALLPAPER_LAYOUT_STRETCH
,
97 WALLPAPER_LAYOUT_TILE
,
99 const int length
= arraysize(layouts
);
101 for (int i
= 0; i
< length
; i
++) {
102 WallpaperLayout layout
= layouts
[i
];
103 gfx::ImageSkia
small_image(gfx::ImageSkiaRep(gfx::Size(10, 20), 1.0f
));
105 gfx::ImageSkia resized_small
= Resize(small_image
, gfx::Size(800, 600),
107 EXPECT_EQ(10, resized_small
.width());
108 EXPECT_EQ(20, resized_small
.height());
110 gfx::ImageSkia
large_image(gfx::ImageSkiaRep(gfx::Size(1000, 1000), 1.0f
));
111 gfx::ImageSkia resized_large
= Resize(large_image
, gfx::Size(800, 600),
113 EXPECT_EQ(800, resized_large
.width());
114 EXPECT_EQ(600, resized_large
.height());
118 // Test for crbug.com/244629. "CENTER_CROPPED generates the same image as
120 TEST_F(WallpaperResizerTest
, AllLayoutDifferent
) {
121 gfx::ImageSkia image
= CreateTestImage(
122 gfx::Size(kTestImageWidth
, kTestImageHeight
));
124 gfx::Size target_size
= gfx::Size(kTargetWidth
, kTargetHeight
);
125 gfx::ImageSkia center
= Resize(image
, target_size
, WALLPAPER_LAYOUT_CENTER
);
127 gfx::ImageSkia center_cropped
= Resize(image
, target_size
,
128 WALLPAPER_LAYOUT_CENTER_CROPPED
);
130 gfx::ImageSkia stretch
= Resize(image
, target_size
, WALLPAPER_LAYOUT_STRETCH
);
132 gfx::ImageSkia tile
= Resize(image
, target_size
, WALLPAPER_LAYOUT_TILE
);
134 EXPECT_TRUE(IsColor(center
, kExpectedCenter
));
135 EXPECT_TRUE(IsColor(center_cropped
, kExpectedCenterCropped
));
136 EXPECT_TRUE(IsColor(stretch
, kExpectedStretch
));
137 EXPECT_TRUE(IsColor(tile
, kExpectedTile
));
140 TEST_F(WallpaperResizerTest
, ImageId
) {
141 gfx::ImageSkia image
= CreateTestImage(
142 gfx::Size(kTestImageWidth
, kTestImageHeight
));
144 // Create a WallpaperResizer and check that it reports an original image ID
145 // both pre- and post-resize that matches the ID returned by GetImageId().
146 WallpaperResizer
resizer(image
, gfx::Size(10, 20), WALLPAPER_LAYOUT_STRETCH
);
147 EXPECT_EQ(WallpaperResizer::GetImageId(image
), resizer
.original_image_id());
148 resizer
.AddObserver(this);
149 resizer
.StartResize();
151 resizer
.RemoveObserver(this);
152 EXPECT_EQ(WallpaperResizer::GetImageId(image
), resizer
.original_image_id());