Give names to all utility processes.
[chromium-blink-merge.git] / chrome / browser / media_galleries / fileapi / itunes_file_util_unittest.cc
blob5e0166313450ba1792a2e36403614af763eaf892
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.
5 #include <set>
6 #include <string>
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/run_loop.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "base/time/time.h"
16 #include "chrome/browser/media_galleries/fileapi/itunes_data_provider.h"
17 #include "chrome/browser/media_galleries/fileapi/itunes_file_util.h"
18 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h"
19 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h"
20 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/test/mock_special_storage_policy.h"
23 #include "content/public/test/test_browser_thread.h"
24 #include "content/public/test/test_file_system_options.h"
25 #include "storage/browser/fileapi/async_file_util.h"
26 #include "storage/browser/fileapi/external_mount_points.h"
27 #include "storage/browser/fileapi/file_system_context.h"
28 #include "storage/browser/fileapi/file_system_operation_context.h"
29 #include "storage/browser/fileapi/file_system_operation_runner.h"
30 #include "testing/gtest/include/gtest/gtest.h"
32 using storage::FileSystemOperationContext;
33 using storage::FileSystemOperation;
34 using storage::FileSystemURL;
36 namespace itunes {
38 namespace {
40 void ReadDirectoryTestHelperCallback(
41 base::RunLoop* run_loop,
42 FileSystemOperation::FileEntryList* contents,
43 bool* completed, base::File::Error error,
44 const FileSystemOperation::FileEntryList& file_list,
45 bool has_more) {
46 DCHECK(!*completed);
47 *completed = (!has_more && error == base::File::FILE_OK);
48 *contents = file_list;
49 run_loop->Quit();
52 void ReadDirectoryTestHelper(storage::FileSystemOperationRunner* runner,
53 const FileSystemURL& url,
54 FileSystemOperation::FileEntryList* contents,
55 bool* completed) {
56 DCHECK(contents);
57 DCHECK(completed);
58 base::RunLoop run_loop;
59 runner->ReadDirectory(
60 url, base::Bind(&ReadDirectoryTestHelperCallback, &run_loop, contents,
61 completed));
62 run_loop.Run();
65 } // namespace
67 class TestITunesDataProvider : public ITunesDataProvider {
68 public:
69 explicit TestITunesDataProvider(const base::FilePath& fake_library_path)
70 : ITunesDataProvider(fake_library_path) {
71 EXPECT_TRUE(fake_auto_add_dir_.CreateUniqueTempDir());
74 ~TestITunesDataProvider() override {}
76 void RefreshData(const ReadyCallback& ready_callback) override {
77 ready_callback.Run(true /* success */);
80 const base::FilePath& auto_add_path() const override {
81 return fake_auto_add_dir_.path();
84 void SetProvideAutoAddDir(bool provide_auto_add_dir) {
85 if (provide_auto_add_dir) {
86 if (!fake_auto_add_dir_.IsValid())
87 ASSERT_TRUE(fake_auto_add_dir_.CreateUniqueTempDir());
88 } else {
89 ASSERT_TRUE(fake_auto_add_dir_.Delete());
93 private:
94 base::ScopedTempDir fake_auto_add_dir_;
97 class TestITunesFileUtil : public ITunesFileUtil {
98 public:
99 explicit TestITunesFileUtil(MediaPathFilter* media_path_filter,
100 ITunesDataProvider* data_provider)
101 : ITunesFileUtil(media_path_filter),
102 data_provider_(data_provider) {
104 ~TestITunesFileUtil() override {}
106 private:
107 ITunesDataProvider* GetDataProvider() override { return data_provider_; }
109 ITunesDataProvider* data_provider_;
112 class TestMediaFileSystemBackend : public MediaFileSystemBackend {
113 public:
114 TestMediaFileSystemBackend(const base::FilePath& profile_path,
115 ITunesFileUtil* itunes_file_util)
116 : MediaFileSystemBackend(
117 profile_path,
118 MediaFileSystemBackend::MediaTaskRunner().get()),
119 test_file_util_(itunes_file_util) {}
121 storage::AsyncFileUtil* GetAsyncFileUtil(
122 storage::FileSystemType type) override {
123 if (type != storage::kFileSystemTypeItunes)
124 return NULL;
126 return test_file_util_.get();
129 private:
130 scoped_ptr<storage::AsyncFileUtil> test_file_util_;
133 class ItunesFileUtilTest : public testing::Test {
134 public:
135 ItunesFileUtilTest()
136 : io_thread_(content::BrowserThread::IO, &message_loop_) {
139 void SetUpDataProvider() {
140 ASSERT_TRUE(fake_library_dir_.CreateUniqueTempDir());
141 ASSERT_EQ(
143 base::WriteFile(
144 fake_library_dir_.path().AppendASCII(kITunesLibraryXML),
145 NULL,
146 0));
148 itunes_data_provider_.reset(
149 new TestITunesDataProvider(fake_library_dir_.path()));
152 void SetUp() override {
153 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir());
154 ImportedMediaGalleryRegistry::GetInstance()->Initialize();
156 scoped_refptr<storage::SpecialStoragePolicy> storage_policy =
157 new content::MockSpecialStoragePolicy();
159 // Initialize fake ItunesDataProvider on media task runner thread.
160 MediaFileSystemBackend::MediaTaskRunner()->PostTask(
161 FROM_HERE,
162 base::Bind(&ItunesFileUtilTest::SetUpDataProvider,
163 base::Unretained(this)));
164 base::WaitableEvent event(true, false /* initially_signalled */);
165 MediaFileSystemBackend::MediaTaskRunner()->PostTask(
166 FROM_HERE,
167 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event)));
168 event.Wait();
170 media_path_filter_.reset(new MediaPathFilter());
171 ScopedVector<storage::FileSystemBackend> additional_providers;
172 additional_providers.push_back(new TestMediaFileSystemBackend(
173 profile_dir_.path(),
174 new TestITunesFileUtil(media_path_filter_.get(),
175 itunes_data_provider_.get())));
177 file_system_context_ = new storage::FileSystemContext(
178 base::MessageLoopProxy::current().get(),
179 base::MessageLoopProxy::current().get(),
180 storage::ExternalMountPoints::CreateRefCounted().get(),
181 storage_policy.get(),
182 NULL,
183 additional_providers.Pass(),
184 std::vector<storage::URLRequestAutoMountHandler>(),
185 profile_dir_.path(),
186 content::CreateAllowFileAccessOptions());
189 protected:
190 void TestNonexistentFolder(const std::string& path_append) {
191 FileSystemOperation::FileEntryList contents;
192 FileSystemURL url = CreateURL(path_append);
193 bool completed = false;
194 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
196 ASSERT_FALSE(completed);
199 FileSystemURL CreateURL(const std::string& path) const {
200 base::FilePath virtual_path =
201 ImportedMediaGalleryRegistry::GetInstance()->ImportedRoot();
202 virtual_path = virtual_path.AppendASCII("itunes");
203 virtual_path = virtual_path.AppendASCII(path);
204 return file_system_context_->CreateCrackedFileSystemURL(
205 GURL("http://www.example.com"),
206 storage::kFileSystemTypeItunes,
207 virtual_path);
210 storage::FileSystemOperationRunner* operation_runner() const {
211 return file_system_context_->operation_runner();
214 scoped_refptr<storage::FileSystemContext> file_system_context() const {
215 return file_system_context_;
218 TestITunesDataProvider* data_provider() const {
219 return itunes_data_provider_.get();
222 private:
223 base::MessageLoop message_loop_;
224 content::TestBrowserThread io_thread_;
226 base::ScopedTempDir profile_dir_;
227 base::ScopedTempDir fake_library_dir_;
229 scoped_refptr<storage::FileSystemContext> file_system_context_;
230 scoped_ptr<MediaPathFilter> media_path_filter_;
231 scoped_ptr<TestITunesDataProvider> itunes_data_provider_;
233 DISALLOW_COPY_AND_ASSIGN(ItunesFileUtilTest);
236 TEST_F(ItunesFileUtilTest, RootContents) {
237 FileSystemOperation::FileEntryList contents;
238 FileSystemURL url = CreateURL("");
239 bool completed = false;
240 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
242 ASSERT_TRUE(completed);
243 ASSERT_EQ(2u, contents.size());
245 EXPECT_FALSE(contents.front().is_directory);
246 EXPECT_TRUE(contents.back().is_directory);
248 EXPECT_EQ(base::FilePath::FromUTF8Unsafe(kITunesLibraryXML).value(),
249 contents.front().name);
250 EXPECT_EQ(base::FilePath::FromUTF8Unsafe(kITunesMediaDir).value(),
251 contents.back().name);
254 TEST_F(ItunesFileUtilTest, ItunesMediaDirectoryContentsNoAutoAdd) {
255 data_provider()->SetProvideAutoAddDir(false);
257 FileSystemOperation::FileEntryList contents;
258 FileSystemURL url = CreateURL(kITunesMediaDir);
259 bool completed = false;
260 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
262 ASSERT_TRUE(completed);
263 ASSERT_EQ(1u, contents.size());
265 EXPECT_TRUE(contents.front().is_directory);
266 EXPECT_EQ(base::FilePath::FromUTF8Unsafe(kITunesMusicDir).value(),
267 contents.back().name);
270 TEST_F(ItunesFileUtilTest, ItunesMediaDirectoryContentsAutoAdd) {
271 data_provider()->SetProvideAutoAddDir(true);
273 FileSystemOperation::FileEntryList contents;
274 FileSystemURL url = CreateURL(kITunesMediaDir);
275 bool completed = false;
276 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
278 ASSERT_TRUE(completed);
279 ASSERT_EQ(2u, contents.size());
281 EXPECT_TRUE(contents.front().is_directory);
282 EXPECT_TRUE(contents.back().is_directory);
284 EXPECT_EQ(base::FilePath::FromUTF8Unsafe(kITunesAutoAddDir).value(),
285 contents.front().name);
286 EXPECT_EQ(base::FilePath::FromUTF8Unsafe(kITunesMusicDir).value(),
287 contents.back().name);
290 TEST_F(ItunesFileUtilTest, ItunesAutoAddDirEnumerate) {
291 data_provider()->SetProvideAutoAddDir(true);
292 ASSERT_EQ(0, base::WriteFile(
293 data_provider()->auto_add_path().AppendASCII("baz.ogg"), NULL, 0));
295 FileSystemOperation::FileEntryList contents;
296 FileSystemURL url = CreateURL(
297 std::string(kITunesMediaDir) + "/" + kITunesAutoAddDir);
298 bool completed = false;
300 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
301 ASSERT_TRUE(completed);
302 ASSERT_EQ(1u, contents.size());
303 EXPECT_FALSE(contents.front().is_directory);
304 EXPECT_EQ(base::FilePath().AppendASCII("baz.ogg").value(),
305 contents.front().name);
308 TEST_F(ItunesFileUtilTest, ItunesAutoAddDirEnumerateNested) {
309 data_provider()->SetProvideAutoAddDir(true);
310 base::FilePath nested_dir =
311 data_provider()->auto_add_path().AppendASCII("foo").AppendASCII("bar");
312 ASSERT_TRUE(base::CreateDirectory(nested_dir));
313 ASSERT_EQ(0,
314 base::WriteFile(nested_dir.AppendASCII("baz.ogg"), NULL, 0));
316 FileSystemOperation::FileEntryList contents;
317 FileSystemURL url = CreateURL(
318 std::string(kITunesMediaDir) + "/" + kITunesAutoAddDir);
319 bool completed = false;
321 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
322 ASSERT_TRUE(completed);
323 ASSERT_EQ(1u, contents.size());
324 EXPECT_TRUE(contents.front().is_directory);
325 EXPECT_EQ(base::FilePath().AppendASCII("foo").value(), contents.front().name);
327 contents.clear();
328 url = CreateURL(
329 std::string(kITunesMediaDir) + "/" + kITunesAutoAddDir + "/foo");
330 completed = false;
331 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
332 ASSERT_TRUE(completed);
333 ASSERT_EQ(1u, contents.size());
334 EXPECT_TRUE(contents.front().is_directory);
335 EXPECT_EQ(base::FilePath().AppendASCII("bar").value(), contents.front().name);
337 contents.clear();
338 url = CreateURL(
339 std::string(kITunesMediaDir) + "/" + kITunesAutoAddDir + "/foo/bar");
340 completed = false;
341 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
342 ASSERT_TRUE(completed);
343 ASSERT_EQ(1u, contents.size());
344 EXPECT_FALSE(contents.front().is_directory);
345 EXPECT_EQ(base::FilePath().AppendASCII("baz.ogg").value(),
346 contents.front().name);
349 } // namespace itunes