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

Side by Side Diff: chrome/browser/chromeos/drive/drive_url_request_job_unittest.cc

Issue 14755002: drive: Drop "Drive" from FakeDriveFileSystem and MockDriveFileSystem (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: some more cleanup Created 7 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 | Annotate | Revision Log
OLDNEW
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/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "chrome/browser/chromeos/drive/drive_file_stream_reader.h" 9 #include "chrome/browser/chromeos/drive/drive_file_stream_reader.h"
10 #include "chrome/browser/chromeos/drive/fake_drive_file_system.h" 10 #include "chrome/browser/chromeos/drive/fake_file_system.h"
11 #include "chrome/browser/chromeos/drive/file_system_util.h" 11 #include "chrome/browser/chromeos/drive/file_system_util.h"
12 #include "chrome/browser/google_apis/fake_drive_service.h" 12 #include "chrome/browser/google_apis/fake_drive_service.h"
13 #include "chrome/browser/google_apis/task_util.h" 13 #include "chrome/browser/google_apis/task_util.h"
14 #include "chrome/browser/google_apis/test_util.h" 14 #include "chrome/browser/google_apis/test_util.h"
15 #include "chrome/common/url_constants.h" 15 #include "chrome/common/url_constants.h"
16 #include "content/public/test/test_browser_thread.h" 16 #include "content/public/test/test_browser_thread.h"
17 #include "googleurl/src/gurl.h" 17 #include "googleurl/src/gurl.h"
18 #include "net/base/test_completion_callback.h" 18 #include "net/base/test_completion_callback.h"
19 #include "net/url_request/url_request_test_util.h" 19 #include "net/url_request/url_request_test_util.h"
20 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
21 21
22 using content::BrowserThread; 22 using content::BrowserThread;
23 23
24 namespace drive { 24 namespace drive {
25 namespace { 25 namespace {
26 26
27 // A simple URLRequestJobFactory implementation to create DriveURLReuqestJob. 27 // A simple URLRequestJobFactory implementation to create DriveURLReuqestJob.
28 class TestURLRequestJobFactory : public net::URLRequestJobFactory { 28 class TestURLRequestJobFactory : public net::URLRequestJobFactory {
29 public: 29 public:
30 TestURLRequestJobFactory( 30 TestURLRequestJobFactory(
31 const DriveURLRequestJob::DriveFileSystemGetter& file_system_getter) 31 const DriveURLRequestJob::FileSystemGetter& file_system_getter)
32 : file_system_getter_(file_system_getter) { 32 : file_system_getter_(file_system_getter) {
33 } 33 }
34 34
35 virtual ~TestURLRequestJobFactory() {} 35 virtual ~TestURLRequestJobFactory() {}
36 36
37 // net::URLRequestJobFactory override: 37 // net::URLRequestJobFactory override:
38 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( 38 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler(
39 const std::string& scheme, 39 const std::string& scheme,
40 net::URLRequest* request, 40 net::URLRequest* request,
41 net::NetworkDelegate* network_delegate) const OVERRIDE { 41 net::NetworkDelegate* network_delegate) const OVERRIDE {
42 return new DriveURLRequestJob(file_system_getter_, 42 return new DriveURLRequestJob(file_system_getter_,
43 request, 43 request,
44 network_delegate); 44 network_delegate);
45 } 45 }
46 46
47 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { 47 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE {
48 return scheme == chrome::kDriveScheme; 48 return scheme == chrome::kDriveScheme;
49 } 49 }
50 50
51 virtual bool IsHandledURL(const GURL& url) const OVERRIDE { 51 virtual bool IsHandledURL(const GURL& url) const OVERRIDE {
52 return url.is_valid() && IsHandledProtocol(url.scheme()); 52 return url.is_valid() && IsHandledProtocol(url.scheme());
53 } 53 }
54 54
55 private: 55 private:
56 const DriveURLRequestJob::DriveFileSystemGetter file_system_getter_; 56 const DriveURLRequestJob::FileSystemGetter file_system_getter_;
57 57
58 DISALLOW_COPY_AND_ASSIGN(TestURLRequestJobFactory); 58 DISALLOW_COPY_AND_ASSIGN(TestURLRequestJobFactory);
59 }; 59 };
60 60
61 class TestDelegate : public net::TestDelegate { 61 class TestDelegate : public net::TestDelegate {
62 public: 62 public:
63 TestDelegate() {} 63 TestDelegate() {}
64 64
65 const GURL& redirect_url() const { return redirect_url_; } 65 const GURL& redirect_url() const { return redirect_url_; }
66 66
(...skipping 28 matching lines...) Expand all
95 BrowserThread::PostTaskAndReply( 95 BrowserThread::PostTaskAndReply(
96 BrowserThread::UI, 96 BrowserThread::UI,
97 FROM_HERE, 97 FROM_HERE,
98 base::Bind(&DriveURLRequestJobTest::SetUpOnUIThread, 98 base::Bind(&DriveURLRequestJobTest::SetUpOnUIThread,
99 base::Unretained(this)), 99 base::Unretained(this)),
100 base::MessageLoop::QuitClosure()); 100 base::MessageLoop::QuitClosure());
101 message_loop_.Run(); 101 message_loop_.Run();
102 102
103 test_network_delegate_.reset(new net::TestNetworkDelegate); 103 test_network_delegate_.reset(new net::TestNetworkDelegate);
104 test_url_request_job_factory_.reset(new TestURLRequestJobFactory( 104 test_url_request_job_factory_.reset(new TestURLRequestJobFactory(
105 base::Bind(&DriveURLRequestJobTest::GetDriveFileSystem, 105 base::Bind(&DriveURLRequestJobTest::GetFileSystem,
106 base::Unretained(this)))); 106 base::Unretained(this))));
107 url_request_context_.reset(new net::URLRequestContext()); 107 url_request_context_.reset(new net::URLRequestContext());
108 url_request_context_->set_job_factory(test_url_request_job_factory_.get()); 108 url_request_context_->set_job_factory(test_url_request_job_factory_.get());
109 url_request_context_->set_network_delegate(test_network_delegate_.get()); 109 url_request_context_->set_network_delegate(test_network_delegate_.get());
110 test_delegate_.reset(new TestDelegate); 110 test_delegate_.reset(new TestDelegate);
111 } 111 }
112 112
113 virtual void TearDown() OVERRIDE { 113 virtual void TearDown() OVERRIDE {
114 test_delegate_.reset(); 114 test_delegate_.reset();
115 url_request_context_.reset(); 115 url_request_context_.reset();
(...skipping 12 matching lines...) Expand all
128 void SetUpOnUIThread() { 128 void SetUpOnUIThread() {
129 // Initialize FakeDriveService. 129 // Initialize FakeDriveService.
130 fake_drive_service_.reset(new google_apis::FakeDriveService); 130 fake_drive_service_.reset(new google_apis::FakeDriveService);
131 ASSERT_TRUE(fake_drive_service_->LoadResourceListForWapi( 131 ASSERT_TRUE(fake_drive_service_->LoadResourceListForWapi(
132 "chromeos/gdata/root_feed.json")); 132 "chromeos/gdata/root_feed.json"));
133 ASSERT_TRUE(fake_drive_service_->LoadAccountMetadataForWapi( 133 ASSERT_TRUE(fake_drive_service_->LoadAccountMetadataForWapi(
134 "chromeos/gdata/account_metadata.json")); 134 "chromeos/gdata/account_metadata.json"));
135 ASSERT_TRUE(fake_drive_service_->LoadAppListForDriveApi( 135 ASSERT_TRUE(fake_drive_service_->LoadAppListForDriveApi(
136 "chromeos/drive/applist.json")); 136 "chromeos/drive/applist.json"));
137 137
138 // Initialize FakeDriveFileSystem. 138 // Initialize FakeFileSystem.
139 fake_drive_file_system_.reset( 139 fake_file_system_.reset(
140 new test_util::FakeDriveFileSystem(fake_drive_service_.get())); 140 new test_util::FakeFileSystem(fake_drive_service_.get()));
141 ASSERT_TRUE(fake_drive_file_system_->InitializeForTesting()); 141 ASSERT_TRUE(fake_file_system_->InitializeForTesting());
142 } 142 }
143 143
144 void TearDownOnUIThread() { 144 void TearDownOnUIThread() {
145 fake_drive_file_system_.reset(); 145 fake_file_system_.reset();
146 fake_drive_service_.reset(); 146 fake_drive_service_.reset();
147 } 147 }
148 148
149 DriveFileSystemInterface* GetDriveFileSystem() { 149 DriveFileSystemInterface* GetFileSystem() {
150 return fake_drive_file_system_.get(); 150 return fake_file_system_.get();
151 } 151 }
152 152
153 std::string ReadDriveFileSync(const base::FilePath& file_path) { 153 std::string ReadDriveFileSync(const base::FilePath& file_path) {
154 net::TestCompletionCallback callback; 154 net::TestCompletionCallback callback;
155 const int kBufferSize = 100; 155 const int kBufferSize = 100;
156 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); 156 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize));
157 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( 157 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader(
158 base::Bind(&DriveURLRequestJobTest::GetDriveFileSystem, 158 base::Bind(&DriveURLRequestJobTest::GetFileSystem,
159 base::Unretained(this)))); 159 base::Unretained(this))));
160 160
161 FileError error = FILE_ERROR_FAILED; 161 FileError error = FILE_ERROR_FAILED;
162 scoped_ptr<DriveEntryProto> entry; 162 scoped_ptr<DriveEntryProto> entry;
163 reader->Initialize( 163 reader->Initialize(
164 file_path, 164 file_path,
165 google_apis::CreateComposedCallback( 165 google_apis::CreateComposedCallback(
166 base::Bind(&google_apis::test_util::RunAndQuit), 166 base::Bind(&google_apis::test_util::RunAndQuit),
167 google_apis::test_util::CreateCopyResultCallback( 167 google_apis::test_util::CreateCopyResultCallback(
168 &error, &entry))); 168 &error, &entry)));
169 message_loop_.Run(); 169 message_loop_.Run();
170 170
171 // Read data from the reader. 171 // Read data from the reader.
172 size_t content_size = entry->file_info().size(); 172 size_t content_size = entry->file_info().size();
173 std::string content; 173 std::string content;
174 while (content.size() < content_size) { 174 while (content.size() < content_size) {
175 int result = reader->Read(buffer.get(), kBufferSize, callback.callback()); 175 int result = reader->Read(buffer.get(), kBufferSize, callback.callback());
176 result = callback.GetResult(result); 176 result = callback.GetResult(result);
177 content.append(buffer->data(), result); 177 content.append(buffer->data(), result);
178 } 178 }
179 179
180 return content; 180 return content;
181 } 181 }
182 182
183 MessageLoopForIO message_loop_; 183 MessageLoopForIO message_loop_;
184 content::TestBrowserThread ui_thread_; 184 content::TestBrowserThread ui_thread_;
185 content::TestBrowserThread io_thread_; 185 content::TestBrowserThread io_thread_;
186 186
187 scoped_ptr<google_apis::FakeDriveService> fake_drive_service_; 187 scoped_ptr<google_apis::FakeDriveService> fake_drive_service_;
188 scoped_ptr<test_util::FakeDriveFileSystem> fake_drive_file_system_; 188 scoped_ptr<test_util::FakeFileSystem> fake_file_system_;
189 189
190 scoped_ptr<net::TestNetworkDelegate> test_network_delegate_; 190 scoped_ptr<net::TestNetworkDelegate> test_network_delegate_;
191 scoped_ptr<TestURLRequestJobFactory> test_url_request_job_factory_; 191 scoped_ptr<TestURLRequestJobFactory> test_url_request_job_factory_;
192 scoped_ptr<net::URLRequestContext> url_request_context_; 192 scoped_ptr<net::URLRequestContext> url_request_context_;
193 scoped_ptr<TestDelegate> test_delegate_; 193 scoped_ptr<TestDelegate> test_delegate_;
194 }; 194 };
195 195
196 TEST_F(DriveURLRequestJobTest, NonGetMethod) { 196 TEST_F(DriveURLRequestJobTest, NonGetMethod) {
197 net::URLRequest request( 197 net::URLRequest request(
198 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), 198 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(),
(...skipping 26 matching lines...) Expand all
225 std::string mime_type; 225 std::string mime_type;
226 request.GetMimeType(&mime_type); 226 request.GetMimeType(&mime_type);
227 EXPECT_EQ("audio/mpeg", mime_type); 227 EXPECT_EQ("audio/mpeg", mime_type);
228 // Reading file must be done after |request| runs, otherwise 228 // Reading file must be done after |request| runs, otherwise
229 // it'll create a local cache file, and we cannot test correctly. 229 // it'll create a local cache file, and we cannot test correctly.
230 EXPECT_EQ(ReadDriveFileSync(kTestFilePath), 230 EXPECT_EQ(ReadDriveFileSync(kTestFilePath),
231 test_delegate_->data_received()); 231 test_delegate_->data_received());
232 } 232 }
233 233
234 // For the second time, the locally cached file should be used. 234 // For the second time, the locally cached file should be used.
235 // The caching emulation is done by FakeDriveFileSystem. 235 // The caching emulation is done by FakeFileSystem.
236 { 236 {
237 test_delegate_.reset(new TestDelegate); 237 test_delegate_.reset(new TestDelegate);
238 net::URLRequest request( 238 net::URLRequest request(
239 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), 239 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(),
240 url_request_context_.get(), test_network_delegate_.get()); 240 url_request_context_.get(), test_network_delegate_.get());
241 request.Start(); 241 request.Start();
242 242
243 MessageLoop::current()->Run(); 243 MessageLoop::current()->Run();
244 244
245 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); 245 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status());
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 // Start the request, and cancel it immediately after it. 325 // Start the request, and cancel it immediately after it.
326 request.Start(); 326 request.Start();
327 request.Cancel(); 327 request.Cancel();
328 328
329 MessageLoop::current()->Run(); 329 MessageLoop::current()->Run();
330 330
331 EXPECT_EQ(net::URLRequestStatus::CANCELED, request.status().status()); 331 EXPECT_EQ(net::URLRequestStatus::CANCELED, request.status().status());
332 } 332 }
333 333
334 } // namespace drive 334 } // namespace drive
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/drive/drive_url_request_job.cc ('k') | chrome/browser/chromeos/drive/fake_drive_file_system.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698