| 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 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "base/string_piece.h" | 24 #include "base/string_piece.h" |
| 25 #include "base/stringprintf.h" | 25 #include "base/stringprintf.h" |
| 26 #include "base/utf_string_conversions.h" | 26 #include "base/utf_string_conversions.h" |
| 27 #include "net/base/load_flags.h" | 27 #include "net/base/load_flags.h" |
| 28 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
| 29 #include "net/base/net_util.h" | 29 #include "net/base/net_util.h" |
| 30 #include "net/http/http_request_headers.h" | 30 #include "net/http/http_request_headers.h" |
| 31 #include "net/url_request/url_request.h" | 31 #include "net/url_request/url_request.h" |
| 32 #include "net/url_request/url_request_test_util.h" | 32 #include "net/url_request/url_request_test_util.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 34 #include "webkit/fileapi/file_system_context.h" |
| 34 #include "webkit/fileapi/file_system_path_manager.h" | 35 #include "webkit/fileapi/file_system_path_manager.h" |
| 35 | 36 |
| 36 namespace fileapi { | 37 namespace fileapi { |
| 37 namespace { | 38 namespace { |
| 38 | 39 |
| 39 // We always use the TEMPORARY FileSystem in this test. | 40 // We always use the TEMPORARY FileSystem in this test. |
| 40 const char kFileSystemURLPrefix[] = "filesystem:http://remote/temporary/"; | 41 const char kFileSystemURLPrefix[] = "filesystem:http://remote/temporary/"; |
| 41 const char kTestFileData[] = "0123456789"; | 42 const char kTestFileData[] = "0123456789"; |
| 42 | 43 |
| 43 void FillBuffer(char* buffer, size_t len) { | 44 void FillBuffer(char* buffer, size_t len) { |
| 44 static bool called = false; | 45 static bool called = false; |
| 45 if (!called) { | 46 if (!called) { |
| 46 called = true; | 47 called = true; |
| 47 int seed = static_cast<int>(base::Time::Now().ToInternalValue()); | 48 int seed = static_cast<int>(base::Time::Now().ToInternalValue()); |
| 48 srand(seed); | 49 srand(seed); |
| 49 } | 50 } |
| 50 | 51 |
| 51 for (size_t i = 0; i < len; i++) { | 52 for (size_t i = 0; i < len; i++) { |
| 52 buffer[i] = static_cast<char>(rand()); | 53 buffer[i] = static_cast<char>(rand()); |
| 53 if (!buffer[i]) | 54 if (!buffer[i]) |
| 54 buffer[i] = 'g'; | 55 buffer[i] = 'g'; |
| 55 } | 56 } |
| 56 } | 57 } |
| 57 | 58 |
| 59 class TestSpecialStoragePolicy : public quota::SpecialStoragePolicy { |
| 60 public: |
| 61 virtual bool IsStorageProtected(const GURL& origin) { |
| 62 return false; |
| 63 } |
| 64 |
| 65 virtual bool IsStorageUnlimited(const GURL& origin) { |
| 66 return true; |
| 67 } |
| 68 |
| 69 virtual bool IsFileHandler(const std::string& extension_id) { |
| 70 return true; |
| 71 } |
| 72 }; |
| 73 |
| 58 class FileSystemURLRequestJobTest : public testing::Test { | 74 class FileSystemURLRequestJobTest : public testing::Test { |
| 59 protected: | 75 protected: |
| 60 FileSystemURLRequestJobTest() | 76 FileSystemURLRequestJobTest() |
| 61 : message_loop_(MessageLoop::TYPE_IO), // simulate an IO thread | 77 : message_loop_(MessageLoop::TYPE_IO), // simulate an IO thread |
| 62 ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)) { | 78 ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)) { |
| 63 } | 79 } |
| 64 | 80 |
| 65 virtual void SetUp() { | 81 virtual void SetUp() { |
| 66 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 82 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 67 | 83 |
| 84 special_storage_policy_ = new TestSpecialStoragePolicy(); |
| 68 // We use the main thread so that we can get the root path synchronously. | 85 // We use the main thread so that we can get the root path synchronously. |
| 69 // TODO(adamk): Run this on the FILE thread we've created as well. | 86 // TODO(adamk): Run this on the FILE thread we've created as well. |
| 70 path_manager_.reset(new FileSystemPathManager( | 87 file_system_context_ = |
| 71 base::MessageLoopProxy::CreateForCurrentThread(), | 88 new FileSystemContext( |
| 72 temp_dir_.path(), NULL, false, false)); | 89 base::MessageLoopProxy::CreateForCurrentThread(), |
| 90 base::MessageLoopProxy::CreateForCurrentThread(), |
| 91 special_storage_policy_, |
| 92 FilePath(), false /* is_incognito */, |
| 93 false, true, |
| 94 new FileSystemPathManager( |
| 95 base::MessageLoopProxy::CreateForCurrentThread(), |
| 96 temp_dir_.path(), NULL, false, false)); |
| 73 | 97 |
| 74 path_manager_->GetFileSystemRootPath( | 98 file_system_context_->path_manager()->GetFileSystemRootPath( |
| 75 GURL("http://remote/"), kFileSystemTypeTemporary, true, // create | 99 GURL("http://remote/"), kFileSystemTypeTemporary, true, // create |
| 76 callback_factory_.NewCallback( | 100 callback_factory_.NewCallback( |
| 77 &FileSystemURLRequestJobTest::OnGetRootPath)); | 101 &FileSystemURLRequestJobTest::OnGetRootPath)); |
| 78 MessageLoop::current()->RunAllPending(); | 102 MessageLoop::current()->RunAllPending(); |
| 79 | 103 |
| 80 net::URLRequest::RegisterProtocolFactory("filesystem", | 104 net::URLRequest::RegisterProtocolFactory("filesystem", |
| 81 &FileSystemURLRequestJobFactory); | 105 &FileSystemURLRequestJobFactory); |
| 82 } | 106 } |
| 83 | 107 |
| 84 virtual void TearDown() { | 108 virtual void TearDown() { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 100 | 124 |
| 101 void TestRequestWithHeaders(const GURL& url, | 125 void TestRequestWithHeaders(const GURL& url, |
| 102 const net::HttpRequestHeaders* headers) { | 126 const net::HttpRequestHeaders* headers) { |
| 103 delegate_.reset(new TestDelegate()); | 127 delegate_.reset(new TestDelegate()); |
| 104 // Make delegate_ exit the MessageLoop when the request is done. | 128 // Make delegate_ exit the MessageLoop when the request is done. |
| 105 delegate_->set_quit_on_complete(true); | 129 delegate_->set_quit_on_complete(true); |
| 106 delegate_->set_quit_on_redirect(true); | 130 delegate_->set_quit_on_redirect(true); |
| 107 request_.reset(new net::URLRequest(url, delegate_.get())); | 131 request_.reset(new net::URLRequest(url, delegate_.get())); |
| 108 if (headers) | 132 if (headers) |
| 109 request_->SetExtraRequestHeaders(*headers); | 133 request_->SetExtraRequestHeaders(*headers); |
| 110 job_ = new FileSystemURLRequestJob(request_.get(), path_manager_.get(), | 134 job_ = new FileSystemURLRequestJob( |
| 135 request_.get(), |
| 136 file_system_context_.get(), |
| 111 base::MessageLoopProxy::CreateForCurrentThread()); | 137 base::MessageLoopProxy::CreateForCurrentThread()); |
| 112 | 138 |
| 113 request_->Start(); | 139 request_->Start(); |
| 114 ASSERT_TRUE(request_->is_pending()); // verify that we're starting async | 140 ASSERT_TRUE(request_->is_pending()); // verify that we're starting async |
| 115 MessageLoop::current()->Run(); | 141 MessageLoop::current()->Run(); |
| 116 } | 142 } |
| 117 | 143 |
| 118 void WriteFile(const base::StringPiece file_name, | 144 void WriteFile(const base::StringPiece file_name, |
| 119 const char* buf, int buf_size) { | 145 const char* buf, int buf_size) { |
| 120 FilePath path = origin_root_path_.AppendASCII(file_name); | 146 FilePath path = origin_root_path_.AppendASCII(file_name); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 131 DCHECK(job_); | 157 DCHECK(job_); |
| 132 net::URLRequestJob* temp = job_; | 158 net::URLRequestJob* temp = job_; |
| 133 job_ = NULL; | 159 job_ = NULL; |
| 134 return temp; | 160 return temp; |
| 135 } | 161 } |
| 136 | 162 |
| 137 ScopedTempDir temp_dir_; | 163 ScopedTempDir temp_dir_; |
| 138 FilePath origin_root_path_; | 164 FilePath origin_root_path_; |
| 139 scoped_ptr<net::URLRequest> request_; | 165 scoped_ptr<net::URLRequest> request_; |
| 140 scoped_ptr<TestDelegate> delegate_; | 166 scoped_ptr<TestDelegate> delegate_; |
| 141 scoped_ptr<FileSystemPathManager> path_manager_; | 167 scoped_refptr<TestSpecialStoragePolicy> special_storage_policy_; |
| 142 | 168 scoped_refptr<FileSystemContext> file_system_context_; |
| 143 MessageLoop message_loop_; | 169 MessageLoop message_loop_; |
| 144 base::ScopedCallbackFactory<FileSystemURLRequestJobTest> callback_factory_; | 170 base::ScopedCallbackFactory<FileSystemURLRequestJobTest> callback_factory_; |
| 145 | 171 |
| 146 static net::URLRequestJob* job_; | 172 static net::URLRequestJob* job_; |
| 147 }; | 173 }; |
| 148 | 174 |
| 149 // static | 175 // static |
| 150 net::URLRequestJob* FileSystemURLRequestJobTest::job_ = NULL; | 176 net::URLRequestJob* FileSystemURLRequestJobTest::job_ = NULL; |
| 151 | 177 |
| 152 TEST_F(FileSystemURLRequestJobTest, FileTest) { | 178 TEST_F(FileSystemURLRequestJobTest, FileTest) { |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 | 284 |
| 259 TEST_F(FileSystemURLRequestJobTest, NoSuchFile) { | 285 TEST_F(FileSystemURLRequestJobTest, NoSuchFile) { |
| 260 TestRequest(CreateFileSystemURL("somefile")); | 286 TestRequest(CreateFileSystemURL("somefile")); |
| 261 ASSERT_FALSE(request_->is_pending()); | 287 ASSERT_FALSE(request_->is_pending()); |
| 262 EXPECT_TRUE(delegate_->request_failed()); | 288 EXPECT_TRUE(delegate_->request_failed()); |
| 263 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, request_->status().os_error()); | 289 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, request_->status().os_error()); |
| 264 } | 290 } |
| 265 | 291 |
| 266 } // namespace (anonymous) | 292 } // namespace (anonymous) |
| 267 } // namespace fileapi | 293 } // namespace fileapi |
| OLD | NEW |