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
3 // found in the LICENSE file.
4
5 #include <map>
6 #include <set>
7 #include <string>
8 #include <vector>
9
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/file_util.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "base/run_loop.h"
15 #include "base/synchronization/waitable_event.h"
16 #include "base/time/time.h"
17 #include "chrome/browser/media_galleries/fileapi/iphoto_data_provider.h"
18 #include "chrome/browser/media_galleries/fileapi/iphoto_file_util.h"
19 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h"
20 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h"
21 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "content/public/test/mock_special_storage_policy.h"
24 #include "content/public/test/test_browser_thread.h"
25 #include "content/public/test/test_file_system_options.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "webkit/browser/fileapi/async_file_util.h"
28 #include "webkit/browser/fileapi/external_mount_points.h"
29 #include "webkit/browser/fileapi/file_system_context.h"
30 #include "webkit/browser/fileapi/file_system_operation_context.h"
31 #include "webkit/browser/fileapi/file_system_operation_runner.h"
32
33 using fileapi::FileSystemOperationContext;
34 using fileapi::FileSystemOperation;
35 using fileapi::FileSystemURL;
36
37 namespace iphoto {
38
39 namespace {
40
ReadDirectoryTestHelperCallback(base::RunLoop * run_loop,FileSystemOperation::FileEntryList * contents,bool * completed,base::File::Error error,const FileSystemOperation::FileEntryList & file_list,bool has_more)41 void ReadDirectoryTestHelperCallback(
42 base::RunLoop* run_loop,
43 FileSystemOperation::FileEntryList* contents,
44 bool* completed,
45 base::File::Error error,
46 const FileSystemOperation::FileEntryList& file_list,
47 bool has_more) {
48 DCHECK(!*completed);
49 *completed = !has_more && error == base::File::FILE_OK;
50 *contents = file_list;
51 run_loop->Quit();
52 }
53
ReadDirectoryTestHelper(fileapi::FileSystemOperationRunner * runner,const FileSystemURL & url,FileSystemOperation::FileEntryList * contents,bool * completed)54 void ReadDirectoryTestHelper(fileapi::FileSystemOperationRunner* runner,
55 const FileSystemURL& url,
56 FileSystemOperation::FileEntryList* contents,
57 bool* completed) {
58 DCHECK(contents);
59 DCHECK(completed);
60 base::RunLoop run_loop;
61 runner->ReadDirectory(
62 url, base::Bind(&ReadDirectoryTestHelperCallback, &run_loop, contents,
63 completed));
64 run_loop.Run();
65 }
66
67 } // namespace
68
69 class TestIPhotoDataProvider : public IPhotoDataProvider {
70 public:
TestIPhotoDataProvider(const base::FilePath & fake_library_path)71 explicit TestIPhotoDataProvider(const base::FilePath& fake_library_path)
72 : IPhotoDataProvider(fake_library_path) {
73 EXPECT_TRUE(fake_auto_add_dir_.CreateUniqueTempDir());
74 }
75
~TestIPhotoDataProvider()76 virtual ~TestIPhotoDataProvider() {}
77
RefreshData(const ReadyCallback & ready_callback)78 virtual void RefreshData(const ReadyCallback& ready_callback) OVERRIDE {
79 ready_callback.Run(true /* success */);
80 }
81
GetAlbumNames() const82 virtual std::vector<std::string> GetAlbumNames() const OVERRIDE {
83 std::vector<std::string> names;
84 names.push_back("Album1");
85 names.push_back("has_originals");
86 return names;
87 }
88
GetAlbumContents(const std::string & album) const89 virtual std::map<std::string, base::FilePath> GetAlbumContents(
90 const std::string& album) const OVERRIDE {
91 std::map<std::string, base::FilePath> contents;
92 contents["a.jpg"] = library_path().AppendASCII("a.jpg");
93 return contents;
94 }
95
GetPhotoLocationInAlbum(const std::string & album,const std::string & filename) const96 virtual base::FilePath GetPhotoLocationInAlbum(
97 const std::string& album,
98 const std::string& filename) const OVERRIDE {
99 return library_path().AppendASCII("a.jpg");
100 }
101
HasOriginals(const std::string & album) const102 virtual bool HasOriginals(const std::string& album) const OVERRIDE {
103 return (album == "has_originals");
104 }
105
GetOriginals(const std::string & album) const106 virtual std::map<std::string, base::FilePath> GetOriginals(
107 const std::string& album) const OVERRIDE {
108 std::map<std::string, base::FilePath> contents;
109 contents["a.jpg"] = library_path().AppendASCII("orig.jpg");
110 return contents;
111 }
112
GetOriginalPhotoLocation(const std::string & album,const std::string & filename) const113 virtual base::FilePath GetOriginalPhotoLocation(
114 const std::string& album,
115 const std::string& filename) const OVERRIDE {
116 return library_path().AppendASCII("orig.jpg");
117 }
118
119 private:
120 base::ScopedTempDir fake_auto_add_dir_;
121 };
122
123 class TestIPhotoFileUtil : public IPhotoFileUtil {
124 public:
TestIPhotoFileUtil(MediaPathFilter * media_path_filter,IPhotoDataProvider * data_provider)125 explicit TestIPhotoFileUtil(MediaPathFilter* media_path_filter,
126 IPhotoDataProvider* data_provider)
127 : IPhotoFileUtil(media_path_filter),
128 data_provider_(data_provider) {
129 }
~TestIPhotoFileUtil()130 virtual ~TestIPhotoFileUtil() {}
131
132 private:
GetDataProvider()133 virtual IPhotoDataProvider* GetDataProvider() OVERRIDE {
134 return data_provider_;
135 }
136
137 IPhotoDataProvider* data_provider_;
138 };
139
140 class TestMediaFileSystemBackend : public MediaFileSystemBackend {
141 public:
TestMediaFileSystemBackend(const base::FilePath & profile_path,IPhotoFileUtil * iphoto_file_util)142 TestMediaFileSystemBackend(const base::FilePath& profile_path,
143 IPhotoFileUtil* iphoto_file_util)
144 : MediaFileSystemBackend(
145 profile_path,
146 MediaFileSystemBackend::MediaTaskRunner().get()),
147 test_file_util_(iphoto_file_util) {}
148
149 virtual fileapi::AsyncFileUtil*
GetAsyncFileUtil(fileapi::FileSystemType type)150 GetAsyncFileUtil(fileapi::FileSystemType type) OVERRIDE {
151 if (type != fileapi::kFileSystemTypeIphoto)
152 return NULL;
153
154 return test_file_util_.get();
155 }
156
157 private:
158 scoped_ptr<fileapi::AsyncFileUtil> test_file_util_;
159 };
160
161 class IPhotoFileUtilTest : public testing::Test {
162 public:
IPhotoFileUtilTest()163 IPhotoFileUtilTest()
164 : io_thread_(content::BrowserThread::IO, &message_loop_) {
165 }
~IPhotoFileUtilTest()166 virtual ~IPhotoFileUtilTest() {}
167
SetUpDataProvider()168 void SetUpDataProvider() {
169 ASSERT_TRUE(fake_library_dir_.CreateUniqueTempDir());
170 ASSERT_EQ(
171 0,
172 base::WriteFile(
173 fake_library_dir_.path().AppendASCII("a.jpg"),
174 NULL,
175 0));
176 ASSERT_EQ(
177 0,
178 base::WriteFile(
179 fake_library_dir_.path().AppendASCII("orig.jpg"),
180 NULL,
181 0));
182
183 iphoto_data_provider_.reset(
184 new TestIPhotoDataProvider(fake_library_dir_.path()));
185 }
186
SetUp()187 virtual void SetUp() OVERRIDE {
188 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir());
189 ImportedMediaGalleryRegistry::GetInstance()->Initialize();
190
191 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
192 new content::MockSpecialStoragePolicy();
193
194 // Initialize fake IPhotoDataProvider on media task runner thread.
195 MediaFileSystemBackend::MediaTaskRunner()->PostTask(
196 FROM_HERE,
197 base::Bind(&IPhotoFileUtilTest::SetUpDataProvider,
198 base::Unretained(this)));
199 base::WaitableEvent event(true, false /* initially_signalled */);
200 MediaFileSystemBackend::MediaTaskRunner()->PostTask(
201 FROM_HERE,
202 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event)));
203 event.Wait();
204
205 media_path_filter_.reset(new MediaPathFilter());
206 ScopedVector<fileapi::FileSystemBackend> additional_providers;
207 additional_providers.push_back(new TestMediaFileSystemBackend(
208 profile_dir_.path(),
209 new TestIPhotoFileUtil(media_path_filter_.get(),
210 iphoto_data_provider_.get())));
211
212 file_system_context_ = new fileapi::FileSystemContext(
213 base::MessageLoopProxy::current().get(),
214 base::MessageLoopProxy::current().get(),
215 fileapi::ExternalMountPoints::CreateRefCounted().get(),
216 storage_policy.get(),
217 NULL,
218 additional_providers.Pass(),
219 std::vector<fileapi::URLRequestAutoMountHandler>(),
220 profile_dir_.path(),
221 content::CreateAllowFileAccessOptions());
222 }
223
224 protected:
TestNonexistentFolder(const std::string & path_append)225 void TestNonexistentFolder(const std::string& path_append) {
226 FileSystemOperation::FileEntryList contents;
227 FileSystemURL url = CreateURL(path_append);
228 bool completed = false;
229 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
230
231 ASSERT_FALSE(completed);
232 }
233
CreateURL(const std::string & path) const234 FileSystemURL CreateURL(const std::string& path) const {
235 base::FilePath virtual_path =
236 ImportedMediaGalleryRegistry::GetInstance()->ImportedRoot();
237 virtual_path = virtual_path.AppendASCII("iphoto");
238 virtual_path = virtual_path.AppendASCII(path);
239 return file_system_context_->CreateCrackedFileSystemURL(
240 GURL("http://www.example.com"), fileapi::kFileSystemTypeIphoto,
241 virtual_path);
242 }
243
operation_runner() const244 fileapi::FileSystemOperationRunner* operation_runner() const {
245 return file_system_context_->operation_runner();
246 }
247
file_system_context() const248 scoped_refptr<fileapi::FileSystemContext> file_system_context() const {
249 return file_system_context_;
250 }
251
data_provider() const252 TestIPhotoDataProvider* data_provider() const {
253 return iphoto_data_provider_.get();
254 }
255
256 private:
257 base::MessageLoop message_loop_;
258 content::TestBrowserThread io_thread_;
259
260 base::ScopedTempDir profile_dir_;
261 base::ScopedTempDir fake_library_dir_;
262
263 scoped_refptr<fileapi::FileSystemContext> file_system_context_;
264 scoped_ptr<MediaPathFilter> media_path_filter_;
265 scoped_ptr<TestIPhotoDataProvider> iphoto_data_provider_;
266
267 DISALLOW_COPY_AND_ASSIGN(IPhotoFileUtilTest);
268 };
269
TEST_F(IPhotoFileUtilTest,RootContents)270 TEST_F(IPhotoFileUtilTest, RootContents) {
271 FileSystemOperation::FileEntryList contents;
272 FileSystemURL url = CreateURL("");
273 bool completed = false;
274 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
275
276 ASSERT_TRUE(completed);
277 ASSERT_EQ(1u, contents.size());
278
279 EXPECT_TRUE(contents.front().is_directory);
280
281 EXPECT_EQ(base::FilePath::FromUTF8Unsafe(kIPhotoAlbumsDir).value(),
282 contents.back().name);
283 }
284
TEST_F(IPhotoFileUtilTest,AlbumsDirectoryContents)285 TEST_F(IPhotoFileUtilTest, AlbumsDirectoryContents) {
286 FileSystemOperation::FileEntryList contents;
287 FileSystemURL url = CreateURL(kIPhotoAlbumsDir);
288 bool completed = false;
289 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
290
291 ASSERT_TRUE(completed);
292 ASSERT_EQ(2u, contents.size());
293
294 EXPECT_TRUE(contents.front().is_directory);
295
296 EXPECT_EQ("Album1", contents.front().name);
297 EXPECT_EQ("has_originals", contents.back().name);
298 }
299
TEST_F(IPhotoFileUtilTest,AlbumContents)300 TEST_F(IPhotoFileUtilTest, AlbumContents) {
301 FileSystemOperation::FileEntryList contents;
302 FileSystemURL url = CreateURL(std::string(kIPhotoAlbumsDir) + "/Album1");
303 bool completed = false;
304 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
305
306 ASSERT_TRUE(completed);
307 ASSERT_EQ(1u, contents.size());
308
309 EXPECT_FALSE(contents.front().is_directory);
310
311 EXPECT_EQ("a.jpg", contents.back().name);
312 }
313
TEST_F(IPhotoFileUtilTest,BadAccess)314 TEST_F(IPhotoFileUtilTest, BadAccess) {
315 FileSystemOperation::FileEntryList contents;
316 FileSystemURL url = CreateURL("None");
317 bool completed = false;
318 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
319 ASSERT_FALSE(completed);
320 ASSERT_EQ(0u, contents.size());
321
322 url = CreateURL(std::string(kIPhotoAlbumsDir) + "/NoAlbum");
323 completed = false;
324 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
325 ASSERT_FALSE(completed);
326 ASSERT_EQ(0u, contents.size());
327 }
328
TEST_F(IPhotoFileUtilTest,Originals)329 TEST_F(IPhotoFileUtilTest, Originals) {
330 FileSystemOperation::FileEntryList contents;
331 FileSystemURL url =
332 CreateURL(std::string(kIPhotoAlbumsDir) + "/has_originals");
333 bool completed = false;
334 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
335
336 ASSERT_TRUE(completed);
337 ASSERT_EQ(2u, contents.size());
338 EXPECT_TRUE(contents.front().is_directory);
339 EXPECT_EQ("Originals", contents.front().name);
340 EXPECT_FALSE(contents.back().is_directory);
341 EXPECT_EQ("a.jpg", contents.back().name);
342
343 url = CreateURL(std::string(kIPhotoAlbumsDir) + "/has_originals/Originals");
344 completed = false;
345 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
346 ASSERT_TRUE(completed);
347 ASSERT_EQ(1u, contents.size());
348
349 EXPECT_FALSE(contents.front().is_directory);
350 EXPECT_EQ("a.jpg", contents.front().name);
351 }
352
353 } // namespace iphoto
354