OLD | NEW |
---|---|
(Empty) | |
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/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/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 | |
bshe
2014/04/29 23:24:06
It doesn't look like you accessed private methods/
Alexander Alekseev
2014/04/30 00:04:44
This one is not a friend. It is a descendant of Wa
bshe
2014/05/02 14:58:18
Hah, Right. Sorry I miss read WallpaperManagerTest
| |
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 while (!empty_) | |
49 base::RunLoop().Run(); | |
50 } | |
51 | |
52 private: | |
53 bool empty_; | |
54 WallpaperManager* wallpaper_manager_; | |
55 | |
56 DISALLOW_COPY_AND_ASSIGN(TestWallpaperObserverPendingListEmpty); | |
57 }; | |
58 | |
59 } // namespace | |
60 | |
61 const SkColor WallpaperManagerTestUtils::kLargeDefaultWallpaperColor = | |
62 SK_ColorRED; | |
63 const SkColor WallpaperManagerTestUtils::kSmallDefaultWallpaperColor = | |
64 SK_ColorGREEN; | |
65 const SkColor WallpaperManagerTestUtils::kLargeGuestWallpaperColor = | |
66 SK_ColorBLUE; | |
67 const SkColor WallpaperManagerTestUtils::kSmallGuestWallpaperColor = | |
68 SK_ColorYELLOW; | |
69 | |
70 const SkColor WallpaperManagerTestUtils::kCustomWallpaperColor = | |
71 SK_ColorMAGENTA; | |
72 | |
73 const int WallpaperManagerTestUtils::kWallpaperSize = 2; | |
74 | |
75 // static | |
76 bool WallpaperManagerTestUtils::CreateJPEGImage( | |
77 int width, | |
78 int height, | |
79 SkColor color, | |
80 std::vector<unsigned char>* output) { | |
81 SkBitmap bitmap; | |
82 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height, 0); | |
83 bitmap.allocPixels(); | |
84 bitmap.eraseColor(color); | |
85 | |
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; | |
97 } | |
98 return true; | |
99 } | |
100 | |
101 // static | |
102 gfx::ImageSkia WallpaperManagerTestUtils::CreateTestImage(int width, | |
103 int height, | |
104 SkColor color) { | |
105 SkBitmap bitmap; | |
106 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); | |
107 bitmap.allocPixels(); | |
108 bitmap.eraseColor(color); | |
109 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap); | |
110 } | |
111 | |
112 // static | |
113 bool WallpaperManagerTestUtils::WriteJPEGFile(const base::FilePath& path, | |
114 int width, | |
115 int height, | |
116 SkColor color) { | |
117 std::vector<unsigned char> output; | |
118 if (!CreateJPEGImage(width, height, color, &output)) | |
119 return false; | |
120 | |
121 size_t bytes_written = base::WriteFile( | |
122 path, reinterpret_cast<const char*>(&output[0]), output.size()); | |
123 if (bytes_written != output.size()) { | |
124 LOG(ERROR) << "Wrote " << bytes_written << " byte(s) instead of " | |
125 << output.size() << " to " << path.value(); | |
126 return false; | |
127 } | |
128 return true; | |
129 } | |
130 | |
131 // static | |
132 bool WallpaperManagerTestUtils::ImageIsNearColor(gfx::ImageSkia image, | |
133 SkColor expected_color) { | |
134 if (image.size().IsEmpty()) { | |
135 LOG(ERROR) << "Image is empty"; | |
136 return false; | |
137 } | |
138 | |
139 const SkBitmap* bitmap = image.bitmap(); | |
140 if (!bitmap) { | |
141 LOG(ERROR) << "Unable to get bitmap from image"; | |
142 return false; | |
143 } | |
144 | |
145 bitmap->lockPixels(); | |
146 gfx::Point center = gfx::Rect(image.size()).CenterPoint(); | |
147 SkColor image_color = bitmap->getColor(center.x(), center.y()); | |
148 bitmap->unlockPixels(); | |
149 | |
150 const int kDiff = 3; | |
151 if (std::abs(static_cast<int>(SkColorGetA(image_color)) - | |
152 static_cast<int>(SkColorGetA(expected_color))) > kDiff || | |
153 std::abs(static_cast<int>(SkColorGetR(image_color)) - | |
154 static_cast<int>(SkColorGetR(expected_color))) > kDiff || | |
155 std::abs(static_cast<int>(SkColorGetG(image_color)) - | |
156 static_cast<int>(SkColorGetG(expected_color))) > kDiff || | |
157 std::abs(static_cast<int>(SkColorGetB(image_color)) - | |
158 static_cast<int>(SkColorGetB(expected_color))) > kDiff) { | |
159 LOG(ERROR) << "Expected color near 0x" << std::hex << expected_color | |
160 << " but got 0x" << image_color; | |
161 return false; | |
162 } | |
163 | |
164 return true; | |
165 } | |
166 | |
167 // static | |
168 void WallpaperManagerTestUtils::WaitAsyncWallpaperLoadFinished() { | |
169 TestWallpaperObserverPendingListEmpty observer(WallpaperManager::Get()); | |
170 observer.WaitForPendingListEmpty(); | |
171 } | |
172 | |
173 // static | |
174 void WallpaperManagerTestUtils::CreateCmdlineWallpapers( | |
175 const base::ScopedTempDir& dir, | |
176 scoped_ptr<base::CommandLine>* command_line) { | |
177 std::vector<std::string> options; | |
178 options.push_back(std::string("WM_Test_cmdline")); | |
179 const base::FilePath small_file = | |
180 dir.path().Append(FILE_PATH_LITERAL("small.jpg")); | |
181 options.push_back(std::string("--") + | |
182 ash::switches::kAshDefaultWallpaperSmall + "=" + | |
183 small_file.value()); | |
184 const base::FilePath large_file = | |
185 dir.path().Append(FILE_PATH_LITERAL("large.jpg")); | |
186 options.push_back(std::string("--") + | |
187 ash::switches::kAshDefaultWallpaperLarge + "=" + | |
188 large_file.value()); | |
189 | |
190 const base::FilePath guest_small_file = | |
191 dir.path().Append(FILE_PATH_LITERAL("guest_small.jpg")); | |
192 options.push_back(std::string("--") + ash::switches::kAshGuestWallpaperSmall + | |
193 "=" + guest_small_file.value()); | |
194 const base::FilePath guest_large_file = | |
195 dir.path().Append(FILE_PATH_LITERAL("guest_large.jpg")); | |
196 options.push_back(std::string("--") + ash::switches::kAshGuestWallpaperLarge + | |
197 "=" + guest_large_file.value()); | |
198 | |
199 ASSERT_TRUE(WriteJPEGFile( | |
200 small_file, kWallpaperSize, kWallpaperSize, kSmallDefaultWallpaperColor)); | |
201 ASSERT_TRUE(WriteJPEGFile( | |
202 large_file, kWallpaperSize, kWallpaperSize, kLargeDefaultWallpaperColor)); | |
203 | |
204 ASSERT_TRUE(WriteJPEGFile(guest_small_file, | |
205 kWallpaperSize, | |
206 kWallpaperSize, | |
207 kSmallGuestWallpaperColor)); | |
208 ASSERT_TRUE(WriteJPEGFile(guest_large_file, | |
209 kWallpaperSize, | |
210 kWallpaperSize, | |
211 kLargeGuestWallpaperColor)); | |
212 | |
213 command_line->reset(new base::CommandLine(options)); | |
214 WallpaperManager::Get()->SetCommandLineForTesting(command_line->get()); | |
215 } | |
216 | |
217 } // namespace chromeos | |
OLD | NEW |