1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/base64.h"
#include "base/base_paths_win.h"
#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_path_override.h"
#include "chrome/browser/media_galleries/fileapi/iapps_finder_impl.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/in_process_browser_test.h"
namespace iapps {
namespace {
std::string EncodePath(const base::FilePath& path) {
std::string input(reinterpret_cast<const char*>(path.value().data()),
path.value().size()*2);
std::string result;
base::Base64Encode(input, &result);
return result;
}
void TouchFile(const base::FilePath& file) {
ASSERT_EQ(1, base::WriteFile(file, " ", 1));
}
class ITunesFinderWinTest : public InProcessBrowserTest {
public:
ITunesFinderWinTest() : test_finder_callback_called_(false) {}
virtual ~ITunesFinderWinTest() {}
virtual void SetUp() override {
ASSERT_TRUE(app_data_dir_.CreateUniqueTempDir());
ASSERT_TRUE(music_dir_.CreateUniqueTempDir());
app_data_dir_override_.reset(
new base::ScopedPathOverride(base::DIR_APP_DATA, app_data_dir()));
music_dir_override_.reset(
new base::ScopedPathOverride(chrome::DIR_USER_MUSIC, music_dir()));
InProcessBrowserTest::SetUp();
}
const base::FilePath& app_data_dir() {
return app_data_dir_.path();
}
const base::FilePath& music_dir() {
return music_dir_.path();
}
void WritePrefFile(const std::string& data) {
base::FilePath pref_dir =
app_data_dir().AppendASCII("Apple Computer").AppendASCII("iTunes");
ASSERT_TRUE(base::CreateDirectory(pref_dir));
ASSERT_EQ(data.size(),
base::WriteFile(pref_dir.AppendASCII("iTunesPrefs.xml"),
data.data(), data.size()));
}
void TouchDefault() {
base::FilePath default_dir = music_dir().AppendASCII("iTunes");
ASSERT_TRUE(base::CreateDirectory(default_dir));
TouchFile(default_dir.AppendASCII("iTunes Music Library.xml"));
}
void TestFindITunesLibrary() {
test_finder_callback_called_ = false;
result_.clear();
base::RunLoop loop;
FindITunesLibrary(base::Bind(&ITunesFinderWinTest::TestFinderCallback,
base::Unretained(this), loop.QuitClosure()));
loop.Run();
}
bool EmptyResult() const {
return result_.empty();
}
bool test_finder_callback_called() const {
return test_finder_callback_called_;
}
private:
void TestFinderCallback(const base::Closure& quit_closure,
const std::string& result) {
test_finder_callback_called_ = true;
result_ = result;
quit_closure.Run();
}
scoped_ptr<base::ScopedPathOverride> app_data_dir_override_;
scoped_ptr<base::ScopedPathOverride> music_dir_override_;
base::ScopedTempDir app_data_dir_;
base::ScopedTempDir music_dir_;
bool test_finder_callback_called_;
std::string result_;
DISALLOW_COPY_AND_ASSIGN(ITunesFinderWinTest);
};
IN_PROC_BROWSER_TEST_F(ITunesFinderWinTest, NotFound) {
TestFindITunesLibrary();
EXPECT_TRUE(test_finder_callback_called());
EXPECT_TRUE(EmptyResult());
}
IN_PROC_BROWSER_TEST_F(ITunesFinderWinTest, DefaultLocation) {
TouchDefault();
TestFindITunesLibrary();
EXPECT_TRUE(test_finder_callback_called());
EXPECT_FALSE(EmptyResult());
}
IN_PROC_BROWSER_TEST_F(ITunesFinderWinTest, CustomLocation) {
base::FilePath library_xml = music_dir().AppendASCII("library.xml");
TouchFile(library_xml);
std::string xml = base::StringPrintf(
"<plist>"
" <dict>"
" <key>User Preferences</key>"
" <dict>"
" <key>iTunes Library XML Location:1</key>"
" <data>%s</data>"
" </dict>"
" </dict>"
"</plist>", EncodePath(library_xml).c_str());
WritePrefFile(xml);
TestFindITunesLibrary();
EXPECT_TRUE(test_finder_callback_called());
EXPECT_FALSE(EmptyResult());
}
IN_PROC_BROWSER_TEST_F(ITunesFinderWinTest, BadCustomLocation) {
// Missing file.
base::FilePath library_xml = music_dir().AppendASCII("library.xml");
std::string xml = base::StringPrintf(
"<plist>"
" <dict>"
" <key>User Preferences</key>"
" <dict>"
" <key>iTunes Library XML Location:1</key>"
" <data>%s</data>"
" </dict>"
" </dict>"
"</plist>", EncodePath(library_xml).c_str());
WritePrefFile(xml);
TestFindITunesLibrary();
EXPECT_TRUE(test_finder_callback_called());
EXPECT_TRUE(EmptyResult());
TouchFile(library_xml);
// User Preferences dictionary at the wrong level.
xml = base::StringPrintf(
"<plist>"
" <key>User Preferences</key>"
" <dict>"
" <key>iTunes Library XML Location:1</key>"
" <data>%s</data>"
" </dict>"
"</plist>", EncodePath(library_xml).c_str());
WritePrefFile(xml);
TestFindITunesLibrary();
EXPECT_TRUE(test_finder_callback_called());
EXPECT_TRUE(EmptyResult());
// Library location at the wrong scope.
xml = base::StringPrintf(
"<plist>"
" <dict>"
" <key>User Preferences</key>"
" <dict/>"
" <key>iTunes Library XML Location:1</key>"
" <data>%s</data>"
" </dict>"
"</plist>", EncodePath(library_xml).c_str());
WritePrefFile(xml);
TestFindITunesLibrary();
EXPECT_TRUE(test_finder_callback_called());
EXPECT_TRUE(EmptyResult());
}
} // namespace
} // namespace iapps
|