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

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

Issue 10912173: Replace the DownloadFileManager with direct ownership of DownloadFileImpl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync'd to LKGR (r162700) Created 8 years, 2 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 #include "base/file_util.h" 5 #include "base/file_util.h"
6 #include "base/message_loop.h" 6 #include "base/message_loop.h"
7 #include "base/string_number_conversions.h" 7 #include "base/string_number_conversions.h"
8 #include "base/test/test_file_util.h" 8 #include "base/test/test_file_util.h"
9 #include "content/browser/browser_thread_impl.h" 9 #include "content/browser/browser_thread_impl.h"
10 #include "content/browser/download/byte_stream.h" 10 #include "content/browser/download/byte_stream.h"
11 #include "content/browser/download/download_create_info.h" 11 #include "content/browser/download/download_create_info.h"
12 #include "content/browser/download/download_file_impl.h" 12 #include "content/browser/download/download_file_impl.h"
13 #include "content/browser/download/download_request_handle.h" 13 #include "content/browser/download/download_request_handle.h"
14 #include "content/browser/power_save_blocker.h" 14 #include "content/browser/power_save_blocker.h"
15 #include "content/public/browser/download_destination_observer.h"
15 #include "content/public/browser/download_interrupt_reasons.h" 16 #include "content/public/browser/download_interrupt_reasons.h"
16 #include "content/public/browser/download_manager.h" 17 #include "content/public/browser/download_manager.h"
17 #include "content/public/test/mock_download_manager.h" 18 #include "content/public/test/mock_download_manager.h"
18 #include "net/base/file_stream.h" 19 #include "net/base/file_stream.h"
19 #include "net/base/mock_file_stream.h" 20 #include "net/base/mock_file_stream.h"
20 #include "net/base/net_errors.h" 21 #include "net/base/net_errors.h"
21 #include "testing/gmock/include/gmock/gmock.h" 22 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
23 24
24 using content::BrowserThread; 25 using content::BrowserThread;
25 using content::BrowserThreadImpl; 26 using content::BrowserThreadImpl;
26 using content::DownloadFile; 27 using content::DownloadFile;
27 using content::DownloadId; 28 using content::DownloadId;
28 using content::DownloadManager;
29 using ::testing::_; 29 using ::testing::_;
30 using ::testing::AnyNumber; 30 using ::testing::AnyNumber;
31 using ::testing::DoAll; 31 using ::testing::DoAll;
32 using ::testing::InSequence; 32 using ::testing::InSequence;
33 using ::testing::Return; 33 using ::testing::Return;
34 using ::testing::SetArgPointee; 34 using ::testing::SetArgPointee;
35 using ::testing::StrictMock; 35 using ::testing::StrictMock;
36 36
37 namespace { 37 namespace {
38 38
39 class MockByteStreamReader : public content::ByteStreamReader { 39 class MockByteStreamReader : public content::ByteStreamReader {
40 public: 40 public:
41 MockByteStreamReader() {} 41 MockByteStreamReader() {}
42 ~MockByteStreamReader() {} 42 ~MockByteStreamReader() {}
43 43
44 // ByteStream functions 44 // ByteStream functions
45 MOCK_METHOD2(Read, content::ByteStreamReader::StreamState( 45 MOCK_METHOD2(Read, content::ByteStreamReader::StreamState(
46 scoped_refptr<net::IOBuffer>*, size_t*)); 46 scoped_refptr<net::IOBuffer>*, size_t*));
47 MOCK_CONST_METHOD0(GetStatus, content::DownloadInterruptReason()); 47 MOCK_CONST_METHOD0(GetStatus, content::DownloadInterruptReason());
48 MOCK_METHOD1(RegisterCallback, void(const base::Closure&)); 48 MOCK_METHOD1(RegisterCallback, void(const base::Closure&));
49 }; 49 };
50 50
51 class LocalMockDownloadManager : public content::MockDownloadManager { 51 class MockDownloadDestinationObserver
52 : public content::DownloadDestinationObserver {
52 public: 53 public:
53 MOCK_METHOD4(CurrentUpdateStatus, 54 MOCK_METHOD3(DestinationUpdate, void(int64, int64, const std::string&));
54 void(int32, int64, int64, const std::string&)); 55 MOCK_METHOD1(DestinationError, void(content::DownloadInterruptReason));
55 protected: 56 MOCK_METHOD1(DestinationCompleted, void(const std::string&));
56 virtual ~LocalMockDownloadManager() {} 57
58 // Doesn't override any methods in the base class. Used to make sure
59 // that the last DestinationUpdate before a Destination{Completed,Error}
60 // had the right values.
61 MOCK_METHOD3(CurrentUpdateStatus,
62 void(int64, int64, const std::string&));
57 }; 63 };
58 64
59 MATCHER(IsNullCallback, "") { return (arg.is_null()); } 65 MATCHER(IsNullCallback, "") { return (arg.is_null()); }
60 66
61 } // namespace 67 } // namespace
62 68
63 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; 69 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain";
64 70
65 class DownloadFileTest : public testing::Test { 71 class DownloadFileTest : public testing::Test {
66 public: 72 public:
67 73
68 static const char* kTestData1; 74 static const char* kTestData1;
69 static const char* kTestData2; 75 static const char* kTestData2;
70 static const char* kTestData3; 76 static const char* kTestData3;
71 static const char* kDataHash; 77 static const char* kDataHash;
72 static const int32 kDummyDownloadId; 78 static const int32 kDummyDownloadId;
73 static const int kDummyChildId; 79 static const int kDummyChildId;
74 static const int kDummyRequestId; 80 static const int kDummyRequestId;
75 81
76 // We need a UI |BrowserThread| in order to destruct |download_manager_|,
77 // which has trait |BrowserThread::DeleteOnUIThread|. Without this,
78 // calling Release() on |download_manager_| won't ever result in its
79 // destructor being called and we get a leak.
80 DownloadFileTest() : 82 DownloadFileTest() :
81 update_download_id_(-1), 83 observer_(new StrictMock<MockDownloadDestinationObserver>),
84 observer_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(observer_.get())),
82 bytes_(-1), 85 bytes_(-1),
83 bytes_per_sec_(-1), 86 bytes_per_sec_(-1),
84 hash_state_("xyzzy"), 87 hash_state_("xyzzy"),
85 ui_thread_(BrowserThread::UI, &loop_), 88 ui_thread_(BrowserThread::UI, &loop_),
86 file_thread_(BrowserThread::FILE, &loop_) { 89 file_thread_(BrowserThread::FILE, &loop_) {
87 } 90 }
88 91
89 ~DownloadFileTest() { 92 ~DownloadFileTest() {
90 } 93 }
91 94
92 void SetUpdateDownloadInfo(int32 id, int64 bytes, int64 bytes_per_sec, 95 void SetUpdateDownloadInfo(int64 bytes, int64 bytes_per_sec,
93 const std::string& hash_state) { 96 const std::string& hash_state) {
94 update_download_id_ = id;
95 bytes_ = bytes; 97 bytes_ = bytes;
96 bytes_per_sec_ = bytes_per_sec; 98 bytes_per_sec_ = bytes_per_sec;
97 hash_state_ = hash_state; 99 hash_state_ = hash_state;
98 } 100 }
99 101
100 void ConfirmUpdateDownloadInfo() { 102 void ConfirmUpdateDownloadInfo() {
101 download_manager_->CurrentUpdateStatus( 103 observer_->CurrentUpdateStatus(bytes_, bytes_per_sec_, hash_state_);
102 update_download_id_, bytes_, bytes_per_sec_, hash_state_);
103 } 104 }
104 105
105 virtual void SetUp() { 106 virtual void SetUp() {
106 download_manager_ = new StrictMock<LocalMockDownloadManager>; 107 EXPECT_CALL(*(observer_.get()), DestinationUpdate(_, _, _))
107 EXPECT_CALL(*(download_manager_.get()),
108 UpdateDownload(
109 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(),
110 _, _, _))
111 .Times(AnyNumber()) 108 .Times(AnyNumber())
112 .WillRepeatedly(Invoke(this, &DownloadFileTest::SetUpdateDownloadInfo)); 109 .WillRepeatedly(Invoke(this, &DownloadFileTest::SetUpdateDownloadInfo));
113 } 110 }
114 111
115 virtual void TearDown() {
116 // When a DownloadManager's reference count drops to 0, it is not
117 // deleted immediately. Instead, a task is posted to the UI thread's
118 // message loop to delete it.
119 // So, drop the reference count to 0 and run the message loop once
120 // to ensure that all resources are cleaned up before the test exits.
121 download_manager_ = NULL;
122 ui_thread_.message_loop()->RunAllPending();
123 }
124
125 // Mock calls to this function are forwarded here. 112 // Mock calls to this function are forwarded here.
126 void RegisterCallback(base::Closure sink_callback) { 113 void RegisterCallback(base::Closure sink_callback) {
127 sink_callback_ = sink_callback; 114 sink_callback_ = sink_callback;
128 } 115 }
129 116
117 void SetInterruptReasonCallback(bool* was_called,
118 content::DownloadInterruptReason* reason_p,
119 content::DownloadInterruptReason reason) {
120 *was_called = true;
121 *reason_p = reason;
122 }
123
130 virtual bool CreateDownloadFile(int offset, bool calculate_hash) { 124 virtual bool CreateDownloadFile(int offset, bool calculate_hash) {
131 // There can be only one. 125 // There can be only one.
132 DCHECK(!download_file_.get()); 126 DCHECK(!download_file_.get());
133 127
134 input_stream_ = new StrictMock<MockByteStreamReader>(); 128 input_stream_ = new StrictMock<MockByteStreamReader>();
135 129
136 // TODO: Need to actually create a function that'll set the variables 130 // TODO: Need to actually create a function that'll set the variables
137 // based on the inputs from the callback. 131 // based on the inputs from the callback.
138 EXPECT_CALL(*input_stream_, RegisterCallback(_)) 132 EXPECT_CALL(*input_stream_, RegisterCallback(_))
139 .WillOnce(Invoke(this, &DownloadFileTest::RegisterCallback)) 133 .WillOnce(Invoke(this, &DownloadFileTest::RegisterCallback))
140 .RetiresOnSaturation(); 134 .RetiresOnSaturation();
141 135
142 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo()); 136 scoped_ptr<content::DownloadSaveInfo> save_info(
143 // info.request_handle default constructed to null. 137 new content::DownloadSaveInfo());
144 info->download_id = DownloadId(kValidIdDomain, kDummyDownloadId + offset);
145 download_file_.reset( 138 download_file_.reset(
146 new DownloadFileImpl( 139 new DownloadFileImpl(
147 info.Pass(), 140 save_info.Pass(),
148 scoped_ptr<content::ByteStreamReader>(input_stream_).Pass(), 141 FilePath(),
149 scoped_ptr<DownloadRequestHandleInterface>( 142 GURL(), // Source
150 new DownloadRequestHandle()), 143 GURL(), // Referrer
151 download_manager_, calculate_hash, 144 0, // Received bytes
152 scoped_ptr<content::PowerSaveBlocker>(), 145 calculate_hash,
153 net::BoundNetLog())); 146 scoped_ptr<content::ByteStreamReader>(input_stream_),
147 net::BoundNetLog(),
148 scoped_ptr<content::PowerSaveBlocker>(NULL).Pass(),
149 observer_factory_.GetWeakPtr()));
154 150
155 EXPECT_CALL(*input_stream_, Read(_, _)) 151 EXPECT_CALL(*input_stream_, Read(_, _))
156 .WillOnce(Return(content::ByteStreamReader::STREAM_EMPTY)) 152 .WillOnce(Return(content::ByteStreamReader::STREAM_EMPTY))
157 .RetiresOnSaturation(); 153 .RetiresOnSaturation();
158 content::DownloadInterruptReason result = download_file_->Initialize(); 154
155 base::WeakPtrFactory<DownloadFileTest> weak_ptr_factory(this);
156 bool called = false;
157 content::DownloadInterruptReason result;
158 download_file_->Initialize(base::Bind(
159 &DownloadFileTest::SetInterruptReasonCallback,
160 weak_ptr_factory.GetWeakPtr(), &called, &result));
161 loop_.RunAllPending();
162 EXPECT_TRUE(called);
163
159 ::testing::Mock::VerifyAndClearExpectations(input_stream_); 164 ::testing::Mock::VerifyAndClearExpectations(input_stream_);
160 return result == content::DOWNLOAD_INTERRUPT_REASON_NONE; 165 return result == content::DOWNLOAD_INTERRUPT_REASON_NONE;
161 } 166 }
162 167
163 virtual void DestroyDownloadFile(int offset) { 168 virtual void DestroyDownloadFile(int offset) {
164 EXPECT_EQ(kDummyDownloadId + offset, download_file_->Id());
165 EXPECT_EQ(download_manager_, download_file_->GetDownloadManager());
166 EXPECT_FALSE(download_file_->InProgress()); 169 EXPECT_FALSE(download_file_->InProgress());
167 EXPECT_EQ(static_cast<int64>(expected_data_.size()), 170 EXPECT_EQ(static_cast<int64>(expected_data_.size()),
168 download_file_->BytesSoFar()); 171 download_file_->BytesSoFar());
169 172
170 // Make sure the data has been properly written to disk. 173 // Make sure the data has been properly written to disk.
171 std::string disk_data; 174 std::string disk_data;
172 EXPECT_TRUE(file_util::ReadFileToString(download_file_->FullPath(), 175 EXPECT_TRUE(file_util::ReadFileToString(download_file_->FullPath(),
173 &disk_data)); 176 &disk_data));
174 EXPECT_EQ(expected_data_, disk_data); 177 EXPECT_EQ(expected_data_, disk_data);
175 178
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 .RetiresOnSaturation(); 228 .RetiresOnSaturation();
226 EXPECT_CALL(*input_stream_, GetStatus()) 229 EXPECT_CALL(*input_stream_, GetStatus())
227 .InSequence(s) 230 .InSequence(s)
228 .WillOnce(Return(interrupt_reason)) 231 .WillOnce(Return(interrupt_reason))
229 .RetiresOnSaturation(); 232 .RetiresOnSaturation();
230 EXPECT_CALL(*input_stream_, RegisterCallback(_)) 233 EXPECT_CALL(*input_stream_, RegisterCallback(_))
231 .RetiresOnSaturation(); 234 .RetiresOnSaturation();
232 } 235 }
233 236
234 void FinishStream(content::DownloadInterruptReason interrupt_reason, 237 void FinishStream(content::DownloadInterruptReason interrupt_reason,
235 bool check_download_manager) { 238 bool check_observer) {
236 ::testing::Sequence s1; 239 ::testing::Sequence s1;
237 SetupFinishStream(interrupt_reason, s1); 240 SetupFinishStream(interrupt_reason, s1);
238 sink_callback_.Run(); 241 sink_callback_.Run();
239 VerifyStreamAndSize(); 242 VerifyStreamAndSize();
240 if (check_download_manager) { 243 if (check_observer) {
241 EXPECT_CALL(*download_manager_, OnResponseCompleted(_, _, _)); 244 EXPECT_CALL(*(observer_.get()), DestinationCompleted(_));
242 loop_.RunAllPending(); 245 loop_.RunAllPending();
243 ::testing::Mock::VerifyAndClearExpectations(download_manager_.get()); 246 ::testing::Mock::VerifyAndClearExpectations(observer_.get());
244 EXPECT_CALL(*(download_manager_.get()), 247 EXPECT_CALL(*(observer_.get()), DestinationUpdate(_, _, _))
245 UpdateDownload(
246 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(),
247 _, _, _))
248 .Times(AnyNumber()) 248 .Times(AnyNumber())
249 .WillRepeatedly(Invoke(this, 249 .WillRepeatedly(Invoke(this,
250 &DownloadFileTest::SetUpdateDownloadInfo)); 250 &DownloadFileTest::SetUpdateDownloadInfo));
251 } 251 }
252 } 252 }
253 253
254 content::DownloadInterruptReason Rename( 254 content::DownloadInterruptReason Rename(
255 const FilePath& full_path, bool overwrite_existing_file, 255 const FilePath& full_path, bool overwrite_existing_file,
256 FilePath* result_path_p) { 256 FilePath* result_path_p) {
257 base::WeakPtrFactory<DownloadFileTest> weak_ptr_factory(this); 257 base::WeakPtrFactory<DownloadFileTest> weak_ptr_factory(this);
258 content::DownloadInterruptReason result_reason( 258 content::DownloadInterruptReason result_reason(
259 content::DOWNLOAD_INTERRUPT_REASON_NONE); 259 content::DOWNLOAD_INTERRUPT_REASON_NONE);
260 bool callback_was_called(false); 260 bool callback_was_called(false);
261 FilePath result_path; 261 FilePath result_path;
262 262
263 download_file_->Rename(full_path, overwrite_existing_file, 263 download_file_->Rename(full_path, overwrite_existing_file,
264 base::Bind(&DownloadFileTest::SetRenameResult, 264 base::Bind(&DownloadFileTest::SetRenameResult,
265 weak_ptr_factory.GetWeakPtr(), 265 weak_ptr_factory.GetWeakPtr(),
266 &callback_was_called, 266 &callback_was_called,
267 &result_reason, result_path_p)); 267 &result_reason, result_path_p));
268 loop_.RunAllPending(); 268 loop_.RunAllPending();
269 269
270 EXPECT_TRUE(callback_was_called); 270 EXPECT_TRUE(callback_was_called);
271 return result_reason; 271 return result_reason;
272 } 272 }
273 273
274 protected: 274 protected:
275 scoped_refptr<StrictMock<LocalMockDownloadManager> > download_manager_; 275 scoped_ptr<StrictMock<MockDownloadDestinationObserver> > observer_;
276 base::WeakPtrFactory<content::DownloadDestinationObserver>
277 observer_factory_;
276 278
277 // DownloadFile instance we are testing. 279 // DownloadFile instance we are testing.
278 scoped_ptr<DownloadFile> download_file_; 280 scoped_ptr<DownloadFile> download_file_;
279 281
280 // Stream for sending data into the download file. 282 // Stream for sending data into the download file.
281 // Owned by download_file_; will be alive for lifetime of download_file_. 283 // Owned by download_file_; will be alive for lifetime of download_file_.
282 StrictMock<MockByteStreamReader>* input_stream_; 284 StrictMock<MockByteStreamReader>* input_stream_;
283 285
284 // Sink callback data for stream. 286 // Sink callback data for stream.
285 base::Closure sink_callback_; 287 base::Closure sink_callback_;
286 288
287 // Latest update sent to the download manager. 289 // Latest update sent to the observer.
288 int32 update_download_id_;
289 int64 bytes_; 290 int64 bytes_;
290 int64 bytes_per_sec_; 291 int64 bytes_per_sec_;
291 std::string hash_state_; 292 std::string hash_state_;
292 293
293 MessageLoop loop_; 294 MessageLoop loop_;
294 295
295 private: 296 private:
296 void SetRenameResult(bool* called_p, 297 void SetRenameResult(bool* called_p,
297 content::DownloadInterruptReason* reason_p, 298 content::DownloadInterruptReason* reason_p,
298 FilePath* result_path_p, 299 FilePath* result_path_p,
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
479 480
480 // Various tests of the StreamActive method. 481 // Various tests of the StreamActive method.
481 TEST_F(DownloadFileTest, StreamEmptySuccess) { 482 TEST_F(DownloadFileTest, StreamEmptySuccess) {
482 ASSERT_TRUE(CreateDownloadFile(0, true)); 483 ASSERT_TRUE(CreateDownloadFile(0, true));
483 FilePath initial_path(download_file_->FullPath()); 484 FilePath initial_path(download_file_->FullPath());
484 EXPECT_TRUE(file_util::PathExists(initial_path)); 485 EXPECT_TRUE(file_util::PathExists(initial_path));
485 486
486 // Test that calling the sink_callback_ on an empty stream shouldn't 487 // Test that calling the sink_callback_ on an empty stream shouldn't
487 // do anything. 488 // do anything.
488 AppendDataToFile(NULL, 0); 489 AppendDataToFile(NULL, 0);
489 ::testing::Mock::VerifyAndClearExpectations(download_manager_.get());
490 EXPECT_CALL(*(download_manager_.get()),
491 UpdateDownload(
492 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(),
493 _, _, _))
494 .Times(AnyNumber())
495 .WillRepeatedly(Invoke(this, &DownloadFileTest::SetUpdateDownloadInfo));
496 490
497 // Finish the download this way and make sure we see it on the 491 // Finish the download this way and make sure we see it on the
498 // DownloadManager. 492 // observer.
499 EXPECT_CALL(*(download_manager_.get()), 493 EXPECT_CALL(*(observer_.get()), DestinationCompleted(_));
500 OnResponseCompleted(DownloadId(kValidIdDomain,
501 kDummyDownloadId + 0).local(),
502 0, _));
503 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, false); 494 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, false);
495 loop_.RunAllPending();
504 496
505 DestroyDownloadFile(0); 497 DestroyDownloadFile(0);
506 } 498 }
507 499
508 TEST_F(DownloadFileTest, StreamEmptyError) { 500 TEST_F(DownloadFileTest, StreamEmptyError) {
509 ASSERT_TRUE(CreateDownloadFile(0, true)); 501 ASSERT_TRUE(CreateDownloadFile(0, true));
510 FilePath initial_path(download_file_->FullPath()); 502 FilePath initial_path(download_file_->FullPath());
511 EXPECT_TRUE(file_util::PathExists(initial_path)); 503 EXPECT_TRUE(file_util::PathExists(initial_path));
512 504
513 // Finish the download in error and make sure we see it on the 505 // Finish the download in error and make sure we see it on the
514 // DownloadManager. 506 // observer.
515 EXPECT_CALL(*(download_manager_.get()), 507 EXPECT_CALL(*(observer_.get()),
516 OnDownloadInterrupted( 508 DestinationError(
517 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(),
518 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED)) 509 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED))
519 .WillOnce(InvokeWithoutArgs( 510 .WillOnce(InvokeWithoutArgs(
520 this, &DownloadFileTest::ConfirmUpdateDownloadInfo)); 511 this, &DownloadFileTest::ConfirmUpdateDownloadInfo));
521 512
522 // If this next EXPECT_CALL fails flakily, it's probably a real failure. 513 // If this next EXPECT_CALL fails flakily, it's probably a real failure.
523 // We'll be getting a stream of UpdateDownload calls from the timer, and 514 // We'll be getting a stream of UpdateDownload calls from the timer, and
524 // the last one may have the correct information even if the failure 515 // the last one may have the correct information even if the failure
525 // doesn't produce an update, as the timer update may have triggered at the 516 // doesn't produce an update, as the timer update may have triggered at the
526 // same time. 517 // same time.
527 EXPECT_CALL(*(download_manager_.get()), 518 EXPECT_CALL(*(observer_.get()), CurrentUpdateStatus(0, _, _));
528 CurrentUpdateStatus(kDummyDownloadId + 0, 0, _, _));
529 519
530 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, false); 520 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, false);
531 521
532 loop_.RunAllPending(); 522 loop_.RunAllPending();
533 523
534 DestroyDownloadFile(0); 524 DestroyDownloadFile(0);
535 } 525 }
536 526
537 TEST_F(DownloadFileTest, StreamNonEmptySuccess) { 527 TEST_F(DownloadFileTest, StreamNonEmptySuccess) {
538 ASSERT_TRUE(CreateDownloadFile(0, true)); 528 ASSERT_TRUE(CreateDownloadFile(0, true));
539 FilePath initial_path(download_file_->FullPath()); 529 FilePath initial_path(download_file_->FullPath());
540 EXPECT_TRUE(file_util::PathExists(initial_path)); 530 EXPECT_TRUE(file_util::PathExists(initial_path));
541 531
542 const char* chunks1[] = { kTestData1, kTestData2 }; 532 const char* chunks1[] = { kTestData1, kTestData2 };
543 ::testing::Sequence s1; 533 ::testing::Sequence s1;
544 SetupDataAppend(chunks1, 2, s1); 534 SetupDataAppend(chunks1, 2, s1);
545 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, s1); 535 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, s1);
546 EXPECT_CALL(*(download_manager_.get()), 536 EXPECT_CALL(*(observer_.get()), DestinationCompleted(_));
547 OnResponseCompleted(DownloadId(kValidIdDomain,
548 kDummyDownloadId + 0).local(),
549 strlen(kTestData1) + strlen(kTestData2),
550 _));
551 sink_callback_.Run(); 537 sink_callback_.Run();
552 VerifyStreamAndSize(); 538 VerifyStreamAndSize();
539 loop_.RunAllPending();
553 DestroyDownloadFile(0); 540 DestroyDownloadFile(0);
554 } 541 }
555 542
556 TEST_F(DownloadFileTest, StreamNonEmptyError) { 543 TEST_F(DownloadFileTest, StreamNonEmptyError) {
557 ASSERT_TRUE(CreateDownloadFile(0, true)); 544 ASSERT_TRUE(CreateDownloadFile(0, true));
558 FilePath initial_path(download_file_->FullPath()); 545 FilePath initial_path(download_file_->FullPath());
559 EXPECT_TRUE(file_util::PathExists(initial_path)); 546 EXPECT_TRUE(file_util::PathExists(initial_path));
560 547
561 const char* chunks1[] = { kTestData1, kTestData2 }; 548 const char* chunks1[] = { kTestData1, kTestData2 };
562 ::testing::Sequence s1; 549 ::testing::Sequence s1;
563 SetupDataAppend(chunks1, 2, s1); 550 SetupDataAppend(chunks1, 2, s1);
564 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, 551 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED,
565 s1); 552 s1);
566 553
567 EXPECT_CALL(*(download_manager_.get()), 554 EXPECT_CALL(*(observer_.get()),
568 OnDownloadInterrupted( 555 DestinationError(
569 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(),
570 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED)) 556 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED))
571 .WillOnce(InvokeWithoutArgs( 557 .WillOnce(InvokeWithoutArgs(
572 this, &DownloadFileTest::ConfirmUpdateDownloadInfo)); 558 this, &DownloadFileTest::ConfirmUpdateDownloadInfo));
573 559
574 // If this next EXPECT_CALL fails flakily, it's probably a real failure. 560 // If this next EXPECT_CALL fails flakily, it's probably a real failure.
575 // We'll be getting a stream of UpdateDownload calls from the timer, and 561 // We'll be getting a stream of UpdateDownload calls from the timer, and
576 // the last one may have the correct information even if the failure 562 // the last one may have the correct information even if the failure
577 // doesn't produce an update, as the timer update may have triggered at the 563 // doesn't produce an update, as the timer update may have triggered at the
578 // same time. 564 // same time.
579 EXPECT_CALL(*(download_manager_.get()), 565 EXPECT_CALL(*(observer_.get()),
580 CurrentUpdateStatus(kDummyDownloadId + 0, 566 CurrentUpdateStatus(strlen(kTestData1) + strlen(kTestData2),
581 strlen(kTestData1) + strlen(kTestData2),
582 _, _)); 567 _, _));
583 568
584 sink_callback_.Run(); 569 sink_callback_.Run();
585 loop_.RunAllPending(); 570 loop_.RunAllPending();
586 VerifyStreamAndSize(); 571 VerifyStreamAndSize();
587 DestroyDownloadFile(0); 572 DestroyDownloadFile(0);
588 } 573 }
589 574
590 // Send some data, wait 3/4s of a second, run the message loop, and 575 // Send some data, wait 3/4s of a second, run the message loop, and
591 // confirm the values the DownloadManager received are correct. 576 // confirm the values the observer received are correct.
592 TEST_F(DownloadFileTest, ConfirmUpdate) { 577 TEST_F(DownloadFileTest, ConfirmUpdate) {
593 CreateDownloadFile(0, true); 578 CreateDownloadFile(0, true);
594 579
595 const char* chunks1[] = { kTestData1, kTestData2 }; 580 const char* chunks1[] = { kTestData1, kTestData2 };
596 AppendDataToFile(chunks1, 2); 581 AppendDataToFile(chunks1, 2);
597 582
598 // Run the message loops for 750ms and check for results. 583 // Run the message loops for 750ms and check for results.
599 loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), 584 loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(),
600 base::TimeDelta::FromMilliseconds(750)); 585 base::TimeDelta::FromMilliseconds(750));
601 loop_.Run(); 586 loop_.Run();
602 587
603 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)), 588 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)),
604 bytes_); 589 bytes_);
605 EXPECT_EQ(download_file_->GetHashState(), hash_state_); 590 EXPECT_EQ(download_file_->GetHashState(), hash_state_);
606 591
607 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true); 592 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true);
608 DestroyDownloadFile(0); 593 DestroyDownloadFile(0);
609 } 594 }
OLDNEW
« no previous file with comments | « content/browser/download/download_file_manager_unittest.cc ('k') | content/browser/download/download_item_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698