Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / chromeos / login / users / wallpaper / wallpaper_manager_test_utils.cc
blobb6554096200a36a4353091bb8b085a63cf9e7c69
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"
22 namespace chromeos {
24 namespace {
26 class TestWallpaperObserverPendingListEmpty
27 : public WallpaperManager::Observer {
28 public:
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 {
43 empty_ = true;
44 base::MessageLoop::current()->Quit();
47 void WaitForPendingListEmpty() {
48 if (wallpaper_manager_->GetPendingListSizeForTesting() == 0) {
49 empty_ = true;
50 return;
52 while (!empty_)
53 base::RunLoop().Run();
56 private:
57 bool empty_;
58 WallpaperManager* wallpaper_manager_;
60 DISALLOW_COPY_AND_ASSIGN(TestWallpaperObserverPendingListEmpty);
63 } // namespace
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,
79 int height,
80 SkColor color,
81 std::vector<unsigned char>* output) {
82 SkBitmap bitmap;
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,
90 width,
91 height,
92 bitmap.rowBytes(),
93 kQuality,
94 output)) {
95 LOG(ERROR) << "Unable to encode " << width << "x" << height << " bitmap";
96 return false;
98 return true;
101 gfx::ImageSkia CreateTestImage(int width, int height, SkColor color) {
102 SkBitmap bitmap;
103 bitmap.allocN32Pixels(width, height);
104 bitmap.eraseColor(color);
105 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
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;
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;
123 return true;
126 bool ImageIsNearColor(gfx::ImageSkia image, SkColor expected_color) {
127 if (image.size().IsEmpty()) {
128 LOG(ERROR) << "Image is empty";
129 return false;
132 const SkBitmap* bitmap = image.bitmap();
133 if (!bitmap) {
134 LOG(ERROR) << "Unable to get bitmap from image";
135 return false;
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();
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;
157 return true;
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 + "=" +
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 chromeos::switches::kDefaultWallpaperLarge + "=" +
178 large_file.value());
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,
208 kWallpaperSize,
209 kWallpaperSize,
210 kSmallGuestWallpaperColor));
211 ASSERT_TRUE(WriteJPEGFile(guest_large_file,
212 kWallpaperSize,
213 kWallpaperSize,
214 kLargeGuestWallpaperColor));
216 ASSERT_TRUE(WriteJPEGFile(child_small_file,
217 kWallpaperSize,
218 kWallpaperSize,
219 kSmallChildWallpaperColor));
220 ASSERT_TRUE(WriteJPEGFile(child_large_file,
221 kWallpaperSize,
222 kWallpaperSize,
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