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.
5 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager_test_utils.h"
7 #include "ash/ash_switches.h"
8 #include "base/command_line.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.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"
25 class TestWallpaperObserverPendingListEmpty
26 : public WallpaperManager::Observer {
28 explicit TestWallpaperObserverPendingListEmpty(
29 WallpaperManager* wallpaper_manager)
30 : empty_(false), wallpaper_manager_(wallpaper_manager) {
31 DCHECK(wallpaper_manager_);
32 wallpaper_manager_->AddObserver(this);
35 virtual ~TestWallpaperObserverPendingListEmpty() {
36 wallpaper_manager_->RemoveObserver(this);
39 virtual void OnWallpaperAnimationFinished(
40 const std::string& user_id) override {}
42 virtual void OnPendingListEmptyForTesting() override {
44 base::MessageLoop::current()->Quit();
47 void WaitForPendingListEmpty() {
48 if (wallpaper_manager_->GetPendingListSizeForTesting() == 0) {
53 base::RunLoop().Run();
58 WallpaperManager* wallpaper_manager_;
60 DISALLOW_COPY_AND_ASSIGN(TestWallpaperObserverPendingListEmpty);
65 namespace wallpaper_manager_test_utils {
67 const SkColor kLargeDefaultWallpaperColor = SK_ColorRED;
68 const SkColor kSmallDefaultWallpaperColor = SK_ColorGREEN;
69 const SkColor kLargeGuestWallpaperColor = SK_ColorBLUE;
70 const SkColor kSmallGuestWallpaperColor = SK_ColorYELLOW;
72 const SkColor kCustomWallpaperColor = SK_ColorMAGENTA;
74 const int kWallpaperSize = 2;
76 bool CreateJPEGImage(int width,
79 std::vector<unsigned char>* output) {
81 bitmap.allocN32Pixels(width, height);
82 bitmap.eraseColor(color);
84 const int kQuality = 80;
85 if (!gfx::JPEGCodec::Encode(
86 static_cast<const unsigned char*>(bitmap.getPixels()),
87 gfx::JPEGCodec::FORMAT_SkBitmap,
93 LOG(ERROR) << "Unable to encode " << width << "x" << height << " bitmap";
99 gfx::ImageSkia CreateTestImage(int width, int height, SkColor color) {
101 bitmap.allocN32Pixels(width, height);
102 bitmap.eraseColor(color);
103 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
106 bool WriteJPEGFile(const base::FilePath& path,
110 std::vector<unsigned char> output;
111 if (!CreateJPEGImage(width, height, color, &output))
114 size_t bytes_written = base::WriteFile(
115 path, reinterpret_cast<const char*>(&output[0]), output.size());
116 if (bytes_written != output.size()) {
117 LOG(ERROR) << "Wrote " << bytes_written << " byte(s) instead of "
118 << output.size() << " to " << path.value();
124 bool ImageIsNearColor(gfx::ImageSkia image, SkColor expected_color) {
125 if (image.size().IsEmpty()) {
126 LOG(ERROR) << "Image is empty";
130 const SkBitmap* bitmap = image.bitmap();
132 LOG(ERROR) << "Unable to get bitmap from image";
136 bitmap->lockPixels();
137 gfx::Point center = gfx::Rect(image.size()).CenterPoint();
138 SkColor image_color = bitmap->getColor(center.x(), center.y());
139 bitmap->unlockPixels();
142 if (std::abs(static_cast<int>(SkColorGetA(image_color)) -
143 static_cast<int>(SkColorGetA(expected_color))) > kDiff ||
144 std::abs(static_cast<int>(SkColorGetR(image_color)) -
145 static_cast<int>(SkColorGetR(expected_color))) > kDiff ||
146 std::abs(static_cast<int>(SkColorGetG(image_color)) -
147 static_cast<int>(SkColorGetG(expected_color))) > kDiff ||
148 std::abs(static_cast<int>(SkColorGetB(image_color)) -
149 static_cast<int>(SkColorGetB(expected_color))) > kDiff) {
150 LOG(ERROR) << "Expected color near 0x" << std::hex << expected_color
151 << " but got 0x" << image_color;
158 void WaitAsyncWallpaperLoadFinished() {
159 TestWallpaperObserverPendingListEmpty observer(WallpaperManager::Get());
160 observer.WaitForPendingListEmpty();
163 void CreateCmdlineWallpapers(const base::ScopedTempDir& dir,
164 scoped_ptr<base::CommandLine>* command_line) {
165 std::vector<std::string> options;
166 options.push_back(std::string("WM_Test_cmdline"));
167 const base::FilePath small_file =
168 dir.path().Append(FILE_PATH_LITERAL("small.jpg"));
169 options.push_back(std::string("--") +
170 ash::switches::kAshDefaultWallpaperSmall + "=" +
172 const base::FilePath large_file =
173 dir.path().Append(FILE_PATH_LITERAL("large.jpg"));
174 options.push_back(std::string("--") +
175 ash::switches::kAshDefaultWallpaperLarge + "=" +
178 const base::FilePath guest_small_file =
179 dir.path().Append(FILE_PATH_LITERAL("guest_small.jpg"));
180 options.push_back(std::string("--") + ash::switches::kAshGuestWallpaperSmall +
181 "=" + guest_small_file.value());
182 const base::FilePath guest_large_file =
183 dir.path().Append(FILE_PATH_LITERAL("guest_large.jpg"));
184 options.push_back(std::string("--") + ash::switches::kAshGuestWallpaperLarge +
185 "=" + guest_large_file.value());
187 ASSERT_TRUE(WriteJPEGFile(
188 small_file, kWallpaperSize, kWallpaperSize, kSmallDefaultWallpaperColor));
189 ASSERT_TRUE(WriteJPEGFile(
190 large_file, kWallpaperSize, kWallpaperSize, kLargeDefaultWallpaperColor));
192 ASSERT_TRUE(WriteJPEGFile(guest_small_file,
195 kSmallGuestWallpaperColor));
196 ASSERT_TRUE(WriteJPEGFile(guest_large_file,
199 kLargeGuestWallpaperColor));
201 command_line->reset(new base::CommandLine(options));
202 WallpaperManager::Get()->SetCommandLineForTesting(command_line->get());
205 } // namespace wallpaper_manager_test_utils
207 } // namespace chromeos