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/files/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/run_loop.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/test/scoped_path_override.h"
16 #include "chrome/browser/media_galleries/fileapi/iapps_finder_impl.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/test/base/in_process_browser_test.h"
24 std::string
EncodePath(const base::FilePath
& path
) {
25 std::string
input(reinterpret_cast<const char*>(path
.value().data()),
26 path
.value().size()*2);
28 base::Base64Encode(input
, &result
);
32 void TouchFile(const base::FilePath
& file
) {
33 ASSERT_EQ(1, base::WriteFile(file
, " ", 1));
36 class ITunesFinderWinTest
: public InProcessBrowserTest
{
38 ITunesFinderWinTest() : test_finder_callback_called_(false) {}
40 ~ITunesFinderWinTest() override
{}
42 void SetUp() override
{
43 ASSERT_TRUE(app_data_dir_
.CreateUniqueTempDir());
44 ASSERT_TRUE(music_dir_
.CreateUniqueTempDir());
45 app_data_dir_override_
.reset(
46 new base::ScopedPathOverride(base::DIR_APP_DATA
, app_data_dir()));
47 music_dir_override_
.reset(
48 new base::ScopedPathOverride(chrome::DIR_USER_MUSIC
, music_dir()));
49 InProcessBrowserTest::SetUp();
52 const base::FilePath
& app_data_dir() {
53 return app_data_dir_
.path();
56 const base::FilePath
& music_dir() {
57 return music_dir_
.path();
60 void WritePrefFile(const std::string
& data
) {
61 base::FilePath pref_dir
=
62 app_data_dir().AppendASCII("Apple Computer").AppendASCII("iTunes");
63 ASSERT_TRUE(base::CreateDirectory(pref_dir
));
64 ASSERT_EQ(data
.size(),
65 base::WriteFile(pref_dir
.AppendASCII("iTunesPrefs.xml"),
66 data
.data(), data
.size()));
70 base::FilePath default_dir
= music_dir().AppendASCII("iTunes");
71 ASSERT_TRUE(base::CreateDirectory(default_dir
));
72 TouchFile(default_dir
.AppendASCII("iTunes Music Library.xml"));
75 void TestFindITunesLibrary() {
76 test_finder_callback_called_
= false;
79 FindITunesLibrary(base::Bind(&ITunesFinderWinTest::TestFinderCallback
,
80 base::Unretained(this), loop
.QuitClosure()));
84 bool EmptyResult() const {
85 return result_
.empty();
88 bool test_finder_callback_called() const {
89 return test_finder_callback_called_
;
93 void TestFinderCallback(const base::Closure
& quit_closure
,
94 const std::string
& result
) {
95 test_finder_callback_called_
= true;
100 scoped_ptr
<base::ScopedPathOverride
> app_data_dir_override_
;
101 scoped_ptr
<base::ScopedPathOverride
> music_dir_override_
;
102 base::ScopedTempDir app_data_dir_
;
103 base::ScopedTempDir music_dir_
;
105 bool test_finder_callback_called_
;
108 DISALLOW_COPY_AND_ASSIGN(ITunesFinderWinTest
);
111 IN_PROC_BROWSER_TEST_F(ITunesFinderWinTest
, NotFound
) {
112 TestFindITunesLibrary();
113 EXPECT_TRUE(test_finder_callback_called());
114 EXPECT_TRUE(EmptyResult());
117 IN_PROC_BROWSER_TEST_F(ITunesFinderWinTest
, DefaultLocation
) {
119 TestFindITunesLibrary();
120 EXPECT_TRUE(test_finder_callback_called());
121 EXPECT_FALSE(EmptyResult());
124 IN_PROC_BROWSER_TEST_F(ITunesFinderWinTest
, CustomLocation
) {
125 base::FilePath library_xml
= music_dir().AppendASCII("library.xml");
126 TouchFile(library_xml
);
127 std::string xml
= base::StringPrintf(
130 " <key>User Preferences</key>"
132 " <key>iTunes Library XML Location:1</key>"
136 "</plist>", EncodePath(library_xml
).c_str());
138 TestFindITunesLibrary();
139 EXPECT_TRUE(test_finder_callback_called());
140 EXPECT_FALSE(EmptyResult());
143 IN_PROC_BROWSER_TEST_F(ITunesFinderWinTest
, BadCustomLocation
) {
145 base::FilePath library_xml
= music_dir().AppendASCII("library.xml");
146 std::string xml
= base::StringPrintf(
149 " <key>User Preferences</key>"
151 " <key>iTunes Library XML Location:1</key>"
155 "</plist>", EncodePath(library_xml
).c_str());
157 TestFindITunesLibrary();
158 EXPECT_TRUE(test_finder_callback_called());
159 EXPECT_TRUE(EmptyResult());
160 TouchFile(library_xml
);
162 // User Preferences dictionary at the wrong level.
163 xml
= base::StringPrintf(
165 " <key>User Preferences</key>"
167 " <key>iTunes Library XML Location:1</key>"
170 "</plist>", EncodePath(library_xml
).c_str());
172 TestFindITunesLibrary();
173 EXPECT_TRUE(test_finder_callback_called());
174 EXPECT_TRUE(EmptyResult());
176 // Library location at the wrong scope.
177 xml
= base::StringPrintf(
180 " <key>User Preferences</key>"
182 " <key>iTunes Library XML Location:1</key>"
185 "</plist>", EncodePath(library_xml
).c_str());
187 TestFindITunesLibrary();
188 EXPECT_TRUE(test_finder_callback_called());
189 EXPECT_TRUE(EmptyResult());