| 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/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.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/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/threading/sequenced_worker_pool.h" | 12 #include "base/threading/sequenced_worker_pool.h" |
| 13 #include "chrome/browser/component_updater/crx_downloader.h" | 13 #include "chrome/browser/component_updater/crx_downloader.h" |
| 14 #include "chrome/common/chrome_paths.h" | 14 #include "chrome/common/chrome_paths.h" |
| 15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/test/test_browser_thread_bundle.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "content/test/net/url_request_prepackaged_interceptor.h" | 17 #include "content/test/net/url_request_prepackaged_interceptor.h" |
| 18 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 19 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 21 |
| 22 using content::BrowserThread; | 22 using content::BrowserThread; |
| 23 using base::ContentsEqual; |
| 23 | 24 |
| 24 namespace component_updater { | 25 namespace component_updater { |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 28 // Intercepts HTTP GET requests sent to "localhost". | 29 // Intercepts HTTP GET requests sent to "localhost". |
| 29 typedef content::URLLocalHostRequestPrepackagedInterceptor GetInterceptor; | 30 typedef content::URLLocalHostRequestPrepackagedInterceptor GetInterceptor; |
| 30 | 31 |
| 31 base::FilePath test_file(const char* file) { | 32 const char kTestFileName[] = "jebgalgnebhfojomionfpkfelancnnkf.crx"; |
| 33 |
| 34 base::FilePath MakeTestFilePath(const char* file) { |
| 32 base::FilePath path; | 35 base::FilePath path; |
| 33 PathService::Get(chrome::DIR_TEST_DATA, &path); | 36 PathService::Get(chrome::DIR_TEST_DATA, &path); |
| 34 return path.AppendASCII("components").AppendASCII(file); | 37 return path.AppendASCII("components").AppendASCII(file); |
| 35 } | 38 } |
| 36 | 39 |
| 37 } // namespace | 40 } // namespace |
| 38 | 41 |
| 39 class CrxDownloaderTest : public testing::Test { | 42 class CrxDownloaderTest : public testing::Test { |
| 40 public: | 43 public: |
| 41 CrxDownloaderTest(); | 44 CrxDownloaderTest(); |
| 42 virtual ~CrxDownloaderTest(); | 45 virtual ~CrxDownloaderTest(); |
| 43 | 46 |
| 44 // Overrides from testing::Test. | 47 // Overrides from testing::Test. |
| 45 virtual void SetUp() OVERRIDE; | 48 virtual void SetUp() OVERRIDE; |
| 46 virtual void TearDown() OVERRIDE; | 49 virtual void TearDown() OVERRIDE; |
| 47 | 50 |
| 48 void Quit(); | 51 void Quit(); |
| 49 void RunThreads(); | 52 void RunThreads(); |
| 50 void RunThreadsUntilIdle(); | 53 void RunThreadsUntilIdle(); |
| 51 | 54 |
| 52 void DownloadComplete(int crx_context, const CrxDownloader::Result& result); | 55 void DownloadComplete(int crx_context, const CrxDownloader::Result& result); |
| 53 | 56 |
| 54 protected: | 57 protected: |
| 55 scoped_ptr<CrxDownloader> crx_downloader_; | 58 scoped_ptr<CrxDownloader> crx_downloader_; |
| 56 | 59 |
| 57 CrxDownloader::DownloadCallback callback_; | 60 CrxDownloader::DownloadCallback callback_; |
| 58 | 61 |
| 59 int crx_context_; | 62 int crx_context_; |
| 60 int error_; | |
| 61 base::FilePath response_; | |
| 62 | 63 |
| 63 int num_calls_; | 64 int num_download_complete_calls_; |
| 65 CrxDownloader::Result download_complete_result_; |
| 64 | 66 |
| 65 // A magic value for the context to be used in the tests. | 67 // A magic value for the context to be used in the tests. |
| 66 static const int kExpectedContext = 0xaabb; | 68 static const int kExpectedContext = 0xaabb; |
| 67 | 69 |
| 68 private: | 70 private: |
| 69 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; | 71 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
| 70 scoped_refptr<net::TestURLRequestContextGetter> context_; | 72 scoped_refptr<net::TestURLRequestContextGetter> context_; |
| 71 content::TestBrowserThreadBundle thread_bundle_; | 73 content::TestBrowserThreadBundle thread_bundle_; |
| 72 base::Closure quit_closure_; | 74 base::Closure quit_closure_; |
| 73 }; | 75 }; |
| 74 | 76 |
| 75 const int CrxDownloaderTest::kExpectedContext; | 77 const int CrxDownloaderTest::kExpectedContext; |
| 76 | 78 |
| 77 CrxDownloaderTest::CrxDownloaderTest() | 79 CrxDownloaderTest::CrxDownloaderTest() |
| 78 : callback_(base::Bind(&CrxDownloaderTest::DownloadComplete, | 80 : callback_(base::Bind(&CrxDownloaderTest::DownloadComplete, |
| 79 base::Unretained(this), | 81 base::Unretained(this), |
| 80 kExpectedContext)), | 82 kExpectedContext)), |
| 81 crx_context_(0), | 83 crx_context_(0), |
| 82 error_(0), | 84 num_download_complete_calls_(0), |
| 83 num_calls_(0), | |
| 84 blocking_task_runner_(BrowserThread::GetBlockingPool()-> | 85 blocking_task_runner_(BrowserThread::GetBlockingPool()-> |
| 85 GetSequencedTaskRunnerWithShutdownBehavior( | 86 GetSequencedTaskRunnerWithShutdownBehavior( |
| 86 BrowserThread::GetBlockingPool()->GetSequenceToken(), | 87 BrowserThread::GetBlockingPool()->GetSequenceToken(), |
| 87 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), | 88 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), |
| 88 context_(new net::TestURLRequestContextGetter( | 89 context_(new net::TestURLRequestContextGetter( |
| 89 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))), | 90 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))), |
| 90 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | 91 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
| 91 } | 92 } |
| 92 | 93 |
| 93 CrxDownloaderTest::~CrxDownloaderTest() { | 94 CrxDownloaderTest::~CrxDownloaderTest() { |
| 94 context_ = NULL; | 95 context_ = NULL; |
| 95 } | 96 } |
| 96 | 97 |
| 97 void CrxDownloaderTest::SetUp() { | 98 void CrxDownloaderTest::SetUp() { |
| 98 num_calls_ = 0; | 99 num_download_complete_calls_ = 0; |
| 100 download_complete_result_ = CrxDownloader::Result(); |
| 99 crx_downloader_.reset(CrxDownloader::Create( | 101 crx_downloader_.reset(CrxDownloader::Create( |
| 100 false, // Do not use the background downloader in these tests. | 102 false, // Do not use the background downloader in these tests. |
| 101 context_.get(), | 103 context_.get(), |
| 102 blocking_task_runner_)); | 104 blocking_task_runner_)); |
| 103 } | 105 } |
| 104 | 106 |
| 105 void CrxDownloaderTest::TearDown() { | 107 void CrxDownloaderTest::TearDown() { |
| 106 } | 108 } |
| 107 | 109 |
| 108 void CrxDownloaderTest::Quit() { | 110 void CrxDownloaderTest::Quit() { |
| 109 if (!quit_closure_.is_null()) | 111 if (!quit_closure_.is_null()) |
| 110 quit_closure_.Run(); | 112 quit_closure_.Run(); |
| 111 } | 113 } |
| 112 | 114 |
| 113 void CrxDownloaderTest::DownloadComplete( | 115 void CrxDownloaderTest::DownloadComplete(int crx_context, |
| 114 int crx_context, | 116 const CrxDownloader::Result& result) { |
| 115 const CrxDownloader::Result& result) { | 117 ++num_download_complete_calls_; |
| 116 ++num_calls_; | |
| 117 crx_context_ = crx_context; | 118 crx_context_ = crx_context; |
| 118 error_ = result.error; | 119 download_complete_result_ = result; |
| 119 response_ = result.response; | |
| 120 Quit(); | 120 Quit(); |
| 121 } | 121 } |
| 122 | 122 |
| 123 void CrxDownloaderTest::RunThreads() { | 123 void CrxDownloaderTest::RunThreads() { |
| 124 base::RunLoop runloop; | 124 base::RunLoop runloop; |
| 125 quit_closure_ = runloop.QuitClosure(); | 125 quit_closure_ = runloop.QuitClosure(); |
| 126 runloop.Run(); | 126 runloop.Run(); |
| 127 | 127 |
| 128 // Since some tests need to drain currently enqueued tasks such as network | 128 // Since some tests need to drain currently enqueued tasks such as network |
| 129 // intercepts on the IO thread, run the threads until they are | 129 // intercepts on the IO thread, run the threads until they are |
| 130 // idle. The component updater service won't loop again until the loop count | 130 // idle. The component updater service won't loop again until the loop count |
| 131 // is set and the service is started. | 131 // is set and the service is started. |
| 132 RunThreadsUntilIdle(); | 132 RunThreadsUntilIdle(); |
| 133 } | 133 } |
| 134 | 134 |
| 135 void CrxDownloaderTest::RunThreadsUntilIdle() { | 135 void CrxDownloaderTest::RunThreadsUntilIdle() { |
| 136 base::RunLoop().RunUntilIdle(); | 136 base::RunLoop().RunUntilIdle(); |
| 137 } | 137 } |
| 138 | 138 |
| 139 // Tests that starting a download without a url results in an error. | 139 // Tests that starting a download without a url results in an error. |
| 140 TEST_F(CrxDownloaderTest, NoUrl) { | 140 TEST_F(CrxDownloaderTest, NoUrl) { |
| 141 std::vector<GURL> urls; | 141 std::vector<GURL> urls; |
| 142 crx_downloader_->StartDownload(urls, callback_); | 142 crx_downloader_->StartDownload(urls, callback_); |
| 143 RunThreadsUntilIdle(); |
| 143 | 144 |
| 144 RunThreadsUntilIdle(); | 145 EXPECT_EQ(1, num_download_complete_calls_); |
| 145 EXPECT_EQ(-1, error_); | |
| 146 EXPECT_EQ(kExpectedContext, crx_context_); | 146 EXPECT_EQ(kExpectedContext, crx_context_); |
| 147 | 147 EXPECT_EQ(-1, download_complete_result_.error); |
| 148 EXPECT_EQ(1, num_calls_); | 148 EXPECT_TRUE(download_complete_result_.response.empty()); |
| 149 } | 149 } |
| 150 | 150 |
| 151 // Tests that downloading from one url is successful. | 151 // Tests that downloading from one url is successful. |
| 152 TEST_F(CrxDownloaderTest, OneUrl) { | 152 TEST_F(CrxDownloaderTest, OneUrl) { |
| 153 const GURL expected_crx_url = | 153 const GURL expected_crx_url = |
| 154 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | 154 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
| 155 | 155 |
| 156 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
| 156 GetInterceptor interceptor; | 157 GetInterceptor interceptor; |
| 157 interceptor.SetResponse( | 158 interceptor.SetResponse(expected_crx_url, test_file); |
| 158 expected_crx_url, | |
| 159 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); | |
| 160 | 159 |
| 161 crx_downloader_->StartDownloadFromUrl(expected_crx_url, callback_); | 160 crx_downloader_->StartDownloadFromUrl(expected_crx_url, callback_); |
| 161 RunThreads(); |
| 162 | 162 |
| 163 RunThreads(); | |
| 164 EXPECT_EQ(1, interceptor.GetHitCount()); | 163 EXPECT_EQ(1, interceptor.GetHitCount()); |
| 165 EXPECT_EQ(0, error_); | 164 |
| 165 EXPECT_EQ(1, num_download_complete_calls_); |
| 166 EXPECT_EQ(kExpectedContext, crx_context_); | 166 EXPECT_EQ(kExpectedContext, crx_context_); |
| 167 EXPECT_EQ(0, download_complete_result_.error); |
| 168 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); |
| 167 | 169 |
| 168 EXPECT_EQ(1, num_calls_); | 170 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false)); |
| 169 } | 171 } |
| 170 | 172 |
| 171 // Tests that specifying from two urls has no side effects. Expect a successful | 173 // Tests that specifying from two urls has no side effects. Expect a successful |
| 172 // download, and only one download request be made. | 174 // download, and only one download request be made. |
| 173 // This test is flaky on Android. crbug.com/329883 | 175 // This test is flaky on Android. crbug.com/329883 |
| 174 #if defined(OS_ANDROID) | 176 #if defined(OS_ANDROID) |
| 175 #define MAYBE_TwoUrls DISABLED_TwoUrls | 177 #define MAYBE_TwoUrls DISABLED_TwoUrls |
| 176 #else | 178 #else |
| 177 #define MAYBE_TwoUrls TwoUrls | 179 #define MAYBE_TwoUrls TwoUrls |
| 178 #endif | 180 #endif |
| 179 TEST_F(CrxDownloaderTest, MAYBE_TwoUrls) { | 181 TEST_F(CrxDownloaderTest, MAYBE_TwoUrls) { |
| 180 const GURL expected_crx_url = | 182 const GURL expected_crx_url = |
| 181 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | 183 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
| 182 | 184 |
| 185 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
| 183 GetInterceptor interceptor; | 186 GetInterceptor interceptor; |
| 184 interceptor.SetResponse( | 187 interceptor.SetResponse(expected_crx_url, test_file); |
| 185 expected_crx_url, | |
| 186 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); | |
| 187 | 188 |
| 188 std::vector<GURL> urls; | 189 std::vector<GURL> urls; |
| 189 urls.push_back(expected_crx_url); | 190 urls.push_back(expected_crx_url); |
| 190 urls.push_back(expected_crx_url); | 191 urls.push_back(expected_crx_url); |
| 191 | 192 |
| 192 crx_downloader_->StartDownload(urls, callback_); | 193 crx_downloader_->StartDownload(urls, callback_); |
| 194 RunThreads(); |
| 193 | 195 |
| 194 RunThreads(); | |
| 195 EXPECT_EQ(1, interceptor.GetHitCount()); | 196 EXPECT_EQ(1, interceptor.GetHitCount()); |
| 196 EXPECT_EQ(0, error_); | 197 |
| 198 EXPECT_EQ(1, num_download_complete_calls_); |
| 197 EXPECT_EQ(kExpectedContext, crx_context_); | 199 EXPECT_EQ(kExpectedContext, crx_context_); |
| 200 EXPECT_EQ(0, download_complete_result_.error); |
| 201 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); |
| 198 | 202 |
| 199 EXPECT_EQ(1, num_calls_); | 203 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false)); |
| 200 } | 204 } |
| 201 | 205 |
| 202 // Tests that an invalid host results in a download error. | 206 // Tests that an invalid host results in a download error. |
| 203 TEST_F(CrxDownloaderTest, OneUrl_InvalidHost) { | 207 TEST_F(CrxDownloaderTest, OneUrl_InvalidHost) { |
| 204 const GURL expected_crx_url = | 208 const GURL expected_crx_url = |
| 205 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | 209 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
| 206 | 210 |
| 211 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
| 207 GetInterceptor interceptor; | 212 GetInterceptor interceptor; |
| 208 interceptor.SetResponse( | 213 interceptor.SetResponse(expected_crx_url, test_file); |
| 209 expected_crx_url, | |
| 210 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); | |
| 211 | 214 |
| 212 crx_downloader_->StartDownloadFromUrl( | 215 crx_downloader_->StartDownloadFromUrl( |
| 213 GURL("http://no.such.host" | 216 GURL("http://no.such.host" |
| 214 "/download/jebgalgnebhfojomionfpkfelancnnkf.crx"), | 217 "/download/jebgalgnebhfojomionfpkfelancnnkf.crx"), |
| 215 callback_); | 218 callback_); |
| 219 RunThreads(); |
| 216 | 220 |
| 217 RunThreads(); | |
| 218 EXPECT_EQ(0, interceptor.GetHitCount()); | 221 EXPECT_EQ(0, interceptor.GetHitCount()); |
| 219 EXPECT_NE(0, error_); | 222 |
| 223 EXPECT_EQ(1, num_download_complete_calls_); |
| 220 EXPECT_EQ(kExpectedContext, crx_context_); | 224 EXPECT_EQ(kExpectedContext, crx_context_); |
| 221 | 225 EXPECT_NE(0, download_complete_result_.error); |
| 222 EXPECT_EQ(1, num_calls_); | 226 EXPECT_TRUE(download_complete_result_.response.empty()); |
| 223 } | 227 } |
| 224 | 228 |
| 225 // Tests that an invalid path results in a download error. | 229 // Tests that an invalid path results in a download error. |
| 226 TEST_F(CrxDownloaderTest, OneUrl_InvalidPath) { | 230 TEST_F(CrxDownloaderTest, OneUrl_InvalidPath) { |
| 227 const GURL expected_crx_url = | 231 const GURL expected_crx_url = |
| 228 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | 232 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
| 229 | 233 |
| 234 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
| 230 GetInterceptor interceptor; | 235 GetInterceptor interceptor; |
| 231 interceptor.SetResponse( | 236 interceptor.SetResponse(expected_crx_url, test_file); |
| 232 expected_crx_url, | |
| 233 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); | |
| 234 | 237 |
| 235 crx_downloader_->StartDownloadFromUrl( | 238 crx_downloader_->StartDownloadFromUrl( |
| 236 GURL("http://localhost/no/such/file"), | 239 GURL("http://localhost/no/such/file"), |
| 237 callback_); | 240 callback_); |
| 241 RunThreads(); |
| 238 | 242 |
| 239 RunThreads(); | |
| 240 EXPECT_EQ(0, interceptor.GetHitCount()); | 243 EXPECT_EQ(0, interceptor.GetHitCount()); |
| 241 EXPECT_NE(0, error_); | 244 |
| 245 EXPECT_EQ(1, num_download_complete_calls_); |
| 242 EXPECT_EQ(kExpectedContext, crx_context_); | 246 EXPECT_EQ(kExpectedContext, crx_context_); |
| 243 | 247 EXPECT_NE(0, download_complete_result_.error); |
| 244 EXPECT_EQ(1, num_calls_); | 248 EXPECT_TRUE(download_complete_result_.response.empty()); |
| 245 } | 249 } |
| 246 | 250 |
| 247 // Tests that the fallback to a valid url is successful. | 251 // Tests that the fallback to a valid url is successful. |
| 248 // This test is flaky on Android. crbug.com/329883 | 252 // This test is flaky on Android. crbug.com/329883 |
| 249 #if defined(OS_ANDROID) | 253 #if defined(OS_ANDROID) |
| 250 #define MAYBE_TwoUrls_FirstInvalid DISABLED_TwoUrls_FirstInvalid | 254 #define MAYBE_TwoUrls_FirstInvalid DISABLED_TwoUrls_FirstInvalid |
| 251 #else | 255 #else |
| 252 #define MAYBE_TwoUrls_FirstInvalid TwoUrls_FirstInvalid | 256 #define MAYBE_TwoUrls_FirstInvalid TwoUrls_FirstInvalid |
| 253 #endif | 257 #endif |
| 254 TEST_F(CrxDownloaderTest, MAYBE_TwoUrls_FirstInvalid) { | 258 TEST_F(CrxDownloaderTest, MAYBE_TwoUrls_FirstInvalid) { |
| 255 const GURL expected_crx_url = | 259 const GURL expected_crx_url = |
| 256 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | 260 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
| 257 | 261 |
| 262 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
| 258 GetInterceptor interceptor; | 263 GetInterceptor interceptor; |
| 259 interceptor.SetResponse( | 264 interceptor.SetResponse(expected_crx_url, test_file); |
| 260 expected_crx_url, | |
| 261 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); | |
| 262 | 265 |
| 263 std::vector<GURL> urls; | 266 std::vector<GURL> urls; |
| 264 urls.push_back(GURL("http://localhost/no/such/file")); | 267 urls.push_back(GURL("http://localhost/no/such/file")); |
| 265 urls.push_back(expected_crx_url); | 268 urls.push_back(expected_crx_url); |
| 266 | 269 |
| 267 crx_downloader_->StartDownload(urls, callback_); | 270 crx_downloader_->StartDownload(urls, callback_); |
| 271 RunThreads(); |
| 268 | 272 |
| 269 RunThreads(); | |
| 270 EXPECT_EQ(1, interceptor.GetHitCount()); | 273 EXPECT_EQ(1, interceptor.GetHitCount()); |
| 271 EXPECT_EQ(0, error_); | 274 |
| 275 EXPECT_EQ(1, num_download_complete_calls_); |
| 272 EXPECT_EQ(kExpectedContext, crx_context_); | 276 EXPECT_EQ(kExpectedContext, crx_context_); |
| 277 EXPECT_EQ(0, download_complete_result_.error); |
| 278 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); |
| 273 | 279 |
| 274 EXPECT_EQ(1, num_calls_); | 280 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false)); |
| 275 } | 281 } |
| 276 | 282 |
| 277 // Tests that the download succeeds if the first url is correct and the | 283 // Tests that the download succeeds if the first url is correct and the |
| 278 // second bad url does not have a side-effect. | 284 // second bad url does not have a side-effect. |
| 279 TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) { | 285 TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) { |
| 280 const GURL expected_crx_url = | 286 const GURL expected_crx_url = |
| 281 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | 287 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
| 282 | 288 |
| 289 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
| 283 GetInterceptor interceptor; | 290 GetInterceptor interceptor; |
| 284 interceptor.SetResponse( | 291 interceptor.SetResponse(expected_crx_url, test_file); |
| 285 expected_crx_url, | |
| 286 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); | |
| 287 | 292 |
| 288 std::vector<GURL> urls; | 293 std::vector<GURL> urls; |
| 289 urls.push_back(expected_crx_url); | 294 urls.push_back(expected_crx_url); |
| 290 urls.push_back(GURL("http://localhost/no/such/file")); | 295 urls.push_back(GURL("http://localhost/no/such/file")); |
| 291 | 296 |
| 292 crx_downloader_->StartDownload(urls, callback_); | 297 crx_downloader_->StartDownload(urls, callback_); |
| 298 RunThreads(); |
| 293 | 299 |
| 294 RunThreads(); | |
| 295 EXPECT_EQ(1, interceptor.GetHitCount()); | 300 EXPECT_EQ(1, interceptor.GetHitCount()); |
| 296 EXPECT_EQ(0, error_); | 301 |
| 302 EXPECT_EQ(1, num_download_complete_calls_); |
| 297 EXPECT_EQ(kExpectedContext, crx_context_); | 303 EXPECT_EQ(kExpectedContext, crx_context_); |
| 304 EXPECT_EQ(0, download_complete_result_.error); |
| 305 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); |
| 298 | 306 |
| 299 EXPECT_EQ(1, num_calls_); | 307 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false)); |
| 300 } | 308 } |
| 301 | 309 |
| 302 // Tests that the download fails if both urls are bad. | 310 // Tests that the download fails if both urls are bad. |
| 303 TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) { | 311 TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) { |
| 304 const GURL expected_crx_url = | 312 const GURL expected_crx_url = |
| 305 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | 313 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
| 306 | 314 |
| 315 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
| 307 GetInterceptor interceptor; | 316 GetInterceptor interceptor; |
| 308 interceptor.SetResponse( | 317 interceptor.SetResponse(expected_crx_url, test_file); |
| 309 expected_crx_url, | |
| 310 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); | |
| 311 | 318 |
| 312 std::vector<GURL> urls; | 319 std::vector<GURL> urls; |
| 313 urls.push_back(GURL("http://localhost/no/such/file")); | 320 urls.push_back(GURL("http://localhost/no/such/file")); |
| 314 urls.push_back(GURL("http://no.such.host/" | 321 urls.push_back(GURL("http://no.such.host/" |
| 315 "/download/jebgalgnebhfojomionfpkfelancnnkf.crx")); | 322 "/download/jebgalgnebhfojomionfpkfelancnnkf.crx")); |
| 316 | 323 |
| 317 crx_downloader_->StartDownload(urls, callback_); | 324 crx_downloader_->StartDownload(urls, callback_); |
| 325 RunThreads(); |
| 318 | 326 |
| 319 RunThreads(); | |
| 320 EXPECT_EQ(0, interceptor.GetHitCount()); | 327 EXPECT_EQ(0, interceptor.GetHitCount()); |
| 321 EXPECT_NE(0, error_); | 328 |
| 329 EXPECT_EQ(1, num_download_complete_calls_); |
| 322 EXPECT_EQ(kExpectedContext, crx_context_); | 330 EXPECT_EQ(kExpectedContext, crx_context_); |
| 323 | 331 EXPECT_NE(0, download_complete_result_.error); |
| 324 EXPECT_EQ(1, num_calls_); | 332 EXPECT_TRUE(download_complete_result_.response.empty()); |
| 325 } | 333 } |
| 326 | 334 |
| 327 } // namespace component_updater | 335 } // namespace component_updater |
| OLD | NEW |