| 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/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
| 7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/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/byte_stream.h" | 10 #include "content/browser/byte_stream.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 using ::testing::AnyNumber; | 26 using ::testing::AnyNumber; |
| 27 using ::testing::DoAll; | 27 using ::testing::DoAll; |
| 28 using ::testing::InSequence; | 28 using ::testing::InSequence; |
| 29 using ::testing::Return; | 29 using ::testing::Return; |
| 30 using ::testing::SetArgPointee; | 30 using ::testing::SetArgPointee; |
| 31 using ::testing::StrictMock; | 31 using ::testing::StrictMock; |
| 32 | 32 |
| 33 namespace content { | 33 namespace content { |
| 34 namespace { | 34 namespace { |
| 35 | 35 |
| 36 class MockByteStreamReader : public ByteStreamReader { | 36 class MockByteStreamReader : public ByteStreamReader<DownloadInterruptReason> { |
| 37 public: | 37 public: |
| 38 MockByteStreamReader() {} | 38 MockByteStreamReader() {} |
| 39 ~MockByteStreamReader() {} | 39 ~MockByteStreamReader() {} |
| 40 | 40 |
| 41 // ByteStream functions | 41 // ByteStream functions |
| 42 MOCK_METHOD2(Read, ByteStreamReader::StreamState( | 42 MOCK_METHOD2(Read, ByteStreamReader<DownloadInterruptReason>::StreamState( |
| 43 scoped_refptr<net::IOBuffer>*, size_t*)); | 43 scoped_refptr<net::IOBuffer>*, size_t*)); |
| 44 MOCK_CONST_METHOD0(GetStatus, DownloadInterruptReason()); | 44 MOCK_CONST_METHOD0(GetStatus, DownloadInterruptReason()); |
| 45 MOCK_METHOD1(RegisterCallback, void(const base::Closure&)); | 45 MOCK_METHOD1(RegisterCallback, void(const base::Closure&)); |
| 46 }; | 46 }; |
| 47 | 47 |
| 48 class MockDownloadDestinationObserver : public DownloadDestinationObserver { | 48 class MockDownloadDestinationObserver : public DownloadDestinationObserver { |
| 49 public: | 49 public: |
| 50 MOCK_METHOD3(DestinationUpdate, void(int64, int64, const std::string&)); | 50 MOCK_METHOD3(DestinationUpdate, void(int64, int64, const std::string&)); |
| 51 MOCK_METHOD1(DestinationError, void(DownloadInterruptReason)); | 51 MOCK_METHOD1(DestinationError, void(DownloadInterruptReason)); |
| 52 MOCK_METHOD1(DestinationCompleted, void(const std::string&)); | 52 MOCK_METHOD1(DestinationCompleted, void(const std::string&)); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 input_stream_ = new StrictMock<MockByteStreamReader>(); | 122 input_stream_ = new StrictMock<MockByteStreamReader>(); |
| 123 | 123 |
| 124 // TODO: Need to actually create a function that'll set the variables | 124 // TODO: Need to actually create a function that'll set the variables |
| 125 // based on the inputs from the callback. | 125 // based on the inputs from the callback. |
| 126 EXPECT_CALL(*input_stream_, RegisterCallback(_)) | 126 EXPECT_CALL(*input_stream_, RegisterCallback(_)) |
| 127 .WillOnce(Invoke(this, &DownloadFileTest::RegisterCallback)) | 127 .WillOnce(Invoke(this, &DownloadFileTest::RegisterCallback)) |
| 128 .RetiresOnSaturation(); | 128 .RetiresOnSaturation(); |
| 129 | 129 |
| 130 scoped_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo()); | 130 scoped_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo()); |
| 131 download_file_.reset( | 131 download_file_.reset( |
| 132 new DownloadFileImpl(save_info.Pass(), | 132 new DownloadFileImpl( |
| 133 base::FilePath(), | 133 save_info.Pass(), |
| 134 GURL(), // Source | 134 base::FilePath(), |
| 135 GURL(), // Referrer | 135 GURL(), // Source |
| 136 calculate_hash, | 136 GURL(), // Referrer |
| 137 scoped_ptr<ByteStreamReader>(input_stream_), | 137 calculate_hash, |
| 138 net::BoundNetLog(), | 138 scoped_ptr<ByteStreamReader<DownloadInterruptReason> >( |
| 139 scoped_ptr<PowerSaveBlocker>().Pass(), | 139 input_stream_), |
| 140 observer_factory_.GetWeakPtr())); | 140 net::BoundNetLog(), |
| 141 scoped_ptr<PowerSaveBlocker>().Pass(), |
| 142 observer_factory_.GetWeakPtr())); |
| 141 | 143 |
| 142 EXPECT_CALL(*input_stream_, Read(_, _)) | 144 EXPECT_CALL(*input_stream_, Read(_, _)) |
| 143 .WillOnce(Return(ByteStreamReader::STREAM_EMPTY)) | 145 .WillOnce(Return( |
| 146 ByteStreamReader<DownloadInterruptReason>::STREAM_EMPTY)) |
| 144 .RetiresOnSaturation(); | 147 .RetiresOnSaturation(); |
| 145 | 148 |
| 146 base::WeakPtrFactory<DownloadFileTest> weak_ptr_factory(this); | 149 base::WeakPtrFactory<DownloadFileTest> weak_ptr_factory(this); |
| 147 bool called = false; | 150 bool called = false; |
| 148 DownloadInterruptReason result; | 151 DownloadInterruptReason result; |
| 149 download_file_->Initialize(base::Bind( | 152 download_file_->Initialize(base::Bind( |
| 150 &DownloadFileTest::SetInterruptReasonCallback, | 153 &DownloadFileTest::SetInterruptReasonCallback, |
| 151 weak_ptr_factory.GetWeakPtr(), &called, &result)); | 154 weak_ptr_factory.GetWeakPtr(), &called, &result)); |
| 152 loop_.RunUntilIdle(); | 155 loop_.RunUntilIdle(); |
| 153 EXPECT_TRUE(called); | 156 EXPECT_TRUE(called); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 177 DCHECK(input_stream_); | 180 DCHECK(input_stream_); |
| 178 for (size_t i = 0; i < num_chunks; i++) { | 181 for (size_t i = 0; i < num_chunks; i++) { |
| 179 const char *source_data = data_chunks[i]; | 182 const char *source_data = data_chunks[i]; |
| 180 size_t length = strlen(source_data); | 183 size_t length = strlen(source_data); |
| 181 scoped_refptr<net::IOBuffer> data = new net::IOBuffer(length); | 184 scoped_refptr<net::IOBuffer> data = new net::IOBuffer(length); |
| 182 memcpy(data->data(), source_data, length); | 185 memcpy(data->data(), source_data, length); |
| 183 EXPECT_CALL(*input_stream_, Read(_, _)) | 186 EXPECT_CALL(*input_stream_, Read(_, _)) |
| 184 .InSequence(s) | 187 .InSequence(s) |
| 185 .WillOnce(DoAll(SetArgPointee<0>(data), | 188 .WillOnce(DoAll(SetArgPointee<0>(data), |
| 186 SetArgPointee<1>(length), | 189 SetArgPointee<1>(length), |
| 187 Return(ByteStreamReader::STREAM_HAS_DATA))) | 190 Return( |
| 191 ByteStreamReader<DownloadInterruptReason>:: |
| 192 STREAM_HAS_DATA))) |
| 188 .RetiresOnSaturation(); | 193 .RetiresOnSaturation(); |
| 189 expected_data_ += source_data; | 194 expected_data_ += source_data; |
| 190 } | 195 } |
| 191 } | 196 } |
| 192 | 197 |
| 193 void VerifyStreamAndSize() { | 198 void VerifyStreamAndSize() { |
| 194 ::testing::Mock::VerifyAndClearExpectations(input_stream_); | 199 ::testing::Mock::VerifyAndClearExpectations(input_stream_); |
| 195 int64 size; | 200 int64 size; |
| 196 EXPECT_TRUE(file_util::GetFileSize(download_file_->FullPath(), &size)); | 201 EXPECT_TRUE(file_util::GetFileSize(download_file_->FullPath(), &size)); |
| 197 EXPECT_EQ(expected_data_.size(), static_cast<size_t>(size)); | 202 EXPECT_EQ(expected_data_.size(), static_cast<size_t>(size)); |
| 198 } | 203 } |
| 199 | 204 |
| 200 // TODO(rdsmith): Manage full percentage issues properly. | 205 // TODO(rdsmith): Manage full percentage issues properly. |
| 201 void AppendDataToFile(const char **data_chunks, size_t num_chunks) { | 206 void AppendDataToFile(const char **data_chunks, size_t num_chunks) { |
| 202 ::testing::Sequence s1; | 207 ::testing::Sequence s1; |
| 203 SetupDataAppend(data_chunks, num_chunks, s1); | 208 SetupDataAppend(data_chunks, num_chunks, s1); |
| 204 EXPECT_CALL(*input_stream_, Read(_, _)) | 209 EXPECT_CALL(*input_stream_, Read(_, _)) |
| 205 .InSequence(s1) | 210 .InSequence(s1) |
| 206 .WillOnce(Return(ByteStreamReader::STREAM_EMPTY)) | 211 .WillOnce(Return( |
| 212 ByteStreamReader<DownloadInterruptReason>::STREAM_EMPTY)) |
| 207 .RetiresOnSaturation(); | 213 .RetiresOnSaturation(); |
| 208 sink_callback_.Run(); | 214 sink_callback_.Run(); |
| 209 VerifyStreamAndSize(); | 215 VerifyStreamAndSize(); |
| 210 } | 216 } |
| 211 | 217 |
| 212 void SetupFinishStream(DownloadInterruptReason interrupt_reason, | 218 void SetupFinishStream(DownloadInterruptReason interrupt_reason, |
| 213 ::testing::Sequence s) { | 219 ::testing::Sequence s) { |
| 214 EXPECT_CALL(*input_stream_, Read(_, _)) | 220 EXPECT_CALL(*input_stream_, Read(_, _)) |
| 215 .InSequence(s) | 221 .InSequence(s) |
| 216 .WillOnce(Return(ByteStreamReader::STREAM_COMPLETE)) | 222 .WillOnce(Return( |
| 223 ByteStreamReader<DownloadInterruptReason>::STREAM_COMPLETE)) |
| 217 .RetiresOnSaturation(); | 224 .RetiresOnSaturation(); |
| 218 EXPECT_CALL(*input_stream_, GetStatus()) | 225 EXPECT_CALL(*input_stream_, GetStatus()) |
| 219 .InSequence(s) | 226 .InSequence(s) |
| 220 .WillOnce(Return(interrupt_reason)) | 227 .WillOnce(Return(interrupt_reason)) |
| 221 .RetiresOnSaturation(); | 228 .RetiresOnSaturation(); |
| 222 EXPECT_CALL(*input_stream_, RegisterCallback(_)) | 229 EXPECT_CALL(*input_stream_, RegisterCallback(_)) |
| 223 .RetiresOnSaturation(); | 230 .RetiresOnSaturation(); |
| 224 } | 231 } |
| 225 | 232 |
| 226 void FinishStream(DownloadInterruptReason interrupt_reason, | 233 void FinishStream(DownloadInterruptReason interrupt_reason, |
| (...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 594 | 601 |
| 595 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)), | 602 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)), |
| 596 bytes_); | 603 bytes_); |
| 597 EXPECT_EQ(download_file_->GetHashState(), hash_state_); | 604 EXPECT_EQ(download_file_->GetHashState(), hash_state_); |
| 598 | 605 |
| 599 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true); | 606 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true); |
| 600 DestroyDownloadFile(0); | 607 DestroyDownloadFile(0); |
| 601 } | 608 } |
| 602 | 609 |
| 603 } // namespace content | 610 } // namespace content |
| OLD | NEW |