Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / media_galleries / fileapi / iapps_finder_impl_win_browsertest.cc
blob9155830c6dd2516f374870d32d9a066713edb05d
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"
7 #include "base/bind.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"
20 namespace iapps {
22 namespace {
24 std::string EncodePath(const base::FilePath& path) {
25 std::string input(reinterpret_cast<const char*>(path.value().data()),
26 path.value().size()*2);
27 std::string result;
28 base::Base64Encode(input, &result);
29 return result;
32 void TouchFile(const base::FilePath& file) {
33 ASSERT_EQ(1, base::WriteFile(file, " ", 1));
36 class ITunesFinderWinTest : public InProcessBrowserTest {
37 public:
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()));
69 void TouchDefault() {
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;
77 result_.clear();
78 base::RunLoop loop;
79 FindITunesLibrary(base::Bind(&ITunesFinderWinTest::TestFinderCallback,
80 base::Unretained(this), loop.QuitClosure()));
81 loop.Run();
84 bool EmptyResult() const {
85 return result_.empty();
88 bool test_finder_callback_called() const {
89 return test_finder_callback_called_;
92 private:
93 void TestFinderCallback(const base::Closure& quit_closure,
94 const std::string& result) {
95 test_finder_callback_called_ = true;
96 result_ = result;
97 quit_closure.Run();
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_;
106 std::string result_;
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) {
118 TouchDefault();
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(
128 "<plist>"
129 " <dict>"
130 " <key>User Preferences</key>"
131 " <dict>"
132 " <key>iTunes Library XML Location:1</key>"
133 " <data>%s</data>"
134 " </dict>"
135 " </dict>"
136 "</plist>", EncodePath(library_xml).c_str());
137 WritePrefFile(xml);
138 TestFindITunesLibrary();
139 EXPECT_TRUE(test_finder_callback_called());
140 EXPECT_FALSE(EmptyResult());
143 IN_PROC_BROWSER_TEST_F(ITunesFinderWinTest, BadCustomLocation) {
144 // Missing file.
145 base::FilePath library_xml = music_dir().AppendASCII("library.xml");
146 std::string xml = base::StringPrintf(
147 "<plist>"
148 " <dict>"
149 " <key>User Preferences</key>"
150 " <dict>"
151 " <key>iTunes Library XML Location:1</key>"
152 " <data>%s</data>"
153 " </dict>"
154 " </dict>"
155 "</plist>", EncodePath(library_xml).c_str());
156 WritePrefFile(xml);
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(
164 "<plist>"
165 " <key>User Preferences</key>"
166 " <dict>"
167 " <key>iTunes Library XML Location:1</key>"
168 " <data>%s</data>"
169 " </dict>"
170 "</plist>", EncodePath(library_xml).c_str());
171 WritePrefFile(xml);
172 TestFindITunesLibrary();
173 EXPECT_TRUE(test_finder_callback_called());
174 EXPECT_TRUE(EmptyResult());
176 // Library location at the wrong scope.
177 xml = base::StringPrintf(
178 "<plist>"
179 " <dict>"
180 " <key>User Preferences</key>"
181 " <dict/>"
182 " <key>iTunes Library XML Location:1</key>"
183 " <data>%s</data>"
184 " </dict>"
185 "</plist>", EncodePath(library_xml).c_str());
186 WritePrefFile(xml);
187 TestFindITunesLibrary();
188 EXPECT_TRUE(test_finder_callback_called());
189 EXPECT_TRUE(EmptyResult());
192 } // namespace
194 } // namespace iapps