Don't trigger HW acceleration from Toasts on low-end devices.
[chromium-blink-merge.git] / content / browser / byte_stream_unittest.cc
blobf750cf2cdfdcc619a78f392ae34223cab3d814dd
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.
5 #include "content/browser/byte_stream.h"
7 #include <deque>
8 #include <limits>
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/test/test_simple_task_runner.h"
14 #include "net/base/io_buffer.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace content {
18 namespace {
20 void CountCallbacks(int* counter) {
21 ++*counter;
24 } // namespace
26 class ByteStreamTest : public testing::Test {
27 public:
28 ByteStreamTest();
30 // Create a new IO buffer of the given |buffer_size|. Details of the
31 // contents of the created buffer will be kept, and can be validated
32 // by ValidateIOBuffer.
33 scoped_refptr<net::IOBuffer> NewIOBuffer(size_t buffer_size) {
34 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(buffer_size));
35 char *bufferp = buffer->data();
36 for (size_t i = 0; i < buffer_size; i++)
37 bufferp[i] = (i + producing_seed_key_) % (1 << sizeof(char));
38 pointer_queue_.push_back(bufferp);
39 length_queue_.push_back(buffer_size);
40 ++producing_seed_key_;
41 return buffer;
44 // Create an IOBuffer of the appropriate size and add it to the
45 // ByteStream, returning the result of the ByteStream::Write.
46 // Separate function to avoid duplication of buffer_size in test
47 // calls.
48 bool Write(ByteStreamWriter* byte_stream_input, size_t buffer_size) {
49 return byte_stream_input->Write(NewIOBuffer(buffer_size), buffer_size);
52 // Validate that we have the IOBuffer we expect. This routine must be
53 // called on buffers that were allocated from NewIOBuffer, and in the
54 // order that they were allocated. Calls to NewIOBuffer &&
55 // ValidateIOBuffer may be interleaved.
56 bool ValidateIOBuffer(
57 scoped_refptr<net::IOBuffer> buffer, size_t buffer_size) {
58 char *bufferp = buffer->data();
60 char *expected_ptr = pointer_queue_.front();
61 size_t expected_length = length_queue_.front();
62 pointer_queue_.pop_front();
63 length_queue_.pop_front();
64 ++consuming_seed_key_;
66 EXPECT_EQ(expected_ptr, bufferp);
67 if (expected_ptr != bufferp)
68 return false;
70 EXPECT_EQ(expected_length, buffer_size);
71 if (expected_length != buffer_size)
72 return false;
74 for (size_t i = 0; i < buffer_size; i++) {
75 // Already incremented, so subtract one from the key.
76 EXPECT_EQ(static_cast<int>((i + consuming_seed_key_ - 1)
77 % (1 << sizeof(char))),
78 bufferp[i]);
79 if (static_cast<int>((i + consuming_seed_key_ - 1) %
80 (1 << sizeof(char))) != bufferp[i]) {
81 return false;
84 return true;
87 protected:
88 base::MessageLoop message_loop_;
90 private:
91 int producing_seed_key_;
92 int consuming_seed_key_;
93 std::deque<char*> pointer_queue_;
94 std::deque<size_t> length_queue_;
97 ByteStreamTest::ByteStreamTest()
98 : producing_seed_key_(0),
99 consuming_seed_key_(0) { }
101 // Confirm that filling and emptying the stream works properly, and that
102 // we get full triggers when we expect.
103 TEST_F(ByteStreamTest, ByteStream_PushBack) {
104 scoped_ptr<ByteStreamWriter> byte_stream_input;
105 scoped_ptr<ByteStreamReader> byte_stream_output;
106 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
107 3 * 1024, &byte_stream_input, &byte_stream_output);
109 // Push a series of IO buffers on; test pushback happening and
110 // that it's advisory.
111 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
112 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
113 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
114 EXPECT_FALSE(Write(byte_stream_input.get(), 1));
115 EXPECT_FALSE(Write(byte_stream_input.get(), 1024));
116 // Flush
117 byte_stream_input->Close(0);
118 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes());
119 message_loop_.RunUntilIdle();
120 // Data already sent to reader is also counted in.
121 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes());
123 // Pull the IO buffers out; do we get the same buffers and do they
124 // have the same contents?
125 scoped_refptr<net::IOBuffer> output_io_buffer;
126 size_t output_length;
127 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
128 byte_stream_output->Read(&output_io_buffer, &output_length));
129 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
131 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
132 byte_stream_output->Read(&output_io_buffer, &output_length));
133 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
135 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
136 byte_stream_output->Read(&output_io_buffer, &output_length));
137 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
139 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
140 byte_stream_output->Read(&output_io_buffer, &output_length));
141 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
143 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
144 byte_stream_output->Read(&output_io_buffer, &output_length));
145 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
147 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
148 byte_stream_output->Read(&output_io_buffer, &output_length));
150 message_loop_.RunUntilIdle();
151 // Reader now knows that all data is read out.
152 EXPECT_EQ(1024U, byte_stream_input->GetTotalBufferedBytes());
155 // Confirm that Flush() method makes the writer to send written contents to
156 // the reader.
157 TEST_F(ByteStreamTest, ByteStream_Flush) {
158 scoped_ptr<ByteStreamWriter> byte_stream_input;
159 scoped_ptr<ByteStreamReader> byte_stream_output;
160 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
161 1024, &byte_stream_input, &byte_stream_output);
163 EXPECT_TRUE(Write(byte_stream_input.get(), 1));
164 message_loop_.RunUntilIdle();
166 scoped_refptr<net::IOBuffer> output_io_buffer;
167 size_t output_length = 0;
168 // Check that data is not sent to the reader yet.
169 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
170 byte_stream_output->Read(&output_io_buffer, &output_length));
172 byte_stream_input->Flush();
173 message_loop_.RunUntilIdle();
175 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
176 byte_stream_output->Read(&output_io_buffer, &output_length));
177 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
179 // Check that it's ok to Flush() an empty writer.
180 byte_stream_input->Flush();
181 message_loop_.RunUntilIdle();
183 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
184 byte_stream_output->Read(&output_io_buffer, &output_length));
186 byte_stream_input->Close(0);
187 message_loop_.RunUntilIdle();
189 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
190 byte_stream_output->Read(&output_io_buffer, &output_length));
193 // Same as above, only use knowledge of the internals to confirm
194 // that we're getting pushback even when data's split across the two
195 // objects
196 TEST_F(ByteStreamTest, ByteStream_PushBackSplit) {
197 scoped_ptr<ByteStreamWriter> byte_stream_input;
198 scoped_ptr<ByteStreamReader> byte_stream_output;
199 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
200 9 * 1024, &byte_stream_input, &byte_stream_output);
202 // Push a series of IO buffers on; test pushback happening and
203 // that it's advisory.
204 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
205 message_loop_.RunUntilIdle();
206 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
207 message_loop_.RunUntilIdle();
208 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
209 message_loop_.RunUntilIdle();
210 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
211 message_loop_.RunUntilIdle();
212 EXPECT_FALSE(Write(byte_stream_input.get(), 6 * 1024));
213 message_loop_.RunUntilIdle();
215 // Pull the IO buffers out; do we get the same buffers and do they
216 // have the same contents?
217 scoped_refptr<net::IOBuffer> output_io_buffer;
218 size_t output_length;
219 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
220 byte_stream_output->Read(&output_io_buffer, &output_length));
221 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
223 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
224 byte_stream_output->Read(&output_io_buffer, &output_length));
225 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
227 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
228 byte_stream_output->Read(&output_io_buffer, &output_length));
229 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
231 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
232 byte_stream_output->Read(&output_io_buffer, &output_length));
233 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
235 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
236 byte_stream_output->Read(&output_io_buffer, &output_length));
237 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
239 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
240 byte_stream_output->Read(&output_io_buffer, &output_length));
243 // Confirm that a Close() notification transmits in-order
244 // with data on the stream.
245 TEST_F(ByteStreamTest, ByteStream_CompleteTransmits) {
246 scoped_ptr<ByteStreamWriter> byte_stream_input;
247 scoped_ptr<ByteStreamReader> byte_stream_output;
249 scoped_refptr<net::IOBuffer> output_io_buffer;
250 size_t output_length;
252 // Empty stream, non-error case.
253 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
254 3 * 1024, &byte_stream_input, &byte_stream_output);
255 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
256 byte_stream_output->Read(&output_io_buffer, &output_length));
257 byte_stream_input->Close(0);
258 message_loop_.RunUntilIdle();
259 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
260 byte_stream_output->Read(&output_io_buffer, &output_length));
261 EXPECT_EQ(0, byte_stream_output->GetStatus());
263 // Non-empty stream, non-error case.
264 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
265 3 * 1024, &byte_stream_input, &byte_stream_output);
266 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
267 byte_stream_output->Read(&output_io_buffer, &output_length));
268 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
269 byte_stream_input->Close(0);
270 message_loop_.RunUntilIdle();
271 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
272 byte_stream_output->Read(&output_io_buffer, &output_length));
273 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
274 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
275 byte_stream_output->Read(&output_io_buffer, &output_length));
276 EXPECT_EQ(0, byte_stream_output->GetStatus());
278 const int kFakeErrorCode = 22;
280 // Empty stream, error case.
281 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
282 3 * 1024, &byte_stream_input, &byte_stream_output);
283 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
284 byte_stream_output->Read(&output_io_buffer, &output_length));
285 byte_stream_input->Close(kFakeErrorCode);
286 message_loop_.RunUntilIdle();
287 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
288 byte_stream_output->Read(&output_io_buffer, &output_length));
289 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
291 // Non-empty stream, error case.
292 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
293 3 * 1024, &byte_stream_input, &byte_stream_output);
294 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
295 byte_stream_output->Read(&output_io_buffer, &output_length));
296 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
297 byte_stream_input->Close(kFakeErrorCode);
298 message_loop_.RunUntilIdle();
299 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
300 byte_stream_output->Read(&output_io_buffer, &output_length));
301 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
302 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
303 byte_stream_output->Read(&output_io_buffer, &output_length));
304 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
307 // Confirm that callbacks on the sink side are triggered when they should be.
308 TEST_F(ByteStreamTest, ByteStream_SinkCallback) {
309 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
310 new base::TestSimpleTaskRunner());
312 scoped_ptr<ByteStreamWriter> byte_stream_input;
313 scoped_ptr<ByteStreamReader> byte_stream_output;
314 CreateByteStream(message_loop_.task_runner(), task_runner, 10000,
315 &byte_stream_input, &byte_stream_output);
317 scoped_refptr<net::IOBuffer> output_io_buffer;
318 size_t output_length;
320 // Note that the specifics of when the callbacks are called with regard
321 // to how much data is pushed onto the stream is not (currently) part
322 // of the interface contract. If it becomes part of the contract, the
323 // tests below should get much more precise.
325 // Confirm callback called when you add more than 33% of the buffer.
327 // Setup callback
328 int num_callbacks = 0;
329 byte_stream_output->RegisterCallback(
330 base::Bind(CountCallbacks, &num_callbacks));
332 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
333 message_loop_.RunUntilIdle();
335 EXPECT_EQ(0, num_callbacks);
336 task_runner->RunUntilIdle();
337 EXPECT_EQ(1, num_callbacks);
339 // Check data and stream state.
340 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
341 byte_stream_output->Read(&output_io_buffer, &output_length));
342 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
343 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
344 byte_stream_output->Read(&output_io_buffer, &output_length));
346 // Confirm callback *isn't* called at less than 33% (by lack of
347 // unexpected call on task runner).
348 EXPECT_TRUE(Write(byte_stream_input.get(), 3000));
349 message_loop_.RunUntilIdle();
351 // This reflects an implementation artifact that data goes with callbacks,
352 // which should not be considered part of the interface guarantee.
353 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
354 byte_stream_output->Read(&output_io_buffer, &output_length));
357 // Confirm that callbacks on the source side are triggered when they should
358 // be.
359 TEST_F(ByteStreamTest, ByteStream_SourceCallback) {
360 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
361 new base::TestSimpleTaskRunner());
363 scoped_ptr<ByteStreamWriter> byte_stream_input;
364 scoped_ptr<ByteStreamReader> byte_stream_output;
365 CreateByteStream(task_runner, message_loop_.task_runner(), 10000,
366 &byte_stream_input, &byte_stream_output);
368 scoped_refptr<net::IOBuffer> output_io_buffer;
369 size_t output_length;
371 // Note that the specifics of when the callbacks are called with regard
372 // to how much data is pulled from the stream is not (currently) part
373 // of the interface contract. If it becomes part of the contract, the
374 // tests below should get much more precise.
376 // Confirm callback called when about 33% space available, and not
377 // at other transitions.
379 // Add data.
380 int num_callbacks = 0;
381 byte_stream_input->RegisterCallback(
382 base::Bind(CountCallbacks, &num_callbacks));
383 EXPECT_TRUE(Write(byte_stream_input.get(), 2000));
384 EXPECT_TRUE(Write(byte_stream_input.get(), 2001));
385 EXPECT_FALSE(Write(byte_stream_input.get(), 6000));
387 // Allow bytes to transition (needed for message passing implementation),
388 // and get and validate the data.
389 message_loop_.RunUntilIdle();
390 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
391 byte_stream_output->Read(&output_io_buffer, &output_length));
392 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
394 // Grab data, triggering callback. Recorded on dispatch, but doesn't
395 // happen because it's caught by the mock.
396 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
397 byte_stream_output->Read(&output_io_buffer, &output_length));
398 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
400 // Confirm that the callback passed to the mock does what we expect.
401 EXPECT_EQ(0, num_callbacks);
402 task_runner->RunUntilIdle();
403 EXPECT_EQ(1, num_callbacks);
405 // Same drill with final buffer.
406 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
407 byte_stream_output->Read(&output_io_buffer, &output_length));
408 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
409 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
410 byte_stream_output->Read(&output_io_buffer, &output_length));
411 EXPECT_EQ(1, num_callbacks);
412 task_runner->RunUntilIdle();
413 // Should have updated the internal structures but not called the
414 // callback.
415 EXPECT_EQ(1, num_callbacks);
418 // Confirm that racing a change to a sink callback with a post results
419 // in the new callback being called.
420 TEST_F(ByteStreamTest, ByteStream_SinkInterrupt) {
421 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
422 new base::TestSimpleTaskRunner());
424 scoped_ptr<ByteStreamWriter> byte_stream_input;
425 scoped_ptr<ByteStreamReader> byte_stream_output;
426 CreateByteStream(message_loop_.task_runner(), task_runner, 10000,
427 &byte_stream_input, &byte_stream_output);
429 scoped_refptr<net::IOBuffer> output_io_buffer;
430 size_t output_length;
431 base::Closure intermediate_callback;
433 // Record initial state.
434 int num_callbacks = 0;
435 byte_stream_output->RegisterCallback(
436 base::Bind(CountCallbacks, &num_callbacks));
438 // Add data, and pass it across.
439 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
440 message_loop_.RunUntilIdle();
442 // The task runner should have been hit, but the callback count
443 // isn't changed until we actually run the callback.
444 EXPECT_EQ(0, num_callbacks);
446 // If we change the callback now, the new one should be run
447 // (simulates race with post task).
448 int num_alt_callbacks = 0;
449 byte_stream_output->RegisterCallback(
450 base::Bind(CountCallbacks, &num_alt_callbacks));
451 task_runner->RunUntilIdle();
452 EXPECT_EQ(0, num_callbacks);
453 EXPECT_EQ(1, num_alt_callbacks);
455 // Final cleanup.
456 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
457 byte_stream_output->Read(&output_io_buffer, &output_length));
458 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
459 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
460 byte_stream_output->Read(&output_io_buffer, &output_length));
464 // Confirm that racing a change to a source callback with a post results
465 // in the new callback being called.
466 TEST_F(ByteStreamTest, ByteStream_SourceInterrupt) {
467 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
468 new base::TestSimpleTaskRunner());
470 scoped_ptr<ByteStreamWriter> byte_stream_input;
471 scoped_ptr<ByteStreamReader> byte_stream_output;
472 CreateByteStream(task_runner, message_loop_.task_runner(), 10000,
473 &byte_stream_input, &byte_stream_output);
475 scoped_refptr<net::IOBuffer> output_io_buffer;
476 size_t output_length;
477 base::Closure intermediate_callback;
479 // Setup state for test.
480 int num_callbacks = 0;
481 byte_stream_input->RegisterCallback(
482 base::Bind(CountCallbacks, &num_callbacks));
483 EXPECT_TRUE(Write(byte_stream_input.get(), 2000));
484 EXPECT_TRUE(Write(byte_stream_input.get(), 2001));
485 EXPECT_FALSE(Write(byte_stream_input.get(), 6000));
486 message_loop_.RunUntilIdle();
488 // Initial get should not trigger callback.
489 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
490 byte_stream_output->Read(&output_io_buffer, &output_length));
491 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
492 message_loop_.RunUntilIdle();
494 // Second get *should* trigger callback.
495 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
496 byte_stream_output->Read(&output_io_buffer, &output_length));
497 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
499 // Which should do the right thing when it's run.
500 int num_alt_callbacks = 0;
501 byte_stream_input->RegisterCallback(
502 base::Bind(CountCallbacks, &num_alt_callbacks));
503 task_runner->RunUntilIdle();
504 EXPECT_EQ(0, num_callbacks);
505 EXPECT_EQ(1, num_alt_callbacks);
507 // Third get should also trigger callback.
508 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
509 byte_stream_output->Read(&output_io_buffer, &output_length));
510 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
511 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
512 byte_stream_output->Read(&output_io_buffer, &output_length));
515 // Confirm that callback is called on zero data transfer but source
516 // complete.
517 TEST_F(ByteStreamTest, ByteStream_ZeroCallback) {
518 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
519 new base::TestSimpleTaskRunner());
521 scoped_ptr<ByteStreamWriter> byte_stream_input;
522 scoped_ptr<ByteStreamReader> byte_stream_output;
523 CreateByteStream(message_loop_.task_runner(), task_runner, 10000,
524 &byte_stream_input, &byte_stream_output);
526 base::Closure intermediate_callback;
528 // Record initial state.
529 int num_callbacks = 0;
530 byte_stream_output->RegisterCallback(
531 base::Bind(CountCallbacks, &num_callbacks));
533 // Immediately close the stream.
534 byte_stream_input->Close(0);
535 task_runner->RunUntilIdle();
536 EXPECT_EQ(1, num_callbacks);
539 TEST_F(ByteStreamTest, ByteStream_CloseWithoutAnyWrite) {
540 scoped_ptr<ByteStreamWriter> byte_stream_input;
541 scoped_ptr<ByteStreamReader> byte_stream_output;
542 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
543 3 * 1024, &byte_stream_input, &byte_stream_output);
545 byte_stream_input->Close(0);
546 message_loop_.RunUntilIdle();
548 scoped_refptr<net::IOBuffer> output_io_buffer;
549 size_t output_length;
550 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
551 byte_stream_output->Read(&output_io_buffer, &output_length));
554 TEST_F(ByteStreamTest, ByteStream_FlushWithoutAnyWrite) {
555 scoped_ptr<ByteStreamWriter> byte_stream_input;
556 scoped_ptr<ByteStreamReader> byte_stream_output;
557 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
558 3 * 1024, &byte_stream_input, &byte_stream_output);
560 byte_stream_input->Flush();
561 message_loop_.RunUntilIdle();
563 scoped_refptr<net::IOBuffer> output_io_buffer;
564 size_t output_length;
565 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
566 byte_stream_output->Read(&output_io_buffer, &output_length));
568 byte_stream_input->Close(0);
569 message_loop_.RunUntilIdle();
571 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
572 byte_stream_output->Read(&output_io_buffer, &output_length));
575 TEST_F(ByteStreamTest, ByteStream_WriteOverflow) {
576 scoped_ptr<ByteStreamWriter> byte_stream_input;
577 scoped_ptr<ByteStreamReader> byte_stream_output;
578 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
579 std::numeric_limits<size_t>::max(), &byte_stream_input,
580 &byte_stream_output);
582 EXPECT_TRUE(Write(byte_stream_input.get(), 1));
583 // 1 + size_t max -> Overflow.
584 scoped_refptr<net::IOBuffer> empty_io_buffer;
585 EXPECT_FALSE(byte_stream_input->Write(empty_io_buffer,
586 std::numeric_limits<size_t>::max()));
587 message_loop_.RunUntilIdle();
589 // The first write is below PostToPeer threshold. We shouldn't get anything
590 // from the output.
591 scoped_refptr<net::IOBuffer> output_io_buffer;
592 size_t output_length;
593 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
594 byte_stream_output->Read(&output_io_buffer, &output_length));
597 } // namespace content