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

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

Issue 10799005: Replace the DownloadFileManager with direct ownership (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed Windows compile error. Created 8 years, 4 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
65
59 MATCHER(IsNullCallback, "") { return (arg.is_null()); } 66 MATCHER(IsNullCallback, "") { return (arg.is_null()); }
60 67
61 } // namespace 68 } // namespace
62 69
63 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; 70 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain";
64 71
65 class DownloadFileTest : public testing::Test { 72 class DownloadFileTest : public testing::Test {
66 public: 73 public:
67 74
68 static const char* kTestData1; 75 static const char* kTestData1;
69 static const char* kTestData2; 76 static const char* kTestData2;
70 static const char* kTestData3; 77 static const char* kTestData3;
71 static const char* kDataHash; 78 static const char* kDataHash;
72 static const int32 kDummyDownloadId; 79 static const int32 kDummyDownloadId;
73 static const int kDummyChildId; 80 static const int kDummyChildId;
74 static const int kDummyRequestId; 81 static const int kDummyRequestId;
75 82
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() : 83 DownloadFileTest() :
81 update_download_id_(-1), 84 observer_(new StrictMock<MockDownloadDestinationObserver>),
85 observer_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(observer_.get())),
82 bytes_(-1), 86 bytes_(-1),
83 bytes_per_sec_(-1), 87 bytes_per_sec_(-1),
84 hash_state_("xyzzy"), 88 hash_state_("xyzzy"),
85 ui_thread_(BrowserThread::UI, &loop_), 89 ui_thread_(BrowserThread::UI, &loop_),
86 file_thread_(BrowserThread::FILE, &loop_) { 90 file_thread_(BrowserThread::FILE, &loop_) {
87 } 91 }
88 92
89 ~DownloadFileTest() { 93 ~DownloadFileTest() {
90 } 94 }
91 95
92 void SetUpdateDownloadInfo(int32 id, int64 bytes, int64 bytes_per_sec, 96 void SetUpdateDownloadInfo(int64 bytes, int64 bytes_per_sec,
93 const std::string& hash_state) { 97 const std::string& hash_state) {
94 update_download_id_ = id;
95 bytes_ = bytes; 98 bytes_ = bytes;
96 bytes_per_sec_ = bytes_per_sec; 99 bytes_per_sec_ = bytes_per_sec;
97 hash_state_ = hash_state; 100 hash_state_ = hash_state;
98 } 101 }
99 102
100 void ConfirmUpdateDownloadInfo() { 103 void ConfirmUpdateDownloadInfo() {
101 download_manager_->CurrentUpdateStatus( 104 observer_->CurrentUpdateStatus(bytes_, bytes_per_sec_, hash_state_);
102 update_download_id_, bytes_, bytes_per_sec_, hash_state_);
103 } 105 }
104 106
105 virtual void SetUp() { 107 virtual void SetUp() {
106 download_manager_ = new StrictMock<LocalMockDownloadManager>; 108 EXPECT_CALL(*(observer_.get()), DestinationUpdate(_, _, _))
107 EXPECT_CALL(*(download_manager_.get()),
108 UpdateDownload(
109 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(),
110 _, _, _))
111 .Times(AnyNumber()) 109 .Times(AnyNumber())
112 .WillRepeatedly(Invoke(this, &DownloadFileTest::SetUpdateDownloadInfo)); 110 .WillRepeatedly(Invoke(this, &DownloadFileTest::SetUpdateDownloadInfo));
113 } 111 }
114 112
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. 113 // Mock calls to this function are forwarded here.
126 void RegisterCallback(base::Closure sink_callback) { 114 void RegisterCallback(base::Closure sink_callback) {
127 sink_callback_ = sink_callback; 115 sink_callback_ = sink_callback;
128 } 116 }
129 117
118 void SetInterruptReasonCallback(bool* was_called,
119 content::DownloadInterruptReason* reason_p,
120 content::DownloadInterruptReason reason) {
121 *was_called = true;
122 *reason_p = reason;
123 }
124
130 virtual bool CreateDownloadFile(int offset, bool calculate_hash) { 125 virtual bool CreateDownloadFile(int offset, bool calculate_hash) {
131 // There can be only one. 126 // There can be only one.
132 DCHECK(!download_file_.get()); 127 DCHECK(!download_file_.get());
133 128
134 input_stream_ = new StrictMock<MockByteStreamReader>(); 129 input_stream_ = new StrictMock<MockByteStreamReader>();
135 130
136 // TODO: Need to actually create a function that'll set the variables 131 // TODO: Need to actually create a function that'll set the variables
137 // based on the inputs from the callback. 132 // based on the inputs from the callback.
138 EXPECT_CALL(*input_stream_, RegisterCallback(_)) 133 EXPECT_CALL(*input_stream_, RegisterCallback(_))
139 .WillOnce(Invoke(this, &DownloadFileTest::RegisterCallback)) 134 .WillOnce(Invoke(this, &DownloadFileTest::RegisterCallback))
140 .RetiresOnSaturation(); 135 .RetiresOnSaturation();
141 136
142 DownloadCreateInfo info;
143 // info.request_handle default constructed to null.
144 info.download_id = DownloadId(kValidIdDomain, kDummyDownloadId + offset);
145 info.save_info.file_stream = file_stream_;
146 download_file_.reset( 137 download_file_.reset(
147 new DownloadFileImpl( 138 new DownloadFileImpl(
148 &info, 139 content::DownloadSaveInfo(),
149 scoped_ptr<content::ByteStreamReader>(input_stream_).Pass(), 140 GURL(), // Source
150 new DownloadRequestHandle(), 141 GURL(), // Referrer
151 download_manager_, calculate_hash, 142 0, // Received bytes
143 calculate_hash,
144 scoped_ptr<content::ByteStreamReader>(input_stream_),
145 net::BoundNetLog(),
152 scoped_ptr<content::PowerSaveBlocker>(NULL).Pass(), 146 scoped_ptr<content::PowerSaveBlocker>(NULL).Pass(),
153 net::BoundNetLog())); 147 observer_factory_.GetWeakPtr()));
154 148
155 EXPECT_CALL(*input_stream_, Read(_, _)) 149 EXPECT_CALL(*input_stream_, Read(_, _))
156 .WillOnce(Return(content::ByteStreamReader::STREAM_EMPTY)) 150 .WillOnce(Return(content::ByteStreamReader::STREAM_EMPTY))
157 .RetiresOnSaturation(); 151 .RetiresOnSaturation();
158 content::DownloadInterruptReason result = download_file_->Initialize(); 152
153 base::WeakPtrFactory<DownloadFileTest> weak_ptr_factory(this);
154 bool called = false;
155 content::DownloadInterruptReason result;
156 download_file_->Initialize(base::Bind(
157 &DownloadFileTest::SetInterruptReasonCallback,
158 weak_ptr_factory.GetWeakPtr(), &called, &result));
159 loop_.RunAllPending();
160 EXPECT_TRUE(called);
161
159 ::testing::Mock::VerifyAndClearExpectations(input_stream_); 162 ::testing::Mock::VerifyAndClearExpectations(input_stream_);
160 return result == content::DOWNLOAD_INTERRUPT_REASON_NONE; 163 return result == content::DOWNLOAD_INTERRUPT_REASON_NONE;
161 } 164 }
162 165
163 virtual void DestroyDownloadFile(int offset) { 166 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()); 167 EXPECT_FALSE(download_file_->InProgress());
167 EXPECT_EQ(static_cast<int64>(expected_data_.size()), 168 EXPECT_EQ(static_cast<int64>(expected_data_.size()),
168 download_file_->BytesSoFar()); 169 download_file_->BytesSoFar());
169 170
170 // Make sure the data has been properly written to disk. 171 // Make sure the data has been properly written to disk.
171 std::string disk_data; 172 std::string disk_data;
172 EXPECT_TRUE(file_util::ReadFileToString(download_file_->FullPath(), 173 EXPECT_TRUE(file_util::ReadFileToString(download_file_->FullPath(),
173 &disk_data)); 174 &disk_data));
174 EXPECT_EQ(expected_data_, disk_data); 175 EXPECT_EQ(expected_data_, disk_data);
175 176
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 .RetiresOnSaturation(); 226 .RetiresOnSaturation();
226 EXPECT_CALL(*input_stream_, GetStatus()) 227 EXPECT_CALL(*input_stream_, GetStatus())
227 .InSequence(s) 228 .InSequence(s)
228 .WillOnce(Return(interrupt_reason)) 229 .WillOnce(Return(interrupt_reason))
229 .RetiresOnSaturation(); 230 .RetiresOnSaturation();
230 EXPECT_CALL(*input_stream_, RegisterCallback(_)) 231 EXPECT_CALL(*input_stream_, RegisterCallback(_))
231 .RetiresOnSaturation(); 232 .RetiresOnSaturation();
232 } 233 }
233 234
234 void FinishStream(content::DownloadInterruptReason interrupt_reason, 235 void FinishStream(content::DownloadInterruptReason interrupt_reason,
235 bool check_download_manager) { 236 bool check_observer) {
236 ::testing::Sequence s1; 237 ::testing::Sequence s1;
237 SetupFinishStream(interrupt_reason, s1); 238 SetupFinishStream(interrupt_reason, s1);
238 sink_callback_.Run(); 239 sink_callback_.Run();
239 VerifyStreamAndSize(); 240 VerifyStreamAndSize();
240 if (check_download_manager) { 241 if (check_observer) {
241 EXPECT_CALL(*download_manager_, OnResponseCompleted(_, _, _)); 242 EXPECT_CALL(*(observer_.get()), DestinationCompleted(_));
242 loop_.RunAllPending(); 243 loop_.RunAllPending();
243 ::testing::Mock::VerifyAndClearExpectations(download_manager_.get()); 244 ::testing::Mock::VerifyAndClearExpectations(observer_.get());
244 EXPECT_CALL(*(download_manager_.get()), 245 EXPECT_CALL(*(observer_.get()), DestinationUpdate(_, _, _))
245 UpdateDownload(
246 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(),
247 _, _, _))
248 .Times(AnyNumber()) 246 .Times(AnyNumber())
249 .WillRepeatedly(Invoke(this, 247 .WillRepeatedly(Invoke(this,
250 &DownloadFileTest::SetUpdateDownloadInfo)); 248 &DownloadFileTest::SetUpdateDownloadInfo));
251 } 249 }
252 } 250 }
253 251
254 content::DownloadInterruptReason Rename( 252 content::DownloadInterruptReason Rename(
255 const FilePath& full_path, bool overwrite_existing_file, 253 const FilePath& full_path, bool overwrite_existing_file,
256 FilePath* result_path_p) { 254 FilePath* result_path_p) {
257 base::WeakPtrFactory<DownloadFileTest> weak_ptr_factory(this); 255 base::WeakPtrFactory<DownloadFileTest> weak_ptr_factory(this);
258 content::DownloadInterruptReason result_reason( 256 content::DownloadInterruptReason result_reason(
259 content::DOWNLOAD_INTERRUPT_REASON_NONE); 257 content::DOWNLOAD_INTERRUPT_REASON_NONE);
260 bool callback_was_called(false); 258 bool callback_was_called(false);
261 FilePath result_path; 259 FilePath result_path;
262 260
263 download_file_->Rename(full_path, overwrite_existing_file, 261 download_file_->Rename(full_path, overwrite_existing_file,
264 base::Bind(&DownloadFileTest::SetRenameResult, 262 base::Bind(&DownloadFileTest::SetRenameResult,
265 weak_ptr_factory.GetWeakPtr(), 263 weak_ptr_factory.GetWeakPtr(),
266 &callback_was_called, 264 &callback_was_called,
267 &result_reason, result_path_p)); 265 &result_reason, result_path_p));
268 loop_.RunAllPending(); 266 loop_.RunAllPending();
269 267
270 EXPECT_TRUE(callback_was_called); 268 EXPECT_TRUE(callback_was_called);
271 return result_reason; 269 return result_reason;
272 } 270 }
273 271
274 protected: 272 protected:
275 scoped_refptr<StrictMock<LocalMockDownloadManager> > download_manager_; 273 scoped_ptr<StrictMock<MockDownloadDestinationObserver> > observer_;
274 base::WeakPtrFactory<content::DownloadDestinationObserver>
275 observer_factory_;
276 276
277 linked_ptr<net::FileStream> file_stream_; 277 linked_ptr<net::FileStream> file_stream_;
278 278
279 // DownloadFile instance we are testing. 279 // DownloadFile instance we are testing.
280 scoped_ptr<DownloadFile> download_file_; 280 scoped_ptr<DownloadFile> download_file_;
281 281
282 // Stream for sending data into the download file. 282 // Stream for sending data into the download file.
283 // 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_.
284 StrictMock<MockByteStreamReader>* input_stream_; 284 StrictMock<MockByteStreamReader>* input_stream_;
285 285
286 // Sink callback data for stream. 286 // Sink callback data for stream.
287 base::Closure sink_callback_; 287 base::Closure sink_callback_;
288 288
289 // Latest update sent to the download manager. 289 // Latest update sent to the observer.
290 int32 update_download_id_;
291 int64 bytes_; 290 int64 bytes_;
292 int64 bytes_per_sec_; 291 int64 bytes_per_sec_;
293 std::string hash_state_; 292 std::string hash_state_;
294 293
295 MessageLoop loop_; 294 MessageLoop loop_;
296 295
297 private: 296 private:
298 void SetRenameResult(bool* called_p, 297 void SetRenameResult(bool* called_p,
299 content::DownloadInterruptReason* reason_p, 298 content::DownloadInterruptReason* reason_p,
300 FilePath* result_path_p, 299 FilePath* result_path_p,
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
481 480
482 // Various tests of the StreamActive method. 481 // Various tests of the StreamActive method.
483 TEST_F(DownloadFileTest, StreamEmptySuccess) { 482 TEST_F(DownloadFileTest, StreamEmptySuccess) {
484 ASSERT_TRUE(CreateDownloadFile(0, true)); 483 ASSERT_TRUE(CreateDownloadFile(0, true));
485 FilePath initial_path(download_file_->FullPath()); 484 FilePath initial_path(download_file_->FullPath());
486 EXPECT_TRUE(file_util::PathExists(initial_path)); 485 EXPECT_TRUE(file_util::PathExists(initial_path));
487 486
488 // 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
489 // do anything. 488 // do anything.
490 AppendDataToFile(NULL, 0); 489 AppendDataToFile(NULL, 0);
491 ::testing::Mock::VerifyAndClearExpectations(download_manager_.get());
492 EXPECT_CALL(*(download_manager_.get()),
493 UpdateDownload(
494 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(),
495 _, _, _))
496 .Times(AnyNumber())
497 .WillRepeatedly(Invoke(this, &DownloadFileTest::SetUpdateDownloadInfo));
498 490
499 // 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
500 // DownloadManager. 492 // observer.
501 EXPECT_CALL(*(download_manager_.get()), 493 EXPECT_CALL(*(observer_.get()), DestinationCompleted(_));
502 OnResponseCompleted(DownloadId(kValidIdDomain,
503 kDummyDownloadId + 0).local(),
504 0, _));
505 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, false); 494 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, false);
495 loop_.RunAllPending();
506 496
507 DestroyDownloadFile(0); 497 DestroyDownloadFile(0);
508 } 498 }
509 499
510 TEST_F(DownloadFileTest, StreamEmptyError) { 500 TEST_F(DownloadFileTest, StreamEmptyError) {
511 ASSERT_TRUE(CreateDownloadFile(0, true)); 501 ASSERT_TRUE(CreateDownloadFile(0, true));
512 FilePath initial_path(download_file_->FullPath()); 502 FilePath initial_path(download_file_->FullPath());
513 EXPECT_TRUE(file_util::PathExists(initial_path)); 503 EXPECT_TRUE(file_util::PathExists(initial_path));
514 504
515 // 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
516 // DownloadManager. 506 // observer.
517 EXPECT_CALL(*(download_manager_.get()), 507 EXPECT_CALL(*(observer_.get()),
518 OnDownloadInterrupted( 508 DestinationError(
519 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(),
520 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED)) 509 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED))
521 .WillOnce(InvokeWithoutArgs( 510 .WillOnce(InvokeWithoutArgs(
522 this, &DownloadFileTest::ConfirmUpdateDownloadInfo)); 511 this, &DownloadFileTest::ConfirmUpdateDownloadInfo));
523 512
524 // 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.
525 // 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
526 // 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
527 // 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
528 // same time. 517 // same time.
529 EXPECT_CALL(*(download_manager_.get()), 518 EXPECT_CALL(*(observer_.get()), CurrentUpdateStatus(0, _, _));
530 CurrentUpdateStatus(kDummyDownloadId + 0, 0, _, _));
531 519
532 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, false); 520 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, false);
533 521
534 loop_.RunAllPending(); 522 loop_.RunAllPending();
535 523
536 DestroyDownloadFile(0); 524 DestroyDownloadFile(0);
537 } 525 }
538 526
539 TEST_F(DownloadFileTest, StreamNonEmptySuccess) { 527 TEST_F(DownloadFileTest, StreamNonEmptySuccess) {
540 ASSERT_TRUE(CreateDownloadFile(0, true)); 528 ASSERT_TRUE(CreateDownloadFile(0, true));
541 FilePath initial_path(download_file_->FullPath()); 529 FilePath initial_path(download_file_->FullPath());
542 EXPECT_TRUE(file_util::PathExists(initial_path)); 530 EXPECT_TRUE(file_util::PathExists(initial_path));
543 531
544 const char* chunks1[] = { kTestData1, kTestData2 }; 532 const char* chunks1[] = { kTestData1, kTestData2 };
545 ::testing::Sequence s1; 533 ::testing::Sequence s1;
546 SetupDataAppend(chunks1, 2, s1); 534 SetupDataAppend(chunks1, 2, s1);
547 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, s1); 535 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, s1);
548 EXPECT_CALL(*(download_manager_.get()), 536 EXPECT_CALL(*(observer_.get()), DestinationCompleted(_));
549 OnResponseCompleted(DownloadId(kValidIdDomain,
550 kDummyDownloadId + 0).local(),
551 strlen(kTestData1) + strlen(kTestData2),
552 _));
553 sink_callback_.Run(); 537 sink_callback_.Run();
554 VerifyStreamAndSize(); 538 VerifyStreamAndSize();
539 loop_.RunAllPending();
555 DestroyDownloadFile(0); 540 DestroyDownloadFile(0);
556 } 541 }
557 542
558 TEST_F(DownloadFileTest, StreamNonEmptyError) { 543 TEST_F(DownloadFileTest, StreamNonEmptyError) {
559 ASSERT_TRUE(CreateDownloadFile(0, true)); 544 ASSERT_TRUE(CreateDownloadFile(0, true));
560 FilePath initial_path(download_file_->FullPath()); 545 FilePath initial_path(download_file_->FullPath());
561 EXPECT_TRUE(file_util::PathExists(initial_path)); 546 EXPECT_TRUE(file_util::PathExists(initial_path));
562 547
563 const char* chunks1[] = { kTestData1, kTestData2 }; 548 const char* chunks1[] = { kTestData1, kTestData2 };
564 ::testing::Sequence s1; 549 ::testing::Sequence s1;
565 SetupDataAppend(chunks1, 2, s1); 550 SetupDataAppend(chunks1, 2, s1);
566 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, 551 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED,
567 s1); 552 s1);
568 553
569 EXPECT_CALL(*(download_manager_.get()), 554 EXPECT_CALL(*(observer_.get()),
570 OnDownloadInterrupted( 555 DestinationError(
571 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(),
572 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED)) 556 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED))
573 .WillOnce(InvokeWithoutArgs( 557 .WillOnce(InvokeWithoutArgs(
574 this, &DownloadFileTest::ConfirmUpdateDownloadInfo)); 558 this, &DownloadFileTest::ConfirmUpdateDownloadInfo));
575 559
576 // 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.
577 // 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
578 // 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
579 // 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
580 // same time. 564 // same time.
581 EXPECT_CALL(*(download_manager_.get()), 565 EXPECT_CALL(*(observer_.get()),
582 CurrentUpdateStatus(kDummyDownloadId + 0, 566 CurrentUpdateStatus(strlen(kTestData1) + strlen(kTestData2),
583 strlen(kTestData1) + strlen(kTestData2),
584 _, _)); 567 _, _));
585 568
586 sink_callback_.Run(); 569 sink_callback_.Run();
587 loop_.RunAllPending(); 570 loop_.RunAllPending();
588 VerifyStreamAndSize(); 571 VerifyStreamAndSize();
589 DestroyDownloadFile(0); 572 DestroyDownloadFile(0);
590 } 573 }
591 574
592 // 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
593 // confirm the values the DownloadManager received are correct. 576 // confirm the values the observer received are correct.
594 TEST_F(DownloadFileTest, ConfirmUpdate) { 577 TEST_F(DownloadFileTest, ConfirmUpdate) {
595 CreateDownloadFile(0, true); 578 CreateDownloadFile(0, true);
596 579
597 const char* chunks1[] = { kTestData1, kTestData2 }; 580 const char* chunks1[] = { kTestData1, kTestData2 };
598 AppendDataToFile(chunks1, 2); 581 AppendDataToFile(chunks1, 2);
599 582
600 // Run the message loops for 750ms and check for results. 583 // Run the message loops for 750ms and check for results.
601 loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), 584 loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(),
602 base::TimeDelta::FromMilliseconds(750)); 585 base::TimeDelta::FromMilliseconds(750));
603 loop_.Run(); 586 loop_.Run();
604 587
605 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)), 588 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)),
606 bytes_); 589 bytes_);
607 EXPECT_EQ(download_file_->GetHashState(), hash_state_); 590 EXPECT_EQ(download_file_->GetHashState(), hash_state_);
608 591
609 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true); 592 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true);
610 DestroyDownloadFile(0); 593 DestroyDownloadFile(0);
611 } 594 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698