Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Side by Side Diff: chrome/browser/chromeos/customization_wallpaper_downloader_browsertest.cc

Issue 253833006: Add browser test for CustomizationWallpaperDownloader. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update after review. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 size_t kMinOEMWallpaperRetryIntervalSec = 10;
57
58 class TestWallpaperObserver : public WallpaperManager::Observer {
59 public:
60 explicit TestWallpaperObserver(WallpaperManager* wallpaper_manager)
61 : finished_(false), wallpaper_manager_(wallpaper_manager) {
62 DCHECK(wallpaper_manager_);
63 wallpaper_manager_->AddObserver(this);
64 }
65
66 virtual ~TestWallpaperObserver() {
67 wallpaper_manager_->RemoveObserver(this);
68 }
69
70 virtual void OnWallpaperAnimationFinished(const std::string&) OVERRIDE {
71 finished_ = true;
72 base::MessageLoop::current()->Quit();
73 }
74
75 void WaitForWallpaperAnimationFinished() {
76 while (!finished_)
77 base::RunLoop().Run();
78 }
79
80 private:
81 bool finished_;
82 WallpaperManager* wallpaper_manager_;
83
84 DISALLOW_COPY_AND_ASSIGN(TestWallpaperObserver);
85 };
86
87 // This is helper class for net::FakeURLFetcherFactory.
88 class WallpaperImageURLFetcherCallback {
89 public:
90 WallpaperImageURLFetcherCallback(
91 const GURL& url,
92 const size_t require_retries,
93 const std::vector<unsigned char>& jpeg_data_raw)
94 : url_(url), require_retries_(require_retries), factory_(NULL) {
95 jpeg_data_.resize(jpeg_data_raw.size());
96 std::copy(jpeg_data_raw.begin(), jpeg_data_raw.end(), jpeg_data_.begin());
97 }
98
99 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher(
100 const GURL& url,
101 net::URLFetcherDelegate* d,
102 const std::string& response_data,
103 net::HttpStatusCode response_code,
104 net::URLRequestStatus::Status status) {
105 attempts_.push_back(base::Time::Now());
106 if (attempts_.size() > 1) {
107 const base::TimeDelta passed =
108 attempts_.back() - attempts_[attempts_.size() - 2];
109 EXPECT_GE(passed,
110 base::TimeDelta::FromSeconds(kMinOEMWallpaperRetryIntervalSec))
111 << "Retry too fast. Actual interval " << passed.InSecondsF()
112 << " seconds, but expected at least "
113 << kMinOEMWallpaperRetryIntervalSec << " seconds.";
114 }
115 if (attempts_.size() > require_retries_) {
116 response_code = net::HTTP_OK;
117 status = net::URLRequestStatus::SUCCESS;
118 factory_->SetFakeResponse(url, response_data, response_code, status);
119 }
120 scoped_ptr<net::FakeURLFetcher> fetcher(
121 new net::FakeURLFetcher(url, d, response_data, response_code, status));
122 scoped_refptr<net::HttpResponseHeaders> download_headers =
123 new net::HttpResponseHeaders(std::string());
124 download_headers->AddHeader("Content-Type: image/jpeg");
125 fetcher->set_response_headers(download_headers);
126 return fetcher.Pass();
127 }
128
129 void Initialize(net::FakeURLFetcherFactory* factory) {
130 factory_ = factory;
131 factory_->SetFakeResponse(url_,
132 jpeg_data_,
133 net::HTTP_INTERNAL_SERVER_ERROR,
134 net::URLRequestStatus::FAILED);
135 }
136
137 size_t attempts() const { return attempts_.size(); }
138
139 private:
140 const GURL url_;
141 // Respond with OK on required retry attempt.
142 const size_t require_retries_;
143 net::FakeURLFetcherFactory* factory_;
144 std::vector<base::Time> attempts_;
145 std::string jpeg_data_;
146
147 DISALLOW_COPY_AND_ASSIGN(WallpaperImageURLFetcherCallback);
148 };
149
150 // This implements fake remote source for wallpaper image.
151 // JPEG image is created here and served to CustomizationWallpaperDownloader
152 // via net::FakeURLFetcher.
153 class WallpaperImageFetcherFactory {
154 public:
155 WallpaperImageFetcherFactory(const GURL& url,
156 int width,
157 int height,
158 SkColor color,
159 const size_t require_retries) {
160 // ASSERT_TRUE() cannot be directly used in constructor.
161 Initialize(url, width, height, color, require_retries);
162 }
163
164 ~WallpaperImageFetcherFactory() {
165 fetcher_factory_.reset();
166 net::URLFetcherImpl::set_factory(fallback_fetcher_factory_.get());
167 fallback_fetcher_factory_.reset();
168 }
169
170 size_t attempts() const { return url_callback_->attempts(); }
171
172 private:
173 void Initialize(const GURL& url,
174 int width,
175 int height,
176 SkColor color,
177 const size_t require_retries) {
178 std::vector<unsigned char> oem_wallpaper_;
179 ASSERT_TRUE(WallpaperManagerTestUtils::CreateJPEGImage(
180 width, height, color, &oem_wallpaper_));
181
182 url_callback_.reset(new WallpaperImageURLFetcherCallback(
183 url, require_retries, oem_wallpaper_));
184 fallback_fetcher_factory_.reset(new net::TestURLFetcherFactory);
185 net::URLFetcherImpl::set_factory(NULL);
186 fetcher_factory_.reset(new net::FakeURLFetcherFactory(
187 fallback_fetcher_factory_.get(),
188 base::Bind(&WallpaperImageURLFetcherCallback::CreateURLFetcher,
189 base::Unretained(url_callback_.get()))));
190 url_callback_->Initialize(fetcher_factory_.get());
191 }
192
193 scoped_ptr<WallpaperImageURLFetcherCallback> url_callback_;
194
195 // Use a test factory as a fallback so we don't have to deal with other
196 // requests.
197 scoped_ptr<net::TestURLFetcherFactory> fallback_fetcher_factory_;
198 scoped_ptr<net::FakeURLFetcherFactory> fetcher_factory_;
199
200 DISALLOW_COPY_AND_ASSIGN(WallpaperImageFetcherFactory);
201 };
202
203 } // namespace
204
205 class CustomizationWallpaperDownloaderBrowserTest
206 : public InProcessBrowserTest {
207 public:
208 CustomizationWallpaperDownloaderBrowserTest()
209 : controller_(NULL), local_state_(NULL) {
210 }
211
212 virtual ~CustomizationWallpaperDownloaderBrowserTest() {}
213
214 virtual void SetUpOnMainThread() OVERRIDE {
215 controller_ = ash::Shell::GetInstance()->desktop_background_controller();
216 local_state_ = g_browser_process->local_state();
217 }
218
219 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
220 command_line->AppendSwitch(chromeos::switches::kLoginManager);
221 command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile, "user");
222 }
223
224 virtual void CleanUpOnMainThread() OVERRIDE { controller_ = NULL; }
225
226 protected:
227
228 void CreateCmdlineWallpapers() {
229 cmdline_wallpaper_dir_.reset(new base::ScopedTempDir);
230 ASSERT_TRUE(cmdline_wallpaper_dir_->CreateUniqueTempDir());
231 WallpaperManagerTestUtils::CreateCmdlineWallpapers(
232 *cmdline_wallpaper_dir_, &wallpaper_manager_command_line_);
233 }
234
235 ash::DesktopBackgroundController* controller_;
236 PrefService* local_state_;
237 scoped_ptr<base::CommandLine> wallpaper_manager_command_line_;
238
239 // Directory created by CreateCmdlineWallpapersAndSetFlags() to store default
240 // wallpaper images.
241 scoped_ptr<base::ScopedTempDir> cmdline_wallpaper_dir_;
242
243 private:
244 DISALLOW_COPY_AND_ASSIGN(CustomizationWallpaperDownloaderBrowserTest);
245 };
246
247 IN_PROC_BROWSER_TEST_F(CustomizationWallpaperDownloaderBrowserTest,
248 OEMWallpaperIsPresent) {
249 CreateCmdlineWallpapers();
250 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
251 WallpaperManagerTestUtils::WaitAsyncWallpaperLoadFinished();
252 EXPECT_TRUE(WallpaperManagerTestUtils::ImageIsNearColor(
253 controller_->GetWallpaper(),
254 WallpaperManagerTestUtils::kSmallDefaultWallpaperColor));
255
256 WallpaperImageFetcherFactory url_factory(
257 GURL(kOEMWallpaperURL),
258 WallpaperManagerTestUtils::kWallpaperSize,
259 WallpaperManagerTestUtils::kWallpaperSize,
260 WallpaperManagerTestUtils::kCustomWallpaperColor,
261 0 /* require_retries */);
262
263 TestWallpaperObserver observer(WallpaperManager::Get());
264 chromeos::ServicesCustomizationDocument* customization =
265 chromeos::ServicesCustomizationDocument::GetInstance();
266 EXPECT_TRUE(
267 customization->LoadManifestFromString(std::string(kServicesManifest)));
268
269 observer.WaitForWallpaperAnimationFinished();
270 EXPECT_TRUE(WallpaperManagerTestUtils::ImageIsNearColor(
271 controller_->GetWallpaper(),
272 WallpaperManagerTestUtils::kCustomWallpaperColor));
273 EXPECT_EQ(size_t(1), url_factory.attempts());
274 }
275
276 IN_PROC_BROWSER_TEST_F(CustomizationWallpaperDownloaderBrowserTest,
277 OEMWallpaperRetryFetch) {
278 CreateCmdlineWallpapers();
279 WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
280 WallpaperManagerTestUtils::WaitAsyncWallpaperLoadFinished();
281 EXPECT_TRUE(WallpaperManagerTestUtils::ImageIsNearColor(
282 controller_->GetWallpaper(),
283 WallpaperManagerTestUtils::kSmallDefaultWallpaperColor));
284
285 WallpaperImageFetcherFactory url_factory(
286 GURL(kOEMWallpaperURL),
287 WallpaperManagerTestUtils::kWallpaperSize,
288 WallpaperManagerTestUtils::kWallpaperSize,
289 WallpaperManagerTestUtils::kCustomWallpaperColor,
290 1 /* require_retries */);
291
292 TestWallpaperObserver observer(WallpaperManager::Get());
293 chromeos::ServicesCustomizationDocument* customization =
294 chromeos::ServicesCustomizationDocument::GetInstance();
295 EXPECT_TRUE(
296 customization->LoadManifestFromString(std::string(kServicesManifest)));
297
298 observer.WaitForWallpaperAnimationFinished();
299 EXPECT_TRUE(WallpaperManagerTestUtils::ImageIsNearColor(
300 controller_->GetWallpaper(),
301 WallpaperManagerTestUtils::kCustomWallpaperColor));
302
303 EXPECT_EQ(size_t(2), url_factory.attempts());
304 }
305
306 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698