Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / ash / desktop_background / wallpaper_resizer_unittest.cc
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.
4
5 #include "ash/desktop_background/wallpaper_resizer.h"
6
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"
12
13 using aura::Window;
14
15 namespace {
16
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;
25
26 gfx::ImageSkia CreateTestImage(const gfx::Size& size) {
27   SkBitmap src;
28   int w = size.width();
29   int h = size.height();
30   src.setConfig(SkBitmap::kARGB_8888_Config, w, h);
31   src.allocPixels();
32
33   // Fill bitmap with data.
34   for (int y = 0; y < h; ++y) {
35     for (int x = 0; x < w; ++x) {
36       const uint8_t component = static_cast<uint8_t>(y * w + x);
37       const SkColor pixel = SkColorSetARGB(component, component,
38                                            component, component);
39       *(src.getAddr32(x, y)) = pixel;
40     }
41   }
42
43   gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(src);
44   return image;
45 }
46
47 bool IsColor(const gfx::ImageSkia& image, const uint32_t expect) {
48   EXPECT_EQ(image.width(), kTargetWidth);
49   EXPECT_EQ(image.height(), kTargetHeight);
50   const SkBitmap* image_bitmap = image.bitmap();
51   SkAutoLockPixels image_lock(*image_bitmap);
52   return *image_bitmap->getAddr32(0, 0) == expect;
53 }
54
55 }  // namespace
56
57 namespace ash {
58 namespace internal {
59
60 class WallpaperResizerTest : public testing::Test,
61                              public WallpaperResizerObserver {
62  public:
63   WallpaperResizerTest()
64       : ui_thread_(content::BrowserThread::UI, &message_loop_) {
65   }
66   virtual ~WallpaperResizerTest() {}
67
68   gfx::ImageSkia Resize(const gfx::ImageSkia& image,
69                         const gfx::Size& target_size,
70                         WallpaperLayout layout) {
71     scoped_ptr<WallpaperResizer> resizer;
72     resizer.reset(new WallpaperResizer(image, target_size, layout));
73     resizer->AddObserver(this);
74     resizer->StartResize();
75     WaitForResize();
76     resizer->RemoveObserver(this);
77     return resizer->image();
78   }
79
80   void WaitForResize() {
81     message_loop_.Run();
82   }
83
84   virtual void OnWallpaperResized() OVERRIDE {
85     message_loop_.Quit();
86   }
87
88  private:
89   base::MessageLoop message_loop_;
90   content::TestBrowserThread ui_thread_;
91
92   DISALLOW_COPY_AND_ASSIGN(WallpaperResizerTest);
93 };
94
95 TEST_F(WallpaperResizerTest, BasicResize) {
96   // Keeps in sync with WallpaperLayout enum.
97   WallpaperLayout layouts[4] = {
98       WALLPAPER_LAYOUT_CENTER,
99       WALLPAPER_LAYOUT_CENTER_CROPPED,
100       WALLPAPER_LAYOUT_STRETCH,
101       WALLPAPER_LAYOUT_TILE,
102   };
103   const int length = arraysize(layouts);
104
105   for (int i = 0; i < length; i++) {
106     WallpaperLayout layout = layouts[i];
107     gfx::ImageSkia small_image(gfx::ImageSkiaRep(gfx::Size(10, 20), 1.0f));
108
109     gfx::ImageSkia resized_small = Resize(small_image, gfx::Size(800, 600),
110                                           layout);
111     EXPECT_EQ(10, resized_small.width());
112     EXPECT_EQ(20, resized_small.height());
113
114     gfx::ImageSkia large_image(gfx::ImageSkiaRep(gfx::Size(1000, 1000), 1.0f));
115     gfx::ImageSkia resized_large = Resize(large_image, gfx::Size(800, 600),
116                                           layout);
117     EXPECT_EQ(800, resized_large.width());
118     EXPECT_EQ(600, resized_large.height());
119   }
120 }
121
122 // Test for crbug.com/244629. "CENTER_CROPPED generates the same image as
123 // STRETCH layout"
124 TEST_F(WallpaperResizerTest, AllLayoutDifferent) {
125   gfx::ImageSkia image = CreateTestImage(
126       gfx::Size(kTestImageWidth, kTestImageHeight));
127
128   gfx::Size target_size = gfx::Size(kTargetWidth, kTargetHeight);
129   gfx::ImageSkia center = Resize(image, target_size, WALLPAPER_LAYOUT_CENTER);
130
131   gfx::ImageSkia center_cropped = Resize(image, target_size,
132                                          WALLPAPER_LAYOUT_CENTER_CROPPED);
133
134   gfx::ImageSkia stretch = Resize(image, target_size, WALLPAPER_LAYOUT_STRETCH);
135
136   gfx::ImageSkia tile = Resize(image, target_size, WALLPAPER_LAYOUT_TILE);
137
138   EXPECT_TRUE(IsColor(center, kExpectedCenter));
139   EXPECT_TRUE(IsColor(center_cropped, kExpectedCenterCropped));
140   EXPECT_TRUE(IsColor(stretch, kExpectedStretch));
141   EXPECT_TRUE(IsColor(tile, kExpectedTile));
142 }
143
144 TEST_F(WallpaperResizerTest, ImageId) {
145   gfx::ImageSkia image = CreateTestImage(
146       gfx::Size(kTestImageWidth, kTestImageHeight));
147
148   // Create a WallpaperResizer and check that it reports an original image ID
149   // both pre- and post-resize that matches the ID returned by GetImageId().
150   WallpaperResizer resizer(image, gfx::Size(10, 20), WALLPAPER_LAYOUT_STRETCH);
151   EXPECT_EQ(WallpaperResizer::GetImageId(image), resizer.original_image_id());
152   resizer.AddObserver(this);
153   resizer.StartResize();
154   WaitForResize();
155   resizer.RemoveObserver(this);
156   EXPECT_EQ(WallpaperResizer::GetImageId(image), resizer.original_image_id());
157 }
158
159 }  // namespace internal
160 }  // namespace ash