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 | |
6 #include <vector> | |
7 | |
8 #include "ash/desktop_background/desktop_background_controller.h" | |
9 #include "ash/shell.h" | |
10 #include "base/command_line.h" | |
11 #include "base/files/scoped_temp_dir.h" | |
12 #include "base/run_loop.h" | |
13 #include "base/time/time.h" | |
14 #include "chrome/browser/chromeos/customization_document.h" | |
15 #include "chrome/browser/chromeos/customization_wallpaper_downloader.h" | |
16 #include "chrome/browser/chromeos/login/wallpaper_manager.h" | |
17 #include "chrome/browser/chromeos/login/wallpaper_manager_test_utils.h" | |
18 #include "chrome/browser/google/google_url_tracker.h" | |
19 #include "chrome/test/base/in_process_browser_test.h" | |
20 #include "chrome/test/base/testing_browser_process.h" | |
21 #include "chromeos/chromeos_switches.h" | |
22 #include "net/http/http_response_headers.h" | |
23 #include "net/http/http_status_code.h" | |
24 #include "net/url_request/test_url_fetcher_factory.h" | |
25 #include "net/url_request/url_fetcher_impl.h" | |
26 #include "testing/gtest/include/gtest/gtest.h" | |
27 | |
28 namespace chromeos { | |
29 | |
30 namespace { | |
31 | |
32 const char kOEMWallpaperURL[] = "http://somedomain.com/image.png"; | |
33 | |
34 const char kServicesManifest[] = | |
35 "{" | |
36 " \"version\": \"1.0\"," | |
37 " \"default_wallpaper\": \"http://somedomain.com/image.png\",\n" | |
38 " \"default_apps\": [\n" | |
39 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" | |
40 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" | |
41 " ],\n" | |
42 " \"localized_content\": {\n" | |
43 " \"en-US\": {\n" | |
44 " \"default_apps_folder_name\": \"EN-US OEM Name\"\n" | |
45 " },\n" | |
46 " \"en\": {\n" | |
47 " \"default_apps_folder_name\": \"EN OEM Name\"\n" | |
48 " },\n" | |
49 " \"default\": {\n" | |
50 " \"default_apps_folder_name\": \"Default OEM Name\"\n" | |
51 " }\n" | |
52 " }\n" | |
53 "}"; | |
54 | |
55 // Expected minimal wallpaper download retry interval in seconds. | |
56 const double kMinOEMWallpaperRetryIntervalSec = 2; | |
Daniel Erat
2014/04/30 21:23:06
s/double/int/
can this be smaller?
Alexander Alekseev
2014/05/04 23:30:56
Done.
| |
57 | |
58 class TestWallpaperObserver : public WallpaperManager::Observer { | |
59 public: | |
60 explicit TestWallpaperObserver(WallpaperManager* wallpaper_manager) | |
61 : finished_(false), | |
62 wallpaper_manager_(wallpaper_manager) { | |
63 DCHECK(wallpaper_manager_); | |
64 wallpaper_manager_->AddObserver(this); | |
65 } | |
66 | |
67 virtual ~TestWallpaperObserver() { | |
68 wallpaper_manager_->RemoveObserver(this); | |
69 } | |
70 | |
71 virtual void OnWallpaperAnimationFinished(const std::string&) OVERRIDE { | |
72 finished_ = true; | |
73 base::MessageLoop::current()->Quit(); | |
74 } | |
75 | |
76 void WaitForWallpaperAnimationFinished() { | |
77 while (!finished_) | |
78 base::RunLoop().Run(); | |
79 } | |
80 | |
81 private: | |
82 bool finished_; | |
83 WallpaperManager* wallpaper_manager_; | |
84 | |
85 DISALLOW_COPY_AND_ASSIGN(TestWallpaperObserver); | |
86 }; | |
87 | |
88 } // namespace | |
89 | |
90 // This is helper class for net::FakeURLFetcherFactory. | |
91 class TestWallpaperImageURLFetcherCallback { | |
92 public: | |
93 TestWallpaperImageURLFetcherCallback( | |
94 const GURL& url, | |
95 const size_t require_retries, | |
96 const std::vector<unsigned char>& jpeg_data_raw) | |
97 : url_(url), | |
98 require_retries_(require_retries), | |
99 factory_(NULL) { | |
100 jpeg_data_.resize(jpeg_data_raw.size()); | |
101 std::copy(jpeg_data_raw.begin(), jpeg_data_raw.end(), jpeg_data_.begin()); | |
102 } | |
103 | |
104 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher( | |
105 const GURL& url, | |
106 net::URLFetcherDelegate* delegate, | |
107 const std::string& response_data, | |
108 net::HttpStatusCode response_code, | |
109 net::URLRequestStatus::Status status) { | |
110 chromeos::ServicesCustomizationDocument* customization = | |
111 chromeos::ServicesCustomizationDocument::GetInstance(); | |
112 customization->wallpaper_downloader_->set_retry_delay_for_testing( | |
113 kMinOEMWallpaperRetryIntervalSec); | |
114 | |
115 attempts_.push_back(base::TimeTicks::Now()); | |
116 if (attempts_.size() > 1) { | |
117 const int retry = num_attempts() - 1; | |
118 const base::TimeDelta passed = | |
119 attempts_.back() - attempts_[attempts_.size() - 2]; | |
120 EXPECT_GE(passed, | |
121 base::TimeDelta::FromSecondsD(kMinOEMWallpaperRetryIntervalSec * | |
122 retry * retry)) | |
123 << "Retry too fast. Actual interval " << passed.InSecondsF() | |
124 << " seconds, but expected at least " | |
125 << kMinOEMWallpaperRetryIntervalSec << " * (retry=" << retry | |
126 << " * retry)= " << kMinOEMWallpaperRetryIntervalSec * retry * retry | |
127 << " seconds."; | |
128 } | |
129 if (attempts_.size() > require_retries_) { | |
130 response_code = net::HTTP_OK; | |
131 status = net::URLRequestStatus::SUCCESS; | |
132 factory_->SetFakeResponse(url, response_data, response_code, status); | |
133 } | |
134 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher( | |
135 url, delegate, response_data, response_code, status)); | |
136 scoped_refptr<net::HttpResponseHeaders> download_headers = | |
137 new net::HttpResponseHeaders(std::string()); | |
138 download_headers->AddHeader("Content-Type: image/jpeg"); | |
139 fetcher->set_response_headers(download_headers); | |
140 return fetcher.Pass(); | |
141 } | |
142 | |
143 void Initialize(net::FakeURLFetcherFactory* factory) { | |
144 factory_ = factory; | |
145 factory_->SetFakeResponse(url_, | |
146 jpeg_data_, | |
147 net::HTTP_INTERNAL_SERVER_ERROR, | |
148 net::URLRequestStatus::FAILED); | |
149 } | |
150 | |
151 size_t num_attempts() const { return attempts_.size(); } | |
152 | |
153 private: | |
154 const GURL url_; | |
155 // Respond with OK on required retry attempt. | |
156 const size_t require_retries_; | |
157 net::FakeURLFetcherFactory* factory_; | |
158 std::vector<base::TimeTicks> attempts_; | |
159 std::string jpeg_data_; | |
160 | |
161 DISALLOW_COPY_AND_ASSIGN(TestWallpaperImageURLFetcherCallback); | |
162 }; | |
163 | |
164 // This implements fake remote source for wallpaper image. | |
165 // JPEG image is created here and served to CustomizationWallpaperDownloader | |
166 // via net::FakeURLFetcher. | |
167 class WallpaperImageFetcherFactory { | |
168 public: | |
169 WallpaperImageFetcherFactory(const GURL& url, | |
170 int width, | |
171 int height, | |
172 SkColor color, | |
173 const size_t require_retries) { | |
174 // ASSERT_TRUE() cannot be directly used in constructor. | |
175 Initialize(url, width, height, color, require_retries); | |
176 } | |
177 | |
178 ~WallpaperImageFetcherFactory() { | |
179 fetcher_factory_.reset(); | |
180 net::URLFetcherImpl::set_factory(fallback_fetcher_factory_.get()); | |
181 fallback_fetcher_factory_.reset(); | |
182 } | |
183 | |
184 size_t num_attempts() const { return url_callback_->num_attempts(); } | |
185 | |
186 private: | |
187 void Initialize(const GURL& url, | |
188 int width, | |
189 int height, | |
190 SkColor color, | |
191 const size_t require_retries) { | |
192 std::vector<unsigned char> oem_wallpaper_; | |
193 ASSERT_TRUE(wallpaper_manager_test_utils::CreateJPEGImage( | |
194 width, height, color, &oem_wallpaper_)); | |
195 | |
196 url_callback_.reset(new TestWallpaperImageURLFetcherCallback( | |
197 url, require_retries, oem_wallpaper_)); | |
198 fallback_fetcher_factory_.reset(new net::TestURLFetcherFactory); | |
199 net::URLFetcherImpl::set_factory(NULL); | |
200 fetcher_factory_.reset(new net::FakeURLFetcherFactory( | |
201 fallback_fetcher_factory_.get(), | |
202 base::Bind(&TestWallpaperImageURLFetcherCallback::CreateURLFetcher, | |
203 base::Unretained(url_callback_.get())))); | |
204 url_callback_->Initialize(fetcher_factory_.get()); | |
205 } | |
206 | |
207 scoped_ptr<TestWallpaperImageURLFetcherCallback> url_callback_; | |
208 | |
209 // Use a test factory as a fallback so we don't have to deal with other | |
210 // requests. | |
211 scoped_ptr<net::TestURLFetcherFactory> fallback_fetcher_factory_; | |
212 scoped_ptr<net::FakeURLFetcherFactory> fetcher_factory_; | |
213 | |
214 DISALLOW_COPY_AND_ASSIGN(WallpaperImageFetcherFactory); | |
215 }; | |
216 | |
217 class CustomizationWallpaperDownloaderBrowserTest | |
218 : public InProcessBrowserTest { | |
219 public: | |
220 CustomizationWallpaperDownloaderBrowserTest() | |
221 : controller_(NULL), | |
222 local_state_(NULL) { | |
223 } | |
224 | |
225 virtual ~CustomizationWallpaperDownloaderBrowserTest() {} | |
226 | |
227 virtual void SetUpOnMainThread() OVERRIDE { | |
228 controller_ = ash::Shell::GetInstance()->desktop_background_controller(); | |
229 local_state_ = g_browser_process->local_state(); | |
230 } | |
231 | |
232 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
233 command_line->AppendSwitch(chromeos::switches::kLoginManager); | |
234 command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile, "user"); | |
235 } | |
236 | |
237 virtual void CleanUpOnMainThread() OVERRIDE { controller_ = NULL; } | |
238 | |
239 protected: | |
240 void CreateCmdlineWallpapers() { | |
241 cmdline_wallpaper_dir_.reset(new base::ScopedTempDir); | |
242 ASSERT_TRUE(cmdline_wallpaper_dir_->CreateUniqueTempDir()); | |
243 wallpaper_manager_test_utils::CreateCmdlineWallpapers( | |
244 *cmdline_wallpaper_dir_, &wallpaper_manager_command_line_); | |
245 } | |
246 | |
247 ash::DesktopBackgroundController* controller_; | |
248 PrefService* local_state_; | |
249 scoped_ptr<base::CommandLine> wallpaper_manager_command_line_; | |
250 | |
251 // Directory created by CreateCmdlineWallpapersAndSetFlags() to store default | |
252 // wallpaper images. | |
253 scoped_ptr<base::ScopedTempDir> cmdline_wallpaper_dir_; | |
254 | |
255 private: | |
256 DISALLOW_COPY_AND_ASSIGN(CustomizationWallpaperDownloaderBrowserTest); | |
257 }; | |
258 | |
259 IN_PROC_BROWSER_TEST_F(CustomizationWallpaperDownloaderBrowserTest, | |
260 OEMWallpaperIsPresent) { | |
261 CreateCmdlineWallpapers(); | |
262 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string()); | |
263 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished(); | |
264 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor( | |
265 controller_->GetWallpaper(), | |
266 wallpaper_manager_test_utils::kSmallDefaultWallpaperColor)); | |
267 | |
268 WallpaperImageFetcherFactory url_factory( | |
269 GURL(kOEMWallpaperURL), | |
270 wallpaper_manager_test_utils::kWallpaperSize, | |
271 wallpaper_manager_test_utils::kWallpaperSize, | |
272 wallpaper_manager_test_utils::kCustomWallpaperColor, | |
273 0 /* require_retries */); | |
274 | |
275 TestWallpaperObserver observer(WallpaperManager::Get()); | |
276 chromeos::ServicesCustomizationDocument* customization = | |
277 chromeos::ServicesCustomizationDocument::GetInstance(); | |
278 EXPECT_TRUE( | |
279 customization->LoadManifestFromString(std::string(kServicesManifest))); | |
280 | |
281 observer.WaitForWallpaperAnimationFinished(); | |
282 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor( | |
283 controller_->GetWallpaper(), | |
284 wallpaper_manager_test_utils::kCustomWallpaperColor)); | |
285 EXPECT_EQ(1U, url_factory.num_attempts()); | |
286 } | |
287 | |
288 IN_PROC_BROWSER_TEST_F(CustomizationWallpaperDownloaderBrowserTest, | |
289 OEMWallpaperRetryFetch) { | |
290 CreateCmdlineWallpapers(); | |
291 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string()); | |
292 wallpaper_manager_test_utils::WaitAsyncWallpaperLoadFinished(); | |
293 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor( | |
294 controller_->GetWallpaper(), | |
295 wallpaper_manager_test_utils::kSmallDefaultWallpaperColor)); | |
296 | |
297 WallpaperImageFetcherFactory url_factory( | |
298 GURL(kOEMWallpaperURL), | |
299 wallpaper_manager_test_utils::kWallpaperSize, | |
300 wallpaper_manager_test_utils::kWallpaperSize, | |
301 wallpaper_manager_test_utils::kCustomWallpaperColor, | |
302 1 /* require_retries */); | |
303 | |
304 TestWallpaperObserver observer(WallpaperManager::Get()); | |
305 chromeos::ServicesCustomizationDocument* customization = | |
306 chromeos::ServicesCustomizationDocument::GetInstance(); | |
307 EXPECT_TRUE( | |
308 customization->LoadManifestFromString(std::string(kServicesManifest))); | |
309 | |
310 observer.WaitForWallpaperAnimationFinished(); | |
311 EXPECT_TRUE(wallpaper_manager_test_utils::ImageIsNearColor( | |
312 controller_->GetWallpaper(), | |
313 wallpaper_manager_test_utils::kCustomWallpaperColor)); | |
314 | |
315 EXPECT_EQ(2U, url_factory.num_attempts()); | |
316 } | |
317 | |
318 } // namespace chromeos | |
OLD | NEW |