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 "base/base64.h"
6 #include "base/base_paths_win.h"
8 #include "base/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/path_service.h"
14 #include "base/run_loop.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/test/scoped_path_override.h"
17 #include "chrome/browser/media_galleries/fileapi/iapps_finder_impl.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/test/base/in_process_browser_test.h"
25 std::string
EncodePath(const base::FilePath
& path
) {
26 std::string
input(reinterpret_cast<const char*>(path
.value().data()),
27 path
.value().size()*2);
29 base::Base64Encode(input
, &result
);
33 void TouchFile(const base::FilePath
& file
) {
34 ASSERT_EQ(1, file_util::WriteFile(file
, " ", 1));
37 class ITunesFinderWinTest
: public InProcessBrowserTest
{
39 ITunesFinderWinTest() : test_finder_callback_called_(false) {}
41 virtual ~ITunesFinderWinTest() {}
43 virtual void SetUp() OVERRIDE
{
44 ASSERT_TRUE(app_data_dir_
.CreateUniqueTempDir());
45 ASSERT_TRUE(music_dir_
.CreateUniqueTempDir());
46 app_data_dir_override_
.reset(
47 new base::ScopedPathOverride(base::DIR_APP_DATA
, app_data_dir()));
48 music_dir_override_
.reset(
49 new base::ScopedPathOverride(chrome::DIR_USER_MUSIC
, music_dir()));
50 InProcessBrowserTest::SetUp();
53 const base::FilePath
& app_data_dir() {
54 return app_data_dir_
.path();
57 const base::FilePath
& music_dir() {
58 return music_dir_
.path();
61 void WritePrefFile(const std::string
& data
) {
62 base::FilePath pref_dir
=
63 app_data_dir().AppendASCII("Apple Computer").AppendASCII("iTunes");
64 ASSERT_TRUE(base::CreateDirectory(pref_dir
));
65 ASSERT_EQ(data
.size(),
66 file_util::WriteFile(pref_dir
.AppendASCII("iTunesPrefs.xml"),
67 data
.data(), data
.size()));
71 base::FilePath default_dir
= music_dir().AppendASCII("iTunes");
72 ASSERT_TRUE(base::CreateDirectory(default_dir
));
73 TouchFile(default_dir
.AppendASCII("iTunes Music Library.xml"));
76 void TestFindITunesLibrary() {
77 test_finder_callback_called_
= false;
80 FindITunesLibrary(base::Bind(&ITunesFinderWinTest::TestFinderCallback
,
81 base::Unretained(this), loop
.QuitClosure()));
85 bool EmptyResult() const {
86 return result_
.empty();
89 bool test_finder_callback_called() const {
90 return test_finder_callback_called_
;
94 void TestFinderCallback(const base::Closure
& quit_closure
,
95 const std::string
& result
) {
96 test_finder_callback_called_
= true;
101 scoped_ptr
<base::ScopedPathOverride
> app_data_dir_override_
;
102 scoped_ptr
<base::ScopedPathOverride
> music_dir_override_
;
103 base::ScopedTempDir app_data_dir_
;
104 base::ScopedTempDir music_dir_
;
106 bool test_finder_callback_called_
;
109 DISALLOW_COPY_AND_ASSIGN(ITunesFinderWinTest
);
112 IN_PROC_BROWSER_TEST_F(ITunesFinderWinTest
, NotFound
) {
113 TestFindITunesLibrary();
114 EXPECT_TRUE(test_finder_callback_called());
115 EXPECT_TRUE(EmptyResult());
118 IN_PROC_BROWSER_TEST_F(ITunesFinderWinTest
, DefaultLocation
) {
120 TestFindITunesLibrary();
121 EXPECT_TRUE(test_finder_callback_called());
122 EXPECT_FALSE(EmptyResult());
125 IN_PROC_BROWSER_TEST_F(ITunesFinderWinTest
, CustomLocation
) {
126 base::FilePath library_xml
= music_dir().AppendASCII("library.xml");
127 TouchFile(library_xml
);
128 std::string xml
= base::StringPrintf(
131 " <key>User Preferences</key>"
133 " <key>iTunes Library XML Location:1</key>"
137 "</plist>", EncodePath(library_xml
).c_str());
139 TestFindITunesLibrary();
140 EXPECT_TRUE(test_finder_callback_called());
141 EXPECT_FALSE(EmptyResult());
144 IN_PROC_BROWSER_TEST_F(ITunesFinderWinTest
, BadCustomLocation
) {
146 base::FilePath library_xml
= music_dir().AppendASCII("library.xml");
147 std::string xml
= base::StringPrintf(
150 " <key>User Preferences</key>"
152 " <key>iTunes Library XML Location:1</key>"
156 "</plist>", EncodePath(library_xml
).c_str());
158 TestFindITunesLibrary();
159 EXPECT_TRUE(test_finder_callback_called());
160 EXPECT_TRUE(EmptyResult());
161 TouchFile(library_xml
);
163 // User Preferences dictionary at the wrong level.
164 xml
= base::StringPrintf(
166 " <key>User Preferences</key>"
168 " <key>iTunes Library XML Location:1</key>"
171 "</plist>", EncodePath(library_xml
).c_str());
173 TestFindITunesLibrary();
174 EXPECT_TRUE(test_finder_callback_called());
175 EXPECT_TRUE(EmptyResult());
177 // Library location at the wrong scope.
178 xml
= base::StringPrintf(
181 " <key>User Preferences</key>"
183 " <key>iTunes Library XML Location:1</key>"
186 "</plist>", EncodePath(library_xml
).c_str());
188 TestFindITunesLibrary();
189 EXPECT_TRUE(test_finder_callback_called());
190 EXPECT_TRUE(EmptyResult());