| Index: chrome/browser/chromeos/customization_wallpaper_downloader_browsertest.cc
|
| diff --git a/chrome/browser/chromeos/customization_wallpaper_downloader_browsertest.cc b/chrome/browser/chromeos/customization_wallpaper_downloader_browsertest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..8c7aba9089f0c9288adfc6fc90befecd169d84a6
|
| --- /dev/null
|
| +++ b/chrome/browser/chromeos/customization_wallpaper_downloader_browsertest.cc
|
| @@ -0,0 +1,306 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +
|
| +#include <vector>
|
| +
|
| +#include "ash/desktop_background/desktop_background_controller.h"
|
| +#include "ash/shell.h"
|
| +#include "base/command_line.h"
|
| +#include "base/files/scoped_temp_dir.h"
|
| +#include "base/run_loop.h"
|
| +#include "base/time/time.h"
|
| +#include "chrome/browser/chromeos/customization_document.h"
|
| +#include "chrome/browser/chromeos/customization_wallpaper_downloader.h"
|
| +#include "chrome/browser/chromeos/login/wallpaper_manager.h"
|
| +#include "chrome/browser/chromeos/login/wallpaper_manager_test_utils.h"
|
| +#include "chrome/browser/google/google_url_tracker.h"
|
| +#include "chrome/test/base/in_process_browser_test.h"
|
| +#include "chrome/test/base/testing_browser_process.h"
|
| +#include "chromeos/chromeos_switches.h"
|
| +#include "net/http/http_response_headers.h"
|
| +#include "net/http/http_status_code.h"
|
| +#include "net/url_request/test_url_fetcher_factory.h"
|
| +#include "net/url_request/url_fetcher_impl.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace chromeos {
|
| +
|
| +namespace {
|
| +
|
| +const char kOEMWallpaperURL[] = "http://somedomain.com/image.png";
|
| +
|
| +const char kServicesManifest[] =
|
| + "{"
|
| + " \"version\": \"1.0\","
|
| + " \"default_wallpaper\": \"http://somedomain.com/image.png\",\n"
|
| + " \"default_apps\": [\n"
|
| + " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
|
| + " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
|
| + " ],\n"
|
| + " \"localized_content\": {\n"
|
| + " \"en-US\": {\n"
|
| + " \"default_apps_folder_name\": \"EN-US OEM Name\"\n"
|
| + " },\n"
|
| + " \"en\": {\n"
|
| + " \"default_apps_folder_name\": \"EN OEM Name\"\n"
|
| + " },\n"
|
| + " \"default\": {\n"
|
| + " \"default_apps_folder_name\": \"Default OEM Name\"\n"
|
| + " }\n"
|
| + " }\n"
|
| + "}";
|
| +
|
| +// Expected minimal wallpaper download retry interval in seconds.
|
| +const size_t kMinOEMWallpaperRetryIntervalSec = 10;
|
| +
|
| +class TestWallpaperObserver : public WallpaperManager::Observer {
|
| + public:
|
| + explicit TestWallpaperObserver(WallpaperManager* wallpaper_manager)
|
| + : finished_(false), wallpaper_manager_(wallpaper_manager) {
|
| + DCHECK(wallpaper_manager_);
|
| + wallpaper_manager_->AddObserver(this);
|
| + }
|
| +
|
| + virtual ~TestWallpaperObserver() {
|
| + wallpaper_manager_->RemoveObserver(this);
|
| + }
|
| +
|
| + virtual void OnWallpaperAnimationFinished(const std::string&) OVERRIDE {
|
| + finished_ = true;
|
| + base::MessageLoop::current()->Quit();
|
| + }
|
| +
|
| + void WaitForWallpaperAnimationFinished() {
|
| + while (!finished_)
|
| + base::RunLoop().Run();
|
| + }
|
| +
|
| + private:
|
| + bool finished_;
|
| + WallpaperManager* wallpaper_manager_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(TestWallpaperObserver);
|
| +};
|
| +
|
| +// This is helper class for net::FakeURLFetcherFactory.
|
| +class WallpaperImageURLFetcherCallback {
|
| + public:
|
| + WallpaperImageURLFetcherCallback(
|
| + const GURL& url,
|
| + const size_t require_retries,
|
| + const std::vector<unsigned char>& jpeg_data_raw)
|
| + : url_(url), require_retries_(require_retries), factory_(NULL) {
|
| + jpeg_data_.resize(jpeg_data_raw.size());
|
| + std::copy(jpeg_data_raw.begin(), jpeg_data_raw.end(), jpeg_data_.begin());
|
| + }
|
| +
|
| + scoped_ptr<net::FakeURLFetcher> CreateURLFetcher(
|
| + const GURL& url,
|
| + net::URLFetcherDelegate* d,
|
| + const std::string& response_data,
|
| + net::HttpStatusCode response_code,
|
| + net::URLRequestStatus::Status status) {
|
| + attempts_.push_back(base::Time::Now());
|
| + if (attempts_.size() > 1) {
|
| + const base::TimeDelta passed =
|
| + attempts_.back() - attempts_[attempts_.size() - 2];
|
| + EXPECT_GE(passed,
|
| + base::TimeDelta::FromSeconds(kMinOEMWallpaperRetryIntervalSec))
|
| + << "Retry too fast. Actual interval " << passed.InSecondsF()
|
| + << " seconds, but expected at least "
|
| + << kMinOEMWallpaperRetryIntervalSec << " seconds.";
|
| + }
|
| + if (attempts_.size() > require_retries_) {
|
| + response_code = net::HTTP_OK;
|
| + status = net::URLRequestStatus::SUCCESS;
|
| + factory_->SetFakeResponse(url, response_data, response_code, status);
|
| + }
|
| + scoped_ptr<net::FakeURLFetcher> fetcher(
|
| + new net::FakeURLFetcher(url, d, response_data, response_code, status));
|
| + scoped_refptr<net::HttpResponseHeaders> download_headers =
|
| + new net::HttpResponseHeaders(std::string());
|
| + download_headers->AddHeader("Content-Type: image/jpeg");
|
| + fetcher->set_response_headers(download_headers);
|
| + return fetcher.Pass();
|
| + }
|
| +
|
| + void Initialize(net::FakeURLFetcherFactory* factory) {
|
| + factory_ = factory;
|
| + factory_->SetFakeResponse(url_,
|
| + jpeg_data_,
|
| + net::HTTP_INTERNAL_SERVER_ERROR,
|
| + net::URLRequestStatus::FAILED);
|
| + }
|
| +
|
| + size_t attempts() const { return attempts_.size(); }
|
| +
|
| + private:
|
| + const GURL url_;
|
| + // Respond with OK on required retry attempt.
|
| + const size_t require_retries_;
|
| + net::FakeURLFetcherFactory* factory_;
|
| + std::vector<base::Time> attempts_;
|
| + std::string jpeg_data_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(WallpaperImageURLFetcherCallback);
|
| +};
|
| +
|
| +// This implements fake remote source for wallpaper image.
|
| +// JPEG image is created here and served to CustomizationWallpaperDownloader
|
| +// via net::FakeURLFetcher.
|
| +class WallpaperImageFetcherFactory {
|
| + public:
|
| + WallpaperImageFetcherFactory(const GURL& url,
|
| + int width,
|
| + int height,
|
| + SkColor color,
|
| + const size_t require_retries) {
|
| + // ASSERT_TRUE() cannot be directly used in constructor.
|
| + Initialize(url, width, height, color, require_retries);
|
| + }
|
| +
|
| + ~WallpaperImageFetcherFactory() {
|
| + fetcher_factory_.reset();
|
| + net::URLFetcherImpl::set_factory(fallback_fetcher_factory_.get());
|
| + fallback_fetcher_factory_.reset();
|
| + }
|
| +
|
| + size_t attempts() const { return url_callback_->attempts(); }
|
| +
|
| + private:
|
| + void Initialize(const GURL& url,
|
| + int width,
|
| + int height,
|
| + SkColor color,
|
| + const size_t require_retries) {
|
| + std::vector<unsigned char> oem_wallpaper_;
|
| + ASSERT_TRUE(WallpaperManagerTestUtils::CreateJPEGImage(
|
| + width, height, color, &oem_wallpaper_));
|
| +
|
| + url_callback_.reset(new WallpaperImageURLFetcherCallback(
|
| + url, require_retries, oem_wallpaper_));
|
| + fallback_fetcher_factory_.reset(new net::TestURLFetcherFactory);
|
| + net::URLFetcherImpl::set_factory(NULL);
|
| + fetcher_factory_.reset(new net::FakeURLFetcherFactory(
|
| + fallback_fetcher_factory_.get(),
|
| + base::Bind(&WallpaperImageURLFetcherCallback::CreateURLFetcher,
|
| + base::Unretained(url_callback_.get()))));
|
| + url_callback_->Initialize(fetcher_factory_.get());
|
| + }
|
| +
|
| + scoped_ptr<WallpaperImageURLFetcherCallback> url_callback_;
|
| +
|
| + // Use a test factory as a fallback so we don't have to deal with other
|
| + // requests.
|
| + scoped_ptr<net::TestURLFetcherFactory> fallback_fetcher_factory_;
|
| + scoped_ptr<net::FakeURLFetcherFactory> fetcher_factory_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(WallpaperImageFetcherFactory);
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| +class CustomizationWallpaperDownloaderBrowserTest
|
| + : public InProcessBrowserTest {
|
| + public:
|
| + CustomizationWallpaperDownloaderBrowserTest()
|
| + : controller_(NULL), local_state_(NULL) {
|
| + }
|
| +
|
| + virtual ~CustomizationWallpaperDownloaderBrowserTest() {}
|
| +
|
| + virtual void SetUpOnMainThread() OVERRIDE {
|
| + controller_ = ash::Shell::GetInstance()->desktop_background_controller();
|
| + local_state_ = g_browser_process->local_state();
|
| + }
|
| +
|
| + virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
|
| + command_line->AppendSwitch(chromeos::switches::kLoginManager);
|
| + command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile, "user");
|
| + }
|
| +
|
| + virtual void CleanUpOnMainThread() OVERRIDE { controller_ = NULL; }
|
| +
|
| + protected:
|
| +
|
| + void CreateCmdlineWallpapers() {
|
| + cmdline_wallpaper_dir_.reset(new base::ScopedTempDir);
|
| + ASSERT_TRUE(cmdline_wallpaper_dir_->CreateUniqueTempDir());
|
| + WallpaperManagerTestUtils::CreateCmdlineWallpapers(
|
| + *cmdline_wallpaper_dir_, &wallpaper_manager_command_line_);
|
| + }
|
| +
|
| + ash::DesktopBackgroundController* controller_;
|
| + PrefService* local_state_;
|
| + scoped_ptr<base::CommandLine> wallpaper_manager_command_line_;
|
| +
|
| + // Directory created by CreateCmdlineWallpapersAndSetFlags() to store default
|
| + // wallpaper images.
|
| + scoped_ptr<base::ScopedTempDir> cmdline_wallpaper_dir_;
|
| +
|
| + private:
|
| + DISALLOW_COPY_AND_ASSIGN(CustomizationWallpaperDownloaderBrowserTest);
|
| +};
|
| +
|
| +IN_PROC_BROWSER_TEST_F(CustomizationWallpaperDownloaderBrowserTest,
|
| + OEMWallpaperIsPresent) {
|
| + CreateCmdlineWallpapers();
|
| + WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
|
| + WallpaperManagerTestUtils::WaitAsyncWallpaperLoadFinished();
|
| + EXPECT_TRUE(WallpaperManagerTestUtils::ImageIsNearColor(
|
| + controller_->GetWallpaper(),
|
| + WallpaperManagerTestUtils::kSmallDefaultWallpaperColor));
|
| +
|
| + WallpaperImageFetcherFactory url_factory(
|
| + GURL(kOEMWallpaperURL),
|
| + WallpaperManagerTestUtils::kWallpaperSize,
|
| + WallpaperManagerTestUtils::kWallpaperSize,
|
| + WallpaperManagerTestUtils::kCustomWallpaperColor,
|
| + 0 /* require_retries */);
|
| +
|
| + TestWallpaperObserver observer(WallpaperManager::Get());
|
| + chromeos::ServicesCustomizationDocument* customization =
|
| + chromeos::ServicesCustomizationDocument::GetInstance();
|
| + EXPECT_TRUE(
|
| + customization->LoadManifestFromString(std::string(kServicesManifest)));
|
| +
|
| + observer.WaitForWallpaperAnimationFinished();
|
| + EXPECT_TRUE(WallpaperManagerTestUtils::ImageIsNearColor(
|
| + controller_->GetWallpaper(),
|
| + WallpaperManagerTestUtils::kCustomWallpaperColor));
|
| + EXPECT_EQ(size_t(1), url_factory.attempts());
|
| +}
|
| +
|
| +IN_PROC_BROWSER_TEST_F(CustomizationWallpaperDownloaderBrowserTest,
|
| + OEMWallpaperRetryFetch) {
|
| + CreateCmdlineWallpapers();
|
| + WallpaperManager::Get()->SetDefaultWallpaperNow(std::string());
|
| + WallpaperManagerTestUtils::WaitAsyncWallpaperLoadFinished();
|
| + EXPECT_TRUE(WallpaperManagerTestUtils::ImageIsNearColor(
|
| + controller_->GetWallpaper(),
|
| + WallpaperManagerTestUtils::kSmallDefaultWallpaperColor));
|
| +
|
| + WallpaperImageFetcherFactory url_factory(
|
| + GURL(kOEMWallpaperURL),
|
| + WallpaperManagerTestUtils::kWallpaperSize,
|
| + WallpaperManagerTestUtils::kWallpaperSize,
|
| + WallpaperManagerTestUtils::kCustomWallpaperColor,
|
| + 1 /* require_retries */);
|
| +
|
| + TestWallpaperObserver observer(WallpaperManager::Get());
|
| + chromeos::ServicesCustomizationDocument* customization =
|
| + chromeos::ServicesCustomizationDocument::GetInstance();
|
| + EXPECT_TRUE(
|
| + customization->LoadManifestFromString(std::string(kServicesManifest)));
|
| +
|
| + observer.WaitForWallpaperAnimationFinished();
|
| + EXPECT_TRUE(WallpaperManagerTestUtils::ImageIsNearColor(
|
| + controller_->GetWallpaper(),
|
| + WallpaperManagerTestUtils::kCustomWallpaperColor));
|
| +
|
| + EXPECT_EQ(size_t(2), url_factory.attempts());
|
| +}
|
| +
|
| +} // namespace chromeos
|
|
|