#include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
+using base::ContentsEqual;
namespace component_updater {
// Intercepts HTTP GET requests sent to "localhost".
typedef content::URLLocalHostRequestPrepackagedInterceptor GetInterceptor;
-base::FilePath test_file(const char* file) {
+const char kTestFileName[] = "jebgalgnebhfojomionfpkfelancnnkf.crx";
+
+base::FilePath MakeTestFilePath(const char* file) {
base::FilePath path;
PathService::Get(chrome::DIR_TEST_DATA, &path);
return path.AppendASCII("components").AppendASCII(file);
void DownloadComplete(int crx_context, const CrxDownloader::Result& result);
+ void DownloadProgress(int crx_context, const CrxDownloader::Result& result);
+
protected:
scoped_ptr<CrxDownloader> crx_downloader_;
+ CrxDownloader::DownloadCallback callback_;
+ CrxDownloader::ProgressCallback progress_callback_;
+
int crx_context_;
- int error_;
- base::FilePath response_;
- int num_calls_;
+ int num_download_complete_calls_;
+ CrxDownloader::Result download_complete_result_;
+
+ // These members are updated by DownloadProgress.
+ int num_progress_calls_;
+ CrxDownloader::Result download_progress_result_;
// A magic value for the context to be used in the tests.
static const int kExpectedContext = 0xaabb;
const int CrxDownloaderTest::kExpectedContext;
CrxDownloaderTest::CrxDownloaderTest()
- : crx_context_(0),
- error_(0),
- num_calls_(0),
+ : callback_(base::Bind(&CrxDownloaderTest::DownloadComplete,
+ base::Unretained(this),
+ kExpectedContext)),
+ progress_callback_(base::Bind(&CrxDownloaderTest::DownloadProgress,
+ base::Unretained(this),
+ kExpectedContext)),
+ crx_context_(0),
+ num_download_complete_calls_(0),
+ num_progress_calls_(0),
blocking_task_runner_(BrowserThread::GetBlockingPool()->
GetSequencedTaskRunnerWithShutdownBehavior(
BrowserThread::GetBlockingPool()->GetSequenceToken(),
}
void CrxDownloaderTest::SetUp() {
- num_calls_ = 0;
+ num_download_complete_calls_ = 0;
+ download_complete_result_ = CrxDownloader::Result();
+ num_progress_calls_ = 0;
+ download_progress_result_ = CrxDownloader::Result();
crx_downloader_.reset(CrxDownloader::Create(
- false, // Do not use the background downloader in these tests.
+ false, // Do not use the background downloader in these tests.
context_.get(),
- blocking_task_runner_,
- base::Bind(&CrxDownloaderTest::DownloadComplete,
- base::Unretained(this),
- kExpectedContext)));
+ blocking_task_runner_));
+ crx_downloader_->set_progress_callback(progress_callback_);
}
void CrxDownloaderTest::TearDown() {
+ crx_downloader_.reset();
}
void CrxDownloaderTest::Quit() {
quit_closure_.Run();
}
-void CrxDownloaderTest::DownloadComplete(
- int crx_context,
- const CrxDownloader::Result& result) {
- ++num_calls_;
+void CrxDownloaderTest::DownloadComplete(int crx_context,
+ const CrxDownloader::Result& result) {
+ ++num_download_complete_calls_;
crx_context_ = crx_context;
- error_ = result.error;
- response_ = result.response;
+ download_complete_result_ = result;
Quit();
}
+void CrxDownloaderTest::DownloadProgress(int crx_context,
+ const CrxDownloader::Result& result) {
+ ++num_progress_calls_;
+ download_progress_result_ = result;
+}
+
void CrxDownloaderTest::RunThreads() {
base::RunLoop runloop;
quit_closure_ = runloop.QuitClosure();
// Tests that starting a download without a url results in an error.
TEST_F(CrxDownloaderTest, NoUrl) {
std::vector<GURL> urls;
- crx_downloader_->StartDownload(urls);
-
+ crx_downloader_->StartDownload(urls, callback_);
RunThreadsUntilIdle();
- EXPECT_EQ(-1, error_);
- EXPECT_EQ(kExpectedContext, crx_context_);
- EXPECT_EQ(1, num_calls_);
+ EXPECT_EQ(1, num_download_complete_calls_);
+ EXPECT_EQ(kExpectedContext, crx_context_);
+ EXPECT_EQ(-1, download_complete_result_.error);
+ EXPECT_TRUE(download_complete_result_.response.empty());
+ EXPECT_EQ(-1, download_complete_result_.downloaded_bytes);
+ EXPECT_EQ(-1, download_complete_result_.total_bytes);
+ EXPECT_EQ(0, num_progress_calls_);
}
// Tests that downloading from one url is successful.
TEST_F(CrxDownloaderTest, OneUrl) {
const GURL expected_crx_url =
- GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
+ GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
+ const base::FilePath test_file(MakeTestFilePath(kTestFileName));
GetInterceptor interceptor;
- interceptor.SetResponse(
- expected_crx_url,
- test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
-
- crx_downloader_->StartDownloadFromUrl(expected_crx_url);
+ interceptor.SetResponse(expected_crx_url, test_file);
+ crx_downloader_->StartDownloadFromUrl(expected_crx_url, callback_);
RunThreads();
+
EXPECT_EQ(1, interceptor.GetHitCount());
- EXPECT_EQ(0, error_);
+
+ EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
+ EXPECT_EQ(0, download_complete_result_.error);
+ EXPECT_EQ(1843, download_complete_result_.downloaded_bytes);
+ EXPECT_EQ(1843, download_complete_result_.total_bytes);
+ EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
+
+ EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false));
- EXPECT_EQ(1, num_calls_);
+ EXPECT_LE(1, num_progress_calls_);
+ EXPECT_EQ(1843, download_progress_result_.downloaded_bytes);
+ EXPECT_EQ(1843, download_progress_result_.total_bytes);
}
// Tests that specifying from two urls has no side effects. Expect a successful
// download, and only one download request be made.
-TEST_F(CrxDownloaderTest, TwoUrls) {
+// This test is flaky on Android. crbug.com/329883
+#if defined(OS_ANDROID)
+#define MAYBE_TwoUrls DISABLED_TwoUrls
+#else
+#define MAYBE_TwoUrls TwoUrls
+#endif
+TEST_F(CrxDownloaderTest, MAYBE_TwoUrls) {
const GURL expected_crx_url =
- GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
+ GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
+ const base::FilePath test_file(MakeTestFilePath(kTestFileName));
GetInterceptor interceptor;
- interceptor.SetResponse(
- expected_crx_url,
- test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
+ interceptor.SetResponse(expected_crx_url, test_file);
std::vector<GURL> urls;
urls.push_back(expected_crx_url);
urls.push_back(expected_crx_url);
- crx_downloader_->StartDownload(urls);
-
+ crx_downloader_->StartDownload(urls, callback_);
RunThreads();
+
EXPECT_EQ(1, interceptor.GetHitCount());
- EXPECT_EQ(0, error_);
+
+ EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
+ EXPECT_EQ(0, download_complete_result_.error);
+ EXPECT_EQ(1843, download_complete_result_.downloaded_bytes);
+ EXPECT_EQ(1843, download_complete_result_.total_bytes);
+ EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
+
+ EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false));
- EXPECT_EQ(1, num_calls_);
+ EXPECT_LE(1, num_progress_calls_);
+ EXPECT_EQ(1843, download_progress_result_.downloaded_bytes);
+ EXPECT_EQ(1843, download_progress_result_.total_bytes);
}
// Tests that an invalid host results in a download error.
TEST_F(CrxDownloaderTest, OneUrl_InvalidHost) {
const GURL expected_crx_url =
- GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
+ GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
+ const base::FilePath test_file(MakeTestFilePath(kTestFileName));
GetInterceptor interceptor;
- interceptor.SetResponse(
- expected_crx_url,
- test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
+ interceptor.SetResponse(expected_crx_url, test_file);
crx_downloader_->StartDownloadFromUrl(
GURL("http://no.such.host"
- "/download/jebgalgnebhfojomionfpkfelancnnkf.crx"));
-
+ "/download/jebgalgnebhfojomionfpkfelancnnkf.crx"),
+ callback_);
RunThreads();
+
EXPECT_EQ(0, interceptor.GetHitCount());
- EXPECT_NE(0, error_);
- EXPECT_EQ(kExpectedContext, crx_context_);
- EXPECT_EQ(1, num_calls_);
+ EXPECT_EQ(1, num_download_complete_calls_);
+ EXPECT_EQ(kExpectedContext, crx_context_);
+ EXPECT_NE(0, download_complete_result_.error);
+ EXPECT_TRUE(download_complete_result_.response.empty());
}
// Tests that an invalid path results in a download error.
TEST_F(CrxDownloaderTest, OneUrl_InvalidPath) {
const GURL expected_crx_url =
- GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
+ GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
+ const base::FilePath test_file(MakeTestFilePath(kTestFileName));
GetInterceptor interceptor;
- interceptor.SetResponse(
- expected_crx_url,
- test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
-
- crx_downloader_->StartDownloadFromUrl(GURL("http://localhost/no/such/file"));
+ interceptor.SetResponse(expected_crx_url, test_file);
+ crx_downloader_->StartDownloadFromUrl(GURL("http://localhost/no/such/file"),
+ callback_);
RunThreads();
+
EXPECT_EQ(0, interceptor.GetHitCount());
- EXPECT_NE(0, error_);
- EXPECT_EQ(kExpectedContext, crx_context_);
- EXPECT_EQ(1, num_calls_);
+ EXPECT_EQ(1, num_download_complete_calls_);
+ EXPECT_EQ(kExpectedContext, crx_context_);
+ EXPECT_NE(0, download_complete_result_.error);
+ EXPECT_TRUE(download_complete_result_.response.empty());
}
// Tests that the fallback to a valid url is successful.
-TEST_F(CrxDownloaderTest, TwoUrls_FirstInvalid) {
+// This test is flaky on Android. crbug.com/329883
+#if defined(OS_ANDROID)
+#define MAYBE_TwoUrls_FirstInvalid DISABLED_TwoUrls_FirstInvalid
+#else
+#define MAYBE_TwoUrls_FirstInvalid TwoUrls_FirstInvalid
+#endif
+TEST_F(CrxDownloaderTest, MAYBE_TwoUrls_FirstInvalid) {
const GURL expected_crx_url =
- GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
+ GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
+ const base::FilePath test_file(MakeTestFilePath(kTestFileName));
GetInterceptor interceptor;
- interceptor.SetResponse(
- expected_crx_url,
- test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
+ interceptor.SetResponse(expected_crx_url, test_file);
std::vector<GURL> urls;
urls.push_back(GURL("http://localhost/no/such/file"));
urls.push_back(expected_crx_url);
- crx_downloader_->StartDownload(urls);
-
+ crx_downloader_->StartDownload(urls, callback_);
RunThreads();
+
EXPECT_EQ(1, interceptor.GetHitCount());
- EXPECT_EQ(0, error_);
+
+ EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
+ EXPECT_EQ(0, download_complete_result_.error);
+ EXPECT_EQ(1843, download_complete_result_.downloaded_bytes);
+ EXPECT_EQ(1843, download_complete_result_.total_bytes);
+ EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
+
+ EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false));
- EXPECT_EQ(1, num_calls_);
+ EXPECT_LE(1, num_progress_calls_);
+ EXPECT_EQ(1843, download_progress_result_.downloaded_bytes);
+ EXPECT_EQ(1843, download_progress_result_.total_bytes);
}
// Tests that the download succeeds if the first url is correct and the
// second bad url does not have a side-effect.
TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) {
const GURL expected_crx_url =
- GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
+ GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
+ const base::FilePath test_file(MakeTestFilePath(kTestFileName));
GetInterceptor interceptor;
- interceptor.SetResponse(
- expected_crx_url,
- test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
+ interceptor.SetResponse(expected_crx_url, test_file);
std::vector<GURL> urls;
urls.push_back(expected_crx_url);
urls.push_back(GURL("http://localhost/no/such/file"));
- crx_downloader_->StartDownload(urls);
-
+ crx_downloader_->StartDownload(urls, callback_);
RunThreads();
+
EXPECT_EQ(1, interceptor.GetHitCount());
- EXPECT_EQ(0, error_);
+
+ EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
+ EXPECT_EQ(0, download_complete_result_.error);
+ EXPECT_EQ(1843, download_complete_result_.downloaded_bytes);
+ EXPECT_EQ(1843, download_complete_result_.total_bytes);
+ EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
- EXPECT_EQ(1, num_calls_);
+ EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false));
+
+ EXPECT_LE(1, num_progress_calls_);
+ EXPECT_EQ(1843, download_progress_result_.downloaded_bytes);
+ EXPECT_EQ(1843, download_progress_result_.total_bytes);
}
// Tests that the download fails if both urls are bad.
TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) {
const GURL expected_crx_url =
- GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
+ GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
+ const base::FilePath test_file(MakeTestFilePath(kTestFileName));
GetInterceptor interceptor;
- interceptor.SetResponse(
- expected_crx_url,
- test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
+ interceptor.SetResponse(expected_crx_url, test_file);
std::vector<GURL> urls;
urls.push_back(GURL("http://localhost/no/such/file"));
urls.push_back(GURL("http://no.such.host/"
"/download/jebgalgnebhfojomionfpkfelancnnkf.crx"));
- crx_downloader_->StartDownload(urls);
-
+ crx_downloader_->StartDownload(urls, callback_);
RunThreads();
+
EXPECT_EQ(0, interceptor.GetHitCount());
- EXPECT_NE(0, error_);
- EXPECT_EQ(kExpectedContext, crx_context_);
- EXPECT_EQ(1, num_calls_);
+ EXPECT_EQ(1, num_download_complete_calls_);
+ EXPECT_EQ(kExpectedContext, crx_context_);
+ EXPECT_NE(0, download_complete_result_.error);
+ EXPECT_TRUE(download_complete_result_.response.empty());
}
} // namespace component_updater
-