| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // NOTE: These tests are run as part of "unit_tests" (in chrome/test/unit) | 5 // NOTE: These tests are run as part of "unit_tests" (in chrome/test/unit) |
| 6 // rather than as part of test_shell_tests because they rely on being able | 6 // rather than as part of test_shell_tests because they rely on being able |
| 7 // to instantiate a MessageLoop of type TYPE_IO. test_shell_tests uses | 7 // to instantiate a MessageLoop of type TYPE_IO. test_shell_tests uses |
| 8 // TYPE_UI, which URLRequest doesn't allow. | 8 // TYPE_UI, which URLRequest doesn't allow. |
| 9 // | 9 // |
| 10 | 10 |
| 11 #include "webkit/fileapi/file_system_dir_url_request_job.h" | 11 #include "webkit/fileapi/file_system_dir_url_request_job.h" |
| 12 | 12 |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 | 14 |
| 15 #include <string> | 15 #include <string> |
| 16 | 16 |
| 17 #include "base/file_path.h" | 17 #include "base/file_path.h" |
| 18 #include "base/file_util.h" | 18 #include "base/file_util.h" |
| 19 #include "base/format_macros.h" | 19 #include "base/format_macros.h" |
| 20 #include "base/memory/scoped_temp_dir.h" | 20 #include "base/memory/scoped_temp_dir.h" |
| 21 #include "base/message_loop.h" | 21 #include "base/message_loop.h" |
| 22 #include "base/platform_file.h" | 22 #include "base/platform_file.h" |
| 23 #include "base/string_piece.h" | 23 #include "base/string_piece.h" |
| 24 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
| 25 #include "net/base/net_util.h" | 25 #include "net/base/net_util.h" |
| 26 #include "net/http/http_request_headers.h" | 26 #include "net/http/http_request_headers.h" |
| 27 #include "net/url_request/url_request.h" | 27 #include "net/url_request/url_request.h" |
| 28 #include "net/url_request/url_request_test_util.h" | 28 #include "net/url_request/url_request_test_util.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| 30 #include "webkit/fileapi/file_system_context.h" |
| 30 #include "webkit/fileapi/file_system_path_manager.h" | 31 #include "webkit/fileapi/file_system_path_manager.h" |
| 31 | 32 |
| 32 namespace fileapi { | 33 namespace fileapi { |
| 33 namespace { | 34 namespace { |
| 34 | 35 |
| 35 // We always use the TEMPORARY FileSystem in this test. | 36 // We always use the TEMPORARY FileSystem in this test. |
| 36 static const char kFileSystemURLPrefix[] = "filesystem:http://remote/temporary/"
; | 37 static const char kFileSystemURLPrefix[] = "filesystem:http://remote/temporary/"
; |
| 37 | 38 |
| 39 class TestSpecialStoragePolicy : public quota::SpecialStoragePolicy { |
| 40 public: |
| 41 virtual bool IsStorageProtected(const GURL& origin) { |
| 42 return false; |
| 43 } |
| 44 |
| 45 virtual bool IsStorageUnlimited(const GURL& origin) { |
| 46 return true; |
| 47 } |
| 48 |
| 49 virtual bool IsFileHandler(const std::string& extension_id) { |
| 50 return true; |
| 51 } |
| 52 }; |
| 53 |
| 38 class FileSystemDirURLRequestJobTest : public testing::Test { | 54 class FileSystemDirURLRequestJobTest : public testing::Test { |
| 39 protected: | 55 protected: |
| 40 FileSystemDirURLRequestJobTest() | 56 FileSystemDirURLRequestJobTest() |
| 41 : message_loop_(MessageLoop::TYPE_IO), // simulate an IO thread | 57 : message_loop_(MessageLoop::TYPE_IO), // simulate an IO thread |
| 42 ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)) { | 58 ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)) { |
| 43 } | 59 } |
| 44 | 60 |
| 45 virtual void SetUp() { | 61 virtual void SetUp() { |
| 46 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 62 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 47 | 63 |
| 48 file_thread_proxy_ = base::MessageLoopProxy::CreateForCurrentThread(); | 64 file_thread_proxy_ = base::MessageLoopProxy::CreateForCurrentThread(); |
| 49 | 65 |
| 50 path_manager_.reset(new FileSystemPathManager( | 66 special_storage_policy_ = new TestSpecialStoragePolicy(); |
| 51 file_thread_proxy_, temp_dir_.path(), | 67 file_system_context_ = |
| 52 NULL, false, false)); | 68 new FileSystemContext( |
| 69 base::MessageLoopProxy::CreateForCurrentThread(), |
| 70 base::MessageLoopProxy::CreateForCurrentThread(), |
| 71 special_storage_policy_, |
| 72 FilePath(), false /* is_incognito */, |
| 73 false, true, |
| 74 new FileSystemPathManager( |
| 75 file_thread_proxy_, temp_dir_.path(), |
| 76 NULL, false, false)); |
| 53 | 77 |
| 54 path_manager_->GetFileSystemRootPath( | 78 file_system_context_->path_manager()->ValidateFileSystemRootAndGetURL( |
| 55 GURL("http://remote/"), kFileSystemTypeTemporary, true, // create | 79 GURL("http://remote/"), kFileSystemTypeTemporary, true, // create |
| 56 callback_factory_.NewCallback( | 80 callback_factory_.NewCallback( |
| 57 &FileSystemDirURLRequestJobTest::OnGetRootPath)); | 81 &FileSystemDirURLRequestJobTest::OnGetRootPath)); |
| 58 MessageLoop::current()->RunAllPending(); | 82 MessageLoop::current()->RunAllPending(); |
| 59 | 83 |
| 60 net::URLRequest::RegisterProtocolFactory( | 84 net::URLRequest::RegisterProtocolFactory( |
| 61 "filesystem", &FileSystemDirURLRequestJobFactory); | 85 "filesystem", &FileSystemDirURLRequestJobFactory); |
| 62 } | 86 } |
| 63 | 87 |
| 64 virtual void TearDown() { | 88 virtual void TearDown() { |
| 65 // NOTE: order matters, request must die before delegate | 89 // NOTE: order matters, request must die before delegate |
| 66 request_.reset(NULL); | 90 request_.reset(NULL); |
| 67 delegate_.reset(NULL); | 91 delegate_.reset(NULL); |
| 68 | 92 |
| 69 net::URLRequest::RegisterProtocolFactory("filesystem", NULL); | 93 net::URLRequest::RegisterProtocolFactory("filesystem", NULL); |
| 70 } | 94 } |
| 71 | 95 |
| 72 void OnGetRootPath(bool success, const FilePath& root_path, | 96 void OnGetRootPath(bool success, const FilePath& root_path, |
| 73 const std::string& name) { | 97 const std::string& name) { |
| 74 ASSERT_TRUE(success); | 98 ASSERT_TRUE(success); |
| 75 root_path_ = root_path; | 99 root_path_ = root_path; |
| 76 } | 100 } |
| 77 | 101 |
| 78 void TestRequest(const GURL& url) { | 102 void TestRequest(const GURL& url) { |
| 79 delegate_.reset(new TestDelegate()); | 103 delegate_.reset(new TestDelegate()); |
| 80 delegate_->set_quit_on_redirect(true); | 104 delegate_->set_quit_on_redirect(true); |
| 81 request_.reset(new net::URLRequest(url, delegate_.get())); | 105 request_.reset(new net::URLRequest(url, delegate_.get())); |
| 82 job_ = new FileSystemDirURLRequestJob(request_.get(), | 106 job_ = new FileSystemDirURLRequestJob(request_.get(), |
| 83 path_manager_.get(), | 107 file_system_context_.get(), |
| 84 file_thread_proxy_); | 108 file_thread_proxy_); |
| 85 | 109 |
| 86 request_->Start(); | 110 request_->Start(); |
| 87 ASSERT_TRUE(request_->is_pending()); // verify that we're starting async | 111 ASSERT_TRUE(request_->is_pending()); // verify that we're starting async |
| 88 MessageLoop::current()->Run(); | 112 MessageLoop::current()->Run(); |
| 89 } | 113 } |
| 90 | 114 |
| 91 void CreateDirectory(const base::StringPiece dir_name) { | 115 void CreateDirectory(const base::StringPiece dir_name) { |
| 92 FilePath path = root_path_.AppendASCII(dir_name); | 116 FilePath path = root_path_.AppendASCII(dir_name); |
| 93 ASSERT_TRUE(file_util::CreateDirectory(path)); | 117 ASSERT_TRUE(file_util::CreateDirectory(path)); |
| 94 } | 118 } |
| 95 | 119 |
| 96 GURL CreateFileSystemURL(const std::string path) { | 120 GURL CreateFileSystemURL(const std::string path) { |
| 97 return GURL(kFileSystemURLPrefix + path); | 121 return GURL(kFileSystemURLPrefix + path); |
| 98 } | 122 } |
| 99 | 123 |
| 100 static net::URLRequestJob* FileSystemDirURLRequestJobFactory( | 124 static net::URLRequestJob* FileSystemDirURLRequestJobFactory( |
| 101 net::URLRequest* request, | 125 net::URLRequest* request, |
| 102 const std::string& scheme) { | 126 const std::string& scheme) { |
| 103 DCHECK(job_); | 127 DCHECK(job_); |
| 104 net::URLRequestJob* temp = job_; | 128 net::URLRequestJob* temp = job_; |
| 105 job_ = NULL; | 129 job_ = NULL; |
| 106 return temp; | 130 return temp; |
| 107 } | 131 } |
| 108 | 132 |
| 109 ScopedTempDir temp_dir_; | 133 ScopedTempDir temp_dir_; |
| 110 FilePath root_path_; | 134 FilePath root_path_; |
| 111 scoped_ptr<net::URLRequest> request_; | 135 scoped_ptr<net::URLRequest> request_; |
| 112 scoped_ptr<TestDelegate> delegate_; | 136 scoped_ptr<TestDelegate> delegate_; |
| 113 scoped_ptr<FileSystemPathManager> path_manager_; | 137 scoped_refptr<TestSpecialStoragePolicy> special_storage_policy_; |
| 138 scoped_refptr<FileSystemContext> file_system_context_; |
| 114 scoped_refptr<base::MessageLoopProxy> file_thread_proxy_; | 139 scoped_refptr<base::MessageLoopProxy> file_thread_proxy_; |
| 115 | 140 |
| 116 MessageLoop message_loop_; | 141 MessageLoop message_loop_; |
| 117 base::ScopedCallbackFactory<FileSystemDirURLRequestJobTest> callback_factory_; | 142 base::ScopedCallbackFactory<FileSystemDirURLRequestJobTest> callback_factory_; |
| 118 | 143 |
| 119 static net::URLRequestJob* job_; | 144 static net::URLRequestJob* job_; |
| 120 }; | 145 }; |
| 121 | 146 |
| 122 // static | 147 // static |
| 123 net::URLRequestJob* FileSystemDirURLRequestJobTest::job_ = NULL; | 148 net::URLRequestJob* FileSystemDirURLRequestJobTest::job_ = NULL; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 154 | 179 |
| 155 TEST_F(FileSystemDirURLRequestJobTest, NoSuchDirectory) { | 180 TEST_F(FileSystemDirURLRequestJobTest, NoSuchDirectory) { |
| 156 TestRequest(CreateFileSystemURL("somedir/")); | 181 TestRequest(CreateFileSystemURL("somedir/")); |
| 157 ASSERT_FALSE(request_->is_pending()); | 182 ASSERT_FALSE(request_->is_pending()); |
| 158 ASSERT_FALSE(request_->status().is_success()); | 183 ASSERT_FALSE(request_->status().is_success()); |
| 159 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, request_->status().os_error()); | 184 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, request_->status().os_error()); |
| 160 } | 185 } |
| 161 | 186 |
| 162 } // namespace (anonymous) | 187 } // namespace (anonymous) |
| 163 } // namespace fileapi | 188 } // namespace fileapi |
| OLD | NEW |