| 1 | /*************************************************************************** |
| 2 | copyright : (C) 2012 by Michael Helmling |
| 3 | email : [email protected] |
| 4 | ***************************************************************************/ |
| 5 | |
| 6 | /*************************************************************************** |
| 7 | * This library is free software; you can redistribute it and/or modify * |
| 8 | * it under the terms of the GNU Lesser General Public License version * |
| 9 | * 2.1 as published by the Free Software Foundation. * |
| 10 | * * |
| 11 | * This library is distributed in the hope that it will be useful, but * |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 | * Lesser General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU Lesser General Public * |
| 17 | * License along with this library; if not, write to the Free Software * |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * |
| 19 | * 02110-1301 USA * |
| 20 | * * |
| 21 | * Alternatively, this file is available under the Mozilla Public * |
| 22 | * License Version 1.1. You may obtain a copy of the License at * |
| 23 | * http://www.mozilla.org/MPL/ * |
| 24 | ***************************************************************************/ |
| 25 | |
| 26 | #include "taglib_config.h" |
| 27 | #include "tpropertymap.h" |
| 28 | #include "tag.h" |
| 29 | #include "id3v1tag.h" |
| 30 | #include "id3v2tag.h" |
| 31 | #ifdef TAGLIB_WITH_APE |
| 32 | #include "apetag.h" |
| 33 | #endif |
| 34 | #ifdef TAGLIB_WITH_ASF |
| 35 | #include "asftag.h" |
| 36 | #endif |
| 37 | #ifdef TAGLIB_WITH_RIFF |
| 38 | #include "infotag.h" |
| 39 | #endif |
| 40 | #ifdef TAGLIB_WITH_MP4 |
| 41 | #include "mp4tag.h" |
| 42 | #endif |
| 43 | #ifdef TAGLIB_WITH_VORBIS |
| 44 | #include "xiphcomment.h" |
| 45 | #endif |
| 46 | #include <cppunit/extensions/HelperMacros.h> |
| 47 | #include "utils.h" |
| 48 | |
| 49 | using namespace TagLib; |
| 50 | |
| 51 | class TestPropertyMap : public CppUnit::TestFixture |
| 52 | { |
| 53 | CPPUNIT_TEST_SUITE(TestPropertyMap); |
| 54 | CPPUNIT_TEST(testInvalidKeys); |
| 55 | CPPUNIT_TEST(testGetSetId3v1); |
| 56 | CPPUNIT_TEST(testGetSetId3v2); |
| 57 | CPPUNIT_TEST(testGetSet); |
| 58 | #ifdef TAGLIB_WITH_APE |
| 59 | CPPUNIT_TEST(testGetSetApe); |
| 60 | #endif |
| 61 | #ifdef TAGLIB_WITH_ASF |
| 62 | CPPUNIT_TEST(testGetSetAsf); |
| 63 | #endif |
| 64 | #ifdef TAGLIB_WITH_RIFF |
| 65 | CPPUNIT_TEST(testGetSetInfo); |
| 66 | #endif |
| 67 | #ifdef TAGLIB_WITH_MP4 |
| 68 | CPPUNIT_TEST(testGetSetMp4); |
| 69 | #endif |
| 70 | #ifdef TAGLIB_WITH_VORBIS |
| 71 | CPPUNIT_TEST(testGetSetXiphComment); |
| 72 | #endif |
| 73 | CPPUNIT_TEST_SUITE_END(); |
| 74 | |
| 75 | public: |
| 76 | void testInvalidKeys() |
| 77 | { |
| 78 | PropertyMap map1; |
| 79 | CPPUNIT_ASSERT(map1.isEmpty()); |
| 80 | map1[L"\x00c4\x00d6\x00dc"].append("test"); |
| 81 | CPPUNIT_ASSERT_EQUAL(map1.size(), 1u); |
| 82 | |
| 83 | PropertyMap map2; |
| 84 | map2[L"\x00c4\x00d6\x00dc"].append("test"); |
| 85 | CPPUNIT_ASSERT(map1 == map2); |
| 86 | CPPUNIT_ASSERT(map1.contains(map2)); |
| 87 | |
| 88 | map2["ARTIST"] = String("Test Artist"); |
| 89 | CPPUNIT_ASSERT(map1 != map2); |
| 90 | CPPUNIT_ASSERT(map2.contains(map1)); |
| 91 | |
| 92 | map2[L"\x00c4\x00d6\x00dc"].append("test 2"); |
| 93 | CPPUNIT_ASSERT(!map2.contains(map1)); |
| 94 | |
| 95 | } |
| 96 | |
| 97 | template <typename T> |
| 98 | void tagGetSet() |
| 99 | { |
| 100 | T tag; |
| 101 | |
| 102 | tag.setTitle("Test Title"); |
| 103 | tag.setArtist("Test Artist"); |
| 104 | tag.setAlbum("Test Album"); |
| 105 | tag.setYear(2015); |
| 106 | tag.setTrack(10); |
| 107 | |
| 108 | { |
| 109 | PropertyMap prop = tag.properties(); |
| 110 | CPPUNIT_ASSERT_EQUAL(String("Test Title"), prop["TITLE" ].front()); |
| 111 | CPPUNIT_ASSERT_EQUAL(String("Test Artist"), prop["ARTIST" ].front()); |
| 112 | CPPUNIT_ASSERT_EQUAL(String("Test Album"), prop["ALBUM" ].front()); |
| 113 | CPPUNIT_ASSERT_EQUAL(String("2015"), prop["DATE" ].front()); |
| 114 | CPPUNIT_ASSERT_EQUAL(String("10"), prop["TRACKNUMBER"].front()); |
| 115 | |
| 116 | prop["TITLE" ].front() = "Test Title 2"; |
| 117 | prop["ARTIST" ].front() = "Test Artist 2"; |
| 118 | prop["TRACKNUMBER"].front() = "5"; |
| 119 | |
| 120 | tag.setProperties(prop); |
| 121 | } |
| 122 | |
| 123 | CPPUNIT_ASSERT_EQUAL(String("Test Title 2"), tag.title()); |
| 124 | CPPUNIT_ASSERT_EQUAL(String("Test Artist 2"), tag.artist()); |
| 125 | CPPUNIT_ASSERT_EQUAL(5U, tag.track()); |
| 126 | CPPUNIT_ASSERT(!tag.isEmpty()); |
| 127 | |
| 128 | PropertyMap props = tag.properties(); |
| 129 | CPPUNIT_ASSERT_EQUAL(StringList("Test Artist 2"), props.find("ARTIST")->second); |
| 130 | CPPUNIT_ASSERT(props.find("COMMENT") == props.end()); |
| 131 | props.replace("ARTIST", StringList("Test Artist 3")); |
| 132 | CPPUNIT_ASSERT_EQUAL(StringList("Test Artist 3"), props["ARTIST"]); |
| 133 | |
| 134 | PropertyMap eraseMap; |
| 135 | eraseMap.insert("ARTIST", StringList()); |
| 136 | eraseMap.insert("ALBUM", StringList()); |
| 137 | eraseMap.insert("TITLE", StringList()); |
| 138 | props.erase(eraseMap); |
| 139 | CPPUNIT_ASSERT_EQUAL(String("DATE=2015\nTRACKNUMBER=5\n"), props.toString()); |
| 140 | |
| 141 | tag.setProperties(PropertyMap()); |
| 142 | |
| 143 | CPPUNIT_ASSERT(tag.isEmpty()); |
| 144 | CPPUNIT_ASSERT(tag.properties().isEmpty()); |
| 145 | CPPUNIT_ASSERT_EQUAL(String(""), tag.title()); |
| 146 | CPPUNIT_ASSERT_EQUAL(String(""), tag.artist()); |
| 147 | CPPUNIT_ASSERT_EQUAL(String(""), tag.album()); |
| 148 | CPPUNIT_ASSERT_EQUAL(String(""), tag.comment()); |
| 149 | CPPUNIT_ASSERT_EQUAL(String(""), tag.genre()); |
| 150 | CPPUNIT_ASSERT_EQUAL(0U, tag.track()); |
| 151 | CPPUNIT_ASSERT(tag.isEmpty()); |
| 152 | CPPUNIT_ASSERT(tag.properties().isEmpty()); |
| 153 | } |
| 154 | |
| 155 | void testGetSetId3v1() |
| 156 | { |
| 157 | tagGetSet<ID3v1::Tag>(); |
| 158 | } |
| 159 | |
| 160 | void testGetSetId3v2() |
| 161 | { |
| 162 | tagGetSet<ID3v2::Tag>(); |
| 163 | } |
| 164 | |
| 165 | #ifdef TAGLIB_WITH_VORBIS |
| 166 | void testGetSetXiphComment() |
| 167 | { |
| 168 | tagGetSet<Ogg::XiphComment>(); |
| 169 | } |
| 170 | #endif |
| 171 | |
| 172 | #ifdef TAGLIB_WITH_APE |
| 173 | void testGetSetApe() |
| 174 | { |
| 175 | tagGetSet<APE::Tag>(); |
| 176 | } |
| 177 | #endif |
| 178 | |
| 179 | #ifdef TAGLIB_WITH_ASF |
| 180 | void testGetSetAsf() |
| 181 | { |
| 182 | tagGetSet<ASF::Tag>(); |
| 183 | } |
| 184 | #endif |
| 185 | |
| 186 | #ifdef TAGLIB_WITH_MP4 |
| 187 | void testGetSetMp4() |
| 188 | { |
| 189 | tagGetSet<MP4::Tag>(); |
| 190 | } |
| 191 | #endif |
| 192 | |
| 193 | #ifdef TAGLIB_WITH_RIFF |
| 194 | void testGetSetInfo() |
| 195 | { |
| 196 | tagGetSet<RIFF::Info::Tag>(); |
| 197 | } |
| 198 | #endif |
| 199 | |
| 200 | void testGetSet() |
| 201 | { |
| 202 | PropertyMap props; |
| 203 | props["Title"] = String("Test Title"); |
| 204 | StringList artists("Artist 1"); |
| 205 | artists.append("Artist 2"); |
| 206 | props.insert("Artist", artists); |
| 207 | CPPUNIT_ASSERT_EQUAL(StringList("Test Title"), props.value("TITLE")); |
| 208 | CPPUNIT_ASSERT_EQUAL(StringList("Test Title"), props.value("Title")); |
| 209 | CPPUNIT_ASSERT_EQUAL(StringList("Test Title"), props["TITLE"]); |
| 210 | CPPUNIT_ASSERT_EQUAL(StringList("Test Title"), props["Title"]); |
| 211 | CPPUNIT_ASSERT(props.contains("title")); |
| 212 | CPPUNIT_ASSERT_EQUAL(StringList("Test Title"), props.find("TITLE")->second); |
| 213 | CPPUNIT_ASSERT_EQUAL(2U, props.size()); |
| 214 | CPPUNIT_ASSERT(!props.isEmpty()); |
| 215 | props.clear(); |
| 216 | CPPUNIT_ASSERT(props.isEmpty()); |
| 217 | CPPUNIT_ASSERT_EQUAL(StringList(), props.value("TITLE")); |
| 218 | CPPUNIT_ASSERT_EQUAL(StringList(), props.value("Title")); |
| 219 | CPPUNIT_ASSERT_EQUAL(artists, props.value("Title", artists)); |
| 220 | CPPUNIT_ASSERT(!props.contains("title")); |
| 221 | CPPUNIT_ASSERT(props.find("TITLE") == props.end()); |
| 222 | CPPUNIT_ASSERT_EQUAL(0U, props.size()); |
| 223 | CPPUNIT_ASSERT(props.isEmpty()); |
| 224 | CPPUNIT_ASSERT_EQUAL(StringList(), props["TITLE"]); |
| 225 | CPPUNIT_ASSERT_EQUAL(StringList(), props["Title"]); |
| 226 | CPPUNIT_ASSERT(props.contains("title")); |
| 227 | CPPUNIT_ASSERT(props.find("TITLE") != props.end()); |
| 228 | CPPUNIT_ASSERT_EQUAL(1U, props.size()); |
| 229 | CPPUNIT_ASSERT(!props.isEmpty()); |
| 230 | } |
| 231 | |
| 232 | }; |
| 233 | |
| 234 | CPPUNIT_TEST_SUITE_REGISTRATION(TestPropertyMap); |