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::RootWindow; 14 using aura::Window; 15 16 namespace { 17 18 const int kTestImageWidth = 5; 19 const int kTestImageHeight = 2; 20 const int kTargetWidth = 1; 21 const int kTargetHeight = 1; 22 const uint32_t kExpectedCenter = 0x02020202u; 23 const uint32_t kExpectedCenterCropped = 0x03030303u; 24 const uint32_t kExpectedStretch = 0x04040404u; 25 const uint32_t kExpectedTile = 0; 26 27 gfx::ImageSkia CreateTestImage(const gfx::Size& size) { 28 SkBitmap src; 29 int w = size.width(); 30 int h = size.height(); 31 src.setConfig(SkBitmap::kARGB_8888_Config, w, h); 32 src.allocPixels(); 33 34 // Fill bitmap with data. 35 for (int y = 0; y < h; ++y) { 36 for (int x = 0; x < w; ++x) { 37 const uint8_t component = static_cast<uint8_t>(y * w + x); 38 const SkColor pixel = SkColorSetARGB(component, component, 39 component, component); 40 *(src.getAddr32(x, y)) = pixel; 41 } 42 } 43 44 return gfx::ImageSkia::CreateFrom1xBitmap(src); 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->wallpaper_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), 108 ui::SCALE_FACTOR_100P)); 109 110 gfx::ImageSkia resized_small = Resize(small_image, gfx::Size(800, 600), 111 layout); 112 EXPECT_EQ(10, resized_small.width()); 113 EXPECT_EQ(20, resized_small.height()); 114 115 gfx::ImageSkia large_image(gfx::ImageSkiaRep(gfx::Size(1000, 1000), 116 ui::SCALE_FACTOR_100P)); 117 gfx::ImageSkia resized_large = Resize(large_image, gfx::Size(800, 600), 118 layout); 119 EXPECT_EQ(800, resized_large.width()); 120 EXPECT_EQ(600, resized_large.height()); 121 } 122 } 123 124 // Test for crbug.com/244629. "CENTER_CROPPED generates the same image as 125 // STRETCH layout" 126 TEST_F(WallpaperResizerTest, AllLayoutDifferent) { 127 gfx::ImageSkia image = CreateTestImage( 128 gfx::Size(kTestImageWidth, kTestImageHeight)); 129 130 gfx::Size target_size = gfx::Size(kTargetWidth, kTargetHeight); 131 gfx::ImageSkia center = Resize(image, target_size, WALLPAPER_LAYOUT_CENTER); 132 133 gfx::ImageSkia center_cropped = Resize(image, target_size, 134 WALLPAPER_LAYOUT_CENTER_CROPPED); 135 136 gfx::ImageSkia stretch = Resize(image, target_size, WALLPAPER_LAYOUT_STRETCH); 137 138 gfx::ImageSkia tile = Resize(image, target_size, WALLPAPER_LAYOUT_TILE); 139 140 EXPECT_TRUE(IsColor(center, kExpectedCenter)); 141 EXPECT_TRUE(IsColor(center_cropped, kExpectedCenterCropped)); 142 EXPECT_TRUE(IsColor(stretch, kExpectedStretch)); 143 EXPECT_TRUE(IsColor(tile, kExpectedTile)); 144 } 145 146 } // namespace internal 147 } // namespace ash 148