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

Side by Side Diff: chrome/browser/chromeos/file_system_provider/fileapi/provider_async_file_util_unittest.cc

Issue 258783006: [fsp] Add the getMetadata operation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased. Created 6 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 <string> 5 #include <string>
6 #include <vector> 6 #include <vector>
7 7
8 #include "base/files/file.h" 8 #include "base/files/file.h"
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h" 12 #include "base/memory/weak_ptr.h"
13 #include "base/platform_file.h" 13 #include "base/platform_file.h"
14 #include "base/run_loop.h"
15 #include "chrome/browser/chromeos/file_system_provider/fake_provided_file_system .h"
14 #include "chrome/browser/chromeos/file_system_provider/fileapi/provider_async_fi le_util.h" 16 #include "chrome/browser/chromeos/file_system_provider/fileapi/provider_async_fi le_util.h"
15 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h" 17 #include "chrome/browser/chromeos/file_system_provider/service.h"
18 #include "chrome/browser/chromeos/file_system_provider/service_factory.h"
19 #include "chrome/test/base/testing_browser_process.h"
16 #include "chrome/test/base/testing_profile.h" 20 #include "chrome/test/base/testing_profile.h"
21 #include "chrome/test/base/testing_profile_manager.h"
17 #include "content/public/test/test_browser_thread_bundle.h" 22 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "content/public/test/test_file_system_context.h" 23 #include "content/public/test/test_file_system_context.h"
24 #include "extensions/browser/extension_registry.h"
19 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
20 #include "webkit/browser/fileapi/async_file_util.h" 26 #include "webkit/browser/fileapi/async_file_util.h"
21 #include "webkit/browser/fileapi/external_mount_points.h" 27 #include "webkit/browser/fileapi/external_mount_points.h"
22 #include "webkit/browser/fileapi/file_system_context.h" 28 #include "webkit/browser/fileapi/file_system_context.h"
23 #include "webkit/browser/fileapi/file_system_url.h" 29 #include "webkit/browser/fileapi/file_system_url.h"
24 #include "webkit/common/blob/shareable_file_reference.h" 30 #include "webkit/common/blob/shareable_file_reference.h"
25 31
26 namespace chromeos { 32 namespace chromeos {
27 namespace file_system_provider { 33 namespace file_system_provider {
28 namespace { 34 namespace {
29 35
30 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; 36 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
31 const int kFileSystemId = 1;
32 37
33 // Logs callbacks invocations on the tested operations. 38 // Logs callbacks invocations on the tested operations.
34 // TODO(mtomasz): Store and verify more arguments, once the operations return 39 // TODO(mtomasz): Store and verify more arguments, once the operations return
35 // anything else than just an error. 40 // anything else than just an error.
36 class EventLogger { 41 class EventLogger {
37 public: 42 public:
38 EventLogger() : weak_ptr_factory_(this) {} 43 EventLogger() : weak_ptr_factory_(this) {}
39 virtual ~EventLogger() {} 44 virtual ~EventLogger() {}
40 45
41 void OnStatus(base::File::Error error) { 46 void OnStatus(base::File::Error error) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 84
80 base::File::Error* error() { return error_.get(); } 85 base::File::Error* error() { return error_.get(); }
81 86
82 private: 87 private:
83 scoped_ptr<base::File::Error> error_; 88 scoped_ptr<base::File::Error> error_;
84 base::WeakPtrFactory<EventLogger> weak_ptr_factory_; 89 base::WeakPtrFactory<EventLogger> weak_ptr_factory_;
85 90
86 DISALLOW_COPY_AND_ASSIGN(EventLogger); 91 DISALLOW_COPY_AND_ASSIGN(EventLogger);
87 }; 92 };
88 93
89 // Registers an external mount point, and removes it once the object gets out
90 // of scope. To ensure that creating the mount point succeeded, call is_valid().
91 class ScopedExternalMountPoint {
92 public:
93 ScopedExternalMountPoint(const std::string& mount_point_name,
94 const base::FilePath& mount_path,
95 fileapi::FileSystemType type)
96 : mount_point_name_(mount_point_name) {
97 fileapi::ExternalMountPoints* const mount_points =
98 fileapi::ExternalMountPoints::GetSystemInstance();
99 DCHECK(mount_points);
100 is_valid_ =
101 mount_points->RegisterFileSystem(mount_point_name,
102 fileapi::kFileSystemTypeProvided,
103 fileapi::FileSystemMountOption(),
104 mount_path);
105 }
106
107 virtual ~ScopedExternalMountPoint() {
108 if (!is_valid_)
109 return;
110
111 // If successfully registered in the constructor, then unregister.
112 fileapi::ExternalMountPoints* const mount_points =
113 fileapi::ExternalMountPoints::GetSystemInstance();
114 DCHECK(mount_points);
115 mount_points->RevokeFileSystem(mount_point_name_);
116 }
117
118 bool is_valid() { return is_valid_; }
119
120 private:
121 const std::string mount_point_name_;
122 bool is_valid_;
123 };
124
125 // Creates a cracked FileSystemURL for tests. 94 // Creates a cracked FileSystemURL for tests.
126 fileapi::FileSystemURL CreateFileSystemURL(const std::string& mount_point_name, 95 fileapi::FileSystemURL CreateFileSystemURL(const std::string& mount_point_name,
127 const base::FilePath& file_path) { 96 const base::FilePath& file_path) {
128 const std::string origin = std::string("chrome-extension://") + kExtensionId; 97 const std::string origin = std::string("chrome-extension://") + kExtensionId;
129 const fileapi::ExternalMountPoints* const mount_points = 98 const fileapi::ExternalMountPoints* const mount_points =
130 fileapi::ExternalMountPoints::GetSystemInstance(); 99 fileapi::ExternalMountPoints::GetSystemInstance();
131 return mount_points->CreateCrackedFileSystemURL( 100 return mount_points->CreateCrackedFileSystemURL(
132 GURL(origin), 101 GURL(origin),
133 fileapi::kFileSystemTypeExternal, 102 fileapi::kFileSystemTypeExternal,
134 base::FilePath::FromUTF8Unsafe(mount_point_name).Append(file_path)); 103 base::FilePath::FromUTF8Unsafe(mount_point_name).Append(file_path));
135 } 104 }
136 105
106 // Creates a Service instance. Used to be able to destroy the service in
107 // TearDown().
108 KeyedService* CreateService(content::BrowserContext* context) {
109 return new Service(Profile::FromBrowserContext(context),
110 extensions::ExtensionRegistry::Get(context));
111 }
112
137 } // namespace 113 } // namespace
138 114
139 // Tests in this file are very lightweight and just test integration between 115 // Tests in this file are very lightweight and just test integration between
140 // AsyncFileUtil and ProvideFileSystemInterface. Currently it tests if not 116 // AsyncFileUtil and ProvideFileSystemInterface. Currently it tests if not
141 // implemented operations return a correct error code. For not allowed 117 // implemented operations return a correct error code. For not allowed
142 // operations it is FILE_ERROR_SECURITY, and for not implemented the error is 118 // operations it is FILE_ERROR_SECURITY, and for not implemented the error is
143 // FILE_ERROR_NOT_FOUND. 119 // FILE_ERROR_NOT_FOUND.
144 class FileSystemProviderProviderAsyncFileUtilTest : public testing::Test { 120 class FileSystemProviderProviderAsyncFileUtilTest : public testing::Test {
145 protected: 121 protected:
146 FileSystemProviderProviderAsyncFileUtilTest() {} 122 FileSystemProviderProviderAsyncFileUtilTest() {}
147 virtual ~FileSystemProviderProviderAsyncFileUtilTest() {} 123 virtual ~FileSystemProviderProviderAsyncFileUtilTest() {}
148 124
149 virtual void SetUp() OVERRIDE { 125 virtual void SetUp() OVERRIDE {
150 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); 126 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
151 profile_.reset(new TestingProfile); 127 profile_manager_.reset(
128 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
129 ASSERT_TRUE(profile_manager_->SetUp());
130 profile_ = profile_manager_->CreateTestingProfile("testing-profile");
152 async_file_util_.reset(new internal::ProviderAsyncFileUtil); 131 async_file_util_.reset(new internal::ProviderAsyncFileUtil);
153 const base::FilePath mount_path = 132
154 util::GetMountPath(profile_.get(), kExtensionId, kFileSystemId);
155 file_system_context_ = 133 file_system_context_ =
156 content::CreateFileSystemContextForTesting(NULL, data_dir_.path()); 134 content::CreateFileSystemContextForTesting(NULL, data_dir_.path());
157 135
158 const std::string mount_point_name = mount_path.BaseName().AsUTF8Unsafe(); 136 ServiceFactory::GetInstance()->SetTestingFactory(profile_, &CreateService);
159 mount_point_.reset(new ScopedExternalMountPoint( 137 Service* service = Service::Get(profile_); // Owned by its factory.
160 mount_point_name, mount_path, fileapi::kFileSystemTypeProvided)); 138 service->SetFileSystemFactoryForTests(
161 ASSERT_TRUE(mount_point_->is_valid()); 139 base::Bind(&FakeProvidedFileSystem::Create));
140
141 const int file_system_id =
142 service->MountFileSystem(kExtensionId, "testing-file-system");
143 ASSERT_LT(0, file_system_id);
144 const ProvidedFileSystemInfo& file_system_info =
145 service->GetProvidedFileSystem(kExtensionId, file_system_id)
146 ->GetFileSystemInfo();
147 const std::string mount_point_name =
148 file_system_info.mount_path().BaseName().AsUTF8Unsafe();
162 149
163 file_url_ = CreateFileSystemURL( 150 file_url_ = CreateFileSystemURL(
164 mount_point_name, base::FilePath::FromUTF8Unsafe("hello/world.txt")); 151 mount_point_name, base::FilePath::FromUTF8Unsafe("hello/world.txt"));
165 ASSERT_TRUE(file_url_.is_valid()); 152 ASSERT_TRUE(file_url_.is_valid());
166 directory_url_ = CreateFileSystemURL( 153 directory_url_ = CreateFileSystemURL(
167 mount_point_name, base::FilePath::FromUTF8Unsafe("hello")); 154 mount_point_name, base::FilePath::FromUTF8Unsafe("hello"));
168 ASSERT_TRUE(directory_url_.is_valid()); 155 ASSERT_TRUE(directory_url_.is_valid());
169 root_url_ = CreateFileSystemURL(mount_point_name, base::FilePath()); 156 root_url_ = CreateFileSystemURL(mount_point_name, base::FilePath());
170 ASSERT_TRUE(root_url_.is_valid()); 157 ASSERT_TRUE(root_url_.is_valid());
171 } 158 }
172 159
160 virtual void TearDown() OVERRIDE {
161 // Setting the testing factory to NULL will destroy the created service
162 // associated with the testing profile.
163 ServiceFactory::GetInstance()->SetTestingFactory(profile_, NULL);
164 }
165
173 scoped_ptr<fileapi::FileSystemOperationContext> CreateOperationContext() { 166 scoped_ptr<fileapi::FileSystemOperationContext> CreateOperationContext() {
174 return make_scoped_ptr( 167 return make_scoped_ptr(
175 new fileapi::FileSystemOperationContext(file_system_context_.get())); 168 new fileapi::FileSystemOperationContext(file_system_context_.get()));
176 } 169 }
177 170
178 content::TestBrowserThreadBundle thread_bundle_; 171 content::TestBrowserThreadBundle thread_bundle_;
179 base::ScopedTempDir data_dir_; 172 base::ScopedTempDir data_dir_;
180 scoped_ptr<TestingProfile> profile_; 173 scoped_ptr<TestingProfileManager> profile_manager_;
174 TestingProfile* profile_; // Owned by TestingProfileManager.
181 scoped_ptr<fileapi::AsyncFileUtil> async_file_util_; 175 scoped_ptr<fileapi::AsyncFileUtil> async_file_util_;
182 scoped_refptr<fileapi::FileSystemContext> file_system_context_; 176 scoped_refptr<fileapi::FileSystemContext> file_system_context_;
183 scoped_ptr<ScopedExternalMountPoint> mount_point_;
184 fileapi::FileSystemURL file_url_; 177 fileapi::FileSystemURL file_url_;
185 fileapi::FileSystemURL directory_url_; 178 fileapi::FileSystemURL directory_url_;
186 fileapi::FileSystemURL root_url_; 179 fileapi::FileSystemURL root_url_;
187 }; 180 };
188 181
189 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, CreateOrOpen_Create) { 182 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, CreateOrOpen_Create) {
190 EventLogger logger; 183 EventLogger logger;
191 184
192 async_file_util_->CreateOrOpen( 185 async_file_util_->CreateOrOpen(
193 CreateOperationContext(), 186 CreateOperationContext(),
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 269
277 ASSERT_TRUE(logger.error()); 270 ASSERT_TRUE(logger.error());
278 EXPECT_EQ(base::File::FILE_ERROR_SECURITY, *logger.error()); 271 EXPECT_EQ(base::File::FILE_ERROR_SECURITY, *logger.error());
279 } 272 }
280 273
281 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, GetFileInfo) { 274 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, GetFileInfo) {
282 EventLogger logger; 275 EventLogger logger;
283 276
284 async_file_util_->GetFileInfo( 277 async_file_util_->GetFileInfo(
285 CreateOperationContext(), 278 CreateOperationContext(),
286 file_url_, 279 root_url_,
287 base::Bind(&EventLogger::OnGetFileInfo, logger.GetWeakPtr())); 280 base::Bind(&EventLogger::OnGetFileInfo, logger.GetWeakPtr()));
281 base::RunLoop().RunUntilIdle();
288 282
289 ASSERT_TRUE(logger.error()); 283 ASSERT_TRUE(logger.error());
290 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, *logger.error()); 284 EXPECT_EQ(base::File::FILE_OK, *logger.error());
291 } 285 }
292 286
293 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, ReadDirectory) { 287 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, ReadDirectory) {
294 EventLogger logger; 288 EventLogger logger;
295 289
296 async_file_util_->ReadDirectory( 290 async_file_util_->ReadDirectory(
297 CreateOperationContext(), 291 CreateOperationContext(),
298 root_url_, 292 root_url_,
299 base::Bind(&EventLogger::OnReadDirectory, logger.GetWeakPtr())); 293 base::Bind(&EventLogger::OnReadDirectory, logger.GetWeakPtr()));
300 294
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 CreateOperationContext(), 408 CreateOperationContext(),
415 file_url_, 409 file_url_,
416 base::Bind(&EventLogger::OnCreateSnapshotFile, logger.GetWeakPtr())); 410 base::Bind(&EventLogger::OnCreateSnapshotFile, logger.GetWeakPtr()));
417 411
418 ASSERT_TRUE(logger.error()); 412 ASSERT_TRUE(logger.error());
419 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, *logger.error()); 413 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, *logger.error());
420 } 414 }
421 415
422 } // namespace file_system_provider 416 } // namespace file_system_provider
423 } // namespace chromeos 417 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698