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 59 class WallpaperResizerTest : public testing::Test, 60 public WallpaperResizerObserver { 61 public: 62 WallpaperResizerTest() 63 : ui_thread_(content::BrowserThread::UI, &message_loop_) { 64 } 65 virtual ~WallpaperResizerTest() {} 66 67 gfx::ImageSkia Resize(const gfx::ImageSkia& image, 68 const gfx::Size& target_size, 69 WallpaperLayout layout) { 70 scoped_ptr<WallpaperResizer> resizer; 71 resizer.reset(new WallpaperResizer(image, target_size, layout)); 72 resizer->AddObserver(this); 73 resizer->StartResize(); 74 WaitForResize(); 75 resizer->RemoveObserver(this); 76 return resizer->image(); 77 } 78 79 void WaitForResize() { 80 message_loop_.Run(); 81 } 82 83 virtual void OnWallpaperResized() OVERRIDE { 84 message_loop_.Quit(); 85 } 86 87 private: 88 base::MessageLoop message_loop_; 89 content::TestBrowserThread ui_thread_; 90 91 DISALLOW_COPY_AND_ASSIGN(WallpaperResizerTest); 92 }; 93 94 TEST_F(WallpaperResizerTest, BasicResize) { 95 // Keeps in sync with WallpaperLayout enum. 96 WallpaperLayout layouts[4] = { 97 WALLPAPER_LAYOUT_CENTER, 98 WALLPAPER_LAYOUT_CENTER_CROPPED, 99 WALLPAPER_LAYOUT_STRETCH, 100 WALLPAPER_LAYOUT_TILE, 101 }; 102 const int length = arraysize(layouts); 103 104 for (int i = 0; i < length; i++) { 105 WallpaperLayout layout = layouts[i]; 106 gfx::ImageSkia small_image(gfx::ImageSkiaRep(gfx::Size(10, 20), 1.0f)); 107 108 gfx::ImageSkia resized_small = Resize(small_image, gfx::Size(800, 600), 109 layout); 110 EXPECT_EQ(10, resized_small.width()); 111 EXPECT_EQ(20, resized_small.height()); 112 113 gfx::ImageSkia large_image(gfx::ImageSkiaRep(gfx::Size(1000, 1000), 1.0f)); 114 gfx::ImageSkia resized_large = Resize(large_image, gfx::Size(800, 600), 115 layout); 116 EXPECT_EQ(800, resized_large.width()); 117 EXPECT_EQ(600, resized_large.height()); 118 } 119 } 120 121 // Test for crbug.com/244629. "CENTER_CROPPED generates the same image as 122 // STRETCH layout" 123 TEST_F(WallpaperResizerTest, AllLayoutDifferent) { 124 gfx::ImageSkia image = CreateTestImage( 125 gfx::Size(kTestImageWidth, kTestImageHeight)); 126 127 gfx::Size target_size = gfx::Size(kTargetWidth, kTargetHeight); 128 gfx::ImageSkia center = Resize(image, target_size, WALLPAPER_LAYOUT_CENTER); 129 130 gfx::ImageSkia center_cropped = Resize(image, target_size, 131 WALLPAPER_LAYOUT_CENTER_CROPPED); 132 133 gfx::ImageSkia stretch = Resize(image, target_size, WALLPAPER_LAYOUT_STRETCH); 134 135 gfx::ImageSkia tile = Resize(image, target_size, WALLPAPER_LAYOUT_TILE); 136 137 EXPECT_TRUE(IsColor(center, kExpectedCenter)); 138 EXPECT_TRUE(IsColor(center_cropped, kExpectedCenterCropped)); 139 EXPECT_TRUE(IsColor(stretch, kExpectedStretch)); 140 EXPECT_TRUE(IsColor(tile, kExpectedTile)); 141 } 142 143 TEST_F(WallpaperResizerTest, ImageId) { 144 gfx::ImageSkia image = CreateTestImage( 145 gfx::Size(kTestImageWidth, kTestImageHeight)); 146 147 // Create a WallpaperResizer and check that it reports an original image ID 148 // both pre- and post-resize that matches the ID returned by GetImageId(). 149 WallpaperResizer resizer(image, gfx::Size(10, 20), WALLPAPER_LAYOUT_STRETCH); 150 EXPECT_EQ(WallpaperResizer::GetImageId(image), resizer.original_image_id()); 151 resizer.AddObserver(this); 152 resizer.StartResize(); 153 WaitForResize(); 154 resizer.RemoveObserver(this); 155 EXPECT_EQ(WallpaperResizer::GetImageId(image), resizer.original_image_id()); 156 } 157 158 } // namespace ash 159