Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/bind.h" | |
| 6 #include "base/callback.h" | |
| 7 #include "base/location.h" | |
| 8 #include "base/memory/ref_counted.h" | |
| 9 #include "base/task_runner.h" | |
| 10 #include "content/browser/download/byte_stream.h" | |
|
willchan no longer on Chromium
2012/04/27 21:22:29
goes first, see google c++ style guide's include o
| |
| 11 #include "net/base/io_buffer.h" | |
| 12 #include "testing/gmock/include/gmock/gmock.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 using ::testing::_; | |
| 16 using ::testing::Return; | |
| 17 using ::testing::SaveArg; | |
| 18 using ::testing::StrictMock; | |
| 19 | |
| 20 namespace tracked_objects { | |
| 21 class Location; | |
| 22 } | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 class MockTaskRunner : public base::TaskRunner { | |
| 27 public: | |
| 28 MockTaskRunner(); | |
| 29 | |
| 30 // TaskRunner functions. | |
| 31 MOCK_METHOD3(PostDelayedTask, bool(const tracked_objects::Location&, | |
| 32 const base::Closure&, int64)); | |
| 33 MOCK_METHOD3(PostDelayedTask, bool(const tracked_objects::Location&, | |
| 34 const base::Closure&, base::TimeDelta)); | |
| 35 MOCK_CONST_METHOD0(RunsTasksOnCurrentThread, bool()); | |
| 36 | |
| 37 protected: | |
| 38 ~MockTaskRunner(); | |
| 39 }; | |
| 40 | |
| 41 MockTaskRunner::MockTaskRunner() { } | |
| 42 | |
| 43 MockTaskRunner::~MockTaskRunner() { } | |
| 44 | |
| 45 int null_callback_call_count = 0; | |
| 46 | |
| 47 void NullCallback(int arg) { | |
| 48 null_callback_call_count++; | |
| 49 } | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 class ByteStreamTest : public testing::Test { | |
| 54 public: | |
| 55 // Create a new IO buffer of the given |buffer_size|, with contents | |
| 56 // dependent on the |seed_key|. The |seed_key| is also used for comparing | |
| 57 // pointers between NewIOBuffer and ValidateIOBuffer; do not re-use any | |
| 58 // |seed_key| value within a single test. | |
| 59 scoped_refptr<net::IOBuffer> NewIOBuffer(size_t buffer_size, int seed_key) { | |
| 60 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(buffer_size)); | |
| 61 char *bufferp = buffer->data(); | |
| 62 for (size_t i = 0; i < buffer_size; i++) | |
| 63 bufferp[i] = (i + seed_key) % (1 << sizeof(char)); | |
| 64 DCHECK(pointer_map_.find(seed_key) == pointer_map_.end()); | |
| 65 DCHECK(length_map_.find(seed_key) == length_map_.end()); | |
| 66 pointer_map_[seed_key] = bufferp; | |
| 67 length_map_[seed_key] = buffer_size; | |
| 68 return buffer; | |
| 69 } | |
| 70 | |
| 71 // Create an IOBuffer of the appropriate size and add it to the | |
| 72 // ByteStream, returning the result of the ByteStream::AddData. | |
| 73 // Separate function to avoid duplication of buffer_size in test | |
| 74 // calls. | |
| 75 bool AddData(scoped_refptr<content::ByteStream> byte_stream, | |
| 76 int seed_key, size_t buffer_size) { | |
| 77 return byte_stream->AddData(NewIOBuffer(buffer_size, seed_key), | |
| 78 buffer_size); | |
| 79 } | |
| 80 | |
| 81 // Validate that we have the IOBuffer we expect. This IOBuffer must | |
| 82 // have been created through NewIOBuffer with the given |buffer_size| | |
| 83 // and |seed_key|. | |
| 84 bool ValidateIOBuffer(scoped_refptr<net::IOBuffer> buffer, int seed_key, | |
| 85 size_t buffer_size) { | |
| 86 char *bufferp = buffer->data(); | |
| 87 EXPECT_TRUE(pointer_map_.find(seed_key) != pointer_map_.end()); | |
| 88 if (pointer_map_.find(seed_key) == pointer_map_.end()) | |
| 89 return false; | |
| 90 EXPECT_EQ(bufferp, pointer_map_[seed_key]); | |
| 91 EXPECT_TRUE(length_map_.find(seed_key) != length_map_.end()); | |
| 92 if (length_map_.find(seed_key) == length_map_.end()) | |
| 93 return false; | |
| 94 EXPECT_EQ(buffer_size, length_map_[seed_key]); | |
| 95 for (size_t i = 0; i < buffer_size; i++) { | |
| 96 EXPECT_EQ(static_cast<int>((i + seed_key) % (1 << sizeof(char))), | |
| 97 bufferp[i]); | |
| 98 if (static_cast<int>((i + seed_key) % (1 << sizeof(char))) != bufferp[i]) | |
| 99 return false; | |
| 100 } | |
| 101 return true; | |
| 102 } | |
| 103 | |
| 104 private: | |
| 105 std::map<int, char*> pointer_map_; | |
| 106 std::map<int, size_t> length_map_; | |
| 107 }; | |
| 108 | |
| 109 // Confirm that filling and emptying the pipe works properly, and that | |
| 110 // we get full triggers when we expect. | |
| 111 TEST_F(ByteStreamTest, PushBack) { | |
| 112 scoped_refptr<content::ByteStream> byte_stream(new content::ByteStream()); | |
| 113 byte_stream->SetBufferSize(3 * 1024); | |
| 114 | |
| 115 // Push a series of IO buffers on; test pushback happening and | |
| 116 // that it's advisory. | |
| 117 EXPECT_TRUE(AddData(byte_stream, 0, 1024)); | |
| 118 EXPECT_FALSE(byte_stream->IsFull()); | |
| 119 EXPECT_TRUE(AddData(byte_stream, 1, 1024)); | |
| 120 EXPECT_FALSE(byte_stream->IsFull()); | |
| 121 EXPECT_TRUE(AddData(byte_stream, 2, 1024)); | |
| 122 EXPECT_FALSE(byte_stream->IsFull()); | |
| 123 EXPECT_FALSE(AddData(byte_stream, 3, 1)); | |
| 124 EXPECT_TRUE(byte_stream->IsFull()); | |
| 125 EXPECT_FALSE(AddData(byte_stream, 4, 1024)); | |
| 126 EXPECT_TRUE(byte_stream->IsFull()); | |
| 127 EXPECT_EQ(0u, byte_stream->bytes_read()); | |
| 128 EXPECT_EQ(0u, byte_stream->buffers_read()); | |
| 129 | |
| 130 // Pull the IO buffers out; do we get the same buffers and do they | |
| 131 // have the same contents? | |
| 132 scoped_refptr<net::IOBuffer> output_io_buffer; | |
| 133 size_t output_length; | |
| 134 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 135 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 136 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 0, output_length)); | |
| 137 EXPECT_EQ(1024u, byte_stream->bytes_read()); | |
| 138 EXPECT_EQ(1u, byte_stream->buffers_read()); | |
| 139 | |
| 140 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 141 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 142 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 1, output_length)); | |
| 143 EXPECT_EQ(2048u, byte_stream->bytes_read()); | |
| 144 EXPECT_EQ(2u, byte_stream->buffers_read()); | |
| 145 | |
| 146 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 147 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 148 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 2, output_length)); | |
| 149 EXPECT_EQ(3072u, byte_stream->bytes_read()); | |
| 150 EXPECT_EQ(3u, byte_stream->buffers_read()); | |
| 151 | |
| 152 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 153 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 154 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 3, output_length)); | |
| 155 EXPECT_EQ(3073u, byte_stream->bytes_read()); | |
| 156 EXPECT_EQ(4u, byte_stream->buffers_read()); | |
| 157 | |
| 158 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 159 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 160 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 4, output_length)); | |
| 161 EXPECT_EQ(4097u, byte_stream->bytes_read()); | |
| 162 EXPECT_EQ(5u, byte_stream->buffers_read()); | |
| 163 | |
| 164 EXPECT_EQ(content::ByteStream::STREAM_EMPTY, | |
| 165 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 166 } | |
| 167 | |
| 168 // Confirm that a SourceComplete() notification transmits in-order | |
| 169 // with data on the pipe. | |
| 170 TEST_F(ByteStreamTest, CompleteTransmits) { | |
| 171 scoped_refptr<content::ByteStream> byte_stream; | |
| 172 | |
| 173 scoped_refptr<net::IOBuffer> output_io_buffer; | |
| 174 size_t output_length; | |
| 175 | |
| 176 // Empty stream, non-error case. | |
| 177 byte_stream = new content::ByteStream(); | |
| 178 EXPECT_EQ(content::ByteStream::STREAM_EMPTY, | |
| 179 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 180 byte_stream->SourceComplete(content::DOWNLOAD_INTERRUPT_REASON_NONE); | |
| 181 ASSERT_EQ(content::ByteStream::STREAM_COMPLETE, | |
| 182 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 183 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, | |
| 184 byte_stream->GetSourceResult()); | |
| 185 | |
| 186 // Non-empty stream, non-error case. | |
| 187 byte_stream = new content::ByteStream(); | |
| 188 EXPECT_EQ(content::ByteStream::STREAM_EMPTY, | |
| 189 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 190 EXPECT_TRUE(AddData(byte_stream, 0, 1024)); | |
| 191 byte_stream->SourceComplete(content::DOWNLOAD_INTERRUPT_REASON_NONE); | |
| 192 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 193 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 194 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 0, output_length)); | |
| 195 ASSERT_EQ(content::ByteStream::STREAM_COMPLETE, | |
| 196 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 197 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, | |
| 198 byte_stream->GetSourceResult()); | |
| 199 | |
| 200 // Empty stream, non-error case. | |
| 201 byte_stream = new content::ByteStream(); | |
| 202 EXPECT_EQ(content::ByteStream::STREAM_EMPTY, | |
| 203 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 204 byte_stream->SourceComplete( | |
| 205 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED); | |
| 206 ASSERT_EQ(content::ByteStream::STREAM_COMPLETE, | |
| 207 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 208 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, | |
| 209 byte_stream->GetSourceResult()); | |
| 210 | |
| 211 // Non-empty stream, non-error case. | |
| 212 byte_stream = new content::ByteStream(); | |
| 213 EXPECT_EQ(content::ByteStream::STREAM_EMPTY, | |
| 214 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 215 EXPECT_TRUE(AddData(byte_stream, 1, 1024)); | |
| 216 byte_stream->SourceComplete( | |
| 217 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED); | |
| 218 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 219 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 220 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 1, output_length)); | |
| 221 ASSERT_EQ(content::ByteStream::STREAM_COMPLETE, | |
| 222 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 223 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, | |
| 224 byte_stream->GetSourceResult()); | |
| 225 } | |
| 226 | |
| 227 // Confirm that callbacks on the sink side are triggered when they should be. | |
| 228 TEST_F(ByteStreamTest, SinkCallback) { | |
| 229 scoped_refptr<MockTaskRunner> task_runner(new StrictMock<MockTaskRunner>()); | |
| 230 int null_callback_call_count_start = 0; | |
| 231 | |
| 232 scoped_refptr<content::ByteStream> byte_stream(new content::ByteStream()); | |
| 233 byte_stream->SetBufferSize(10000); | |
| 234 | |
| 235 scoped_refptr<net::IOBuffer> output_io_buffer; | |
| 236 size_t output_length; | |
| 237 base::Closure intermediate_callback; | |
| 238 | |
| 239 // Callback to be called on any data. | |
| 240 byte_stream->RegisterSinkCallback(task_runner, | |
| 241 base::Bind(NullCallback, 0), | |
| 242 0); | |
| 243 null_callback_call_count_start = null_callback_call_count; | |
| 244 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, 0)) | |
| 245 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), | |
| 246 Return(true))); | |
| 247 EXPECT_EQ(0u, byte_stream->num_sink_callbacks()); | |
| 248 EXPECT_TRUE(AddData(byte_stream, 0, 1024)); | |
| 249 EXPECT_EQ(1u, byte_stream->num_sink_callbacks()); | |
| 250 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); | |
| 251 EXPECT_EQ(null_callback_call_count_start, null_callback_call_count); | |
| 252 intermediate_callback.Run(); | |
| 253 EXPECT_EQ(null_callback_call_count_start+1, null_callback_call_count); | |
| 254 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 255 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 256 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 0, output_length)); | |
| 257 EXPECT_EQ(content::ByteStream::STREAM_EMPTY, | |
| 258 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 259 | |
| 260 // Callback to be called on 33% full. | |
| 261 byte_stream->RegisterSinkCallback(task_runner, | |
| 262 base::Bind(NullCallback, 0), | |
| 263 33); | |
| 264 null_callback_call_count_start = null_callback_call_count; | |
| 265 EXPECT_TRUE(AddData(byte_stream, 1, 3300)); | |
| 266 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, 0)) | |
| 267 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), | |
| 268 Return(true))); | |
| 269 EXPECT_EQ(1u, byte_stream->num_sink_callbacks()); | |
| 270 EXPECT_TRUE(AddData(byte_stream, 2, 1)); | |
| 271 EXPECT_EQ(2u, byte_stream->num_sink_callbacks()); | |
| 272 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); | |
| 273 EXPECT_EQ(null_callback_call_count_start, null_callback_call_count); | |
| 274 intermediate_callback.Run(); | |
| 275 EXPECT_EQ(null_callback_call_count_start+1, null_callback_call_count); | |
| 276 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 277 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 278 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 1, output_length)); | |
| 279 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 280 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 281 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 2, output_length)); | |
| 282 EXPECT_EQ(content::ByteStream::STREAM_EMPTY, | |
| 283 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 284 | |
| 285 // Callback to be called on 100% full. | |
| 286 byte_stream->RegisterSinkCallback(task_runner, | |
| 287 base::Bind(NullCallback, 0), | |
| 288 100); | |
| 289 null_callback_call_count_start = null_callback_call_count; | |
| 290 EXPECT_TRUE(AddData(byte_stream, 3, 3300)); | |
| 291 EXPECT_TRUE(AddData(byte_stream, 4, 3300)); | |
| 292 EXPECT_TRUE(AddData(byte_stream, 5, 3400)); | |
| 293 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, 0)) | |
| 294 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), | |
| 295 Return(true))); | |
| 296 EXPECT_EQ(2u, byte_stream->num_sink_callbacks()); | |
| 297 EXPECT_FALSE(AddData(byte_stream, 6, 1)); | |
| 298 EXPECT_EQ(3u, byte_stream->num_sink_callbacks()); | |
| 299 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); | |
| 300 EXPECT_EQ(null_callback_call_count_start, null_callback_call_count); | |
| 301 intermediate_callback.Run(); | |
| 302 EXPECT_EQ(null_callback_call_count_start+1, null_callback_call_count); | |
| 303 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 304 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 305 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 3, output_length)); | |
| 306 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 307 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 308 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 4, output_length)); | |
| 309 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 310 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 311 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 5, output_length)); | |
| 312 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 313 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 314 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 6, output_length)); | |
| 315 EXPECT_EQ(content::ByteStream::STREAM_EMPTY, | |
| 316 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 317 EXPECT_EQ(3u, byte_stream->num_sink_callbacks()); | |
| 318 } | |
| 319 | |
| 320 // Confirm that callbacks on the source side are triggered when they should | |
| 321 // be. | |
| 322 TEST_F(ByteStreamTest, SourceCallback) { | |
| 323 scoped_refptr<MockTaskRunner> task_runner(new StrictMock<MockTaskRunner>()); | |
| 324 int null_callback_call_count_start = 0; | |
| 325 | |
| 326 scoped_refptr<content::ByteStream> byte_stream(new content::ByteStream()); | |
| 327 byte_stream->SetBufferSize(10000); | |
| 328 | |
| 329 scoped_refptr<net::IOBuffer> output_io_buffer; | |
| 330 size_t output_length; | |
| 331 base::Closure intermediate_callback; | |
| 332 | |
| 333 // Confirm callback called when any space available. | |
| 334 EXPECT_TRUE(AddData(byte_stream, 0, 1)); | |
| 335 EXPECT_FALSE(AddData(byte_stream, 1, 10000)); | |
| 336 byte_stream->RegisterSourceCallback(task_runner, | |
| 337 base::Bind(NullCallback, 0), | |
| 338 0); | |
| 339 null_callback_call_count_start = null_callback_call_count; | |
| 340 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, 0)) | |
| 341 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), | |
| 342 Return(true))); | |
| 343 EXPECT_EQ(0u, byte_stream->num_source_callbacks()); | |
| 344 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 345 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 346 EXPECT_EQ(1u, byte_stream->num_source_callbacks()); | |
| 347 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 0, output_length)); | |
| 348 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); | |
| 349 intermediate_callback.Run(); | |
| 350 EXPECT_EQ(null_callback_call_count_start+1, null_callback_call_count); | |
| 351 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 352 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 353 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 1, output_length)); | |
| 354 EXPECT_EQ(content::ByteStream::STREAM_EMPTY, | |
| 355 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 356 | |
| 357 // Confirm callback called when 33% space available. | |
| 358 EXPECT_TRUE(AddData(byte_stream, 2, 3300)); | |
| 359 EXPECT_TRUE(AddData(byte_stream, 3, 1)); | |
| 360 EXPECT_FALSE(AddData(byte_stream, 4, 6700)); | |
| 361 byte_stream->RegisterSourceCallback(task_runner, | |
| 362 base::Bind(NullCallback, 0), | |
| 363 33); | |
| 364 null_callback_call_count_start = null_callback_call_count; | |
| 365 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 366 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 367 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 2, output_length)); | |
| 368 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, 0)) | |
| 369 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), | |
| 370 Return(true))); | |
| 371 EXPECT_EQ(1u, byte_stream->num_source_callbacks()); | |
| 372 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 373 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 374 EXPECT_EQ(2u, byte_stream->num_source_callbacks()); | |
| 375 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 3, output_length)); | |
| 376 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); | |
| 377 intermediate_callback.Run(); | |
| 378 EXPECT_EQ(null_callback_call_count_start+1, null_callback_call_count); | |
| 379 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 380 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 381 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 4, output_length)); | |
| 382 EXPECT_EQ(content::ByteStream::STREAM_EMPTY, | |
| 383 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 384 | |
| 385 // Confirm callback called only when 100% space available. | |
| 386 EXPECT_TRUE(AddData(byte_stream, 5, 3300)); | |
| 387 EXPECT_TRUE(AddData(byte_stream, 6, 6700)); | |
| 388 EXPECT_FALSE(AddData(byte_stream, 7, 1)); | |
| 389 byte_stream->RegisterSourceCallback(task_runner, | |
| 390 base::Bind(NullCallback, 0), | |
| 391 100); | |
| 392 null_callback_call_count_start = null_callback_call_count; | |
| 393 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 394 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 395 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 5, output_length)); | |
| 396 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 397 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 398 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 6, output_length)); | |
| 399 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, 0)) | |
| 400 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), | |
| 401 Return(true))); | |
| 402 EXPECT_EQ(2u, byte_stream->num_source_callbacks()); | |
| 403 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 404 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 405 EXPECT_EQ(3u, byte_stream->num_source_callbacks()); | |
| 406 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 7, output_length)); | |
| 407 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); | |
| 408 intermediate_callback.Run(); | |
| 409 EXPECT_EQ(null_callback_call_count_start+1, null_callback_call_count); | |
| 410 EXPECT_EQ(content::ByteStream::STREAM_EMPTY, | |
| 411 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 412 EXPECT_EQ(3u, byte_stream->num_source_callbacks()); | |
| 413 } | |
| 414 | |
| 415 // Confirm that changing a sink callback on the same "thread" as the original | |
| 416 // callback was targeted to results in the callback being dropped on the | |
| 417 // floor. | |
| 418 TEST_F(ByteStreamTest, SinkInterrupt) { | |
| 419 scoped_refptr<MockTaskRunner> task_runner(new StrictMock<MockTaskRunner>()); | |
| 420 int null_callback_call_count_start = 0; | |
| 421 | |
| 422 scoped_refptr<content::ByteStream> byte_stream(new content::ByteStream()); | |
| 423 byte_stream->SetBufferSize(10000); | |
| 424 | |
| 425 base::Closure intermediate_callback; | |
| 426 | |
| 427 // Callback to be called on any data. | |
| 428 byte_stream->RegisterSinkCallback(task_runner, | |
| 429 base::Bind(NullCallback, 0), | |
| 430 0); | |
| 431 null_callback_call_count_start = null_callback_call_count; | |
| 432 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, 0)) | |
| 433 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), | |
| 434 Return(true))); | |
| 435 EXPECT_TRUE(AddData(byte_stream, 0, 1024)); | |
| 436 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); | |
| 437 EXPECT_EQ(null_callback_call_count_start, null_callback_call_count); | |
| 438 byte_stream->RegisterSinkCallback(scoped_refptr<base::TaskRunner>(), | |
| 439 base::Closure(), 0); | |
| 440 intermediate_callback.Run(); | |
| 441 EXPECT_EQ(null_callback_call_count_start, null_callback_call_count); | |
| 442 } | |
| 443 | |
| 444 // Confirm that changing a source callback on the same "thread" as the original | |
| 445 // callback was targeted to results in the callback being dropped on the | |
| 446 // floor. | |
| 447 TEST_F(ByteStreamTest, SourceInterrupt) { | |
| 448 scoped_refptr<MockTaskRunner> task_runner(new StrictMock<MockTaskRunner>()); | |
| 449 int null_callback_call_count_start = 0; | |
| 450 | |
| 451 scoped_refptr<content::ByteStream> byte_stream(new content::ByteStream()); | |
| 452 byte_stream->SetBufferSize(10000); | |
| 453 | |
| 454 base::Closure intermediate_callback; | |
| 455 scoped_refptr<net::IOBuffer> output_io_buffer; | |
| 456 size_t output_length; | |
| 457 | |
| 458 EXPECT_TRUE(AddData(byte_stream, 0, 1)); | |
| 459 EXPECT_FALSE(AddData(byte_stream, 1, 10000)); | |
| 460 byte_stream->RegisterSourceCallback(task_runner, | |
| 461 base::Bind(NullCallback, 0), | |
| 462 0); | |
| 463 null_callback_call_count_start = null_callback_call_count; | |
| 464 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, 0)) | |
| 465 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), | |
| 466 Return(true))); | |
| 467 EXPECT_EQ(content::ByteStream::STREAM_HAS_DATA, | |
| 468 byte_stream->GetData(&output_io_buffer, &output_length)); | |
| 469 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, 0, output_length)); | |
| 470 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); | |
| 471 byte_stream->RegisterSourceCallback(scoped_refptr<base::TaskRunner>(), | |
| 472 base::Closure(), 0); | |
| 473 intermediate_callback.Run(); | |
| 474 EXPECT_EQ(null_callback_call_count_start, null_callback_call_count); | |
| 475 } | |
| OLD | NEW |