Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / media_galleries / fileapi / iapps_finder_impl_win_browsertest.cc
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.
4
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/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"
20
21 namespace iapps {
22
23 namespace {
24
25 std::string EncodePath(const base::FilePath& path) {
26   std::string input(reinterpret_cast<const char*>(path.value().data()),
27                                                   path.value().size()*2);
28   std::string result;
29   base::Base64Encode(input, &result);
30   return result;
31 }
32
33 void TouchFile(const base::FilePath& file) {
34   ASSERT_EQ(1, base::WriteFile(file, " ", 1));
35 }
36
37 class ITunesFinderWinTest : public InProcessBrowserTest {
38  public:
39   ITunesFinderWinTest() : test_finder_callback_called_(false) {}
40
41   virtual ~ITunesFinderWinTest() {}
42
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();
51   }
52
53   const base::FilePath& app_data_dir() {
54     return app_data_dir_.path();
55   }
56
57   const base::FilePath& music_dir() {
58     return music_dir_.path();
59   }
60
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               base::WriteFile(pref_dir.AppendASCII("iTunesPrefs.xml"),
67                               data.data(), data.size()));
68   }
69
70   void TouchDefault() {
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"));
74   }
75
76   void TestFindITunesLibrary() {
77     test_finder_callback_called_ = false;
78     result_.clear();
79     base::RunLoop loop;
80     FindITunesLibrary(base::Bind(&ITunesFinderWinTest::TestFinderCallback,
81                                  base::Unretained(this), loop.QuitClosure()));
82     loop.Run();
83   }
84
85   bool EmptyResult() const {
86     return result_.empty();
87   }
88
89   bool test_finder_callback_called() const {
90     return test_finder_callback_called_;
91   }
92
93  private:
94   void TestFinderCallback(const base::Closure& quit_closure,
95                           const std::string& result) {
96     test_finder_callback_called_ = true;
97     result_ = result;
98     quit_closure.Run();
99   }
100
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_;
105
106   bool test_finder_callback_called_;
107   std::string result_;
108
109   DISALLOW_COPY_AND_ASSIGN(ITunesFinderWinTest);
110 };
111
112 IN_PROC_BROWSER_TEST_F(ITunesFinderWinTest, NotFound) {
113   TestFindITunesLibrary();
114   EXPECT_TRUE(test_finder_callback_called());
115   EXPECT_TRUE(EmptyResult());
116 }
117
118 IN_PROC_BROWSER_TEST_F(ITunesFinderWinTest, DefaultLocation) {
119   TouchDefault();
120   TestFindITunesLibrary();
121   EXPECT_TRUE(test_finder_callback_called());
122   EXPECT_FALSE(EmptyResult());
123 }
124
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(
129       "<plist>"
130       "  <dict>"
131       "    <key>User Preferences</key>"
132       "    <dict>"
133       "      <key>iTunes Library XML Location:1</key>"
134       "      <data>%s</data>"
135       "    </dict>"
136       "  </dict>"
137       "</plist>", EncodePath(library_xml).c_str());
138   WritePrefFile(xml);
139   TestFindITunesLibrary();
140   EXPECT_TRUE(test_finder_callback_called());
141   EXPECT_FALSE(EmptyResult());
142 }
143
144 IN_PROC_BROWSER_TEST_F(ITunesFinderWinTest, BadCustomLocation) {
145   // Missing file.
146   base::FilePath library_xml = music_dir().AppendASCII("library.xml");
147   std::string xml = base::StringPrintf(
148       "<plist>"
149       "  <dict>"
150       "    <key>User Preferences</key>"
151       "    <dict>"
152       "      <key>iTunes Library XML Location:1</key>"
153       "      <data>%s</data>"
154       "    </dict>"
155       "  </dict>"
156       "</plist>", EncodePath(library_xml).c_str());
157   WritePrefFile(xml);
158   TestFindITunesLibrary();
159   EXPECT_TRUE(test_finder_callback_called());
160   EXPECT_TRUE(EmptyResult());
161   TouchFile(library_xml);
162
163   // User Preferences dictionary at the wrong level.
164   xml = base::StringPrintf(
165       "<plist>"
166       "    <key>User Preferences</key>"
167       "    <dict>"
168       "      <key>iTunes Library XML Location:1</key>"
169       "      <data>%s</data>"
170       "    </dict>"
171       "</plist>", EncodePath(library_xml).c_str());
172   WritePrefFile(xml);
173   TestFindITunesLibrary();
174   EXPECT_TRUE(test_finder_callback_called());
175   EXPECT_TRUE(EmptyResult());
176
177   // Library location at the wrong scope.
178   xml = base::StringPrintf(
179       "<plist>"
180       "  <dict>"
181       "    <key>User Preferences</key>"
182       "    <dict/>"
183       "    <key>iTunes Library XML Location:1</key>"
184       "    <data>%s</data>"
185       "  </dict>"
186       "</plist>", EncodePath(library_xml).c_str());
187   WritePrefFile(xml);
188   TestFindITunesLibrary();
189   EXPECT_TRUE(test_finder_callback_called());
190   EXPECT_TRUE(EmptyResult());
191 }
192
193 }  // namespace
194
195 }  // namespace iapps