OLD | NEW |
---|---|
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_controller.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 MockDownloadDestinationController |
52 : public content::DownloadDestinationController { | |
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 // Specific to this class. | |
benjhayden
2012/07/25 15:19:16
Specific to MockDDC or c::DDC? As opposed to gener
Randy Smith (Not in Mondays)
2012/07/30 01:07:23
Comment rewritten in Egyptian; let me know if it's
| |
59 MOCK_METHOD3(CurrentUpdateStatus, | |
60 void(int64, int64, const std::string&)); | |
57 }; | 61 }; |
58 | 62 |
63 | |
59 MATCHER(IsNullCallback, "") { return (arg.is_null()); } | 64 MATCHER(IsNullCallback, "") { return (arg.is_null()); } |
60 | 65 |
61 } // namespace | 66 } // namespace |
62 | 67 |
63 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; | 68 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; |
64 | 69 |
65 class DownloadFileTest : public testing::Test { | 70 class DownloadFileTest : public testing::Test { |
66 public: | 71 public: |
67 | 72 |
68 static const char* kTestData1; | 73 static const char* kTestData1; |
69 static const char* kTestData2; | 74 static const char* kTestData2; |
70 static const char* kTestData3; | 75 static const char* kTestData3; |
71 static const char* kDataHash; | 76 static const char* kDataHash; |
72 static const int32 kDummyDownloadId; | 77 static const int32 kDummyDownloadId; |
73 static const int kDummyChildId; | 78 static const int kDummyChildId; |
74 static const int kDummyRequestId; | 79 static const int kDummyRequestId; |
75 | 80 |
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() : | 81 DownloadFileTest() : |
81 update_download_id_(-1), | 82 controller_(new StrictMock<MockDownloadDestinationController>), |
83 controller_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(controller_.get())), | |
82 bytes_(-1), | 84 bytes_(-1), |
83 bytes_per_sec_(-1), | 85 bytes_per_sec_(-1), |
84 hash_state_("xyzzy"), | 86 hash_state_("xyzzy"), |
85 ui_thread_(BrowserThread::UI, &loop_), | 87 ui_thread_(BrowserThread::UI, &loop_), |
86 file_thread_(BrowserThread::FILE, &loop_) { | 88 file_thread_(BrowserThread::FILE, &loop_) { |
87 } | 89 } |
88 | 90 |
89 ~DownloadFileTest() { | 91 ~DownloadFileTest() { |
90 } | 92 } |
91 | 93 |
92 void SetUpdateDownloadInfo(int32 id, int64 bytes, int64 bytes_per_sec, | 94 void SetUpdateDownloadInfo(int64 bytes, int64 bytes_per_sec, |
93 const std::string& hash_state) { | 95 const std::string& hash_state) { |
94 update_download_id_ = id; | |
95 bytes_ = bytes; | 96 bytes_ = bytes; |
96 bytes_per_sec_ = bytes_per_sec; | 97 bytes_per_sec_ = bytes_per_sec; |
97 hash_state_ = hash_state; | 98 hash_state_ = hash_state; |
98 } | 99 } |
99 | 100 |
100 void ConfirmUpdateDownloadInfo() { | 101 void ConfirmUpdateDownloadInfo() { |
101 download_manager_->CurrentUpdateStatus( | 102 controller_->CurrentUpdateStatus(bytes_, bytes_per_sec_, hash_state_); |
102 update_download_id_, bytes_, bytes_per_sec_, hash_state_); | |
103 } | 103 } |
104 | 104 |
105 virtual void SetUp() { | 105 virtual void SetUp() { |
106 download_manager_ = new StrictMock<LocalMockDownloadManager>; | 106 EXPECT_CALL(*(controller_.get()), DestinationUpdate(_, _, _)) |
107 EXPECT_CALL(*(download_manager_.get()), | |
108 UpdateDownload( | |
109 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), | |
110 _, _, _)) | |
111 .Times(AnyNumber()) | 107 .Times(AnyNumber()) |
112 .WillRepeatedly(Invoke(this, &DownloadFileTest::SetUpdateDownloadInfo)); | 108 .WillRepeatedly(Invoke(this, &DownloadFileTest::SetUpdateDownloadInfo)); |
113 } | 109 } |
114 | 110 |
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. | 111 // Mock calls to this function are forwarded here. |
126 void RegisterCallback(base::Closure sink_callback) { | 112 void RegisterCallback(base::Closure sink_callback) { |
127 sink_callback_ = sink_callback; | 113 sink_callback_ = sink_callback; |
128 } | 114 } |
129 | 115 |
116 void SetInterruptReasonCallback(bool* was_called, | |
117 content::DownloadInterruptReason* reason_p, | |
118 content::DownloadInterruptReason reason) | |
119 { | |
benjhayden
2012/07/25 15:19:16
brace on previous line, please.
Randy Smith (Not in Mondays)
2012/07/30 01:07:23
Whoops; done
| |
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 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( | 136 download_file_.reset( |
147 new DownloadFileImpl( | 137 new DownloadFileImpl( |
148 &info, | 138 content::DownloadSaveInfo(), GURL(), GURL(), 0, calculate_hash, |
benjhayden
2012/07/25 15:19:16
Can you please name all these fiddly little things
Randy Smith (Not in Mondays)
2012/07/30 01:07:23
I've commented those arguments that don't seem obv
| |
149 scoped_ptr<content::ByteStreamReader>(input_stream_).Pass(), | 139 scoped_ptr<content::ByteStreamReader>(input_stream_), |
150 new DownloadRequestHandle(), | 140 net::BoundNetLog(), |
151 download_manager_, calculate_hash, | |
152 scoped_ptr<content::PowerSaveBlocker>(NULL).Pass(), | 141 scoped_ptr<content::PowerSaveBlocker>(NULL).Pass(), |
153 net::BoundNetLog())); | 142 controller_factory_.GetWeakPtr())); |
154 | 143 |
155 EXPECT_CALL(*input_stream_, Read(_, _)) | 144 EXPECT_CALL(*input_stream_, Read(_, _)) |
156 .WillOnce(Return(content::ByteStreamReader::STREAM_EMPTY)) | 145 .WillOnce(Return(content::ByteStreamReader::STREAM_EMPTY)) |
157 .RetiresOnSaturation(); | 146 .RetiresOnSaturation(); |
158 content::DownloadInterruptReason result = download_file_->Initialize(); | 147 |
148 base::WeakPtrFactory<DownloadFileTest> weak_ptr_factory(this); | |
149 bool called = false; | |
150 content::DownloadInterruptReason result; | |
151 download_file_->Initialize(base::Bind( | |
152 &DownloadFileTest::SetInterruptReasonCallback, | |
153 weak_ptr_factory.GetWeakPtr(), &called, &result)); | |
154 loop_.RunAllPending(); | |
155 EXPECT_TRUE(called); | |
156 | |
159 ::testing::Mock::VerifyAndClearExpectations(input_stream_); | 157 ::testing::Mock::VerifyAndClearExpectations(input_stream_); |
160 return result == content::DOWNLOAD_INTERRUPT_REASON_NONE; | 158 return result == content::DOWNLOAD_INTERRUPT_REASON_NONE; |
161 } | 159 } |
162 | 160 |
163 virtual void DestroyDownloadFile(int offset) { | 161 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()); | 162 EXPECT_FALSE(download_file_->InProgress()); |
167 EXPECT_EQ(static_cast<int64>(expected_data_.size()), | 163 EXPECT_EQ(static_cast<int64>(expected_data_.size()), |
168 download_file_->BytesSoFar()); | 164 download_file_->BytesSoFar()); |
169 | 165 |
170 // Make sure the data has been properly written to disk. | 166 // Make sure the data has been properly written to disk. |
171 std::string disk_data; | 167 std::string disk_data; |
172 EXPECT_TRUE(file_util::ReadFileToString(download_file_->FullPath(), | 168 EXPECT_TRUE(file_util::ReadFileToString(download_file_->FullPath(), |
173 &disk_data)); | 169 &disk_data)); |
174 EXPECT_EQ(expected_data_, disk_data); | 170 EXPECT_EQ(expected_data_, disk_data); |
175 | 171 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
225 .RetiresOnSaturation(); | 221 .RetiresOnSaturation(); |
226 EXPECT_CALL(*input_stream_, GetStatus()) | 222 EXPECT_CALL(*input_stream_, GetStatus()) |
227 .InSequence(s) | 223 .InSequence(s) |
228 .WillOnce(Return(interrupt_reason)) | 224 .WillOnce(Return(interrupt_reason)) |
229 .RetiresOnSaturation(); | 225 .RetiresOnSaturation(); |
230 EXPECT_CALL(*input_stream_, RegisterCallback(_)) | 226 EXPECT_CALL(*input_stream_, RegisterCallback(_)) |
231 .RetiresOnSaturation(); | 227 .RetiresOnSaturation(); |
232 } | 228 } |
233 | 229 |
234 void FinishStream(content::DownloadInterruptReason interrupt_reason, | 230 void FinishStream(content::DownloadInterruptReason interrupt_reason, |
235 bool check_download_manager) { | 231 bool check_controller) { |
236 ::testing::Sequence s1; | 232 ::testing::Sequence s1; |
237 SetupFinishStream(interrupt_reason, s1); | 233 SetupFinishStream(interrupt_reason, s1); |
238 sink_callback_.Run(); | 234 sink_callback_.Run(); |
239 VerifyStreamAndSize(); | 235 VerifyStreamAndSize(); |
240 if (check_download_manager) { | 236 if (check_controller) { |
241 EXPECT_CALL(*download_manager_, OnResponseCompleted(_, _, _)); | 237 EXPECT_CALL(*(controller_.get()), DestinationCompleted(_)); |
242 loop_.RunAllPending(); | 238 loop_.RunAllPending(); |
243 ::testing::Mock::VerifyAndClearExpectations(download_manager_.get()); | 239 ::testing::Mock::VerifyAndClearExpectations(controller_.get()); |
244 EXPECT_CALL(*(download_manager_.get()), | 240 EXPECT_CALL(*(controller_.get()), DestinationUpdate(_, _, _)) |
245 UpdateDownload( | |
246 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), | |
247 _, _, _)) | |
248 .Times(AnyNumber()) | 241 .Times(AnyNumber()) |
249 .WillRepeatedly(Invoke(this, | 242 .WillRepeatedly(Invoke(this, |
250 &DownloadFileTest::SetUpdateDownloadInfo)); | 243 &DownloadFileTest::SetUpdateDownloadInfo)); |
251 } | 244 } |
252 } | 245 } |
253 | 246 |
254 content::DownloadInterruptReason Rename( | 247 content::DownloadInterruptReason Rename( |
255 const FilePath& full_path, bool overwrite_existing_file, | 248 const FilePath& full_path, bool overwrite_existing_file, |
256 FilePath* result_path_p) { | 249 FilePath* result_path_p) { |
257 base::WeakPtrFactory<DownloadFileTest> weak_ptr_factory(this); | 250 base::WeakPtrFactory<DownloadFileTest> weak_ptr_factory(this); |
258 content::DownloadInterruptReason result_reason( | 251 content::DownloadInterruptReason result_reason( |
259 content::DOWNLOAD_INTERRUPT_REASON_NONE); | 252 content::DOWNLOAD_INTERRUPT_REASON_NONE); |
260 bool callback_was_called(false); | 253 bool callback_was_called(false); |
261 FilePath result_path; | 254 FilePath result_path; |
262 | 255 |
263 download_file_->Rename(full_path, overwrite_existing_file, | 256 download_file_->Rename(full_path, overwrite_existing_file, |
264 base::Bind(&DownloadFileTest::SetRenameResult, | 257 base::Bind(&DownloadFileTest::SetRenameResult, |
265 weak_ptr_factory.GetWeakPtr(), | 258 weak_ptr_factory.GetWeakPtr(), |
266 &callback_was_called, | 259 &callback_was_called, |
267 &result_reason, result_path_p)); | 260 &result_reason, result_path_p)); |
268 loop_.RunAllPending(); | 261 loop_.RunAllPending(); |
269 | 262 |
270 EXPECT_TRUE(callback_was_called); | 263 EXPECT_TRUE(callback_was_called); |
271 return result_reason; | 264 return result_reason; |
272 } | 265 } |
273 | 266 |
274 protected: | 267 protected: |
275 scoped_refptr<StrictMock<LocalMockDownloadManager> > download_manager_; | 268 scoped_ptr<StrictMock<MockDownloadDestinationController> > controller_; |
269 base::WeakPtrFactory<content::DownloadDestinationController> | |
270 controller_factory_; | |
276 | 271 |
277 linked_ptr<net::FileStream> file_stream_; | 272 linked_ptr<net::FileStream> file_stream_; |
278 | 273 |
279 // DownloadFile instance we are testing. | 274 // DownloadFile instance we are testing. |
280 scoped_ptr<DownloadFile> download_file_; | 275 scoped_ptr<DownloadFile> download_file_; |
281 | 276 |
282 // Stream for sending data into the download file. | 277 // Stream for sending data into the download file. |
283 // Owned by download_file_; will be alive for lifetime of download_file_. | 278 // Owned by download_file_; will be alive for lifetime of download_file_. |
284 StrictMock<MockByteStreamReader>* input_stream_; | 279 StrictMock<MockByteStreamReader>* input_stream_; |
285 | 280 |
286 // Sink callback data for stream. | 281 // Sink callback data for stream. |
287 base::Closure sink_callback_; | 282 base::Closure sink_callback_; |
288 | 283 |
289 // Latest update sent to the download manager. | 284 // Latest update sent to the controller. |
290 int32 update_download_id_; | |
291 int64 bytes_; | 285 int64 bytes_; |
292 int64 bytes_per_sec_; | 286 int64 bytes_per_sec_; |
293 std::string hash_state_; | 287 std::string hash_state_; |
294 | 288 |
295 MessageLoop loop_; | 289 MessageLoop loop_; |
296 | 290 |
297 private: | 291 private: |
298 void SetRenameResult(bool* called_p, | 292 void SetRenameResult(bool* called_p, |
299 content::DownloadInterruptReason* reason_p, | 293 content::DownloadInterruptReason* reason_p, |
300 FilePath* result_path_p, | 294 FilePath* result_path_p, |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
481 | 475 |
482 // Various tests of the StreamActive method. | 476 // Various tests of the StreamActive method. |
483 TEST_F(DownloadFileTest, StreamEmptySuccess) { | 477 TEST_F(DownloadFileTest, StreamEmptySuccess) { |
484 ASSERT_TRUE(CreateDownloadFile(0, true)); | 478 ASSERT_TRUE(CreateDownloadFile(0, true)); |
485 FilePath initial_path(download_file_->FullPath()); | 479 FilePath initial_path(download_file_->FullPath()); |
486 EXPECT_TRUE(file_util::PathExists(initial_path)); | 480 EXPECT_TRUE(file_util::PathExists(initial_path)); |
487 | 481 |
488 // Test that calling the sink_callback_ on an empty stream shouldn't | 482 // Test that calling the sink_callback_ on an empty stream shouldn't |
489 // do anything. | 483 // do anything. |
490 AppendDataToFile(NULL, 0); | 484 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 | 485 |
499 // Finish the download this way and make sure we see it on the | 486 // Finish the download this way and make sure we see it on the |
500 // DownloadManager. | 487 // controller. |
501 EXPECT_CALL(*(download_manager_.get()), | 488 EXPECT_CALL(*(controller_.get()), DestinationCompleted(_)); |
502 OnResponseCompleted(DownloadId(kValidIdDomain, | |
503 kDummyDownloadId + 0).local(), | |
504 0, _)); | |
505 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, false); | 489 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, false); |
490 loop_.RunAllPending(); | |
506 | 491 |
507 DestroyDownloadFile(0); | 492 DestroyDownloadFile(0); |
508 } | 493 } |
509 | 494 |
510 TEST_F(DownloadFileTest, StreamEmptyError) { | 495 TEST_F(DownloadFileTest, StreamEmptyError) { |
511 ASSERT_TRUE(CreateDownloadFile(0, true)); | 496 ASSERT_TRUE(CreateDownloadFile(0, true)); |
512 FilePath initial_path(download_file_->FullPath()); | 497 FilePath initial_path(download_file_->FullPath()); |
513 EXPECT_TRUE(file_util::PathExists(initial_path)); | 498 EXPECT_TRUE(file_util::PathExists(initial_path)); |
514 | 499 |
515 // Finish the download in error and make sure we see it on the | 500 // Finish the download in error and make sure we see it on the |
516 // DownloadManager. | 501 // controller. |
517 EXPECT_CALL(*(download_manager_.get()), | 502 EXPECT_CALL(*(controller_.get()), |
518 OnDownloadInterrupted( | 503 DestinationError( |
519 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), | |
520 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED)) | 504 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED)) |
521 .WillOnce(InvokeWithoutArgs( | 505 .WillOnce(InvokeWithoutArgs( |
522 this, &DownloadFileTest::ConfirmUpdateDownloadInfo)); | 506 this, &DownloadFileTest::ConfirmUpdateDownloadInfo)); |
523 | 507 |
524 // If this next EXPECT_CALL fails flakily, it's probably a real failure. | 508 // 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 | 509 // 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 | 510 // 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 | 511 // doesn't produce an update, as the timer update may have triggered at the |
528 // same time. | 512 // same time. |
529 EXPECT_CALL(*(download_manager_.get()), | 513 EXPECT_CALL(*(controller_.get()), CurrentUpdateStatus(0, _, _)); |
530 CurrentUpdateStatus(kDummyDownloadId + 0, 0, _, _)); | |
531 | 514 |
532 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, false); | 515 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, false); |
533 | 516 |
534 loop_.RunAllPending(); | 517 loop_.RunAllPending(); |
535 | 518 |
536 DestroyDownloadFile(0); | 519 DestroyDownloadFile(0); |
537 } | 520 } |
538 | 521 |
539 TEST_F(DownloadFileTest, StreamNonEmptySuccess) { | 522 TEST_F(DownloadFileTest, StreamNonEmptySuccess) { |
540 ASSERT_TRUE(CreateDownloadFile(0, true)); | 523 ASSERT_TRUE(CreateDownloadFile(0, true)); |
541 FilePath initial_path(download_file_->FullPath()); | 524 FilePath initial_path(download_file_->FullPath()); |
542 EXPECT_TRUE(file_util::PathExists(initial_path)); | 525 EXPECT_TRUE(file_util::PathExists(initial_path)); |
543 | 526 |
544 const char* chunks1[] = { kTestData1, kTestData2 }; | 527 const char* chunks1[] = { kTestData1, kTestData2 }; |
545 ::testing::Sequence s1; | 528 ::testing::Sequence s1; |
546 SetupDataAppend(chunks1, 2, s1); | 529 SetupDataAppend(chunks1, 2, s1); |
547 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, s1); | 530 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, s1); |
548 EXPECT_CALL(*(download_manager_.get()), | 531 EXPECT_CALL(*(controller_.get()), DestinationCompleted(_)); |
549 OnResponseCompleted(DownloadId(kValidIdDomain, | |
550 kDummyDownloadId + 0).local(), | |
551 strlen(kTestData1) + strlen(kTestData2), | |
552 _)); | |
553 sink_callback_.Run(); | 532 sink_callback_.Run(); |
554 VerifyStreamAndSize(); | 533 VerifyStreamAndSize(); |
534 loop_.RunAllPending(); | |
555 DestroyDownloadFile(0); | 535 DestroyDownloadFile(0); |
556 } | 536 } |
557 | 537 |
558 TEST_F(DownloadFileTest, StreamNonEmptyError) { | 538 TEST_F(DownloadFileTest, StreamNonEmptyError) { |
559 ASSERT_TRUE(CreateDownloadFile(0, true)); | 539 ASSERT_TRUE(CreateDownloadFile(0, true)); |
560 FilePath initial_path(download_file_->FullPath()); | 540 FilePath initial_path(download_file_->FullPath()); |
561 EXPECT_TRUE(file_util::PathExists(initial_path)); | 541 EXPECT_TRUE(file_util::PathExists(initial_path)); |
562 | 542 |
563 const char* chunks1[] = { kTestData1, kTestData2 }; | 543 const char* chunks1[] = { kTestData1, kTestData2 }; |
564 ::testing::Sequence s1; | 544 ::testing::Sequence s1; |
565 SetupDataAppend(chunks1, 2, s1); | 545 SetupDataAppend(chunks1, 2, s1); |
566 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, | 546 SetupFinishStream(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, |
567 s1); | 547 s1); |
568 | 548 |
569 EXPECT_CALL(*(download_manager_.get()), | 549 EXPECT_CALL(*(controller_.get()), |
570 OnDownloadInterrupted( | 550 DestinationError( |
571 DownloadId(kValidIdDomain, kDummyDownloadId + 0).local(), | |
572 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED)) | 551 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED)) |
573 .WillOnce(InvokeWithoutArgs( | 552 .WillOnce(InvokeWithoutArgs( |
574 this, &DownloadFileTest::ConfirmUpdateDownloadInfo)); | 553 this, &DownloadFileTest::ConfirmUpdateDownloadInfo)); |
575 | 554 |
576 // If this next EXPECT_CALL fails flakily, it's probably a real failure. | 555 // 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 | 556 // 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 | 557 // 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 | 558 // doesn't produce an update, as the timer update may have triggered at the |
580 // same time. | 559 // same time. |
581 EXPECT_CALL(*(download_manager_.get()), | 560 EXPECT_CALL(*(controller_.get()), |
582 CurrentUpdateStatus(kDummyDownloadId + 0, | 561 CurrentUpdateStatus(strlen(kTestData1) + strlen(kTestData2), |
583 strlen(kTestData1) + strlen(kTestData2), | |
584 _, _)); | 562 _, _)); |
585 | 563 |
586 sink_callback_.Run(); | 564 sink_callback_.Run(); |
587 loop_.RunAllPending(); | 565 loop_.RunAllPending(); |
588 VerifyStreamAndSize(); | 566 VerifyStreamAndSize(); |
589 DestroyDownloadFile(0); | 567 DestroyDownloadFile(0); |
590 } | 568 } |
591 | 569 |
592 // Send some data, wait 3/4s of a second, run the message loop, and | 570 // Send some data, wait 3/4s of a second, run the message loop, and |
593 // confirm the values the DownloadManager received are correct. | 571 // confirm the values the controller received are correct. |
594 TEST_F(DownloadFileTest, ConfirmUpdate) { | 572 TEST_F(DownloadFileTest, ConfirmUpdate) { |
595 CreateDownloadFile(0, true); | 573 CreateDownloadFile(0, true); |
596 | 574 |
597 const char* chunks1[] = { kTestData1, kTestData2 }; | 575 const char* chunks1[] = { kTestData1, kTestData2 }; |
598 AppendDataToFile(chunks1, 2); | 576 AppendDataToFile(chunks1, 2); |
599 | 577 |
600 // Run the message loops for 750ms and check for results. | 578 // Run the message loops for 750ms and check for results. |
601 loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), | 579 loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), |
602 base::TimeDelta::FromMilliseconds(750)); | 580 base::TimeDelta::FromMilliseconds(750)); |
603 loop_.Run(); | 581 loop_.Run(); |
604 | 582 |
605 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)), | 583 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)), |
606 bytes_); | 584 bytes_); |
607 EXPECT_EQ(download_file_->GetHashState(), hash_state_); | 585 EXPECT_EQ(download_file_->GetHashState(), hash_state_); |
608 | 586 |
609 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true); | 587 FinishStream(content::DOWNLOAD_INTERRUPT_REASON_NONE, true); |
610 DestroyDownloadFile(0); | 588 DestroyDownloadFile(0); |
611 } | 589 } |
OLD | NEW |