1 // Copyright 2014 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 "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager_test_utils.h" 6 7 #include "ash/ash_switches.h" 8 #include "base/command_line.h" 9 #include "base/file_util.h" 10 #include "base/files/file_path.h" 11 #include "base/files/scoped_temp_dir.h" 12 #include "base/logging.h" 13 #include "base/run_loop.h" 14 #include "base/time/time.h" 15 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" 16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "ui/gfx/codec/jpeg_codec.h" 18 #include "ui/gfx/point.h" 19 #include "ui/gfx/rect.h" 20 21 namespace chromeos { 22 23 namespace { 24 25 class TestWallpaperObserverPendingListEmpty 26 : public WallpaperManager::Observer { 27 public: 28 explicit TestWallpaperObserverPendingListEmpty( 29 WallpaperManager* wallpaper_manager) 30 : empty_(false), wallpaper_manager_(wallpaper_manager) { 31 DCHECK(wallpaper_manager_); 32 wallpaper_manager_->AddObserver(this); 33 } 34 35 virtual ~TestWallpaperObserverPendingListEmpty() { 36 wallpaper_manager_->RemoveObserver(this); 37 } 38 39 virtual void OnWallpaperAnimationFinished( 40 const std::string& user_id) OVERRIDE {} 41 42 virtual void OnPendingListEmptyForTesting() OVERRIDE { 43 empty_ = true; 44 base::MessageLoop::current()->Quit(); 45 } 46 47 void WaitForPendingListEmpty() { 48 if (wallpaper_manager_->GetPendingListSizeForTesting() == 0) { 49 empty_ = true; 50 return; 51 } 52 while (!empty_) 53 base::RunLoop().Run(); 54 } 55 56 private: 57 bool empty_; 58 WallpaperManager* wallpaper_manager_; 59 60 DISALLOW_COPY_AND_ASSIGN(TestWallpaperObserverPendingListEmpty); 61 }; 62 63 } // namespace 64 65 namespace wallpaper_manager_test_utils { 66 67 const SkColor kLargeDefaultWallpaperColor = SK_ColorRED; 68 const SkColor kSmallDefaultWallpaperColor = SK_ColorGREEN; 69 const SkColor kLargeGuestWallpaperColor = SK_ColorBLUE; 70 const SkColor kSmallGuestWallpaperColor = SK_ColorYELLOW; 71 72 const SkColor kCustomWallpaperColor = SK_ColorMAGENTA; 73 74 const int kWallpaperSize = 2; 75 76 bool CreateJPEGImage(int width, 77 int height, 78 SkColor color, 79 std::vector<unsigned char>* output) { 80 SkBitmap bitmap; 81 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height, 0); 82 bitmap.allocPixels(); 83 bitmap.eraseColor(color); 84 85 const int kQuality = 80; 86 if (!gfx::JPEGCodec::Encode( 87 static_cast<const unsigned char*>(bitmap.getPixels()), 88 gfx::JPEGCodec::FORMAT_SkBitmap, 89 width, 90 height, 91 bitmap.rowBytes(), 92 kQuality, 93 output)) { 94 LOG(ERROR) << "Unable to encode " << width << "x" << height << " bitmap"; 95 return false; 96 } 97 return true; 98 } 99 100 gfx::ImageSkia CreateTestImage(int width, int height, SkColor color) { 101 SkBitmap bitmap; 102 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); 103 bitmap.allocPixels(); 104 bitmap.eraseColor(color); 105 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap); 106 } 107 108 bool WriteJPEGFile(const base::FilePath& path, 109 int width, 110 int height, 111 SkColor color) { 112 std::vector<unsigned char> output; 113 if (!CreateJPEGImage(width, height, color, &output)) 114 return false; 115 116 size_t bytes_written = base::WriteFile( 117 path, reinterpret_cast<const char*>(&output[0]), output.size()); 118 if (bytes_written != output.size()) { 119 LOG(ERROR) << "Wrote " << bytes_written << " byte(s) instead of " 120 << output.size() << " to " << path.value(); 121 return false; 122 } 123 return true; 124 } 125 126 bool ImageIsNearColor(gfx::ImageSkia image, SkColor expected_color) { 127 if (image.size().IsEmpty()) { 128 LOG(ERROR) << "Image is empty"; 129 return false; 130 } 131 132 const SkBitmap* bitmap = image.bitmap(); 133 if (!bitmap) { 134 LOG(ERROR) << "Unable to get bitmap from image"; 135 return false; 136 } 137 138 bitmap->lockPixels(); 139 gfx::Point center = gfx::Rect(image.size()).CenterPoint(); 140 SkColor image_color = bitmap->getColor(center.x(), center.y()); 141 bitmap->unlockPixels(); 142 143 const int kDiff = 3; 144 if (std::abs(static_cast<int>(SkColorGetA(image_color)) - 145 static_cast<int>(SkColorGetA(expected_color))) > kDiff || 146 std::abs(static_cast<int>(SkColorGetR(image_color)) - 147 static_cast<int>(SkColorGetR(expected_color))) > kDiff || 148 std::abs(static_cast<int>(SkColorGetG(image_color)) - 149 static_cast<int>(SkColorGetG(expected_color))) > kDiff || 150 std::abs(static_cast<int>(SkColorGetB(image_color)) - 151 static_cast<int>(SkColorGetB(expected_color))) > kDiff) { 152 LOG(ERROR) << "Expected color near 0x" << std::hex << expected_color 153 << " but got 0x" << image_color; 154 return false; 155 } 156 157 return true; 158 } 159 160 void WaitAsyncWallpaperLoadFinished() { 161 TestWallpaperObserverPendingListEmpty observer(WallpaperManager::Get()); 162 observer.WaitForPendingListEmpty(); 163 } 164 165 void CreateCmdlineWallpapers(const base::ScopedTempDir& dir, 166 scoped_ptr<base::CommandLine>* command_line) { 167 std::vector<std::string> options; 168 options.push_back(std::string("WM_Test_cmdline")); 169 const base::FilePath small_file = 170 dir.path().Append(FILE_PATH_LITERAL("small.jpg")); 171 options.push_back(std::string("--") + 172 ash::switches::kAshDefaultWallpaperSmall + "=" + 173 small_file.value()); 174 const base::FilePath large_file = 175 dir.path().Append(FILE_PATH_LITERAL("large.jpg")); 176 options.push_back(std::string("--") + 177 ash::switches::kAshDefaultWallpaperLarge + "=" + 178 large_file.value()); 179 180 const base::FilePath guest_small_file = 181 dir.path().Append(FILE_PATH_LITERAL("guest_small.jpg")); 182 options.push_back(std::string("--") + ash::switches::kAshGuestWallpaperSmall + 183 "=" + guest_small_file.value()); 184 const base::FilePath guest_large_file = 185 dir.path().Append(FILE_PATH_LITERAL("guest_large.jpg")); 186 options.push_back(std::string("--") + ash::switches::kAshGuestWallpaperLarge + 187 "=" + guest_large_file.value()); 188 189 ASSERT_TRUE(WriteJPEGFile( 190 small_file, kWallpaperSize, kWallpaperSize, kSmallDefaultWallpaperColor)); 191 ASSERT_TRUE(WriteJPEGFile( 192 large_file, kWallpaperSize, kWallpaperSize, kLargeDefaultWallpaperColor)); 193 194 ASSERT_TRUE(WriteJPEGFile(guest_small_file, 195 kWallpaperSize, 196 kWallpaperSize, 197 kSmallGuestWallpaperColor)); 198 ASSERT_TRUE(WriteJPEGFile(guest_large_file, 199 kWallpaperSize, 200 kWallpaperSize, 201 kLargeGuestWallpaperColor)); 202 203 command_line->reset(new base::CommandLine(options)); 204 WallpaperManager::Get()->SetCommandLineForTesting(command_line->get()); 205 } 206 207 } // namespace wallpaper_manager_test_utils 208 209 } // namespace chromeos 210