summaryrefslogtreecommitdiffstats
path: root/chrome/utility/media_galleries/itunes_library_parser_unittest.cc
blob: cb772f4b99084c23a6ac07d8b0d7c7557fa0b186 (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// 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/logging.h"
#include "chrome/common/media_galleries/itunes_library.h"
#include "chrome/utility/media_galleries/itunes_library_parser.h"
#include "testing/gtest/include/gtest/gtest.h"

#define SIMPLE_HEADER()         \
    "<plist>"                   \
    "  <dict>"                  \
    "    <key>Tracks</key>"     \
    "    <dict>"

#define SIMPLE_TRACK(key, id, path, artist, album)                     \
    "<key>" #key "</key>"                                              \
    "<dict>"                                                           \
    "  <key>Track ID</key><integer>" #id "</integer>"                  \
    "  <key>Location</key><string>file://localhost/" path "</string>"  \
    "  <key>Artist</key><string>" artist "</string>"                   \
    "  <key>Album</key><string>" album "</string>"                     \
    "</dict>"

#define SIMPLE_FOOTER()  \
    "    </dict>"        \
    "  </dict>"          \
    "</plist>"

namespace itunes {

namespace {

void CompareTrack(const parser::Track& a, const parser::Track& b) {
  EXPECT_EQ(a.id, b.id);
  EXPECT_EQ(a.location.value(), b.location.value());
}

void CompareAlbum(const parser::Album& a, const parser::Album& b) {
  EXPECT_EQ(a.size(), b.size());

  parser::Album::const_iterator a_it;
  parser::Album::const_iterator b_it;
  for (a_it = a.begin(), b_it = b.begin();
       a_it != a.end() && b_it != b.end();
       ++a_it, ++b_it) {
    CompareTrack(*a_it, *b_it);
  }
}

void CompareAlbums(const parser::Albums& a, const parser::Albums& b) {
  EXPECT_EQ(a.size(), b.size());

  parser::Albums::const_iterator a_it;
  parser::Albums::const_iterator b_it;
  for (a_it = a.begin(), b_it = b.begin();
       a_it != a.end() && b_it != b.end();
       ++a_it, ++b_it) {
    EXPECT_EQ(a_it->first, b_it->first);
    CompareAlbum(a_it->second, b_it->second);
  }
}

void CompareLibrary(const parser::Library& a, const parser::Library& b) {
  EXPECT_EQ(a.size(), b.size());

  parser::Library::const_iterator a_it;
  parser::Library::const_iterator b_it;
  for (a_it = a.begin(), b_it = b.begin();
       a_it != a.end() && b_it != b.end();
       ++a_it, ++b_it) {
    EXPECT_EQ(a_it->first, b_it->first);
    CompareAlbums(a_it->second, b_it->second);
  }
}

class ITunesLibraryParserTest : public testing::Test {
 public:
  ITunesLibraryParserTest() {}

  void TestParser(bool expected_result, const std::string& xml) {
    ITunesLibraryParser parser;

    EXPECT_EQ(expected_result, parser.Parse(xml));
    if (!expected_result)
      return;

    CompareLibrary(expected_library_, parser.library());
  }

  void AddExpectedTrack(uint32 id, const std::string& location,
                        const std::string& artist, const std::string& album) {
    // On Mac this pretends that C: is a directory.
#if defined(OS_MACOSX)
    std::string os_location = "/" + location;
#else
    const std::string& os_location = location;
#endif
    parser::Track track(id, base::FilePath::FromUTF8Unsafe(os_location));
    expected_library_[artist][album].insert(track);
  }

 private:
  parser::Library expected_library_;

  DISALLOW_COPY_AND_ASSIGN(ITunesLibraryParserTest);
};

TEST_F(ITunesLibraryParserTest, EmptyLibrary) {
  TestParser(false, "");
}

TEST_F(ITunesLibraryParserTest, MinimalXML) {
  AddExpectedTrack(1, "C:/dir/Song With Space.mp3", "Artist A", "Album A");
  TestParser(
      true,
      SIMPLE_HEADER()
      SIMPLE_TRACK(1, 1, "C:/dir/Song%20With%20Space.mp3", "Artist A",
                   "Album A")
      SIMPLE_FOOTER());
}

TEST_F(ITunesLibraryParserTest, MultipleSongs) {
  AddExpectedTrack(1, "C:/dir/SongA1.mp3", "Artist A", "Album A");
  AddExpectedTrack(2, "C:/dir/SongA2.mp3", "Artist A", "Album A");
  AddExpectedTrack(3, "C:/dir/SongA3.mp3", "Artist A", "Album A");
  AddExpectedTrack(4, "C:/dir/SongB1.mp3", "Artist A", "Album B");
  AddExpectedTrack(5, "C:/dir/SongB2.mp3", "Artist A", "Album B");
  AddExpectedTrack(6, "C:/dir2/SongB1.mp3", "Artist B", "Album B");
  AddExpectedTrack(7, "C:/dir2/SongB2.mp3", "Artist B", "Album B");
  TestParser(
      true,
      SIMPLE_HEADER()
      SIMPLE_TRACK(1, 1, "C:/dir/SongA1.mp3", "Artist A", "Album A")
      SIMPLE_TRACK(2, 2, "C:/dir/SongA2.mp3", "Artist A", "Album A")
      SIMPLE_TRACK(3, 3, "C:/dir/SongA3.mp3", "Artist A", "Album A")
      SIMPLE_TRACK(4, 4, "C:/dir/SongB1.mp3", "Artist A", "Album B")
      SIMPLE_TRACK(5, 5, "C:/dir/SongB2.mp3", "Artist A", "Album B")
      SIMPLE_TRACK(6, 6, "C:/dir2/SongB1.mp3", "Artist B", "Album B")
      SIMPLE_TRACK(7, 7, "C:/dir2/SongB2.mp3", "Artist B", "Album B")
      SIMPLE_FOOTER());
}

TEST_F(ITunesLibraryParserTest, MismatchedId) {
  TestParser(
      false,
      SIMPLE_HEADER()
      SIMPLE_TRACK(1, 2, "C:/dir/SongA1.mp3", "Artist A", "Album A")
      SIMPLE_FOOTER());

  AddExpectedTrack(1, "C:/dir/SongA1.mp3", "Artist A", "Album A");
  TestParser(
      true,
      SIMPLE_HEADER()
      SIMPLE_TRACK(1, 1, "C:/dir/SongA1.mp3", "Artist A", "Album A")
      SIMPLE_TRACK(2, 3, "C:/dir/SongA2.mp3", "Artist A", "Album A")
      SIMPLE_FOOTER());
}

TEST_F(ITunesLibraryParserTest, OtherDictionaryEntries) {
  AddExpectedTrack(1, "C:/dir/SongA1.mp3", "Artist A", "Album A");
  TestParser(
      true,
      "<plist>"
      "  <dict>"
      "    <key>Other section</key>"
      "    <dict>"
      // In Other section, not Tracks.
      SIMPLE_TRACK(10, 10, "C:/dir/SongB2.mp3", "Artist B", "Album B")
      "    </dict>"
      "    <key>Tracks</key>"
      "    <dict>"
      "      <key>1</key>"
      "      <dict>"
      // In the body of a track dictionary before the interesting entries.
      SIMPLE_TRACK(20, 20, "C:/dir/SongB2.mp3", "Artist B", "Album B")
      // Entries in a different order.
      "        <key>Artist</key><string>Artist A</string>"
      "        <key>Location</key>"
      "          <string>file://localhost/C:/dir/SongA1.mp3</string>"
      "        <key>Album</key><string>Album A</string>"
      "        <key>Track ID</key><integer>1</integer>"
      // In the body of a track dictionary after the interesting entries.
      SIMPLE_TRACK(30, 30, "C:/dir/SongB3.mp3", "Artist B", "Album B")
      "      </dict>"
      "      <key>40</key>"
      "      <dict>"
      // Missing album name.
      "        <key>Artist</key><string>Artist B</string>"
      "        <key>Location</key>"
      "          <string>file://localhost/C:/dir/SongB4.mp3</string>"
      "        <key>Track ID</key><integer>1</integer>"
      "      </dict>"
      SIMPLE_FOOTER());
}

TEST_F(ITunesLibraryParserTest, MissingEntry) {
  AddExpectedTrack(1, "C:/dir/SongA1.mp3", "Artist A", "Album A");
  AddExpectedTrack(3, "C:/dir/SongA3.mp3", "Artist A", "Album A");
  TestParser(
      true,
      SIMPLE_HEADER()
      SIMPLE_TRACK(1, 1, "C:/dir/SongA1.mp3", "Artist A", "Album A")
      "<key>2</key><dict>"
      "  <key>Track ID</key><integer>2</integer>"
      "  <key>Album</key><string>Album A</string>"
      "  <key>Foo</key><string>Bar</string>"
      "   "  // A whitespace node is important for the test.
      "</dict>"
      SIMPLE_TRACK(3, 3, "C:/dir/SongA3.mp3", "Artist A", "Album A")
      SIMPLE_FOOTER());
}

TEST_F(ITunesLibraryParserTest, UnknownAlbumOrArtist) {
  AddExpectedTrack(1, "C:/dir/SongA1.mp3", "Artist A", "Unknown Album");
  AddExpectedTrack(2, "C:/dir/SongA2.mp3", "Unknown Artist", "Album A");
  AddExpectedTrack(3, "C:/dir/SongA3.mp3", "Unknown Artist", "Unknown Album");
  TestParser(
      true,
      SIMPLE_HEADER()
      "<key>1</key><dict>"
      "  <key>Track ID</key><integer>1</integer>"
      "  <key>Location</key><string>file://localhost/C:/dir/SongA1.mp3</string>"
      "  <key>Artist</key><string>Artist A</string>"
      "</dict>"
      "<key>2</key><dict>"
      "  <key>Track ID</key><integer>2</integer>"
      "  <key>Location</key><string>file://localhost/C:/dir/SongA2.mp3</string>"
      "  <key>Album</key><string>Album A</string>"
      "</dict>"
      "<key>3</key><dict>"
      "  <key>Track ID</key><integer>3</integer>"
      "  <key>Location</key><string>file://localhost/C:/dir/SongA3.mp3</string>"
      "</dict>"
      SIMPLE_FOOTER());
}

TEST_F(ITunesLibraryParserTest, AlbumArtist) {
  AddExpectedTrack(1, "C:/dir/SongA1.mp3", "Artist A", "Unknown Album");
  AddExpectedTrack(2, "C:/dir/SongA2.mp3", "Artist A", "Unknown Album");
  AddExpectedTrack(3, "C:/dir/SongA3.mp3", "Artist A", "Unknown Album");
  AddExpectedTrack(4, "C:/dir/SongA4.mp3", "Artist A", "Album");
  TestParser(
      true,
      SIMPLE_HEADER()
      "<key>1</key><dict>"
      "  <key>Track ID</key><integer>1</integer>"
      "  <key>Location</key><string>file://localhost/C:/dir/SongA1.mp3</string>"
      "  <key>Album Artist</key><string>Artist A</string>"
      "</dict>"
      "<key>2</key><dict>"
      "  <key>Track ID</key><integer>2</integer>"
      "  <key>Location</key><string>file://localhost/C:/dir/SongA2.mp3</string>"
      "  <key>Artist</key><string>Artist B</string>"
      "  <key>Album Artist</key><string>Artist A</string>"
      "</dict>"
      "<key>3</key><dict>"
      "  <key>Track ID</key><integer>3</integer>"
      "  <key>Location</key><string>file://localhost/C:/dir/SongA3.mp3</string>"
      "  <key>Album Artist</key><string>Artist A</string>"
      "  <key>Artist</key><string>Artist B</string>"
      "</dict>"
      "<key>4</key><dict>"
      "  <key>Track ID</key><integer>4</integer>"
      "  <key>Location</key><string>file://localhost/C:/dir/SongA4.mp3</string>"
      "  <key>Album</key><string>Album</string>"
      "  <key>Artist</key><string>Artist B</string>"
      "  <key>Album Artist</key><string>Artist A</string>"
      "</dict>"
      SIMPLE_FOOTER());
}

TEST_F(ITunesLibraryParserTest, MacPath) {
  AddExpectedTrack(1, "dir/Song With Space.mp3", "Artist A", "Album A");
  TestParser(
      true,
      SIMPLE_HEADER()
      // This path is concatenated with "http://localhost/", so no leading
      // slash should be used.
      SIMPLE_TRACK(1, 1, "dir/Song%20With%20Space.mp3", "Artist A", "Album A")
      SIMPLE_FOOTER());
}

}  // namespace

}  // namespace itunes