OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/chromeos/drive/drive_url_request_job.h" | 5 #include "chrome/browser/chromeos/drive/drive_url_request_job.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_util.h" | |
8 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
10 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
11 #include "base/sequenced_task_runner.h" | |
12 #include "base/threading/sequenced_worker_pool.h" | |
13 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
14 #include "chrome/browser/chromeos/drive/drive_file_stream_reader.h" | 13 #include "chrome/browser/chromeos/drive/drive_file_stream_reader.h" |
14 #include "chrome/browser/chromeos/drive/drive_integration_service.h" | |
15 #include "chrome/browser/chromeos/drive/fake_file_system.h" | 15 #include "chrome/browser/chromeos/drive/fake_file_system.h" |
16 #include "chrome/browser/chromeos/drive/file_system_util.h" | 16 #include "chrome/browser/chromeos/drive/file_system_util.h" |
17 #include "chrome/browser/chromeos/drive/test_util.h" | 17 #include "chrome/browser/chromeos/drive/test_util.h" |
18 #include "chrome/browser/drive/fake_drive_service.h" | 18 #include "chrome/browser/drive/fake_drive_service.h" |
19 #include "chrome/browser/drive/test_util.h" | 19 #include "chrome/browser/drive/test_util.h" |
20 #include "chrome/browser/prefs/browser_prefs.h" | |
21 #include "chrome/browser/prefs/pref_service_syncable.h" | |
22 #include "chrome/browser/profiles/profile_manager.h" | |
20 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
24 #include "chrome/test/base/testing_browser_process.h" | |
25 #include "chrome/test/base/testing_profile.h" | |
26 #include "components/pref_registry/pref_registry_syncable.h" | |
27 #include "components/pref_registry/testing_pref_service_syncable.h" | |
21 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
22 #include "content/public/test/test_browser_thread_bundle.h" | 29 #include "content/public/test/test_browser_thread_bundle.h" |
30 #include "content/public/test/test_file_system_options.h" | |
23 #include "google_apis/drive/test_util.h" | 31 #include "google_apis/drive/test_util.h" |
24 #include "net/base/request_priority.h" | 32 #include "net/base/request_priority.h" |
25 #include "net/base/test_completion_callback.h" | 33 #include "net/base/test_completion_callback.h" |
26 #include "net/http/http_byte_range.h" | 34 #include "net/http/http_byte_range.h" |
27 #include "net/url_request/redirect_info.h" | 35 #include "net/url_request/redirect_info.h" |
28 #include "net/url_request/url_request.h" | 36 #include "net/url_request/url_request.h" |
29 #include "net/url_request/url_request_context.h" | 37 #include "net/url_request/url_request_context.h" |
30 #include "net/url_request/url_request_test_util.h" | 38 #include "net/url_request/url_request_test_util.h" |
39 #include "storage/browser/fileapi/external_mount_points.h" | |
40 #include "storage/browser/fileapi/file_system_context.h" | |
31 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
32 #include "url/gurl.h" | 42 #include "url/gurl.h" |
33 | 43 |
34 namespace drive { | 44 namespace drive { |
35 namespace { | 45 namespace { |
46 class MockProfileManager : public ProfileManagerWithoutInit { | |
mtomasz
2014/09/19 05:30:53
Is this \n after namespace { and before closing co
hirono
2014/09/19 06:26:36
Done.
| |
47 public: | |
48 explicit MockProfileManager(const base::FilePath& user_data_dir) | |
mtomasz
2014/09/19 05:30:53
I think this is not a mock. It's rather some kind
hirono
2014/09/19 06:26:36
I removed the class as you suggested. Thank you!
| |
49 : ProfileManagerWithoutInit(user_data_dir) {} | |
50 | |
51 protected: | |
52 virtual Profile* CreateProfileHelper( | |
mtomasz
2014/09/19 05:30:53
Why is this needed? Do we need method? If so, then
hirono
2014/09/19 06:26:36
I removed the class as you suggested. Thank you!
| |
53 const base::FilePath& file_path) OVERRIDE { | |
54 if (!base::PathExists(file_path)) { | |
55 if (!base::CreateDirectory(file_path)) | |
56 return NULL; | |
57 } | |
58 return new TestingProfile(file_path); | |
59 } | |
60 }; | |
mtomasz
2014/09/19 05:30:53
In general, is this entire class needed? Isn't Tes
hirono
2014/09/19 06:26:36
I found the class is not needed. Removed.
| |
36 | 61 |
37 // A simple URLRequestJobFactory implementation to create DriveURLRequestJob. | 62 // A simple URLRequestJobFactory implementation to create DriveURLRequestJob. |
38 class TestURLRequestJobFactory : public net::URLRequestJobFactory { | 63 class TestURLRequestJobFactory : public net::URLRequestJobFactory { |
39 public: | 64 public: |
40 TestURLRequestJobFactory( | 65 explicit TestURLRequestJobFactory(void* profile_id) |
41 const DriveURLRequestJob::FileSystemGetter& file_system_getter, | 66 : profile_id_(profile_id) {} |
42 base::SequencedTaskRunner* sequenced_task_runner) | |
43 : file_system_getter_(file_system_getter), | |
44 sequenced_task_runner_(sequenced_task_runner) { | |
45 } | |
46 | 67 |
47 virtual ~TestURLRequestJobFactory() {} | 68 virtual ~TestURLRequestJobFactory() {} |
48 | 69 |
49 // net::URLRequestJobFactory override: | 70 // net::URLRequestJobFactory override: |
50 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( | 71 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( |
51 const std::string& scheme, | 72 const std::string& scheme, |
52 net::URLRequest* request, | 73 net::URLRequest* request, |
53 net::NetworkDelegate* network_delegate) const OVERRIDE { | 74 net::NetworkDelegate* network_delegate) const OVERRIDE { |
54 return new DriveURLRequestJob(file_system_getter_, | 75 return new DriveURLRequestJob(profile_id_, request, network_delegate); |
55 sequenced_task_runner_.get(), | |
56 request, | |
57 network_delegate); | |
58 } | 76 } |
59 | 77 |
60 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { | 78 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { |
61 return scheme == chrome::kDriveScheme; | 79 return scheme == chrome::kDriveScheme; |
62 } | 80 } |
63 | 81 |
64 virtual bool IsHandledURL(const GURL& url) const OVERRIDE { | 82 virtual bool IsHandledURL(const GURL& url) const OVERRIDE { |
65 return url.is_valid() && IsHandledProtocol(url.scheme()); | 83 return url.is_valid() && IsHandledProtocol(url.scheme()); |
66 } | 84 } |
67 | 85 |
68 virtual bool IsSafeRedirectTarget(const GURL& location) const OVERRIDE { | 86 virtual bool IsSafeRedirectTarget(const GURL& location) const OVERRIDE { |
69 return true; | 87 return true; |
70 } | 88 } |
71 | 89 |
72 private: | 90 private: |
73 const DriveURLRequestJob::FileSystemGetter file_system_getter_; | 91 void* const profile_id_; |
74 scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; | |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(TestURLRequestJobFactory); | 92 DISALLOW_COPY_AND_ASSIGN(TestURLRequestJobFactory); |
77 }; | 93 }; |
78 | 94 |
79 class TestDelegate : public net::TestDelegate { | 95 class TestDelegate : public net::TestDelegate { |
80 public: | 96 public: |
81 TestDelegate() {} | 97 TestDelegate() {} |
82 | 98 |
83 const GURL& redirect_url() const { return redirect_url_; } | 99 const GURL& redirect_url() const { return redirect_url_; } |
84 | 100 |
85 // net::TestDelegate override. | 101 // net::TestDelegate override. |
86 virtual void OnReceivedRedirect(net::URLRequest* request, | 102 virtual void OnReceivedRedirect(net::URLRequest* request, |
87 const net::RedirectInfo& redirect_info, | 103 const net::RedirectInfo& redirect_info, |
88 bool* defer_redirect) OVERRIDE{ | 104 bool* defer_redirect) OVERRIDE { |
89 redirect_url_ = redirect_info.new_url; | 105 redirect_url_ = redirect_info.new_url; |
90 net::TestDelegate::OnReceivedRedirect( | 106 net::TestDelegate::OnReceivedRedirect( |
91 request, redirect_info, defer_redirect); | 107 request, redirect_info, defer_redirect); |
92 } | 108 } |
93 | 109 |
94 private: | 110 private: |
95 GURL redirect_url_; | 111 GURL redirect_url_; |
96 | 112 |
97 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | 113 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
98 }; | 114 }; |
99 | 115 |
100 } // namespace | 116 } // namespace |
101 | 117 |
102 class DriveURLRequestJobTest : public testing::Test { | 118 class DriveURLRequestJobTest : public testing::Test { |
103 protected: | 119 protected: |
104 DriveURLRequestJobTest() | 120 DriveURLRequestJobTest() |
105 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | 121 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
106 } | 122 integration_service_factory_callback_( |
123 base::Bind(&DriveURLRequestJobTest::GetDriveIntegrationService, | |
124 base::Unretained(this))), | |
125 fake_file_system_(NULL) {} | |
107 | 126 |
108 virtual ~DriveURLRequestJobTest() { | 127 virtual ~DriveURLRequestJobTest() { |
109 } | 128 } |
110 | 129 |
111 virtual void SetUp() OVERRIDE { | 130 virtual void SetUp() OVERRIDE { |
112 // Initialize FakeDriveService. | 131 // Create a testing profile. |
113 fake_drive_service_.reset(new FakeDriveService); | 132 prefs_.reset(new TestingPrefServiceSimple); |
114 ASSERT_TRUE(test_util::SetUpTestEntries(fake_drive_service_.get())); | 133 chrome::RegisterLocalState(prefs_->registry()); |
134 TestingBrowserProcess::GetGlobal()->SetLocalState(prefs_.get()); | |
135 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
136 ProfileManager* const profile_manager = | |
137 new MockProfileManager(temp_dir_.path()); | |
138 TestingBrowserProcess::GetGlobal()->SetProfileManager(profile_manager); | |
139 Profile* const profile = | |
140 profile_manager->GetProfile(temp_dir_.path().Append("user")); | |
115 | 141 |
116 // Initialize FakeFileSystem. | 142 // Create the drive integration service for the profile. |
117 fake_file_system_.reset( | 143 integration_service_factory_scope_.reset( |
118 new test_util::FakeFileSystem(fake_drive_service_.get())); | 144 new DriveIntegrationServiceFactory::ScopedFactoryForTest( |
145 &integration_service_factory_callback_)); | |
146 DriveIntegrationServiceFactory::GetForProfile(profile); | |
119 | 147 |
120 scoped_refptr<base::SequencedWorkerPool> blocking_pool = | 148 // Create the URL request job factory. |
121 content::BrowserThread::GetBlockingPool(); | |
122 test_network_delegate_.reset(new net::TestNetworkDelegate); | 149 test_network_delegate_.reset(new net::TestNetworkDelegate); |
123 test_url_request_job_factory_.reset(new TestURLRequestJobFactory( | 150 test_url_request_job_factory_.reset(new TestURLRequestJobFactory(profile)); |
124 base::Bind(&DriveURLRequestJobTest::GetFileSystem, | |
125 base::Unretained(this)), | |
126 blocking_pool->GetSequencedTaskRunner( | |
127 blocking_pool->GetSequenceToken()).get())); | |
128 url_request_context_.reset(new net::URLRequestContext()); | 151 url_request_context_.reset(new net::URLRequestContext()); |
129 url_request_context_->set_job_factory(test_url_request_job_factory_.get()); | 152 url_request_context_->set_job_factory(test_url_request_job_factory_.get()); |
130 url_request_context_->set_network_delegate(test_network_delegate_.get()); | 153 url_request_context_->set_network_delegate(test_network_delegate_.get()); |
131 test_delegate_.reset(new TestDelegate); | 154 test_delegate_.reset(new TestDelegate); |
132 } | 155 } |
133 | 156 |
134 FileSystemInterface* GetFileSystem() { | 157 virtual void TearDown() { |
135 return fake_file_system_.get(); | 158 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); |
159 TestingBrowserProcess::GetGlobal()->SetLocalState(NULL); | |
136 } | 160 } |
137 | 161 |
138 bool ReadDriveFileSync( | 162 bool ReadDriveFileSync( |
139 const base::FilePath& file_path, std::string* out_content) { | 163 const base::FilePath& file_path, std::string* out_content) { |
140 scoped_ptr<base::Thread> worker_thread( | 164 scoped_ptr<base::Thread> worker_thread( |
141 new base::Thread("ReadDriveFileSync")); | 165 new base::Thread("ReadDriveFileSync")); |
142 if (!worker_thread->Start()) | 166 if (!worker_thread->Start()) |
143 return false; | 167 return false; |
144 | 168 |
145 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( | 169 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( |
(...skipping 21 matching lines...) Expand all Loading... | |
167 if (test_util::ReadAllData(reader.get(), &content) != net::OK) | 191 if (test_util::ReadAllData(reader.get(), &content) != net::OK) |
168 return false; | 192 return false; |
169 | 193 |
170 if (static_cast<size_t>(entry->file_info().size()) != content.size()) | 194 if (static_cast<size_t>(entry->file_info().size()) != content.size()) |
171 return false; | 195 return false; |
172 | 196 |
173 *out_content = content; | 197 *out_content = content; |
174 return true; | 198 return true; |
175 } | 199 } |
176 | 200 |
201 scoped_ptr<net::URLRequestContext> url_request_context_; | |
202 scoped_ptr<TestDelegate> test_delegate_; | |
203 | |
204 private: | |
205 // Create the drive integration service for the |profile| | |
206 DriveIntegrationService* GetDriveIntegrationService(Profile* profile) { | |
207 FakeDriveService* const drive_service = new FakeDriveService; | |
208 if (!test_util::SetUpTestEntries(drive_service)) | |
209 return NULL; | |
210 | |
211 const std::string& drive_mount_name = | |
212 util::GetDriveMountPointPath(profile).BaseName().AsUTF8Unsafe(); | |
213 storage::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( | |
214 drive_mount_name, | |
215 storage::kFileSystemTypeDrive, | |
216 storage::FileSystemMountOption(), | |
217 util::GetDriveMountPointPath(profile)); | |
218 DCHECK(!fake_file_system_); | |
219 fake_file_system_ = new test_util::FakeFileSystem(drive_service); | |
220 return new drive::DriveIntegrationService(profile, | |
221 NULL, | |
222 drive_service, | |
223 drive_mount_name, | |
224 temp_dir_.path().Append("cache"), | |
225 fake_file_system_); | |
226 } | |
227 | |
228 FileSystemInterface* GetFileSystem() { return fake_file_system_; } | |
229 | |
177 content::TestBrowserThreadBundle thread_bundle_; | 230 content::TestBrowserThreadBundle thread_bundle_; |
178 | 231 DriveIntegrationServiceFactory::FactoryCallback |
179 scoped_ptr<FakeDriveService> fake_drive_service_; | 232 integration_service_factory_callback_; |
180 scoped_ptr<test_util::FakeFileSystem> fake_file_system_; | 233 scoped_ptr<DriveIntegrationServiceFactory::ScopedFactoryForTest> |
234 integration_service_factory_scope_; | |
235 scoped_ptr<DriveIntegrationService> integration_service_; | |
236 test_util::FakeFileSystem* fake_file_system_; | |
181 | 237 |
182 scoped_ptr<net::TestNetworkDelegate> test_network_delegate_; | 238 scoped_ptr<net::TestNetworkDelegate> test_network_delegate_; |
183 scoped_ptr<TestURLRequestJobFactory> test_url_request_job_factory_; | 239 scoped_ptr<TestURLRequestJobFactory> test_url_request_job_factory_; |
184 scoped_ptr<net::URLRequestContext> url_request_context_; | 240 |
185 scoped_ptr<TestDelegate> test_delegate_; | 241 base::ScopedTempDir temp_dir_; |
242 scoped_ptr<TestingPrefServiceSimple> prefs_; | |
243 scoped_refptr<storage::FileSystemContext> file_system_context_; | |
186 }; | 244 }; |
187 | 245 |
188 TEST_F(DriveURLRequestJobTest, NonGetMethod) { | 246 TEST_F(DriveURLRequestJobTest, NonGetMethod) { |
189 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( | 247 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
190 GURL("drive:drive/root/File 1.txt"), | 248 GURL("drive:drive/root/File 1.txt"), |
191 net::DEFAULT_PRIORITY, | 249 net::DEFAULT_PRIORITY, |
192 test_delegate_.get(), | 250 test_delegate_.get(), |
193 NULL)); | 251 NULL)); |
194 request->set_method("POST"); // Set non "GET" method. | 252 request->set_method("POST"); // Set non "GET" method. |
195 request->Start(); | 253 request->Start(); |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
383 "Range", "Wrong Range Header Value", false /* overwrite */); | 441 "Range", "Wrong Range Header Value", false /* overwrite */); |
384 request->Start(); | 442 request->Start(); |
385 | 443 |
386 base::RunLoop().Run(); | 444 base::RunLoop().Run(); |
387 | 445 |
388 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); | 446 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
389 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, request->status().error()); | 447 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, request->status().error()); |
390 } | 448 } |
391 | 449 |
392 } // namespace drive | 450 } // namespace drive |
OLD | NEW |