OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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/logging.h" |
| 6 #include "chrome/browser/media_galleries/fileapi/itunes_library_parser.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 |
| 9 #define SIMPLE_HEADER() \ |
| 10 "<plist>" \ |
| 11 " <dict>" \ |
| 12 " <key>Tracks</key>" \ |
| 13 " <dict>" |
| 14 |
| 15 #define SIMPLE_TRACK(key, id, path, artist, album) \ |
| 16 "<key>" #key "</key>" \ |
| 17 "<dict>" \ |
| 18 " <key>Track ID</key><integer>" #id "</integer>" \ |
| 19 " <key>Location</key><string>file://localhost/" path "</string>" \ |
| 20 " <key>Album Artist</key><string>" artist "</string>" \ |
| 21 " <key>Album</key><string>" album "</string>" \ |
| 22 "</dict>" |
| 23 |
| 24 #define SIMPLE_FOOTER() \ |
| 25 " </dict>" \ |
| 26 " </dict>" \ |
| 27 "</plist>" |
| 28 |
| 29 namespace itunes { |
| 30 |
| 31 namespace { |
| 32 |
| 33 void CompareTrack(const ITunesLibraryParser::Track& a, |
| 34 const ITunesLibraryParser::Track& b) { |
| 35 EXPECT_EQ(a.id, b.id); |
| 36 EXPECT_EQ(a.location, b.location); |
| 37 } |
| 38 |
| 39 void CompareAlbum(const ITunesLibraryParser::Album& a, |
| 40 const ITunesLibraryParser::Album& b) { |
| 41 EXPECT_EQ(a.size(), b.size()); |
| 42 |
| 43 ITunesLibraryParser::Album::const_iterator a_it; |
| 44 ITunesLibraryParser::Album::const_iterator b_it; |
| 45 for (a_it = a.begin(), b_it = b.begin(); |
| 46 a_it != a.end() && b_it != b.end(); |
| 47 ++a_it, ++b_it) { |
| 48 CompareTrack(*a_it, *b_it); |
| 49 } |
| 50 } |
| 51 |
| 52 void CompareAlbums(const ITunesLibraryParser::Albums& a, |
| 53 const ITunesLibraryParser::Albums& b) { |
| 54 EXPECT_EQ(a.size(), b.size()); |
| 55 |
| 56 ITunesLibraryParser::Albums::const_iterator a_it; |
| 57 ITunesLibraryParser::Albums::const_iterator b_it; |
| 58 for (a_it = a.begin(), b_it = b.begin(); |
| 59 a_it != a.end() && b_it != b.end(); |
| 60 ++a_it, ++b_it) { |
| 61 EXPECT_EQ(a_it->first, b_it->first); |
| 62 CompareAlbum(a_it->second, b_it->second); |
| 63 } |
| 64 } |
| 65 |
| 66 void CompareLibrary(const ITunesLibraryParser::Library& a, |
| 67 const ITunesLibraryParser::Library& b) { |
| 68 EXPECT_EQ(a.size(), b.size()); |
| 69 |
| 70 ITunesLibraryParser::Library::const_iterator a_it; |
| 71 ITunesLibraryParser::Library::const_iterator b_it; |
| 72 for (a_it = a.begin(), b_it = b.begin(); |
| 73 a_it != a.end() && b_it != b.end(); |
| 74 ++a_it, ++b_it) { |
| 75 EXPECT_EQ(a_it->first, b_it->first); |
| 76 CompareAlbums(a_it->second, b_it->second); |
| 77 } |
| 78 } |
| 79 |
| 80 class ITunesLibraryParserTest : public testing::Test { |
| 81 public: |
| 82 ITunesLibraryParserTest() {} |
| 83 |
| 84 void TestParser(bool expected_result, const std::string& xml) { |
| 85 ITunesLibraryParser parser; |
| 86 |
| 87 EXPECT_EQ(expected_result, parser.Parse(xml)); |
| 88 if (!expected_result) |
| 89 return; |
| 90 |
| 91 CompareLibrary(expected_library_, parser.library()); |
| 92 } |
| 93 |
| 94 void AddExpectedTrack(uint32 id, const std::string& location, |
| 95 const std::string& artist, const std::string& album) { |
| 96 ITunesLibraryParser::Track track(id, |
| 97 base::FilePath::FromUTF8Unsafe(location)); |
| 98 expected_library_[artist][album].insert(track); |
| 99 } |
| 100 |
| 101 private: |
| 102 ITunesLibraryParser::Library expected_library_; |
| 103 |
| 104 DISALLOW_COPY_AND_ASSIGN(ITunesLibraryParserTest); |
| 105 }; |
| 106 |
| 107 TEST_F(ITunesLibraryParserTest, EmptyLibrary) { |
| 108 TestParser(false, ""); |
| 109 } |
| 110 |
| 111 TEST_F(ITunesLibraryParserTest, MinimalXML) { |
| 112 AddExpectedTrack(1, "C:/dir/Song With Space.mp3", "Artist A", "Album A"); |
| 113 TestParser( |
| 114 true, |
| 115 SIMPLE_HEADER() |
| 116 SIMPLE_TRACK(1, 1, "C:/dir/Song%20With%20Space.mp3", "Artist A", |
| 117 "Album A") |
| 118 SIMPLE_FOOTER()); |
| 119 } |
| 120 |
| 121 TEST_F(ITunesLibraryParserTest, MultipleSongs) { |
| 122 AddExpectedTrack(1, "C:/dir/SongA1.mp3", "Artist A", "Album A"); |
| 123 AddExpectedTrack(2, "C:/dir/SongA2.mp3", "Artist A", "Album A"); |
| 124 AddExpectedTrack(3, "C:/dir/SongA3.mp3", "Artist A", "Album A"); |
| 125 AddExpectedTrack(4, "C:/dir/SongB1.mp3", "Artist A", "Album B"); |
| 126 AddExpectedTrack(5, "C:/dir/SongB2.mp3", "Artist A", "Album B"); |
| 127 AddExpectedTrack(6, "C:/dir2/SongB1.mp3", "Artist B", "Album B"); |
| 128 AddExpectedTrack(7, "C:/dir2/SongB2.mp3", "Artist B", "Album B"); |
| 129 TestParser( |
| 130 true, |
| 131 SIMPLE_HEADER() |
| 132 SIMPLE_TRACK(1, 1, "C:/dir/SongA1.mp3", "Artist A", "Album A") |
| 133 SIMPLE_TRACK(2, 2, "C:/dir/SongA2.mp3", "Artist A", "Album A") |
| 134 SIMPLE_TRACK(3, 3, "C:/dir/SongA3.mp3", "Artist A", "Album A") |
| 135 SIMPLE_TRACK(4, 4, "C:/dir/SongB1.mp3", "Artist A", "Album B") |
| 136 SIMPLE_TRACK(5, 5, "C:/dir/SongB2.mp3", "Artist A", "Album B") |
| 137 SIMPLE_TRACK(6, 6, "C:/dir2/SongB1.mp3", "Artist B", "Album B") |
| 138 SIMPLE_TRACK(7, 7, "C:/dir2/SongB2.mp3", "Artist B", "Album B") |
| 139 SIMPLE_FOOTER()); |
| 140 } |
| 141 |
| 142 TEST_F(ITunesLibraryParserTest, MismatchedId) { |
| 143 TestParser( |
| 144 false, |
| 145 SIMPLE_HEADER() |
| 146 SIMPLE_TRACK(1, 2, "C:/dir/SongA1.mp3", "Artist A", "Album A") |
| 147 SIMPLE_FOOTER()); |
| 148 |
| 149 AddExpectedTrack(1, "C:/dir/SongA1.mp3", "Artist A", "Album A"); |
| 150 TestParser( |
| 151 true, |
| 152 SIMPLE_HEADER() |
| 153 SIMPLE_TRACK(1, 1, "C:/dir/SongA1.mp3", "Artist A", "Album A") |
| 154 SIMPLE_TRACK(2, 3, "C:/dir/SongA2.mp3", "Artist A", "Album A") |
| 155 SIMPLE_FOOTER()); |
| 156 } |
| 157 |
| 158 TEST_F(ITunesLibraryParserTest, OtherDictionaryEntries) { |
| 159 AddExpectedTrack(1, "C:/dir/SongA1.mp3", "Artist A", "Album A"); |
| 160 TestParser( |
| 161 true, |
| 162 "<plist>" |
| 163 " <dict>" |
| 164 " <key>Other section</key>" |
| 165 " <dict>" |
| 166 // In Other section, not Tracks. |
| 167 SIMPLE_TRACK(10, 10, "C:/dir/SongB2.mp3", "Artist B", "Album B") |
| 168 " </dict>" |
| 169 " <key>Tracks</key>" |
| 170 " <dict>" |
| 171 " <key>1</key>" |
| 172 " <dict>" |
| 173 // In the body of a track dictionary before the interesting entries. |
| 174 SIMPLE_TRACK(20, 20, "C:/dir/SongB2.mp3", "Artist B", "Album B") |
| 175 // Entries in a different order. |
| 176 " <key>Album Artist</key><string>Artist A</string>" |
| 177 " <key>Location</key>" |
| 178 " <string>file://localhost/C:/dir/SongA1.mp3</string>" |
| 179 " <key>Album</key><string>Album A</string>" |
| 180 " <key>Track ID</key><integer>1</integer>" |
| 181 // In the body of a track dictionary after the interesting entries. |
| 182 SIMPLE_TRACK(30, 30, "C:/dir/SongB3.mp3", "Artist B", "Album B") |
| 183 " </dict>" |
| 184 " <key>40</key>" |
| 185 " <dict>" |
| 186 // Missing album name. |
| 187 " <key>Album Artist</key><string>Artist B</string>" |
| 188 " <key>Location</key>" |
| 189 " <string>file://localhost/C:/dir/SongB4.mp3</string>" |
| 190 " <key>Track ID</key><integer>1</integer>" |
| 191 " </dict>" |
| 192 SIMPLE_FOOTER()); |
| 193 } |
| 194 |
| 195 } // namespace |
| 196 |
| 197 } // namespace itunes |
OLD | NEW |