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 "chromeos/chromeos_switches.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "ui/gfx/codec/jpeg_codec.h"
19 #include "ui/gfx/geometry/point.h"
20 #include "ui/gfx/geometry/rect.h"
26 class TestWallpaperObserverPendingListEmpty
27 : public WallpaperManager::Observer
{
29 explicit TestWallpaperObserverPendingListEmpty(
30 WallpaperManager
* wallpaper_manager
)
31 : empty_(false), wallpaper_manager_(wallpaper_manager
) {
32 DCHECK(wallpaper_manager_
);
33 wallpaper_manager_
->AddObserver(this);
36 ~TestWallpaperObserverPendingListEmpty() override
{
37 wallpaper_manager_
->RemoveObserver(this);
40 void OnWallpaperAnimationFinished(const std::string
& user_id
) override
{}
42 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
;
71 const SkColor kLargeChildWallpaperColor
= SK_ColorCYAN
;
72 const SkColor kSmallChildWallpaperColor
= SK_ColorMAGENTA
;
74 const SkColor kCustomWallpaperColor
= SK_ColorMAGENTA
;
76 const int kWallpaperSize
= 2;
78 bool CreateJPEGImage(int width
,
81 std::vector
<unsigned char>* output
) {
83 bitmap
.allocN32Pixels(width
, height
);
84 bitmap
.eraseColor(color
);
86 const int kQuality
= 80;
87 if (!gfx::JPEGCodec::Encode(
88 static_cast<const unsigned char*>(bitmap
.getPixels()),
89 gfx::JPEGCodec::FORMAT_SkBitmap
,
95 LOG(ERROR
) << "Unable to encode " << width
<< "x" << height
<< " bitmap";
101 gfx::ImageSkia
CreateTestImage(int width
, int height
, SkColor color
) {
103 bitmap
.allocN32Pixels(width
, height
);
104 bitmap
.eraseColor(color
);
105 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap
);
108 bool WriteJPEGFile(const base::FilePath
& path
,
112 std::vector
<unsigned char> output
;
113 if (!CreateJPEGImage(width
, height
, color
, &output
))
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();
126 bool ImageIsNearColor(gfx::ImageSkia image
, SkColor expected_color
) {
127 if (image
.size().IsEmpty()) {
128 LOG(ERROR
) << "Image is empty";
132 const SkBitmap
* bitmap
= image
.bitmap();
134 LOG(ERROR
) << "Unable to get bitmap from image";
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();
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
;
160 void WaitAsyncWallpaperLoadFinished() {
161 TestWallpaperObserverPendingListEmpty
observer(WallpaperManager::Get());
162 observer
.WaitForPendingListEmpty();
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 chromeos::switches::kDefaultWallpaperSmall
+ "=" +
174 const base::FilePath large_file
=
175 dir
.path().Append(FILE_PATH_LITERAL("large.jpg"));
176 options
.push_back(std::string("--") +
177 chromeos::switches::kDefaultWallpaperLarge
+ "=" +
180 const base::FilePath guest_small_file
=
181 dir
.path().Append(FILE_PATH_LITERAL("guest_small.jpg"));
182 options
.push_back(std::string("--") +
183 chromeos::switches::kGuestWallpaperSmall
+ "=" +
184 guest_small_file
.value());
185 const base::FilePath guest_large_file
=
186 dir
.path().Append(FILE_PATH_LITERAL("guest_large.jpg"));
187 options
.push_back(std::string("--") +
188 chromeos::switches::kGuestWallpaperLarge
+ "=" +
189 guest_large_file
.value());
191 const base::FilePath child_small_file
=
192 dir
.path().Append(FILE_PATH_LITERAL("child_small.jpg"));
193 options
.push_back(std::string("--") +
194 chromeos::switches::kChildWallpaperSmall
+ "=" +
195 child_small_file
.value());
196 const base::FilePath child_large_file
=
197 dir
.path().Append(FILE_PATH_LITERAL("child_large.jpg"));
198 options
.push_back(std::string("--") +
199 chromeos::switches::kChildWallpaperLarge
+ "=" +
200 child_large_file
.value());
202 ASSERT_TRUE(WriteJPEGFile(
203 small_file
, kWallpaperSize
, kWallpaperSize
, kSmallDefaultWallpaperColor
));
204 ASSERT_TRUE(WriteJPEGFile(
205 large_file
, kWallpaperSize
, kWallpaperSize
, kLargeDefaultWallpaperColor
));
207 ASSERT_TRUE(WriteJPEGFile(guest_small_file
,
210 kSmallGuestWallpaperColor
));
211 ASSERT_TRUE(WriteJPEGFile(guest_large_file
,
214 kLargeGuestWallpaperColor
));
216 ASSERT_TRUE(WriteJPEGFile(child_small_file
,
219 kSmallChildWallpaperColor
));
220 ASSERT_TRUE(WriteJPEGFile(child_large_file
,
223 kLargeChildWallpaperColor
));
225 command_line
->reset(new base::CommandLine(options
));
226 WallpaperManager::Get()->SetCommandLineForTesting(command_line
->get());
229 } // namespace wallpaper_manager_test_utils
231 } // namespace chromeos