| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 10 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 11 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 12 #include "base/threading/sequenced_worker_pool.h" | |
| 13 #include "chrome/common/chrome_paths.h" | |
| 14 #include "components/component_updater/crx_downloader.h" | 13 #include "components/component_updater/crx_downloader.h" |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "content/public/test/test_browser_thread_bundle.h" | |
| 17 #include "content/test/net/url_request_prepackaged_interceptor.h" | |
| 18 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 15 #include "net/url_request/test_url_request_interceptor.h" |
| 19 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 18 |
| 22 using content::BrowserThread; | |
| 23 using base::ContentsEqual; | 19 using base::ContentsEqual; |
| 24 | 20 |
| 25 namespace component_updater { | 21 namespace component_updater { |
| 26 | 22 |
| 27 namespace { | 23 namespace { |
| 28 | 24 |
| 29 // Intercepts HTTP GET requests sent to "localhost". | 25 // Intercepts HTTP GET requests sent to "localhost". |
| 30 typedef content::URLLocalHostRequestPrepackagedInterceptor GetInterceptor; | 26 typedef net::LocalHostTestURLRequestInterceptor GetInterceptor; |
| 31 | 27 |
| 32 const char kTestFileName[] = "jebgalgnebhfojomionfpkfelancnnkf.crx"; | 28 const char kTestFileName[] = "jebgalgnebhfojomionfpkfelancnnkf.crx"; |
| 33 | 29 |
| 34 base::FilePath MakeTestFilePath(const char* file) { | 30 base::FilePath MakeTestFilePath(const char* file) { |
| 35 base::FilePath path; | 31 base::FilePath path; |
| 36 PathService::Get(chrome::DIR_TEST_DATA, &path); | 32 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 37 return path.AppendASCII("components").AppendASCII(file); | 33 return path.AppendASCII("components") |
| 34 .AppendASCII("test") |
| 35 .AppendASCII("data") |
| 36 .AppendASCII("component_updater") |
| 37 .AppendASCII(file); |
| 38 } | 38 } |
| 39 | 39 |
| 40 } // namespace | 40 } // namespace |
| 41 | 41 |
| 42 class CrxDownloaderTest : public testing::Test { | 42 class CrxDownloaderTest : public testing::Test { |
| 43 public: | 43 public: |
| 44 CrxDownloaderTest(); | 44 CrxDownloaderTest(); |
| 45 virtual ~CrxDownloaderTest(); | 45 virtual ~CrxDownloaderTest(); |
| 46 | 46 |
| 47 // Overrides from testing::Test. | 47 // Overrides from testing::Test. |
| 48 virtual void SetUp() OVERRIDE; | 48 virtual void SetUp() OVERRIDE; |
| 49 virtual void TearDown() OVERRIDE; | 49 virtual void TearDown() OVERRIDE; |
| 50 | 50 |
| 51 void Quit(); | 51 void Quit(); |
| 52 void RunThreads(); | 52 void RunThreads(); |
| 53 void RunThreadsUntilIdle(); | 53 void RunThreadsUntilIdle(); |
| 54 | 54 |
| 55 void DownloadComplete(int crx_context, const CrxDownloader::Result& result); | 55 void DownloadComplete(int crx_context, const CrxDownloader::Result& result); |
| 56 | 56 |
| 57 void DownloadProgress(int crx_context, const CrxDownloader::Result& result); | 57 void DownloadProgress(int crx_context, const CrxDownloader::Result& result); |
| 58 | 58 |
| 59 protected: | 59 protected: |
| 60 scoped_ptr<CrxDownloader> crx_downloader_; | 60 scoped_ptr<CrxDownloader> crx_downloader_; |
| 61 | 61 |
| 62 scoped_ptr<GetInterceptor> get_interceptor_; |
| 63 |
| 62 CrxDownloader::DownloadCallback callback_; | 64 CrxDownloader::DownloadCallback callback_; |
| 63 CrxDownloader::ProgressCallback progress_callback_; | 65 CrxDownloader::ProgressCallback progress_callback_; |
| 64 | 66 |
| 65 int crx_context_; | 67 int crx_context_; |
| 66 | 68 |
| 67 int num_download_complete_calls_; | 69 int num_download_complete_calls_; |
| 68 CrxDownloader::Result download_complete_result_; | 70 CrxDownloader::Result download_complete_result_; |
| 69 | 71 |
| 70 // These members are updated by DownloadProgress. | 72 // These members are updated by DownloadProgress. |
| 71 int num_progress_calls_; | 73 int num_progress_calls_; |
| 72 CrxDownloader::Result download_progress_result_; | 74 CrxDownloader::Result download_progress_result_; |
| 73 | 75 |
| 74 // A magic value for the context to be used in the tests. | 76 // A magic value for the context to be used in the tests. |
| 75 static const int kExpectedContext = 0xaabb; | 77 static const int kExpectedContext = 0xaabb; |
| 76 | 78 |
| 77 private: | 79 private: |
| 78 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; | 80 base::MessageLoopForIO loop_; |
| 79 scoped_refptr<net::TestURLRequestContextGetter> context_; | 81 scoped_refptr<net::TestURLRequestContextGetter> context_; |
| 80 content::TestBrowserThreadBundle thread_bundle_; | |
| 81 base::Closure quit_closure_; | 82 base::Closure quit_closure_; |
| 82 }; | 83 }; |
| 83 | 84 |
| 84 const int CrxDownloaderTest::kExpectedContext; | 85 const int CrxDownloaderTest::kExpectedContext; |
| 85 | 86 |
| 86 CrxDownloaderTest::CrxDownloaderTest() | 87 CrxDownloaderTest::CrxDownloaderTest() |
| 87 : callback_(base::Bind(&CrxDownloaderTest::DownloadComplete, | 88 : callback_(base::Bind(&CrxDownloaderTest::DownloadComplete, |
| 88 base::Unretained(this), | 89 base::Unretained(this), |
| 89 kExpectedContext)), | 90 kExpectedContext)), |
| 90 progress_callback_(base::Bind(&CrxDownloaderTest::DownloadProgress, | 91 progress_callback_(base::Bind(&CrxDownloaderTest::DownloadProgress, |
| 91 base::Unretained(this), | 92 base::Unretained(this), |
| 92 kExpectedContext)), | 93 kExpectedContext)), |
| 93 crx_context_(0), | 94 crx_context_(0), |
| 94 num_download_complete_calls_(0), | 95 num_download_complete_calls_(0), |
| 95 num_progress_calls_(0), | 96 num_progress_calls_(0), |
| 96 blocking_task_runner_(BrowserThread::GetBlockingPool()-> | |
| 97 GetSequencedTaskRunnerWithShutdownBehavior( | |
| 98 BrowserThread::GetBlockingPool()->GetSequenceToken(), | |
| 99 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), | |
| 100 context_(new net::TestURLRequestContextGetter( | 97 context_(new net::TestURLRequestContextGetter( |
| 101 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))), | 98 base::MessageLoopProxy::current())) { |
| 102 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | |
| 103 } | 99 } |
| 104 | 100 |
| 105 CrxDownloaderTest::~CrxDownloaderTest() { | 101 CrxDownloaderTest::~CrxDownloaderTest() { |
| 106 context_ = NULL; | 102 context_ = NULL; |
| 103 |
| 104 // The GetInterceptor requires the message loop to run to destruct correctly. |
| 105 get_interceptor_.reset(); |
| 106 RunThreadsUntilIdle(); |
| 107 } | 107 } |
| 108 | 108 |
| 109 void CrxDownloaderTest::SetUp() { | 109 void CrxDownloaderTest::SetUp() { |
| 110 num_download_complete_calls_ = 0; | 110 num_download_complete_calls_ = 0; |
| 111 download_complete_result_ = CrxDownloader::Result(); | 111 download_complete_result_ = CrxDownloader::Result(); |
| 112 num_progress_calls_ = 0; | 112 num_progress_calls_ = 0; |
| 113 download_progress_result_ = CrxDownloader::Result(); | 113 download_progress_result_ = CrxDownloader::Result(); |
| 114 |
| 114 crx_downloader_.reset(CrxDownloader::Create( | 115 crx_downloader_.reset(CrxDownloader::Create( |
| 115 false, // Do not use the background downloader in these tests. | 116 false, // Do not use the background downloader in these tests. |
| 116 context_.get(), | 117 context_.get(), |
| 117 blocking_task_runner_, | 118 base::MessageLoopProxy::current(), |
| 118 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE))); | 119 NULL)); // No |background_task_runner| because no background downloader. |
| 119 crx_downloader_->set_progress_callback(progress_callback_); | 120 crx_downloader_->set_progress_callback(progress_callback_); |
| 121 |
| 122 get_interceptor_.reset(new GetInterceptor(base::MessageLoopProxy::current(), |
| 123 base::MessageLoopProxy::current())); |
| 120 } | 124 } |
| 121 | 125 |
| 122 void CrxDownloaderTest::TearDown() { | 126 void CrxDownloaderTest::TearDown() { |
| 123 crx_downloader_.reset(); | 127 crx_downloader_.reset(); |
| 124 } | 128 } |
| 125 | 129 |
| 126 void CrxDownloaderTest::Quit() { | 130 void CrxDownloaderTest::Quit() { |
| 127 if (!quit_closure_.is_null()) | 131 if (!quit_closure_.is_null()) |
| 128 quit_closure_.Run(); | 132 quit_closure_.Run(); |
| 129 } | 133 } |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 EXPECT_EQ(-1, download_complete_result_.total_bytes); | 176 EXPECT_EQ(-1, download_complete_result_.total_bytes); |
| 173 EXPECT_EQ(0, num_progress_calls_); | 177 EXPECT_EQ(0, num_progress_calls_); |
| 174 } | 178 } |
| 175 | 179 |
| 176 // Tests that downloading from one url is successful. | 180 // Tests that downloading from one url is successful. |
| 177 TEST_F(CrxDownloaderTest, OneUrl) { | 181 TEST_F(CrxDownloaderTest, OneUrl) { |
| 178 const GURL expected_crx_url = | 182 const GURL expected_crx_url = |
| 179 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | 183 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
| 180 | 184 |
| 181 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | 185 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
| 182 GetInterceptor interceptor; | 186 get_interceptor_->SetResponse(expected_crx_url, test_file); |
| 183 interceptor.SetResponse(expected_crx_url, test_file); | |
| 184 | 187 |
| 185 crx_downloader_->StartDownloadFromUrl(expected_crx_url, callback_); | 188 crx_downloader_->StartDownloadFromUrl(expected_crx_url, callback_); |
| 186 RunThreads(); | 189 RunThreads(); |
| 187 | 190 |
| 188 EXPECT_EQ(1, interceptor.GetHitCount()); | 191 EXPECT_EQ(1, get_interceptor_->GetHitCount()); |
| 189 | 192 |
| 190 EXPECT_EQ(1, num_download_complete_calls_); | 193 EXPECT_EQ(1, num_download_complete_calls_); |
| 191 EXPECT_EQ(kExpectedContext, crx_context_); | 194 EXPECT_EQ(kExpectedContext, crx_context_); |
| 192 EXPECT_EQ(0, download_complete_result_.error); | 195 EXPECT_EQ(0, download_complete_result_.error); |
| 193 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); | 196 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); |
| 194 EXPECT_EQ(1843, download_complete_result_.total_bytes); | 197 EXPECT_EQ(1843, download_complete_result_.total_bytes); |
| 195 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); | 198 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); |
| 196 | 199 |
| 197 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false)); | 200 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false)); |
| 198 | 201 |
| 199 EXPECT_LE(1, num_progress_calls_); | 202 EXPECT_LE(1, num_progress_calls_); |
| 200 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes); | 203 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes); |
| 201 EXPECT_EQ(1843, download_progress_result_.total_bytes); | 204 EXPECT_EQ(1843, download_progress_result_.total_bytes); |
| 202 } | 205 } |
| 203 | 206 |
| 204 // Tests that specifying from two urls has no side effects. Expect a successful | 207 // Tests that specifying from two urls has no side effects. Expect a successful |
| 205 // download, and only one download request be made. | 208 // download, and only one download request be made. |
| 206 // This test is flaky on Android. crbug.com/329883 | 209 // This test is flaky on Android. crbug.com/329883 |
| 207 #if defined(OS_ANDROID) | 210 #if defined(OS_ANDROID) |
| 208 #define MAYBE_TwoUrls DISABLED_TwoUrls | 211 #define MAYBE_TwoUrls DISABLED_TwoUrls |
| 209 #else | 212 #else |
| 210 #define MAYBE_TwoUrls TwoUrls | 213 #define MAYBE_TwoUrls TwoUrls |
| 211 #endif | 214 #endif |
| 212 TEST_F(CrxDownloaderTest, MAYBE_TwoUrls) { | 215 TEST_F(CrxDownloaderTest, MAYBE_TwoUrls) { |
| 213 const GURL expected_crx_url = | 216 const GURL expected_crx_url = |
| 214 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | 217 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
| 215 | 218 |
| 216 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | 219 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
| 217 GetInterceptor interceptor; | 220 get_interceptor_->SetResponse(expected_crx_url, test_file); |
| 218 interceptor.SetResponse(expected_crx_url, test_file); | |
| 219 | 221 |
| 220 std::vector<GURL> urls; | 222 std::vector<GURL> urls; |
| 221 urls.push_back(expected_crx_url); | 223 urls.push_back(expected_crx_url); |
| 222 urls.push_back(expected_crx_url); | 224 urls.push_back(expected_crx_url); |
| 223 | 225 |
| 224 crx_downloader_->StartDownload(urls, callback_); | 226 crx_downloader_->StartDownload(urls, callback_); |
| 225 RunThreads(); | 227 RunThreads(); |
| 226 | 228 |
| 227 EXPECT_EQ(1, interceptor.GetHitCount()); | 229 EXPECT_EQ(1, get_interceptor_->GetHitCount()); |
| 228 | 230 |
| 229 EXPECT_EQ(1, num_download_complete_calls_); | 231 EXPECT_EQ(1, num_download_complete_calls_); |
| 230 EXPECT_EQ(kExpectedContext, crx_context_); | 232 EXPECT_EQ(kExpectedContext, crx_context_); |
| 231 EXPECT_EQ(0, download_complete_result_.error); | 233 EXPECT_EQ(0, download_complete_result_.error); |
| 232 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); | 234 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); |
| 233 EXPECT_EQ(1843, download_complete_result_.total_bytes); | 235 EXPECT_EQ(1843, download_complete_result_.total_bytes); |
| 234 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); | 236 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); |
| 235 | 237 |
| 236 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false)); | 238 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false)); |
| 237 | 239 |
| 238 EXPECT_LE(1, num_progress_calls_); | 240 EXPECT_LE(1, num_progress_calls_); |
| 239 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes); | 241 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes); |
| 240 EXPECT_EQ(1843, download_progress_result_.total_bytes); | 242 EXPECT_EQ(1843, download_progress_result_.total_bytes); |
| 241 } | 243 } |
| 242 | 244 |
| 243 // Tests that an invalid host results in a download error. | 245 // Tests that an invalid host results in a download error. |
| 244 TEST_F(CrxDownloaderTest, OneUrl_InvalidHost) { | 246 TEST_F(CrxDownloaderTest, OneUrl_InvalidHost) { |
| 245 const GURL expected_crx_url = | 247 const GURL expected_crx_url = |
| 246 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | 248 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
| 247 | 249 |
| 248 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | 250 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
| 249 GetInterceptor interceptor; | 251 get_interceptor_->SetResponse(expected_crx_url, test_file); |
| 250 interceptor.SetResponse(expected_crx_url, test_file); | |
| 251 | 252 |
| 252 crx_downloader_->StartDownloadFromUrl( | 253 crx_downloader_->StartDownloadFromUrl( |
| 253 GURL("http://no.such.host" | 254 GURL( |
| 254 "/download/jebgalgnebhfojomionfpkfelancnnkf.crx"), | 255 "http://no.such.host" |
| 256 "/download/jebgalgnebhfojomionfpkfelancnnkf.crx"), |
| 255 callback_); | 257 callback_); |
| 256 RunThreads(); | 258 RunThreads(); |
| 257 | 259 |
| 258 EXPECT_EQ(0, interceptor.GetHitCount()); | 260 EXPECT_EQ(0, get_interceptor_->GetHitCount()); |
| 259 | 261 |
| 260 EXPECT_EQ(1, num_download_complete_calls_); | 262 EXPECT_EQ(1, num_download_complete_calls_); |
| 261 EXPECT_EQ(kExpectedContext, crx_context_); | 263 EXPECT_EQ(kExpectedContext, crx_context_); |
| 262 EXPECT_NE(0, download_complete_result_.error); | 264 EXPECT_NE(0, download_complete_result_.error); |
| 263 EXPECT_TRUE(download_complete_result_.response.empty()); | 265 EXPECT_TRUE(download_complete_result_.response.empty()); |
| 264 } | 266 } |
| 265 | 267 |
| 266 // Tests that an invalid path results in a download error. | 268 // Tests that an invalid path results in a download error. |
| 267 TEST_F(CrxDownloaderTest, OneUrl_InvalidPath) { | 269 TEST_F(CrxDownloaderTest, OneUrl_InvalidPath) { |
| 268 const GURL expected_crx_url = | 270 const GURL expected_crx_url = |
| 269 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | 271 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
| 270 | 272 |
| 271 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | 273 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
| 272 GetInterceptor interceptor; | 274 get_interceptor_->SetResponse(expected_crx_url, test_file); |
| 273 interceptor.SetResponse(expected_crx_url, test_file); | |
| 274 | 275 |
| 275 crx_downloader_->StartDownloadFromUrl(GURL("http://localhost/no/such/file"), | 276 crx_downloader_->StartDownloadFromUrl(GURL("http://localhost/no/such/file"), |
| 276 callback_); | 277 callback_); |
| 277 RunThreads(); | 278 RunThreads(); |
| 278 | 279 |
| 279 EXPECT_EQ(0, interceptor.GetHitCount()); | 280 EXPECT_EQ(0, get_interceptor_->GetHitCount()); |
| 280 | 281 |
| 281 EXPECT_EQ(1, num_download_complete_calls_); | 282 EXPECT_EQ(1, num_download_complete_calls_); |
| 282 EXPECT_EQ(kExpectedContext, crx_context_); | 283 EXPECT_EQ(kExpectedContext, crx_context_); |
| 283 EXPECT_NE(0, download_complete_result_.error); | 284 EXPECT_NE(0, download_complete_result_.error); |
| 284 EXPECT_TRUE(download_complete_result_.response.empty()); | 285 EXPECT_TRUE(download_complete_result_.response.empty()); |
| 285 } | 286 } |
| 286 | 287 |
| 287 // Tests that the fallback to a valid url is successful. | 288 // Tests that the fallback to a valid url is successful. |
| 288 // This test is flaky on Android. crbug.com/329883 | 289 // This test is flaky on Android. crbug.com/329883 |
| 289 #if defined(OS_ANDROID) | 290 #if defined(OS_ANDROID) |
| 290 #define MAYBE_TwoUrls_FirstInvalid DISABLED_TwoUrls_FirstInvalid | 291 #define MAYBE_TwoUrls_FirstInvalid DISABLED_TwoUrls_FirstInvalid |
| 291 #else | 292 #else |
| 292 #define MAYBE_TwoUrls_FirstInvalid TwoUrls_FirstInvalid | 293 #define MAYBE_TwoUrls_FirstInvalid TwoUrls_FirstInvalid |
| 293 #endif | 294 #endif |
| 294 TEST_F(CrxDownloaderTest, MAYBE_TwoUrls_FirstInvalid) { | 295 TEST_F(CrxDownloaderTest, MAYBE_TwoUrls_FirstInvalid) { |
| 295 const GURL expected_crx_url = | 296 const GURL expected_crx_url = |
| 296 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | 297 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
| 297 | 298 |
| 298 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | 299 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
| 299 GetInterceptor interceptor; | 300 get_interceptor_->SetResponse(expected_crx_url, test_file); |
| 300 interceptor.SetResponse(expected_crx_url, test_file); | |
| 301 | 301 |
| 302 std::vector<GURL> urls; | 302 std::vector<GURL> urls; |
| 303 urls.push_back(GURL("http://localhost/no/such/file")); | 303 urls.push_back(GURL("http://localhost/no/such/file")); |
| 304 urls.push_back(expected_crx_url); | 304 urls.push_back(expected_crx_url); |
| 305 | 305 |
| 306 crx_downloader_->StartDownload(urls, callback_); | 306 crx_downloader_->StartDownload(urls, callback_); |
| 307 RunThreads(); | 307 RunThreads(); |
| 308 | 308 |
| 309 EXPECT_EQ(1, interceptor.GetHitCount()); | 309 EXPECT_EQ(1, get_interceptor_->GetHitCount()); |
| 310 | 310 |
| 311 EXPECT_EQ(1, num_download_complete_calls_); | 311 EXPECT_EQ(1, num_download_complete_calls_); |
| 312 EXPECT_EQ(kExpectedContext, crx_context_); | 312 EXPECT_EQ(kExpectedContext, crx_context_); |
| 313 EXPECT_EQ(0, download_complete_result_.error); | 313 EXPECT_EQ(0, download_complete_result_.error); |
| 314 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); | 314 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); |
| 315 EXPECT_EQ(1843, download_complete_result_.total_bytes); | 315 EXPECT_EQ(1843, download_complete_result_.total_bytes); |
| 316 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); | 316 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); |
| 317 | 317 |
| 318 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false)); | 318 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false)); |
| 319 | 319 |
| 320 EXPECT_LE(1, num_progress_calls_); | 320 EXPECT_LE(1, num_progress_calls_); |
| 321 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes); | 321 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes); |
| 322 EXPECT_EQ(1843, download_progress_result_.total_bytes); | 322 EXPECT_EQ(1843, download_progress_result_.total_bytes); |
| 323 } | 323 } |
| 324 | 324 |
| 325 // Tests that the download succeeds if the first url is correct and the | 325 // Tests that the download succeeds if the first url is correct and the |
| 326 // second bad url does not have a side-effect. | 326 // second bad url does not have a side-effect. |
| 327 TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) { | 327 TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) { |
| 328 const GURL expected_crx_url = | 328 const GURL expected_crx_url = |
| 329 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | 329 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
| 330 | 330 |
| 331 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | 331 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
| 332 GetInterceptor interceptor; | 332 get_interceptor_->SetResponse(expected_crx_url, test_file); |
| 333 interceptor.SetResponse(expected_crx_url, test_file); | |
| 334 | 333 |
| 335 std::vector<GURL> urls; | 334 std::vector<GURL> urls; |
| 336 urls.push_back(expected_crx_url); | 335 urls.push_back(expected_crx_url); |
| 337 urls.push_back(GURL("http://localhost/no/such/file")); | 336 urls.push_back(GURL("http://localhost/no/such/file")); |
| 338 | 337 |
| 339 crx_downloader_->StartDownload(urls, callback_); | 338 crx_downloader_->StartDownload(urls, callback_); |
| 340 RunThreads(); | 339 RunThreads(); |
| 341 | 340 |
| 342 EXPECT_EQ(1, interceptor.GetHitCount()); | 341 EXPECT_EQ(1, get_interceptor_->GetHitCount()); |
| 343 | 342 |
| 344 EXPECT_EQ(1, num_download_complete_calls_); | 343 EXPECT_EQ(1, num_download_complete_calls_); |
| 345 EXPECT_EQ(kExpectedContext, crx_context_); | 344 EXPECT_EQ(kExpectedContext, crx_context_); |
| 346 EXPECT_EQ(0, download_complete_result_.error); | 345 EXPECT_EQ(0, download_complete_result_.error); |
| 347 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); | 346 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); |
| 348 EXPECT_EQ(1843, download_complete_result_.total_bytes); | 347 EXPECT_EQ(1843, download_complete_result_.total_bytes); |
| 349 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); | 348 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); |
| 350 | 349 |
| 351 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false)); | 350 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false)); |
| 352 | 351 |
| 353 EXPECT_LE(1, num_progress_calls_); | 352 EXPECT_LE(1, num_progress_calls_); |
| 354 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes); | 353 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes); |
| 355 EXPECT_EQ(1843, download_progress_result_.total_bytes); | 354 EXPECT_EQ(1843, download_progress_result_.total_bytes); |
| 356 } | 355 } |
| 357 | 356 |
| 358 // Tests that the download fails if both urls are bad. | 357 // Tests that the download fails if both urls are bad. |
| 359 TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) { | 358 TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) { |
| 360 const GURL expected_crx_url = | 359 const GURL expected_crx_url = |
| 361 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | 360 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
| 362 | 361 |
| 363 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | 362 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
| 364 GetInterceptor interceptor; | 363 get_interceptor_->SetResponse(expected_crx_url, test_file); |
| 365 interceptor.SetResponse(expected_crx_url, test_file); | |
| 366 | 364 |
| 367 std::vector<GURL> urls; | 365 std::vector<GURL> urls; |
| 368 urls.push_back(GURL("http://localhost/no/such/file")); | 366 urls.push_back(GURL("http://localhost/no/such/file")); |
| 369 urls.push_back(GURL("http://no.such.host/" | 367 urls.push_back(GURL( |
| 370 "/download/jebgalgnebhfojomionfpkfelancnnkf.crx")); | 368 "http://no.such.host/" |
| 369 "/download/jebgalgnebhfojomionfpkfelancnnkf.crx")); |
| 371 | 370 |
| 372 crx_downloader_->StartDownload(urls, callback_); | 371 crx_downloader_->StartDownload(urls, callback_); |
| 373 RunThreads(); | 372 RunThreads(); |
| 374 | 373 |
| 375 EXPECT_EQ(0, interceptor.GetHitCount()); | 374 EXPECT_EQ(0, get_interceptor_->GetHitCount()); |
| 376 | 375 |
| 377 EXPECT_EQ(1, num_download_complete_calls_); | 376 EXPECT_EQ(1, num_download_complete_calls_); |
| 378 EXPECT_EQ(kExpectedContext, crx_context_); | 377 EXPECT_EQ(kExpectedContext, crx_context_); |
| 379 EXPECT_NE(0, download_complete_result_.error); | 378 EXPECT_NE(0, download_complete_result_.error); |
| 380 EXPECT_TRUE(download_complete_result_.response.empty()); | 379 EXPECT_TRUE(download_complete_result_.response.empty()); |
| 381 } | 380 } |
| 382 | 381 |
| 383 } // namespace component_updater | 382 } // namespace component_updater |
| OLD | NEW |