Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / media_galleries / fileapi / itunes_file_util_unittest.cc
blob4a19776a1fbd4f983117cf0fc98d10d4d8d549f2
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/single_thread_task_runner.h"
15 #include "base/synchronization/waitable_event.h"
16 #include "base/thread_task_runner_handle.h"
17 #include "base/time/time.h"
18 #include "chrome/browser/media_galleries/fileapi/itunes_data_provider.h"
19 #include "chrome/browser/media_galleries/fileapi/itunes_file_util.h"
20 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h"
21 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h"
22 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/test/mock_special_storage_policy.h"
25 #include "content/public/test/test_browser_thread.h"
26 #include "content/public/test/test_file_system_options.h"
27 #include "storage/browser/fileapi/async_file_util.h"
28 #include "storage/browser/fileapi/external_mount_points.h"
29 #include "storage/browser/fileapi/file_system_context.h"
30 #include "storage/browser/fileapi/file_system_operation_context.h"
31 #include "storage/browser/fileapi/file_system_operation_runner.h"
32 #include "testing/gtest/include/gtest/gtest.h"
34 using storage::FileSystemOperationContext;
35 using storage::FileSystemOperation;
36 using storage::FileSystemURL;
38 namespace itunes {
40 namespace {
42 void ReadDirectoryTestHelperCallback(
43 base::RunLoop* run_loop,
44 FileSystemOperation::FileEntryList* contents,
45 bool* completed, 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();
54 void ReadDirectoryTestHelper(storage::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();
67 } // namespace
69 class TestITunesDataProvider : public ITunesDataProvider {
70 public:
71 explicit TestITunesDataProvider(const base::FilePath& fake_library_path)
72 : ITunesDataProvider(fake_library_path) {
73 EXPECT_TRUE(fake_auto_add_dir_.CreateUniqueTempDir());
76 ~TestITunesDataProvider() override {}
78 void RefreshData(const ReadyCallback& ready_callback) override {
79 ready_callback.Run(true /* success */);
82 const base::FilePath& auto_add_path() const override {
83 return fake_auto_add_dir_.path();
86 void SetProvideAutoAddDir(bool provide_auto_add_dir) {
87 if (provide_auto_add_dir) {
88 if (!fake_auto_add_dir_.IsValid())
89 ASSERT_TRUE(fake_auto_add_dir_.CreateUniqueTempDir());
90 } else {
91 ASSERT_TRUE(fake_auto_add_dir_.Delete());
95 private:
96 base::ScopedTempDir fake_auto_add_dir_;
99 class TestITunesFileUtil : public ITunesFileUtil {
100 public:
101 explicit TestITunesFileUtil(MediaPathFilter* media_path_filter,
102 ITunesDataProvider* data_provider)
103 : ITunesFileUtil(media_path_filter),
104 data_provider_(data_provider) {
106 ~TestITunesFileUtil() override {}
108 private:
109 ITunesDataProvider* GetDataProvider() override { return data_provider_; }
111 ITunesDataProvider* data_provider_;
114 class TestMediaFileSystemBackend : public MediaFileSystemBackend {
115 public:
116 TestMediaFileSystemBackend(const base::FilePath& profile_path,
117 ITunesFileUtil* itunes_file_util)
118 : MediaFileSystemBackend(
119 profile_path,
120 MediaFileSystemBackend::MediaTaskRunner().get()),
121 test_file_util_(itunes_file_util) {}
123 storage::AsyncFileUtil* GetAsyncFileUtil(
124 storage::FileSystemType type) override {
125 if (type != storage::kFileSystemTypeItunes)
126 return NULL;
128 return test_file_util_.get();
131 private:
132 scoped_ptr<storage::AsyncFileUtil> test_file_util_;
135 class ItunesFileUtilTest : public testing::Test {
136 public:
137 ItunesFileUtilTest()
138 : io_thread_(content::BrowserThread::IO, &message_loop_) {
141 void SetUpDataProvider() {
142 ASSERT_TRUE(fake_library_dir_.CreateUniqueTempDir());
143 ASSERT_EQ(
145 base::WriteFile(
146 fake_library_dir_.path().AppendASCII(kITunesLibraryXML),
147 NULL,
148 0));
150 itunes_data_provider_.reset(
151 new TestITunesDataProvider(fake_library_dir_.path()));
154 void SetUp() override {
155 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir());
156 ImportedMediaGalleryRegistry::GetInstance()->Initialize();
158 scoped_refptr<storage::SpecialStoragePolicy> storage_policy =
159 new content::MockSpecialStoragePolicy();
161 // Initialize fake ItunesDataProvider on media task runner thread.
162 MediaFileSystemBackend::MediaTaskRunner()->PostTask(
163 FROM_HERE,
164 base::Bind(&ItunesFileUtilTest::SetUpDataProvider,
165 base::Unretained(this)));
166 base::WaitableEvent event(true, false /* initially_signalled */);
167 MediaFileSystemBackend::MediaTaskRunner()->PostTask(
168 FROM_HERE,
169 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event)));
170 event.Wait();
172 media_path_filter_.reset(new MediaPathFilter());
173 ScopedVector<storage::FileSystemBackend> additional_providers;
174 additional_providers.push_back(new TestMediaFileSystemBackend(
175 profile_dir_.path(),
176 new TestITunesFileUtil(media_path_filter_.get(),
177 itunes_data_provider_.get())));
179 file_system_context_ = new storage::FileSystemContext(
180 base::ThreadTaskRunnerHandle::Get().get(),
181 base::ThreadTaskRunnerHandle::Get().get(),
182 storage::ExternalMountPoints::CreateRefCounted().get(),
183 storage_policy.get(),
184 NULL,
185 additional_providers.Pass(),
186 std::vector<storage::URLRequestAutoMountHandler>(),
187 profile_dir_.path(),
188 content::CreateAllowFileAccessOptions());
191 protected:
192 void TestNonexistentFolder(const std::string& path_append) {
193 FileSystemOperation::FileEntryList contents;
194 FileSystemURL url = CreateURL(path_append);
195 bool completed = false;
196 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
198 ASSERT_FALSE(completed);
201 FileSystemURL CreateURL(const std::string& path) const {
202 base::FilePath virtual_path =
203 ImportedMediaGalleryRegistry::GetInstance()->ImportedRoot();
204 virtual_path = virtual_path.AppendASCII("itunes");
205 virtual_path = virtual_path.AppendASCII(path);
206 return file_system_context_->CreateCrackedFileSystemURL(
207 GURL("http://www.example.com"),
208 storage::kFileSystemTypeItunes,
209 virtual_path);
212 storage::FileSystemOperationRunner* operation_runner() const {
213 return file_system_context_->operation_runner();
216 scoped_refptr<storage::FileSystemContext> file_system_context() const {
217 return file_system_context_;
220 TestITunesDataProvider* data_provider() const {
221 return itunes_data_provider_.get();
224 private:
225 base::MessageLoop message_loop_;
226 content::TestBrowserThread io_thread_;
228 base::ScopedTempDir profile_dir_;
229 base::ScopedTempDir fake_library_dir_;
231 scoped_refptr<storage::FileSystemContext> file_system_context_;
232 scoped_ptr<MediaPathFilter> media_path_filter_;
233 scoped_ptr<TestITunesDataProvider> itunes_data_provider_;
235 DISALLOW_COPY_AND_ASSIGN(ItunesFileUtilTest);
238 TEST_F(ItunesFileUtilTest, RootContents) {
239 FileSystemOperation::FileEntryList contents;
240 FileSystemURL url = CreateURL("");
241 bool completed = false;
242 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
244 ASSERT_TRUE(completed);
245 ASSERT_EQ(2u, contents.size());
247 EXPECT_FALSE(contents.front().is_directory);
248 EXPECT_TRUE(contents.back().is_directory);
250 EXPECT_EQ(base::FilePath::FromUTF8Unsafe(kITunesLibraryXML).value(),
251 contents.front().name);
252 EXPECT_EQ(base::FilePath::FromUTF8Unsafe(kITunesMediaDir).value(),
253 contents.back().name);
256 TEST_F(ItunesFileUtilTest, ItunesMediaDirectoryContentsNoAutoAdd) {
257 data_provider()->SetProvideAutoAddDir(false);
259 FileSystemOperation::FileEntryList contents;
260 FileSystemURL url = CreateURL(kITunesMediaDir);
261 bool completed = false;
262 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
264 ASSERT_TRUE(completed);
265 ASSERT_EQ(1u, contents.size());
267 EXPECT_TRUE(contents.front().is_directory);
268 EXPECT_EQ(base::FilePath::FromUTF8Unsafe(kITunesMusicDir).value(),
269 contents.back().name);
272 TEST_F(ItunesFileUtilTest, ItunesMediaDirectoryContentsAutoAdd) {
273 data_provider()->SetProvideAutoAddDir(true);
275 FileSystemOperation::FileEntryList contents;
276 FileSystemURL url = CreateURL(kITunesMediaDir);
277 bool completed = false;
278 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
280 ASSERT_TRUE(completed);
281 ASSERT_EQ(2u, contents.size());
283 EXPECT_TRUE(contents.front().is_directory);
284 EXPECT_TRUE(contents.back().is_directory);
286 EXPECT_EQ(base::FilePath::FromUTF8Unsafe(kITunesAutoAddDir).value(),
287 contents.front().name);
288 EXPECT_EQ(base::FilePath::FromUTF8Unsafe(kITunesMusicDir).value(),
289 contents.back().name);
292 TEST_F(ItunesFileUtilTest, ItunesAutoAddDirEnumerate) {
293 data_provider()->SetProvideAutoAddDir(true);
294 ASSERT_EQ(0, base::WriteFile(
295 data_provider()->auto_add_path().AppendASCII("baz.ogg"), NULL, 0));
297 FileSystemOperation::FileEntryList contents;
298 FileSystemURL url = CreateURL(
299 std::string(kITunesMediaDir) + "/" + kITunesAutoAddDir);
300 bool completed = false;
302 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
303 ASSERT_TRUE(completed);
304 ASSERT_EQ(1u, contents.size());
305 EXPECT_FALSE(contents.front().is_directory);
306 EXPECT_EQ(base::FilePath().AppendASCII("baz.ogg").value(),
307 contents.front().name);
310 TEST_F(ItunesFileUtilTest, ItunesAutoAddDirEnumerateNested) {
311 data_provider()->SetProvideAutoAddDir(true);
312 base::FilePath nested_dir =
313 data_provider()->auto_add_path().AppendASCII("foo").AppendASCII("bar");
314 ASSERT_TRUE(base::CreateDirectory(nested_dir));
315 ASSERT_EQ(0,
316 base::WriteFile(nested_dir.AppendASCII("baz.ogg"), NULL, 0));
318 FileSystemOperation::FileEntryList contents;
319 FileSystemURL url = CreateURL(
320 std::string(kITunesMediaDir) + "/" + kITunesAutoAddDir);
321 bool completed = false;
323 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
324 ASSERT_TRUE(completed);
325 ASSERT_EQ(1u, contents.size());
326 EXPECT_TRUE(contents.front().is_directory);
327 EXPECT_EQ(base::FilePath().AppendASCII("foo").value(), contents.front().name);
329 contents.clear();
330 url = CreateURL(
331 std::string(kITunesMediaDir) + "/" + kITunesAutoAddDir + "/foo");
332 completed = false;
333 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
334 ASSERT_TRUE(completed);
335 ASSERT_EQ(1u, contents.size());
336 EXPECT_TRUE(contents.front().is_directory);
337 EXPECT_EQ(base::FilePath().AppendASCII("bar").value(), contents.front().name);
339 contents.clear();
340 url = CreateURL(
341 std::string(kITunesMediaDir) + "/" + kITunesAutoAddDir + "/foo/bar");
342 completed = false;
343 ReadDirectoryTestHelper(operation_runner(), url, &contents, &completed);
344 ASSERT_TRUE(completed);
345 ASSERT_EQ(1u, contents.size());
346 EXPECT_FALSE(contents.front().is_directory);
347 EXPECT_EQ(base::FilePath().AppendASCII("baz.ogg").value(),
348 contents.front().name);
351 } // namespace itunes