Home | History | Annotate | Download | only in fileapi
      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/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/itunes_finder_win.h"
     18 #include "chrome/common/chrome_paths.h"
     19 #include "chrome/test/base/in_process_browser_test.h"
     20 
     21 namespace itunes {
     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, file_util::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(file_util::CreateDirectory(pref_dir));
     65     ASSERT_EQ(data.size(),
     66               file_util::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(file_util::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     ITunesFinder::FindITunesLibrary(
     81         base::Bind(&ITunesFinderWinTest::TestFinderCallback,
     82                    base::Unretained(this),
     83                    loop.QuitClosure()));
     84     loop.Run();
     85   }
     86 
     87   bool EmptyResult() const {
     88     return result_.empty();
     89   }
     90 
     91   bool test_finder_callback_called() const {
     92     return test_finder_callback_called_;
     93   }
     94 
     95  private:
     96   void TestFinderCallback(const base::Closure& quit_closure,
     97                           const std::string& result) {
     98     test_finder_callback_called_ = true;
     99     result_ = result;
    100     quit_closure.Run();
    101   }
    102 
    103   scoped_ptr<base::ScopedPathOverride> app_data_dir_override_;
    104   scoped_ptr<base::ScopedPathOverride> music_dir_override_;
    105   base::ScopedTempDir app_data_dir_;
    106   base::ScopedTempDir music_dir_;
    107 
    108   bool test_finder_callback_called_;
    109   std::string result_;
    110 
    111   DISALLOW_COPY_AND_ASSIGN(ITunesFinderWinTest);
    112 };
    113 
    114 IN_PROC_BROWSER_TEST_F(ITunesFinderWinTest, NotFound) {
    115   TestFindITunesLibrary();
    116   EXPECT_TRUE(test_finder_callback_called());
    117   EXPECT_TRUE(EmptyResult());
    118 }
    119 
    120 IN_PROC_BROWSER_TEST_F(ITunesFinderWinTest, DefaultLocation) {
    121   TouchDefault();
    122   TestFindITunesLibrary();
    123   EXPECT_TRUE(test_finder_callback_called());
    124   EXPECT_FALSE(EmptyResult());
    125 }
    126 
    127 IN_PROC_BROWSER_TEST_F(ITunesFinderWinTest, CustomLocation) {
    128   base::FilePath library_xml = music_dir().AppendASCII("library.xml");
    129   TouchFile(library_xml);
    130   std::string xml = base::StringPrintf(
    131       "<plist>"
    132       "  <dict>"
    133       "    <key>User Preferences</key>"
    134       "    <dict>"
    135       "      <key>iTunes Library XML Location:1</key>"
    136       "      <data>%s</data>"
    137       "    </dict>"
    138       "  </dict>"
    139       "</plist>", EncodePath(library_xml).c_str());
    140   WritePrefFile(xml);
    141   TestFindITunesLibrary();
    142   EXPECT_TRUE(test_finder_callback_called());
    143   EXPECT_FALSE(EmptyResult());
    144 }
    145 
    146 IN_PROC_BROWSER_TEST_F(ITunesFinderWinTest, BadCustomLocation) {
    147   // Missing file.
    148   base::FilePath library_xml = music_dir().AppendASCII("library.xml");
    149   std::string xml = base::StringPrintf(
    150       "<plist>"
    151       "  <dict>"
    152       "    <key>User Preferences</key>"
    153       "    <dict>"
    154       "      <key>iTunes Library XML Location:1</key>"
    155       "      <data>%s</data>"
    156       "    </dict>"
    157       "  </dict>"
    158       "</plist>", EncodePath(library_xml).c_str());
    159   WritePrefFile(xml);
    160   TestFindITunesLibrary();
    161   EXPECT_TRUE(test_finder_callback_called());
    162   EXPECT_TRUE(EmptyResult());
    163   TouchFile(library_xml);
    164 
    165   // User Preferences dictionary at the wrong level.
    166   xml = base::StringPrintf(
    167       "<plist>"
    168       "    <key>User Preferences</key>"
    169       "    <dict>"
    170       "      <key>iTunes Library XML Location:1</key>"
    171       "      <data>%s</data>"
    172       "    </dict>"
    173       "</plist>", EncodePath(library_xml).c_str());
    174   WritePrefFile(xml);
    175   TestFindITunesLibrary();
    176   EXPECT_TRUE(test_finder_callback_called());
    177   EXPECT_TRUE(EmptyResult());
    178 
    179   // Library location at the wrong scope.
    180   xml = base::StringPrintf(
    181       "<plist>"
    182       "  <dict>"
    183       "    <key>User Preferences</key>"
    184       "    <dict/>"
    185       "    <key>iTunes Library XML Location:1</key>"
    186       "    <data>%s</data>"
    187       "  </dict>"
    188       "</plist>", EncodePath(library_xml).c_str());
    189   WritePrefFile(xml);
    190   TestFindITunesLibrary();
    191   EXPECT_TRUE(test_finder_callback_called());
    192   EXPECT_TRUE(EmptyResult());
    193 }
    194 
    195 }  // namespace
    196 
    197 }  // namespace itunes
    198