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

Side by Side Diff: content/browser/download/download_browsertest.cc

Issue 148133007: [Downloads] Always call DM::StartDownload() for explicit downloads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // This file contains download browser tests that are known to be runnable 5 // This file contains download browser tests that are known to be runnable
6 // in a pure content context. Over time tests should be migrated here. 6 // in a pure content context. Over time tests should be migrated here.
7 7
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 // We're in a content_browsertest; we know that the DownloadManager 96 // We're in a content_browsertest; we know that the DownloadManager
97 // is a DownloadManagerImpl. 97 // is a DownloadManagerImpl.
98 return static_cast<DownloadManagerImpl*>( 98 return static_cast<DownloadManagerImpl*>(
99 BrowserContext::GetDownloadManager( 99 BrowserContext::GetDownloadManager(
100 shell->web_contents()->GetBrowserContext())); 100 shell->web_contents()->GetBrowserContext()));
101 } 101 }
102 102
103 class DownloadFileWithDelay : public DownloadFileImpl { 103 class DownloadFileWithDelay : public DownloadFileImpl {
104 public: 104 public:
105 DownloadFileWithDelay( 105 DownloadFileWithDelay(
106 scoped_ptr<DownloadSaveInfo> save_info, 106 const DownloadSaveInfo& save_info,
107 const base::FilePath& default_download_directory, 107 const base::FilePath& default_download_directory,
108 const GURL& url, 108 const GURL& url,
109 const GURL& referrer_url, 109 const GURL& referrer_url,
110 bool calculate_hash, 110 bool calculate_hash,
111 scoped_ptr<ByteStreamReader> stream, 111 scoped_ptr<net::FileStream> file_stream,
112 scoped_ptr<ByteStreamReader> byte_stream,
112 const net::BoundNetLog& bound_net_log, 113 const net::BoundNetLog& bound_net_log,
113 scoped_ptr<PowerSaveBlocker> power_save_blocker, 114 scoped_ptr<PowerSaveBlocker> power_save_blocker,
114 base::WeakPtr<DownloadDestinationObserver> observer, 115 base::WeakPtr<DownloadDestinationObserver> observer,
115 base::WeakPtr<DownloadFileWithDelayFactory> owner); 116 base::WeakPtr<DownloadFileWithDelayFactory> owner);
116 117
117 virtual ~DownloadFileWithDelay(); 118 virtual ~DownloadFileWithDelay();
118 119
119 // Wraps DownloadFileImpl::Rename* and intercepts the return callback, 120 // Wraps DownloadFileImpl::Rename* and intercepts the return callback,
120 // storing it in the factory that produced this object for later 121 // storing it in the factory that produced this object for later
121 // retrieval. 122 // retrieval.
(...skipping 22 matching lines...) Expand all
144 }; 145 };
145 146
146 // All routines on this class must be called on the UI thread. 147 // All routines on this class must be called on the UI thread.
147 class DownloadFileWithDelayFactory : public DownloadFileFactory { 148 class DownloadFileWithDelayFactory : public DownloadFileFactory {
148 public: 149 public:
149 DownloadFileWithDelayFactory(); 150 DownloadFileWithDelayFactory();
150 virtual ~DownloadFileWithDelayFactory(); 151 virtual ~DownloadFileWithDelayFactory();
151 152
152 // DownloadFileFactory interface. 153 // DownloadFileFactory interface.
153 virtual DownloadFile* CreateFile( 154 virtual DownloadFile* CreateFile(
154 scoped_ptr<DownloadSaveInfo> save_info, 155 const DownloadSaveInfo& save_info,
155 const base::FilePath& default_download_directory, 156 const base::FilePath& default_download_directory,
156 const GURL& url, 157 const GURL& url,
157 const GURL& referrer_url, 158 const GURL& referrer_url,
158 bool calculate_hash, 159 bool calculate_hash,
159 scoped_ptr<ByteStreamReader> stream, 160 scoped_ptr<net::FileStream> file_stream,
161 scoped_ptr<ByteStreamReader> byte_stream,
160 const net::BoundNetLog& bound_net_log, 162 const net::BoundNetLog& bound_net_log,
161 base::WeakPtr<DownloadDestinationObserver> observer) OVERRIDE; 163 base::WeakPtr<DownloadDestinationObserver> observer) OVERRIDE;
162 164
163 void AddRenameCallback(base::Closure callback); 165 void AddRenameCallback(base::Closure callback);
164 void GetAllRenameCallbacks(std::vector<base::Closure>* results); 166 void GetAllRenameCallbacks(std::vector<base::Closure>* results);
165 167
166 // Do not return until GetAllRenameCallbacks() will return a non-empty list. 168 // Do not return until GetAllRenameCallbacks() will return a non-empty list.
167 void WaitForSomeCallback(); 169 void WaitForSomeCallback();
168 170
169 private: 171 private:
170 base::WeakPtrFactory<DownloadFileWithDelayFactory> weak_ptr_factory_; 172 base::WeakPtrFactory<DownloadFileWithDelayFactory> weak_ptr_factory_;
171 std::vector<base::Closure> rename_callbacks_; 173 std::vector<base::Closure> rename_callbacks_;
172 bool waiting_; 174 bool waiting_;
173 175
174 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelayFactory); 176 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelayFactory);
175 }; 177 };
176 178
177 DownloadFileWithDelay::DownloadFileWithDelay( 179 DownloadFileWithDelay::DownloadFileWithDelay(
178 scoped_ptr<DownloadSaveInfo> save_info, 180 const DownloadSaveInfo& save_info,
179 const base::FilePath& default_download_directory, 181 const base::FilePath& default_download_directory,
180 const GURL& url, 182 const GURL& url,
181 const GURL& referrer_url, 183 const GURL& referrer_url,
182 bool calculate_hash, 184 bool calculate_hash,
183 scoped_ptr<ByteStreamReader> stream, 185 scoped_ptr<net::FileStream> file_stream,
186 scoped_ptr<ByteStreamReader> byte_stream,
184 const net::BoundNetLog& bound_net_log, 187 const net::BoundNetLog& bound_net_log,
185 scoped_ptr<PowerSaveBlocker> power_save_blocker, 188 scoped_ptr<PowerSaveBlocker> power_save_blocker,
186 base::WeakPtr<DownloadDestinationObserver> observer, 189 base::WeakPtr<DownloadDestinationObserver> observer,
187 base::WeakPtr<DownloadFileWithDelayFactory> owner) 190 base::WeakPtr<DownloadFileWithDelayFactory> owner)
188 : DownloadFileImpl( 191 : DownloadFileImpl(
189 save_info.Pass(), default_download_directory, url, referrer_url, 192 save_info, default_download_directory, url, referrer_url,
190 calculate_hash, stream.Pass(), bound_net_log, 193 calculate_hash, file_stream.Pass(), byte_stream.Pass(), bound_net_log,
191 power_save_blocker.Pass(), observer), 194 power_save_blocker.Pass(), observer),
192 owner_(owner) {} 195 owner_(owner) {}
193 196
194 DownloadFileWithDelay::~DownloadFileWithDelay() {} 197 DownloadFileWithDelay::~DownloadFileWithDelay() {}
195 198
196 void DownloadFileWithDelay::RenameAndUniquify( 199 void DownloadFileWithDelay::RenameAndUniquify(
197 const base::FilePath& full_path, 200 const base::FilePath& full_path,
198 const RenameCompletionCallback& callback) { 201 const RenameCompletionCallback& callback) {
199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
200 DownloadFileImpl::RenameAndUniquify( 203 DownloadFileImpl::RenameAndUniquify(
(...skipping 20 matching lines...) Expand all
221 return; 224 return;
222 factory->AddRenameCallback(base::Bind(original_callback, reason, path)); 225 factory->AddRenameCallback(base::Bind(original_callback, reason, path));
223 } 226 }
224 227
225 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory() 228 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory()
226 : weak_ptr_factory_(this), 229 : weak_ptr_factory_(this),
227 waiting_(false) {} 230 waiting_(false) {}
228 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {} 231 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {}
229 232
230 DownloadFile* DownloadFileWithDelayFactory::CreateFile( 233 DownloadFile* DownloadFileWithDelayFactory::CreateFile(
231 scoped_ptr<DownloadSaveInfo> save_info, 234 const DownloadSaveInfo& save_info,
232 const base::FilePath& default_download_directory, 235 const base::FilePath& default_download_directory,
233 const GURL& url, 236 const GURL& url,
234 const GURL& referrer_url, 237 const GURL& referrer_url,
235 bool calculate_hash, 238 bool calculate_hash,
236 scoped_ptr<ByteStreamReader> stream, 239 scoped_ptr<net::FileStream> file_stream,
240 scoped_ptr<ByteStreamReader> byte_stream,
237 const net::BoundNetLog& bound_net_log, 241 const net::BoundNetLog& bound_net_log,
238 base::WeakPtr<DownloadDestinationObserver> observer) { 242 base::WeakPtr<DownloadDestinationObserver> observer) {
239 scoped_ptr<PowerSaveBlocker> psb( 243 scoped_ptr<PowerSaveBlocker> psb(
240 PowerSaveBlocker::Create( 244 PowerSaveBlocker::Create(
241 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, 245 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension,
242 "Download in progress")); 246 "Download in progress"));
243 return new DownloadFileWithDelay( 247 return new DownloadFileWithDelay(
244 save_info.Pass(), default_download_directory, url, referrer_url, 248 save_info, default_download_directory, url, referrer_url,
245 calculate_hash, stream.Pass(), bound_net_log, 249 calculate_hash, file_stream.Pass(), byte_stream.Pass(), bound_net_log,
246 psb.Pass(), observer, weak_ptr_factory_.GetWeakPtr()); 250 psb.Pass(), observer, weak_ptr_factory_.GetWeakPtr());
247 } 251 }
248 252
249 void DownloadFileWithDelayFactory::AddRenameCallback(base::Closure callback) { 253 void DownloadFileWithDelayFactory::AddRenameCallback(base::Closure callback) {
250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
251 rename_callbacks_.push_back(callback); 255 rename_callbacks_.push_back(callback);
252 if (waiting_) 256 if (waiting_)
253 base::MessageLoopForUI::current()->Quit(); 257 base::MessageLoopForUI::current()->Quit();
254 } 258 }
255 259
256 void DownloadFileWithDelayFactory::GetAllRenameCallbacks( 260 void DownloadFileWithDelayFactory::GetAllRenameCallbacks(
257 std::vector<base::Closure>* results) { 261 std::vector<base::Closure>* results) {
258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 262 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
259 results->swap(rename_callbacks_); 263 results->swap(rename_callbacks_);
260 } 264 }
261 265
262 void DownloadFileWithDelayFactory::WaitForSomeCallback() { 266 void DownloadFileWithDelayFactory::WaitForSomeCallback() {
263 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 267 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
264 268
265 if (rename_callbacks_.empty()) { 269 if (rename_callbacks_.empty()) {
266 waiting_ = true; 270 waiting_ = true;
267 RunMessageLoop(); 271 RunMessageLoop();
268 waiting_ = false; 272 waiting_ = false;
269 } 273 }
270 } 274 }
271 275
272 class CountingDownloadFile : public DownloadFileImpl { 276 class CountingDownloadFile : public DownloadFileImpl {
273 public: 277 public:
274 CountingDownloadFile( 278 CountingDownloadFile(
275 scoped_ptr<DownloadSaveInfo> save_info, 279 const DownloadSaveInfo& save_info,
276 const base::FilePath& default_downloads_directory, 280 const base::FilePath& default_downloads_directory,
277 const GURL& url, 281 const GURL& url,
278 const GURL& referrer_url, 282 const GURL& referrer_url,
279 bool calculate_hash, 283 bool calculate_hash,
280 scoped_ptr<ByteStreamReader> stream, 284 scoped_ptr<net::FileStream> file_stream,
285 scoped_ptr<ByteStreamReader> byte_stream,
281 const net::BoundNetLog& bound_net_log, 286 const net::BoundNetLog& bound_net_log,
282 scoped_ptr<PowerSaveBlocker> power_save_blocker, 287 scoped_ptr<PowerSaveBlocker> power_save_blocker,
283 base::WeakPtr<DownloadDestinationObserver> observer) 288 base::WeakPtr<DownloadDestinationObserver> observer)
284 : DownloadFileImpl(save_info.Pass(), default_downloads_directory, 289 : DownloadFileImpl(save_info, default_downloads_directory,
285 url, referrer_url, calculate_hash, 290 url, referrer_url, calculate_hash,
286 stream.Pass(), bound_net_log, 291 file_stream.Pass(), byte_stream.Pass(), bound_net_log,
287 power_save_blocker.Pass(), observer) {} 292 power_save_blocker.Pass(), observer) {}
288 293
289 virtual ~CountingDownloadFile() { 294 virtual ~CountingDownloadFile() {
290 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 295 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
291 active_files_--; 296 active_files_--;
292 } 297 }
293 298
294 virtual void Initialize(const InitializeCallback& callback) OVERRIDE { 299 virtual void Initialize(const InitializeCallback& callback) OVERRIDE {
295 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 300 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
296 active_files_++; 301 active_files_++;
(...skipping 25 matching lines...) Expand all
322 327
323 int CountingDownloadFile::active_files_ = 0; 328 int CountingDownloadFile::active_files_ = 0;
324 329
325 class CountingDownloadFileFactory : public DownloadFileFactory { 330 class CountingDownloadFileFactory : public DownloadFileFactory {
326 public: 331 public:
327 CountingDownloadFileFactory() {} 332 CountingDownloadFileFactory() {}
328 virtual ~CountingDownloadFileFactory() {} 333 virtual ~CountingDownloadFileFactory() {}
329 334
330 // DownloadFileFactory interface. 335 // DownloadFileFactory interface.
331 virtual DownloadFile* CreateFile( 336 virtual DownloadFile* CreateFile(
332 scoped_ptr<DownloadSaveInfo> save_info, 337 const DownloadSaveInfo& save_info,
333 const base::FilePath& default_downloads_directory, 338 const base::FilePath& default_downloads_directory,
334 const GURL& url, 339 const GURL& url,
335 const GURL& referrer_url, 340 const GURL& referrer_url,
336 bool calculate_hash, 341 bool calculate_hash,
337 scoped_ptr<ByteStreamReader> stream, 342 scoped_ptr<net::FileStream> file_stream,
338 const net::BoundNetLog& bound_net_log, 343 scoped_ptr<ByteStreamReader> byte_stream,
339 base::WeakPtr<DownloadDestinationObserver> observer) OVERRIDE { 344 const net::BoundNetLog& bound_net_log,
345 base::WeakPtr<DownloadDestinationObserver> observer) OVERRIDE {
340 scoped_ptr<PowerSaveBlocker> psb( 346 scoped_ptr<PowerSaveBlocker> psb(
341 PowerSaveBlocker::Create( 347 PowerSaveBlocker::Create(
342 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, 348 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension,
343 "Download in progress")); 349 "Download in progress"));
344 return new CountingDownloadFile( 350 return new CountingDownloadFile(
345 save_info.Pass(), default_downloads_directory, url, referrer_url, 351 save_info, default_downloads_directory, url, referrer_url,
346 calculate_hash, stream.Pass(), bound_net_log, 352 calculate_hash, file_stream.Pass(), byte_stream.Pass(), bound_net_log,
347 psb.Pass(), observer); 353 psb.Pass(), observer);
348 } 354 }
349 }; 355 };
350 356
351 class TestShellDownloadManagerDelegate : public ShellDownloadManagerDelegate { 357 class TestShellDownloadManagerDelegate : public ShellDownloadManagerDelegate {
352 public: 358 public:
353 TestShellDownloadManagerDelegate() 359 TestShellDownloadManagerDelegate()
354 : delay_download_open_(false) {} 360 : delay_download_open_(false) {}
355 virtual ~TestShellDownloadManagerDelegate() {} 361 virtual ~TestShellDownloadManagerDelegate() {}
356 362
(...skipping 806 matching lines...) Expand 10 before | Expand all | Expand 10 after
1163 completion_observer.WaitForEvent(); 1169 completion_observer.WaitForEvent();
1164 1170
1165 ConfirmFileStatusForResume( 1171 ConfirmFileStatusForResume(
1166 download, true, GetSafeBufferChunk() * 3, GetSafeBufferChunk() * 3, 1172 download, true, GetSafeBufferChunk() * 3, GetSafeBufferChunk() * 3,
1167 base::FilePath(FILE_PATH_LITERAL("rangereset"))); 1173 base::FilePath(FILE_PATH_LITERAL("rangereset")));
1168 EXPECT_EQ("BadPrecondition0", download->GetETag()); 1174 EXPECT_EQ("BadPrecondition0", download->GetETag());
1169 1175
1170 static const RecordingDownloadObserver::RecordStruct expected_record[] = { 1176 static const RecordingDownloadObserver::RecordStruct expected_record[] = {
1171 // Result of RST 1177 // Result of RST
1172 {DownloadItem::INTERRUPTED, GetSafeBufferChunk()}, 1178 {DownloadItem::INTERRUPTED, GetSafeBufferChunk()},
1173 // Starting continuation
1174 {DownloadItem::IN_PROGRESS, GetSafeBufferChunk()},
1175 // Server precondition fail. 1179 // Server precondition fail.
1176 {DownloadItem::INTERRUPTED, 0}, 1180 {DownloadItem::INTERRUPTED, 0},
1177 // Notification of successful restart. 1181 // Notification of successful restart.
1178 {DownloadItem::IN_PROGRESS, 0}, 1182 {DownloadItem::IN_PROGRESS, 0},
1179 // Completion. 1183 // Completion.
1180 {DownloadItem::COMPLETE, GetSafeBufferChunk() * 3}, 1184 {DownloadItem::COMPLETE, GetSafeBufferChunk() * 3},
1181 }; 1185 };
1182 1186
1183 recorder.CompareToExpectedRecord(expected_record, arraysize(expected_record)); 1187 recorder.CompareToExpectedRecord(expected_record, arraysize(expected_record));
1184 } 1188 }
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
1302 // Start and watch for interrupt. 1306 // Start and watch for interrupt.
1303 scoped_ptr<DownloadTestObserver> int_observer( 1307 scoped_ptr<DownloadTestObserver> int_observer(
1304 CreateInterruptedWaiter(shell(), 1)); 1308 CreateInterruptedWaiter(shell(), 1));
1305 DownloadItem* download(StartDownloadAndReturnItem(url)); 1309 DownloadItem* download(StartDownloadAndReturnItem(url));
1306 int_observer->WaitForFinished(); 1310 int_observer->WaitForFinished();
1307 ASSERT_EQ(DownloadItem::INTERRUPTED, download->GetState()); 1311 ASSERT_EQ(DownloadItem::INTERRUPTED, download->GetState());
1308 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE, 1312 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE,
1309 download->GetLastReason()); 1313 download->GetLastReason());
1310 EXPECT_EQ(0, download->GetReceivedBytes()); 1314 EXPECT_EQ(0, download->GetReceivedBytes());
1311 EXPECT_TRUE(download->GetFullPath().empty()); 1315 EXPECT_TRUE(download->GetFullPath().empty());
1312 EXPECT_TRUE(download->GetTargetFilePath().empty()); 1316 EXPECT_FALSE(download->GetTargetFilePath().empty());
1313 1317
1314 // We need to make sure that any cross-thread downloads communication has 1318 // We need to make sure that any cross-thread downloads communication has
1315 // quiesced before clearing and injecting the new errors, as the 1319 // quiesced before clearing and injecting the new errors, as the
1316 // InjectErrors() routine alters the currently in use download file 1320 // InjectErrors() routine alters the currently in use download file
1317 // factory, which is a file thread object. 1321 // factory, which is a file thread object.
1318 RunAllPendingInMessageLoop(BrowserThread::FILE); 1322 RunAllPendingInMessageLoop(BrowserThread::FILE);
1319 RunAllPendingInMessageLoop(); 1323 RunAllPendingInMessageLoop();
1320 1324
1321 // Clear the old errors list. 1325 // Clear the old errors list.
1322 injector->ClearErrors(); 1326 injector->ClearErrors();
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after
1647 ASSERT_EQ(1u, downloads.size()); 1651 ASSERT_EQ(1u, downloads.size());
1648 ASSERT_EQ(DownloadItem::COMPLETE, downloads[0]->GetState()); 1652 ASSERT_EQ(DownloadItem::COMPLETE, downloads[0]->GetState());
1649 1653
1650 // Check that the cookies were correctly set. 1654 // Check that the cookies were correctly set.
1651 EXPECT_EQ("A=B", 1655 EXPECT_EQ("A=B",
1652 content::GetCookies(shell()->web_contents()->GetBrowserContext(), 1656 content::GetCookies(shell()->web_contents()->GetBrowserContext(),
1653 GURL(download))); 1657 GURL(download)));
1654 } 1658 }
1655 1659
1656 } // namespace content 1660 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698