| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/url_request/url_request_file_dir_job.h" | 5 #include "net/url_request/url_request_file_dir_job.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/message_loop/message_loop.h" | |
| 16 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 17 #include "base/test/scoped_task_scheduler.h" | 16 #include "base/task_scheduler/task_scheduler.h" |
| 18 #include "net/base/filename_util.h" | 17 #include "net/base/filename_util.h" |
| 19 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
| 20 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 21 #include "net/test/gtest_util.h" | 20 #include "net/test/gtest_util.h" |
| 22 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" | 21 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
| 23 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" |
| 24 #include "net/url_request/url_request_test_util.h" | 23 #include "net/url_request/url_request_test_util.h" |
| 25 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 26 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 bool got_response_started() const { return got_response_started_; } | 87 bool got_response_started() const { return got_response_started_; } |
| 89 | 88 |
| 90 private: | 89 private: |
| 91 bool got_response_started_ = false; | 90 bool got_response_started_ = false; |
| 92 | 91 |
| 93 DISALLOW_COPY_AND_ASSIGN(TestDirectoryURLRequestDelegate); | 92 DISALLOW_COPY_AND_ASSIGN(TestDirectoryURLRequestDelegate); |
| 94 }; | 93 }; |
| 95 | 94 |
| 96 class URLRequestFileDirTest : public testing::Test { | 95 class URLRequestFileDirTest : public testing::Test { |
| 97 public: | 96 public: |
| 98 URLRequestFileDirTest() | 97 URLRequestFileDirTest() : buffer_(new IOBuffer(kBufferSize)) {} |
| 99 : scoped_task_scheduler_(base::MessageLoop::current()), | |
| 100 buffer_(new IOBuffer(kBufferSize)) {} | |
| 101 | 98 |
| 102 protected: | 99 protected: |
| 103 base::test::ScopedTaskScheduler scoped_task_scheduler_; | |
| 104 TestURLRequestContext context_; | 100 TestURLRequestContext context_; |
| 105 TestDirectoryURLRequestDelegate delegate_; | 101 TestDirectoryURLRequestDelegate delegate_; |
| 106 scoped_refptr<IOBuffer> buffer_; | 102 scoped_refptr<IOBuffer> buffer_; |
| 107 }; | 103 }; |
| 108 | 104 |
| 109 TEST_F(URLRequestFileDirTest, ListCompletionOnNoPending) { | 105 TEST_F(URLRequestFileDirTest, ListCompletionOnNoPending) { |
| 110 base::ScopedTempDir directory; | 106 base::ScopedTempDir directory; |
| 111 // It is necessary to pass an existing directory to UrlRequest object, | 107 // It is necessary to pass an existing directory to UrlRequest object, |
| 112 // but it will be deleted for testing purpose after request is started. | 108 // but it will be deleted for testing purpose after request is started. |
| 113 ASSERT_TRUE(directory.CreateUniqueTempDir()); | 109 ASSERT_TRUE(directory.CreateUniqueTempDir()); |
| 114 TestJobFactory factory(directory.GetPath()); | 110 TestJobFactory factory(directory.GetPath()); |
| 115 context_.set_job_factory(&factory); | 111 context_.set_job_factory(&factory); |
| 116 std::unique_ptr<URLRequest> request(context_.CreateRequest( | 112 std::unique_ptr<URLRequest> request(context_.CreateRequest( |
| 117 FilePathToFileURL( | 113 FilePathToFileURL( |
| 118 directory.GetPath().AppendASCII("this_path_does_not_exist")), | 114 directory.GetPath().AppendASCII("this_path_does_not_exist")), |
| 119 DEFAULT_PRIORITY, &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS)); | 115 DEFAULT_PRIORITY, &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 120 | 116 |
| 121 request->Start(); | 117 request->Start(); |
| 122 ASSERT_TRUE(directory.Delete()); | 118 ASSERT_TRUE(directory.Delete()); |
| 123 | 119 |
| 124 // Since the DirectoryLister is running on the network thread, this | 120 // Since the DirectoryLister is running on the network thread, this |
| 125 // will spin the message loop until the read error is returned to the | 121 // will spin the message loop until the read error is returned to the |
| 126 // URLRequestFileDirJob. | 122 // URLRequestFileDirJob. |
| 127 base::RunLoop().RunUntilIdle(); | 123 base::RunLoop().RunUntilIdle(); |
| 124 base::TaskScheduler::GetInstance()->FlushForTesting(); |
| 125 base::RunLoop().RunUntilIdle(); |
| 128 ASSERT_TRUE(delegate_.got_response_started()); | 126 ASSERT_TRUE(delegate_.got_response_started()); |
| 129 | 127 |
| 130 int bytes_read = request->Read(buffer_.get(), kBufferSize); | 128 int bytes_read = request->Read(buffer_.get(), kBufferSize); |
| 131 | 129 |
| 132 // The URLRequestFileDirJobShould return the cached read error synchronously. | 130 // The URLRequestFileDirJobShould return the cached read error synchronously. |
| 133 // If it's not returned synchronously, the code path this is intended to test | 131 // If it's not returned synchronously, the code path this is intended to test |
| 134 // was not executed. | 132 // was not executed. |
| 135 EXPECT_THAT(bytes_read, IsError(ERR_FILE_NOT_FOUND)); | 133 EXPECT_THAT(bytes_read, IsError(ERR_FILE_NOT_FOUND)); |
| 136 } | 134 } |
| 137 | 135 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 148 std::unique_ptr<URLRequest> request( | 146 std::unique_ptr<URLRequest> request( |
| 149 context_.CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, | 147 context_.CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 150 &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS)); | 148 &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 151 request->Start(); | 149 request->Start(); |
| 152 EXPECT_TRUE(request->is_pending()); | 150 EXPECT_TRUE(request->is_pending()); |
| 153 | 151 |
| 154 // Since the DirectoryLister is running on the network thread, this will spin | 152 // Since the DirectoryLister is running on the network thread, this will spin |
| 155 // the message loop until the URLRequetsFileDirJob has received the | 153 // the message loop until the URLRequetsFileDirJob has received the |
| 156 // entire directory listing and cached it. | 154 // entire directory listing and cached it. |
| 157 base::RunLoop().RunUntilIdle(); | 155 base::RunLoop().RunUntilIdle(); |
| 156 base::TaskScheduler::GetInstance()->FlushForTesting(); |
| 157 base::RunLoop().RunUntilIdle(); |
| 158 | 158 |
| 159 // This will complete synchronously, since the URLRequetsFileDirJob had | 159 // This will complete synchronously, since the URLRequetsFileDirJob had |
| 160 // directory listing cached in memory. | 160 // directory listing cached in memory. |
| 161 int bytes_read = request->Read(buffer_.get(), kBufferSize); | 161 int bytes_read = request->Read(buffer_.get(), kBufferSize); |
| 162 | 162 |
| 163 ASSERT_GT(bytes_read, 0); | 163 ASSERT_GT(bytes_read, 0); |
| 164 ASSERT_LE(bytes_read, kBufferSize); | 164 ASSERT_LE(bytes_read, kBufferSize); |
| 165 std::string data(buffer_->data(), bytes_read); | 165 std::string data(buffer_->data(), bytes_read); |
| 166 EXPECT_TRUE(data.find(directory.GetPath().BaseName().MaybeAsASCII()) != | 166 EXPECT_TRUE(data.find(directory.GetPath().BaseName().MaybeAsASCII()) != |
| 167 std::string::npos); | 167 std::string::npos); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 ASSERT_GT(delegate.bytes_received(), 0); | 250 ASSERT_GT(delegate.bytes_received(), 0); |
| 251 ASSERT_LE(delegate.bytes_received(), kBufferSize); | 251 ASSERT_LE(delegate.bytes_received(), kBufferSize); |
| 252 EXPECT_TRUE(delegate.data_received().find( | 252 EXPECT_TRUE(delegate.data_received().find( |
| 253 directory.GetPath().BaseName().MaybeAsASCII()) != | 253 directory.GetPath().BaseName().MaybeAsASCII()) != |
| 254 std::string::npos); | 254 std::string::npos); |
| 255 } | 255 } |
| 256 | 256 |
| 257 } // namespace | 257 } // namespace |
| 258 | 258 |
| 259 } // namespace net | 259 } // namespace net |
| OLD | NEW |