Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1122)

Side by Side Diff: net/url_request/url_request_file_dir_job_unittest.cc

Issue 2839663002: Instantiate ScopedTaskEnvironment in net unittests. (Closed)
Patch Set: self-review Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/test/scoped_task_environment.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"
21 #include "net/test/net_test_suite.h"
22 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" 22 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
23 #include "net/url_request/url_request.h" 23 #include "net/url_request/url_request.h"
24 #include "net/url_request/url_request_test_util.h" 24 #include "net/url_request/url_request_test_util.h"
25 #include "testing/gmock/include/gmock/gmock.h" 25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
27 27
28 using net::test::IsError; 28 using net::test::IsError;
29 using net::test::IsOk; 29 using net::test::IsOk;
30 30
31 namespace net { 31 namespace net {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 bool got_response_started() const { return got_response_started_; } 88 bool got_response_started() const { return got_response_started_; }
89 89
90 private: 90 private:
91 bool got_response_started_ = false; 91 bool got_response_started_ = false;
92 92
93 DISALLOW_COPY_AND_ASSIGN(TestDirectoryURLRequestDelegate); 93 DISALLOW_COPY_AND_ASSIGN(TestDirectoryURLRequestDelegate);
94 }; 94 };
95 95
96 class URLRequestFileDirTest : public testing::Test { 96 class URLRequestFileDirTest : public testing::Test {
97 public: 97 public:
98 URLRequestFileDirTest() 98 URLRequestFileDirTest() : buffer_(new IOBuffer(kBufferSize)) {}
99 : scoped_task_scheduler_(base::MessageLoop::current()),
100 buffer_(new IOBuffer(kBufferSize)) {}
101 99
102 protected: 100 protected:
103 base::test::ScopedTaskScheduler scoped_task_scheduler_;
104 TestURLRequestContext context_; 101 TestURLRequestContext context_;
105 TestDirectoryURLRequestDelegate delegate_; 102 TestDirectoryURLRequestDelegate delegate_;
106 scoped_refptr<IOBuffer> buffer_; 103 scoped_refptr<IOBuffer> buffer_;
107 }; 104 };
108 105
109 TEST_F(URLRequestFileDirTest, ListCompletionOnNoPending) { 106 TEST_F(URLRequestFileDirTest, ListCompletionOnNoPending) {
110 base::ScopedTempDir directory; 107 base::ScopedTempDir directory;
111 // It is necessary to pass an existing directory to UrlRequest object, 108 // It is necessary to pass an existing directory to UrlRequest object,
112 // but it will be deleted for testing purpose after request is started. 109 // but it will be deleted for testing purpose after request is started.
113 ASSERT_TRUE(directory.CreateUniqueTempDir()); 110 ASSERT_TRUE(directory.CreateUniqueTempDir());
114 TestJobFactory factory(directory.GetPath()); 111 TestJobFactory factory(directory.GetPath());
115 context_.set_job_factory(&factory); 112 context_.set_job_factory(&factory);
116 std::unique_ptr<URLRequest> request(context_.CreateRequest( 113 std::unique_ptr<URLRequest> request(context_.CreateRequest(
117 FilePathToFileURL( 114 FilePathToFileURL(
118 directory.GetPath().AppendASCII("this_path_does_not_exist")), 115 directory.GetPath().AppendASCII("this_path_does_not_exist")),
119 DEFAULT_PRIORITY, &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS)); 116 DEFAULT_PRIORITY, &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS));
120 117
121 request->Start(); 118 request->Start();
122 ASSERT_TRUE(directory.Delete()); 119 ASSERT_TRUE(directory.Delete());
123 120
124 // Since the DirectoryLister is running on the network thread, this 121 // Since the DirectoryLister is running on the network thread, this
125 // will spin the message loop until the read error is returned to the 122 // will spin the message loop until the read error is returned to the
126 // URLRequestFileDirJob. 123 // URLRequestFileDirJob.
127 base::RunLoop().RunUntilIdle(); 124 NetTestSuite::GetScopedTaskEnvironment()->RunUntilIdle();
128 ASSERT_TRUE(delegate_.got_response_started()); 125 ASSERT_TRUE(delegate_.got_response_started());
129 126
130 int bytes_read = request->Read(buffer_.get(), kBufferSize); 127 int bytes_read = request->Read(buffer_.get(), kBufferSize);
131 128
132 // The URLRequestFileDirJobShould return the cached read error synchronously. 129 // The URLRequestFileDirJobShould return the cached read error synchronously.
133 // If it's not returned synchronously, the code path this is intended to test 130 // If it's not returned synchronously, the code path this is intended to test
134 // was not executed. 131 // was not executed.
135 EXPECT_THAT(bytes_read, IsError(ERR_FILE_NOT_FOUND)); 132 EXPECT_THAT(bytes_read, IsError(ERR_FILE_NOT_FOUND));
136 } 133 }
137 134
138 // Test the case where reading the response completes synchronously. 135 // Test the case where reading the response completes synchronously.
139 TEST_F(URLRequestFileDirTest, DirectoryWithASingleFileSync) { 136 TEST_F(URLRequestFileDirTest, DirectoryWithASingleFileSync) {
140 base::ScopedTempDir directory; 137 base::ScopedTempDir directory;
141 ASSERT_TRUE(directory.CreateUniqueTempDir()); 138 ASSERT_TRUE(directory.CreateUniqueTempDir());
142 base::FilePath path; 139 base::FilePath path;
143 base::CreateTemporaryFileInDir(directory.GetPath(), &path); 140 base::CreateTemporaryFileInDir(directory.GetPath(), &path);
144 141
145 TestJobFactory factory(directory.GetPath()); 142 TestJobFactory factory(directory.GetPath());
146 context_.set_job_factory(&factory); 143 context_.set_job_factory(&factory);
147 144
148 std::unique_ptr<URLRequest> request( 145 std::unique_ptr<URLRequest> request(
149 context_.CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, 146 context_.CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY,
150 &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS)); 147 &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS));
151 request->Start(); 148 request->Start();
152 EXPECT_TRUE(request->is_pending()); 149 EXPECT_TRUE(request->is_pending());
153 150
154 // Since the DirectoryLister is running on the network thread, this will spin 151 // Since the DirectoryLister is running on the network thread, this will spin
155 // the message loop until the URLRequetsFileDirJob has received the 152 // the message loop until the URLRequetsFileDirJob has received the
156 // entire directory listing and cached it. 153 // entire directory listing and cached it.
157 base::RunLoop().RunUntilIdle(); 154 NetTestSuite::GetScopedTaskEnvironment()->RunUntilIdle();
158 155
159 // This will complete synchronously, since the URLRequetsFileDirJob had 156 // This will complete synchronously, since the URLRequetsFileDirJob had
160 // directory listing cached in memory. 157 // directory listing cached in memory.
161 int bytes_read = request->Read(buffer_.get(), kBufferSize); 158 int bytes_read = request->Read(buffer_.get(), kBufferSize);
162 159
163 ASSERT_GT(bytes_read, 0); 160 ASSERT_GT(bytes_read, 0);
164 ASSERT_LE(bytes_read, kBufferSize); 161 ASSERT_LE(bytes_read, kBufferSize);
165 std::string data(buffer_->data(), bytes_read); 162 std::string data(buffer_->data(), bytes_read);
166 EXPECT_TRUE(data.find(directory.GetPath().BaseName().MaybeAsASCII()) != 163 EXPECT_TRUE(data.find(directory.GetPath().BaseName().MaybeAsASCII()) !=
167 std::string::npos); 164 std::string::npos);
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 ASSERT_GT(delegate.bytes_received(), 0); 247 ASSERT_GT(delegate.bytes_received(), 0);
251 ASSERT_LE(delegate.bytes_received(), kBufferSize); 248 ASSERT_LE(delegate.bytes_received(), kBufferSize);
252 EXPECT_TRUE(delegate.data_received().find( 249 EXPECT_TRUE(delegate.data_received().find(
253 directory.GetPath().BaseName().MaybeAsASCII()) != 250 directory.GetPath().BaseName().MaybeAsASCII()) !=
254 std::string::npos); 251 std::string::npos);
255 } 252 }
256 253
257 } // namespace 254 } // namespace
258 255
259 } // namespace net 256 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698