| 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 "net/base/file_stream.h" | 5 #include "net/base/file_stream.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
| 16 #include "base/test/sequenced_worker_pool_owner.h" |
| 16 #include "base/test/test_timeouts.h" | 17 #include "base/test/test_timeouts.h" |
| 17 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
| 18 #include "base/threading/sequenced_worker_pool.h" | |
| 19 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
| 20 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
| 21 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
| 22 #include "net/base/test_completion_callback.h" | 22 #include "net/base/test_completion_callback.h" |
| 23 #include "net/log/test_net_log.h" | 23 #include "net/log/test_net_log.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "testing/platform_test.h" | 25 #include "testing/platform_test.h" |
| 26 | 26 |
| 27 #if defined(OS_ANDROID) | 27 #if defined(OS_ANDROID) |
| 28 #include "base/test/test_file_util.h" | 28 #include "base/test/test_file_util.h" |
| (...skipping 678 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 707 EXPECT_LT(0, total_bytes_written); | 707 EXPECT_LT(0, total_bytes_written); |
| 708 EXPECT_EQ(kTestDataSize, total_bytes_written); | 708 EXPECT_EQ(kTestDataSize, total_bytes_written); |
| 709 | 709 |
| 710 stream.reset(); | 710 stream.reset(); |
| 711 | 711 |
| 712 EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); | 712 EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); |
| 713 EXPECT_EQ(kTestDataSize * 2, file_size); | 713 EXPECT_EQ(kTestDataSize * 2, file_size); |
| 714 } | 714 } |
| 715 | 715 |
| 716 TEST_F(FileStreamTest, OpenAndDelete) { | 716 TEST_F(FileStreamTest, OpenAndDelete) { |
| 717 scoped_refptr<base::SequencedWorkerPool> pool( | 717 base::SequencedWorkerPoolOwner pool_owner(1, "StreamTest"); |
| 718 new base::SequencedWorkerPool(1, "StreamTest")); | |
| 719 | 718 |
| 720 bool prev = base::ThreadRestrictions::SetIOAllowed(false); | 719 bool prev = base::ThreadRestrictions::SetIOAllowed(false); |
| 721 scoped_ptr<FileStream> stream(new FileStream(pool.get())); | 720 scoped_ptr<FileStream> stream(new FileStream(pool_owner.pool())); |
| 722 int flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE | | 721 int flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE | |
| 723 base::File::FLAG_ASYNC; | 722 base::File::FLAG_ASYNC; |
| 724 TestCompletionCallback open_callback; | 723 TestCompletionCallback open_callback; |
| 725 int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); | 724 int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); |
| 726 EXPECT_EQ(ERR_IO_PENDING, rv); | 725 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 727 | 726 |
| 728 // Delete the stream without waiting for the open operation to be | 727 // Delete the stream without waiting for the open operation to be |
| 729 // complete. Should be safe. | 728 // complete. Should be safe. |
| 730 stream.reset(); | 729 stream.reset(); |
| 731 | 730 |
| 732 // Force an operation through the pool. | 731 // Force an operation through the pool. |
| 733 scoped_ptr<FileStream> stream2(new FileStream(pool.get())); | 732 scoped_ptr<FileStream> stream2(new FileStream(pool_owner.pool())); |
| 734 TestCompletionCallback open_callback2; | 733 TestCompletionCallback open_callback2; |
| 735 rv = stream2->Open(temp_file_path(), flags, open_callback2.callback()); | 734 rv = stream2->Open(temp_file_path(), flags, open_callback2.callback()); |
| 736 EXPECT_EQ(OK, open_callback2.GetResult(rv)); | 735 EXPECT_EQ(OK, open_callback2.GetResult(rv)); |
| 737 stream2.reset(); | 736 stream2.reset(); |
| 738 | 737 |
| 739 pool->Shutdown(); | |
| 740 | |
| 741 // open_callback won't be called. | 738 // open_callback won't be called. |
| 742 base::RunLoop().RunUntilIdle(); | 739 base::RunLoop().RunUntilIdle(); |
| 743 EXPECT_FALSE(open_callback.have_result()); | 740 EXPECT_FALSE(open_callback.have_result()); |
| 744 base::ThreadRestrictions::SetIOAllowed(prev); | 741 base::ThreadRestrictions::SetIOAllowed(prev); |
| 745 } | 742 } |
| 746 | 743 |
| 747 // Verify that Write() errors are mapped correctly. | 744 // Verify that Write() errors are mapped correctly. |
| 748 TEST_F(FileStreamTest, WriteError) { | 745 TEST_F(FileStreamTest, WriteError) { |
| 749 // Try opening file as read-only and then writing to it using FileStream. | 746 // Try opening file as read-only and then writing to it using FileStream. |
| 750 uint32_t flags = | 747 uint32_t flags = |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 833 total_bytes_read += rv; | 830 total_bytes_read += rv; |
| 834 data_read.append(buf->data(), rv); | 831 data_read.append(buf->data(), rv); |
| 835 } | 832 } |
| 836 EXPECT_EQ(file_size, total_bytes_read); | 833 EXPECT_EQ(file_size, total_bytes_read); |
| 837 } | 834 } |
| 838 #endif | 835 #endif |
| 839 | 836 |
| 840 } // namespace | 837 } // namespace |
| 841 | 838 |
| 842 } // namespace net | 839 } // namespace net |
| OLD | NEW |