| 1 | /*************************************************************************** |
| 2 | copyright : (C) 2010 by Lukas Lalinsky |
| 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 <string> |
| 27 | #include <cstdio> |
| 28 | |
| 29 | #include "tstringlist.h" |
| 30 | #include "tpropertymap.h" |
| 31 | #include "tag.h" |
| 32 | #include "apefile.h" |
| 33 | #include "apetag.h" |
| 34 | #include <cppunit/extensions/HelperMacros.h> |
| 35 | #include "utils.h" |
| 36 | |
| 37 | using namespace std; |
| 38 | using namespace TagLib; |
| 39 | |
| 40 | class TestAPETag : public CppUnit::TestFixture |
| 41 | { |
| 42 | CPPUNIT_TEST_SUITE(TestAPETag); |
| 43 | CPPUNIT_TEST(testIsEmpty); |
| 44 | CPPUNIT_TEST(testIsEmpty2); |
| 45 | CPPUNIT_TEST(testPropertyInterface1); |
| 46 | CPPUNIT_TEST(testPropertyInterface2); |
| 47 | CPPUNIT_TEST(testInvalidKeys); |
| 48 | CPPUNIT_TEST(testTextBinary); |
| 49 | CPPUNIT_TEST(testID3v1Collision); |
| 50 | CPPUNIT_TEST_SUITE_END(); |
| 51 | |
| 52 | public: |
| 53 | |
| 54 | void testIsEmpty() |
| 55 | { |
| 56 | APE::Tag tag; |
| 57 | CPPUNIT_ASSERT(tag.isEmpty()); |
| 58 | tag.addValue("COMPOSER", "Mike Oldfield"); |
| 59 | CPPUNIT_ASSERT(!tag.isEmpty()); |
| 60 | } |
| 61 | |
| 62 | void testIsEmpty2() |
| 63 | { |
| 64 | APE::Tag tag; |
| 65 | CPPUNIT_ASSERT(tag.isEmpty()); |
| 66 | tag.setArtist("Mike Oldfield"); |
| 67 | CPPUNIT_ASSERT(!tag.isEmpty()); |
| 68 | } |
| 69 | |
| 70 | void testPropertyInterface1() |
| 71 | { |
| 72 | APE::Tag tag; |
| 73 | PropertyMap dict = tag.properties(); |
| 74 | CPPUNIT_ASSERT(dict.isEmpty()); |
| 75 | dict["ARTIST"] = String("artist 1"); |
| 76 | dict["ARTIST"].append("artist 2"); |
| 77 | dict["TRACKNUMBER"].append("17"); |
| 78 | tag.setProperties(dict); |
| 79 | CPPUNIT_ASSERT_EQUAL(String("17"), tag.itemListMap()["TRACK"].values()[0]); |
| 80 | CPPUNIT_ASSERT_EQUAL(2u, tag.itemListMap()["ARTIST"].values().size()); |
| 81 | CPPUNIT_ASSERT_EQUAL(String("artist 1 / artist 2"), tag.artist()); |
| 82 | CPPUNIT_ASSERT_EQUAL(17u, tag.track()); |
| 83 | const APE::Item &textItem = tag.itemListMap()["TRACK"]; |
| 84 | CPPUNIT_ASSERT_EQUAL(APE::Item::Text, textItem.type()); |
| 85 | CPPUNIT_ASSERT(!textItem.isEmpty()); |
| 86 | CPPUNIT_ASSERT_EQUAL(9 + 5 + 2, textItem.size()); |
| 87 | } |
| 88 | |
| 89 | void testPropertyInterface2() |
| 90 | { |
| 91 | APE::Tag tag; |
| 92 | APE::Item item1("TRACK", String("17")); |
| 93 | tag.setItem("TRACK", item1); |
| 94 | |
| 95 | APE::Item item2; |
| 96 | item2.setType(APE::Item::Binary); |
| 97 | ByteVector binaryData1("first"); |
| 98 | item2.setBinaryData(binaryData1); |
| 99 | tag.setItem("TESTBINARY", item2); |
| 100 | |
| 101 | PropertyMap properties = tag.properties(); |
| 102 | CPPUNIT_ASSERT_EQUAL(1u, properties.unsupportedData().size()); |
| 103 | CPPUNIT_ASSERT(properties.contains("TRACKNUMBER")); |
| 104 | CPPUNIT_ASSERT(!properties.contains("TRACK")); |
| 105 | CPPUNIT_ASSERT(tag.itemListMap().contains("TESTBINARY")); |
| 106 | CPPUNIT_ASSERT_EQUAL(binaryData1, |
| 107 | tag.itemListMap()["TESTBINARY"].binaryData()); |
| 108 | ByteVector binaryData2("second"); |
| 109 | tag.setData("TESTBINARY", binaryData2); |
| 110 | const APE::Item &binaryItem = tag.itemListMap()["TESTBINARY"]; |
| 111 | CPPUNIT_ASSERT_EQUAL(APE::Item::Binary, binaryItem.type()); |
| 112 | CPPUNIT_ASSERT(!binaryItem.isEmpty()); |
| 113 | CPPUNIT_ASSERT_EQUAL(9 + 10 + static_cast<int>(binaryData2.size()), |
| 114 | binaryItem.size()); |
| 115 | CPPUNIT_ASSERT_EQUAL(binaryData2, binaryItem.binaryData()); |
| 116 | |
| 117 | tag.removeUnsupportedProperties(properties.unsupportedData()); |
| 118 | CPPUNIT_ASSERT(!tag.itemListMap().contains("TESTBINARY")); |
| 119 | |
| 120 | APE::Item item3("TRACKNUMBER", String("29")); |
| 121 | tag.setItem("TRACKNUMBER", item3); |
| 122 | properties = tag.properties(); |
| 123 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), properties["TRACKNUMBER"].size()); |
| 124 | CPPUNIT_ASSERT_EQUAL(String("17"), properties["TRACKNUMBER"][0]); |
| 125 | CPPUNIT_ASSERT_EQUAL(String("29"), properties["TRACKNUMBER"][1]); |
| 126 | |
| 127 | } |
| 128 | |
| 129 | void testInvalidKeys() |
| 130 | { |
| 131 | PropertyMap properties; |
| 132 | properties["A"] = String("invalid key: one character"); |
| 133 | properties["MP+"] = String("invalid key: forbidden string"); |
| 134 | properties[L"\x1234\x3456"] = String("invalid key: Unicode"); |
| 135 | properties["A B~C"] = String("valid key: space and tilde"); |
| 136 | properties["ARTIST"] = String("valid key: normal one"); |
| 137 | |
| 138 | APE::Tag tag; |
| 139 | PropertyMap unsuccessful = tag.setProperties(properties); |
| 140 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), unsuccessful.size()); |
| 141 | CPPUNIT_ASSERT(unsuccessful.contains("A")); |
| 142 | CPPUNIT_ASSERT(unsuccessful.contains("MP+")); |
| 143 | CPPUNIT_ASSERT(unsuccessful.contains(L"\x1234\x3456")); |
| 144 | |
| 145 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), tag.itemListMap().size()); |
| 146 | tag.addValue("VALID KEY", "Test Value 1"); |
| 147 | tag.addValue("INVALID KEY \x7f", "Test Value 2"); |
| 148 | tag.addValue(L"INVALID KEY \x1234\x3456", "Test Value 3"); |
| 149 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), tag.itemListMap().size()); |
| 150 | } |
| 151 | |
| 152 | void testTextBinary() |
| 153 | { |
| 154 | APE::Item item("DUMMY", String("Test Text")); |
| 155 | CPPUNIT_ASSERT_EQUAL(String("Test Text"), item.toString()); |
| 156 | CPPUNIT_ASSERT_EQUAL(ByteVector(), item.binaryData()); |
| 157 | |
| 158 | ByteVector data("Test Data"); |
| 159 | item.setBinaryData(data); |
| 160 | CPPUNIT_ASSERT(item.values().isEmpty()); |
| 161 | CPPUNIT_ASSERT_EQUAL(String(), item.toString()); |
| 162 | CPPUNIT_ASSERT_EQUAL(data, item.binaryData()); |
| 163 | |
| 164 | item.setValue("Test Text 2"); |
| 165 | CPPUNIT_ASSERT_EQUAL(String("Test Text 2"), item.toString()); |
| 166 | CPPUNIT_ASSERT_EQUAL(ByteVector(), item.binaryData()); |
| 167 | } |
| 168 | |
| 169 | void testID3v1Collision() |
| 170 | { |
| 171 | ScopedFileCopy copy("no-tags", ".mpc"); |
| 172 | string newname = copy.fileName(); |
| 173 | |
| 174 | { |
| 175 | APE::File f(newname.c_str()); |
| 176 | f.APETag(true)->setArtist("Filltointersect "); |
| 177 | f.APETag()->setTitle("Filltointersect "); |
| 178 | f.save(); |
| 179 | } |
| 180 | |
| 181 | { |
| 182 | APE::File f(newname.c_str()); |
| 183 | CPPUNIT_ASSERT(!f.hasID3v1Tag()); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | }; |
| 188 | |
| 189 | CPPUNIT_TEST_SUITE_REGISTRATION(TestAPETag); |